Current File : //usr/local/apps/python3/lib/python3.11/test/__pycache__/test_string_literals.cpython-311.pyc
�

�Kej2���dZddlZddlZddlZddlZddlZddlZdZd�ZGd�dej	��Z
edkrej��dSdS)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/usr/local/apps/python3/lib/python3.11/test/test_string_literals.py�byter<s���!��:�:��c��eZdZd�Zd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�Zd
�Zd�Zd�Zdd�Zd�Zd�Zd�Zd�Zd�Zd�ZdS)�TestLiteralsc��tjdd�|_tj��|_tj�d|j��dS)Nr)�sys�path�	save_path�tempfile�mkdtemp�tmpdir�insert��selfs r�setUpzTestLiterals.setUpBsA����!�!�!�����&�(�(��������4�;�'�'�'�'�'rc�h�|jtjdd�<tj|jd���dS)NT)�
ignore_errors)rrr
�shutil�rmtreerrs r�tearDownzTestLiterals.tearDownGs0���n��������
�d�k��6�6�6�6�6�6rc�r�tD].}|dks&d|cxkrdksnJt|������/dS)N�
� �~)�TEMPLATE�repr)r�cs  r�
test_templatezTestLiterals.test_templateKsM���	9�	9�A���9�9��q�����C�������a�������	9�	9rc�L�|�td��d��|�td��td����|�td��td����|�td��td����|�td��td����|�td	��td
����|�td��td
����|�td��td
����|�td��td
����dS)Nz 'x' �xz '\x01' �z '' z '\x81' �u '' z
 '\u1881' �u 'ᢁ' z '\U0001d120' � �u '𝄠' ��assertEqual�eval�chrrs r�test_eval_str_normalz!TestLiterals.test_eval_str_normalQsJ������k�*�*�C�0�0�0�����o�.�.��A���7�7�7�����n�-�-�s�1�v�v�6�6�6�����o�.�.��D�	�	�:�:�:�����n�-�-�s�4�y�y�9�9�9�����/�0�0�#�f�+�+�>�>�>�����.�/�/��V���=�=�=�����3�4�4�c�'�l�l�C�C�C�����2�3�3�S��\�\�B�B�B�B�Brc��|�ttd��|�ttd��|�ttd��|�ttd��|�ttd��|�ttd��|�ttd��|�ttd��|�ttd	��|�ttd
