
    dafG4                         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    4/root/Python-3.12.4/Lib/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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 ]%  }|dk(  r	d|cxk  rdk  rn J t        |              y )N
 ~)TEMPLATErepr)r   cs     r   test_templatezTestLiterals.test_templateK   s3      	9A9qC8a8/	9r   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linenor0   r1   	exceptionmsgoffsetr:   assertRegexr   bwcmexcs        r   test_eval_str_invalid_escapez)TestLiterals.test_eval_str_invalid_escapel   s   q# 	DA66!!-0 D  hl!3TCF]CD D	D $$D1 	 Q!!(]C	  	Q#QqT\\*,KL1
31a($$D1 	Q!!'MB"";/ $2]#$,,C		
 	B"ABz2Q'Q' $$D1 	Q!!(]C"";/  2Y ,,C		
 	Q#16QqT\\*,EF1
3AD D	  	 $ $	 	   	 	s_   +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'Tr6   r8   r9   z'''
\407'''r%   r   $invalid octal escape sequence '\407'r<   r=   )r>   r?   r@   r*   r+   r,   rA   rB   rC   rD   rE   rF   rG   rH   r0   r1   rI   rJ   rK   r   r   rO   rP   rQ   s        r   "test_eval_str_invalid_octal_escapez/TestLiterals.test_eval_str_invalid_octal_escape   s   uf% 	=A!!-0 =  hl!3SV<= =	= $$D1 	"Q!!(]C!	" 	Q#QqT\\*@	B1
31a($$D1 	Q!!'MB"";/ &2_%&,,C		
 	B"IJz2Q'Q'+= =	" 	"& &	 	s;   (H-'H31H5$H)0H5H	H&)H2	.H55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   r0   r1   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%   r4   s   
"'01234567\abfnrtvxzb'\%c'   \Tr6   r8   r9   z
b'''
\z'''r   r;   r<   r=   r>   r?   r@   r*   r+   r   rA   rB   rC   rD   rE   rF   rG   rH   r0   r1   rI   rJ   rM   s        r   test_eval_bytes_invalid_escapez+TestLiterals.test_eval_bytes_invalid_escape   s   q# 	JA33!!-0 J  i!m!4eeQCj6HIJ J	J $$D1 	!Q!!(]C 	! 	Q#QqT\\*,KL1
31a($$D1 	Q!!'MB"";/ %2^$%,,C		
 	B"ABz2Q''J J	! 	!% %	 	s;   ,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)NrT   rU   zb'\%o'   Tr6   r8   r9   zb'''
\407'''r%   r   rV   r<   r=   rh   rW   s        r   $test_eval_bytes_invalid_octal_escapez1TestLiterals.test_eval_bytes_invalid_octal_escape   s   uf% 	JA!!-0 J  i!m!4eQYK6HIJ J	J $$D1 	#Q!!(]C!"	# 	Q#QqT\\*@	B1
31a($$D1 	Q!!'MB"";/ '2%&',,C		
 	B"IJz2Q')J J	# 	#' '	 	s;   ,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^   r_   z rb'\u1881' u	    br'ᢁ' u	    rb'ᢁ' r`   ra   z rb'\U0001d120' u
    br'𝄠' u
    rb'𝄠' z bb'' z rr'' z brr'' z bbr'' z rrb'' z rbb'' rb   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+   r0   r1   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' rZ   z
 BR'\x01' rn   z
 F'{1+1}'    z U'\U0001d120' u   𝄠)r*   r+   r   s    r   test_uppercase_prefixesz$TestLiterals.test_uppercase_prefixes  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.pyrO   )encoding)replaceosr   joinr   openwriter   close
__import__r   modules)r   rz   extramodnamefnfs         r   check_encodingzTestLiterals.check_encoding   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_8,  s    9GU+r   c                 L    d}| j                  t        | j                  d|       y )Nu   b''
r   )r0   r1   r   r   s     r   test_file_utf_8_errorz"TestLiterals.test_file_utf_8_error0  s!    +t':':GUKr   c                 &    | j                  d       y )Nr   r   r   s    r   test_file_utf8zTestLiterals.test_file_utf84  s    G$r   c                 &    | j                  d       y )Nz
iso-8859-1r   r   s    r   test_file_iso_8859_1z!TestLiterals.test_file_iso_8859_17  s    L)r   c                 &    | j                  d       y )Nzlatin-1r   r   s    r   test_file_latin_1zTestLiterals.test_file_latin_1:  s    I&r   c                 &    | j                  d       y )Nlatin9r   r   s    r   test_file_latin9zTestLiterals.test_file_latin9=  s    H%r   N) )__name__
__module____qualname__r   r   r"   r-   r2   rR   rX   r[   rc   re   ri   rl   ro   rr   ru   r   r   r   r   r   r   r    r   r   r
   r
   @   sw    (
79	CC $4L(2	D	D>(2(0<4;F
!,L%*'&r   r
   __main__)__doc__r|   r   r   r   unittestrA   r   r   TestCaser
   r   mainr   r   r   <module>r      sY   : 
 
    ,~&8$$ ~&B zHMMO r   