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.
This commit is contained in:
Jeremy Hylton 2003-01-23 18:29:29 +00:00
parent ba60319a78
commit 250684ddd8
1 changed files with 18 additions and 24 deletions

View File

@ -19,7 +19,7 @@ Logging package for Python. Based on PEP 282 and comments thereto in
comp.lang.python, and influenced by Apache's log4j system. comp.lang.python, and influenced by Apache's log4j system.
Should work under Python versions >= 1.5.2, except that source line Should work under Python versions >= 1.5.2, except that source line
information is not available unless 'inspect' is. information is not available unless 'sys._getframe()' is.
Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved. Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
@ -33,10 +33,6 @@ try:
import threading import threading
except ImportError: except ImportError:
thread = None thread = None
try:
import inspect
except ImportError:
inspect = None
__author__ = "Vinay Sajip <vinay_sajip@red-dove.com>" __author__ = "Vinay Sajip <vinay_sajip@red-dove.com>"
__status__ = "alpha" __status__ = "alpha"
@ -56,6 +52,13 @@ else:
_srcfile = __file__ _srcfile = __file__
_srcfile = os.path.normcase(_srcfile) _srcfile = os.path.normcase(_srcfile)
# _srcfile is only used in conjunction with sys._getframe().
# To provide compatibility with older versions of Python, set _srcfile
# to None if _getframe() is not available; this value will prevent
# findCaller() from being called.
if not hasattr(sys, "_getframe"):
_srcfile = None
# #
#_startTime is used as the base when calculating the relative time of events #_startTime is used as the base when calculating the relative time of events
# #
@ -927,19 +930,14 @@ class Logger(Filterer):
Find the stack frame of the caller so that we can note the source Find the stack frame of the caller so that we can note the source
file name and line number. file name and line number.
""" """
rv = (None, None) f = sys._getframe(1)
frame = inspect.currentframe().f_back while 1:
while frame: co = f.f_code
sfn = inspect.getsourcefile(frame) filename = os.path.normcase(co.co_filename)
if sfn: if filename == _srcfile:
sfn = os.path.normcase(sfn) f = f.f_back
if sfn != _srcfile: continue
#print frame.f_code.co_code return filename, f.f_lineno
lineno = inspect.getlineno(frame)
rv = (sfn, lineno)
break
frame = frame.f_back
return rv
def makeRecord(self, name, level, fn, lno, msg, args, exc_info): def makeRecord(self, name, level, fn, lno, msg, args, exc_info):
""" """
@ -953,12 +951,8 @@ class Logger(Filterer):
Low-level logging routine which creates a LogRecord and then calls Low-level logging routine which creates a LogRecord and then calls
all the handlers of this logger to handle the record. all the handlers of this logger to handle the record.
""" """
if inspect and _srcfile: if _srcfile:
_acquireLock()
try:
fn, lno = self.findCaller() fn, lno = self.findCaller()
finally:
_releaseLock()
else: else:
fn, lno = "<unknown file>", 0 fn, lno = "<unknown file>", 0
if exc_info: if exc_info: