Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 22 additions & 1 deletion integration/tests/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,25 @@ def __init__(self):
a = A()
print("a.a", a.a)
a.a += 1
assert a.a == 2, "Failed to store attribute after inplace addition"
assert a.a == 2, "Failed to store attribute after inplace addition"

def attribute_builtins():
class B:
pass

b = B()
setattr(b, "x", 5)
assert getattr(b, "x") == 5, "setattr/getattr round-trip failed"
assert hasattr(b, "x"), "hasattr should find a set attribute"
assert not hasattr(b, "missing"), "hasattr should be False for a missing attribute"
assert getattr(b, "missing", 42) == 42, "getattr should return the default for a missing attribute"

for builtin, arg_count in [(hasattr, 2), (setattr, 3)]:
try:
builtin(b)
except TypeError:
assert True
else:
assert False, "Expected attribute builtin to raise TypeError on wrong arity"

attribute_builtins()
43 changes: 42 additions & 1 deletion integration/tests/bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,45 @@ def bytes_translate():
result = bytearray(b'read this short text').translate(None, b'aeiou')
assert result == bytearray(b'rd ths shrt txt')

bytes_translate()
bytes_translate()

def bytearray_find():
a = bytearray(b'hello')
assert a.find(ord('l')) == 2, "bytearray.find should return the first matching index"
assert a.find(ord('l'), 3) == 3, "bytearray.find should honour the start argument"

try:
a.find(b'l')
except TypeError:
assert True
else:
assert False, "Expected bytearray.find to raise TypeError when the pattern is not an int"

try:
a.find()
except TypeError:
assert True
else:
assert False, "Expected bytearray.find to raise TypeError when called with no arguments"

bytearray_find()

def bytes_decode():
assert b'hello'.decode() == 'hello', "bytes.decode() should default to utf-8"
assert b'hello'.decode('utf-8') == 'hello', "bytes.decode('utf-8') failed"

try:
b'hello'.decode(1)
except TypeError:
assert True
else:
assert False, "Expected bytes.decode to raise TypeError when encoding is not a string"

try:
b'hello'.decode('utf-8', 'strict', 'extra')
except TypeError:
assert True
else:
assert False, "Expected bytes.decode to raise TypeError when given too many arguments"

bytes_decode()
57 changes: 57 additions & 0 deletions integration/tests/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ class C:
c = C()
assert c.a() == foo()

def staticmethod_arity():
try:
staticmethod()
except TypeError:
assert True
else:
assert False, "Expected staticmethod() with no arguments to raise TypeError"

staticmethod_arity()

class A:
def __init__(self, a):
self._a = a
Expand All @@ -52,6 +62,16 @@ def a(self):
assert A(10).a == 20
assert A.new(10).a == 20

def classmethod_arity():
try:
classmethod()
except TypeError:
assert True
else:
assert False, "Expected classmethod() with no arguments to raise TypeError"

classmethod_arity()

class D:
def test(self):
return __class__ == D
Expand All @@ -78,3 +98,40 @@ def value(self):
assert False

class_closure()

def property_accessors():
class C:
@property
def x(self):
return self._x

@x.setter
def x(self, value):
self._x = value

c = C()
c.x = 42
assert c.x == 42, "property getter/setter round-trip failed"

try:
C.x.getter()
except TypeError:
assert True
else:
assert False, "Expected property.getter() with no arguments to raise TypeError"

property_accessors()

def type_three_arg():
Foo = type("Foo", (), {})
assert Foo.__name__ == "Foo", "type() should set the class name"
assert isinstance(Foo(), Foo), "type()-created class should be instantiable"

try:
type("Bad", "notatuple", {})
except TypeError:
assert True
else:
assert False, "Expected type() with non-tuple bases to raise TypeError"

type_three_arg()
44 changes: 44 additions & 0 deletions integration/tests/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
assert a.get("3") == None
assert a.get("3", 3) == 3

def dict_get_arity():
try:
a.get()
except TypeError:
assert True
else:
assert False, "Expected dict.get to raise TypeError when called with no arguments"

dict_get_arity()

assert a["1"] == 1
a["10"] = 10
assert a["10"] == 10
Expand All @@ -23,6 +33,13 @@ def dict_from_keys():
a = dict.fromkeys([1, 2, 3], "a")
assert a == {1: "a", 2: "a", 3: "a"}

try:
dict.fromkeys()
except TypeError:
assert True
else:
assert False, "Expected dict.fromkeys to raise TypeError when called with no arguments"

dict_from_keys()

def dict_from_map():
Expand Down Expand Up @@ -68,3 +85,30 @@ def __repr__(self):
assert raised is not None, "dict.pop on missing key with failing __repr__ must not abort"

dict_pop_missing_key_with_failing_repr()

def dict_pop_arity():
d = {"a": 1}
assert d.pop("a", 99) == 1, "dict.pop should return the value for an existing key"
assert d.pop("a", 99) == 99, "dict.pop should return the default for a missing key"
try:
d.pop()
except TypeError:
assert True
else:
assert False, "Expected dict.pop to raise TypeError when called with no arguments"

dict_pop_arity()

