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;
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index 8552575..c7c1530 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -3147,9 +3147,7 @@
        it also helps Tcl find its encodings. */
     uexe = PyUnicode_FromWideChar(Py_GetProgramName(), -1);
     if (uexe) {
-        cexe = PyUnicode_AsEncodedString(uexe,
-                                         Py_FileSystemDefaultEncoding,
-                                         NULL);
+        cexe = PyUnicode_EncodeFSDefault(uexe);
         if (cexe)
             Tcl_FindExecutable(PyBytes_AsString(cexe));
         Py_XDECREF(cexe);
diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c
index d10a79d..d64c142 100644
--- a/Modules/grpmodule.c
+++ b/Modules/grpmodule.c
@@ -111,8 +111,7 @@
 
     if (!PyArg_ParseTuple(args, "U:getgrnam", &arg))
         return NULL;
-    if ((bytes = PyUnicode_AsEncodedString(arg, Py_FileSystemDefaultEncoding,
-                                           "surrogateescape")) == NULL)
+    if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
         return NULL;
     if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
         goto out;
diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c
index 35a387a..b303f95 100644
--- a/Modules/pwdmodule.c
+++ b/Modules/pwdmodule.c
@@ -132,9 +132,7 @@
 
     if (!PyArg_ParseTuple(args, "U:getpwnam", &arg))
         return NULL;
-    if ((bytes = PyUnicode_AsEncodedString(arg,
-                                           Py_FileSystemDefaultEncoding,
-                                           "surrogateescape")) == NULL)
+    if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
         return NULL;
     if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
         goto out;
diff --git a/Modules/spwdmodule.c b/Modules/spwdmodule.c
index da452e9..96707b4 100644
--- a/Modules/spwdmodule.c
+++ b/Modules/spwdmodule.c
@@ -118,9 +118,7 @@
 
     if (!PyArg_ParseTuple(args, "U:getspnam", &arg))
         return NULL;
-    if ((bytes = PyUnicode_AsEncodedString(arg,
-                                           Py_FileSystemDefaultEncoding,
-                                           "surrogateescape")) == NULL)
+    if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
         return NULL;
     if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
         goto out;