Skip to content

Commit f2d6931

Browse files
[3.13] pythongh-58124: Avoid CP_UTF8 in UnicodeDecodeError (pythonGH-137415) (python#137461)
pythongh-58124: Avoid CP_UTF8 in UnicodeDecodeError (pythonGH-137415) Fix name of the Python encoding in Unicode errors of the code page codec: use "cp65000" and "cp65001" instead of "CP_UTF7" and "CP_UTF8" which are not valid Python code names. (cherry picked from commit ce1b747) Co-authored-by: Victor Stinner <[email protected]>
1 parent 9cfa4df commit f2d6931

File tree

4 files changed

+5
-6
lines changed

4 files changed

+5
-6
lines changed

Lib/test/test_codecs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3284,7 +3284,7 @@ def test_code_page_name(self):
32843284
codecs.code_page_encode, 932, '\xff')
32853285
self.assertRaisesRegex(UnicodeDecodeError, 'cp932',
32863286
codecs.code_page_decode, 932, b'\x81\x00', 'strict', True)
3287-
self.assertRaisesRegex(UnicodeDecodeError, 'CP_UTF8',
3287+
self.assertRaisesRegex(UnicodeDecodeError, 'cp65001',
32883288
codecs.code_page_decode, self.CP_UTF8, b'\xff', 'strict', True)
32893289

32903290
def check_decode(self, cp, tests):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix name of the Python encoding in Unicode errors of the code page codec:
2+
use "cp65000" and "cp65001" instead of "CP_UTF7" and "CP_UTF8" which are not
3+
valid Python code names. Patch by Victor Stinner.

Objects/unicodeobject.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7290,10 +7290,6 @@ code_page_name(UINT code_page, PyObject **obj)
72907290
*obj = NULL;
72917291
if (code_page == CP_ACP)
72927292
return "mbcs";
7293-
if (code_page == CP_UTF7)
7294-
return "CP_UTF7";
7295-
if (code_page == CP_UTF8)
7296-
return "CP_UTF8";
72977293

72987294
*obj = PyBytes_FromFormat("cp%u", code_page);
72997295
if (*obj == NULL)

Python/codecs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ get_standard_encoding(const char *encoding, int *bytelength)
10871087
}
10881088
}
10891089
}
1090-
else if (strcmp(encoding, "CP_UTF8") == 0) {
1090+
else if (strcmp(encoding, "cp65001") == 0) {
10911091
*bytelength = 3;
10921092
return ENC_UTF8;
10931093
}

0 commit comments

Comments
 (0)