Mercurial > cpython-lru_cache-weakref
diff Python/import.c @ 63985:953010c0b168
Issue #9425: Create private _Py_stat() function
Use stat() or _wstat() depending on the OS.
| author | Victor Stinner <victor.stinner@haypocalc.com> |
|---|---|
| date | Sat, 14 Aug 2010 14:50:26 +0000 |
| parents | 03e884fac4db |
| children | b7220e429abb |
line wrap: on
line diff
--- a/Python/import.c Sat Aug 14 13:43:37 2010 +0000 +++ b/Python/import.c Sat Aug 14 14:50:26 2010 +0000 @@ -1962,6 +1962,39 @@ #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[MAXPATHLEN+1]; + Py_ssize_t len; + int err; + struct _stat wstatbuf; + + len = PyUnicode_AsWideChar((PyUnicodeObject*)unicode, path, + sizeof(path) / sizeof(path[0])); + if (len == -1) + return -1; + err = _wstat(path, &wstatbuf); + 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)