��|�ttd��|�ttd��|�ttd
��|�ttd��dS)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+rs 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�Brc	��tdd��D]o}|dvr�|�t��5|�t	d|z��dt|��z��ddd��n#1swxYwY�pt
jd���5}t
jdt�	��t	d
��ddd��n#1swxYwY|�t|��d��|�t|dj��d��|�|djd
��|�|dj
d��t
jd���5}t
jdt�	��|�t��5}t	d
��ddd��n#1swxYwY|j}ddd��n#1swxYwY|�|g��|�|jd��|�|jd
��|�|j
d��|�|jd��dS)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�linenor0r1�	exception�msg�offset�r�b�w�cm�excs     r�test_eval_str_invalid_escapez)TestLiterals.test_eval_str_invalid_escapelsQ���q�#���	D�	D�A��6�6�6���!�!�"4�5�5�
D�
D�� � ��h��l�!3�!3�T�C��F�F�]�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����#�#�#�����Q�q�T�\�*�*�,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�5A9	�<A9	�+C
�
C�C�6G0�6G�G0�G	�G0�G	�
G0�0G4�7G4c���tdd��D]g}|�t��5|�t	d|z��t|����ddd��n#1swxYwY�ht
jd���5}t
jdt���t	d��ddd��n#1swxYwY|�t|��d	��|�t|d
j��d��|�|d
jd��|�|d
j
d	��t
jd���5}t
jd
t���|�t��5}t	d��ddd��n#1swxYwY|j}ddd��n#1swxYwY|�|g��|�|jd��|�|jd��|�|j
d	��|�|jd	��dS)N��z'\%o'Tr6r8r9z'''
\407'''r%r�$invalid octal escape sequence '\407'r<r=r>�rrrOrPrQs     r�"test_eval_str_invalid_octal_escapez/TestLiterals.test_eval_str_invalid_octal_escape�s6���u�f�%�%�	=�	=�A��!�!�"4�5�5�
=�
=�� � ��h��l�!3�!3�S��V�V�<�<�<�
=�
=�
=�
=�
=�
=�
=�
=�
=�
=�
=����
=�
=�
=�
=���
$�D�
1�
1�
1�	"�Q��!�(�5G�H�H�H�H���!�!�!�	"�	"�	"�	"�	"�	"�	"�	"�	"�	"�	"����	"�	"�	"�	"�	
����Q����#�#�#�����Q�q�T�\�*�*�@�	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	�4A1	�+C�C	�C	�86G(�.G
�>G(�
G	�G(�G	�
G(�(G,�/G,c���|�td��d��|�td��d��|�td��td����|�td��d��|�td	��td
����|�td��d��|�td
��td����|�td��d��|�td��td����dS)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)rs r�test_eval_str_rawzTestLiterals.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�5�7I�J�J�J�����3�4�4�c�'�l�l�C�C�C�C�Crc��|�td��d��|�td��td����|�td��td����|�td��td����|�ttd��|�td	��d
��|�ttd��|�td��d
��|�ttd��dS)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+rr0r1rs 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�2�4D�E�E�E����+�t�->�?�?�?�����5�6�6�8L�M�M�M����+�t�-B�C�C�C�C�Crc��|�ttd��|�ttd��dS)Nz b'\x' z b'\x0' r/rs r�test_eval_bytes_incompletez'TestLiterals.test_eval_bytes_incomplete�s8�����+�t�^�<�<�<����+�t�_�=�=�=�=�=rc	���tdd��D]p}|dvr�|�t��5|�t	d|z��dt|g��z��ddd��n#1swxYwY�qt
jd���5}t
jdt�	��t	d
��ddd��n#1swxYwY|�t|��d��|�t|dj��d��|�|djd
��|�|dj
d��t
jd���5}t
jdt�	��|�t��5}t	d
��ddd��n#1swxYwY|j}ddd��n#1swxYwY|�|g��|�|jd��|�|jd
��|�|j
d��dS)Nr%r4s

