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

Skip to content

Commit 194bcaf

Browse files
committed
Issue #21709: Improved implementation to cover the frozen module case.
1 parent 307bccc commit 194bcaf

1 file changed

Lines changed: 34 additions & 28 deletions

File tree

Lib/logging/__init__.py

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,6 @@
5151
# Miscellaneous module data
5252
#---------------------------------------------------------------------------
5353

54-
#
55-
# _srcfile is used when walking the stack to check when we've got the first
56-
# caller stack frame.
57-
#
58-
if hasattr(sys, 'frozen'):
59-
_srcfile = os.path.join('logging', '__init__.py')
60-
else:
61-
_srcfile = __file__
62-
_srcfile = os.path.normcase(_srcfile)
63-
64-
65-
if hasattr(sys, '_getframe'):
66-
currentframe = lambda: sys._getframe(3)
67-
else: #pragma: no cover
68-
def currentframe():
69-
"""Return the frame object for the caller's stack frame."""
70-
try:
71-
raise Exception
72-
except Exception:
73-
return sys.exc_info()[2].tb_frame.f_back
74-
75-
# _srcfile is only used in conjunction with sys._getframe().
76-
# To provide compatibility with older versions of Python, set _srcfile
77-
# to None if _getframe() is not available; this value will prevent
78-
# findCaller() from being called.
79-
#if not hasattr(sys, "_getframe"):
80-
# _srcfile = None
81-
8254
#
8355
#_startTime is used as the base when calculating the relative time of events
8456
#
@@ -172,6 +144,40 @@ def addLevelName(level, levelName):
172144
finally:
173145
_releaseLock()
174146

147+
if hasattr(sys, '_getframe'):
148+
currentframe = lambda: sys._getframe(3)
149+
else: #pragma: no cover
150+
def currentframe():
151+
"""Return the frame object for the caller's stack frame."""
152+
try:
153+
raise Exception
154+
except Exception:
155+
return sys.exc_info()[2].tb_frame.f_back
156+
157+
#
158+
# _srcfile is used when walking the stack to check when we've got the first
159+
# caller stack frame, by skipping frames whose filename is that of this
160+
# module's source. It therefore should contain the filename of this module's
161+
# source file.
162+
#
163+
# Ordinarily we would use __file__ for this, but frozen modules don't always
164+
# have __file__ set, for some reason (see Issue #21736). Thus, we get the
165+
# filename from a handy code object from a function defined in this module.
166+
# (There's no particular reason for picking addLevelName.)
167+
#
168+
169+
_srcfile = os.path.normcase(addLevelName.__code__.co_filename)
170+
171+
# _srcfile is only used in conjunction with sys._getframe().
172+
# To provide compatibility with older versions of Python, set _srcfile
173+
# to None if _getframe() is not available; this value will prevent
174+
# findCaller() from being called. You can also do this if you want to avoid
175+
# the overhead of fetching caller information, even when _getframe() is
176+
# available.
177+
#if not hasattr(sys, '_getframe'):
178+
# _srcfile = None
179+
180+
175181
def _checkLevel(level):
176182
if isinstance(level, int):
177183
rv = level

0 commit comments

Comments
 (0)