[Python-checkins] cpython: Issue #23752: _Py_fstat() is now responsible to raise the Python exception

victor.stinner python-checkins at python.org
Mon Mar 30 10:21:24 CEST 2015


https://hg.python.org/cpython/rev/128f92ae8bae
changeset:   95285:128f92ae8bae
parent:      95283:2fcd99929dba
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Mar 30 10:09:31 2015 +0200
summary:
  Issue #23752: _Py_fstat() is now responsible to raise the Python exception

Add _Py_fstat_noraise() function when a Python exception is not welcome.

files:
  Include/fileutils.h          |   8 ++-
  Modules/_io/fileio.c         |  10 +--
  Modules/main.c               |   6 +-
  Modules/mmapmodule.c         |  25 ++++-----
  Modules/posixmodule.c        |   2 +-
  Modules/signalmodule.c       |  10 +--
  Programs/_freeze_importlib.c |   6 +-
  Python/dynload_shlib.c       |  14 ++---
  Python/fileutils.c           |  58 +++++++++++++++++++----
  Python/marshal.c             |   2 +-
  Python/random.c              |   3 +-
  Python/sysmodule.c           |   2 +-
  12 files changed, 89 insertions(+), 57 deletions(-)


diff --git a/Include/fileutils.h b/Include/fileutils.h
--- a/Include/fileutils.h
+++ b/Include/fileutils.h
@@ -41,12 +41,16 @@
 
 PyAPI_FUNC(int) _Py_fstat(
     int fd,
-    struct _Py_stat_struct *stat);
+    struct _Py_stat_struct *status);
+
+PyAPI_FUNC(int) _Py_fstat_noraise(
+    int fd,
+    struct _Py_stat_struct *status);
 #endif   /* Py_LIMITED_API */
 
 PyAPI_FUNC(int) _Py_stat(
     PyObject *path,
-    struct stat *statbuf);
+    struct stat *status);
 
 #ifndef Py_LIMITED_API
 PyAPI_FUNC(int) _Py_open(
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -399,10 +399,8 @@
     }
 
     self->blksize = DEFAULT_BUFFER_SIZE;
-    if (_Py_fstat(self->fd, &fdfstat) < 0) {
-        PyErr_SetFromErrno(PyExc_OSError);
+    if (_Py_fstat(self->fd, &fdfstat) < 0)
         goto error;
-    }
 #if defined(S_ISDIR) && defined(EISDIR)
     /* On Unix, open will succeed for directories.
        In Python, there should be no file objects referring to
@@ -589,7 +587,7 @@
 static PyObject *
 fileio_readall(fileio *self)
 {
-    struct _Py_stat_struct st;
+    struct _Py_stat_struct status;
     Py_off_t pos, end;
     PyObject *result;
     Py_ssize_t bytes_read = 0;
@@ -606,8 +604,8 @@
 #else
     pos = lseek(self->fd, 0L, SEEK_CUR);
 #endif
-    if (_Py_fstat(self->fd, &st) == 0)
-        end = st.st_size;
+    if (_Py_fstat_noraise(self->fd, &status) == 0)
+        end = status.st_size;
     else
         end = (Py_off_t)-1;
 
diff --git a/Modules/main.c b/Modules/main.c
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -753,9 +753,11 @@
             }
             {
                 struct _Py_stat_struct sb;
-                if (_Py_fstat(fileno(fp), &sb) == 0 &&
+                if (_Py_fstat_noraise(fileno(fp), &sb) == 0 &&
                     S_ISDIR(sb.st_mode)) {
-                    fprintf(stderr, "%ls: '%ls' is a directory, cannot continue\n", argv[0], filename);
+                    fprintf(stderr,
+                            "%ls: '%ls' is a directory, cannot continue\n",
+                            argv[0], filename);
                     fclose(fp);
                     return 1;
                 }
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -465,15 +465,13 @@
 
 #ifdef UNIX
     {
-        struct _Py_stat_struct buf;
-        if (-1 == _Py_fstat(self->fd, &buf)) {
-            PyErr_SetFromErrno(PyExc_OSError);
+        struct _Py_stat_struct status;
+        if (_Py_fstat(self->fd, &status) == -1)
             return NULL;
-        }
 #ifdef HAVE_LARGEFILE_SUPPORT
-        return PyLong_FromLongLong(buf.st_size);
+        return PyLong_FromLongLong(status.st_size);
 #else
-        return PyLong_FromLong(buf.st_size);
+        return PyLong_FromLong(status.st_size);
 #endif
     }
 #endif /* UNIX */
