Skip to content

bpo-38307: Add end_lineno attribute to pyclbr _Objects #24348

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
1f25ed4
compeltes the Stack implementation to yield ending line for each class.
Sep 28, 2019
7d2354a
reverts whitespace and unwanted changes including functions, comments…
Oct 6, 2019
c8a003d
removes leftover whitespace in b/w functions: unwanted
Oct 6, 2019
7e6f079
Initialize stack of (class, indent) pairs.
Feb 4, 2020
b7c1ce7
removes Stack() custom class and adds direct attribute (end_lineno) i…
Feb 4, 2020
3466f63
corrects the attribute name to end_lineno and sets the same without u…
Feb 8, 2020
f2a58c6
removes unwanted module: inspect
Feb 8, 2020
94edb5b
corrects the way of setting attribute
kebab-mai-haddi Feb 16, 2020
454d305
📜🤖 Added by blurb_it.
blurb-it[bot] Mar 16, 2020
dc0166f
wip: added end_lineno in tests
kebab-mai-haddi Mar 24, 2020
5a26a5c
Merge branch 'endline-in-readmodule-module' of github.com:avisrivasta…
kebab-mai-haddi Mar 24, 2020
ea2090b
wip
kebab-mai-haddi Aug 13, 2020
49d0695
compeltes the Stack implementation to yield ending line for each class.
Sep 28, 2019
33aa14c
reverts whitespace and unwanted changes including functions, comments…
Oct 6, 2019
2f9579a
removes leftover whitespace in b/w functions: unwanted
Oct 6, 2019
668fe22
wip: added end_lineno in tests
kebab-mai-haddi Mar 24, 2020
efb49ea
📜🤖 Added by blurb_it.
blurb-it[bot] Mar 16, 2020
963b783
wip
kebab-mai-haddi Aug 13, 2020
d0d0966
fixed conflicts after diverge
kebab-mai-haddi Jan 27, 2021
d924d4a
adds end_lineno as an attribute for Class and Functions objects
kebab-mai-haddi Jan 27, 2021
78f166f
adds the news.
kebab-mai-haddi Jan 27, 2021
fc74ec7
adds positional arguments in the tests and end_lineno as an argument …
kebab-mai-haddi Jan 27, 2021
b5f29c0
removes debugging print statements.
kebab-mai-haddi Jan 27, 2021
f3c09fe
adds endline no in all the tests for the dummy tree"
kebab-mai-haddi Jan 27, 2021
7337551
adds endline no in all the tests for the dummy tree
kebab-mai-haddi Jan 27, 2021
3e8181c
Merge branch 'endline-in-readmodule-module' of github.com:kebab-mai-h…
kebab-mai-haddi Jan 27, 2021
77784e3
Update 2020-03-16-03-03-21.bpo-38307.2cmw2i.rst
terryjreedy Jan 27, 2021
d73395a
Update 2020-03-16-03-03-21.bpo-38307.2cmw2i.rst
terryjreedy Jan 27, 2021
cc82d4f
Merge remote-tracking branch 'upstream/master' into pr_24348
terryjreedy Feb 1, 2021
9238fcd
Fix end_lineno.
terryjreedy Feb 1, 2021
ed20fa4
Add What's New in 3.10 entry
terryjreedy Feb 1, 2021
48b7341
Update Doc/whatsnew/3.10.rst
terryjreedy Feb 1, 2021
9172efc
Direct calls by by keyword
terryjreedy Feb 1, 2021
d0ca986
blank line
terryjreedy Feb 1, 2021
af31c28
Merge branch 'endline-in-readmodule-module' of https://github.com/keb…
terryjreedy Feb 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ py_compile
Added ``--quiet`` option to command-line interface of :mod:`py_compile`.
(Contributed by Gregory Schevchenko in :issue:`38731`.)

pyclbr
------

Added an ``end_lineno`` attribute to the ``Function`` and ``Class``
objects in the tree returned by :func:`pyclbr.readline` and
:func:`pyclbr.readline_ex`. It matches the existing (start) ``lineno``.
(Contributed by Aviral Srivastava in :issue:`38307`.)

