mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #23752: _Py_fstat() is now responsible to raise the Python exception
Add _Py_fstat_noraise() function when a Python exception is not welcome.
This commit is contained in:
parent
2e1c4e5db2
commit
e134a7fe36
12 changed files with 89 additions and 57 deletions
|
@ -41,12 +41,16 @@ struct _Py_stat_struct {
|
||||||
|
|
||||||
PyAPI_FUNC(int) _Py_fstat(
|
PyAPI_FUNC(int) _Py_fstat(
|
||||||
int fd,
|
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 */
|
#endif /* Py_LIMITED_API */
|
||||||
|
|
||||||
PyAPI_FUNC(int) _Py_stat(
|
PyAPI_FUNC(int) _Py_stat(
|
||||||
PyObject *path,
|
PyObject *path,
|
||||||
struct stat *statbuf);
|
struct stat *status);
|
||||||
|
|
||||||
#ifndef Py_LIMITED_API
|
#ifndef Py_LIMITED_API
|
||||||
PyAPI_FUNC(int) _Py_open(
|
PyAPI_FUNC(int) _Py_open(
|
||||||
|
|
|
@ -399,10 +399,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
|
||||||
}
|
}
|
||||||
|
|
||||||
self->blksize = DEFAULT_BUFFER_SIZE;
|
self->blksize = DEFAULT_BUFFER_SIZE;
|
||||||
if (_Py_fstat(self->fd, &fdfstat) < 0) {
|
if (_Py_fstat(self->fd, &fdfstat) < 0)
|
||||||
PyErr_SetFromErrno(PyExc_OSError);
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
|
||||||
#if defined(S_ISDIR) && defined(EISDIR)
|
#if defined(S_ISDIR) && defined(EISDIR)
|
||||||
/* On Unix, open will succeed for directories.
|
/* On Unix, open will succeed for directories.
|
||||||
In Python, there should be no file objects referring to
|
In Python, there should be no file objects referring to
|
||||||
|
@ -589,7 +587,7 @@ new_buffersize(fileio *self, size_t currentsize)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
fileio_readall(fileio *self)
|
fileio_readall(fileio *self)
|
||||||
{
|
{
|
||||||
struct _Py_stat_struct st;
|
struct _Py_stat_struct status;
|
||||||
Py_off_t pos, end;
|
Py_off_t pos, end;
|
||||||
PyObject *result;
|
PyObject *result;
|
||||||
Py_ssize_t bytes_read = 0;
|
Py_ssize_t bytes_read = 0;
|
||||||
|
@ -606,8 +604,8 @@ fileio_readall(fileio *self)
|
||||||
#else
|
#else
|
||||||
pos = lseek(self->fd, 0L, SEEK_CUR);
|
pos = lseek(self->fd, 0L, SEEK_CUR);
|
||||||
#endif
|
#endif
|
||||||
if (_Py_fstat(self->fd, &st) == 0)
|
if (_Py_fstat_noraise(self->fd, &status) == 0)
|
||||||
end = st.st_size;
|
end = status.st_size;
|
||||||
else
|
else
|
||||||
end = (Py_off_t)-1;
|
end = (Py_off_t)-1;
|
||||||
|
|
||||||
|
|
|
@ -753,9 +753,11 @@ Py_Main(int argc, wchar_t **argv)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
struct _Py_stat_struct sb;
|
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)) {
|
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);
|
fclose(fp);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -465,15 +465,13 @@ mmap_size_method(mmap_object *self,
|
||||||
|
|
||||||
#ifdef UNIX
|
#ifdef UNIX
|
||||||
{
|
{
|
||||||
struct _Py_stat_struct buf;
|
struct _Py_stat_struct status;
|
||||||
if (-1 == _Py_fstat(self->fd, &buf)) {
|
if (_Py_fstat(self->fd, &status) == -1)
|
||||||
PyErr_SetFromErrno(PyExc_OSError);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
#ifdef HAVE_LARGEFILE_SUPPORT
|
#ifdef HAVE_LARGEFILE_SUPPORT
|
||||||
return PyLong_FromLongLong(buf.st_size);
|
return PyLong_FromLongLong(status.st_size);
|
||||||
#else
|
#else
|
||||||
return PyLong_FromLong(buf.st_size);
|
return PyLong_FromLong(status.st_size);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#endif /* UNIX */
|
#endif /* UNIX */
|
||||||
|
@ -1112,7 +1110,7 @@ _GetMapSize(PyObject *o, const char* param)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
|
new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
|
||||||
{
|
{
|
||||||
struct _Py_stat_struct st;
|
struct _Py_stat_struct status;
|
||||||
mmap_object *m_obj;
|
mmap_object *m_obj;
|
||||||
PyObject *map_size_obj = NULL;
|
PyObject *map_size_obj = NULL;
|
||||||
Py_ssize_t map_size;
|
Py_ssize_t map_size;
|
||||||
|
@ -1177,25 +1175,26 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
|
||||||
if (fd != -1)
|
if (fd != -1)
|
||||||
(void)fcntl(fd, F_FULLFSYNC);
|
(void)fcntl(fd, F_FULLFSYNC);
|
||||||
#endif
|
#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 (map_size == 0) {
|
||||||
if (st.st_size == 0) {
|
if (status.st_size == 0) {
|
||||||
PyErr_SetString(PyExc_ValueError,
|
PyErr_SetString(PyExc_ValueError,
|
||||||
"cannot mmap an empty file");
|
"cannot mmap an empty file");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (offset >= st.st_size) {
|
if (offset >= status.st_size) {
|
||||||
PyErr_SetString(PyExc_ValueError,
|
PyErr_SetString(PyExc_ValueError,
|
||||||
"mmap offset is greater than file size");
|
"mmap offset is greater than file size");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (st.st_size - offset > PY_SSIZE_T_MAX) {
|
if (status.st_size - offset > PY_SSIZE_T_MAX) {
|
||||||
PyErr_SetString(PyExc_ValueError,
|
PyErr_SetString(PyExc_ValueError,
|
||||||
"mmap length is too large");
|
"mmap length is too large");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
map_size = (Py_ssize_t) (st.st_size - offset);
|
map_size = (Py_ssize_t) (status.st_size - offset);
|
||||||
} else if (offset + map_size > st.st_size) {
|
} else if (offset + map_size > status.st_size) {
|
||||||
PyErr_SetString(PyExc_ValueError,
|
PyErr_SetString(PyExc_ValueError,
|
||||||
"mmap length is greater than file size");
|
"mmap length is greater than file size");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
@ -351,7 +351,7 @@ static int win32_can_symlink = 0;
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
# define STAT win32_stat
|
# define STAT win32_stat
|
||||||
# define LSTAT win32_lstat
|
# define LSTAT win32_lstat
|
||||||
# define FSTAT _Py_fstat
|
# define FSTAT _Py_fstat_noraise
|
||||||
# define STRUCT_STAT struct _Py_stat_struct
|
# define STRUCT_STAT struct _Py_stat_struct
|
||||||
#else
|
#else
|
||||||
# define STAT stat
|
# define STAT stat
|
||||||
|
|
|
@ -503,7 +503,7 @@ signal_siginterrupt(PyObject *self, PyObject *args)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
signal_set_wakeup_fd(PyObject *self, PyObject *args)
|
signal_set_wakeup_fd(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
struct _Py_stat_struct st;
|
struct _Py_stat_struct status;
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
PyObject *fdobj;
|
PyObject *fdobj;
|
||||||
SOCKET_T sockfd, old_sockfd;
|
SOCKET_T sockfd, old_sockfd;
|
||||||
|
@ -559,10 +559,8 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_Py_fstat(fd, &st) != 0) {
|
if (_Py_fstat(fd, &status) != 0)
|
||||||
PyErr_SetExcFromWindowsErr(PyExc_OSError, GetLastError());
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
/* on Windows, a file cannot be set to non-blocking mode */
|
/* on Windows, a file cannot be set to non-blocking mode */
|
||||||
}
|
}
|
||||||
|
@ -591,10 +589,8 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_Py_fstat(fd, &st) != 0) {
|
if (_Py_fstat(fd, &status) != 0)
|
||||||
PyErr_SetFromErrno(PyExc_OSError);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
blocking = _Py_get_blocking(fd);
|
blocking = _Py_get_blocking(fd);
|
||||||
if (blocking < 0)
|
if (blocking < 0)
|
||||||
|
|
|
@ -35,7 +35,7 @@ main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
char *inpath, *outpath;
|
char *inpath, *outpath;
|
||||||
FILE *infile = NULL, *outfile = NULL;
|
FILE *infile = NULL, *outfile = NULL;
|
||||||
struct _Py_stat_struct st;
|
struct _Py_stat_struct status;
|
||||||
size_t text_size, data_size, n;
|
size_t text_size, data_size, n;
|
||||||
char *text = NULL;
|
char *text = NULL;
|
||||||
unsigned char *data;
|
unsigned char *data;
|
||||||
|
@ -54,11 +54,11 @@ main(int argc, char *argv[])
|
||||||
fprintf(stderr, "cannot open '%s' for reading\n", inpath);
|
fprintf(stderr, "cannot open '%s' for reading\n", inpath);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if (_Py_fstat(fileno(infile), &st)) {
|
if (_Py_fstat_noraise(fileno(infile), &status)) {
|
||||||
fprintf(stderr, "cannot fstat '%s'\n", inpath);
|
fprintf(stderr, "cannot fstat '%s'\n", inpath);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
text_size = st.st_size;
|
text_size = status.st_size;
|
||||||
text = (char *) malloc(text_size + 1);
|
text = (char *) malloc(text_size + 1);
|
||||||
if (text == NULL) {
|
if (text == NULL) {
|
||||||
fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size);
|
fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size);
|
||||||
|
|
|
@ -71,22 +71,20 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
|
||||||
|
|
||||||
if (fp != NULL) {
|
if (fp != NULL) {
|
||||||
int i;
|
int i;
|
||||||
struct _Py_stat_struct statb;
|
struct _Py_stat_struct status;
|
||||||
if (_Py_fstat(fileno(fp), &statb) == -1) {
|
if (_Py_fstat(fileno(fp), &status) == -1)
|
||||||
PyErr_SetFromErrno(PyExc_IOError);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
for (i = 0; i < nhandles; i++) {
|
for (i = 0; i < nhandles; i++) {
|
||||||
if (statb.st_dev == handles[i].dev &&
|
if (status.st_dev == handles[i].dev &&
|
||||||
statb.st_ino == handles[i].ino) {
|
status.st_ino == handles[i].ino) {
|
||||||
p = (dl_funcptr) dlsym(handles[i].handle,
|
p = (dl_funcptr) dlsym(handles[i].handle,
|
||||||
funcname);
|
funcname);
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nhandles < 128) {
|
if (nhandles < 128) {
|
||||||
handles[nhandles].dev = statb.st_dev;
|
handles[nhandles].dev = status.st_dev;
|
||||||
handles[nhandles].ino = statb.st_ino;
|
handles[nhandles].ino = status.st_ino;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -565,7 +565,8 @@ attributes_to_mode(DWORD attr)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
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));
|
memset(result, 0, sizeof(*result));
|
||||||
result->st_mode = attributes_to_mode(info->dwFileAttributes);
|
result->st_mode = attributes_to_mode(info->dwFileAttributes);
|
||||||
|
@ -595,9 +596,12 @@ _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
|
||||||
files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger
|
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
|
than 2 GB because the file size type is an signed 32-bit integer: see issue
|
||||||
#23152.
|
#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
|
int
|
||||||
_Py_fstat(int fd, struct _Py_stat_struct *result)
|
_Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
|
||||||
{
|
{
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
BY_HANDLE_FILE_INFORMATION info;
|
BY_HANDLE_FILE_INFORMATION info;
|
||||||
|
@ -619,22 +623,21 @@ _Py_fstat(int fd, struct _Py_stat_struct *result)
|
||||||
SetLastError(ERROR_INVALID_HANDLE);
|
SetLastError(ERROR_INVALID_HANDLE);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
memset(result, 0, sizeof(*result));
|
memset(status, 0, sizeof(*status));
|
||||||
|
|
||||||
type = GetFileType(h);
|
type = GetFileType(h);
|
||||||
if (type == FILE_TYPE_UNKNOWN) {
|
if (type == FILE_TYPE_UNKNOWN) {
|
||||||
DWORD error = GetLastError();
|
DWORD error = GetLastError();
|
||||||
if (error != 0) {
|
if (error != 0)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
/* else: valid but unknown file */
|
/* else: valid but unknown file */
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type != FILE_TYPE_DISK) {
|
if (type != FILE_TYPE_DISK) {
|
||||||
if (type == FILE_TYPE_CHAR)
|
if (type == FILE_TYPE_CHAR)
|
||||||
result->st_mode = _S_IFCHR;
|
status->st_mode = _S_IFCHR;
|
||||||
else if (type == FILE_TYPE_PIPE)
|
else if (type == FILE_TYPE_PIPE)
|
||||||
result->st_mode = _S_IFIFO;
|
status->st_mode = _S_IFIFO;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -642,15 +645,48 @@ _Py_fstat(int fd, struct _Py_stat_struct *result)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
_Py_attribute_data_to_stat(&info, 0, result);
|
_Py_attribute_data_to_stat(&info, 0, status);
|
||||||
/* specific to fstat() */
|
/* specific to fstat() */
|
||||||
result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
|
status->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
return fstat(fd, result);
|
return fstat(fd, status);
|
||||||
#endif
|
#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 _wstat() on Windows, or encode the path to the filesystem encoding and
|
||||||
call stat() otherwise. Only fill st_mode attribute on Windows.
|
call stat() otherwise. Only fill st_mode attribute on Windows.
|
||||||
|
|
|
@ -1486,7 +1486,7 @@ static off_t
|
||||||
getfilesize(FILE *fp)
|
getfilesize(FILE *fp)
|
||||||
{
|
{
|
||||||
struct _Py_stat_struct st;
|
struct _Py_stat_struct st;
|
||||||
if (_Py_fstat(fileno(fp), &st) != 0)
|
if (_Py_fstat_noraise(fileno(fp), &st) != 0)
|
||||||
return -1;
|
return -1;
|
||||||
#if SIZEOF_OFF_T == 4
|
#if SIZEOF_OFF_T == 4
|
||||||
else if (st.st_size >= INT_MAX)
|
else if (st.st_size >= INT_MAX)
|
||||||
|
|
|
@ -221,7 +221,7 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
|
||||||
|
|
||||||
if (urandom_cache.fd >= 0) {
|
if (urandom_cache.fd >= 0) {
|
||||||
/* Does the fd point to the same thing as before? (issue #21207) */
|
/* 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_dev != urandom_cache.st_dev
|
||||||
|| st.st_ino != urandom_cache.st_ino) {
|
|| st.st_ino != urandom_cache.st_ino) {
|
||||||
/* Something changed: forget the cached fd (but don't close it,
|
/* Something changed: forget the cached fd (but don't close it,
|
||||||
|
@ -250,7 +250,6 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (_Py_fstat(fd, &st)) {
|
if (_Py_fstat(fd, &st)) {
|
||||||
PyErr_SetFromErrno(PyExc_OSError);
|
|
||||||
close(fd);
|
close(fd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1690,7 +1690,7 @@ _PySys_Init(void)
|
||||||
#if !defined(MS_WINDOWS)
|
#if !defined(MS_WINDOWS)
|
||||||
{
|
{
|
||||||
struct _Py_stat_struct sb;
|
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)) {
|
S_ISDIR(sb.st_mode)) {
|
||||||
/* There's nothing more we can do. */
|
/* There's nothing more we can do. */
|
||||||
/* Py_FatalError() will core dump, so just exit. */
|
/* Py_FatalError() will core dump, so just exit. */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue