diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2010-08-14 17:06:04 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2010-08-14 17:06:04 (GMT) |
commit | f52b705be42d61066a6f4668146376470c28d8d5 (patch) | |
tree | 4722871e9fbd289ff790467fd3c808a6bc2d5392 /Python | |
parent | 17d1e2abd8246aca3024563dea6b71a66d45cbf4 (diff) | |
download | cpython-f52b705be42d61066a6f4668146376470c28d8d5.zip cpython-f52b705be42d61066a6f4668146376470c28d8d5.tar.gz cpython-f52b705be42d61066a6f4668146376470c28d8d5.tar.bz2 |
Create _Py_fopen() for PyUnicodeObject path
Call _wfopen() on Windows, or fopen() otherwise. Return the new file object on
success, or NULL if the file cannot be open or (if PyErr_Occurred()) on unicode
error.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/import.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Python/import.c b/Python/import.c index 84ddc03..bb3756e 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1960,6 +1960,39 @@ case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name) #endif } +/* Call _wfopen() on Windows, or fopen() otherwise. Return the new file + object on success, or NULL if the file cannot be open or (if + PyErr_Occurred()) on unicode error */ + +FILE* +_Py_fopen(PyObject *unicode, const char *mode) +{ +#ifdef MS_WINDOWS + wchar_t path[MAXPATHLEN+1]; + wchar_t wmode[10]; + Py_ssize_t len; + int usize; + + len = PyUnicode_AsWideChar((PyUnicodeObject*)unicode, path, MAXPATHLEN); + if (len == -1) + return NULL; + path[len] = L'\0'; + + usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode)); + if (usize == 0) + return NULL; + + return _wfopen(path, wmode); +#else + FILE *f; + PyObject *bytes = PyUnicode_EncodeFSDefault(unicode); + if (bytes == NULL) + return NULL; + f = fopen(PyBytes_AS_STRING(bytes), mode); + Py_DECREF(bytes); + return f; +#endif +} #ifdef HAVE_STAT |