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: Support disabling file I/O when doing traceback formatting
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, itamarst, vstinner
Priority: normal Keywords:

Created on 2013-10-09 13:17 by itamarst, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg199294 - (view) Author: Itamar Turner-Trauring (itamarst) Date: 2013-10-09 13:17
In certain situations it is best to avoid doing file I/O. For example, a program that runs in an event loop may wish to avoid any potentially blocking operations; reading from a file usually is fast, but can sometimes take arbitrary long. Another example (my specific use case) is a logging library - you don't want to block for an arbitrary amount of time when creating log messages in an application thread (a separate thread would do the writing).

Unfortunately, the traceback.py module reads from files to load the source lines for the traceback (using linecache.py). This means if you want to format a traceback without file I/O you have to either recreate some logic from the standard library, monkey-patch globally, or do a terrible hack you don't want to know about.

It would be better if the there was some way to ask the traceback.py module's functions to not do file I/O. The actual change would be fairly minor I suspect since the formatting functions already support getting None back from linecache.
msg199370 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-10-10 08:14
> Unfortunately, the traceback.py module reads from files to load the source lines for the traceback (using linecache.py).

linecache is supposed to cache the result. When all files generating tracebacks of your project are cached, only os.stat() is called on each file to check if the file has been modified.

But I'm not sure I understood your request: do you want the line content in your traceback or not? If you don't want the line content, the traceback module should be modified to add an option "don't read file content".
msg383101 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-12-15 22:52
Since this issue was created, the traceback module changed considerably, and now we have the TracebackException/StackSummary classes which capture information about an exception for later rendering. The lookup_lines arg controls the timing of the file access: if it is false, this is delayed until render-time.

I believe this solves the use case of the logger - the file access can be delayed till the logger thread renders the exception. I'm not sure I understand the event loop use case, but presumably there, too, the exceptions are not being rendered (because we don't want IO during that loop).
History
Date User Action Args
2022-04-11 14:57:51adminsetgithub: 63405
2020-12-31 14:00:50iritkatrielsetstatus: pending -> closed
stage: resolved
2020-12-15 22:52:58iritkatrielsetstatus: open -> pending
versions: - Python 3.9
nosy: + iritkatriel

messages: + msg383101

resolution: out of date
2020-11-04 17:01:30iritkatrielsetversions: + Python 3.9, Python 3.10
2013-10-10 08:14:15vstinnersetnosy: + vstinner
messages: + msg199370
2013-10-09 13:17:03itamarstcreate