summaryrefslogtreecommitdiff
path: root/Python/fileutils.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-07-10 23:26:06 +0300
committerGitHub <noreply@github.com>2020-07-10 23:26:06 +0300
commit4c8f09d7cef8c7aa07d5b5232b5b64f63819a743 (patch)
tree2d90e13b6dc939f019a6cafc45ea604cbb90f584 /Python/fileutils.c
parent9650fe0197779b4dfded94be111e39c5810f098f (diff)
downloadcpython-git-4c8f09d7cef8c7aa07d5b5232b5b64f63819a743.tar.gz
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.
Diffstat (limited to 'Python/fileutils.c')
-rw-r--r--Python/fileutils.c23
1 files changed, 19 insertions, 4 deletions
diff --git a/Python/fileutils.c b/Python/fileutils.c
index 2c86828ba9..50ef3c174a 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -1008,15 +1008,21 @@ _Py_stat(PyObject *path, struct stat *statbuf)
#ifdef MS_WINDOWS
int err;
struct _stat wstatbuf;
- const wchar_t *wpath;
- wpath = _PyUnicode_AsUnicode(path);
+#if USE_UNICODE_WCHAR_CACHE
+ const wchar_t *wpath = _PyUnicode_AsUnicode(path);
+#else /* USE_UNICODE_WCHAR_CACHE */
+ wchar_t *wpath = PyUnicode_AsWideCharString(path, NULL);
+#endif /* USE_UNICODE_WCHAR_CACHE */
if (wpath == NULL)
return -2;
err = _wstat(wpath, &wstatbuf);
if (!err)
statbuf->st_mode = wstatbuf.st_mode;
+#if !USE_UNICODE_WCHAR_CACHE
+ PyMem_Free(wpath);
+#endif /* USE_UNICODE_WCHAR_CACHE */
return err;
#else
int ret;
@@ -1433,7 +1439,6 @@ _Py_fopen_obj(PyObject *path, const char *mode)
FILE *f;
int async_err = 0;
#ifdef MS_WINDOWS
- const wchar_t *wpath;
wchar_t wmode[10];
int usize;
@@ -1448,7 +1453,11 @@ _Py_fopen_obj(PyObject *path, const char *mode)
Py_TYPE(path));
return NULL;
}
- wpath = _PyUnicode_AsUnicode(path);
+#if USE_UNICODE_WCHAR_CACHE
+ const wchar_t *wpath = _PyUnicode_AsUnicode(path);
+#else /* USE_UNICODE_WCHAR_CACHE */
+ wchar_t *wpath = PyUnicode_AsWideCharString(path, NULL);
+#endif /* USE_UNICODE_WCHAR_CACHE */
if (wpath == NULL)
return NULL;
@@ -1456,6 +1465,9 @@ _Py_fopen_obj(PyObject *path, const char *mode)
wmode, Py_ARRAY_LENGTH(wmode));
if (usize == 0) {
PyErr_SetFromWindowsErr(0);
+#if !USE_UNICODE_WCHAR_CACHE
+ PyMem_Free(wpath);
+#endif /* USE_UNICODE_WCHAR_CACHE */
return NULL;
}
@@ -1465,6 +1477,9 @@ _Py_fopen_obj(PyObject *path, const char *mode)
Py_END_ALLOW_THREADS
} while (f == NULL
&& errno == EINTR && !(async_err = PyErr_CheckSignals()));
+#if !USE_UNICODE_WCHAR_CACHE
+ PyMem_Free(wpath);
+#endif /* USE_UNICODE_WCHAR_CACHE */
#else
PyObject *bytes;
const char *path_bytes;