Skip to content

Commit 120c212

Browse files
author
Victor Stinner
committed
Issue python#8226: sys.setfilesystemencoding() raises a LookupError if the encoding
is unknown.
1 parent cd4ec0e commit 120c212

3 files changed

Lines changed: 9 additions & 1 deletion

File tree

Lib/test/test_sys.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ def test_setfilesystemencoding(self):
799799
old = sys.getfilesystemencoding()
800800
sys.setfilesystemencoding("iso-8859-1")
801801
self.assertEqual(sys.getfilesystemencoding(), "iso-8859-1")
802+
self.assertRaises(LookupError, sys.setfilesystemencoding, "xxx")
802803
sys.setfilesystemencoding(old)
803804

804805
def test_main():

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #8226: sys.setfilesystemencoding() raises a LookupError if the encoding
16+
is unknown
17+
1518
- Issue #1583863: An str subclass can now override the __str__ method
1619

1720
- Issue #8014: Setting a T_UINT or T_PYSSIZET attribute of an object with

Python/bltinmodule.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ int Py_HasFileSystemDefaultEncoding = 0;
2929
int
3030
_Py_SetFileSystemEncoding(PyObject *s)
3131
{
32-
PyObject *defenc;
32+
PyObject *defenc, *codec;
3333
if (!PyUnicode_Check(s)) {
3434
PyErr_BadInternalCall();
3535
return -1;
3636
}
3737
defenc = _PyUnicode_AsDefaultEncodedString(s, NULL);
3838
if (!defenc)
3939
return -1;
40+
codec = _PyCodec_Lookup(PyBytes_AsString(defenc));
41+
if (codec == NULL)
42+
return -1;
43+
Py_DECREF(codec);
4044
if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding)
4145
/* A file system encoding was set at run-time */
4246
free((char*)Py_FileSystemDefaultEncoding);

0 commit comments

Comments
 (0)