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: pydoc.safeimport() raises ErrorDuringImport() if __builtin__.__import__ is monkey patched
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, r.david.murray, segfault87
Priority: normal Keywords: patch

Created on 2016-10-15 16:14 by segfault87, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pydoc.patch segfault87, 2016-10-15 16:14 The suggested patch review
Messages (3)
msg278726 - (view) Author: Joon-Kyu Park (segfault87) Date: 2016-10-15 16:14
`pydoc.safeimport()` should return `None` if the module isn't found.

If Python's default `__import__` is monkey patched, (e.g., by using gevent)
the function misbehaves. According to the current implementation,
the function returns `None` by checking the only last entry of the traceback
if `ImportError` is thrown during calling `__import__()`.

If `__import__` is monkey patched, extra entries can be mixed into the
original traceback when `ImportError` is raised. In the case when the module
is not found, `ErrorDuringImport` is being raised rather than returning `None`
after failing checking the traceback because current implementation only
checks the last traceback entry.

The important thing is to check whether `ImportError` is raised inside
`safeimport()`, I think it's okay to check the whole traceback entries
instead of checking the only last item.

Please check the attached patch which I suggest. Thank you.
msg278768 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-10-16 14:23
Can you provide a test case that shows when this condition might occur?  We'd want a test in any case if we accept the patch.
msg401147 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-09-06 15:51
It is no longer using the traceback check:

https://github.com/python/cpython/blob/37272f5800ee1e9fcb2da4a1766366519b9b3d94/Lib/pydoc.py#L445
History
Date User Action Args
2022-04-11 14:58:38adminsetgithub: 72637
2021-09-06 15:51:54iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg401147

resolution: out of date
stage: resolved
2016-10-16 14:23:54r.david.murraysetnosy: + r.david.murray
messages: + msg278768
2016-10-15 16:14:09segfault87create