mirror of
https://github.com/github/codeql.git
synced 2025-12-24 04:36:35 +01:00
Python: Expand tests for special method calls
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user