
    7bhg8                         d Z ddlZddlZddlZddlZddlZddlZdZd Z G d dej                        Z
edk(  r ej                          yy)a  Test correct treatment of various string literals by the parser.

There are four types of string literals:

    'abc'             -- normal str
    r'abc'            -- raw str
    b'xyz'            -- normal bytes
    br'xyz' | rb'xyz' -- raw bytes

The difference between normal and raw strings is of course that in a
raw string, \ escapes (while still used to determine the end of the
literal) are not interpreted, so that r'\x00' contains four
characters: a backslash, an x, and two zeros; while '\x00' contains a
single character (code point zero).

The tricky thing is what should happen when non-ASCII bytes are used
inside literals.  For bytes literals, this is considered illegal.  But
for str literals, those bytes are supposed to be decoded using the
encoding declared for the file (UTF-8 by default).

We have to test this with various file encodings.  We also test it with
exec()/eval(), which uses a different code path.

This file is really about correct treatment of encodings and
backslashes.  It doesn't concern itself with issues like single
vs. double quotes or singly- vs. triply-quoted strings: that's dealt
with elsewhere (I assume).
    Na  # coding: %s
a = 'x'
assert ord(a) == 120
b = '\x01'
assert ord(b) == 1
c = r'\x01'
assert list(map(ord, c)) == [92, 120, 48, 49]
d = '\x81'
assert ord(d) == 0x81
e = r'\x81'
assert list(map(ord, e)) == [92, 120, 56, 49]
f = '\u1881'
assert ord(f) == 0x1881
g = r'\u1881'
assert list(map(ord, g)) == [92, 117, 49, 56, 56, 49]
h = '\U0001d120'
assert ord(h) == 0x1d120
i = r'\U0001d120'
assert list(map(ord, i)) == [92, 85, 48, 48, 48, 49, 100, 49, 50, 48]
c                     t        | g      S N)bytes)is    C/opt/python-3.12.12/usr/lib/python3.12/test/test_string_literals.pybyter   <   s    !:    c                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd ZddZd Zd Zd Zd Zd Zd Zy)TestLiteralsc                     t         j                  d d  | _        t        j                         | _        t         j                  j                  d| j
                         y )Nr   )syspath	save_pathtempfilemkdtemptmpdirinsertselfs    r   setUpzTestLiterals.setUpB   s7    !&&(4;;'r	   c                     | j                   t        j                  d d  t        j                  | j
                  d       y )NT)ignore_errors)r   r   r   shutilrmtreer   r   s    r   tearDownzTestLiterals.tearDownG   s%    nndkk6r	   c                     t         D ]  } y r   )TEMPLATE)r   cs     r   test_templatezTestLiterals.test_templateK   s     Ar	   c                 z   | j                  t        d      d       | j                  t        d      t        d             | j                  t        d      t        d             | j                  t        d      t        d             | j                  t        d      t        d             | j                  t        d	      t        d
             | j                  t        d      t        d
             | j                  t        d      t        d             | j                  t        d      t        d             y )Nz 'x' xz '\x01'    z '' z '\x81'    u    '' z
 '\u1881'   u    'ᢁ' z '\U0001d120'   u    '𝄠' assertEqualevalchrr   s    r   test_eval_str_normalz!TestLiterals.test_eval_str_normalQ   s    k*C0o.A7n-s1v6o.D	:n-s4y9/0#f+>./V=34c'lC23S\Br	   c                    | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d	       | j                  t        t        d
       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       y )Nz '\x' z '\x0' z '\u' z '\u0' z '\u00' z	 '\u000' z '\U' z '\U0' z '\U00' z	 '\U000' z
 '\U0000' z '\U00000' z '\U000000' z '\U0000000' assertRaisesSyntaxErrorr(   r   s    r   test_eval_str_incompletez%TestLiterals.test_eval_str_incomplete\   s   +t];+t^<+t];+t^<+t_=+t-=>+t];+t^<+t_=+t-=>+t->?+t-?@+t-@A+t-ABr	   c           	         t        dd      D ]O  }|dv r| j                  t              5  | j                  t	        d|z        dt        |      z          d d d        Q t        j                  d      5 }t        j                  dt        	       t	        d
       d d d        | j                  t              d       | j                  t        |d   j                        d       | j                  |d   j                  d       | j                  |d   j                  d       t        j                  d      5 }t        j                  dt        	       | j                  t              5 }t	        d
       d d d        j                   }d d d        | j                  |g        | j                  j"                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j$                  d       t        j                  d      5 }t        j                  dt        	       | j                  t              5 }t	        d       d d d        j                   }d d d        | j                  t        |      d       | j                  |d   j&                  t               | j)                  t        |d   j                        d       | j                  |d   j                  d       y # 1 sw Y   xY w# 1 sw Y   ~xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w)Nr"      s   
