Mercurial > cpython-lru_cache-weakref
diff Python/import.c @ 65162:39014f0abc32
Create fileutils.c/.h
* _Py_fopen() and _Py_stat() come from Python/import.c
* (_Py)_wrealpath() comes from Python/sysmodule.c
* _Py_char2wchar(), _Py_wchar2char() and _Py_wfopen() come from Modules/main.c
* (_Py)_wstat(), (_Py)_wgetcwd(), _Py_wreadlink() come from Modules/getpath.c
| author | Victor Stinner <victor.stinner@haypocalc.com> |
|---|---|
| date | Thu, 07 Oct 2010 21:45:39 +0000 |
| parents | e3ae5b00ba15 |
| children | bb30393d1ea1 |
line wrap: on
line diff
--- a/Python/import.c Thu Oct 07 11:06:49 2010 +0000 +++ b/Python/import.c Thu Oct 07 21:45:39 2010 +0000 @@ -1953,73 +1953,8 @@ #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; - 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; -#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 -/* Call _wstat() on Windows, or stat() otherwise. Only fill st_mode - attribute on Windows. Return 0 on success, -1 on stat error or (if - PyErr_Occurred()) unicode error. */ - -int -_Py_stat(PyObject *unicode, 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); - if (!err) - statbuf->st_mode = wstatbuf.st_mode; - return err; -#else - int ret; - PyObject *bytes = PyUnicode_EncodeFSDefault(unicode); - if (bytes == NULL) - return -1; - ret = stat(PyBytes_AS_STRING(bytes), statbuf); - Py_DECREF(bytes); - return ret; -#endif -} - /* Helper to look for __init__.py or __init__.py[co] in potential package */ static int find_init_module(char *buf)
