Skip to content

Commit 0b39764

Browse files
authored
Merge pull request #2315 from Starbuck5/replace-pyfilesystemdefaultencoding
Replace Py_FileSystemDefaultEncoding
2 parents c41f75a + 97a35d9 commit 0b39764

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

docs/reST/ref/pygame.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ object instead of the module, which can be used to test for availability.
177177
This function is used in encoding file paths. Keyword arguments are
178178
supported.
179179

180+
This function is not needed for normal pygame-ce usage.
181+
180182
.. versionaddedold:: 1.9.2 (primarily for use in unit tests)
181183

182184
.. ## pygame.encode_string ##
@@ -197,6 +199,8 @@ object instead of the module, which can be used to test for availability.
197199
codec as returned by ``sys.getfilesystemencoding()``. Keyword arguments are
198200
supported.
199201

202+
This function is not needed for normal pygame-ce usage.
203+
200204
.. versionaddedold:: 1.9.2 (primarily for use in unit tests)
201205

202206
.. ## pygame.encode_file_path ##

src_c/rwobject.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,39 @@ pg_EncodeString(PyObject *obj, const char *encoding, const char *errors,
243243
static PyObject *
244244
pg_EncodeFilePath(PyObject *obj, PyObject *eclass)
245245
{
246-
PyObject *result = pg_EncodeString(obj, Py_FileSystemDefaultEncoding,
247-
UNICODE_DEF_FS_ERROR, eclass);
246+
/* All of this code is a replacement for Py_FileSystemDefaultEncoding,
247+
* which is deprecated in Python 3.12
248+
*
249+
* But I'm not sure of the use of this function, so maybe it should be
250+
* deprecated. */
251+
252+
PyObject *sys_module = PyImport_ImportModule("sys");
253+
if (sys_module == NULL) {
254+
return NULL;
255+
}
256+
PyObject *system_encoding_obj =
257+
PyObject_CallMethod(sys_module, "getfilesystemencoding", NULL);
258+
if (system_encoding_obj == NULL) {
259+
Py_DECREF(sys_module);
260+
return NULL;
261+
}
262+
Py_DECREF(sys_module);
263+
const char *encoding = PyUnicode_AsUTF8(system_encoding_obj);
264+
if (encoding == NULL) {
265+
Py_DECREF(system_encoding_obj);
266+
return NULL;
267+
}
268+
269+
/* End code replacement section */
270+
271+
if (obj == NULL) {
272+
PyErr_SetString(PyExc_SyntaxError, "Forwarded exception");
273+
}
274+
275+
PyObject *result =
276+
pg_EncodeString(obj, encoding, UNICODE_DEF_FS_ERROR, eclass);
277+
Py_DECREF(system_encoding_obj);
278+
248279
if (result == NULL || result == Py_None) {
249280
return result;
250281
}
@@ -817,9 +848,6 @@ pg_encode_file_path(PyObject *self, PyObject *args, PyObject *keywds)
817848
return NULL;
818849
}
819850

820-
if (obj == NULL) {
821-
PyErr_SetString(PyExc_SyntaxError, "Forwarded exception");
822-
}
823851
return pg_EncodeFilePath(obj, eclass);
824852
}
825853

0 commit comments

Comments
 (0)