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
65a97690
Commit
65a97690
authored
Sep 04, 2011
by
scoder
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #59 from vitek/_final_methods
Final and inline method support
parents
278cc338
7f22634d
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
171 additions
and
12 deletions
+171
-12
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
+9
-4
Cython/Compiler/Options.py
Cython/Compiler/Options.py
+1
-1
Cython/Compiler/ParseTreeTransforms.py
Cython/Compiler/ParseTreeTransforms.py
+4
-3
Cython/Compiler/PyrexTypes.py
Cython/Compiler/PyrexTypes.py
+2
-0
Cython/Compiler/Symtab.py
Cython/Compiler/Symtab.py
+25
-3
Cython/Compiler/TypeSlots.py
Cython/Compiler/TypeSlots.py
+1
-1
tests/errors/final_methods.pyx
tests/errors/final_methods.pyx
+22
-0
tests/run/cmethod_inline_T474.pxd
tests/run/cmethod_inline_T474.pxd
+3
-0
tests/run/cmethod_inline_T474.pyx
tests/run/cmethod_inline_T474.pyx
+29
-0
tests/run/final_method_T586.pyx
tests/run/final_method_T586.pyx
+55
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
65a97690
...
...
@@ -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 @
65a97690
...
...
@@ -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 @
65a97690
...
...
@@ -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
)
...
...
@@ -2243,8 +2244,12 @@ class DefNode(FuncDefNode):
#print "DefNode.declare_pyfunction:", self.name, "in", env ###
name
=
self
.
name
entry
=
env
.
lookup_here
(
name
)
if
entry
and
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
)
if
entry
:
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
)
self
.
entry
=
entry
prefix
=
env
.
next_id
(
env
.
scope_prefix
)
...
...
@@ -3503,7 +3508,7 @@ class CClassDefNode(ClassDefNode):
error
(
self
.
pos
,
"Base class '%s' of type '%s' is incomplete"
%
(
self
.
base_class_name
,
self
.
class_name
))
elif
base_class_entry
.
type
.
scope
and
base_class_entry
.
type
.
scope
.
directives
and
\
base_class_entry
.
type
.
scope
.
directives
[
'final'
]
:
base_class_entry
.
type
.
is_final_type
:
error
(
self
.
pos
,
"Base class '%s' of type '%s' is final"
%
(
self
.
base_class_name
,
self
.
class_name
))
elif
base_class_entry
.
type
.
is_builtin_type
and
\
...
...
Cython/Compiler/Options.py
View file @
65a97690
...
...
@@ -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/ParseTreeTransforms.py
View file @
65a97690
...
...
@@ -567,7 +567,8 @@ class PxdPostParse(CythonTransform, SkipDeclarations):
err
=
None
# allow these slots
if
isinstance
(
node
,
Nodes
.
CFuncDefNode
):
if
u'inline'
in
node
.
modifiers
and
self
.
scope_type
==
'pxd'
:
if
(
u'inline'
in
node
.
modifiers
and
self
.
scope_type
in
(
'pxd'
,
'cclass'
)):
node
.
inline_in_pxd
=
True
if
node
.
visibility
!=
'private'
:
err
=
self
.
ERR_NOGO_WITH_INLINE
%
node
.
visibility
...
...
@@ -1943,9 +1944,9 @@ class CreateClosureClasses(CythonTransform):
objstruct_cname
=
'__pyx_Generator_object'
,
typeobj_cname
=
'__pyx_Generator_type'
,
pos
=
pos
,
defining
=
True
,
implementing
=
True
)
entry
.
type
.
is_final_type
=
True
klass
=
entry
.
type
.
scope
klass
.
is_internal
=
True
klass
.
directives
=
{
'final'
:
True
}
body_type
=
PyrexTypes
.
create_typedef_type
(
'generator_body'
,
PyrexTypes
.
c_void_ptr_type
,
...
...
@@ -2041,11 +2042,11 @@ class CreateClosureClasses(CythonTransform):
entry
=
target_module_scope
.
declare_c_class
(
name
=
as_name
,
pos
=
node
.
pos
,
defining
=
True
,
implementing
=
True
,
base_type
=
base_type
)
entry
.
type
.
is_final_type
=
True
func_scope
.
scope_class
=
entry
class_scope
=
entry
.
type
.
scope
class_scope
.
is_internal
=
True
class_scope
.
directives
=
{
'final'
:
True
}
if
from_closure
:
assert
cscope
.
is_closure_scope
...
...
Cython/Compiler/PyrexTypes.py
View file @
65a97690
...
...
@@ -39,6 +39,7 @@ class PyrexType(BaseType):
#
# is_pyobject boolean Is a Python object type
# is_extension_type boolean Is a Python extension type
# is_final_type boolean Is a final extension type
# is_numeric boolean Is a C numeric type
# is_int boolean Is a C integer type
# is_float boolean Is a C floating point type
...
...
@@ -90,6 +91,7 @@ class PyrexType(BaseType):
is_pyobject
=
0
is_unspecified
=
0
is_extension_type
=
0
is_final_type
=
0
is_builtin_type
=
0
is_numeric
=
0
is_int
=
0
...
...
Cython/Compiler/Symtab.py
View file @
65a97690
...
...
@@ -70,6 +70,8 @@ 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_inline_cmethod boolean Is inlined 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 +141,8 @@ class Entry(object):
is_cmethod = 0
is_builtin_cmethod = False
is_unbound_cmethod = 0
is_final_cmethod = 0
is_inline_cmethod = 0
is_anonymous = 0
is_type = 0
is_cclass = 0
...
...
@@ -157,6 +161,7 @@ class Entry(object):
is_readonly = 0
func_cname = None
func_modifiers = []
final_func_cname = None
doc = None
as_variable = None
xdecref_cleanup = 0
...
...
@@ -829,7 +834,7 @@ class BuiltinScope(Scope):
scope = CClassScope(name, outer_scope=None, visibility='extern')
scope.directives = {}
if name == 'bool':
scope.directives['final']
= True
type.is_final_type
= True
type.set_scope(scope)
self.type_names[name] = 1
entry = self.declare_type(name, type, None, visibility='extern')
...
...
@@ -1245,6 +1250,9 @@ class ModuleScope(Scope):
error(pos, "Type object name differs from previous declaration")
type.typeobj_cname = typeobj_cname
if self.directives.get('
final
'):
entry.type.is_final_type = True
# cdef classes are always exported, but we need to set it to
# distinguish between unused Cython utility code extension classes
entry.used = True
...
...
@@ -1775,7 +1783,7 @@ class CClassScope(ClassScope):
# Otherwise, subtypes may choose to override the
# method, but the optimisation would prevent the
# subtype method from being called.
if
not
self
.
directives
[
'final'
]
:
if
not
self
.
parent_type
.
is_final_type
:
return
None
return
entry
...
...
@@ -1798,7 +1806,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 +1826,12 @@ class CClassScope(ClassScope):
if
defining
:
entry
.
func_cname
=
self
.
mangle
(
Naming
.
func_prefix
,
name
)
entry
.
utility_code
=
utility_code
if
u'inline'
in
modifiers
:
entry
.
is_inline_cmethod
=
True
if
(
self
.
parent_type
.
is_final_type
or
entry
.
is_inline_cmethod
or
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 +1890,12 @@ 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
entry
.
is_inline_cmethod
=
base_entry
.
is_inline_cmethod
if
(
self
.
parent_scope
==
base_scope
.
parent_scope
or
entry
.
is_inline_cmethod
):
entry
.
final_func_cname
=
base_entry
.
final_func_cname
if
is_builtin
:
entry
.
is_builtin_cmethod
=
True
entry
.
as_variable
=
var_entry
...
...
Cython/Compiler/TypeSlots.py
View file @
65a97690
...
...
@@ -343,7 +343,7 @@ class TypeFlagsSlot(SlotDescriptor):
def
slot_code
(
self
,
scope
):
value
=
"Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER"
if
not
scope
.
directives
[
'final'
]
:
if
not
scope
.
parent_type
.
is_final_type
:
value
+=
"|Py_TPFLAGS_BASETYPE"
if
scope
.
needs_gc
():
value
+=
"|Py_TPFLAGS_HAVE_GC"
...
...
tests/errors/final_methods.pyx
0 → 100644
View file @
65a97690
# 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
_ERRORS
=
"""
11:10: Only final type could have final cpdef method
16:9: Overriding final methods is not allowed
"""
tests/run/cmethod_inline_T474.pxd
0 → 100644
View file @
65a97690
cdef
class
TestInlineMethod
(
object
):
cdef
inline
int
cdef
_inline_method
(
self
):
return
0
tests/run/cmethod_inline_T474.pyx
0 → 100644
View file @
65a97690
# mode: run
# ticket: 474
cimport
cython
cdef
class
TestInlineMethod
(
object
):
"""
>>> test = TestInlineMethod()
>>> test.test_cdef_method()
0
"""
@
cython
.
test_assert_path_exists
(
"//AttributeNode[@entry.is_final_cmethod=True]"
)
@
cython
.
test_assert_path_exists
(
"//AttributeNode[@entry.is_inline_cmethod=True]"
)
def
test_cdef_method
(
self
):
return
self
.
cdef
_inline_method
()
cdef
class
Subtyping
(
TestInlineMethod
):
"""
>>> test = Subtyping()
>>> test.test_cdef_subtyping()
0
"""
@
cython
.
test_assert_path_exists
(
"//AttributeNode[@entry.is_final_cmethod=True]"
)
@
cython
.
test_assert_path_exists
(
"//AttributeNode[@entry.is_inline_cmethod=True]"
)
def
test_cdef_subtyping
(
self
):
return
self
.
cdef
_inline_method
()
tests/run/final_method_T586.pyx
0 → 100644
View file @
65a97690
# mode: run
# ticket: 568
cimport
cython
@
cython
.
final
cdef
class
FinalType
(
object
):
"""
>>> obj = FinalType()
>>> obj.test_cdef()
>>> obj.test_cpdef()
"""
@
cython
.
test_assert_path_exists
(
"//CFuncDefNode[@entry.is_final_cmethod=True]"
)
cdef
cdef
_method
(
self
):
pass
@
cython
.
test_assert_path_exists
(
"//CFuncDefNode[@entry.is_final_cmethod=True]"
)
@
cython
.
test_fail_if_path_exists
(
"//CFuncDefNode//OverrideCheckNode"
)
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
BaseTypeWithFinalMethods
(
object
):
"""
>>> obj = BaseTypeWithFinalMethods()
>>> obj.test_cdef()
"""
@
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]"
)
def
test_cdef
(
self
):
self
.
cdef
_method
()
cdef
class
SubType
(
BaseTypeWithFinalMethods
):
"""
>>> obj = SubType()
>>> obj.test_cdef()
"""
@
cython
.
test_assert_path_exists
(
"//AttributeNode[@entry.is_final_cmethod=True]"
)
def
test_cdef
(
self
):
self
.
cdef
_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