summaryrefslogtreecommitdiff
path: root/tests/run/cyfunction_defaults.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'tests/run/cyfunction_defaults.pyx')
-rw-r--r--tests/run/cyfunction_defaults.pyx104
1 files changed, 101 insertions, 3 deletions
diff --git a/tests/run/cyfunction_defaults.pyx b/tests/run/cyfunction_defaults.pyx
index 2fe592a23..e57538952 100644
--- a/tests/run/cyfunction_defaults.pyx
+++ b/tests/run/cyfunction_defaults.pyx
@@ -86,10 +86,49 @@ def test_defaults_nonliteral_func_call(f):
return a
return func
+def assign_defaults_and_check_warnings(func, value=None, delete=False):
+ import warnings
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter("always")
+ if delete:
+ del func.__defaults__
+ else:
+ func.__defaults__ = value
+ assert len(w) == 1, len(w)
+ assert issubclass(w[0].category, RuntimeWarning), w[0].category
+ assert "changes to cyfunction.__defaults__" in str(w[0].message), str(w[0].message)
+
+def test_assign_defaults():
+ """
+ >>> f = test_assign_defaults()
+ >>> f.__defaults__
+ (5, 10)
+ >>> assign_defaults_and_check_warnings(f, value=())
+ >>> f.__defaults__
+ ()
+ >>> assign_defaults_and_check_warnings(f, delete=True)
+ >>> f.__defaults__
+ >>> f.__defaults__ = "Not a tuple"
+ Traceback (most recent call last):
+ TypeError: __defaults__ must be set to a tuple object
+ """
+ def func(a=5, b=10):
+ return a, b
+ return func
+
def cy_kwonly_default_args(a, x=1, *, b=2):
l = m = 1
+def assign_kwdefaults_and_check_warnings(func, value):
+ import warnings
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter("always")
+ func.__kwdefaults__ = value
+ assert len(w) == 1, len(w)
+ assert issubclass(w[0].category, RuntimeWarning), w[0].category
+ assert "changes to cyfunction.__kwdefaults__" in str(w[0].message), str(w[0].message)
+
def test_kwdefaults(value):
"""
>>> cy_kwonly_default_args.__defaults__
@@ -111,12 +150,12 @@ def test_kwdefaults(value):
>>> f.__kwdefaults__ = ()
Traceback (most recent call last):
TypeError: __kwdefaults__ must be set to a dict object
- >>> f.__kwdefaults__ = None
+ >>> assign_kwdefaults_and_check_warnings(f, None)
>>> f.__kwdefaults__
- >>> f.__kwdefaults__ = {}
+ >>> assign_kwdefaults_and_check_warnings(f, {})
>>> f.__kwdefaults__
{}
- >>> f.__kwdefaults__ = {'a': 2}
+ >>> assign_kwdefaults_and_check_warnings(f, {'a': 2})
>>> f.__kwdefaults__
{'a': 2}
"""
@@ -251,3 +290,62 @@ def test_func_default_scope_local():
return arg
print i # genexprs don't leak
return func
+
+cdef class C:
+ def f1(self, a, b=1, c=[]):
+ pass
+ def f2(self, a, b=1,/, c=[1]):
+ pass
+ def f3(self, a, /, b=1, *, c=[1]):
+ pass
+ cpdef f4(self, a, char*c=NULL):
+ pass
+ cpdef f5(self, a, str s = "123"):
+ pass
+ cpdef f6(self, a, int s = 4):
+ pass
+ cpdef f7(self, a, dict s = {'a':22}):
+ pass
+ cpdef f8(self, a, list s = [15]):
+ pass
+ cpdef f9(self, a, int[:] s = None):
+ pass
+ def f10(self, a, /, b=1, *, int[:] c=None):
+ pass
+
+
+def check_defaults_on_methods_for_introspection():
+ """
+ >>> C.f1.__defaults__
+ (1, [])
+ >>> C.f1.__kwdefaults__
+ >>> C.f2.__defaults__
+ (1, [1])
+ >>> C.f2.__kwdefaults__
+ >>> C.f3.__defaults__
+ (1,)
+ >>> C.f3.__kwdefaults__
+ {'c': [1]}
+ >>> C.f4.__defaults__
+ >>> C.f4.__kwdefaults__
+ >>> C.f5.__defaults__
+ ('123',)
+ >>> C.f5.__kwdefaults__
+ >>> C.f6.__defaults__
+ (4,)
+ >>> C.f6.__kwdefaults__
+ >>> C.f7.__defaults__
+ ({'a': 22},)
+ >>> C.f7.__kwdefaults__
+ >>> C.f8.__defaults__
+ ([15],)
+ >>> C.f8.__kwdefaults__
+ >>> C.f9.__defaults__
+ (None,)
+ >>> C.f9.__kwdefaults__
+ >>> C.f10.__defaults__
+ (1,)
+ >>> C.f10.__kwdefaults__
+ {'c': None}
+ """
+ pass