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: Internal importlib frames visible when module imported by import_module throws exception
Type: behavior Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, ulope
Priority: normal Keywords:

Created on 2017-03-10 15:50 by ulope, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg289381 - (view) Author: Ulrich Petri (ulope) * Date: 2017-03-10 15:50
Importing a module that raises an exception on import trough `importlib.import_module()` causes importlib to not strip it's internal frames from the traceback.


Minimal example:

--a.py--
import importlib

importlib.import_module("b")
--a.py--


--b.py--
raise Exception()
--b.py--

#~ python3.6 a.py
Traceback (most recent call last):
  File "a.py", line 3, in <module>
    importlib.import_module("b")
  File "/Users/ulo/.pythonz/pythons/CPython-3.6.0/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/Users/ulo/t/b.py", line 1, in <module>
    raise Exception()
Exception
msg289541 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-03-13 16:50
That's because the frame-stripping trick is done through import.c which importlib.import_module() doesn't pass through. So thanks for the report, Ulrich, but it is working as expected and we won't be changing import_module() to pass through the C code to keep the C code to a minimum.
msg289574 - (view) Author: Ulrich Petri (ulope) * Date: 2017-03-14 09:44
Thanks for the fast response. 
However I disagree with the assertion that this is "working as expected". IMO the same arguments apply as in the original ticket (esp. Georg Brandls) http://bugs.python.org/issue15110#msg163258.

It is unexpected, confusing (esp. to newer devs) and adds a lot of (unnecessary) noise to tracebacks.

Also this basically demotes `import_module` to second rate league since it doesn't get to enjoy the niceties of "proper" import which is unfortunate since it's used heavily for example in django.
msg289602 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-03-14 16:22
Yep, I agree it isn't as nice as syntactic import, but that can happen when you're not getting to use dedicated bytecode like syntactic import does.

If you can come up with a patch that adds hardly any more C code -- say about 10 or 20 lines? -- I would consider reviewing the patch.
History
Date User Action Args
2022-04-11 14:58:44adminsetgithub: 73973
2017-03-14 16:22:18brett.cannonsetmessages: + msg289602
2017-03-14 09:44:54ulopesetmessages: + msg289574
2017-03-13 16:50:33brett.cannonsetstatus: open -> closed

nosy: + brett.cannon
messages: + msg289541

resolution: not a bug
stage: resolved
2017-03-10 15:50:57ulopecreate