Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SystemError: Parent module 'foo' not loaded on import statement #47471

Closed
schmir mannequin opened this issue Jun 27, 2008 · 7 comments
Closed

SystemError: Parent module 'foo' not loaded on import statement #47471

schmir mannequin opened this issue Jun 27, 2008 · 7 comments
Labels
release-blocker type-bug An unexpected behavior, bug, or error

Comments

@schmir
Copy link
Mannequin

schmir mannequin commented Jun 27, 2008

BPO 3221
Nosy @Yhg1s, @ncoghlan

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2008-07-13.15:07:35.773>
created_at = <Date 2008-06-27.20:35:28.442>
labels = ['type-bug', 'release-blocker']
title = "SystemError: Parent module 'foo' not loaded on import statement"
updated_at = <Date 2008-07-13.21:07:57.438>
user = 'https://bugs.python.org/schmir'

bugs.python.org fields:

activity = <Date 2008-07-13.21:07:57.438>
actor = 'schmir'
assignee = 'none'
closed = True
closed_date = <Date 2008-07-13.15:07:35.773>
closer = 'ncoghlan'
components = []
creation = <Date 2008-06-27.20:35:28.442>
creator = 'schmir'
dependencies = []
files = []
hgrepos = []
issue_num = 3221
keywords = []
message_count = 7.0
messages = ['68850', '69270', '69271', '69383', '69385', '69610', '69626']
nosy_count = 3.0
nosy_names = ['twouters', 'ncoghlan', 'schmir']
pr_nums = []
priority = 'release blocker'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue3221'
versions = ['Python 2.6']

@schmir
Copy link
Mannequin Author

schmir mannequin commented Jun 27, 2008

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:

@schmir schmir mannequin added the type-bug An unexpected behavior, bug, or error label Jun 27, 2008
@ncoghlan
Copy link
Contributor

ncoghlan commented Jul 5, 2008

Hmm, setting an invalid value for __package__ will definitely break
relative imports (see PEP-361), but it probably shouldn't be breaking
absolute imports.

@ncoghlan
Copy link
Contributor

ncoghlan commented Jul 5, 2008

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).

@ncoghlan
Copy link
Contributor

ncoghlan commented Jul 7, 2008

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.

@ncoghlan
Copy link
Contributor

ncoghlan commented Jul 7, 2008

Adding to my personal to-do list for next beta.

@ncoghlan ncoghlan assigned ncoghlan and unassigned Yhg1s Jul 7, 2008
@ncoghlan
Copy link
Contributor

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.

@ncoghlan ncoghlan removed their assignment Jul 13, 2008
@schmir
Copy link
Mannequin Author

schmir mannequin commented Jul 13, 2008

Thanks Nick for fixing this in a timely manner.
BTW I've seen this when trying to run doctests with py.test.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-blocker type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants