
    <ej2                         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                     dS dS )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    0/usr/lib/python3.11/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dS )TestLiteralsc                     t           j        d d          | _        t          j                    | _        t           j                            d| j                   d S )Nr   )syspath	save_pathtempfilemkdtemptmpdirinsertselfs    r   setUpzTestLiterals.setUpB   sA    !!!&((4;'''''r   c                 h    | j         t          j        d d <   t          j        | j        d           d S )NT)ignore_errors)r   r   r   shutilrmtreer   r   s    r   tearDownzTestLiterals.tearDownG   s0    ndk666666r   c                 r    t           D ].}|dk    s&d|cxk    rdk    sn J t          |                      /d S )N
 ~)TEMPLATErepr)r   cs     r   test_templatezTestLiterals.test_templateK   sM      	9 	9A99qCa	9 	9r   c                 L   |                      t          d          d           |                      t          d          t          d                     |                      t          d          t          d                     |                      t          d          t          d                     |                      t          d          t          d                     |                      t          d	          t          d
                     |                      t          d          t          d
                     |                      t          d          t          d                     |                      t          d          t          d                     d S )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   sJ   k**C000o..A777n--s1vv666o..D		:::n--s4yy999/00#f++>>>.//V===344c'llCCC233S\\BBBBBr   c                    |                      t          t          d           |                      t          t          d           |                      t          t          d           |                      t          t          d           |                      t          t          d           |                      t          t          d           |                      t          t          d           |                      t          t          d           |                      t          t          d	           |                      t          t          d
           |                      t          t          d           |                      t          t          d           |                      t          t          d           |                      t          t          d           d S )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AA+t-ABBBBBr   c           	         t          dd          D ]o}|dv r|                     t                    5  |                     t	          d|z            dt          |          z              d d d            n# 1 swxY w Y   pt          j        d          5 }t          j        dt          	           t	          d
           d d d            n# 1 swxY w Y   |                     t          |          d           |                     t          |d         j                  d           |                     |d         j        d           |                     |d         j        d           t          j        d          5 }t          j        dt          	           |                     t                    5 }t	          d
           d d d            n# 1 swxY w Y   |j        }d d d            n# 1 swxY w Y   |                     |g            |                     |j        d           |                     |j        d           |                     |j        d           |                     |j        d           d S )Nr%      s   
"'01234567NU\abfnrtuvxz'\%c'\Trecordalwayscategoryz	'''
\z'''r   invalid escape sequence '\z'<string>errorrangeassertWarnsDeprecationWarningr*   r+   r,   warningscatch_warningssimplefilterlenstrmessagefilenamelinenor0   r1   	exceptionmsgoffsetr   bwcmexcs        r   test_eval_str_invalid_escapez)TestLiterals.test_eval_str_invalid_escapel   sQ   q# 	D 	DA666!!"455 D D  hl!3!3TCFF]CCCD D D D D D D D D D D D D D D $D111 	 Q!(5GHHHH	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	Q###QqT\**,KLLL1
3331a((($D111 	Q!'4FGGGG"";// $2]###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $,C		 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 	B"ABBBz222Q'''Q'''''sY   7A55A9	<A9	+CCC 6G06GG0G	G0G	
G00G47G4c                    t          dd          D ]g}|                     t                    5  |                     t	          d|z            t          |                     d d d            n# 1 swxY w Y   ht          j        d          5 }t          j        dt                     t	          d           d d d            n# 1 swxY w Y   |                     t          |          d	           |                     t          |d
         j                  d           |                     |d
         j        d           |                     |d
         j        d	           t          j        d          5 }t          j        dt                     |                     t                    5 }t	          d           d d d            n# 1 swxY w Y   |j        }d d d            n# 1 swxY w Y   |                     |g            |                     |j        d           |                     |j        d           |                     |j        d	           |                     |j        d	           d S )N      z'\%o'Tr6   r8   r9   z'''
\407'''r%   r   $invalid octal escape sequence '\407'r<   r=   r>   r   r   rO   rP   rQ   s        r   "test_eval_str_invalid_octal_escapez/TestLiterals.test_eval_str_invalid_octal_escape   s6   uf%% 	= 	=A!!"455 = =  hl!3!3SVV<<<= = = = = = = = = = = = = = = $D111 	"Q!(5GHHHH!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	Q###QqT\**@	B 	B 	B1
3331a((($D111 	Q!'4FGGGG"";// &2_%%%& & & & & & & & & & & & & & &,C		 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 	B"IJJJz222Q'''Q'''''sY   4A--A1	4A1	+CC	C	86G(.G
>G(
G	G(G	
G((G,/G,c                    |                      t          d          d           |                      t          d          d           |                      t          d          t          d                     |                      t          d          d           |                      t          d	          t          d
                     |                      t          d          d           |                      t          d          t          d                     |                      t          d          d           |                      t          d          t          d                     d S )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   s5   l++S111.//>>>o..A777.//>>>o..D		:::011>BBB/00#f++>>>4557IJJJ344c'llCCCCCr   c                    |                      t          d          d           |                      t          d          t          d                     |                      t          d          t          d                     |                      t          d          t          d                     |                     t          t          d           |                      t          d	          d
           |                     t          t          d           |                      t          d          d           |                     t          t          d           d S )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22.//a999o..Q888.//d<<<+t_===1224DEEE+t->???5668LMMM+t-BCCCCCr   c                     |                      t          t          d           |                      t          t          d           d S )Nz b'\x' z b'\x0' r/   r   s    r   test_eval_bytes_incompletez'TestLiterals.test_eval_bytes_incomplete   s8    +t^<<<+t_=====r   c           	         t          dd          D ]p}|dv r|                     t                    5  |                     t	          d|z            dt          |g          z              d d d            n# 1 swxY w Y   qt          j        d          5 }t          j        dt          	           t	          d
           d d d            n# 1 swxY w Y   |                     t          |          d           |                     t          |d         j                  d           |                     |d         j        d           |                     |d         j        d           t          j        d          5 }t          j        dt          	           |                     t                    5 }t	          d
           d d d            n# 1 swxY w Y   |j        }d d d            n# 1 swxY w Y   |                     |g            |                     |j        d           |                     |j        d           |                     |j        d           d S )Nr%   r4   s   
"'01234567\abfnrtvxzb'\%c'   \Tr6   r8   r9   z
b'''
\z'''r   r;   r<   r=   r?   r@   rA   r*   r+   r   rB   rC   rD   rE   rF   rG   rH   rI   r0   r1   rJ   rK   rM   s        r   test_eval_bytes_invalid_escapez+TestLiterals.test_eval_bytes_invalid_escape   s<   q# 	J 	JA333!!"455 J J  i!m!4!4eeQCjj6HIIIJ J J J J J J J J J J J J J J $D111 	!Q!(5GHHHH   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	Q###QqT\**,KLLL1
3331a((($D111 	Q!'4FGGGG"";// %2^$$$% % % % % % % % % % % % % % %,C		 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 	B"ABBBz222Q'''''sY   8A66A:	=A:	+CCC6G17GG1G	G1G	
G11G58G5c           	         t          dd          D ]k}|                     t                    5  |                     t	          d|z            t          |dz  g                     d d d            n# 1 swxY w Y   lt          j        d          5 }t          j        dt                     t	          d	           d d d            n# 1 swxY w Y   |                     t          |          d
           |                     t          |d         j                  d           |                     |d         j        d           |                     |d         j        d
           t          j        d          5 }t          j        dt                     |                     t                    5 }t	          d	           d d d            n# 1 swxY w Y   |j        }d d d            n# 1 swxY w Y   |                     |g            |                     |j        d           |                     |j        d           |                     |j        d
           d S )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 	JA!!"455 J J  i!m!4!4eQYK6H6HIIIJ J J J J J J J J J J J J J J $D111 	#Q!(5GHHHH!"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	Q###QqT\**@	B 	B 	B1