"'01234567\abfnrtvxzb'\%c'�\Tr6r8r9z
b'''
\z'''rr;r<r=�r?r@rAr*r+rrBrCrDrErFrGrHrIr0r1rJrKrMs     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�!4�e�e�Q�C�j�j�6H�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����#�#�#�����Q�q�T�\�*�*�,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�6A:	�=A:	�+C�C�C�6G1�7G�G1�G	�G1�G	�
G1�1G5�8G5c	��tdd��D]k}|�t��5|�t	d|z��t|dzg����ddd��n#1swxYwY�lt
jd���5}t
jdt���t	d	��ddd��n#1swxYwY|�t|��d
��|�t|dj��d��|�|djd
��|�|dj
d
��t
jd���5}t
jdt���|�t��5}t	d	��ddd��n#1swxYwY|j}ddd��n#1swxYwY|�|g��|�|jd��|�|jd
��|�|j
d
��dS)NrTrUzb'\%o'�Tr6r8r9zb'''
\407'''r%rrVr<r=rhrWs     r�$test_eval_bytes_invalid_octal_escapez1TestLiterals.test_eval_bytes_invalid_octal_escape�s;���u�f�%�%�	J�	J�A��!�!�"4�5�5�
J�
J�� � ��i�!�m�!4�!4�e�Q��Y�K�6H�6H�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����#�#�#�����Q�q�T�\�*�*�@�	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�1A5	�8A5	�+C	�	C
�C
�<6G,�2G�G,�G	�G,�G	�
G,�,G0�3G0c��|�td��d��|�td��d��|�td��d��|�td��d��|�td��td����|�td	��td����|�td
��d��|�td��d��|�ttd
��|�ttd��|�td��d��|�td��d��|�ttd��|�ttd��|�td��d��|�td��d��|�ttd��|�ttd��|�ttd��|�ttd��|�ttd��|�ttd��|�ttd��|�ttd��dS)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`raz rb'\U0001d120' u
 br'𝄠' u
 rb'𝄠' z bb'' z rr'' z brr'' z bbr'' z rrb'' z rbb'' rbrs 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�2�4D�E�E�E�����1�2�2�4D�E�E�E����+�t�-?�@�@�@����+�t�-?�@�@�@�����5�6�6�8L�M�M�M�����5�6�6�8L�M�M�M����+�t�-C�D�D�D����+�t�-C�D�D�D����+�t�\�:�:�:����+�t�\�:�:�:����+�t�]�;�;�;����+�t�]�;�;�;����+�t�]�;�;�;����+�t�]�;�;�;�;�;rc���|�td��d��|�td��d��|�td��d��|�ttd��|�ttd��|�ttd��|�ttd	��dS)
Nz u'x' r$u U'ä' �äu u'ä' z ur'' z ru'' z bu'' z ub'' )r*r+r0r1rs r�test_eval_str_uzTestLiterals.test_eval_str_us�������l�+�+�S�1�1�1�����/�0�0�$�7�7�7�����P�Q�Q�SW�X�X�X����+�t�\�:�:�:����+�t�\�:�:�:����+�t�\�:�:�:����+�t�\�:�:�:�:�:rc�f�|�td��d��|�td��d��|�td��d��|�td��d���|�td	��d
��dS)Nz B'x' r]z	 R'\x01' rZz
 BR'\x01' rnz
 F'{1+1}' �z U'\U0001d120' u𝄠)r*r+rs 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|�dd��z}tj�|j|dz��}t|d|���}	|�t|z��|�|��|���n#|���wxYwt|��tj|=dS)N�xx_�-�_z.pyrO)�encoding)�replace�osr
�joinr�open�writer�close�
__import__r�modules)rr{�extra�modname�fn�fs      r�check_encodingzTestLiterals.check_encodings����(�*�*�3��4�4�4��
�W�\�\�$�+�w���
7�
7����S�8�,�,�,��	�
�G�G�H�x�'�(�(�(�
�G�G�E�N�N�N�
�G�G�I�I�I�I��A�G�G�I�I�I�I�����7�����K�� � � s�2B�B2c�6�d}|�d|��dS)Nu#z = 'ሴ'; assert ord(z) == 0x1234
�utf-8�r��rr�s  r�test_file_utf_8zTestLiterals.test_file_utf_8 s#��9�����G�U�+�+�+�+�+rc�N�d}|�t|jd|��dS)Nub'€'
r�)r0r1r�r�s  r�test_file_utf_8_errorz"TestLiterals.test_file_utf_8_error$s*�������+�t�':�G�U�K�K�K�K�Krc�0�|�d��dS)Nr�r�rs r�test_file_utf8zTestLiterals.test_file_utf8(s�����G�$�$�$�$�$rc�0�|�d��dS)Nz
iso-8859-1r�rs r�test_file_iso_8859_1z!TestLiterals.test_file_iso_8859_1+s�����L�)�)�)�)�)rc�0�|�d��dS)Nzlatin-1r�rs r�test_file_latin_1zTestLiterals.test_file_latin_1.s�����I�&�&�&�&�&rc�0�|�d��dS)N�latin9r�rs r�test_file_latin9zTestLiterals.test_file_latin91s�����H�%�%�%�%�%rN)rv)�__name__�
__module__�__qualname__rrr"r-r2rRrXr[rcrerirlrorrrur�r�r�r�r�r�r��rrr
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�%�%�%�*�*�*�'�'�'�&�&�&�&�&rr
�__main__)
�__doc__r}rrr�unittestrBrr�TestCaser
r��mainr�rr�<module>r�s�����:
�	�	�	�
�
�
�
�
�
�
�
���������������,���r&�r&�r&�r&�r&�8�$�r&�r&�r&�j�z����H�M�O�O�O�O�O��r