Skip to content

Commit

Permalink
handle decreasing and increasing line numbers from dis.findlinestarts
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Mar 2, 2021
1 parent 0a53b42 commit 37520e2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
16 changes: 5 additions & 11 deletions src/twisted/python/deprecate.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,22 +604,16 @@ def warnAboutFunction(offender, warningString):
# inspect.getmodule() is attractive, but somewhat
# broken in Python < 2.6. See Python bug 4845.
offenderModule = sys.modules[offender.__module__]
filename = inspect.getabsfile(offenderModule)
lineStarts = list(findlinestarts(offender.__code__))
lastLineNo = lineStarts[-1][1]
globals = offender.__globals__

kwargs = dict(
warn_explicit(
warningString,
category=DeprecationWarning,
filename=filename,
lineno=lastLineNo,
filename=inspect.getabsfile(offenderModule),
lineno=max(lineNumber for _, lineNumber in findlinestarts(offender.__code__)),
module=offenderModule.__name__,
registry=globals.setdefault("__warningregistry__", {}),
registry=offender.__globals__.setdefault("__warningregistry__", {}),
module_globals=None,
)

warn_explicit(warningString, **kwargs)


def _passedArgSpec(argspec, positional, keyword):
"""
Expand Down
9 changes: 5 additions & 4 deletions src/twisted/trial/_synctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,10 +1154,11 @@ def flushWarnings(self, offendingFunctions=None):

if filename != os.path.normcase(aWarning.filename):
continue
lineStarts = list(_findlinestarts(aFunction.__code__))
first = lineStarts[0][1]
last = lineStarts[-1][1]
if not (first <= aWarning.lineno <= last):
lineNumbers = [
lineNumber
for _, lineNumber in _findlinestarts(aFunction.__code__)
]
if not (min(lineNumbers) <= aWarning.lineno <= max(lineNumbers)):
continue
# The warning points to this function, flush it and move on
# to the next warning.
Expand Down

0 comments on commit 37520e2

Please sign in to comment.