summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-10-07 22:23:10 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2010-10-07 22:23:10 (GMT)
commita4a759515e2e0114d41acdada3642265c01270ac (patch)
tree1929fc7dd922e10ec6d710a390b4f0fdcffaa1db
parentb306d7594ff0ed5ba666dbb26491a269abfbc9d7 (diff)
downloadcpython-a4a759515e2e0114d41acdada3642265c01270ac.zip
cpython-a4a759515e2e0114d41acdada3642265c01270ac.tar.gz
cpython-a4a759515e2e0114d41acdada3642265c01270ac.tar.bz2
_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.
-rw-r--r--Include/fileutils.h4
-rw-r--r--Python/fileutils.c24
2 files changed, 8 insertions, 20 deletions
diff --git a/Include/fileutils.h b/Include/fileutils.h
index 9b2d34d..1d8df15 100644
--- a/Include/fileutils.h
+++ b/Include/fileutils.h
@@ -19,7 +19,7 @@ PyAPI_FUNC(int) _Py_wstat(
#ifdef HAVE_STAT
PyAPI_FUNC(int) _Py_stat(
- PyObject *unicode,
+ PyObject *path,
struct stat *statbuf);
#endif
@@ -28,7 +28,7 @@ PyAPI_FUNC(FILE *) _Py_wfopen(
const wchar_t *mode);
PyAPI_FUNC(FILE*) _Py_fopen(
- PyObject *unicode,
+ PyObject *path,
const char *mode);
#ifdef HAVE_READLINK
diff --git a/Python/fileutils.c b/Python/fileutils.c
index 5d01867..bd6ab5d 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -215,24 +215,19 @@ _Py_wstat(const wchar_t* path, struct stat *buf)
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 @@ _Py_wfopen(const wchar_t *path, const wchar_t *mode)
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);