summaryrefslogtreecommitdiffstats
path: root/Lib/warnings.py
diff options
context:
space:
mode:
authorThomas Kluyver <takowl@gmail.com>2018-06-08 19:28:37 (GMT)
committerBrett Cannon <brettcannon@users.noreply.github.com>2018-06-08 19:28:37 (GMT)
commit11a896652ee98aa44e59ed25237f9efb56635dcf (patch)
tree1f63d8ddb6436d64db43ae7147bddbd994cd06a8 /Lib/warnings.py
parent3b0b90c8c3b8161f0ae9005b83b9b6449d4a8476 (diff)
downloadcpython-11a896652ee98aa44e59ed25237f9efb56635dcf.zip
cpython-11a896652ee98aa44e59ed25237f9efb56635dcf.tar.gz
cpython-11a896652ee98aa44e59ed25237f9efb56635dcf.tar.bz2
bpo-33375: Get filename for warnings from frame.f_code.co_filename (GH-6622)
More consistent with how other parts of Python find the filename (e.g. tracebacks and pdb).
Diffstat (limited to 'Lib/warnings.py')
-rw-r--r--Lib/warnings.py16
1 files changed, 2 insertions, 14 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py
index 81f9864..6830b60 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -303,28 +303,16 @@ def warn(message, category=None, stacklevel=1, source=None):
raise ValueError
except ValueError:
globals = sys.__dict__
+ filename = "sys"
lineno = 1
else:
globals = frame.f_globals
+ filename = frame.f_code.co_filename
lineno = frame.f_lineno
if '__name__' in globals:
module = globals['__name__']
else:
module = "<string>"
- filename = globals.get('__file__')
- if filename:
- fnl = filename.lower()
- if fnl.endswith(".pyc"):
- filename = filename[:-1]
- else:
- if module == "__main__":
- try:
- filename = sys.argv[0]
- except AttributeError:
- # embedded interpreters don't have sys.argv, see bug #839151
- filename = '__main__'
- if not filename:
- filename = module
registry = globals.setdefault("__warningregistry__", {})
warn_explicit(message, category, filename, lineno, module, registry,
globals, source)