@@ -243,8 +243,39 @@ pg_EncodeString(PyObject *obj, const char *encoding, const char *errors,
243243static PyObject *
244244pg_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