3331a((($D111 	Q!'4FGGGG"";// '2%&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ',C		 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 	B"IJJJz222Q'''''sY   8A11A5	8A5	+C		CC<6G,2GG,G	G,G	
G,,G03G0c                    |                      t          d          d           |                      t          d          d           |                      t          d          d           |                      t          d          d           |                      t          d          t          d                     |                      t          d	          t          d                     |                      t          d
          d           |                      t          d          d           |                     t          t          d           |                     t          t          d           |                      t          d          d           |                      t          d          d           |                     t          t          d           |                     t          t          d           |                      t          d          d           |                      t          d          d           |                     t          t          d           |                     t          t          d           |                     t          t          d           |                     t          t          d           |                     t          t          d           |                     t          t          d           |                     t          t          d           |                     t          t          d           d S )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33m,,d333/00.AAA/00.AAA.//a999.//a999/00.AAA/00.AAA+t-=>>>+t-=>>>1224DEEE1224DEEE+t-?@@@+t-?@@@5668LMMM5668LMMM+t-CDDD+t-CDDD+t\:::+t\:::+t];;;+t];;;+t];;;+t];;;;;r   c                    |                      t          d          d           |                      t          d          d           |                      t          d          d           |                     t          t          d           |                     t          t          d           |                     t          t          d           |                     t          t          d	           d S )
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11/00$777PQQSWXXX+t\:::+t\:::+t\:::+t\:::::r   c                 f   |                      t          d          d           |                      t          d          d           |                      t          d          d           |                      t          d          d            |                      t          d	          d
           d S )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  s    l++T222.//999/00(;;;.//C:::455}EEEEEr    c                    d|                     dd          z   }t          j                            | j        |dz             }t          |d|          }	 |                    t          |z             |                    |           |                                 n# |                                 w xY wt          |           t          j        |= d S )Nxx_-_z.pyrO   )encoding)replaceosr   joinr   openwriter   close
__import__r   modules)r   r{   extramodnamefnfs         r   check_encodingzTestLiterals.check_encoding  s    (**3444W\\$+w77S8,,,	GGHx'(((GGENNNGGIIIIAGGIIII7K   s   2B B2c                 6    d}|                      d|           d S )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                 N    d}|                      t          | j        d|           d S )Nu   b''
r   )r0   r1   r   r   s     r   test_file_utf_8_errorz"TestLiterals.test_file_utf_8_error$  s*    +t':GUKKKKKr   c                 0    |                      d           d S )Nr   r   r   s    r   test_file_utf8zTestLiterals.test_file_utf8(  s    G$$$$$r   c                 0    |                      d           d S )Nz
iso-8859-1r   r   s    r   test_file_iso_8859_1z!TestLiterals.test_file_iso_8859_1+  s    L)))))r   c                 0    |                      d           d S )Nzlatin-1r   r   s    r   test_file_latin_1zTestLiterals.test_file_latin_1.  s    I&&&&&r   c                 0    |                      d           d S )Nlatin9r   r   s    r   test_file_latin9zTestLiterals.test_file_latin91  s    H%%%%%r   N)rv   )__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
   @   su       ( ( (
7 7 79 9 9	C 	C 	CC C C ( ( (4( ( (2	D 	D 	D	D 	D 	D> > >( ( (2( ( (0< < <4; ; ;F F F
! 
! 
! 
!, , ,L L L% % %* * *' ' '& & & & &r   r
   __main__)__doc__r}   r   r   r   unittestrB   r   r   TestCaser
   r   mainr   r   r   <module>r      s    : 
			 



    ,  r& r& r& r& r&8$ r& r& r&j zHMOOOOO r   