def dict_update_method():
d = {"a": 1}
d.update({"b": 2})
assert d["a"] == 1, "dict.update should keep existing keys"
assert d["b"] == 2, "dict.update should add new keys"
try:
d.update()
except TypeError:
assert True
else:
assert False, "Expected dict.update to raise TypeError when called with no arguments"

dict_update_method()
16 changes: 16 additions & 0 deletions integration/tests/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@ def invalid_eval():
else:
assert False, "Wrong exception"
invalid_eval()

def exec_arity():
try:
exec()
except TypeError:
assert True
else:
assert False, "Expected exec() with no arguments to raise TypeError"

try:
exec(1, 2, 3, 4)
except TypeError:
assert True
else:
assert False, "Expected exec() with too many arguments to raise TypeError"
exec_arity()
19 changes: 18 additions & 1 deletion integration/tests/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,21 @@ def bar():
return 42

assert bar() == 21
assert bar == new_bar
assert bar == new_bar

def misc_builtin_arity():
it = iter([1, 2, 3])
assert next(it) == 1, "iter/next should yield the first element"
assert hash(1) == hash(1), "hash should be stable"
assert callable(len), "len should be callable"
assert not callable(5), "an int should not be callable"

for fn in [iter, hash, next, callable]:
try:
fn()
except TypeError:
assert True
else:
assert False, "Expected a 1-argument builtin to raise TypeError with no arguments"

misc_builtin_arity()
26 changes: 26 additions & 0 deletions integration/tests/imp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import _imp

def imp_query_tests():
assert _imp.is_builtin("sys") == True, "sys should be reported as a builtin module"
assert _imp.is_builtin("definitely_not_a_module") == False, "unknown module is not builtin"
assert _imp.is_frozen("definitely_not_a_module") == False, "unknown module is not frozen"

imp_query_tests()

def imp_arity_tests():
for fn in [_imp.is_builtin, _imp.is_frozen, _imp.create_builtin, _imp.exec_builtin]:
try:
fn()
except TypeError:
assert True
else:
assert False, "Expected _imp function to raise TypeError with no arguments"

try:
_imp.is_frozen(123)
except TypeError:
assert True
else:
assert False, "Expected _imp.is_frozen with a non-string name to raise TypeError"

imp_arity_tests()
30 changes: 30 additions & 0 deletions integration/tests/inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,33 @@ def bar(self):
assert Base.mro() == [Base, object]
assert Derived.mro() == [Derived, Base, Base1, object]
assert Derived.__bases__ == (Base, Base1)

def predicate_builtin_arity():
assert all([True, True]) == True, "all of all-true should be True"
assert all([True, False]) == False, "all with a falsey element should be False"
assert any([False, True]) == True, "any with a truthy element should be True"
assert any([False, False]) == False, "any of all-false should be False"

try:
isinstance(1)
except TypeError:
assert True
else:
assert False, "Expected isinstance with one argument to raise TypeError"

try:
issubclass(Derived)
except TypeError:
assert True
else:
assert False, "Expected issubclass with one argument to raise TypeError"

for fn in [all, any]:
try:
fn([], [])
except TypeError:
assert True
else:
assert False, "Expected a 1-argument builtin to raise TypeError with too many arguments"

predicate_builtin_arity()
29 changes: 29 additions & 0 deletions integration/tests/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,26 @@ def to_bytes_test():
finally:
assert raise_error, "should raise an error when converting an int that is too large for the given bytes"

try:
(5).to_bytes(2)
except TypeError:
assert True
else:
assert False, "Expected to_bytes with too few arguments to raise TypeError"

to_bytes_test()

def from_bytes_test():
assert int.from_bytes(b"10", "little") == 12337
assert int.from_bytes(b"10", "big") == 12592

try:
int.from_bytes(b"10")
except TypeError:
assert True
else:
assert False, "Expected from_bytes with too few arguments to raise TypeError"

from_bytes_test()

def big_int_addition():
Expand All @@ -25,3 +39,18 @@ def big_int_addition():
assert c == 80235802358023580235

big_int_addition()

def int_constructor():
assert int() == 0, "int() should be 0"
assert int(3.7) == 3, "int(3.7) should truncate to 3"
assert int("10") == 10, "int('10') should be 10"
assert int("ff", 16) == 255, "int('ff', 16) should be 255"

try:
int(1, 2, 3)
except TypeError:
assert True
else:
assert False, "Expected int() with too many arguments to raise TypeError"

int_constructor()
28 changes: 28 additions & 0 deletions integration/tests/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,31 @@ def list_recursive_repr():
assert repr(a) == "[[...]]", "recursive list repr should be idempotent"

list_recursive_repr()

def len_arity():
assert len([1, 2, 3]) == 3, "len of a list failed"
try:
len()
except TypeError:
assert True
else:
assert False, "Expected len() with no arguments to raise TypeError"
try:
len([], [])
except TypeError:
assert True
else:
assert False, "Expected len() with too many arguments to raise TypeError"

len_arity()

def list_class_getitem():
assert str(list[int]) == "list[int]", "list[int] generic alias failed"
try:
list.__class_getitem__()
except TypeError:
assert True
else:
assert False, "Expected list.__class_getitem__ to raise TypeError with no arguments"

list_class_getitem()
Loading
Loading