Skip to content

gh-150072: Optimize logging imports by lazily importing traceback#150073

Open
ByteFlowing1337 wants to merge 1 commit into
python:mainfrom
ByteFlowing1337:faster-logging
Open

gh-150072: Optimize logging imports by lazily importing traceback#150073
ByteFlowing1337 wants to merge 1 commit into
python:mainfrom
ByteFlowing1337:faster-logging

Conversation

@ByteFlowing1337
Copy link
Copy Markdown
Contributor

@ByteFlowing1337 ByteFlowing1337 commented May 19, 2026

@hugovk
Copy link
Copy Markdown
Member

hugovk commented May 19, 2026

Unfortunately the lazy import fails when trying to test logging during shutdown:

======================================================================
FAIL: test_logging_at_shutdown (test.test_logging.ModuleLevelMiscTest.test_logging_at_shutdown)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/runner/work/cpython/cpython/Lib/test/test_logging.py", line 5259, in test_logging_at_shutdown
    self.assertIn("exception in __del__", err)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 'exception in __del__' not found in '--- Logging error ---\nException ignored while calling deallocator <function A.__del__ at 0x20002d3e8d0>:\nTraceback (most recent call last):\n  File "<string>", line 9, in __del__\n  File "/Users/runner/work/cpython/cpython/Lib/logging/__init__.py", line 2194, in exception\n  File "/Users/runner/work/cpython/cpython/Lib/logging/__init__.py", line 2186, in error\n  File "/Users/runner/work/cpython/cpython/Lib/logging/__init__.py", line 1553, in error\n  File "/Users/runner/work/cpython/cpython/Lib/logging/__init__.py", line 1669, in _log\n  File "/Users/runner/work/cpython/cpython/Lib/logging/__init__.py", line 1685, in handle\n  File "/Users/runner/work/cpython/cpython/Lib/logging/__init__.py", line 1741, in callHandlers\n  File "/Users/runner/work/cpython/cpython/Lib/logging/__init__.py", line 1031, in handle\n  File "/Users/runner/work/cpython/cpython/Lib/logging/__init__.py", line 1163, in emit\n  File "/Users/runner/work/cpython/cpython/Lib/logging/__init__.py", line 1080, in handleError\nImportError: sys.meta_path is None, Python is likely shutting down\n'

@ByteFlowing1337
Copy link
Copy Markdown
Contributor Author

We can fix the test by adding atexit.register(lambda: traceback) in __init__.py, which guarantees traceback will be imported before the interpreter cleans up. But sadly, the wall-clock time will still be unoptimized in that case.

@picnixz
Copy link
Copy Markdown
Member

picnixz commented May 19, 2026

I would prefer that we do not overcomplicate atexit callbacks in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants