FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-109653: Improve import time of `logging` by lazy loading `traceback` by danielhollas · Pull Request #112995 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 13 additions & 1 deletion Lib/logging/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
To use, simply 'import logging' and log away!
"""

import sys, os, time, io, re, traceback, warnings, weakref, collections.abc
import sys, os, time, io, re, warnings, weakref, collections.abc

# Speed up import time by delaying traceback import until needed
traceback = None

from types import GenericAlias
from string import Template
Expand Down Expand Up @@ -653,6 +656,9 @@ def formatException(self, ei):
This default implementation just uses
traceback.print_exception()
"""
global traceback
if traceback is None:
import traceback
sio = io.StringIO()
tb = ei[2]
# See issues #9427, #1553375. Commented out for now.
Expand Down Expand Up @@ -1061,6 +1067,9 @@ def handleError(self, record):
The record which was being processed is passed in to this method.
"""
if raiseExceptions and sys.stderr: # see issue 13807
global traceback
if traceback is None:
import traceback
exc = sys.exception()
try:
sys.stderr.write('--- Logging error ---\n')
Expand Down Expand Up @@ -1601,6 +1610,9 @@ def findCaller(self, stack_info=False, stacklevel=1):
co = f.f_code
sinfo = None
if stack_info:
global traceback
if traceback is None:
import traceback
with io.StringIO() as sio:
sio.write("Stack (most recent call last):\n")
traceback.print_stack(f, file=sio)
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reduce the import time of :mod:`logging` module by ~15%. Patch by Daniel Hollas.

Back | FazBrowse Home | New Git URL