]> granicus.if.org Git - python/commitdiff
Issue #9425: Create private _Py_stat() function
authorVictor Stinner <victor.stinner@haypocalc.com>
Sat, 14 Aug 2010 14:50:26 +0000 (14:50 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Sat, 14 Aug 2010 14:50:26 +0000 (14:50 +0000)
Use stat() or _wstat() depending on the OS.

Include/Python.h
Python/import.c

index d5ac13e299c577fbf55567f3afe5a1c3b1925208..eb5ea28b27d5156b30c9235c533710fef612ab02 100644 (file)
@@ -135,6 +135,11 @@ PyAPI_FUNC(wchar_t *) _Py_char2wchar(char *);
 PyAPI_FUNC(char*) _Py_wchar2char(const wchar_t *text);
 PyAPI_FUNC(FILE *) _Py_wfopen(const wchar_t *path, const wchar_t *mode);
 
+/* _Py_stat lives in import.c */
+#ifdef HAVE_STAT
+int _Py_stat(PyObject *unicode, struct stat *statbuf);
+#endif
+
 #ifdef __cplusplus
 }
 #endif
index 4cdce7470286446c2c3939bee3d805b260f5cfe6..79a378e3c52b9e711603143d56722149cf45a69b 100644 (file)
@@ -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)