In CPython <3.3 __import__() ignores non-dict globals.
if (globals == NULL || !PyDict_Check(globals) || !level)
return Py_None;
$ python2.7 -c '__import__("os", None)'
$ python2.7 -c '__import__("os", [])'
$ python3.2 -c '__import__("os", None)'
$ python3.2 -c '__import__("os", [])'
$
In CPython >=3.3 __import__() ignores non-dict globals only if level is <= 0 and raises TypeError otherwise.
if (level > 0 && !PyDict_Check(given_globals)) {
PyErr_SetString(PyExc_TypeError, "globals must be a dict");
goto error;
}
$ python3.3 -c '__import__("os", None, level=0)'
$ python3.3 -c '__import__("os", [], level=0)'
$ python3.3 -c '__import__("os", None, level=1)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: globals must be a dict
$ python3.3 -c '__import__("os", [], level=1)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: globals must be a dict
$
In Jython 2.7 __import__() ignores globals=None and ignores other non-dict globals only if level == 0.
$ jython2.7 -c '__import__("os", None, level=-1)'
$ jython2.7 -c '__import__("os", None, level=0)'
$ jython2.7 -c '__import__("os", None, level=1)'
$ jython2.7 -c '__import__("os", [], level=-1)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: list indices must be integers
$ jython2.7 -c '__import__("os", [], level=0)'
$ jython2.7 -c '__import__("os", [], level=1)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: list indices must be integers
$ jython2.7 -c '__import__("os", 0, level=-1)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: 'int' object is unsubscriptable
$ jython2.7 -c '__import__("os", 0, level=0)'
$ jython2.7 -c '__import__("os", 0, level=1)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: 'int' object is unsubscriptable
$
|