GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-33237: Improve AttributeError message for partially initialized module. by serhiy-storchaka · Pull Request #6398 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Apr 6, 2018 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

https://bugs.python.org/issue33237

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label Apr 6, 2018
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Apr 6, 2018
@bedevere-bot bedevere-bot added the awaiting merge label Apr 6, 2018
@serhiy-storchaka serhiy-storchaka added the DO-NOT-MERGE label Apr 6, 2018
serhiy-storchaka added 3 commits July 24, 2018 12:01
@serhiy-storchaka
bpo-33237: Improve AttributeError message for partially initialized m…
8684e59
…odule.
@serhiy-storchaka
Update error message.
b072b9b
@serhiy-storchaka
Add _PyModule_IsInitializing().
770f2f5
@serhiy-storchaka
serhiy-storchaka force-pushed the mod-attr-error branch from 3755151 to 770f2f5 Compare July 24, 2018 09:04
@serhiy-storchaka
serhiy-storchaka requested a review from a team July 24, 2018 09:04
@serhiy-storchaka serhiy-storchaka removed the DO-NOT-MERGE label Jul 24, 2018
@serhiy-storchaka
Add a news entry.
8df6406
brettcannon
brettcannon approved these changes Jul 27, 2018
View reviewed changes

@brettcannon brettcannon left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor thing to fix, otherwise LGTM!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Objects/moduleobject.c Outdated
initializing = PyObject_IsTrue(value);
Py_DECREF(value);
if (initializing < 0)
PyErr_Clear();

@brettcannon brettcannon Jul 27, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curly braces around the statement.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Jul 27, 2018

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if test value == Py_True instead of PyObject_IsTrue(value)? This would simplify the code, and I don't think that we should expect any true value except True.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@brettcannon brettcannon Jul 27, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm torn. I do agree the code is simpler and you shouldn't expect it, but I also don't know if it's worth restricting the expectations that tightly. Then again, who is going to set this other than import itself?

IOW I don't have an opinion so whatever you prefer. 😄

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Jul 27, 2018

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And please note, that this PR changes the behavior of PyImport_ImportModuleLevelObject(). I have extracted the common code into _PyModule_IsInitializing(), but the code in PyImport_ImportModuleLevelObject() is not exactly the same as in module_getattro(). It used _PyObject_GetAttrId(mod, &PyId___spec__), but now it uses _PyDict_GetItemId(((PyModuleObject *)m)->md_dict, &PyId___spec__), and supports only the module type instances. I.e. __getattr__() will be now bypassed when looking up __spec__.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@brettcannon brettcannon Aug 10, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a breaking changing due to the fact that people could return any object for their module thanks to importlib.abc.Loader.create_module().

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
Refactoring.
4c2dea3
@vstinner

vstinner commented Oct 20, 2018

Copy link
Copy Markdown
Member

How can I trigger the modified code? The following circular import error doesn't seem to be covered by this change:

vstinner@apu$ cat a.py 
from b import B
class A: pass

vstinner@apu$ cat b.py 
from a import A
class B: pass

vstinner@apu$ ./python -c 'import a'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/vstinner/prog/python/master/a.py", line 1, in <module>
    from b import B
  File "/home/vstinner/prog/python/master/b.py", line 1, in <module>
    from a import A
ImportError: cannot import name 'A' from 'a' (/home/vstinner/prog/python/master/a.py)

I get the same traceback and exception with this change.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
Merge branch 'master' into mod-attr-error
86eb2c8
@serhiy-storchaka

serhiy-storchaka commented Oct 20, 2018

Copy link
Copy Markdown
Member Author

See an example on the tracker.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka added 3 commits October 20, 2018 22:26
@serhiy-storchaka
Merge branch 'master' into mod-attr-error
b43863f
@serhiy-storchaka
Restore support of non-module subclasses in PyImport_ImportModuleLeve…
9f37fba
…lObject().
@serhiy-storchaka
Add tests.
1bd96f9
@serhiy-storchaka

serhiy-storchaka commented Oct 21, 2018

Copy link
Copy Markdown
Member Author

Restored support of non-module subclasses and added tests. @brettcannon, please make review again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vstinner
vstinner approved these changes Oct 22, 2018
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I really like the much better error message!

I just a few minor remarks on the implementation and the NEWS entry.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Objects/moduleobject.c
}
}
}
PyErr_Clear();

@vstinner vstinner Oct 22, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you always clear the current exception? What if the function is called with an exception set? Maybe start with a "assert(!PyErr_Occurred());".

I prefer the old code which only clears the exception on error.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Oct 29, 2018

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not wrong to call this function with NULL and an exception set. Checking this here saves several lines of code and/or indentation level at caller places.

The exception is cleared on error in most cases. The only exception from this rule is for _PyDict_GetItemId() returned NULL.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Oct 29, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it makes sense.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Objects/moduleobject.c Outdated
int
_PyModuleSpec_IsInitializing(PyObject *spec)
{
if (spec != NULL && spec != Py_None) {

@vstinner vstinner Oct 22, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just return 0 (without calling PyErr_Clear) if this condition is false? It would avoid one identation level :-)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Oct 29, 2018

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to call PyErr_Clear() in all cases except spec == Py_None. The latter condition was added for mistake (left from old versions). I'll remove it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Misc/NEWS.d/next/Core and Builtins/2018-07-24-12-54-57.bpo-33237.O95mps.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Oct 22, 2018

Copy link
Copy Markdown
Member

See an example on the tracker.

Ah thanks, I succeded to reproduce the error message and I like it :-)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka added 2 commits October 29, 2018 20:37
@serhiy-storchaka
Remove redundant condition and add a comment.
9a78720
@serhiy-storchaka
Merge branch 'master' into mod-attr-error
d08f64d
@serhiy-storchaka
serhiy-storchaka merged commit 3e429dc into python:master Oct 30, 2018
@bedevere-bot bedevere-bot removed the awaiting merge label Oct 30, 2018
@serhiy-storchaka
serhiy-storchaka deleted the mod-attr-error branch October 30, 2018 11:20
@asottile asottile mentioned this pull request Aug 15, 2019
bpo-20490: Improve circular import error messaging #15308
Merged
@asottile

asottile commented Aug 15, 2019

Copy link
Copy Markdown
Contributor

@vstinner I made a patch to improve the circular-from import as well: #15308

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hauntsaninja hauntsaninja mentioned this pull request Dec 1, 2023
gh-95754: Better AttributeError on partially initialised module #112577
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@brettcannon brettcannon brettcannon approved these changes
@vstinner vstinner vstinner approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

type-feature A feature request or enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@serhiy-storchaka @vstinner @asottile @brettcannon @the-knights-who-say-ni @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.