a 5êdgá'ã@srdZddlZddlZddlZddlZddlZddlZddlmZdZ dd„Z Gdd„dej ƒZ e d krne ¡dS) 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). é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|gƒS©N)Úbytes)Úi©rú5/usr/local/lib/python3.9/test/test_string_literals.pyÚbyte=src@s¦eZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z dd„Zd)dd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„Zd&d'„Zd(S)*Ú TestLiteralscCs.tjdd…|_t ¡|_tj d|j¡dS)Nr)ÚsysÚpathÚ save_pathÚtempfileÚmkdtempÚtmpdirÚinsert©ÚselfrrrÚsetUpCs zTestLiterals.setUpcCs$|jtjdd…<tj|jdddS)NT)Ú ignore_errors)r r r ÚshutilÚrmtreerrrrrÚtearDownHszTestLiterals.tearDowncCstD]}qdSr)ÚTEMPLATE)rÚcrrrÚ test_templateLszTestLiterals.test_templatecCs´| 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ÚchrrrrrÚtest_eval_str_normalRsz!TestLiterals.test_eval_str_normalcCsÈ| 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"rrrrÚtest_eval_str_incomplete]sz%TestLiterals.test_eval_str_incompletec Cs†tddƒD]T}|dvrq | t¡,| td|ƒdt|ƒ¡Wdƒq 1sT0Yq tjdd&}tjdtd td ƒWdƒn1s˜0Y| t |ƒd¡| |d j d ¡| |d j d¡tjddX}tjd td |  t ¡}td ƒWdƒn1s0Y|j}Wdƒn1sB0Y| |g¡| |j d ¡| |j d¡| |jd¡dS)Nré€s "'01234567NU\abfnrtuvxz'\%c'ú\T©ÚrecordÚalways©Úcategoryz ''' \z'''rúÚerror)ÚrangeÚ assertWarnsÚDeprecationWarningr!r"r#ÚwarningsÚcatch_warningsÚ simplefilterÚlenÚfilenameÚlinenor&r'Ú exceptionÚoffset©rÚbÚwÚcmÚexcrrrÚtest_eval_str_invalid_escapems( <& (& z)TestLiterals.test_eval_str_invalid_escapecCs¤| tdƒd¡| tdƒd¡| tdƒtdƒ¡| tdƒd¡| td ƒtd ƒ¡| td ƒd ¡| td ƒtdƒ¡| tdƒd¡| tdƒtdƒ¡dS)Nz r'x' rz r'\x01' z\x01z r'' rz r'\x81' z\x81u r'Â' rz r'\u1881' z\u1881u r'á¢' rz r'\U0001d120' z \U0001d120u r'ð„ ' rr rrrrÚtest_eval_str_raw…szTestLiterals.test_eval_str_rawcCsš| tdƒd¡| tdƒtdƒ¡| tdƒtdƒ¡| tdƒtdƒ¡| ttd¡| td ƒd ¡| ttd ¡| td ƒd ¡| ttd¡dS)Nz b'x' óxz b'\x01' rz b'' z b'\x81' ru b'Â' ú br'\u1881' ó\u1881u b'á¢' ú br'\U0001d120' ó \U0001d120u b'ð„ ' ©r!r"rr&r'rrrrÚtest_eval_bytes_normalsz#TestLiterals.test_eval_bytes_normalcCs | ttd¡| ttd¡dS)Nz b'\x' z b'\x0' r%rrrrÚtest_eval_bytes_incomplete›sz'TestLiterals.test_eval_bytes_incompletec CsˆtddƒD]V}|dvrq | t¡.| td|ƒdt|gƒ¡Wdƒq 1sV0Yq tjdd&}tjdtd td ƒWdƒn1sš0Y| t |ƒd¡| |d j d ¡t ƒrÞ| |d j d¡tjddX}tjd td |  t¡}td ƒWdƒn1s$0Y|j}Wdƒn1sJ0Y| |g¡| |j d ¡t ƒr„| |j d¡dS)Nrr)s "'01234567\abfnrtvxzb'\%c'ó\Tr+r-r.z b''' \z'''rr0r1)r2r3r4r!r"rr5r6r7r8r9rr:r&r'r;r=rrrÚtest_eval_bytes_invalid_escapeŸs* >& (& z+TestLiterals.test_eval_bytes_invalid_escapecCst| 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' rDz rb'x' z br'\x01' s\x01z rb'\x01' z br'' rz rb'' z br'\x81' s\x81z rb'\x81' u br'Â' u rb'Â' rErFz rb'\u1881' u br'á¢' u rb'á¢' rGrHz rb'\U0001d120' u br'ð„ ' u rb'ð„ ' z bb'' z rr'' z brr'' z bbr'' z rrb'' z rbb'' rIrrrrÚtest_eval_bytes_raw¸s0z TestLiterals.test_eval_bytes_rawcCsl| tdƒd¡| tdƒd¡| tdƒd¡| ttd¡| ttd¡| ttd¡| ttd ¡dS) Nz u'x' ru U'ä' õäu u'ä' z ur'' z ru'' z bu'' z ub'' )r!r"r&r'rrrrÚtest_eval_str_uÒszTestLiterals.test_eval_str_uÚcCsvd| dd¡}tj |j|d¡}t|d|d}z$| t|¡| |¡W| ¡n | ¡0t |ƒt j |=dS)NZxx_ú-Ú_z.pyr?)Úencoding) ÚreplaceÚosr ÚjoinrÚopenÚwriterÚcloseÚ __import__r Úmodules)rrTÚextraÚmodnameÚfnÚfrrrÚcheck_encodingÛs zTestLiterals.check_encodingcCsd}| d|¡dS)Nu#z = 'ሴ'; assert ord(z) == 0x1234 úutf-8©ra©rr]rrrÚtest_file_utf_8çszTestLiterals.test_file_utf_8cCsd}| t|jd|¡dS)Nub'€' rb)r&r'rardrrrÚtest_file_utf_8_errorësz"TestLiterals.test_file_utf_8_errorcCs| d¡dS)NrbrcrrrrÚtest_file_utf8ïszTestLiterals.test_file_utf8cCs| d¡dS)Nz iso-8859-1rcrrrrÚtest_file_iso_8859_1òsz!TestLiterals.test_file_iso_8859_1cCs| d¡dS)Nzlatin-1rcrrrrÚtest_file_latin_1õszTestLiterals.test_file_latin_1cCs| d¡dS)NÚlatin9rcrrrrÚtest_file_latin9øszTestLiterals.test_file_latin9N)rQ)Ú__name__Ú __module__Ú __qualname__rrrr$r(rBrCrJrKrMrNrPrarerfrgrhrirkrrrrr As&    r Ú__main__)Ú__doc__rVr rr Zunittestr5Z test.supportrrrZTestCaser rlÚmainrrrrÚs <