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.
Created on 2012年10月13日 12:51 by mattheww, last changed 2022年04月11日 14:57 by admin.
| Messages (8) | |||
|---|---|---|---|
| msg172806 - (view) | Author: Matthew Woodcraft (mattheww) | Date: 2012年10月13日 12:51 | |
If I run my code using 'python -m' and there is an unhandled exception, the tracebacks include lines from runpy.py (and now sometimes from importlib._bootstrap) which don't provide useful information, and tend to overwhelm the valuable part of the traceback. I suppose this is in some sense a regression from Python 2.4. echo fail > fail.py python3.3 -m fail python -m fail Traceback (most recent call last): File "/usr/lib/python3.3/runpy.py", line 160, in _run_module_as_main "__main__", fname, loader, pkg_name) File "/usr/lib/python3.3/runpy.py", line 73, in _run_code exec(code, run_globals) File "./fail.py", line 2, in <module> fail NameError: name 'fail' is not defined Here I think it would be better to omit the first two traceback entries. Syntax errors are worse: echo syntax error > fail.py python3.3 -m fail Traceback (most recent call last): File "/usr/lib/python3.3/runpy.py", line 140, in _run_module_as_main mod_name, loader, code, fname = _get_module_details(mod_name) File "/usr/lib/python3.3/runpy.py", line 114, in _get_module_details code = loader.get_code(mod_name) File "<frozen importlib._bootstrap>", line 981, in get_code File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed File "./fail.py", line 1 syntax error Here there are four traceback entries which could be omitted. |
|||
| msg172808 - (view) | Author: Alyssa Coghlan (ncoghlan) * (Python committer) | Date: 2012年10月13日 14:52 | |
Agreed. We do have a mechanism in place to deal with this specifically for importlib in 3.3 (which is why the first traceback is cleaner, although it's not triggering in the SyntaxError case for some reason), but it makes sense to hide the noise from runpy as well. Flagging 3.3 for the moment, but if the change is too intrusive it will end up being 3.4 only. |
|||
| msg245898 - (view) | Author: Alyssa Coghlan (ncoghlan) * (Python committer) | Date: 2015年06月28日 03:23 | |
Hmm, when I haven't moved an issue forward in over two years, I guess that's a fair sign I'm not *actually* working on it... |
|||
| msg300711 - (view) | Author: Dmitry Kazakov (vaultah) * | Date: 2017年08月22日 16:43 | |
In order to load and compile the module code, runpy calls the loader's get_code method. Because that happens outside of the normal import process, and PyImport_ImportModuleLevelObject is currently the only place where remove_importlib_frames is being invoked, tracebacks of exceptions occurring in get_code are kept unmodified. Would it be wrong to drop all importlib frames from all unhandled exceptions, except when the -v flag is present? On the other hand, since the patch from #issue15486 seems to work fine in most scenarios, I propose removing both runpy and importlib traceback entries locally inside the runpy module. There may be better solutions, though... In any case, I'm willing to write a patch. |
|||
| msg300742 - (view) | Author: Alyssa Coghlan (ncoghlan) * (Python committer) | Date: 2017年08月23日 05:26 | |
The reason we don't always drop the importlib frames in general is because we need them when we're debugging importlib itself, as well as when folks are calling into it directly. However, I think it would be reasonable to do it unconditionally for unhandled exceptions triggered via "-m": if we want the unfiltered exception in that case, we can either edit runpy to skip the filtering (when working on runpy), or else call the runpy API directly, rather than via -m. That means I like the idea of implementing the traceback filtering and display inside runpy._run_module_as_main itself, rather than relying on the default interpreter level traceback display. One potential approach to that would be to expand the current code that suppresses tracebacks entirely for runpy._Error exceptions to also handle the "except Exception as exc:" case and print out a traceback variants that omits any frames from runpy, importlib, or _frozen_importlib (KeyboardInterrupt and SystemExit would still escape unmodified) As far as the implementation goes, this could potentially be made a general feature of the new(ish) TracebackException https://docs.python.org/3/library/traceback.html#tracebackexception-objects class by accepting an "ignore_modules" iterable as a parameter to TracebackException.format() and then ignoring frames running code from those modules (conveniently, runpy, importlib, and _frozen_importlib between them will exercise the cases of a single-file module, a self-contained package, and a frozen module, so only namespace package support would end up relying entirely on synthetic test cases). |
|||
| msg300959 - (view) | Author: Dmitry Kazakov (vaultah) * | Date: 2017年08月28日 18:25 | |
Since runpy also handles the execution of directories and zip files, I think it would be reasonable to clean up tracebacks in these cases, too. The ignore_modules argument would be a great addition to the traceback module. Should I create a separate issue/pull request for its implementation? |
|||
| msg300975 - (view) | Author: Alyssa Coghlan (ncoghlan) * (Python committer) | Date: 2017年08月29日 07:46 | |
Aye, separating the two discussions is a good idea. I've filed https://bugs.python.org/issue31299 for the traceback RFE, and added it as a dependency here. |
|||
| msg300978 - (view) | Author: Alyssa Coghlan (ncoghlan) * (Python committer) | Date: 2017年08月29日 09:27 | |
Regarding directories and zipfiles, runpy._run_module_as_main also handles those - the CLI effectively transforms directory and zipfile execution into a "python -m __main__" call with a suitably adjusted sys.path[0] entry (i.e. the given directory or zip archive, rather than the current directory). |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:37 | admin | set | github: 60421 |
| 2020年01月29日 00:38:55 | brett.cannon | set | nosy:
- brett.cannon |
| 2017年08月29日 09:27:06 | ncoghlan | set | messages: + msg300978 |
| 2017年08月29日 07:46:41 | ncoghlan | set | dependencies:
+ Add "ignore_modules" option to TracebackException.format() messages: + msg300975 |
| 2017年08月28日 18:25:21 | vaultah | set | messages: + msg300959 |
| 2017年08月23日 05:26:26 | ncoghlan | set | messages:
+ msg300742 versions: + Python 3.7, - Python 3.3, Python 3.4 |
| 2017年08月22日 16:43:56 | vaultah | set | nosy:
+ vaultah messages: + msg300711 |
| 2015年06月28日 03:23:27 | ncoghlan | set | assignee: ncoghlan -> messages: + msg245898 |
| 2014年12月12日 01:13:42 | Arfrever | set | nosy:
+ Arfrever |
| 2012年11月13日 07:02:53 | eric.snow | set | nosy:
+ eric.snow |
| 2012年10月25日 16:36:05 | ezio.melotti | set | nosy:
+ ezio.melotti components: + Library (Lib) stage: needs patch |
| 2012年10月13日 14:52:32 | ncoghlan | set | versions:
+ Python 3.3, Python 3.4 nosy: + brett.cannon messages: + msg172808 assignee: ncoghlan |
| 2012年10月13日 13:31:42 | pitrou | set | nosy:
+ ncoghlan |
| 2012年10月13日 12:51:41 | mattheww | create | |