Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-31286: Fixed stack usage in absolute imports with binding a submo… #3217

Merged
merged 3 commits into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,23 @@ def test_import_name_binding(self):
import test.support as y
self.assertIs(y, test.support, y.__name__)

def test_issue31286(self):
# import in a 'finally' block resulted in SystemError
try:
x = ...
finally:
import test.support.script_helper as x

# import in a 'while' loop resulted in stack overflow
i = 0
while i < 10:
import test.support.script_helper as x
i += 1

# import in a 'for' loop resulted in segmentation fault
for i in range(2):
import test.support.script_helper as x

def test_failing_reload(self):
# A failing reload should leave the module object in sys.modules.
source = TESTFN + os.extsep + "py"
Expand Down
26 changes: 16 additions & 10 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2673,28 +2673,34 @@ compiler_import_as(struct compiler *c, identifier name, identifier asname)
If there is a dot in name, we need to split it and emit a
IMPORT_FROM for each name.
*/
Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
PyUnicode_GET_LENGTH(name), 1);
Py_ssize_t len = PyUnicode_GET_LENGTH(name);
Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
if (dot == -2)
return 0;
if (dot != -1) {
/* Consume the base module name to get the first attribute */
Py_ssize_t pos = dot + 1;
while (dot != -1) {
while (1) {
Py_ssize_t pos = dot + 1;
PyObject *attr;
dot = PyUnicode_FindChar(name, '.', pos,
PyUnicode_GET_LENGTH(name), 1);
dot = PyUnicode_FindChar(name, '.', pos, len, 1);
if (dot == -2)
return 0;
attr = PyUnicode_Substring(name, pos,
(dot != -1) ? dot :
PyUnicode_GET_LENGTH(name));
attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
if (!attr)
return 0;
ADDOP_O(c, IMPORT_FROM, attr, names);
Py_DECREF(attr);
pos = dot + 1;
if (dot == -1) {
break;
}
ADDOP(c, ROT_TWO);
ADDOP(c, POP_TOP);
}
if (!compiler_nameop(c, asname, Store)) {
return 0;
}
ADDOP(c, POP_TOP);
return 1;
}
return compiler_nameop(c, asname, Store);
}
Expand Down