Commit 66ca26c4 authored by Stefan Behnel's avatar Stefan Behnel

repair CyFunction behaviour for staticmethods and add tests for classmethods

parent 0bc33758
...@@ -630,7 +630,7 @@ static CYTHON_INLINE PyObject *__Pyx_CyFunction_Call(PyObject *func, PyObject *a ...@@ -630,7 +630,7 @@ static CYTHON_INLINE PyObject *__Pyx_CyFunction_Call(PyObject *func, PyObject *a
static PyObject *__Pyx_CyFunction_CallAsMethod(PyObject *func, PyObject *args, PyObject *kw) { static PyObject *__Pyx_CyFunction_CallAsMethod(PyObject *func, PyObject *args, PyObject *kw) {
PyObject *result; PyObject *result;
__pyx_CyFunctionObject *cyfunc = (__pyx_CyFunctionObject *) func; __pyx_CyFunctionObject *cyfunc = (__pyx_CyFunctionObject *) func;
if (cyfunc->flags & __Pyx_CYFUNCTION_CCLASS) { if ((cyfunc->flags & __Pyx_CYFUNCTION_CCLASS) && !(cyfunc->flags & __Pyx_CYFUNCTION_STATICMETHOD)) {
Py_ssize_t argc; Py_ssize_t argc;
PyObject *new_args; PyObject *new_args;
PyObject *self; PyObject *self;
......
__doc__ = u""" __doc__ = u"""
>>> class1.view()
class1
>>> class1.plus(1) >>> class1.plus(1)
6 6
>>> class1.view()
class1
>>> class1().view()
class1
>>> class1.bview()
class1
>>> class1().bview()
class1
>>> class2.view() >>> class2.view()
class2 class2
>>> class2.plus(1) >>> class2.plus(1)
7 7
>>> class3.view() >>> class3.view()
class3 class3
>>> class3.bview()
class3
>>> class3().bview()
class3
>>> class3.plus(1) >>> class3.plus(1)
8 8
>>> class4.view() >>> class4.view()
class4 class4
>>> class5.view() >>> class5.view()
class5 class5
""" """
cimport cython
def f_plus(cls, a): def f_plus(cls, a):
return cls.a + a return cls.a + a
class class1: class class1:
a = 5 a = 5
plus = classmethod(f_plus) plus = classmethod(f_plus)
...@@ -27,6 +43,12 @@ class class1: ...@@ -27,6 +43,12 @@ class class1:
print cls.__name__ print cls.__name__
view = classmethod(view) view = classmethod(view)
@classmethod
@cython.binding(True)
def bview(cls):
print cls.__name__
class class2(object): class class2(object):
a = 6 a = 6
plus = classmethod(f_plus) plus = classmethod(f_plus)
...@@ -34,6 +56,7 @@ class class2(object): ...@@ -34,6 +56,7 @@ class class2(object):
print cls.__name__ print cls.__name__
view = classmethod(view) view = classmethod(view)
cdef class class3: cdef class class3:
a = 7 a = 7
plus = classmethod(f_plus) plus = classmethod(f_plus)
...@@ -41,10 +64,17 @@ cdef class class3: ...@@ -41,10 +64,17 @@ cdef class class3:
print cls.__name__ print cls.__name__
view = classmethod(view) view = classmethod(view)
@classmethod
@cython.binding(True)
def bview(cls):
print cls.__name__
class class4: class class4:
@classmethod @classmethod
def view(cls): def view(cls):
print cls.__name__ print cls.__name__
class class5(class4): class class5(class4):
pass pass
...@@ -7,8 +7,16 @@ __doc__ = u""" ...@@ -7,8 +7,16 @@ __doc__ = u"""
2 2
>>> class4.plus1(1) >>> class4.plus1(1)
2 2
>>> class4().plus1(1)
2
>>> class4.bplus1(1)
2
>>> class4().bplus1(1)
2
""" """
cimport cython
def f_plus(a): def f_plus(a):
return a + 1 return a + 1
...@@ -26,6 +34,12 @@ class class4: ...@@ -26,6 +34,12 @@ class class4:
def plus1(a): def plus1(a):
return a + 1 return a + 1
@staticmethod
@cython.binding(True)
def bplus1(a):
return a + 1
def nested_class(): def nested_class():
""" """
>>> cls = nested_class() >>> cls = nested_class()
...@@ -44,6 +58,7 @@ def nested_class(): ...@@ -44,6 +58,7 @@ def nested_class():
return a + 1 return a + 1
return class5 return class5
cdef class BaseClass(object): cdef class BaseClass(object):
""" """
Test cdef static methods with super() and Python subclasses Test cdef static methods with super() and Python subclasses
...@@ -64,9 +79,11 @@ cdef class BaseClass(object): ...@@ -64,9 +79,11 @@ cdef class BaseClass(object):
print arg1 print arg1
@staticmethod @staticmethod
@cython.binding(True)
def mystaticmethod2(a, b, c): def mystaticmethod2(a, b, c):
print a, b, c print a, b, c
cdef class SubClass(BaseClass): cdef class SubClass(BaseClass):
""" """
>>> obj = SubClass() >>> obj = SubClass()
...@@ -83,6 +100,7 @@ cdef class SubClass(BaseClass): ...@@ -83,6 +100,7 @@ cdef class SubClass(BaseClass):
print arg1 print arg1
super().mystaticmethod(self, arg1 + 1) super().mystaticmethod(self, arg1 + 1)
class SubSubClass(SubClass): class SubSubClass(SubClass):
""" """
>>> obj = SubSubClass() >>> obj = SubSubClass()
...@@ -121,6 +139,7 @@ cdef class ArgsKwargs(object): ...@@ -121,6 +139,7 @@ cdef class ArgsKwargs(object):
""" """
return args + tuple(sorted(kwargs.items())) return args + tuple(sorted(kwargs.items()))
class StaticmethodSubclass(staticmethod): class StaticmethodSubclass(staticmethod):
""" """
>>> s = StaticmethodSubclass(None) >>> s = StaticmethodSubclass(None)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment