_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.
This commit is contained in:
Victor Stinner 2010-10-07 22:23:10 +00:00
parent b306d7594f
commit a4a759515e
2 changed files with 8 additions and 20 deletions

View File

@ -19,7 +19,7 @@ PyAPI_FUNC(int) _Py_wstat(
#ifdef HAVE_STAT #ifdef HAVE_STAT
PyAPI_FUNC(int) _Py_stat( PyAPI_FUNC(int) _Py_stat(
PyObject *unicode, PyObject *path,
struct stat *statbuf); struct stat *statbuf);
#endif #endif
@ -28,7 +28,7 @@ PyAPI_FUNC(FILE *) _Py_wfopen(
const wchar_t *mode); const wchar_t *mode);
PyAPI_FUNC(FILE*) _Py_fopen( PyAPI_FUNC(FILE*) _Py_fopen(
PyObject *unicode, PyObject *path,
const char *mode); const char *mode);
#ifdef HAVE_READLINK #ifdef HAVE_READLINK

View File

@ -215,24 +215,19 @@ _Py_wstat(const wchar_t* path, struct stat *buf)
PyErr_Occurred()) unicode error. */ PyErr_Occurred()) unicode error. */
int int
_Py_stat(PyObject *unicode, struct stat *statbuf) _Py_stat(PyObject *path, struct stat *statbuf)
{ {
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
wchar_t *path;
int err; int err;
struct _stat wstatbuf; struct _stat wstatbuf;
path = PyUnicode_AsWideCharString(unicode, NULL); err = _wstat(PyUnicode_AS_UNICODE(path), &wstatbuf);
if (path == NULL)
return -1;
err = _wstat(path, &wstatbuf);
PyMem_Free(path);
if (!err) if (!err)
statbuf->st_mode = wstatbuf.st_mode; statbuf->st_mode = wstatbuf.st_mode;
return err; return err;
#else #else
int ret; int ret;
PyObject *bytes = PyUnicode_EncodeFSDefault(unicode); PyObject *bytes = PyUnicode_EncodeFSDefault(path);
if (bytes == NULL) if (bytes == NULL)
return -1; return -1;
ret = stat(PyBytes_AS_STRING(bytes), statbuf); ret = stat(PyBytes_AS_STRING(bytes), statbuf);
@ -270,27 +265,20 @@ _Py_wfopen(const wchar_t *path, const wchar_t *mode)
PyErr_Occurred()) on unicode error */ PyErr_Occurred()) on unicode error */
FILE* FILE*
_Py_fopen(PyObject *unicode, const char *mode) _Py_fopen(PyObject *path, const char *mode)
{ {
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
wchar_t *path;
wchar_t wmode[10]; wchar_t wmode[10];
int usize; int usize;
FILE *f;
usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode)); usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
if (usize == 0) if (usize == 0)
return NULL; return NULL;
path = PyUnicode_AsWideCharString(unicode, NULL); return _wfopen(PyUnicode_AS_UNICODE(path), wmode);
if (path == NULL)
return NULL;
f = _wfopen(path, wmode);
PyMem_Free(path);
return f;
#else #else
FILE *f; FILE *f;
PyObject *bytes = PyUnicode_EncodeFSDefault(unicode); PyObject *bytes = PyUnicode_EncodeFSDefault(path);
if (bytes == NULL) if (bytes == NULL)
return NULL; return NULL;
f = fopen(PyBytes_AS_STRING(bytes), mode); f = fopen(PyBytes_AS_STRING(bytes), mode);