"'01234567NU\abfnrtuvxz'\%c'\Trecordalwayscategoryz	'''
\z'''r   invalid escape sequence '\z'<string>   errorz'\e' $zinvalid escape sequence)rangeassertWarnsSyntaxWarningr'   r(   r)   warningscatch_warningssimplefilterlenstrmessagefilenamelinenor-   r.   	exceptionmsgoffsetr7   assertRegexr   bwcmexcs        r   test_eval_str_invalid_escapez)TestLiterals.test_eval_str_invalid_escapel   s}   q#A66!!-0  hl!3TCF]C 10  $$D1Q!!(]C 2 	Q#QqT\\*,KL1
31a($$D1Q!!'MB"";/2]# 0,,C	 2
 	B"ABz2Q'Q' $$D1Q!!(]C"";/2Y 0,,C	 2
 	Q#16QqT\\*,EF1
3A 10 21 0/ 21 0/ 21s_   +L5'L;1L2,L%8L2,1ML?)ML	L"%L/	*L22L<?M	MMc                    t        dd      D ]G  }| j                  t              5  | j                  t	        d|z        t        |             d d d        I t        j                  d      5 }t        j                  dt               t	        d       d d d        | j                  t              d	       | j                  t        |d
   j                        d       | j                  |d
   j                  d       | j                  |d
   j                  d       t        j                  d      5 }t        j                  dt               | j                  t              5 }t	        d       d d d        j                   }d d d        | j                  |g        | j                  j"                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j$                  d	       y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w)N      z'\%o'Tr3   r5   r6   z'''
\407'''r"   r   $invalid octal escape sequence '\407'r9   r:   r;   )r<   r=   r>   r'   r(   r)   r?   r@   rA   rB   rC   rD   rE   rF   r-   r.   rG   rH   rI   r   r   rM   rN   rO   s        r   "test_eval_str_invalid_octal_escapez/TestLiterals.test_eval_str_invalid_octal_escape   s   uf%A!!-0  hl!3SV< 10 & $$D1Q!!(]C! 2 	Q#QqT\\*@	B1
31a($$D1Q!!'MB"";/2_% 0,,C	 2
 	B"IJz2Q'Q'+ 10 21 0/ 21s;   (H-'H31H5$H)0H5H	H&)H2	.H55H>c                 ,   t        j                  d      5 }t        j                  dt               | j	                  t
              5 }t        d       d d d        j                  }d d d        | j                  g        | j                  j                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j                  d	       t        j                  d      5 }t        j                  dt               | j	                  t
              5 }t        d
       d d d        j                  }d d d        | j                  |g        | j                  |j                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j                  d       y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w)NTr3   r;   r6   z&"'''''''''''''''''''''invalid\ Escape"zinvalid escape sequence '\ 'r9   r"      z"''Incorrect \ logic?"   )r?   r@   rA   r>   r-   r.   r(   rG   r'   rH   rE   rF   rI   )r   rM   rN   rO   s       r   )test_invalid_escape_locations_with_offsetz6TestLiterals.test_invalid_escape_locations_with_offset   s   $$D1Q!!'MB"";/2@A 0,,C	 2
 	B"ABz2Q'R($$D1Q!!'MB"";/201 0,,C	 2
 	B"ABz2Q'R(% 0/ 21 0/ 21sG   1G1G$G11H
9G>H
$G.	)G11G;>H	H

Hc                 2   | j                  t        d      d       | j                  t        d      d       | j                  t        d      t        d             | j                  t        d      d       | j                  t        d	      t        d
             | j                  t        d      d       | j                  t        d      t        d             | j                  t        d      d       | j                  t        d      t        d             y )Nz r'x' r!   z	 r'\x01' \x01z r'' r"   z	 r'\x81' z\x81u    r'' r#   z r'\u1881' z\u1881u    r'ᢁ' r$   z r'\U0001d120' z
