classification
Title: help() modifies the string module
Type: Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Zectbumo, benjamin.peterson, eric.smith, mark.dickinson, steven.daprano, zach.ware
Priority: normal Keywords:

Created on 2019-12-17 18:52 by Zectbumo, last changed 2019-12-18 02:15 by zach.ware. This issue is now closed.

Messages (11)
msg358564 - (view) Author: (Zectbumo) Date: 2019-12-17 18:52
import string
a = string.letters
help(int)
b = string.letters
a == b # False
msg358574 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-12-17 20:36
I am unable to reproduce this.

Can you double check that this occurs in the plain Python interpreter or IDLE, and provide a minimum reproducible example? (No third-party IDEs or interpreters, such as Jupyter.)

These guidelines may help:

https://en.wikipedia.org/wiki/Minimal_working_example

http://sscce.org/

https://stackoverflow.com/help/minimal-reproducible-example
msg358575 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2019-12-17 21:05
I *can* reproduce, on macOS 10.14.6, Python 2.7.17 (from macports).

Python 2.7.17 (default, Oct 20 2019, 14:46:50) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> help(int)  # brings up pager; page through to the end

>>> string.letters
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
msg358578 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-12-17 21:13
With 2.7.16 on cygwin, I get:

>>> import string
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> help(int)
>>> string.letters
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

But given that there's only 2 weeks of support left for 2.7, I don't see this getting changed.
msg358579 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2019-12-17 21:20
Here's where `string.letters` is reassigned: https://github.com/python/cpython/blob/5f2c1345a79f205c680ed6e0a6ed44199546d79e/Modules/_localemodule.c#L136-L147

That code looks like it's exercised whenever setlocale is (or possibly just when LC_CTYPE is changed). I haven't yet figured out which part of the help machinery causes that.
msg358582 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2019-12-17 21:22
> But given that there's only 2 weeks of support left for 2.7, I don't see this getting changed.

Agreed: I can't reproduce on Python 3, and it looks as though the offending code is gone from the codebase in Python 3, so this is pretty much just a historical oddity at this point.
msg358588 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2019-12-17 21:48
For completeness, here's the path leading to the reassignment (after hacking in a raise to the appropriate place in _localemodule.c). It's a side-effect of calling `locale.getpreferredencoding`.

>>> help(int)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mdickinson/GitHub/python/cpython/Lib/site.py", line 445, in __call__
    import pydoc
  File "/Users/mdickinson/GitHub/python/cpython/Lib/pydoc.py", line 202, in <module>
    _encoding = locale.getpreferredencoding()
  File "/Users/mdickinson/GitHub/python/cpython/Lib/locale.py", line 616, in getpreferredencoding
    setlocale(LC_CTYPE, "")
  File "/Users/mdickinson/GitHub/python/cpython/Lib/locale.py", line 581, in setlocale
    return _setlocale(category, locale)
ValueError: about to reassign string.letters


So a minimal reproducer is something like:

Python 2.7.17 (default, Oct 20 2019, 14:46:50) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import string, locale
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> locale.getpreferredencoding()
'UTF-8'
>>> string.letters
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
msg358589 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-12-17 21:48
@Zectbumo: is this causing you any practical problem? Or is it just a curiosity?
msg358594 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2019-12-17 22:33
There's no reasonable way I can see to fix this. The reassignment of those string attributes is clearly intentional (it's even documented) and there's probably code somewhere that relies on it. I think the best we can do is close as "won't fix" though "not a bug" may be more accurate: the code is clearly behaving as intended and designed, even if the design is questionable.
msg358596 - (view) Author: (Zectbumo) Date: 2019-12-17 22:42
@eric.smith
It actually caused a problem and then turned into a curiosity. At the beginning I wanted to remind myself about the string module so I did a help(string). Then right after that I wrote a program that generated filenames from string.letters. I closed the python interpreter down and then reopened it and regenerated filenames again using string.letters. To my surprise I found the second iteration to come out with completely different results messing up my work. Once I found that help() was causing this problem and it became a curiosity because you would think help() would not modify anything. Obviously this problem is easy to work around in my case.
msg358607 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2019-12-18 02:15
Since this has been this way for 22 years without a previous report that I can find, +1 to closing as "won't fix"/"not a bug" and so doing.  However, I'm also adding the 2.7 release manager to make sure he knows about it and can reopen if he wants.
History
Date User Action Args
2019-12-18 02:15:45zach.waresetstatus: open -> closed

nosy: + benjamin.peterson, zach.ware
messages: + msg358607

resolution: wont fix
stage: resolved
2019-12-18 02:12:12zach.warelinkissue39084 superseder
2019-12-17 22:42:22Zectbumosetmessages: + msg358596
2019-12-17 22:33:42mark.dickinsonsetmessages: + msg358594
2019-12-17 21:48:45eric.smithsetmessages: + msg358589
2019-12-17 21:48:32mark.dickinsonsetmessages: + msg358588
2019-12-17 21:22:54mark.dickinsonsetmessages: + msg358582
2019-12-17 21:20:41mark.dickinsonsetmessages: + msg358579
2019-12-17 21:13:05eric.smithsetnosy: + eric.smith
messages: + msg358578
2019-12-17 21:05:28mark.dickinsonsetnosy: + mark.dickinson
messages: + msg358575
2019-12-17 20:36:32steven.dapranosetnosy: + steven.daprano
messages: + msg358574
2019-12-17 18:52:27Zectbumocreate