# HG changeset patch # User Paul Boddie # Date 1473370463 -7200 # Node ID 95ebcba817be652de60bc1d23b6f4f10279bd49d # Parent 95831cd5b78689ef96ebcf070f34f8d4f69da2e0 Permit an implicit self in method parameters, adding method tests. diff -r 95831cd5b786 -r 95ebcba817be inspector.py --- a/inspector.py Thu Sep 08 23:06:03 2016 +0200 +++ b/inspector.py Thu Sep 08 23:34:23 2016 +0200 @@ -554,9 +554,18 @@ # Initialise argument and local records. function_name = self.get_object_path(name) + argnames = get_argnames(n.argnames) - argnames = self.importer.function_parameters[function_name] = \ - self.function_parameters[function_name] = get_argnames(n.argnames) + # Insert "self" into methods where not explicitly declared. + + if self.in_class and (not argnames or argnames[0] != "self"): + argnames.insert(0, "self") + + self.importer.function_parameters[function_name] = \ + self.function_parameters[function_name] = argnames + + # Define all arguments/parameters in the local namespace. + locals = self.function_locals[function_name] = {} for argname in argnames: diff -r 95831cd5b786 -r 95ebcba817be tests/methods.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/methods.py Thu Sep 08 23:34:23 2016 +0200 @@ -0,0 +1,26 @@ +class C: + def __init__(self, x, y, z): + self.x = x + self.y = y + self.z = z + + def c(self): + return self.x + +class D(C): + def d(self): + return self.y + +class E(D): + def c(self): + return self.z + +c = C(1, 2, 3) +d = D(1, 2, 3) +e = E(1, 2, 3) + +result1 = c.c() # 1 +result2 = d.c() # 1 +result3 = e.c() # 3 +result4 = d.d() # 2 +result5 = e.d() # 2 diff -r 95831cd5b786 -r 95ebcba817be tests/methods_selfless.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/methods_selfless.py Thu Sep 08 23:34:23 2016 +0200 @@ -0,0 +1,26 @@ +class C: + def __init__(x, y, z): # no explicit self + self.x = x + self.y = y + self.z = z + + def c(): + return self.x + +class D(C): + def d(): + return self.y + +class E(D): + def c(): + return self.z + +c = C(1, 2, 3) +d = D(1, 2, 3) +e = E(1, 2, 3) + +result1 = c.c() # 1 +result2 = d.c() # 1 +result3 = e.c() # 3 +result4 = d.d() # 2 +result5 = e.d() # 2