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

using custom objects as modules: AttributeErrors new in 3.5 #68680

Closed
arigo mannequin opened this issue Jun 23, 2015 · 20 comments
Closed

using custom objects as modules: AttributeErrors new in 3.5 #68680

arigo mannequin opened this issue Jun 23, 2015 · 20 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) release-blocker type-bug An unexpected behavior, bug, or error

Comments

@arigo
Copy link
Mannequin

arigo mannequin commented Jun 23, 2015

BPO 24492
Nosy @brettcannon, @arigo, @terryjreedy, @ncoghlan, @larryhastings, @ericsnowcurrently
Files
  • test_case.py
  • issue24492.diff
  • 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 = 'https://github.com/brettcannon'
    closed_at = <Date 2015-08-14.18:11:34.680>
    created_at = <Date 2015-06-23.17:40:13.694>
    labels = ['interpreter-core', 'type-bug', 'release-blocker']
    title = 'using custom objects as modules: AttributeErrors new in 3.5'
    updated_at = <Date 2015-08-24.20:01:24.482>
    user = 'https://github.com/arigo'

    bugs.python.org fields:

    activity = <Date 2015-08-24.20:01:24.482>
    actor = 'python-dev'
    assignee = 'brett.cannon'
    closed = True
    closed_date = <Date 2015-08-14.18:11:34.680>
    closer = 'brett.cannon'
    components = ['Interpreter Core']
    creation = <Date 2015-06-23.17:40:13.694>
    creator = 'arigo'
    dependencies = []
    files = ['39791', '40154']
    hgrepos = []
    issue_num = 24492
    keywords = ['patch', '3.4regression']
    message_count = 20.0
    messages = ['245696', '245699', '245725', '245726', '245869', '246112', '248345', '248361', '248368', '248369', '248371', '248373', '248377', '248384', '248436', '248541', '248551', '248598', '248599', '249072']
    nosy_count = 7.0
    nosy_names = ['brett.cannon', 'arigo', 'terry.reedy', 'ncoghlan', 'larry', 'python-dev', 'eric.snow']
    pr_nums = []
    priority = 'release blocker'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue24492'
    versions = ['Python 3.5', 'Python 3.6']

    @arigo
    Copy link
    Mannequin Author

    arigo mannequin commented Jun 23, 2015

    A regression in 3.5: if we use custom objects as modules (like some projects do), then these custom objects may not have an attribute called "__name__". There is new code in 3.5 added for issue bpo-17636 which tries sometimes to read the "__name__" of a module when executing an import statement, and if this leads to an AttributeError, it will get propagated.

    I imagine this could break real code using "try: except ImportError:". It _does_ break the tests of the "cffi" project, which try to check that some import of a nonexisting name correctly gives ImportError. Now it gives AttributeError("__name__") instead.

    @arigo arigo mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Jun 23, 2015
    @larryhastings
    Copy link
    Contributor

    What's a sensible approach to ameliorate the problem? Gracefully muddle through without a __name__ on the imported object?

    @arigo
    Copy link
    Mannequin Author

    arigo mannequin commented Jun 24, 2015

    I'd guess so: if the PyObject_GetAttrId() fails, then ignore the rest of the code that was added in https://hg.python.org/cpython/rev/fded07a2d616 and jump straight to PyErr_Format(PyExc_ImportError, ...).

    @arigo
    Copy link
    Mannequin Author

    arigo mannequin commented Jun 24, 2015

    Also, if we want to be paranoid, the _PyObject_GetAttrId() can return anything, not necessarily a string object. This would make the following PyUnicode_FromFormat() fail. So maybe you also want to overwrite failures in PyUnicode_FromFormat() with the final ImportError. (It actually looks like a good--if convoluted--use case for exception chaining at the C level...)

    @terryjreedy
    Copy link
    Member

    I view having a string __name__ attribute as part of quacking like a module (or class or function). Hence pseudonames like '<lambda>' (lambda expression) or <pyshell#n> (the module name for interactive input, where n is the line number). Is there a good reason for custom 'module' objects to not have a name? If so, the 'graceful muddle' precedent is to give them a pseudoname, such as <module>, perhaps with a prefix or suffix.

    @larryhastings
    Copy link
    Contributor

    This is
    a) marked as release blocker, and
    b) is assigned to nobody.
    This is not tenable.

    While I want this fixed, I'm not going to hold up beta 3 for it.

    @brettcannon
    Copy link
    Member

    Here is a patch that adds a test, fixes the issue, and for good measure stops assuming that __name__ will always be a string.

    @brettcannon
    Copy link
    Member

    Larry, does this warrant going into 3.5.0?

    @larryhastings
    Copy link
    Contributor

    I would like the fix in 3.5. However, I'm not qualified to review the code. Can you get a qualified reviewer in to look over the code?

    Once someone suitable has reviewed it, I'll accept a pull request (pasted in here naturally).

    @ericsnowcurrently
    Copy link
    Member

    patch LGTM. Presumably the divergence between importlib (in _handle_fromlist) and import.c was strictly accidental (i.e. lack of test coverage).

    @arigo
    Copy link
    Mannequin Author

    arigo mannequin commented Aug 10, 2015

    Patch LGTM too. Optionally a test is needed for each of the other cases in it too, but please don't cause that comment to stop it from getting in.

    @brettcannon
    Copy link
    Member

    Thanks for the reviews, Eric and Armin. I will get the patch in 3.5.0, 3.5.1, and 3.6 sometime this week and then open another issue for adding more tests for the other branches in the code.

    @larryhastings
    Copy link
    Contributor

    Yes, Eric and Armin are both qualified reviewers in my book. You have my blessing to send a pull request.

    Thanks, everybody!

    @larryhastings
    Copy link
    Contributor

    My Bitbucket repo is now public.

    https://bitbucket.org/larry/cpython350

    @brettcannon
    Copy link
    Member

    Created https://bitbucket.org/larry/cpython350/pull-requests/2/issue-24492-make-sure-that-from-import/diff without the PyUnicode_FromFormat() change as I realized that was conflating two changes in one patch and it wasn't even being tested (verified all tests still pass and a quick check in the interpreter shows ImportError is still raised).

    Larry, let me know when you have accepted the PR and I will commit to 3.5 and default on hg.python.org.

    I also created http://bugs.python.org/issue24846 to track adding more tests for the code.

    @brettcannon
    Copy link
    Member

    I noticed you accepted the PR on Bitbucket, Larry. Should I consider your part done and I can now pull the commit into the 3.5 and default branches on hg.python.org?

    @larryhastings
    Copy link
    Contributor

    Yep. This time I have foisted nearly all the work, including the forward-merging, onto y'all.

    *sits back, sips iced coffee*

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 14, 2015

    New changeset cf3a62a8d786 by Brett Cannon in branch '3.5':
    Issue bpo-24492: make sure that ``from ... import ...` raises an
    https://hg.python.org/cpython/rev/cf3a62a8d786

    New changeset bbe6b215df5d by Brett Cannon in branch '3.5':
    Merge from 3.5.0 for issue bpo-24492
    https://hg.python.org/cpython/rev/bbe6b215df5d

    New changeset b0a6bba16b9c by Brett Cannon in branch 'default':
    Merge from 3.5 for issue bpo-24492
    https://hg.python.org/cpython/rev/b0a6bba16b9c

    @brettcannon
    Copy link
    Member

    I have merged my 3.5.0 patch into 3.5 and default, so that should fix the issue Armin and CFFI was having.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 24, 2015

    New changeset 5a9ac801f9b4 by larry in branch '3.5':
    Merged in brettcannon/cpython350/3.5 (pull request #2)
    https://hg.python.org/cpython/rev/5a9ac801f9b4

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) release-blocker type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants