cpython: ac86e5b2d45b (original) (raw)

Mercurial > cpython

changeset 95669:ac86e5b2d45b

Issue #21217: inspect.getsourcelines() now tries to compute the start and end lines from the code object, fixing an issue when a lambda function is used as decorator argument. Patch by Thomas Ballinger. [#21217]

Antoine Pitrou solipsis@pitrou.net
date Wed, 15 Apr 2015 00:41:29 +0200
parents 966f3b22a67b
children f0a00ee094ff
files Lib/inspect.py Lib/test/inspect_fodder2.py Lib/test/test_inspect.py Misc/ACKS Misc/NEWS
diffstat 5 files changed, 49 insertions(+), 17 deletions(-)[+] [-] Lib/inspect.py 37 Lib/test/inspect_fodder2.py 10 Lib/test/test_inspect.py 11 Misc/ACKS 1 Misc/NEWS 7

line wrap: on

line diff

--- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -32,6 +32,7 @@ Here are some of the useful functions pr 'Yury Selivanov yselivanov@sprymix.com') import ast +import dis import enum import importlib.machinery import itertools @@ -49,18 +50,10 @@ from operator import attrgetter from collections import namedtuple, OrderedDict

Create constants for the compiler flags in Include/code.h

-# We try to get them from dis to avoid duplication, but fall -# back to hard-coding so the dependency is optional -try:

-except ImportError:

-else:

+# We try to get them from dis to avoid duplication +mod_dict = globals() +for k, v in dis.COMPILER_FLAG_NAMES.items():

See Include/object.h

TPFLAGS_IS_ABSTRACT = 1 << 20 @@ -888,6 +881,14 @@ def getblock(lines): pass return lines[:blockfinder.last] +def _line_number_helper(code_obj, lines, lnum):

+

+ def getsourcelines(object): """Return a list of source lines and starting line number for an object. @@ -899,8 +900,16 @@ def getsourcelines(object): object = unwrap(object) lines, lnum = findsource(object)

def getsource(object): """Return the text of the source code for an object.

--- a/Lib/test/inspect_fodder2.py +++ b/Lib/test/inspect_fodder2.py @@ -110,6 +110,14 @@ def annotated(arg1: list): def keyword_only_arg(*, arg): pass +@wrap(lambda: None) +def func114():

+ +class ClassWithMethod:

+ from functools import wraps def decorator(func): @@ -118,7 +126,7 @@ def decorator(func): return 42 return fake -#line 121 +#line 129 @decorator def real(): return 20

--- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -392,6 +392,9 @@ class TestRetrievingSourceCode(GetSource finally: linecache.getlines = getlines

+ class TestDecorators(GetSourceBase): fodderModule = mod2 @@ -402,7 +405,10 @@ class TestDecorators(GetSourceBase): self.assertSourceEqual(mod2.gone, 9, 10) def test_getsource_unwrap(self):

+

class TestOneliners(GetSourceBase): fodderModule = mod2 @@ -497,6 +503,9 @@ class TestBuggyCases(GetSourceBase): self.assertRaises(IOError, inspect.findsource, co) self.assertRaises(IOError, inspect.getsource, co)

+ class TestNoEOL(GetSourceBase): def init(self, *args, **kwargs): self.tempdir = TESTFN + '_dir'

--- a/Misc/ACKS +++ b/Misc/ACKS @@ -72,6 +72,7 @@ Dwayne Bailey Stig Bakken Greg Ball Luigi Ballabio +Thomas Ballinger Jeff Balogh Manuel Balsera Matt Bandy

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -1,4 +1,4 @@ -+++++++++++ ++++++++++++ Python News +++++++++++ @@ -9,6 +9,7 @@ Release date: XXX Core and Builtins ----------------- +