Commit 9b57b1c5 authored by Stefan Behnel's avatar Stefan Behnel

support for 'final' cdef types using a directive decorator

parent 4fabd474
...@@ -81,6 +81,7 @@ directive_defaults = { ...@@ -81,6 +81,7 @@ directive_defaults = {
# Override types possibilities above, if needed # Override types possibilities above, if needed
directive_types = { directive_types = {
'final' : bool, # final cdef classes and methods
'infer_types' : bool, # values can be True/None/False 'infer_types' : bool, # values can be True/None/False
} }
...@@ -90,6 +91,7 @@ for key, val in directive_defaults.items(): ...@@ -90,6 +91,7 @@ for key, val in directive_defaults.items():
directive_scopes = { # defaults to available everywhere directive_scopes = { # defaults to available everywhere
# 'module', 'function', 'class', 'with statement' # 'module', 'function', 'class', 'with statement'
'final' : ('cclass',), # add 'method' in the future
'autotestdict' : ('module',), 'autotestdict' : ('module',),
'test_assert_path_exists' : ('function',), 'test_assert_path_exists' : ('function',),
'test_fail_if_path_exists' : ('function',), 'test_fail_if_path_exists' : ('function',),
......
...@@ -323,7 +323,9 @@ class TypeFlagsSlot(SlotDescriptor): ...@@ -323,7 +323,9 @@ class TypeFlagsSlot(SlotDescriptor):
# Descriptor for the type flags slot. # Descriptor for the type flags slot.
def slot_code(self, scope): def slot_code(self, scope):
value = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_NEWBUFFER" value = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER"
if not scope.directives.get('final', False):
value += "|Py_TPFLAGS_BASETYPE"
if scope.needs_gc(): if scope.needs_gc():
value += "|Py_TPFLAGS_HAVE_GC" value += "|Py_TPFLAGS_HAVE_GC"
return value return value
......
cimport cython
@cython.final
cdef class FinalClass:
"""
>>> f = FinalClass()
>>> test_final_class(f)
Type tested
>>> try:
... class SubType(FinalClass): pass
... except TypeError:
... print 'PASSED!'
PASSED!
"""
cdef class NonFinalClass:
"""
>>> class SubType(NonFinalClass): pass
>>> s = SubType()
"""
@cython.final
cdef class FinalSubClass(NonFinalClass):
"""
>>> f = FinalSubClass()
>>> test_non_final_class(f)
Type tested
>>> try:
... class SubType(FinalSubClass): pass
... except TypeError:
... print 'PASSED!'
PASSED!
"""
def test_final_class(FinalClass c):
print u"Type tested"
def test_non_final_class(NonFinalClass c):
print u"Type tested"
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