mirror of
https://github.com/AdaCore/cpython.git
synced 2026-02-12 12:57:15 -08:00
Handle importing pkg.mod by executing
__import__('mod', {'__packaging__': 'pkg', level=1) w/o properly (and
thus not segfaulting).
This commit is contained in:
@@ -1083,7 +1083,7 @@ def __import__(name, globals={}, locals={}, fromlist=[], level=0):
|
||||
return module
|
||||
else:
|
||||
cut_off = len(name) - len(name.partition('.')[0])
|
||||
return sys.modules[module.__name__[:-cut_off]]
|
||||
return sys.modules[module.__name__[:len(module.__name__)-cut_off]]
|
||||
else:
|
||||
return _handle_fromlist(module, fromlist, _gcd_import)
|
||||
|
||||
|
||||
@@ -193,6 +193,15 @@ class RelativeImports(unittest.TestCase):
|
||||
self.assertEqual(module.__name__, '__runpy_pkg__.uncle.cousin')
|
||||
self.relative_import_test(create, globals_, callback)
|
||||
|
||||
def test_import_relative_import_no_fromlist(self):
|
||||
# Import a relative module w/ no fromlist.
|
||||
create = ['crash.__init__', 'crash.mod']
|
||||
globals_ = [{'__package__': 'crash', '__name__': 'crash'}]
|
||||
def callback(global_):
|
||||
import_util.import_('crash')
|
||||
mod = import_util.import_('mod', global_, {}, [], 1)
|
||||
self.assertEqual(mod.__name__, 'crash.mod')
|
||||
self.relative_import_test(create, globals_, callback)
|
||||
|
||||
|
||||
def test_main():
|
||||
|
||||
@@ -3016,20 +3016,33 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
|
||||
Py_DECREF(partition);
|
||||
|
||||
if (level == 0) {
|
||||
final_mod = PyDict_GetItemWithError(interp->modules, front);
|
||||
final_mod = PyDict_GetItem(interp->modules, front);
|
||||
Py_DECREF(front);
|
||||
Py_XINCREF(final_mod);
|
||||
if (final_mod == NULL) {
|
||||
PyErr_Format(PyExc_KeyError,
|
||||
"%R not in sys.modules as expected", front);
|
||||
}
|
||||
else {
|
||||
Py_INCREF(final_mod);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Py_ssize_t cut_off = PyUnicode_GetLength(name) -
|
||||
PyUnicode_GetLength(front);
|
||||
Py_ssize_t abs_name_len = PyUnicode_GetLength(abs_name);
|
||||
PyObject *to_return = PyUnicode_Substring(name, 0,
|
||||
PyObject *to_return = PyUnicode_Substring(abs_name, 0,
|
||||
abs_name_len - cut_off);
|
||||
|
||||
final_mod = PyDict_GetItem(interp->modules, to_return);
|
||||
Py_INCREF(final_mod);
|
||||
Py_DECREF(to_return);
|
||||
if (final_mod == NULL) {
|
||||
PyErr_Format(PyExc_KeyError,
|
||||
"%R not in sys.modules as expected",
|
||||
to_return);
|
||||
}
|
||||
else {
|
||||
Py_INCREF(final_mod);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user