Issue 3221: SystemError: Parent module 'foo' not loaded on import statement (original) (raw)

Created on 2008-06-27 20:35 by schmir, last changed 2022-04-11 14:56 by admin. 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 exec "from os import path" in res File "", line 1, in 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: Alyssa 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: Alyssa 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: Alyssa 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: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2008-07-07 12:54
Adding to my personal to-do list for next beta.
msg69610 - (view) Author: Alyssa 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
2022-04-11 14:56:35 admin set nosy: + barrygithub: 47471
2008-07-13 21:07:57 schmir set messages: +
2008-07-13 15:07:35 ncoghlan set status: open -> closedassignee: ncoghlan -> resolution: fixedmessages: +
2008-07-07 12:54:08 ncoghlan set assignee: twouters -> ncoghlanmessages: +
2008-07-07 12:49:22 ncoghlan set priority: release blockermessages: +
2008-07-05 00:54:03 ncoghlan set messages: +
2008-07-05 00:40:35 ncoghlan set nosy: + ncoghlanmessages: +
2008-06-27 21:02:50 benjamin.peterson set assignee: twoutersnosy: + twouters
2008-06-27 20:35:28 schmir create