Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
1c187e8b
Commit
1c187e8b
authored
Aug 28, 2011
by
Vitja Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Final method support, see ticket #586
parent
278cc338
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
106 additions
and
3 deletions
+106
-3
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+2
-0
Cython/Compiler/ModuleNode.py
Cython/Compiler/ModuleNode.py
+18
-0
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+2
-1
Cython/Compiler/Options.py
Cython/Compiler/Options.py
+1
-1
Cython/Compiler/Symtab.py
Cython/Compiler/Symtab.py
+13
-1
tests/errors/final_methods.pyx
tests/errors/final_methods.pyx
+25
-0
tests/run/final_method_T586.pyx
tests/run/final_method_T586.pyx
+45
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
1c187e8b
...
...
@@ -3844,6 +3844,8 @@ class AttributeNode(ExprNode):
#print "...obj_code =", obj_code ###
if
self
.
entry
and
self
.
entry
.
is_cmethod
:
if
obj
.
type
.
is_extension_type
and
not
self
.
entry
.
is_builtin_cmethod
:
if
self
.
entry
.
final_func_cname
:
return
self
.
entry
.
final_func_cname
return
"((struct %s *)%s%s%s)->%s"
%
(
obj
.
type
.
vtabstruct_cname
,
obj_code
,
self
.
op
,
obj
.
type
.
vtabslot_cname
,
self
.
member
)
...
...
Cython/Compiler/ModuleNode.py
View file @
1c187e8b
...
...
@@ -457,6 +457,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
self
.
generate_typeobj_predeclaration
(
entry
,
code
)
self
.
generate_exttype_vtable_struct
(
entry
,
code
)
self
.
generate_exttype_vtabptr_declaration
(
entry
,
code
)
self
.
generate_exttype_final_methods_declaration
(
entry
,
code
)
def
generate_declarations_for_modules
(
self
,
env
,
modules
,
globalstate
):
typecode
=
globalstate
[
'type_declarations'
]
...
...
@@ -991,6 +992,23 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
type
.
vtabstruct_cname
,
type
.
vtabptr_cname
))
def
generate_exttype_final_methods_declaration
(
self
,
entry
,
code
):
if
not
entry
.
used
:
return
code
.
mark_pos
(
entry
.
pos
)
# Generate final methods prototypes
type
=
entry
.
type
for
method_entry
in
entry
.
type
.
scope
.
cfunc_entries
:
if
not
method_entry
.
is_inherited
and
method_entry
.
final_func_cname
:
declaration
=
method_entry
.
type
.
declaration_code
(
method_entry
.
final_func_cname
)
if
entry
.
func_modifiers
:
modifiers
=
"%s "
%
' '
.
join
(
entry
.
func_modifiers
).
upper
()
else
:
modifiers
=
''
code
.
putln
(
"static %s%s;"
%
(
modifiers
,
declaration
))
def
generate_objstruct_predeclaration
(
self
,
type
,
code
):
if
not
type
.
scope
:
return
...
...
Cython/Compiler/Nodes.py
View file @
1c187e8b
...
...
@@ -1751,7 +1751,8 @@ class CFuncDefNode(FuncDefNode):
self
.
entry
.
as_variable
=
self
.
py_func
.
entry
# Reset scope entry the above cfunction
env
.
entries
[
name
]
=
self
.
entry
if
not
env
.
is_module_scope
or
Options
.
lookup_module_cpdef
:
if
(
not
self
.
entry
.
is_final_cmethod
and
(
not
env
.
is_module_scope
or
Options
.
lookup_module_cpdef
)):
self
.
override
=
OverrideCheckNode
(
self
.
pos
,
py_func
=
self
.
py_func
)
self
.
body
=
StatListNode
(
self
.
pos
,
stats
=
[
self
.
override
,
self
.
body
])
self
.
create_local_scope
(
env
)
...
...
Cython/Compiler/Options.py
View file @
1c187e8b
...
...
@@ -141,7 +141,7 @@ for key, val in directive_defaults.items():
directive_scopes
=
{
# defaults to available everywhere
# 'module', 'function', 'class', 'with statement'
'final'
:
(
'cclass'
,
),
# add 'method' in the future
'final'
:
(
'cclass'
,
'function'
),
'internal'
:
(
'cclass'
,),
'autotestdict'
:
(
'module'
,),
'autotestdict.all'
:
(
'module'
,),
...
...
Cython/Compiler/Symtab.py
View file @
1c187e8b
...
...
@@ -70,6 +70,7 @@ class Entry(object):
# is_cmethod boolean Is a C method of an extension type
# is_builtin_cmethod boolean Is a C method of a builtin type (implies is_cmethod)
# is_unbound_cmethod boolean Is an unbound C method of an extension type
# is_final_cmethod boolean Is non-overridable C method
# is_anonymous boolean Is a anonymous pyfunction entry
# is_type boolean Is a type definition
# is_cclass boolean Is an extension class
...
...
@@ -139,6 +140,7 @@ class Entry(object):
is_cmethod = 0
is_builtin_cmethod = False
is_unbound_cmethod = 0
is_final_cmethod = 0
is_anonymous = 0
is_type = 0
is_cclass = 0
...
...
@@ -157,6 +159,7 @@ class Entry(object):
is_readonly = 0
func_cname = None
func_modifiers = []
final_func_cname = None
doc = None
as_variable = None
xdecref_cleanup = 0
...
...
@@ -1798,7 +1801,9 @@ class CClassScope(ClassScope):
if
defining
and
entry
.
func_cname
:
error
(
pos
,
"'%s' already defined"
%
name
)
#print "CClassScope.declare_cfunction: checking signature" ###
if
type
.
same_c_signature_as
(
entry
.
type
,
as_cmethod
=
1
)
and
type
.
nogil
==
entry
.
type
.
nogil
:
if
entry
.
is_final_cmethod
:
error
(
pos
,
"Overriding final methods is not allowed"
)
elif
type
.
same_c_signature_as
(
entry
.
type
,
as_cmethod
=
1
)
and
type
.
nogil
==
entry
.
type
.
nogil
:
pass
elif
type
.
compatible_signature_with
(
entry
.
type
,
as_cmethod
=
1
)
and
type
.
nogil
==
entry
.
type
.
nogil
:
entry
=
self
.
add_cfunction
(
name
,
type
,
pos
,
cname
or
name
,
visibility
=
'ignore'
,
modifiers
=
modifiers
)
...
...
@@ -1816,6 +1821,9 @@ class CClassScope(ClassScope):
if
defining
:
entry
.
func_cname
=
self
.
mangle
(
Naming
.
func_prefix
,
name
)
entry
.
utility_code
=
utility_code
if
self
.
directives
.
get
(
'final'
):
entry
.
is_final_cmethod
=
True
entry
.
final_func_cname
=
entry
.
func_cname
return
entry
def
add_cfunction
(
self
,
name
,
type
,
pos
,
cname
,
visibility
,
modifiers
):
...
...
@@ -1874,6 +1882,10 @@ class CClassScope(ClassScope):
base_entry
.
pos
,
cname
,
base_entry
.
visibility
,
base_entry
.
func_modifiers
)
entry
.
is_inherited
=
1
if
base_entry
.
is_final_cmethod
:
entry
.
is_final_cmethod
=
True
if
self
.
parent_scope
==
base_scope
.
parent_scope
:
entry
.
final_func_cname
=
base_entry
.
final_func_cname
if
is_builtin
:
entry
.
is_builtin_cmethod
=
True
entry
.
as_variable
=
var_entry
...
...
tests/errors/final_methods.pyx
0 → 100644
View file @
1c187e8b
# mode: error
cimport
cython
cdef
class
BaseClass
:
@
cython
.
final
cdef
cdef
_method
(
self
):
pass
@
cython
.
final
cpdef
cpdef
_method
(
self
):
pass
cdef
class
SubType
(
BaseClass
):
cdef
cdef
_method
(
self
):
pass
cpdef
cpdef
_method
(
self
):
pass
_ERRORS
=
"""
16:9: Overriding final methods is not allowed
19:10: Overriding final methods is not allowed
"""
tests/run/final_method_T586.pyx
0 → 100644
View file @
1c187e8b
# mode: run
# ticket: 568
cimport
cython
cdef
class
FinalMethods
(
object
):
"""
>>> obj = FinalMethods()
>>> 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
(
"//CFuncDefNode[@entry.is_final_cmethod=True]"
)
@
cython
.
test_fail_if_path_exists
(
"//CFuncDefNode//OverrideCheckNode"
)
@
cython
.
final
cpdef
cpdef
_method
(
self
):
pass
@
cython
.
test_assert_path_exists
(
"//AttributeNode[@entry.is_final_cmethod=True]"
)
def
test_cdef
(
self
):
self
.
cdef
_method
()
@
cython
.
test_assert_path_exists
(
"//AttributeNode[@entry.is_final_cmethod=True]"
)
def
test_cpdef
(
self
):
self
.
cpdef
_method
()
cdef
class
SubType
(
FinalMethods
):
"""
>>> obj = SubType()
>>> obj.test_cdef()
>>> obj.test_cpdef()
"""
@
cython
.
test_assert_path_exists
(
"//AttributeNode[@entry.is_final_cmethod=True]"
)
def
test_cdef
(
self
):
self
.
cdef
_method
()
@
cython
.
test_assert_path_exists
(
"//AttributeNode[@entry.is_final_cmethod=True]"
)
def
test_cpdef
(
self
):
self
.
cpdef
_method
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment