From f3ac81a013d90479624c8ffba67c2d64829ccb73 Mon Sep 17 00:00:00 2001 From: Rasmus Wriedt Larsen Date: Tue, 6 Sep 2022 13:54:17 +0200 Subject: [PATCH] Python: Expand tests for special method calls --- .../CallGraph/code/class_special_methods.py | 57 +++++++++++++++---- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/python/ql/test/experimental/library-tests/CallGraph/code/class_special_methods.py b/python/ql/test/experimental/library-tests/CallGraph/code/class_special_methods.py index 7b8df9c4139..454eb5207a1 100644 --- a/python/ql/test/experimental/library-tests/CallGraph/code/class_special_methods.py +++ b/python/ql/test/experimental/library-tests/CallGraph/code/class_special_methods.py @@ -1,29 +1,66 @@ -class B(object): +class Base(object): def __init__(self, arg): - print('B.__init__', arg) + print("Base.__init__", arg) self.arg = arg def __str__(self): - print('B.__str__') - return 'B (arg={})'.format(self.arg) + print("Base.__str__") + return 'Base STR (arg={})'.format(self.arg) def __add__(self, other): - print('B.__add__') - if isinstance(other, B): - return B(self.arg + other.arg) # $ tt=B.__init__ - return B(self.arg + other) # $ tt=B.__init__ + print("Base.__add__") + if isinstance(other, Base): + return Base(self.arg + other.arg) # $ tt=Base.__init__ + return Base(self.arg + other) # $ tt=Base.__init__ -b = B(1) # $ tt=B.__init__ + def __call__(self, val): + print("Base.__call__", val) + + def wat(self): + print("Base.wat") + self(43) # $ MISSING: tt=Base.__call__ tt=Sub.__call__ + + +b = Base(1) # $ tt=Base.__init__ print(str(b)) # this calls `str(b)` inside print(b) +print("\n! calls") +b(42) # $ MISSING: tt=Base.__call__ +b.wat() # $ pt,tt=Base.wat -b2 = B(2) # $ tt=B.__init__ +b.__call__(44) # $ pt,tt=Base.__call__ + +print("\n! b2") +b2 = Base(2) # $ tt=Base.__init__ # __add__ is called b + b2 b + 100 + + +# ======== +print("\n! Sub") + +class Sub(Base): + def __add__(self, other): + print("Sub.__add__") + + def __call__(self, arg): + print("Sub.__call__", arg) + +sub = Sub(10) # $ tt=Base.__init__ +sub + 42 + +sub(55) # $ MISSING: tt=Sub.__call__ +sub.wat() # $ pt,tt=Base.wat + +# not possible to indirectly access addition of subclass +try: + super(Sub, sub) + 143 +except TypeError: + print("TypeError as expected")