This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Obsolete caveat in reload() docs
Type: Stage: resolved
Components: Documentation Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: brett.cannon, docs@python, eric.snow, ncoghlan, petr.viktorin, python-dev, r.david.murray
Priority: normal Keywords: patch

Created on 2015-04-30 14:12 by petr.viktorin, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
0001-Remove-obsolete-caveat-from-reload-docs.patch petr.viktorin, 2015-04-30 14:12 review
Messages (12)
msg242270 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2015-04-30 14:12
imp.reload() and importlib.reload() docs state::

    If a module is syntactically correct but its initialization fails, the first
    :keyword:`import` statement for it does not bind its name locally, but does
    store a (partially initialized) module object in ``sys.modules``.  To reload
    the module you must first :keyword:`import` it again (this will bind the name
    to the partially initialized module object) before you can :func:`reload` it.

If I reading that correctly, "initialization" refers to executing the module, so for module containing just::

    uninitialized_variable

the following::

    >>> import sys
    >>> import x
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/tmp/x.py", line 1, in <module>
        uninitialized_variable
    NameError: name 'uninitialized_variable' is not defined

should leave me with a initialized module in sys.modules['x']. However, this is not what happens, in either Python 3.4 or 2.7::

    >>> sys.modules['x']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'x'

Here's a patch to remove the caveat in Python 3 docs.
If I missed something, and "initialization" refers to something else, it should be clarified.
msg242365 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-05-02 01:49
This generally comes up in the context of a module importing another module, and *that* module fails to import.  The first module is left in sys.modules as a stub.  Try that experiment, I'm pretty sure that is still true.
msg242383 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2015-05-02 07:34
Not true (on 3.3 & 2.7).

>>> import sys
>>> import x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/x.py", line 1, in <module>
    import y
  File "/tmp/y.py", line 1, in <module>
    1/0
ZeroDivisionError: division by zero
>>> sys.modules['x']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'x'
>>> sys.modules['y']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'y'

For 3.4, PEP 451 explicitly specifies ``del sys.modules[spec.name]`` if ``loader.exec_module`` fails [0].

[0] https://www.python.org/dev/peps/pep-0451/#how-loading-will-work
msg242411 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-05-02 17:15
OK, I can't reproduce it either, neither in python3 nor python2.  Brett, is this left over from a long time ago?

Heh.  I just tried another experiment and got an interesting result:

rdmurray@pydev:~/python/p34>cat temp1.py
import temp2
foo = 1
rdmurray@pydev:~/python/p34>cat temp2.py
import temp1

badval

rdmurray@pydev:~/python/p34>./python
Python 3.4.3+ (3.4:b53cfcfdfe47, May  2 2015, 12:51:46) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import temp2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/rdmurray/python/p34/temp2.py", line 3, in <module>
    badval
NameError: name 'badval' is not defined
>>> import sys
>>> sorted(sys.modules.keys())
['__main__', '_codecs', '_collections_abc', '_frozen_importlib', '_imp', '_io', '_sitebuiltins', '_stat', '_sysconfigdata', '_thread', '_warnings', '_weakref', '_weakrefset', 'abc', 'atexit', 'builtins', 'codecs', 'encodings', 'encodings.aliases', 'encodings.latin_1', 'encodings.utf_8', 'errno', 'genericpath', 'io', 'marshal', 'os', 'os.path', 'posix', 'posixpath', 'readline', 'rlcompleter', 'signal', 'site', 'stat', 'sys', 'sysconfig', 'temp1', 'zipimport']
>>> import temp1
>>> temp1.temp2
<module 'temp2' from '/home/rdmurray/python/p34/temp2.py'>
>>> sorted(sys.modules.keys())
['__main__', '_codecs', '_collections_abc', '_frozen_importlib', '_imp', '_io', '_sitebuiltins', '_stat', '_sysconfigdata', '_thread', '_warnings', '_weakref', '_weakrefset', 'abc', 'atexit', 'builtins', 'codecs', 'encodings', 'encodings.aliases', 'encodings.latin_1', 'encodings.utf_8', 'errno', 'genericpath', 'io', 'marshal', 'os', 'os.path', 'posix', 'posixpath', 'readline', 'rlcompleter', 'signal', 'site', 'stat', 'sys', 'sysconfig', 'temp1', 'zipimport']


