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
bfe7e783
Commit
bfe7e783
authored
Jan 29, 2014
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix repeated list multiplication
--HG-- extra : amend_source : 2797e0ad678cc0a33a2548a8a20b00475bc8a21b
parent
9793a1f5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
7 deletions
+43
-7
CHANGES.rst
CHANGES.rst
+3
-0
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+18
-7
tests/run/constant_folding.py
tests/run/constant_folding.py
+22
-0
No files found.
CHANGES.rst
View file @
bfe7e783
...
...
@@ -11,6 +11,9 @@ Features added
Bugs fixed
----------
* List/Tuple literals multiplied by more than one factor were only multiplied
by the last factor instead of all.
* In-place assignments to variables with inferred Python builtin/extension
types could fail with type errors if the result value type was incompatible
with the type of the previous value.
...
...
Cython/Compiler/Optimize.py
View file @
bfe7e783
...
...
@@ -3379,19 +3379,30 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
def
visit_MulNode
(
self
,
node
):
self
.
_calculate_const
(
node
)
if
isinstance
(
node
.
operand1
,
(
ExprNodes
.
ListNode
,
ExprNodes
.
TupleNode
))
:
return
self
.
_calculate_constant_seq
(
node
.
operand1
,
node
.
operand2
)
if
node
.
operand1
.
is_sequence_constructor
:
return
self
.
_calculate_constant_seq
(
node
,
node
.
operand1
,
node
.
operand2
)
if
isinstance
(
node
.
operand1
,
ExprNodes
.
IntNode
)
and
\
isinstance
(
node
.
operand2
,
(
ExprNodes
.
ListNode
,
ExprNodes
.
TupleNode
))
:
return
self
.
_calculate_constant_seq
(
node
.
operand2
,
node
.
operand1
)
node
.
operand2
.
is_sequence_constructor
:
return
self
.
_calculate_constant_seq
(
node
,
node
.
operand2
,
node
.
operand1
)
return
self
.
visit_BinopNode
(
node
)
def
_calculate_constant_seq
(
self
,
sequence_node
,
factor
):
def
_calculate_constant_seq
(
self
,
node
,
sequence_node
,
factor
):
if
factor
.
constant_result
!=
1
and
sequence_node
.
args
:
if
isinstance
(
factor
.
constant_result
,
(
int
,
long
))
and
factor
.
constant_result
<=
0
:
del
sequence_node
.
args
[:]
factor
=
None
sequence_node
.
mult_factor
=
factor
sequence_node
.
mult_factor
=
None
elif
sequence_node
.
mult_factor
is
not
None
:
if
(
isinstance
(
factor
.
constant_result
,
(
int
,
long
))
and
isinstance
(
sequence_node
.
mult_factor
.
constant_result
,
(
int
,
long
))):
value
=
sequence_node
.
mult_factor
.
constant_result
*
factor
.
constant_result
sequence_node
.
mult_factor
=
ExprNodes
.
IntNode
(
sequence_node
.
mult_factor
.
pos
,
value
=
str
(
value
),
constant_result
=
value
)
else
:
# don't know if we can combine the factors, so don't
return
self
.
visit_BinopNode
(
node
)
else
:
sequence_node
.
mult_factor
=
factor
return
sequence_node
def
visit_PrimaryCmpNode
(
self
,
node
):
...
...
tests/run/constant_folding.py
View file @
bfe7e783
...
...
@@ -291,6 +291,28 @@ def mult_empty_list():
return
5
*
[]
*
100
@
cython
.
test_fail_if_path_exists
(
"//MulNode"
,
)
def
mult_list_int_int
():
"""
>>> mult_list_int_int()
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
"""
return
[
1
,
2
]
*
2
*
3
@
cython
.
test_fail_if_path_exists
(
"//MulNode"
,
)
def
mult_int_list_int
():
"""
>>> mult_int_list_int()
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
"""
return
3
*
[
1
,
2
]
*
2
@
cython
.
test_fail_if_path_exists
(
"//MulNode"
,
"//ListNode//IntNode"
,
...
...
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