]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #21709: Fix the logging module to not depend upon __file__ being set
authorGregory P. Smith <greg@krypto.org>
Thu, 22 Oct 2015 20:09:50 +0000 (13:09 -0700)
committerGregory P. Smith <greg@krypto.org>
Thu, 22 Oct 2015 20:09:50 +0000 (13:09 -0700)
properly to get the filename of its caller from the stack.  This allows it
to work if run in a frozen or embedded environment where the module's
.__file__ attribute does not match its code object's .co_filename.

This same much simpler always correct approach has already been deployed and
used widely in Python 3.4 per the issue referenced above.

Lib/logging/__init__.py
Misc/NEWS

index bd4afeb1aa34dcafc45ed3a3c01a84be46393787..2c0f14f5b0e9d0e18f189fb66be7be1c66328708 100644 (file)
@@ -59,18 +59,6 @@ try:
 except NameError:
     _unicode = False
 
-#
-# _srcfile is used when walking the stack to check when we've got the first
-# caller stack frame.
-#
-if hasattr(sys, 'frozen'): #support for py2exe
-    _srcfile = "logging%s__init__%s" % (os.sep, __file__[-4:])
-elif __file__[-4:].lower() in ['.pyc', '.pyo']:
-    _srcfile = __file__[:-4] + '.py'
-else:
-    _srcfile = __file__
-_srcfile = os.path.normcase(_srcfile)
-
 # next bit filched from 1.5.2's inspect.py
 def currentframe():
     """Return the frame object for the caller's stack frame."""
@@ -82,6 +70,12 @@ def currentframe():
 if hasattr(sys, '_getframe'): currentframe = lambda: sys._getframe(3)
 # done filching
 
+#
+# _srcfile is used when walking the stack to check when we've got the first
+# caller stack frame.
+#
+_srcfile = currentframe.__code__.co_filename
+
 # _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
index 60dfbf02481eeb7b79e96e67a838cca604b0b8d5..c5e1b7fdfac9b5a5eef0fc9808ec99ffc3975bdf 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -46,6 +46,11 @@ Core and Builtins
 Library
 -------
 
+- Issue #21709: Fix the logging module to not depend upon __file__ being set
+  properly to get the filename of its caller from the stack.  This allows it
+  to work if run in a frozen or embedded environment where the module's
+  .__file__ attribute does not match its code object's .co_filename.
+
 - Issue #25319: When threading.Event is reinitialized, the underlying condition
   should use a regular lock rather than a recursive lock.