More or less the reverse of the passage in question.
msg242413 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-05-02 17:22
Note that calling reload on temp1.temp2 will fail with an error that temp2 is not in sys.modules.  So maybe the caveat needs rewording rather than deletion.
msg242417 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2015-05-02 17:36
Either the docs are out-of-date or they are really poorly worded.  Most likely it's the former, but I'm taking a look.
msg242419 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2015-05-02 18:01
I've verified that the documentation is correct under Python 2.3.  The behavior changed under Python 2.4 (and the docs were not updated).  I expect that the change in behavior is an unintended consequence of a change in the import system for 2.4.  There were 7 in 2.4, so I'm checking to see which did it.

FYI, our backward-compatibility policy would preclude a change like this, which is why I think it was unintentional.  That no one noticed implies that we did not have a unit test for the case and that folks really weren't relying on the behavior anyway (at least not enough to open a bug). :)  I suppose there could be an old bug on this, but if that the case then someone decided not to change it back.

Regardless, the docs should be fixed by removing the referenced paragraph.
msg242420 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2015-05-02 18:14
I'm pretty sure this is the culprit:

changeset:   32882:331e60d8ce6da19b168849418776fea0940787ec
branch:      legacy-trunk
user:        Tim Peters <tim.peters@gmail.com>
date:        Mon Aug 02 03:52:12 2004 +0000
summary:     PyImport_ExecCodeModuleEx():  remove module from sys.modules in error cases.
msg242421 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2015-05-02 18:17
patch LGTM for the 3 branches.
msg242422 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-05-02 19:02
New changeset 3cdeafd18e61 by R David Murray in branch '3.4':
#24081: Remove obsolete caveat from import docs.
https://hg.python.org/cpython/rev/3cdeafd18e61

New changeset d57e0c6d292d by R David Murray in branch 'default':
Merge: #24081: Remove obsolete caveat from import docs.
https://hg.python.org/cpython/rev/d57e0c6d292d

New changeset fd1549dd8065 by R David Murray in branch '2.7':
#24081: Remove obsolete caveat from import docs.
https://hg.python.org/cpython/rev/fd1549dd8065
msg242423 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-05-02 19:03
Thanks, Petr.  (And Eric.)
msg242439 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2015-05-02 23:26
I vaguely recall discussing that change, and my recollection is that it was
deliberate in order to evict improperly configured modules - we didn't
count it as a significant compatibility break because it solely affected an
error handling path.

I guess we never thought to check if the old misbehaviour was formally
documented.
History
Date User Action Args
2022-04-11 14:58:16adminsetgithub: 68269
2015-05-02 23:26:59ncoghlansetmessages: + msg242439
2015-05-02 19:03:50r.david.murraysetstatus: open -> closed
resolution: fixed
messages: + msg242423

stage: commit review -> resolved
2015-05-02 19:02:55python-devsetnosy: + python-dev
messages: + msg242422
2015-05-02 18:17:16eric.snowsetmessages: + msg242421
2015-05-02 18:14:53eric.snowsetmessages: + msg242420
2015-05-02 18:03:15r.david.murraysetstage: commit review
2015-05-02 18:01:04eric.snowsetmessages: + msg242419
2015-05-02 17:36:34eric.snowsetmessages: + msg242417
2015-05-02 17:22:00r.david.murraysetmessages: + msg242413
2015-05-02 17:15:11r.david.murraysetmessages: + msg242411
versions: + Python 3.5
2015-05-02 07:34:36petr.viktorinsetmessages: + msg242383
2015-05-02 01:49:59r.david.murraysetnosy: + r.david.murray
messages: + msg242365
2015-04-30 15:43:08berker.peksagsetnosy: + brett.cannon, ncoghlan, eric.snow
2015-04-30 14:12:34petr.viktorincreate