\U0001d120u	    r'𝄠' r%   r&   r   s    r   test_eval_str_rawzTestLiterals.test_eval_str_raw   s    l+S1./>o.A7./>o.D	:01>B/0#f+>457IJ34c'lCr	   c                     | j                  t        d      d       | j                  t        d      t        d             | j                  t        d      t        d             | j                  t        d      t        d             | j                  t        t        d       | j                  t        d	      d
       | j                  t        t        d       | j                  t        d      d       | j                  t        t        d       y )Nz b'x'    xz	 b'\x01' r"   z b'' z	 b'\x81' r#   u    b''  br'\u1881'    \u1881u    b'ᢁ'  br'\U0001d120' 
   \U0001d120u	    b'𝄠' r'   r(   r   r-   r.   r   s    r   test_eval_bytes_normalz#TestLiterals.test_eval_bytes_normal   s    l+T2./a9o.Q8./d<+t_=124DE+t->?568LM+t-BCr	   c                 p    | j                  t        t        d       | j                  t        t        d       y )Nz b'\x' z b'\x0' r,   r   s    r   test_eval_bytes_incompletez'TestLiterals.test_eval_bytes_incomplete   s&    +t^<+t_=r	   c           	      \   t        dd      D ]P  }|dv r| j                  t              5  | j                  t	        d|z        dt        |g      z          d d d        R t        j                  d      5 }t        j                  dt        	       t	        d
       d d d        | j                  t              d       | j                  t        |d   j                        d       | j                  |d   j                  d       | j                  |d   j                  d       t        j                  d      5 }t        j                  dt        	       | j                  t              5 }t	        d
       d d d        j                   }d d d        | j                  |g        | j                  j"                  d       | j                  |j                  d       | j                  |j                  d       y # 1 sw Y   xY w# 1 sw Y   nxY w# 1 sw Y   xY w# 1 sw Y   xY w)Nr"   r1   s   
"'01234567\abfnrtvxzb'\%c'   \Tr3   r5   r6   z
b'''
\z'''r   r8   r9   r:   r;   r<   r=   r>   r'   r(   r   r?   r@   rA   rB   rC   rD   rE   rF   r-   r.   rG   rH   rK   s        r   test_eval_bytes_invalid_escapez+TestLiterals.test_eval_bytes_invalid_escape   s   q#A33!!-0  i!m!4eeQCj6HI 10  $$D1Q!!(]C  2 	Q#QqT\\*,KL1
31a($$D1Q!!'MB"";/2^$ 0,,C	 2
 	B"ABz2Q'' 10 21 0/ 21s;   ,G<6'H	<1H"-H9H"<H		HH	H""H+c           	      R   t        dd      D ]K  }| j                  t              5  | j                  t	        d|z        t        |dz  g             d d d        M t        j                  d      5 }t        j                  dt               t	        d	       d d d        | j                  t              d
       | j                  t        |d   j                        d       | j                  |d   j                  d       | j                  |d   j                  d       t        j                  d      5 }t        j                  dt               | j                  t              5 }t	        d	       d d d        j                   }d d d        | j                  |g        | j                  j"                  d       | j                  |j                  d       | j                  |j                  d       y # 1 sw Y   xY w# 1 sw Y   nxY w# 1 sw Y   xY w# 1 sw Y   xY w)NrR   rS   zb'\%o'   Tr3   r5   r6   zb'''
