summaryrefslogtreecommitdiffstats
path: root/Python/import.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-08-14 14:50:26 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2010-08-14 14:50:26 (GMT)
commit4f4402c4bb780a89c1d1c2564522e8ca901cbe17 (patch)
tree3a0c661bfcb5c4407eae886b58cbf4ba6a410812 /Python/import.c
parent4c9aa451756ab52dd1fba381469fdbb2201ee4c4 (diff)
downloadcpython-4f4402c4bb780a89c1d1c2564522e8ca901cbe17.zip
cpython-4f4402c4bb780a89c1d1c2564522e8ca901cbe17.tar.gz
cpython-4f4402c4bb780a89c1d1c2564522e8ca901cbe17.tar.bz2
Issue #9425: Create private _Py_stat() function
Use stat() or _wstat() depending on the OS.
Diffstat (limited to 'Python/import.c')
-rw-r--r--Python/import.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/Python/import.c b/Python/import.c
index 4cdce74..79a378e 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -1962,6 +1962,39 @@ case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
#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)