Commit 7f22634d authored by Vitja Makarov's avatar Vitja Makarov

Disallow final cpdef methods in non-final extension type

parent 128eb184
...@@ -2244,8 +2244,12 @@ class DefNode(FuncDefNode): ...@@ -2244,8 +2244,12 @@ class DefNode(FuncDefNode):
#print "DefNode.declare_pyfunction:", self.name, "in", env ### #print "DefNode.declare_pyfunction:", self.name, "in", env ###
name = self.name name = self.name
entry = env.lookup_here(name) entry = env.lookup_here(name)
if entry and entry.type.is_cfunction and not entry.is_builtin_cmethod and not self.is_wrapper: if entry:
warning(self.pos, "Overriding cdef method with def method.", 5) if entry.is_final_cmethod and not env.parent_type.is_final_type:
error(self.pos, "Only final type could have final cpdef method")
if (entry.type.is_cfunction and not entry.is_builtin_cmethod
and not self.is_wrapper):
warning(self.pos, "Overriding cdef method with def method.", 5)
entry = env.declare_pyfunction(name, self.pos, allow_redefine=not self.is_wrapper) entry = env.declare_pyfunction(name, self.pos, allow_redefine=not self.is_wrapper)
self.entry = entry self.entry = entry
prefix = env.next_id(env.scope_prefix) prefix = env.next_id(env.scope_prefix)
......
...@@ -16,10 +16,7 @@ cdef class SubType(BaseClass): ...@@ -16,10 +16,7 @@ cdef class SubType(BaseClass):
cdef cdef_method(self): cdef cdef_method(self):
pass pass
cpdef cpdef_method(self):
pass
_ERRORS = """ _ERRORS = """
11:10: Only final type could have final cpdef method
16:9: Overriding final methods is not allowed 16:9: Overriding final methods is not allowed
19:10: Overriding final methods is not allowed
""" """
...@@ -3,21 +3,20 @@ ...@@ -3,21 +3,20 @@
cimport cython cimport cython
cdef class FinalMethods(object): @cython.final
cdef class FinalType(object):
""" """
>>> obj = FinalMethods() >>> obj = FinalType()
>>> obj.test_cdef() >>> obj.test_cdef()
>>> obj.test_cpdef() >>> obj.test_cpdef()
""" """
@cython.test_assert_path_exists("//CFuncDefNode[@entry.is_final_cmethod=True]") @cython.test_assert_path_exists("//CFuncDefNode[@entry.is_final_cmethod=True]")
@cython.final
cdef cdef_method(self): cdef cdef_method(self):
pass pass
@cython.test_assert_path_exists("//CFuncDefNode[@entry.is_final_cmethod=True]") @cython.test_assert_path_exists("//CFuncDefNode[@entry.is_final_cmethod=True]")
@cython.test_fail_if_path_exists("//CFuncDefNode//OverrideCheckNode") @cython.test_fail_if_path_exists("//CFuncDefNode//OverrideCheckNode")
@cython.final
cpdef cpdef_method(self): cpdef cpdef_method(self):
pass pass
...@@ -30,16 +29,27 @@ cdef class FinalMethods(object): ...@@ -30,16 +29,27 @@ cdef class FinalMethods(object):
self.cpdef_method() self.cpdef_method()
cdef class SubType(FinalMethods): cdef class BaseTypeWithFinalMethods(object):
""" """
>>> obj = SubType() >>> obj = BaseTypeWithFinalMethods()
>>> obj.test_cdef() >>> obj.test_cdef()
>>> obj.test_cpdef()
""" """
@cython.test_assert_path_exists("//CFuncDefNode[@entry.is_final_cmethod=True]")
@cython.final
cdef cdef_method(self):
pass
@cython.test_assert_path_exists("//AttributeNode[@entry.is_final_cmethod=True]") @cython.test_assert_path_exists("//AttributeNode[@entry.is_final_cmethod=True]")
def test_cdef(self): def test_cdef(self):
self.cdef_method() self.cdef_method()
cdef class SubType(BaseTypeWithFinalMethods):
"""
>>> obj = SubType()
>>> obj.test_cdef()
"""
@cython.test_assert_path_exists("//AttributeNode[@entry.is_final_cmethod=True]") @cython.test_assert_path_exists("//AttributeNode[@entry.is_final_cmethod=True]")
def test_cpdef(self): def test_cdef(self):
self.cpdef_method() self.cdef_method()
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