shelve
------

Expand Down
18 changes: 9 additions & 9 deletions Lib/idlelib/idle_test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ def test_close(self):
# Nested tree same as in test_pyclbr.py except for supers on C0. C1.
mb = pyclbr
module, fname = 'test', 'test.py'
C0 = mb.Class(module, 'C0', ['base'], fname, 1)
F1 = mb._nest_function(C0, 'F1', 3)
C1 = mb._nest_class(C0, 'C1', 6, [''])
C2 = mb._nest_class(C1, 'C2', 7)
F3 = mb._nest_function(C2, 'F3', 9)
f0 = mb.Function(module, 'f0', fname, 11)
f1 = mb._nest_function(f0, 'f1', 12)
f2 = mb._nest_function(f1, 'f2', 13)
c1 = mb._nest_class(f0, 'c1', 15)
C0 = mb.Class(module, 'C0', ['base'], fname, 1, end_lineno=9)
F1 = mb._nest_function(C0, 'F1', 3, 5)
C1 = mb._nest_class(C0, 'C1', 6, 9, [''])
C2 = mb._nest_class(C1, 'C2', 7, 9)
F3 = mb._nest_function(C2, 'F3', 9, 9)
f0 = mb.Function(module, 'f0', fname, 11, end_lineno=15)
f1 = mb._nest_function(f0, 'f1', 12, 14)
f2 = mb._nest_function(f1, 'f2', 13, 13)
c1 = mb._nest_class(f0, 'c1', 15, 15)
mock_pyclbr_tree = {'C0': C0, 'f0': f0}

# Adjust C0.name, C1.name so tests do not depend on order.
Expand Down
40 changes: 25 additions & 15 deletions Lib/pyclbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
name -- name of the object;
file -- file in which the object is defined;
lineno -- line in the file where the object's definition starts;
end_lineno -- line in the file where the object's definition ends;
parent -- parent of this object, if any;
children -- nested objects contained in this object.
The 'children' attribute is a dictionary mapping names to objects.
Expand Down Expand Up @@ -52,40 +53,50 @@

class _Object:
"Information about Python class or function."
def __init__(self, module, name, file, lineno, parent):
def __init__(self, module, name, file, lineno, end_lineno, parent):
self.module = module
self.name = name
self.file = file
self.lineno = lineno
self.end_lineno = end_lineno
self.parent = parent
self.children = {}
if parent is not None:
parent.children[name] = self


# Odd Function and Class signatures are for back-compatibility.
class Function(_Object):
"Information about a Python function, including methods."
def __init__(self, module, name, file, lineno, parent=None, is_async=False):
super().__init__(module, name, file, lineno, parent)
def __init__(self, module, name, file, lineno,
parent=None, is_async=False, *, end_lineno=None):
super().__init__(module, name, file, lineno, end_lineno, parent)
self.is_async = is_async
if isinstance(parent, Class):
parent.methods[name] = lineno


class Class(_Object):
"Information about a Python class."
def __init__(self, module, name, super_, file, lineno, parent=None):
super().__init__(module, name, file, lineno, parent)
def __init__(self, module, name, super_, file, lineno,
parent=None, *, end_lineno=None):
super().__init__(module, name, file, lineno, end_lineno, parent)
self.super = super_ or []
self.methods = {}


# These 2 functions are used in these tests
# Lib/test/test_pyclbr, Lib/idlelib/idle_test/test_browser.py
def _nest_function(ob, func_name, lineno, is_async=False):
def _nest_function(ob, func_name, lineno, end_lineno, is_async=False):
"Return a Function after nesting within ob."
return Function(ob.module, func_name, ob.file, lineno, ob, is_async)
return Function(ob.module, func_name, ob.file, lineno,
parent=ob, is_async=is_async, end_lineno=end_lineno)

