Optimize the traceback Module For Core Runtime Use (and Support Custom Builds Better) #111915
Labels
3.13
bugs and security fixes
interpreter-core
(Objects, Python, Grammar, and Parser dirs)
type-feature
A feature request or enhancement
Feature or enhancement
Since gh-110702 (Oct. 2023), we now rely on the
traceback
module for the operation of the core runtime. The defaultsys.excepthook()
implementation,_PyErr_Display()
, imports thetraceback
module and callstraceback._print_exception_bltin()
. If there are any problems it falls back to a C implementation. (I haven't looked closely but, given how much code was removed in that PR, I'm guessing the fallback code doesn't do nearly as much as_PyErr_Display()
used to.)Here's the thing that caught my attention: we still import
traceback
from disk. That means there are some extra factors we must consider for the module, as a component of the core runtime (effectively):traceback
from disk is "slow" (at least the initial import)sys.excepthook()
, which adds to import time (and surface area for possible failures)sys.excepthook()
needstraceback
module might be impacted(Regarding the points about code-we-don't-need and dependencies, this is similar to what happened with
collections.abc
, from which we ended up factoring out_collections_abc
, to speed up site.py.)The consequences of the above aren't necessarily1 especially severe because:
sys.excepthook()
(_PyErr_Display()
) falls back to a (less informative?) C implementation iftraceback
can't be imported or there's any other failure related to callingtraceback._print_exception_bltin()
sys.excepthook()
is invoked (i.e. implicitly, viaPy_Main()
), it happens in response to an uncaught exception right before the process exitsWith all that in mind, I think it would be worth doing the following:
_traceback_core
) containing only the code we need forsys.excepthook()
and with minimal dependenciesCC @pablogsal
Footnotes
I anticipate any work done for this issue will not require a significant effort, and the risk of negative consequences if we do nothing is not insignificant, so ISTM it would be worth doing to reduce that risk, however hypothetical it might be. ↩
The text was updated successfully, but these errors were encountered: