Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd0850b

Browse files
author
Victor Stinner
committed
import.c now catchs _Py_stat() exceptions
_Py_stat() now returns -2 if an exception was raised.
1 parent 7bfe899 commit bd0850b

3 files changed

Lines changed: 37 additions & 13 deletions

File tree

Modules/zipimport.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
102102
int rv;
103103

104104
rv = _Py_stat(filename, &statbuf);
105+
if (rv == -2)
106+
goto error;
105107
if (rv == 0) {
106108
/* it exists */
107109
if (!S_ISREG(statbuf.st_mode))
108110
/* it's a not file */
109111
Py_CLEAR(filename);
110112
break;
111113
}
112-
else if (PyErr_Occurred())
113-
goto error;
114114
Py_CLEAR(filename);
115115
/* back up one path element */
116116
flen = PyUnicode_FindChar(path, SEP, 0, flen, -1);

Python/fileutils.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ _Py_wstat(const wchar_t* path, struct stat *buf)
240240
/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
241241
call stat() otherwise. Only fill st_mode attribute on Windows.
242242
243-
Return 0 on success, -1 on _wstat() / stat() error or (if PyErr_Occurred())
244-
unicode error. */
243+
Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
244+
raised. */
245245

246246
int
247247
_Py_stat(PyObject *path, struct stat *statbuf)
@@ -253,7 +253,7 @@ _Py_stat(PyObject *path, struct stat *statbuf)
253253

254254
wpath = PyUnicode_AsUnicode(path);
255255
if (wpath == NULL)
256-
return -1;
256+
return -2;
257257
err = _wstat(wpath, &wstatbuf);
258258
if (!err)
259259
statbuf->st_mode = wstatbuf.st_mode;
@@ -262,7 +262,7 @@ _Py_stat(PyObject *path, struct stat *statbuf)
262262
int ret;
263263
PyObject *bytes = PyUnicode_EncodeFSDefault(path);
264264
if (bytes == NULL)
265-
return -1;
265+
return -2;
266266
ret = stat(PyBytes_AS_STRING(bytes), statbuf);
267267
Py_DECREF(bytes);
268268
return ret;

Python/import.c

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,7 @@ get_sourcefile(PyObject *filename)
15271527
Py_UCS4 *fileuni;
15281528
PyObject *py;
15291529
struct stat statbuf;
1530+
int err;
15301531

15311532
len = PyUnicode_GET_LENGTH(filename);
15321533
if (len == 0)
@@ -1554,7 +1555,10 @@ get_sourcefile(PyObject *filename)
15541555
if (py == NULL)
15551556
goto error;
15561557

1557-
if (_Py_stat(py, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
1558+
err = _Py_stat(py, &statbuf);
1559+
if (err == -2)
1560+
goto error;
1561+
if (err == 0 && S_ISREG(statbuf.st_mode)) {
15581562
PyMem_Free(fileuni);
15591563
return py;
15601564
}
@@ -1760,7 +1764,7 @@ find_module_path(PyObject *fullname, PyObject *name, PyObject *path,
17601764
Py_ssize_t len, pos;
17611765
struct stat statbuf;
17621766
static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
1763-
int result, addsep;
1767+
int err, result, addsep;
17641768

17651769
if (PyUnicode_Check(path)) {
17661770
Py_INCREF(path);
@@ -1844,7 +1848,12 @@ find_module_path(PyObject *fullname, PyObject *name, PyObject *path,
18441848
/* Check for package import (buf holds a directory name,
18451849
and there's an __init__ module in that directory */
18461850
#ifdef HAVE_STAT
1847-
if (_Py_stat(filename, &statbuf) == 0 && /* it exists */
1851+
err = _Py_stat(filename, &statbuf);
1852+
if (err == -2) {
1853+
result = -1;
1854+
goto out;
1855+
}
1856+
if (err == 0 && /* it exists */
18481857
S_ISDIR(statbuf.st_mode)) /* it's a directory */
18491858
{
18501859
int match;
@@ -1905,6 +1914,7 @@ find_module_path_list(PyObject *fullname, PyObject *name,
19051914
FILE *fp = NULL;
19061915
PyObject *prefix, *filename;
19071916
int match;
1917+
int err;
19081918

19091919
npath = PyList_Size(search_path_list);
19101920
for (i = 0; i < npath; i++) {
@@ -1944,8 +1954,13 @@ find_module_path_list(PyObject *fullname, PyObject *name,
19441954
if (Py_VerboseFlag > 1)
19451955
PySys_FormatStderr("# trying %R\n", filename);
19461956

1947-
if (_Py_stat(filename, &statbuf) != 0 || S_ISDIR(statbuf.st_mode))
1948-
{
1957+
err = _Py_stat(filename, &statbuf);
1958+
if (err == -2) {
1959+
Py_DECREF(prefix);
1960+
Py_DECREF(filename);
1961+
return NULL;
1962+
}
1963+
if (err != 0 || S_ISDIR(statbuf.st_mode)) {
19491964
/* it doesn't exist, or it's a directory */
19501965
Py_DECREF(filename);
19511966
continue;
@@ -2345,11 +2360,15 @@ find_init_module(PyObject *directory)
23452360
struct stat statbuf;
23462361
PyObject *filename;
23472362
int match;
2363+
int err;
23482364

23492365
filename = PyUnicode_FromFormat("%U%c__init__.py", directory, SEP);
23502366
if (filename == NULL)
23512367
return -1;
2352-
if (_Py_stat(filename, &statbuf) == 0) {
2368+
err = _Py_stat(filename, &statbuf);
2369+
if (err == -2)
2370+
return -1;
2371+
if (err == 0) {
23532372
/* 3=len(".py") */
23542373
match = case_ok(filename, -3, initstr);
23552374
if (match < 0) {
@@ -2367,7 +2386,12 @@ find_init_module(PyObject *directory)
23672386
directory, SEP, Py_OptimizeFlag ? 'o' : 'c');
23682387
if (filename == NULL)
23692388
return -1;
2370-
if (_Py_stat(filename, &statbuf) == 0) {
2389+
err = _Py_stat(filename, &statbuf);
2390+
if (err == -2) {
2391+
Py_DECREF(filename);
2392+
return -1;
2393+
}
2394+
if (err == 0) {
23712395
/* 4=len(".pyc") */
23722396
match = case_ok(filename, -4, initstr);
23732397
if (match < 0) {

0 commit comments

Comments
 (0)