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
Gwenaël Samain
cython
Commits
f0298152
Commit
f0298152
authored
Feb 17, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
enable MethodDispatcherTransform() to dispatch also on special method calls triggered by operators
parent
23350e5a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
98 additions
and
25 deletions
+98
-25
Cython/Compiler/Visitor.pxd
Cython/Compiler/Visitor.pxd
+4
-1
Cython/Compiler/Visitor.py
Cython/Compiler/Visitor.py
+94
-24
No files found.
Cython/Compiler/Visitor.pxd
View file @
f0298152
...
...
@@ -29,7 +29,10 @@ cdef class EnvTransform(CythonTransform):
cdef
class
MethodDispatcherTransform
(
EnvTransform
):
cdef
_find_handler
(
self
,
match_name
,
bint
has_kwargs
)
cdef
_dispatch_to_handler
(
self
,
node
,
function
,
arg_list
,
kwargs
=*
)
cdef
_dispatch_to_handler
(
self
,
node
,
function
,
arg_list
,
kwargs
)
cdef
_dispatch_to_method_handler
(
self
,
attr_name
,
self_arg
,
is_unbound_method
,
type_name
,
node
,
arg_list
,
kwargs
)
cdef
class
RecursiveNodeReplacer
(
VisitorTransform
):
cdef
public
orig_node
...
...
Cython/Compiler/Visitor.py
View file @
f0298152
...
...
@@ -420,11 +420,43 @@ class NodeRefCleanupMixin(object):
return
replacement
find_special_method_for_binary_operator
=
{
'<'
:
'__lt__'
,
'<='
:
'__le__'
,
'=='
:
'__eq__'
,
'!='
:
'__ne__'
,
'>='
:
'__ge__'
,
'>'
:
'__gt__'
,
'+'
:
'__add__'
,
'&'
:
'__and__'
,
'/'
:
'__truediv__'
,
'//'
:
'__floordiv__'
,
'<<'
:
'__lshift__'
,
'%'
:
'__mod__'
,
'*'
:
'__mul__'
,
'|'
:
'__or__'
,
'**'
:
'__pow__'
,
'>>'
:
'__rshift__'
,
'-'
:
'__sub__'
,
'^'
:
'__xor__'
,
'in'
:
'__contains__'
,
}.
get
find_special_method_for_unary_operator
=
{
'not'
:
'__not__'
,
'~'
:
'__inv__'
,
'-'
:
'__neg__'
,
'+'
:
'__pos__'
,
}.
get
class
MethodDispatcherTransform
(
EnvTransform
):
"""
Base class for transformations that want to intercept on specific
builtin functions or methods of builtin types. Must run after
declaration analysis when entries were assigned.
builtin functions or methods of builtin types, including special
methods triggered by Python operators. Must run after declaration
analysis when entries were assigned.
Naming pattern for handler methods is as follows:
...
...
@@ -432,7 +464,7 @@ class MethodDispatcherTransform(EnvTransform):
* builtin methods: _handle_(general|simple|any)_method_TYPENAME_METHODNAME
"""
# only visit call nodes
# only visit call nodes
and Python operations
def
visit_GeneralCallNode
(
self
,
node
):
self
.
visitchildren
(
node
)
function
=
node
.
function
...
...
@@ -446,8 +478,7 @@ class MethodDispatcherTransform(EnvTransform):
# can't handle **kwargs
return
node
args
=
arg_tuple
.
args
return
self
.
_dispatch_to_handler
(
node
,
function
,
args
,
keyword_args
)
return
self
.
_dispatch_to_handler
(
node
,
function
,
args
,
keyword_args
)
def
visit_SimpleCallNode
(
self
,
node
):
self
.
visitchildren
(
node
)
...
...
@@ -459,8 +490,40 @@ class MethodDispatcherTransform(EnvTransform):
args
=
arg_tuple
.
args
else
:
args
=
node
.
args
return
self
.
_dispatch_to_handler
(
node
,
function
,
args
)
return
self
.
_dispatch_to_handler
(
node
,
function
,
args
,
None
)
def
visit_BinopNode
(
self
,
node
):
self
.
visitchildren
(
node
)
# FIXME: could special case 'not_in'
special_method_name
=
find_special_method_for_binary_operator
(
node
.
operator
)
if
special_method_name
:
operand1
,
operand2
=
node
.
operand1
,
node
.
operand2
if
special_method_name
==
'__contains__'
:
operand1
,
operand2
=
operand2
,
operand1
obj_type
=
operand1
.
type
if
obj_type
.
is_builtin_type
:
type_name
=
obj_type
.
name
else
:
type_name
=
"object"
# safety measure
node
=
self
.
_dispatch_to_method_handler
(
special_method_name
,
None
,
False
,
type_name
,
node
,
[
operand1
,
operand2
],
None
)
return
node
def
visit_UnopNode
(
self
,
node
):
self
.
visitchildren
(
node
)
special_method_name
=
find_special_method_for_unary_operator
(
node
.
operator
)
if
special_method_name
:
operand
=
node
.
operand
obj_type
=
operand
.
type
if
obj_type
.
is_builtin_type
:
type_name
=
obj_type
.
name
else
:
type_name
=
"object"
# safety measure
node
=
self
.
_dispatch_to_method_handler
(
special_method_name
,
None
,
False
,
type_name
,
node
,
[
operand
],
None
)
return
node
### dispatch to specific handlers
...
...
@@ -500,31 +563,38 @@ class MethodDispatcherTransform(EnvTransform):
arg_list
and
arg_list
[
0
].
type
.
is_pyobject
):
# calling an unbound method like 'list.append(L,x)'
# (ignoring 'type.mro()' here ...)
type_name
=
function
.
obj
.
name
type_name
=
self_arg
.
name
self_arg
=
None
is_unbound_method
=
True
else
:
type_name
=
obj_type
.
name
else
:
type_name
=
"object"
# safety measure
method_handler
=
self
.
_find_handler
(
"method_%s_%s"
%
(
type_name
,
attr_name
),
kwargs
)
if
method_handler
is
None
:
if
attr_name
in
TypeSlots
.
method_name_to_slot
\
or
attr_name
==
'__new__'
:
method_handler
=
self
.
_find_handler
(
"slot%s"
%
attr_name
,
kwargs
)
if
method_handler
is
None
:
return
node
if
self_arg
is
not
None
:
arg_list
=
[
self_arg
]
+
list
(
arg_list
)
if
kwargs
:
return
method_handler
(
node
,
arg_list
,
kwargs
,
is_unbound_method
)
else
:
return
method_handler
(
node
,
arg_list
,
is_unbound_method
)
type_name
=
"object"
# safety measure
return
self
.
_dispatch_to_method_handler
(
attr_name
,
self_arg
,
is_unbound_method
,
type_name
,
node
,
arg_list
,
kwargs
)
else
:
return
node
def
_dispatch_to_method_handler
(
self
,
attr_name
,
self_arg
,
is_unbound_method
,
type_name
,
node
,
arg_list
,
kwargs
):
method_handler
=
self
.
_find_handler
(
"method_%s_%s"
%
(
type_name
,
attr_name
),
kwargs
)
if
method_handler
is
None
:
if
(
attr_name
in
TypeSlots
.
method_name_to_slot
or
attr_name
==
'__new__'
):
method_handler
=
self
.
_find_handler
(
"slot%s"
%
attr_name
,
kwargs
)
if
method_handler
is
None
:
return
node
if
self_arg
is
not
None
:
arg_list
=
[
self_arg
]
+
list
(
arg_list
)
if
kwargs
:
return
method_handler
(
node
,
arg_list
,
kwargs
,
is_unbound_method
)
else
:
return
method_handler
(
node
,
arg_list
,
is_unbound_method
)
class
RecursiveNodeReplacer
(
VisitorTransform
):
"""
...
...
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