classification
Title: Wrong behavior of help function on module
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Oleg.Oleinik, corona10, levkivskyi, tuxtimo, yselivanov
Priority: normal Keywords: patch

Created on 2018-05-28 14:22 by Oleg.Oleinik, last changed 2019-08-01 05:15 by corona10.

Pull Requests
URL Status Linked Edit
PR 8284 closed corona10, 2018-07-14 15:03
Messages (6)
msg317852 - (view) Author: Oleg Oleinik (Oleg.Oleinik) Date: 2018-05-28 14:22
I write file test.py:
def __getattr__(key):
    return None
help(__name__)

when I try to run it I've got error:
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    help(__name__)
  File "C:\Program Files\Python37\lib\_sitebuiltins.py", line 103, in __call__
    return pydoc.help(*args, **kwds)
  File "C:\Program Files\Python37\lib\pydoc.py", line 1894, in __call__
    self.help(request)
  File "C:\Program Files\Python37\lib\pydoc.py", line 1944, in help
    elif request: doc(request, 'Help on %s:', output=self._output)
  File "C:\Program Files\Python37\lib\pydoc.py", line 1674, in doc
    pager(render_doc(thing, title, forceload))
  File "C:\Program Files\Python37\lib\pydoc.py", line 1667, in render_doc
    return title % desc + '\n\n' + renderer.document(object, name)
  File "C:\Program Files\Python37\lib\pydoc.py", line 385, in document
    if inspect.ismodule(object): return self.docmodule(*args)
  File "C:\Program Files\Python37\lib\pydoc.py", line 1136, in docmodule
    for key, value in inspect.getmembers(object, inspect.isclass):
  File "C:\Program Files\Python37\lib\inspect.py", line 330, in getmembers
    for base in object.__bases__:
TypeError: 'NoneType' object is not iterable

If I change definition of __getattr__ function to:
def __getattr__(key):
   raise AttributeError()
then help function runs without errors. But I need to return None value from __getattr__ function.
When I wrote this function in class definition there is also no errors during runtime
msg318335 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2018-05-31 17:58
Hm, replacing the return with a random string, this leads to another crash:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ilevkivskyi/src/cpython/Lib/_sitebuiltins.py", line 103, in __call__
    return pydoc.help(*args, **kwds)
  File "/Users/ilevkivskyi/src/cpython/Lib/pydoc.py", line 1895, in __call__
    self.help(request)
  File "/Users/ilevkivskyi/src/cpython/Lib/pydoc.py", line 1954, in help
    else: doc(request, 'Help on %s:', output=self._output)
  File "/Users/ilevkivskyi/src/cpython/Lib/pydoc.py", line 1674, in doc
    pager(render_doc(thing, title, forceload))
  File "/Users/ilevkivskyi/src/cpython/Lib/pydoc.py", line 1667, in render_doc
    return title % desc + '\n\n' + renderer.document(object, name)
  File "/Users/ilevkivskyi/src/cpython/Lib/pydoc.py", line 385, in document
    if inspect.ismodule(object): return self.docmodule(*args)
  File "/Users/ilevkivskyi/src/cpython/Lib/pydoc.py", line 1157, in docmodule
    for importer, modname, ispkg in pkgutil.iter_modules(object.__path__):
  File "/Users/ilevkivskyi/src/cpython/Lib/pkgutil.py", line 123, in iter_modules
    raise ValueError("path must be None or list of paths to look for "
ValueError: path must be None or list of paths to look for modules in

The reason is that `__getattr__` is also triggered when a special attribute is looked up. I am not sure what to do with this. This is a bit inconsistent with how classes behave, where e.g. `__len__` is never searched on an instance. But modules are special in many other ways, so maybe we can just fix pydoc (and other tools like inspect) to expect some ill-typed values in special module attributes and fail gracefully?
msg318336 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2018-05-31 18:01
Adding Yury as an inspect expert. I don't think this is something urgent, we can probably postpone this to 3.7.1.
msg321653 - (view) Author: Dong-hee Na (corona10) * (Python triager) Date: 2018-07-14 15:04
This script works well on Python2 but not on Python3.
I've submitted a PR. Please take a look.
msg321654 - (view) Author: Dong-hee Na (corona10) * (Python triager) Date: 2018-07-14 15:57
I've checked both cases are now working on my patch.
msg348838 - (view) Author: Dong-hee Na (corona10) * (Python triager) Date: 2019-08-01 05:15
I close PR 8284.

If there is a chance to work with this issue.
Please let me know

Thanks!
History
Date User Action Args
2019-08-01 05:15:50corona10setmessages: + msg348838
2018-07-14 15:57:19corona10setmessages: + msg321654
2018-07-14 15:04:46corona10setnosy: + corona10
messages: + msg321653
2018-07-14 15:03:37corona10setkeywords: + patch
stage: patch review
pull_requests: + pull_request7818
2018-05-31 18:01:09levkivskyisetnosy: + yselivanov
messages: + msg318336
2018-05-31 17:58:51levkivskyisetmessages: + msg318335
2018-05-31 16:29:35serhiy.storchakasetnosy: + levkivskyi
2018-05-31 16:15:22tuxtimosetnosy: + tuxtimo
2018-05-28 14:22:23Oleg.Oleinikcreate