Python 3.4 change in importlib/__init__.py breaking cxFreeze?
Hi all. Python 3.4 introduced a change to Lib/importlib/__init__.py that added the following code to it:
else: # importlib._bootstrap is the built-in import, ensure we don't create # a second copy of the module. _bootstrap.__name__ = 'importlib._bootstrap' _bootstrap.__package__ = 'importlib' _bootstrap.__file__ = __file__.replace('__init__.py', '_bootstrap.py') sys.modules['importlib._bootstrap'] = _bootstrap
When attempting to use cxFreeze on a project, using Python 3.4. we ran into a problem with '__file__' identifier not being defined. Could this be a python bug? Why is this code expecting the module loaded from importlib/__init__.py to always have a __file__ identifier? What is supposed to happen when that code gets loaded from a ZIP archive? Just wanted to check here before filing an issue... but if this is an issue I hope it can be resolved before the final 3.4 release. Best regards, Jurko Gospodnetić
On 10 Mar 2014 19:15, "Jurko Gospodnetić"
Hi all.
Python 3.4 introduced a change to Lib/importlib/__init__.py that added
the following code to it:
else: # importlib._bootstrap is the built-in import, ensure we don't create # a second copy of the module. _bootstrap.__name__ = 'importlib._bootstrap' _bootstrap.__package__ = 'importlib' _bootstrap.__file__ = __file__.replace('__init__.py',
'_bootstrap.py')
sys.modules['importlib._bootstrap'] = _bootstrap
When attempting to use cxFreeze on a project, using Python 3.4. we ran into a problem with '__file__' identifier not being defined.
Could this be a python bug? Why is this code expecting the module loaded from importlib/__init__.py to always have a __file__ identifier? What is supposed to happen when that code gets loaded from a ZIP archive?
__file__ is expected to always be set (including when loaded from a zipfile - in that case it's the zipfile name concatenated with the path within the zip file). If it isn't set, there's a buggy loader involved somewhere that isn't setting it properly. Cheers, Nick.
Just wanted to check here before filing an issue... but if this is an
issue I hope it can be resolved before the final 3.4 release.
Best regards, Jurko Gospodnetić
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe:
https://mail.python.org/mailman/options/python-dev/ncoghlan%40gmail.com
On Mar 10, 2014, at 11:25 PM, Nick Coghlan wrote:
__file__ is expected to always be set (including when loaded from a zipfile
Actually, __file__ is an optional attribute on modules since PEP 420 and Python 3.2. It's *usually* there, but unlike in previous version of Python, if the module doesn't actually come from a "file", there's no longer a requirement to craft some mythical value for it if it doesn't make sense. -Barry
On Mon, 10 Mar 2014 23:25:17 +1000, Nick Coghlan
On 10 Mar 2014 19:15, "Jurko Gospodnetić"
wrote: Hi all.
Python 3.4 introduced a change to Lib/importlib/__init__.py that added
the following code to it:
else: # importlib._bootstrap is the built-in import, ensure we don't create # a second copy of the module. _bootstrap.__name__ = 'importlib._bootstrap' _bootstrap.__package__ = 'importlib' _bootstrap.__file__ = __file__.replace('__init__.py',
'_bootstrap.py')
sys.modules['importlib._bootstrap'] = _bootstrap
When attempting to use cxFreeze on a project, using Python 3.4. we ran into a problem with '__file__' identifier not being defined.
Could this be a python bug? Why is this code expecting the module loaded from importlib/__init__.py to always have a __file__ identifier? What is supposed to happen when that code gets loaded from a ZIP archive?
__file__ is expected to always be set (including when loaded from a zipfile - in that case it's the zipfile name concatenated with the path within the zip file). If it isn't set, there's a buggy loader involved somewhere that isn't setting it properly.
I noticed while using cx_Freeze that __file__ is not set on the main module that cx_Freeze calls (and I found that very annoying :). I'm guessing this is a cx_Freeze artifact and will need to be fixed in cx_Freeze. --David
Hi Nick. On 10.3.2014. 14:25, Nick Coghlan wrote:> What is supposed to happen when that code gets loaded from a ZIP archive?
__file__ is expected to always be set (including when loaded from a zipfile - in that case it's the zipfile name concatenated with the path within the zip file). If it isn't set, there's a buggy loader involved somewhere that isn't setting it properly.
I don't recall seeing that ever explicitly stated. For that matter, Python 3.4.0rc3 documentation explicitly states:
__file__ is optional. If set, this attribute’s value must be a string. The import system may opt to leave __file__ unset if it has no semantic meaning (e.g. a module loaded from a database).
and:
Ultimately, the loader is what makes use of __file__ and/or __cached__.
Or is this some rule specific to the importlib/__init__.py stdlib module? As I recall, I first learned that not all loaded modules need to have their __file__ attribute set by researching a failure in some package when installed as a zipped-egg using setuptools. Admittedly though, that was some old setuptools version. Best regards, Jurko Gospodnetić
On Mon Mar 10 2014 at 11:41:27 AM, Jurko Gospodnetić < jurko.gospodnetic@pke.hr> wrote:
Hi Nick.
On 10.3.2014. 14:25, Nick Coghlan wrote:> What is supposed to happen when that code gets loaded from a ZIP archive?
__file__ is expected to always be set (including when loaded from a zipfile - in that case it's the zipfile name concatenated with the path within the zip file). If it isn't set, there's a buggy loader involved somewhere that isn't setting it properly.
I don't recall seeing that ever explicitly stated. For that matter, Python 3.4.0rc3 documentation explicitly states:
__file__ is optional. If set, this attribute’s value must be a string. The import system may opt to leave __file__ unset if it has no semantic meaning (e.g. a module loaded from a database).
and:
Ultimately, the loader is what makes use of __file__ and/or __cached__.
Or is this some rule specific to the importlib/__init__.py stdlib module?
No, Nick was mistaken and Barry's response is accurate: __file__ is optional and left off when it doesn't make any sense. Since importlib._bootstrap is a frozen module by default it doesn't have __file__ set. -Brett
As I recall, I first learned that not all loaded modules need to have their __file__ attribute set by researching a failure in some package when installed as a zipped-egg using setuptools. Admittedly though, that was some old setuptools version.
Best regards, Jurko Gospodnetić
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/ brett%40python.org
Hi Brett. On 10.3.2014. 16:44, Brett Cannon wrote:
__file__ is optional and left off when it doesn't make any sense. Since importlib._bootstrap is a frozen module by default it doesn't have __file__ set.
Issue #20884 opened for this (http://bugs.python.org/issue20884). Best regards, Jurko Gospodnetić
On 11 Mar 2014 01:44, "Brett Cannon"
On Mon Mar 10 2014 at 11:41:27 AM, Jurko Gospodnetić <
Hi Nick.
On 10.3.2014. 14:25, Nick Coghlan wrote:> What is supposed to happen when that code gets loaded from a ZIP archive?
__file__ is expected to always be set (including when loaded from a zipfile - in that case it's the zipfile name concatenated with the path within the zip file). If it isn't set, there's a buggy loader involved somewhere that isn't setting it properly.
I don't recall seeing that ever explicitly stated. For that matter, Python 3.4.0rc3 documentation explicitly states:
__file__ is optional. If set, this attribute’s value must be a string. The import system may opt to leave __file__ unset if it has no semantic meaning (e.g. a module loaded from a database).
and:
Ultimately, the loader is what makes use of __file__ and/or
__cached__.
Or is this some rule specific to the importlib/__init__.py stdlib
module?
No, Nick was mistaken and Barry's response is accurate: __file__ is
jurko.gospodnetic@pke.hr> wrote: optional and left off when it doesn't make any sense. Since importlib._bootstrap is a frozen module by default it doesn't have __file__ set. Yeah, I was thinking of the 3.4 changes to ensure that various *other* module attributes are always set, even in __main__ (and frozen modules). My apologies for the misinformation - I should have waited until I had a chance to look it up properly. Cheers, Nick.
-Brett
As I recall, I first learned that not all loaded modules need to have their __file__ attribute set by researching a failure in some package when installed as a zipped-egg using setuptools. Admittedly though, that was some old setuptools version.
Best regards, Jurko Gospodnetić
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe:
https://mail.python.org/mailman/options/python-dev/brett%40python.org
participants (5)
-
Barry Warsaw
-
Brett Cannon
-
Jurko Gospodnetić
-
Nick Coghlan
-
R. David Murray