diff --git a/Lib/pyclbr.py b/Lib/pyclbr.py index 99a17343fb61fd..247d857651a471 100644 --- a/Lib/pyclbr.py +++ b/Lib/pyclbr.py @@ -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. @@ -202,11 +203,13 @@ def _create_tree(fullmodule, path, fname, source, tree, inpackage): lineno, thisindent = start # Close previous nested classes and defs. while stack and stack[-1][1] >= thisindent: + stack[-1][0].end_lineno = start[0] - 1 del stack[-1] elif token == 'def': lineno, thisindent = start # Close previous nested classes and defs. while stack and stack[-1][1] >= thisindent: + stack[-1][0].end_lineno = start[0] - 1 del stack[-1] tokentype, func_name, start = next(g)[0:3] if tokentype != NAME: @@ -224,6 +227,7 @@ def _create_tree(fullmodule, path, fname, source, tree, inpackage): lineno, thisindent = start # Close previous nested classes and defs. while stack and stack[-1][1] >= thisindent: + stack[-1][0].end_lineno = start[0] - 1 del stack[-1] tokentype, class_name, start = next(g)[0:3] if tokentype != NAME: diff --git a/Lib/test/test_pyclbr.py b/Lib/test/test_pyclbr.py index 4385271cd0f2ba..53a004dc1aef0a 100644 --- a/Lib/test/test_pyclbr.py +++ b/Lib/test/test_pyclbr.py @@ -199,6 +199,8 @@ def compare(parent1, children1, parent2, children2): linkage. We separate comparing string and number attributes from comparing the children of input children. """ + print("Children 1 is: {}".format(children1)) + print("Children 2 is: {}".format(children2)) self.assertEqual(children1.keys(), children2.keys()) for ob in children1.values(): self.assertIs(ob.parent, parent1) @@ -206,8 +208,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) diff --git a/Misc/NEWS.d/next/Library/2020-03-16-03-03-21.bpo-38307.2cmw2i.rst b/Misc/NEWS.d/next/Library/2020-03-16-03-03-21.bpo-38307.2cmw2i.rst new file mode 100644 index 00000000000000..06d476a7326cd2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-03-16-03-03-21.bpo-38307.2cmw2i.rst @@ -0,0 +1,4 @@ +Adds end line no in class' use: generating dependency. +: `end_lineno` is added to denote the scope of a class +along with `lineno` which was already present in the codebase. +Patch by Aviral Srivastava. \ No newline at end of file