summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-09-23 19:37:03 +0200
committerVictor Stinner <victor.stinner@haypocalc.com>2011-09-23 19:37:03 +0200
commit63c22fac727a72f0a4da8f72b12cd5f9b480376b (patch)
tree8c3da37f17c59c36e9204e527c1ceea2a48695f7
parent51b719814e0c32fd0eea570d3769d323bea97cab (diff)
downloadcpython-git-63c22fac727a72f0a4da8f72b12cd5f9b480376b.tar.gz
Issue #7732: Fix a crash on importing a module if a directory has the same name
than a Python module (e.g. "__init__.py"): don't close the file twice. PyFile_FromFile() does also close the file if PyString_FromString() failed. It did already close the file on fill_file_fields() error (e.g. if the file is a directory).
-rw-r--r--Doc/c-api/file.rst3
-rw-r--r--Lib/test/test_import.py8
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/fileobject.c32
-rw-r--r--Python/import.c4
5 files changed, 33 insertions, 17 deletions
diff --git a/Doc/c-api/file.rst b/Doc/c-api/file.rst
index bdb8c49edd..93f6b64db2 100644
--- a/Doc/c-api/file.rst
+++ b/Doc/c-api/file.rst
@@ -55,7 +55,8 @@ change in future releases of Python.
Create a new :ctype:`PyFileObject` from the already-open standard C file
pointer, *fp*. The function *close* will be called when the file should be
- closed. Return *NULL* on failure.
+ closed. Return *NULL* and close the file using *close* on failure.
+ *close* is optional and can be set to *NULL*.
.. cfunction:: FILE* PyFile_AsFile(PyObject \*p)
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
index 5751901584..f654c332b3 100644
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -265,6 +265,14 @@ class ImportTests(unittest.TestCase):
"""))
script_helper.assert_python_ok(testfn)
+ def test_bug7732(self):
+ source = TESTFN + '.py'
+ os.mkdir(source)
+ try:
+ self.assertRaises(IOError, imp.find_module, TESTFN, ["."])
+ finally:
+ os.rmdir(source)
+
class PycRewritingTests(unittest.TestCase):
# Test that the `co_filename` attribute on code objects always points
diff --git a/Misc/NEWS b/Misc/NEWS
index 94782c3da0..b4dd2159c8 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@ What's New in Python 2.7.3?
Core and Builtins
-----------------
+- Issue #7732: Fix a crash on importing a module if a directory has the same
+ name than a Python module (e.g. "__init__.py"): don't close the file twice.
+
- Issue #12973: Fix overflow checks that invoked undefined behaviour in
int.__pow__. These overflow checks were causing int.__pow__ to produce
incorrect results with recent versions of Clang, as a result of the
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index 974d6424e4..edd839e8b0 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -468,28 +468,34 @@ close_the_file(PyFileObject *f)
PyObject *
PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
{
- PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
- NULL, NULL);
- if (f != NULL) {
- PyObject *o_name = PyString_FromString(name);
- if (o_name == NULL)
- return NULL;
- if (fill_file_fields(f, fp, o_name, mode, close) == NULL) {
- Py_DECREF(f);
- f = NULL;
- }
+ PyFileObject *f;
+ PyObject *o_name;
+
+ f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type, NULL, NULL);
+ if (f == NULL)
+ return NULL;
+ o_name = PyString_FromString(name);
+ if (o_name == NULL) {
+ if (close != NULL && fp != NULL)
+ close(fp);
+ Py_DECREF(f);
+ return NULL;
+ }
+ if (fill_file_fields(f, fp, o_name, mode, close) == NULL) {
+ Py_DECREF(f);
Py_DECREF(o_name);
+ return NULL;
}
- return (PyObject *) f;
+ Py_DECREF(o_name);
+ return (PyObject *)f;
}
PyObject *
PyFile_FromString(char *name, char *mode)
{
- extern int fclose(FILE *);
PyFileObject *f;
- f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
+ f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, NULL);
if (f != NULL) {
if (open_the_file(f, name, mode) == NULL) {
Py_DECREF(f);
diff --git a/Python/import.c b/Python/import.c
index 673d1b3324..c60ecfea5e 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -2845,10 +2845,8 @@ call_find_module(char *name, PyObject *path)
return NULL;
if (fp != NULL) {
fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
- if (fob == NULL) {
- fclose(fp);
+ if (fob == NULL)
return NULL;
- }
}
else {
fob = Py_None;