def _nest_class(ob, class_name, lineno, super=None):
def _nest_class(ob, class_name, lineno, end_lineno, super=None):
"Return a Class after nesting within ob."
return Class(ob.module, class_name, super, ob.file, lineno, ob)
return Class(ob.module, class_name, super, ob.file, lineno,
parent=ob, end_lineno=end_lineno)


def readmodule(module, path=None):
"""Return Class objects for the top-level classes in module.
Expand All @@ -108,6 +119,7 @@ def readmodule_ex(module, path=None):
"""
return _readmodule(module, path or [])


def _readmodule(module, path, inpackage=None):
"""Do the hard work for readmodule[_ex].

Expand Down Expand Up @@ -198,9 +210,8 @@ def visit_ClassDef(self, node):
bases.append(name)

parent = self.stack[-1] if self.stack else None
class_ = Class(
self.module, node.name, bases, self.file, node.lineno, parent
)
class_ = Class(self.module, node.name, bases, self.file, node.lineno,
parent=parent, end_lineno=node.end_lineno)
if parent is None:
self.tree[node.name] = class_
self.stack.append(class_)
Expand All @@ -209,9 +220,8 @@ def visit_ClassDef(self, node):

def visit_FunctionDef(self, node, *, is_async=False):
parent = self.stack[-1] if self.stack else None
function = Function(
self.module, node.name, self.file, node.lineno, parent, is_async
)
function = Function(self.module, node.name, self.file, node.lineno,
parent, is_async, end_lineno=node.end_lineno)
if parent is None:
self.tree[node.name] = function
self.stack.append(function)
Expand Down
22 changes: 11 additions & 11 deletions Lib/test/test_pyclbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ def F3(): return 1+1
actual = mb._create_tree(m, p, f, source, t, i)

# Create descriptors, linked together, and expected dict.
f0 = mb.Function(m, 'f0', f, 1)
f1 = mb._nest_function(f0, 'f1', 2)
f2 = mb._nest_function(f1, 'f2', 3)
c1 = mb._nest_class(f0, 'c1', 5)
C0 = mb.Class(m, 'C0', None, f, 6)
F1 = mb._nest_function(C0, 'F1', 8)
C1 = mb._nest_class(C0, 'C1', 11)
C2 = mb._nest_class(C1, 'C2', 12)
F3 = mb._nest_function(C2, 'F3', 14)
f0 = mb.Function(m, 'f0', f, 1, end_lineno=5)
f1 = mb._nest_function(f0, 'f1', 2, 4)
f2 = mb._nest_function(f1, 'f2', 3, 3)
c1 = mb._nest_class(f0, 'c1', 5, 5)
C0 = mb.Class(m, 'C0', None, f, 6, end_lineno=14)
F1 = mb._nest_function(C0, 'F1', 8, 10)
C1 = mb._nest_class(C0, 'C1', 11, 14)
C2 = mb._nest_class(C1, 'C2', 12, 14)
F3 = mb._nest_function(C2, 'F3', 14, 14)
expected = {'f0':f0, 'C0':C0}

def compare(parent1, children1, parent2, children2):
Expand All @@ -203,8 +203,8 @@ def compare(parent1, children1, parent2, children2):
self.assertIs(ob.parent, parent2)
for key in children1.keys():
o1, o2 = children1[key], children2[key]
t1 = type(o1), o1.name, o1.file, o1.module, o1.lineno
t2 = type(o2), o2.name, o2.file, o2.module, o2.lineno
t1 = type(o1), o1.name, o1.file, o1.module, o1.lineno, o1.end_lineno
t2 = type(o2), o2.name, o2.file, o2.module, o2.lineno, o2.end_lineno
self.assertEqual(t1, t2)
if type(o1) is mb.Class:
self.assertEqual(o1.methods, o2.methods)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add an 'end_lineno' attribute to the Class and Function objects that appear in the
tree returned by pyclbr functions. This and the existing 'lineno'
attribute define the extent of class and def statements. Patch by Aviral Srivastava.