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
Boxiang Sun
cython
Commits
e0d366d9
Commit
e0d366d9
authored
Dec 30, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Plain Diff
merged reverted patches from cython-devel
parents
a67d1a16
b30d2f40
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
87 additions
and
44 deletions
+87
-44
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+7
-31
Cython/Compiler/ParseTreeTransforms.py
Cython/Compiler/ParseTreeTransforms.py
+29
-13
Cython/Compiler/Tests/TestDecorators.py
Cython/Compiler/Tests/TestDecorators.py
+25
-0
tests/bugs.txt
tests/bugs.txt
+1
-0
tests/run/decorators_T593.pyx
tests/run/decorators_T593.pyx
+25
-0
No files found.
Cython/Compiler/Nodes.py
View file @
e0d366d9
...
@@ -1975,7 +1975,6 @@ class DefNode(FuncDefNode):
...
@@ -1975,7 +1975,6 @@ class DefNode(FuncDefNode):
# when the def statement is inside a Python class definition.
# when the def statement is inside a Python class definition.
#
#
# assmt AssignmentNode Function construction/assignment
# assmt AssignmentNode Function construction/assignment
# py_cfunc_node PyCFunctionNode/InnerFunctionNode The PyCFunction to create and assign
child_attrs
=
[
"args"
,
"star_arg"
,
"starstar_arg"
,
"body"
,
"decorators"
]
child_attrs
=
[
"args"
,
"star_arg"
,
"starstar_arg"
,
"body"
,
"decorators"
]
...
@@ -1990,7 +1989,6 @@ class DefNode(FuncDefNode):
...
@@ -1990,7 +1989,6 @@ class DefNode(FuncDefNode):
entry
=
None
entry
=
None
acquire_gil
=
0
acquire_gil
=
0
self_in_stararg
=
0
self_in_stararg
=
0
py_cfunc_node
=
None
def
__init__
(
self
,
pos
,
**
kwds
):
def
__init__
(
self
,
pos
,
**
kwds
):
FuncDefNode
.
__init__
(
self
,
pos
,
**
kwds
)
FuncDefNode
.
__init__
(
self
,
pos
,
**
kwds
)
...
@@ -2342,23 +2340,15 @@ class DefNode(FuncDefNode):
...
@@ -2342,23 +2340,15 @@ class DefNode(FuncDefNode):
genv
=
genv
.
outer_scope
genv
=
genv
.
outer_scope
if
genv
.
is_closure_scope
:
if
genv
.
is_closure_scope
:
self
.
py_cfunc_node
=
ExprNodes
.
InnerFunctionNode
(
rhs
=
ExprNodes
.
InnerFunctionNode
(
self
.
pos
,
pymethdef_cname
=
self
.
entry
.
pymethdef_cname
)
self
.
pos
,
pymethdef_cname
=
self
.
entry
.
pymethdef_cname
)
else
:
else
:
self
.
py_cfunc_node
=
ExprNodes
.
PyCFunctionNode
(
rhs
=
ExprNodes
.
PyCFunctionNode
(
self
.
pos
,
pymethdef_cname
=
self
.
entry
.
pymethdef_cname
,
binding
=
env
.
directives
[
'binding'
])
self
.
pos
,
pymethdef_cname
=
self
.
entry
.
pymethdef_cname
,
binding
=
env
.
directives
[
'binding'
])
if
env
.
is_py_class_scope
:
if
env
.
is_py_class_scope
:
if
not
self
.
is_staticmethod
and
not
self
.
is_classmethod
:
if
not
self
.
is_staticmethod
and
not
self
.
is_classmethod
:
self
.
py_cfunc_node
.
binding
=
True
rhs
.
binding
=
True
rhs
=
self
.
py_cfunc_node
if
self
.
decorators
:
for
decorator
in
self
.
decorators
[::
-
1
]:
rhs
=
ExprNodes
.
SimpleCallNode
(
decorator
.
pos
,
function
=
decorator
.
decorator
,
args
=
[
rhs
])
self
.
assmt
=
SingleAssignmentNode
(
self
.
pos
,
self
.
assmt
=
SingleAssignmentNode
(
self
.
pos
,
lhs
=
ExprNodes
.
NameNode
(
self
.
pos
,
name
=
self
.
name
),
lhs
=
ExprNodes
.
NameNode
(
self
.
pos
,
name
=
self
.
name
),
...
@@ -3092,9 +3082,8 @@ class PyClassDefNode(ClassDefNode):
...
@@ -3092,9 +3082,8 @@ class PyClassDefNode(ClassDefNode):
# classobj ClassNode Class object
# classobj ClassNode Class object
# target NameNode Variable to assign class object to
# target NameNode Variable to assign class object to
child_attrs
=
[
"body"
,
"dict"
,
"metaclass"
,
"mkw"
,
"bases"
,
"class
_result
"
,
"target"
]
child_attrs
=
[
"body"
,
"dict"
,
"metaclass"
,
"mkw"
,
"bases"
,
"class
obj
"
,
"target"
]
decorators
=
None
decorators
=
None
class_result
=
None
py3_style_class
=
False
# Python3 style class (bases+kwargs)
py3_style_class
=
False
# Python3 style class (bases+kwargs)
def
__init__
(
self
,
pos
,
name
,
bases
,
doc
,
body
,
decorators
=
None
,
def
__init__
(
self
,
pos
,
name
,
bases
,
doc
,
body
,
decorators
=
None
,
...
@@ -3197,16 +3186,6 @@ class PyClassDefNode(ClassDefNode):
...
@@ -3197,16 +3186,6 @@ class PyClassDefNode(ClassDefNode):
return
cenv
return
cenv
def
analyse_declarations
(
self
,
env
):
def
analyse_declarations
(
self
,
env
):
class_result
=
self
.
classobj
if
self
.
decorators
:
from
ExprNodes
import
SimpleCallNode
for
decorator
in
self
.
decorators
[::
-
1
]:
class_result
=
SimpleCallNode
(
decorator
.
pos
,
function
=
decorator
.
decorator
,
args
=
[
class_result
])
self
.
class_result
=
class_result
self
.
class_result
.
analyse_declarations
(
env
)
self
.
target
.
analyse_target_declaration
(
env
)
self
.
target
.
analyse_target_declaration
(
env
)
cenv
=
self
.
create_scope
(
env
)
cenv
=
self
.
create_scope
(
env
)
cenv
.
directives
=
env
.
directives
cenv
.
directives
=
env
.
directives
...
@@ -3219,7 +3198,7 @@ class PyClassDefNode(ClassDefNode):
...
@@ -3219,7 +3198,7 @@ class PyClassDefNode(ClassDefNode):
self
.
metaclass
.
analyse_expressions
(
env
)
self
.
metaclass
.
analyse_expressions
(
env
)
self
.
mkw
.
analyse_expressions
(
env
)
self
.
mkw
.
analyse_expressions
(
env
)
self
.
dict
.
analyse_expressions
(
env
)
self
.
dict
.
analyse_expressions
(
env
)
self
.
class
_result
.
analyse_expressions
(
env
)
self
.
class
obj
.
analyse_expressions
(
env
)
genv
=
env
.
global_scope
()
genv
=
env
.
global_scope
()
cenv
=
self
.
scope
cenv
=
self
.
scope
self
.
body
.
analyse_expressions
(
cenv
)
self
.
body
.
analyse_expressions
(
cenv
)
...
@@ -3239,9 +3218,9 @@ class PyClassDefNode(ClassDefNode):
...
@@ -3239,9 +3218,9 @@ class PyClassDefNode(ClassDefNode):
self
.
dict
.
generate_evaluation_code
(
code
)
self
.
dict
.
generate_evaluation_code
(
code
)
cenv
.
namespace_cname
=
cenv
.
class_obj_cname
=
self
.
dict
.
result
()
cenv
.
namespace_cname
=
cenv
.
class_obj_cname
=
self
.
dict
.
result
()
self
.
body
.
generate_execution_code
(
code
)
self
.
body
.
generate_execution_code
(
code
)
self
.
class
_result
.
generate_evaluation_code
(
code
)
self
.
class
obj
.
generate_evaluation_code
(
code
)
cenv
.
namespace_cname
=
cenv
.
class_obj_cname
=
self
.
classobj
.
result
()
cenv
.
namespace_cname
=
cenv
.
class_obj_cname
=
self
.
classobj
.
result
()
self
.
target
.
generate_assignment_code
(
self
.
class
_result
,
code
)
self
.
target
.
generate_assignment_code
(
self
.
class
obj
,
code
)
self
.
dict
.
generate_disposal_code
(
code
)
self
.
dict
.
generate_disposal_code
(
code
)
self
.
dict
.
free_temps
(
code
)
self
.
dict
.
free_temps
(
code
)
if
self
.
py3_style_class
:
if
self
.
py3_style_class
:
...
@@ -3300,9 +3279,6 @@ class CClassDefNode(ClassDefNode):
...
@@ -3300,9 +3279,6 @@ class CClassDefNode(ClassDefNode):
if
env
.
in_cinclude
and
not
self
.
objstruct_name
:
if
env
.
in_cinclude
and
not
self
.
objstruct_name
:
error
(
self
.
pos
,
"Object struct name specification required for "
error
(
self
.
pos
,
"Object struct name specification required for "
"C class defined in 'extern from' block"
)
"C class defined in 'extern from' block"
)
if
self
.
decorators
:
error
(
self
.
pos
,
"Decorators not allowed on cdef classes (used on type '%s')"
%
self
.
class_name
)
self
.
base_type
=
None
self
.
base_type
=
None
# Now that module imports are cached, we need to
# Now that module imports are cached, we need to
# import the modules for extern classes.
# import the modules for extern classes.
...
...
Cython/Compiler/ParseTreeTransforms.py
View file @
e0d366d9
...
@@ -945,23 +945,39 @@ class WithTransform(CythonTransform, SkipDeclarations):
...
@@ -945,23 +945,39 @@ class WithTransform(CythonTransform, SkipDeclarations):
return
node
return
node
class
DecoratorTransform
(
ScopeTrackingTransform
,
SkipDeclarations
):
class
DecoratorTransform
(
CythonTransform
,
SkipDeclarations
):
"""Originally, this was the only place where decorators were
transformed into the corresponding calling code. Now, this is
done directly in DefNode and PyClassDefNode to avoid reassignments
to the function/class name - except for cdef class methods. For
those, the reassignment is required as methods are originally
defined in the PyMethodDef struct.
"""
def
visit_DefNode
(
self
,
func_node
):
def
visit_DefNode
(
self
,
func_node
):
scope_type
=
self
.
scope_type
self
.
visitchildren
(
func_node
)
func_node
=
self
.
visit_FuncDefNode
(
func_node
)
if
not
func_node
.
decorators
:
if
scope_type
!=
'cclass'
or
not
func_node
.
decorators
:
return
func_node
return
func_node
return
self
.
_handle_decorators
(
return
self
.
_handle_decorators
(
func_node
,
func_node
.
name
)
func_node
,
func_node
.
name
)
def
visit_CClassDefNode
(
self
,
class_node
):
# This doesn't currently work, so it's disabled.
#
# Problem: assignments to cdef class names do not work. They
# would require an additional check anyway, as the extension
# type must not change its C type, so decorators cannot
# replace an extension type, just alter it and return it.
self
.
visitchildren
(
class_node
)
if
not
class_node
.
decorators
:
return
class_node
error
(
class_node
.
pos
,
"Decorators not allowed on cdef classes (used on type '%s')"
%
class_node
.
class_name
)
return
class_node
#return self._handle_decorators(
# class_node, class_node.class_name)
def
visit_ClassDefNode
(
self
,
class_node
):
self
.
visitchildren
(
class_node
)
if
not
class_node
.
decorators
:
return
class_node
return
self
.
_handle_decorators
(
class_node
,
class_node
.
name
)
def
_handle_decorators
(
self
,
node
,
name
):
def
_handle_decorators
(
self
,
node
,
name
):
decorator_result
=
ExprNodes
.
NameNode
(
node
.
pos
,
name
=
name
)
decorator_result
=
ExprNodes
.
NameNode
(
node
.
pos
,
name
=
name
)
for
decorator
in
node
.
decorators
[::
-
1
]:
for
decorator
in
node
.
decorators
[::
-
1
]:
...
@@ -1451,9 +1467,9 @@ class CreateClosureClasses(CythonTransform):
...
@@ -1451,9 +1467,9 @@ class CreateClosureClasses(CythonTransform):
if
not
from_closure
and
(
self
.
path
or
inner_node
):
if
not
from_closure
and
(
self
.
path
or
inner_node
):
if
not
inner_node
:
if
not
inner_node
:
if
not
node
.
py_cfunc_node
:
if
not
node
.
assmt
:
raise
InternalError
,
"DefNode does not have assignment node"
raise
InternalError
,
"DefNode does not have assignment node"
inner_node
=
node
.
py_cfunc_node
inner_node
=
node
.
assmt
.
rhs
inner_node
.
needs_self_code
=
False
inner_node
.
needs_self_code
=
False
node
.
needs_outer_scope
=
False
node
.
needs_outer_scope
=
False
...
...
Cython/Compiler/Tests/TestDecorators.py
0 → 100644
View file @
e0d366d9
import
unittest
from
Cython.TestUtils
import
TransformTest
from
Cython.Compiler.ParseTreeTransforms
import
DecoratorTransform
class
TestDecorator
(
TransformTest
):
def
test_decorator
(
self
):
t
=
self
.
run_pipeline
([
DecoratorTransform
(
None
)],
u"""
def decorator(fun):
return fun
@decorator
def decorated():
pass
"""
)
self
.
assertCode
(
u"""
def decorator(fun):
return fun
def decorated():
pass
decorated = decorator(decorated)
"""
,
t
)
if
__name__
==
'__main__'
:
unittest
.
main
()
tests/bugs.txt
View file @
e0d366d9
...
@@ -18,6 +18,7 @@ ipow_crash_T562
...
@@ -18,6 +18,7 @@ ipow_crash_T562
pure_mode_cmethod_inheritance_T583
pure_mode_cmethod_inheritance_T583
genexpr_iterable_lookup_T600
genexpr_iterable_lookup_T600
for_from_pyvar_loop_T601
for_from_pyvar_loop_T601
decorators_T593
# CPython regression tests that don't current work:
# CPython regression tests that don't current work:
pyregr.test_threadsignals
pyregr.test_threadsignals
...
...
tests/run/decorators_T593.pyx
View file @
e0d366d9
...
@@ -10,6 +10,7 @@ def testme(func):
...
@@ -10,6 +10,7 @@ def testme(func):
return
True
return
True
except
NameError
:
except
NameError
:
return
False
return
False
@
testme
@
testme
def
am_i_buggy
():
def
am_i_buggy
():
pass
pass
...
@@ -24,6 +25,30 @@ def testclass(klass):
...
@@ -24,6 +25,30 @@ def testclass(klass):
class
Foo
:
class
Foo
:
pass
pass
def
called_deco
(
a
,
b
,
c
):
def
count
(
f
):
a
.
append
(
(
b
,
c
)
)
return
f
return
count
L
=
[]
@
called_deco
(
L
,
5
,
c
=
6
)
@
called_deco
(
L
,
c
=
3
,
b
=
4
)
@
called_deco
(
L
,
1
,
2
)
def
wrapped_func
(
x
):
"""
>>> L
[(1, 2), (4, 3), (5, 6)]
>>> wrapped_func(99)
99
>>> L
[(1, 2), (4, 3), (5, 6)]
"""
return
x
def
class_in_closure
(
x
):
def
class_in_closure
(
x
):
"""
"""
>>> C1, c0 = class_in_closure(5)
>>> C1, c0 = class_in_closure(5)
...
...
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