classification
Title: SystemError: Parent module 'foo' not loaded on import statement
Type: behavior Stage:
Components: Versions: Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ncoghlan, schmir, twouters
Priority: release blocker Keywords:

Created on 2008-06-27 20:35 by schmir, last changed 2008-07-13 21:07 by schmir. This issue is now closed.

Messages (7)
msg68850 - (view) Author: Ralf Schmitt (schmir) Date: 2008-06-27 20:35
The following short program work in python 2.5, but does die with a
SystemError in python2.6:

~/ cat t.py 
#! /usr/bin/env python

res = dict(__package__='foo')
exec "from os import path" in res
~/ python2.5 t.py 
~/ python2.6 t.py 
Traceback (most recent call last):
  File "t.py", line 4, in <module>
exec "from os import path" in res
  File "<string>", line 1, in <module>
SystemError: Parent module 'foo' not loaded



I think this has been introduced with svn revision 42649
(http://hgpy.de/py/trunk/rev/54c72e1678d68ebbf274)

Part of the diff reads:
        modules = PyImport_GetModuleDict();
        parent = PyDict_GetItemString(modules, buf);
        if (parent == NULL)
-               parent = Py_None;
+               PyErr_Format(PyExc_SystemError,
+                               "Parent module '%.200s' not loaded", buf);
        return parent;
        /* We expect, but can't guarantee, if parent != None, that:
msg69270 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2008-07-05 00:40
Hmm, setting an invalid value for __package__ will definitely break
relative imports (see PEP 361), but it probably shouldn't be breaking
absolute imports.
msg69271 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2008-07-05 00:54
One idea would be to change the import code to only produce a warning
for a missing __package__ entry instead of a SystemError (reserving the
SystemError for cases where the package name is derived from __name__
rather than being retrieved from the __package__ attribute).
msg69383 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2008-07-07 12:49
Bumped priority - an existing module shouldn't crash in 2.6 just because
we started using __package__ as part of the import mechanism and that
module happens to already set an attribute by that name.
msg69385 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2008-07-07 12:54
Adding to my personal to-do list for next beta.
msg69610 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2008-07-13 15:07
Fixed in r64915.

The end result is that the import system now only emits a RuntimeWarning
instead of raising SystemError if it can't find the parent module when
attempting to perform an absolute import (regardless of whether the
parent module name was derived from __package__ or from __name__).
Explicit relative imports will still fail if __package__ is set
incorrectly and setting __package__ to a non-string value will still
result in a ValueError.
msg69626 - (view) Author: Ralf Schmitt (schmir) Date: 2008-07-13 21:07
Thanks Nick for fixing this in a timely manner.
BTW I've seen this when trying to run doctests with py.test.
History
Date User Action Args
2008-07-13 21:07:57schmirsetmessages: + msg69626
2008-07-13 15:07:35ncoghlansetstatus: open -> closed
assignee: ncoghlan ->
resolution: fixed
messages: + msg69610
2008-07-07 12:54:08ncoghlansetassignee: twouters -> ncoghlan
messages: + msg69385
2008-07-07 12:49:22ncoghlansetpriority: release blocker
messages: + msg69383
2008-07-05 00:54:03ncoghlansetmessages: + msg69271
2008-07-05 00:40:35ncoghlansetnosy: + ncoghlan
messages: + msg69270
2008-06-27 21:02:50benjamin.petersonsetassignee: twouters
nosy: + twouters
2008-06-27 20:35:28schmircreate