Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inconsistent results with inspect.getsource / .getsourcelines #66551

Open
isedev mannequin opened this issue Sep 7, 2014 · 3 comments
Open

inconsistent results with inspect.getsource / .getsourcelines #66551

isedev mannequin opened this issue Sep 7, 2014 · 3 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@isedev
Copy link
Mannequin

isedev mannequin commented Sep 7, 2014

BPO 22355
Nosy @1st1, @tirkarthi

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2014-09-07.14:45:41.213>
labels = ['type-bug', 'library']
title = 'inconsistent results with inspect.getsource / .getsourcelines'
updated_at = <Date 2018-11-07.06:22:23.846>
user = 'https://bugs.python.org/isedev'

bugs.python.org fields:

activity = <Date 2018-11-07.06:22:23.846>
actor = 'xtreak'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2014-09-07.14:45:41.213>
creator = 'isedev'
dependencies = []
files = []
hgrepos = []
issue_num = 22355
keywords = []
message_count = 3.0
messages = ['226537', '226538', '329406']
nosy_count = 3.0
nosy_names = ['isedev', 'yselivanov', 'xtreak']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue22355'
versions = ['Python 2.7', 'Python 3.4', 'Python 3.5']

@isedev
Copy link
Mannequin Author

isedev mannequin commented Sep 7, 2014

The functions inspect.getsource() and inspect.getsourcelines() return inconsistent results for frames corresponding to class definitions within a function.

Test code:

import sys
import inspect

def case1():
    class C:
        def __init__(self):
            pass
    c = C()

def case2():
    a = 1
    class C:
        def __init__(self):
            pass
    c = C()

def case3():
    def fn():
        pass
    class C:
        def __init__(self):
            pass
    c = C()

def trace(frame,event,arg):
    code = frame.f_code
    print('name:',code.co_name)
    print('source:\n',inspect.getsource(code),'\n')

for case in ('case1','case2','case3'):
    print('#####',case)
    call = getattr(sys.modules[__name__],case)
    sys.settrace(trace)
    try:
        call()
    finally:
        sys.settrace(None)

Result:

##### case1
name: case1
source:
def case1():
class C:
def __init__(self):
pass
c = C()

name: C
source:
def case1():
class C:
def __init__(self):
pass
c = C()

name: __init__
source:
def __init__(self):
pass

##### case2
name: case2
source:
def case2():
a = 1
class C:
def __init__(self):
pass
c = C()

name: C
source:
def case2():
a = 1
class C:
def __init__(self):
pass
c = C()

name: __init__
source:
def __init__(self):
pass

##### case3
name: case3
source:
def case3():
def fn():
pass
class C:
def __init__(self):
pass
c = C()

name: C
source:
def fn():
pass

name: __init__
source:
def __init__(self):
pass

The source listed for frames named 'C' (the class creation code) is not consistent across all three cases. It could be considered incorrect in all cases as it does not correspond only to the class definition source lines.

@isedev isedev mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Sep 7, 2014
@isedev
Copy link
Mannequin Author

isedev mannequin commented Sep 7, 2014

Possible fix:

--- /usr/lib64/python3.3/inspect.py     2014-06-30 19:21:52.000000000 +0200
+++ inspect.py  2014-09-07 17:41:29.463936079 +0200
@@ -600,7 +600,8 @@
         if not hasattr(object, 'co_firstlineno'):
             raise IOError('could not find function definition')
         lnum = object.co_firstlineno - 1
-        pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
+        pat = re.compile(
+            r'^(\s*(?:def|class)\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
         while lnum > 0:
             if pat.match(lines[lnum]): break
             lnum = lnum - 1

@terryjreedy terryjreedy changed the title inconsistent results with inspect.getsource() / inspect.getsourcelines() inconsistent results with inspect.getsource / .getsourcelines Sep 12, 2014
@tirkarthi
Copy link
Member

I think this issue is covered with bpo-35101. It has an open PR as per the proposed fix with tests : https://github.com/python/cpython/pull/10209/files

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant