Skip to content

Commit 5b519e0

Browse files
author
Victor Stinner
committed
Issue #9632: Remove sys.setfilesystemencoding() function: use PYTHONFSENCODING
environment variable to set the filesystem encoding at Python startup. sys.setfilesystemencoding() creates inconsistencies because it is unable to reencode all filenames in all objects.
1 parent 6246d6d commit 5b519e0

File tree

6 files changed

+5
-65
lines changed

6 files changed

+5
-65
lines changed

Doc/library/sys.rst

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -724,15 +724,6 @@ always available.
724724
:file:`/usr/include/dlfcn.h` using the :program:`h2py` script. Availability:
725725
Unix.
726726

727-
.. function:: setfilesystemencoding(enc)
728-
729-
Set the encoding used when converting Python strings to file names to *enc*.
730-
By default, Python tries to determine the encoding it should use automatically
731-
on Unix; on Windows, it avoids such conversion completely. This function can
732-
be used when Python's determination of the encoding needs to be overwritten,
733-
e.g. when not all file names on disk can be decoded using the encoding that
734-
Python had chosen.
735-
736727
.. function:: setprofile(profilefunc)
737728

738729
.. index::

Include/fileobject.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ PyAPI_FUNC(char *) Py_UniversalNewlineFgets(char *, int, FILE*, PyObject *);
2121
*/
2222
PyAPI_DATA(const char *) Py_FileSystemDefaultEncoding;
2323
PyAPI_DATA(int) Py_HasFileSystemDefaultEncoding;
24-
PyAPI_FUNC(int) _Py_SetFileSystemEncoding(PyObject *);
2524

2625
/* Internal API
2726

Lib/test/test_sys.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -630,17 +630,6 @@ def get_fsencoding(env):
630630
env['PYTHONFSENCODING'] = encoding
631631
self.check_fsencoding(get_fsencoding(env), encoding)
632632

633-
def test_setfilesystemencoding(self):
634-
old = sys.getfilesystemencoding()
635-
try:
636-
sys.setfilesystemencoding("iso-8859-1")
637-
self.assertEqual(sys.getfilesystemencoding(), "iso-8859-1")
638-
finally:
639-
sys.setfilesystemencoding(old)
640-
try:
641-
self.assertRaises(LookupError, sys.setfilesystemencoding, "xxx")
642-
finally:
643-
sys.setfilesystemencoding(old)
644633

645634

646635
class SizeofTest(unittest.TestCase):

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ Core and Builtins
2929
Library
3030
-------
3131

32+
- Issue #9632: Remove sys.setfilesystemencoding() function: use
33+
PYTHONFSENCODING environment variable to set the filesystem encoding at
34+
Python startup. sys.setfilesystemencoding() creates inconsistencies because
35+
it is unable to reencode all filenames in all objects.
36+
3237
- Issue #9410: Various optimizations to the pickle module, leading to
3338
speedups up to 4x (depending on the benchmark). Mostly ported from
3439
Unladen Swallow; initial patch by Alexandre Vassalotti.

Python/bltinmodule.c

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,6 @@ const char *Py_FileSystemDefaultEncoding = "utf-8";
3333
int Py_HasFileSystemDefaultEncoding = 1;
3434
#endif
3535

36-
int
37-
_Py_SetFileSystemEncoding(PyObject *s)
38-
{
39-
PyObject *defenc, *codec;
40-
if (!PyUnicode_Check(s)) {
41-
PyErr_BadInternalCall();
42-
return -1;
43-
}
44-
defenc = _PyUnicode_AsDefaultEncodedString(s, NULL);
45-
if (!defenc)
46-
return -1;
47-
codec = _PyCodec_Lookup(PyBytes_AsString(defenc));
48-
if (codec == NULL)
49-
return -1;
50-
Py_DECREF(codec);
51-
if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding)
52-
/* A file system encoding was set at run-time */
53-
free((char*)Py_FileSystemDefaultEncoding);
54-
Py_FileSystemDefaultEncoding = strdup(PyBytes_AsString(defenc));
55-
Py_HasFileSystemDefaultEncoding = 0;
56-
return 0;
57-
}
58-
5936
static PyObject *
6037
builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
6138
{

Python/sysmodule.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -198,25 +198,6 @@ Return the encoding used to convert Unicode filenames in\n\
198198
operating system filenames."
199199
);
200200

201-
static PyObject *
202-
sys_setfilesystemencoding(PyObject *self, PyObject *args)
203-
{
204-
PyObject *new_encoding;
205-
if (!PyArg_ParseTuple(args, "U:setfilesystemencoding", &new_encoding))
206-
return NULL;
207-
if (_Py_SetFileSystemEncoding(new_encoding))
208-
return NULL;
209-
Py_INCREF(Py_None);
210-
return Py_None;
211-
}
212-
213-
PyDoc_STRVAR(setfilesystemencoding_doc,
214-
"setfilesystemencoding(string) -> None\n\
215-
\n\
216-
Set the encoding used to convert Unicode filenames in\n\
217-
operating system filenames."
218-
);
219-
220201
static PyObject *
221202
sys_intern(PyObject *self, PyObject *args)
222203
{
@@ -1012,8 +993,6 @@ static PyMethodDef sys_methods[] = {
1012993
#ifdef USE_MALLOPT
1013994
{"mdebug", sys_mdebug, METH_VARARGS},
1014995
#endif
1015-
{"setfilesystemencoding", sys_setfilesystemencoding, METH_VARARGS,
1016-
setfilesystemencoding_doc},
1017996
{"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
1018997
setcheckinterval_doc},
1019998
{"getcheckinterval", sys_getcheckinterval, METH_NOARGS,

0 commit comments

Comments
 (0)