Issue1952

classification
Title: __import__(): Handling of non-dict globals argument incompatible with CPython
Type: Severity: normal
Components: Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: fwierzbicki Nosy List: Arfrever, fwierzbicki
Priority: normal Keywords:

Created on 2012-07-30.22:09:48 by Arfrever, last changed 2012-08-02.22:49:31 by fwierzbicki.

Messages
msg7340 (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) Date: 2012-07-30.22:09:47
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
$
msg7344 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2012-08-02.22:49:30
Fixed for 2.5.3 in http://hg.python.org/jython/rev/bddb532f5a99 and merged into 2.7 in http://hg.python.org/jython/rev/b84e99394780
History
Date User Action Args
2012-08-02 22:49:31fwierzbickisetstatus: open -> closed
resolution: fixed
messages: + msg7344
2012-08-02 21:37:31fwierzbickisetpriority: normal
assignee: fwierzbicki
nosy: + fwierzbicki
2012-07-30 22:09:48Arfrevercreate