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
be65922f
Commit
be65922f
authored
Dec 16, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement Python corner case of passing a tuple as assert message
parent
461b8932
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
64 additions
and
2 deletions
+64
-2
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+2
-1
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+7
-1
tests/run/assert.pyx
tests/run/assert.pyx
+55
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
be65922f
...
...
@@ -5673,6 +5673,7 @@ class SequenceNode(ExprNode):
is_sequence_constructor
=
1
unpacked_items
=
None
mult_factor
=
None
slow
=
False
# trade speed for code size (e.g. use PyTuple_Pack())
def
compile_time_value_list
(
self
,
denv
):
return
[
arg
.
compile_time_value
(
denv
)
for
arg
in
self
.
args
]
...
...
@@ -5754,7 +5755,7 @@ class SequenceNode(ExprNode):
else
:
size_factor
=
' * ((%s<0) ? 0:%s)'
%
(
c_mult
,
c_mult
)
if
self
.
type
is
Builtin
.
tuple_type
and
self
.
is_literal
and
not
c_mult
:
if
self
.
type
is
Builtin
.
tuple_type
and
(
self
.
is_literal
or
self
.
slow
)
and
not
c_mult
:
# use PyTuple_Pack() to avoid generating huge amounts of one-time code
code
.
putln
(
'%s = PyTuple_Pack(%d, %s); %s'
%
(
target
,
...
...
Cython/Compiler/Nodes.py
View file @
be65922f
...
...
@@ -5222,7 +5222,13 @@ class AssertStatNode(StatNode):
self
.
cond
=
self
.
cond
.
analyse_boolean_expression
(
env
)
if
self
.
value
:
value
=
self
.
value
.
analyse_types
(
env
)
self
.
value
=
value
.
coerce_to_pyobject
(
env
)
if
value
.
type
is
Builtin
.
tuple_type
or
not
value
.
type
.
is_builtin_type
:
# prevent tuple values from being interpreted as argument value tuples
from
ExprNodes
import
TupleNode
value
=
TupleNode
(
value
.
pos
,
args
=
[
value
],
slow
=
True
)
self
.
value
=
value
.
analyse_types
(
env
,
skip_children
=
True
)
else
:
self
.
value
=
value
.
coerce_to_pyobject
(
env
)
return
self
nogil_check
=
Node
.
gil_error
...
...
tests/run/assert.pyx
View file @
be65922f
# mode: run
cimport
cython
def
f
(
a
,
b
,
int
i
):
"""
>>> f(1, 2, 1)
...
...
@@ -15,11 +19,62 @@ def f(a, b, int i):
assert
a
+
b
assert
i
@
cython
.
test_assert_path_exists
(
'//AssertStatNode'
,
'//AssertStatNode//TupleNode'
)
def
g
(
a
,
b
):
"""
>>> g(1, "works")
>>> g(0, "fails")
Traceback (most recent call last):
AssertionError: fails
>>> g(0, (1, 2))
Traceback (most recent call last):
AssertionError: (1, 2)
"""
assert
a
,
b
@
cython
.
test_assert_path_exists
(
'//AssertStatNode'
,
'//AssertStatNode//TupleNode'
)
def
g
(
a
,
b
):
"""
>>> g(1, "works")
>>> g(0, "fails")
Traceback (most recent call last):
AssertionError: fails
>>> g(0, (1, 2))
Traceback (most recent call last):
AssertionError: (1, 2)
"""
assert
a
,
b
@
cython
.
test_assert_path_exists
(
'//AssertStatNode'
,
'//AssertStatNode//TupleNode'
,
'//AssertStatNode//TupleNode//TupleNode'
)
def
assert_with_tuple_arg
(
a
):
"""
>>> assert_with_tuple_arg(True)
>>> assert_with_tuple_arg(False)
Traceback (most recent call last):
AssertionError: (1, 2)
"""
assert
a
,
(
1
,
2
)
@
cython
.
test_assert_path_exists
(
'//AssertStatNode'
)
@
cython
.
test_fail_if_path_exists
(
'//AssertStatNode//TupleNode'
)
def
assert_with_str_arg
(
a
):
"""
>>> assert_with_str_arg(True)
>>> assert_with_str_arg(False)
Traceback (most recent call last):
AssertionError: abc
"""
assert
a
,
'abc'
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