a ze'@srdZddlZddlZddlZddlZddlZddlZddlmZdZ ddZ Gdddej Z e d krnedS) aTest 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). N)use_old_parsera# 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] cCs t|gS)N)bytes)irM/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_string_literals.pybyte=src@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZd)ddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(S)* TestLiteralscCs.tjdd|_t|_tjd|jdS)Nr)syspath save_pathtempfilemkdtemptmpdirinsertselfrrrsetUpCs zTestLiterals.setUpcCs$|jtjdd<tj|jdddS)NT) ignore_errors)r r r shutilrmtreerrrrrtearDownHszTestLiterals.tearDowncCs8tD].}|dksd|kr$dksnJt|qdS)N  ~)TEMPLATErepr)rcrrr test_templateLszTestLiterals.test_templatecCs|tdd|tdtd|tdtd|tdtd|tdtd|td td |td td |td td |tdtd dS)Nz 'x' xz '\x01' z '' z '\x81' u '' z '\u1881' u 'ᢁ' z '\U0001d120' u '𝄠'  assertEqualevalchrrrrrtest_eval_str_normalRsz!TestLiterals.test_eval_str_normalcCs|ttd|ttd|ttd|ttd|ttd|ttd|ttd|ttd|ttd |ttd |ttd |ttd |ttd |ttddS)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%rrrrtest_eval_str_incomplete]sz%TestLiterals.test_eval_str_incompletec CstddD]T}|dvrq |t,|td|dt|Wdq 1sT0Yq tjdd&}tjdtd td Wdn1s0Y|t |d||d j d ||d j dtjddX}tjd td | t }td Wdn1s0Y|j}Wdn1sB0Y||g||j d ||j d||jddS)Nrs "'01234567NU\abfnrtuvxz'\%c'\Trecordalwayscategoryz ''' \z'''rerror)range assertWarnsDeprecationWarningr$r%r&warningscatch_warnings simplefilterlenfilenamelinenor)r* exceptionoffsetrbwcmexcrrrtest_eval_str_invalid_escapems( <& (& z)TestLiterals.test_eval_str_invalid_escapecCs|tdd|tdd|tdtd|tdd|td td |td d |td td|tdd|tdtddS)Nz r'x' rz r'\x01' z\x01z r'' rz r'\x81' z\x81u r'' r z r'\u1881' z\u1881u r'ᢁ' r!z r'\U0001d120' z \U0001d120u r'𝄠' r"r#rrrrtest_eval_str_rawszTestLiterals.test_eval_str_rawcCs|tdd|tdtd|tdtd|tdtd|ttd|td d |ttd |td d |ttddS)Nz b'x' xz b'\x01' rz b'' z b'\x81' r u b'' br'\u1881' \u1881u b'ᢁ'  br'\U0001d120' \U0001d120u b'𝄠' r$r%rr)r*rrrrtest_eval_bytes_normalsz#TestLiterals.test_eval_bytes_normalcCs |ttd|ttddS)Nz b'\x' z b'\x0' r(rrrrtest_eval_bytes_incompletesz'TestLiterals.test_eval_bytes_incompletec CstddD]V}|dvrq |t.|td|dt|gWdq 1sV0Yq tjdd&}tjdtd td Wdn1s0Y|t |d||d j d t r||d j dtjddX}tjd td | t}td Wdn1s$0Y|j}Wdn1sJ0Y||g||j d t r||j ddS)Nrr,s "'01234567\abfnrtvxzb'\%c'\Tr.r0r1z b''' \z'''rr3r4)r5r6r7r$r%rr8r9r:r;r<rr=r)r*r>r@rrrtest_eval_bytes_invalid_escapes* >& (& z+TestLiterals.test_eval_bytes_invalid_escapecCst|tdd|tdd|tdd|tdd|tdtd|td td|td d |td d |ttd |ttd|tdd|tdd|ttd|ttd|tdd|tdd|ttd|ttd|ttd|ttd|ttd|ttd|ttd|ttddS)Nz br'x' rGz rb'x' z br'\x01' s\x01z rb'\x01' z br'' rz rb'' z br'\x81' s\x81z rb'\x81' u br'' u rb'' rHrIz rb'\u1881' u br'ᢁ' u rb'ᢁ' rJrKz rb'\U0001d120' u br'𝄠' u rb'𝄠' z bb'' z rr'' z brr'' z bbr'' z rrb'' z rbb'' rLrrrrtest_eval_bytes_raws0z TestLiterals.test_eval_bytes_rawcCsl|tdd|tdd|tdd|ttd|ttd|ttd|ttd dS) Nz u'x' ru U'ä' äu u'ä' z ur'' z ru'' z bu'' z ub'' )r$r%r)r*rrrrtest_eval_str_uszTestLiterals.test_eval_str_ucCsvd|dd}tj|j|d}t|d|d}z$|t|||W|n |0t |t j |=dS)NZxx_-_z.pyrB)encoding) replaceosr joinropenwriterclose __import__r modules)rrWextramodnamefnfrrrcheck_encodings zTestLiterals.check_encodingcCsd}|d|dS)Nu#z = 'ሴ'; assert ord(z) == 0x1234 utf-8rdrr`rrrtest_file_utf_8szTestLiterals.test_file_utf_8cCsd}|t|jd|dS)Nub'€' re)r)r*rdrgrrrtest_file_utf_8_errorsz"TestLiterals.test_file_utf_8_errorcCs|ddS)Nrerfrrrrtest_file_utf8szTestLiterals.test_file_utf8cCs|ddS)Nz iso-8859-1rfrrrrtest_file_iso_8859_1sz!TestLiterals.test_file_iso_8859_1cCs|ddS)Nzlatin-1rfrrrrtest_file_latin_1szTestLiterals.test_file_latin_1cCs|ddS)Nlatin9rfrrrrtest_file_latin9szTestLiterals.test_file_latin9N)rT)__name__ __module__ __qualname__rrrr'r+rErFrMrNrPrQrSrdrhrirjrkrlrnrrrrrAs&    r__main__)__doc__rYr rr Zunittestr8Z test.supportrrrZTestCaserromainrrrrs <