\407'''r"   r   rT   r9   r:   r;   rj   rU   s        r   $test_eval_bytes_invalid_octal_escapez1TestLiterals.test_eval_bytes_invalid_octal_escape   s   uf%A!!-0  i!m!4eQYK6HI 10 & $$D1Q!!(]C!" 2 	Q#QqT\\*@	B1
31a($$D1Q!!'MB"";/2%& 0,,C	 2
 	B"IJz2Q') 10 21 0/ 21s;   ,G71'H71H(H4H7H	HH	HH&c                 8   | j                  t        d      d       | j                  t        d      d       | j                  t        d      d       | j                  t        d      d       | j                  t        d      t        d             | j                  t        d	      t        d             | j                  t        d
      d       | j                  t        d      d       | j                  t        t        d       | j                  t        t        d       | j                  t        d      d       | j                  t        d      d       | j                  t        t        d       | j                  t        t        d       | j                  t        d      d       | j                  t        d      d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       y )Nz br'x' r_   z rb'x' z
 br'\x01'    \x01z
 rb'\x01' z br'' r"   z rb'' z
 br'\x81' s   \x81z
 rb'\x81' u    br'' u    rb'' r`   ra   z rb'\u1881' u	    br'ᢁ' u	    rb'ᢁ' rb   rc   z rb'\U0001d120' u
    br'𝄠' u
    rb'𝄠' z bb'' z rr'' z brr'' z bbr'' z rrb'' z rbb'' rd   r   s    r   test_eval_bytes_rawz TestLiterals.test_eval_bytes_raw  s   m,d3m,d3/0.A/0.A./a9./a9/0.A/0.A+t-=>+t-=>124DE124DE+t-?@+t-?@568LM568LM+t-CD+t-CD+t\:+t\:+t];+t];+t];+t];r	   c                 ~   | j                  t        d      d       | j                  t        d      d       | j                  t        d      d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d       | j                  t        t        d	       y )
Nz u'x' r!   u    U'ä'    äu    u'ä' z ur'' z ru'' z bu'' z ub'' )r'   r(   r-   r.   r   s    r   test_eval_str_uzTestLiterals.test_eval_str_u'  s    l+S1/0$7PQSWX+t\:+t\:+t\:+t\:r	   c                    | j                  t        d      d       | j                  t        d      d       | j                  t        d      d       | j                  t        d      d        | j                  t        d	      d
       y )Nz B'x' r_   z	 R'\x01' r\   z
 BR'\x01' rp   z
 F'{1+1}' r:   z U'\U0001d120' u   𝄠)r'   r(   r   s    r   test_uppercase_prefixesz$TestLiterals.test_uppercase_prefixes0  sq    l+T2./9/0(;./C5:45}Er	   c                 z   d|j                  dd      z   }t        j                  j                  | j                  |dz         }t        |d|      }	 |j                  t        |z         |j                  |       |j                          t        |       t        j                  |= y # |j                          w xY w)Nxx_-_z.pyrM   )encoding)replaceosr   joinr   openwriter   close
__import__r   modules)r   r{   extramodnamefnfs         r   check_encodingzTestLiterals.check_encoding7  s    (**344WW\\$++w7S8,	GGHx'(GGENGGI7KK  GGIs   )B( (B:c                 ,    d}| j                  d|       y )Nu#   z = 'ሴ'; assert ord(z) == 0x1234
utf-8r   r   r   s     r   test_file_utf_8zTestLiterals.test_file_utf_8C  s    9GU+r	   c                 L    d}| j                  t        | j                  d|       y )Nu   b''
r   )r-   r.   r   r   s     r   test_file_utf_8_errorz"TestLiterals.test_file_utf_8_errorG  s!    +t':':GUKr	   c                 &    | j                  d       y )Nr   r   r   s    r   test_file_utf8zTestLiterals.test_file_utf8K  s    G$r	   c                 &    | j                  d       y )Nz
iso-8859-1r   r   s    r   test_file_iso_8859_1z!TestLiterals.test_file_iso_8859_1N  s    L)r	   c                 &    | j                  d       y )Nzlatin-1r   r   s    r   test_file_latin_1zTestLiterals.test_file_latin_1Q  s    I&r	   c                 &    | j                  d       y )Nlatin9r   r   s    r   test_file_latin9zTestLiterals.test_file_latin9T  s    H%r	   N) )__name__
__module____qualname__r   r   r   r*   r/   rP   rV   rZ   r]   re   rg   rk   rn   rq   rt   rv   r   r   r   r   r   r   r    r	   r   r   r   @   s|    (
79	CC $4L(2).	D	D>(2(0<4;F
!,L%*'&r	   r   __main__)__doc__r}   r   r   r   unittestr?   r   r   TestCaser   r   mainr   r	   r   <module>r      sY   : 
 
    ,U&8$$ U&p zHMMO r	   