@@ -1112,7 +1110,7 @@
 static PyObject *
 new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
 {
-    struct _Py_stat_struct st;
+    struct _Py_stat_struct status;
     mmap_object *m_obj;
     PyObject *map_size_obj = NULL;
     Py_ssize_t map_size;
@@ -1177,25 +1175,26 @@
     if (fd != -1)
         (void)fcntl(fd, F_FULLFSYNC);
 #endif
-    if (fd != -1 && _Py_fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
+    if (fd != -1 && _Py_fstat_noraise(fd, &status) == 0
+        && S_ISREG(status.st_mode)) {
         if (map_size == 0) {
-            if (st.st_size == 0) {
+            if (status.st_size == 0) {
                 PyErr_SetString(PyExc_ValueError,
                                 "cannot mmap an empty file");
                 return NULL;
             }
-            if (offset >= st.st_size) {
+            if (offset >= status.st_size) {
                 PyErr_SetString(PyExc_ValueError,
                                 "mmap offset is greater than file size");
                 return NULL;
             }
-            if (st.st_size - offset > PY_SSIZE_T_MAX) {
+            if (status.st_size - offset > PY_SSIZE_T_MAX) {
                 PyErr_SetString(PyExc_ValueError,
                                  "mmap length is too large");
                 return NULL;
             }
-            map_size = (Py_ssize_t) (st.st_size - offset);
-        } else if (offset + map_size > st.st_size) {
+            map_size = (Py_ssize_t) (status.st_size - offset);
+        } else if (offset + map_size > status.st_size) {
             PyErr_SetString(PyExc_ValueError,
                             "mmap length is greater than file size");
             return NULL;
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -351,7 +351,7 @@
 #ifdef MS_WINDOWS
 #       define STAT win32_stat
 #       define LSTAT win32_lstat
-#       define FSTAT _Py_fstat
+#       define FSTAT _Py_fstat_noraise
 #       define STRUCT_STAT struct _Py_stat_struct
 #else
 #       define STAT stat
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -503,7 +503,7 @@
 static PyObject *
 signal_set_wakeup_fd(PyObject *self, PyObject *args)
 {
-    struct _Py_stat_struct st;
+    struct _Py_stat_struct status;
 #ifdef MS_WINDOWS
     PyObject *fdobj;
     SOCKET_T sockfd, old_sockfd;
@@ -559,10 +559,8 @@
                 return NULL;
             }
 
-            if (_Py_fstat(fd, &st) != 0) {
-                PyErr_SetExcFromWindowsErr(PyExc_OSError, GetLastError());
+            if (_Py_fstat(fd, &status) != 0)
                 return NULL;
-            }
 
             /* on Windows, a file cannot be set to non-blocking mode */
         }
@@ -591,10 +589,8 @@
             return NULL;
         }
 
-        if (_Py_fstat(fd, &st) != 0) {
-            PyErr_SetFromErrno(PyExc_OSError);
+        if (_Py_fstat(fd, &status) != 0)
             return NULL;
-        }
 
         blocking = _Py_get_blocking(fd);
         if (blocking < 0)
diff --git a/Programs/_freeze_importlib.c b/Programs/_freeze_importlib.c
--- a/Programs/_freeze_importlib.c
+++ b/Programs/_freeze_importlib.c
@@ -35,7 +35,7 @@
 {
     char *inpath, *outpath;
     FILE *infile = NULL, *outfile = NULL;
-    struct _Py_stat_struct st;
+    struct _Py_stat_struct status;
     size_t text_size, data_size, n;
     char *text = NULL;
     unsigned char *data;
@@ -54,11 +54,11 @@
         fprintf(stderr, "cannot open '%s' for reading\n", inpath);
         goto error;
     }
-    if (_Py_fstat(fileno(infile), &st)) {
+    if (_Py_fstat_noraise(fileno(infile), &status)) {
         fprintf(stderr, "cannot fstat '%s'\n", inpath);
         goto error;
     }
-    text_size = st.st_size;
+    text_size = status.st_size;
     text = (char *) malloc(text_size + 1);
     if (text == NULL) {
         fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size);
diff --git a/Python/dynload_shlib.c b/Python/dynload_shlib.c
--- a/Python/dynload_shlib.c
+++ b/Python/dynload_shlib.c
@@ -71,22 +71,20 @@
 
     if (fp != NULL) {
         int i;
-        struct _Py_stat_struct statb;
-        if (_Py_fstat(fileno(fp), &statb) == -1) {
-            PyErr_SetFromErrno(PyExc_IOError);
+        struct _Py_stat_struct status;
+        if (_Py_fstat(fileno(fp), &status) == -1)
             return NULL;
-        }
         for (i = 0; i < nhandles; i++) {
-            if (statb.st_dev == handles[i].dev &&
-                statb.st_ino == handles[i].ino) {
+            if (status.st_dev == handles[i].dev &&
+                status.st_ino == handles[i].ino) {
                 p = (dl_funcptr) dlsym(handles[i].handle,
                                        funcname);
                 return p;
             }
         }
         if (nhandles < 128) {
-            handles[nhandles].dev = statb.st_dev;
-            handles[nhandles].ino = statb.st_ino;
+            handles[nhandles].dev = status.st_dev;
+            handles[nhandles].ino = status.st_ino;
         }
     }
 
diff --git a/Python/fileutils.c b/Python/fileutils.c
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -565,7 +565,8 @@
 }
 
 void
-_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct _Py_stat_struct *result)
+_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
+                           struct _Py_stat_struct *result)
 {
     memset(result, 0, sizeof(*result));
     result->st_mode = attributes_to_mode(info->dwFileAttributes);
@@ -595,9 +596,12 @@
    files larger than 2 GB.  fstat() may fail with EOVERFLOW on files larger
    than 2 GB because the file size type is an signed 32-bit integer: see issue
    #23152.
-   */
+
+   On Windows, set the last Windows error and return nonzero on error. On
+   POSIX, set errno and return nonzero on error. Fill status and return 0 on
+   success. */
 int
-_Py_fstat(int fd, struct _Py_stat_struct *result)
+_Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
 {
 #ifdef MS_WINDOWS
     BY_HANDLE_FILE_INFORMATION info;
@@ -619,22 +623,21 @@
         SetLastError(ERROR_INVALID_HANDLE);
         return -1;
     }
-    memset(result, 0, sizeof(*result));
+    memset(status, 0, sizeof(*status));
 
     type = GetFileType(h);
     if (type == FILE_TYPE_UNKNOWN) {
         DWORD error = GetLastError();
-        if (error != 0) {
+        if (error != 0)
             return -1;
-        }
         /* else: valid but unknown file */
     }
 
     if (type != FILE_TYPE_DISK) {
         if (type == FILE_TYPE_CHAR)
-            result->st_mode = _S_IFCHR;
+            status->st_mode = _S_IFCHR;
         else if (type == FILE_TYPE_PIPE)
-            result->st_mode = _S_IFIFO;
+            status->st_mode = _S_IFIFO;
         return 0;
     }
 
@@ -642,15 +645,48 @@
         return -1;
     }
 
-    _Py_attribute_data_to_stat(&info, 0, result);
+    _Py_attribute_data_to_stat(&info, 0, status);
     /* specific to fstat() */
-    result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
+    status->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
     return 0;
 #else
-    return fstat(fd, result);
+    return fstat(fd, status);
 #endif
 }
 
+/* Return information about a file.
+
+   On POSIX, use fstat().
+
+   On Windows, use GetFileType() and GetFileInformationByHandle() which support
+   files larger than 2 GB.  fstat() may fail with EOVERFLOW on files larger
+   than 2 GB because the file size type is an signed 32-bit integer: see issue
+   #23152.
+
+   Raise an exception and return -1 on error. On Windows, set the last Windows
+   error on error. On POSIX, set errno on error. Fill status and return 0 on
+   success.
+
+   The GIL must be held. */
+int
+_Py_fstat(int fd, struct _Py_stat_struct *status)
+{
+    int res;
+
+    Py_BEGIN_ALLOW_THREADS
+    res = _Py_fstat_noraise(fd, status);
+    Py_END_ALLOW_THREADS
+
+    if (res != 0) {
+#ifdef MS_WINDOWS
+        PyErr_SetFromWindowsErr(0);
+#else
+        PyErr_SetFromErrno(PyExc_OSError);
+#endif
+        return -1;
+    }
+    return 0;
+}
 
 /* Call _wstat() on Windows, or encode the path to the filesystem encoding and
    call stat() otherwise. Only fill st_mode attribute on Windows.
diff --git a/Python/marshal.c b/Python/marshal.c
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -1486,7 +1486,7 @@
 getfilesize(FILE *fp)
 {
     struct _Py_stat_struct st;
-    if (_Py_fstat(fileno(fp), &st) != 0)
+    if (_Py_fstat_noraise(fileno(fp), &st) != 0)
         return -1;
 #if SIZEOF_OFF_T == 4
     else if (st.st_size >= INT_MAX)
diff --git a/Python/random.c b/Python/random.c
--- a/Python/random.c
+++ b/Python/random.c
@@ -221,7 +221,7 @@
 
     if (urandom_cache.fd >= 0) {
         /* Does the fd point to the same thing as before? (issue #21207) */
-        if (_Py_fstat(urandom_cache.fd, &st)
+        if (_Py_fstat_noraise(urandom_cache.fd, &st)
             || st.st_dev != urandom_cache.st_dev
             || st.st_ino != urandom_cache.st_ino) {
             /* Something changed: forget the cached fd (but don't close it,
@@ -250,7 +250,6 @@
         }
         else {
             if (_Py_fstat(fd, &st)) {
-                PyErr_SetFromErrno(PyExc_OSError);
                 close(fd);
                 return -1;
             }
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1690,7 +1690,7 @@
 #if !defined(MS_WINDOWS)
     {
         struct _Py_stat_struct sb;
-        if (_Py_fstat(fileno(stdin), &sb) == 0 &&
+        if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 &&
             S_ISDIR(sb.st_mode)) {
             /* There's nothing more we can do. */
             /* Py_FatalError() will core dump, so just exit. */

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list