Mercurial > cpython-lru_cache-weakref
diff Python/fileutils.c @ 65165:2147cc152227
_Py_stat() and _Py_fopen(): avoid PyUnicode_AsWideCharString() on Windows
On Windows, Py_UNICODE is wchar_t, so we can avoid the expensive Py_UNICODE*
=> wchar_t* conversion.
| author | Victor Stinner <victor.stinner@haypocalc.com> |
|---|---|
| date | Thu, 07 Oct 2010 22:23:10 +0000 |
| parents | 1f3919af8a53 |
| children | 8fd5d06462e6 |
line wrap: on
line diff
--- a/Python/fileutils.c Thu Oct 07 22:09:40 2010 +0000 +++ b/Python/fileutils.c Thu Oct 07 22:23:10 2010 +0000 @@ -215,24 +215,19 @@ PyErr_Occurred()) unicode error. */ int -_Py_stat(PyObject *unicode, struct stat *statbuf) +_Py_stat(PyObject *path, struct stat *statbuf) { #ifdef MS_WINDOWS - wchar_t *path; int err; struct _stat wstatbuf; - path = PyUnicode_AsWideCharString(unicode, NULL); - if (path == NULL) - return -1; - err = _wstat(path, &wstatbuf); - PyMem_Free(path); + err = _wstat(PyUnicode_AS_UNICODE(path), &wstatbuf); if (!err) statbuf->st_mode = wstatbuf.st_mode; return err; #else int ret; - PyObject *bytes = PyUnicode_EncodeFSDefault(unicode); + PyObject *bytes = PyUnicode_EncodeFSDefault(path); if (bytes == NULL) return -1; ret = stat(PyBytes_AS_STRING(bytes), statbuf); @@ -270,27 +265,20 @@ PyErr_Occurred()) on unicode error */ FILE* -_Py_fopen(PyObject *unicode, const char *mode) +_Py_fopen(PyObject *path, const char *mode) { #ifdef MS_WINDOWS - wchar_t *path; wchar_t wmode[10]; int usize; - FILE *f; usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode)); if (usize == 0) return NULL; - path = PyUnicode_AsWideCharString(unicode, NULL); - if (path == NULL) - return NULL; - f = _wfopen(path, wmode); - PyMem_Free(path); - return f; + return _wfopen(PyUnicode_AS_UNICODE(path), wmode); #else FILE *f; - PyObject *bytes = PyUnicode_EncodeFSDefault(unicode); + PyObject *bytes = PyUnicode_EncodeFSDefault(path); if (bytes == NULL) return NULL; f = fopen(PyBytes_AS_STRING(bytes), mode);
