_not_found attribute on ImportError
Hi All, I see in Python 3, some ImportErrors have grown a '_not_found' attribute. What's the significance of this attribute and where/how is it added? The only way I can seem to create this attribute is: ex = ImportError ex._not_found = True cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk
On Mon, Feb 11, 2013 at 6:08 PM, Chris Withers
Hi All,
I see in Python 3, some ImportErrors have grown a '_not_found' attribute. What's the significance of this attribute and where/how is it added?
The only way I can seem to create this attribute is:
ex = ImportError ex._not_found = True
It's something importlib does to get fromlists to behave the same way they did in the C implementation. It is set here: http://hg.python.org/cpython/file/3.3/Lib/importlib/_bootstrap.py To make these pass: http://hg.python.org/cpython/file/3.3/Lib/test/test_importlib/import_/test_f... You'd have to ask Brett which specific test is affected, or else you could comment out the line that sets _not_found and see what breaks (you may need to refreeze _frozen_importlib first). Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
On 11/02/2013 10:54, Nick Coghlan wrote:
On Mon, Feb 11, 2013 at 6:08 PM, Chris Withers
wrote: Hi All,
I see in Python 3, some ImportErrors have grown a '_not_found' attribute. What's the significance of this attribute and where/how is it added?
The only way I can seem to create this attribute is:
ex = ImportError ex._not_found = True
It's something importlib does to get fromlists to behave the same way they did in the C implementation.
It is set here: http://hg.python.org/cpython/file/3.3/Lib/importlib/_bootstrap.py
OK. Have any other exceptions grown new attributes in Python 3? cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk
On Mon, Feb 11, 2013 at 8:58 PM, Chris Withers
Have any other exceptions grown new attributes in Python 3?
Off the top of my head, ImportError grew "name" and "path" attributes in 3.3, everything grew __cause__, __context__ and __traceback__ attributes in 3.0 and the __suppress_context__ attribute in 3.3. PEP 3151 may have moved a few attributes around in 3.3 as well. If there are any others, you'll need to trawl the What's New documents looking for them. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
On Feb 11, 2013, at 09:23 PM, Nick Coghlan wrote:
On Mon, Feb 11, 2013 at 8:58 PM, Chris Withers
wrote: Have any other exceptions grown new attributes in Python 3?
Off the top of my head, ImportError grew "name" and "path" attributes in 3.3, everything grew __cause__, __context__ and __traceback__ attributes in 3.0 and the __suppress_context__ attribute in 3.3.
PEP 3151 may have moved a few attributes around in 3.3 as well.
If there are any others, you'll need to trawl the What's New documents looking for them.
Those public attributes should be documented. _not_found should not be since it's an implementation detail. Also, Brett has left little TODO easter eggs in the code which clearly indicate changes he plans for 3.4, although I don't know where the plans for ModuleNotFound are documented. In the meantime, I just ran a test against trunk, with the following change and nothing broke afaict. diff -r a79650aacb43 Lib/importlib/_bootstrap.py --- a/Lib/importlib/_bootstrap.py Mon Feb 11 13:33:00 2013 +0000 +++ b/Lib/importlib/_bootstrap.py Mon Feb 11 09:16:51 2013 -0500 @@ -1640,6 +1640,7 @@ # TODO(brett): In Python 3.4, have import raise # ModuleNotFound and catch that. if getattr(exc, '_not_found', False): + del exc._not_found if exc.name == from_name: continue raise I won't commit this, but it really needs another hasattr() check to be completely valid. Cheers, -Barry
On Mon, Feb 11, 2013 at 9:23 AM, Barry Warsaw
On Feb 11, 2013, at 09:23 PM, Nick Coghlan wrote:
On Mon, Feb 11, 2013 at 8:58 PM, Chris Withers
wrote: Have any other exceptions grown new attributes in Python 3?
Off the top of my head, ImportError grew "name" and "path" attributes in 3.3, everything grew __cause__, __context__ and __traceback__ attributes in 3.0 and the __suppress_context__ attribute in 3.3.
PEP 3151 may have moved a few attributes around in 3.3 as well.
If there are any others, you'll need to trawl the What's New documents looking for them.
Those public attributes should be documented.
They are: http://docs.python.org/3.4/library/exceptions.html?highlight=importerror#Imp...
_not_found should not be since it's an implementation detail.
Yep, hence the leading underscore.
Also, Brett has left little TODO easter eggs in the code which clearly indicate changes he plans for 3.4, although I don't know where the plans for ModuleNotFound are documented.
http://bugs.python.org/issue15767 Basically it's a matter of choosing ModuleNotFound vs. ModuleNotFoundError and then writing the code. -Brett
In the meantime, I just ran a test against trunk, with the following change and nothing broke afaict.
diff -r a79650aacb43 Lib/importlib/_bootstrap.py --- a/Lib/importlib/_bootstrap.py Mon Feb 11 13:33:00 2013 +0000 +++ b/Lib/importlib/_bootstrap.py Mon Feb 11 09:16:51 2013 -0500 @@ -1640,6 +1640,7 @@ # TODO(brett): In Python 3.4, have import raise # ModuleNotFound and catch that. if getattr(exc, '_not_found', False): + del exc._not_found if exc.name == from_name: continue raise
I won't commit this, but it really needs another hasattr() check to be completely valid.
Cheers, -Barry _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/brett%40python.org
participants (4)
-
Barry Warsaw
-
Brett Cannon
-
Chris Withers
-
Nick Coghlan