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
66ffb55c
Commit
66ffb55c
authored
Dec 19, 2008
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use a straight call to PyList_Tuple() on code like tuple([...])
parent
41064a84
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
0 deletions
+49
-0
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+36
-0
tests/run/tuple.pyx
tests/run/tuple.pyx
+13
-0
No files found.
Cython/Compiler/Optimize.py
View file @
66ffb55c
...
@@ -426,6 +426,16 @@ class FlattenInListTransform(Visitor.VisitorTransform, SkipDeclarations):
...
@@ -426,6 +426,16 @@ class FlattenInListTransform(Visitor.VisitorTransform, SkipDeclarations):
class
FlattenBuiltinTypeCreation
(
Visitor
.
VisitorTransform
):
class
FlattenBuiltinTypeCreation
(
Visitor
.
VisitorTransform
):
"""Optimise some common instantiation patterns for builtin types.
"""Optimise some common instantiation patterns for builtin types.
"""
"""
PyList_AsTuple_func_type
=
PyrexTypes
.
CFuncType
(
PyrexTypes
.
py_object_type
,
[
PyrexTypes
.
CFuncTypeArg
(
"list"
,
Builtin
.
list_type
,
None
)
])
PyList_AsTuple_name
=
EncodedString
(
"PyList_AsTuple"
)
PyList_AsTuple_entry
=
Symtab
.
Entry
(
PyList_AsTuple_name
,
PyList_AsTuple_name
,
PyList_AsTuple_func_type
)
def
visit_GeneralCallNode
(
self
,
node
):
def
visit_GeneralCallNode
(
self
,
node
):
self
.
visitchildren
(
node
)
self
.
visitchildren
(
node
)
handler
=
self
.
_find_handler
(
'general'
,
node
.
function
)
handler
=
self
.
_find_handler
(
'general'
,
node
.
function
)
...
@@ -486,6 +496,32 @@ class FlattenBuiltinTypeCreation(Visitor.VisitorTransform):
...
@@ -486,6 +496,32 @@ class FlattenBuiltinTypeCreation(Visitor.VisitorTransform):
else
:
else
:
return
node
return
node
def
_handle_simple_tuple
(
self
,
node
,
pos_args
,
kwargs
):
"""Replace tuple([...]) by a call to PyList_AsTuple.
"""
if
not
isinstance
(
pos_args
,
ExprNodes
.
TupleNode
):
return
node
if
len
(
pos_args
.
args
)
!=
1
:
return
node
list_arg
=
pos_args
.
args
[
0
]
if
list_arg
.
type
is
not
Builtin
.
list_type
:
return
node
if
not
isinstance
(
list_arg
,
(
ExprNodes
.
ComprehensionNode
,
ExprNodes
.
ListNode
)):
# everything else may be None => take the safe path
return
node
node
.
args
=
pos_args
.
args
node
.
arg_tuple
=
None
node
.
type
=
Builtin
.
tuple_type
node
.
result_ctype
=
Builtin
.
tuple_type
node
.
function
=
ExprNodes
.
NameNode
(
pos
=
node
.
pos
,
name
=
self
.
PyList_AsTuple_name
,
type
=
self
.
PyList_AsTuple_func_type
,
entry
=
self
.
PyList_AsTuple_entry
)
return
node
def
visit_PyTypeTestNode
(
self
,
node
):
def
visit_PyTypeTestNode
(
self
,
node
):
"""Flatten redundant type checks after tree changes.
"""Flatten redundant type checks after tree changes.
"""
"""
...
...
tests/run/tuple.pyx
View file @
66ffb55c
...
@@ -11,6 +11,12 @@ __doc__ = u"""
...
@@ -11,6 +11,12 @@ __doc__ = u"""
(2, 3, 4)
(2, 3, 4)
>>> l(1,2,3,4,5)
>>> l(1,2,3,4,5)
(17, 42, 88)
(17, 42, 88)
>>> tuple_none()
Traceback (most recent call last):
TypeError: 'NoneType' object is not iterable
>>> tuple_none_list()
Traceback (most recent call last):
TypeError: 'NoneType' object is not iterable
"""
"""
def
f
(
obj1
,
obj2
,
obj3
,
obj4
,
obj5
):
def
f
(
obj1
,
obj2
,
obj3
,
obj4
,
obj5
):
...
@@ -51,3 +57,10 @@ def l(obj1, obj2, obj3, obj4, obj5):
...
@@ -51,3 +57,10 @@ def l(obj1, obj2, obj3, obj4, obj5):
obj1
=
(
obj2
,
obj3
,
obj4
,)
obj1
=
(
obj2
,
obj3
,
obj4
,)
obj1
=
17
,
42
,
88
obj1
=
17
,
42
,
88
return
obj1
return
obj1
def
tuple_none
():
return
tuple
(
None
)
def
tuple_none_list
():
cdef
list
none
=
None
return
tuple
(
none
)
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