diff options
author | Steve Dower <steve.dower@microsoft.com> | 2015-09-06 22:27:42 -0700 |
---|---|---|
committer | Steve Dower <steve.dower@microsoft.com> | 2015-09-06 22:27:42 -0700 |
commit | f35bd306ffa2c05a1297435bb15cd3b4d47b3977 (patch) | |
tree | 8a909b9eb6a5325a5ebbcbdc5858dd798d78db0d /Lib/warnings.py | |
parent | da19767b86dcee5810ad8c77a05811be041d7c89 (diff) | |
parent | c1635e497d060bae076127152801af2b2ec552ff (diff) | |
download | cpython-git-f35bd306ffa2c05a1297435bb15cd3b4d47b3977.tar.gz |
Merge from 3.5.0 branch.
Diffstat (limited to 'Lib/warnings.py')
-rw-r--r-- | Lib/warnings.py | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py index 16246b4365..1d4fb208f8 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -160,6 +160,20 @@ def _getcategory(category): return cat +def _is_internal_frame(frame): + """Signal whether the frame is an internal CPython implementation detail.""" + filename = frame.f_code.co_filename + return 'importlib' in filename and '_bootstrap' in filename + + +def _next_external_frame(frame): + """Find the next frame that doesn't involve CPython internals.""" + frame = frame.f_back + while frame is not None and _is_internal_frame(frame): + frame = frame.f_back + return frame + + # Code typically replaced by _warnings def warn(message, category=None, stacklevel=1): """Issue a warning, or maybe ignore it or raise an exception.""" @@ -174,13 +188,23 @@ def warn(message, category=None, stacklevel=1): "not '{:s}'".format(type(category).__name__)) # Get context information try: - caller = sys._getframe(stacklevel) + if stacklevel <= 1 or _is_internal_frame(sys._getframe(1)): + # If frame is too small to care or if the warning originated in + # internal code, then do not try to hide any frames. + frame = sys._getframe(stacklevel) + else: + frame = sys._getframe(1) + # Look for one frame less since the above line starts us off. + for x in range(stacklevel-1): + frame = _next_external_frame(frame) + if frame is None: + raise ValueError except ValueError: globals = sys.__dict__ lineno = 1 else: - globals = caller.f_globals - lineno = caller.f_lineno + globals = frame.f_globals + lineno = frame.f_lineno if '__name__' in globals: module = globals['__name__'] else: @@ -374,7 +398,6 @@ try: defaultaction = _defaultaction onceregistry = _onceregistry _warnings_defaults = True - except ImportError: filters = [] defaultaction = "default" |