Issue #8715: Create PyUnicode_EncodeFSDefault() function: Encode a Unicode
object to Py_FileSystemDefaultEncoding with the "surrogateescape" error
handler, return a bytes object. If Py_FileSystemDefaultEncoding is not set,
fall back to UTF-8.
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 6ecce1b..4f450da 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -247,8 +247,7 @@
             if (u == NULL)
                 return -1;
 
-            stringobj = PyUnicode_AsEncodedString(
-                u, Py_FileSystemDefaultEncoding, "surrogateescape");
+            stringobj = PyUnicode_EncodeFSDefault(u);
             Py_DECREF(u);
             if (stringobj == NULL)
                 return -1;