bpo-36346: Make using the legacy Unicode C API optional (GH-21437)
Add compile time option USE_UNICODE_WCHAR_CACHE. Setting it to 0
makes the interpreter not using the wchar_t cache and the legacy Unicode C API.
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 7c8ba37..b9856b3 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -270,7 +270,14 @@
if (!PyUnicode_FSDecoder(nameobj, &stringobj)) {
return -1;
}
+#if USE_UNICODE_WCHAR_CACHE
+_Py_COMP_DIAG_PUSH
+_Py_COMP_DIAG_IGNORE_DEPR_DECLS
widename = PyUnicode_AsUnicode(stringobj);
+_Py_COMP_DIAG_POP
+#else /* USE_UNICODE_WCHAR_CACHE */
+ widename = PyUnicode_AsWideCharString(stringobj, NULL);
+#endif /* USE_UNICODE_WCHAR_CACHE */
if (widename == NULL)
return -1;
#else
@@ -491,6 +498,11 @@
internal_close(self);
done:
+#ifdef MS_WINDOWS
+#if !USE_UNICODE_WCHAR_CACHE
+ PyMem_Free(widename);
+#endif /* USE_UNICODE_WCHAR_CACHE */
+#endif
Py_CLEAR(stringobj);
return ret;
}