Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 250684d

Browse files
committed
Use lightweight introspection instead of the inspect module hammer.
Removing locking are findCaller() calls as the implementation using sys._getframe() is thread-safe. Changes reviewed by Vinay.
1 parent ba60319 commit 250684d

1 file changed

Lines changed: 18 additions & 24 deletions

File tree

Lib/logging/__init__.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
comp.lang.python, and influenced by Apache's log4j system.
2020
2121
Should work under Python versions >= 1.5.2, except that source line
22-
information is not available unless 'inspect' is.
22+
information is not available unless 'sys._getframe()' is.
2323
2424
Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
2525
@@ -33,10 +33,6 @@
3333
import threading
3434
except ImportError:
3535
thread = None
36-
try:
37-
import inspect
38-
except ImportError:
39-
inspect = None
4036

4137
__author__ = "Vinay Sajip <[email protected]>"
4238
__status__ = "alpha"
@@ -56,6 +52,13 @@
5652
_srcfile = __file__
5753
_srcfile = os.path.normcase(_srcfile)
5854

55+
# _srcfile is only used in conjunction with sys._getframe().
56+
# To provide compatibility with older versions of Python, set _srcfile
57+
# to None if _getframe() is not available; this value will prevent
58+
# findCaller() from being called.
59+
if not hasattr(sys, "_getframe"):
60+
_srcfile = None
61+
5962
#
6063
#_startTime is used as the base when calculating the relative time of events
6164
#
@@ -927,19 +930,14 @@ def findCaller(self):
927930
Find the stack frame of the caller so that we can note the source
928931
file name and line number.
929932
"""
930-
rv = (None, None)
931-
frame = inspect.currentframe().f_back
932-
while frame:
933-
sfn = inspect.getsourcefile(frame)
934-
if sfn:
935-
sfn = os.path.normcase(sfn)
936-
if sfn != _srcfile:
937-
#print frame.f_code.co_code
938-
lineno = inspect.getlineno(frame)
939-
rv = (sfn, lineno)
940-
break
941-
frame = frame.f_back
942-
return rv
933+
f = sys._getframe(1)
934+
while 1:
935+
co = f.f_code
936+
filename = os.path.normcase(co.co_filename)
937+
if filename == _srcfile:
938+
f = f.f_back
939+
continue
940+
return filename, f.f_lineno
943941

944942
def makeRecord(self, name, level, fn, lno, msg, args, exc_info):
945943
"""
@@ -953,12 +951,8 @@ def _log(self, level, msg, args, exc_info=None):
953951
Low-level logging routine which creates a LogRecord and then calls
954952
all the handlers of this logger to handle the record.
955953
"""
956-
if inspect and _srcfile:
957-
_acquireLock()
958-
try:
959-
fn, lno = self.findCaller()
960-
finally:
961-
_releaseLock()
954+
if _srcfile:
955+
fn, lno = self.findCaller()
962956
else:
963957
fn, lno = "<unknown file>", 0
964958
if exc_info:

0 commit comments

Comments
 (0)