Mercurial > cpython-lru_cache-weakref
diff Python/import.c @ 64003:d360311f0e52
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.
| author | Victor Stinner <victor.stinner@haypocalc.com> |
|---|---|
| date | Sat, 14 Aug 2010 17:06:04 +0000 |
| parents | b7220e429abb |
| children | 4943078c90c9 |
line wrap: on
line diff
--- a/Python/import.c Sat Aug 14 17:02:49 2010 +0000 +++ b/Python/import.c Sat Aug 14 17:06:04 2010 +0000 @@ -1960,6 +1960,39 @@ #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
