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
a819fc64
Commit
a819fc64
authored
Jan 02, 2014
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement independent coercion of subexpressions for conditional if-else expression
parent
b93d3f4b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
4 deletions
+26
-4
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+15
-4
tests/run/if_else_expr.pyx
tests/run/if_else_expr.pyx
+11
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
a819fc64
...
@@ -9650,8 +9650,9 @@ class CondExprNode(ExprNode):
...
@@ -9650,8 +9650,9 @@ class CondExprNode(ExprNode):
return
self
.
true_val
.
type_dependencies
(
env
)
+
self
.
false_val
.
type_dependencies
(
env
)
return
self
.
true_val
.
type_dependencies
(
env
)
+
self
.
false_val
.
type_dependencies
(
env
)
def
infer_type
(
self
,
env
):
def
infer_type
(
self
,
env
):
return
PyrexTypes
.
independent_spanning_type
(
self
.
true_val
.
infer_type
(
env
),
return
PyrexTypes
.
independent_spanning_type
(
self
.
false_val
.
infer_type
(
env
))
self
.
true_val
.
infer_type
(
env
),
self
.
false_val
.
infer_type
(
env
))
def
calculate_constant_result
(
self
):
def
calculate_constant_result
(
self
):
if
self
.
test
.
constant_result
:
if
self
.
test
.
constant_result
:
...
@@ -9663,17 +9664,27 @@ class CondExprNode(ExprNode):
...
@@ -9663,17 +9664,27 @@ class CondExprNode(ExprNode):
self
.
test
=
self
.
test
.
analyse_types
(
env
).
coerce_to_boolean
(
env
)
self
.
test
=
self
.
test
.
analyse_types
(
env
).
coerce_to_boolean
(
env
)
self
.
true_val
=
self
.
true_val
.
analyse_types
(
env
)
self
.
true_val
=
self
.
true_val
.
analyse_types
(
env
)
self
.
false_val
=
self
.
false_val
.
analyse_types
(
env
)
self
.
false_val
=
self
.
false_val
.
analyse_types
(
env
)
self
.
type
=
PyrexTypes
.
independent_spanning_type
(
self
.
true_val
.
type
,
self
.
false_val
.
type
)
self
.
is_temp
=
1
return
self
.
analyse_result_type
(
env
)
def
analyse_result_type
(
self
,
env
):
self
.
type
=
PyrexTypes
.
independent_spanning_type
(
self
.
true_val
.
type
,
self
.
false_val
.
type
)
if
self
.
type
.
is_pyobject
:
if
self
.
type
.
is_pyobject
:
self
.
result_ctype
=
py_object_type
self
.
result_ctype
=
py_object_type
if
self
.
true_val
.
type
.
is_pyobject
or
self
.
false_val
.
type
.
is_pyobject
:
if
self
.
true_val
.
type
.
is_pyobject
or
self
.
false_val
.
type
.
is_pyobject
:
self
.
true_val
=
self
.
true_val
.
coerce_to
(
self
.
type
,
env
)
self
.
true_val
=
self
.
true_val
.
coerce_to
(
self
.
type
,
env
)
self
.
false_val
=
self
.
false_val
.
coerce_to
(
self
.
type
,
env
)
self
.
false_val
=
self
.
false_val
.
coerce_to
(
self
.
type
,
env
)
self
.
is_temp
=
1
if
self
.
type
==
PyrexTypes
.
error_type
:
if
self
.
type
==
PyrexTypes
.
error_type
:
self
.
type_error
()
self
.
type_error
()
return
self
return
self
def
coerce_to
(
self
,
dst_type
,
env
):
self
.
true_val
=
self
.
true_val
.
coerce_to
(
dst_type
,
env
)
self
.
false_val
=
self
.
false_val
.
coerce_to
(
dst_type
,
env
)
self
.
result_ctype
=
None
return
self
.
analyse_result_type
(
env
)
def
type_error
(
self
):
def
type_error
(
self
):
if
not
(
self
.
true_val
.
type
.
is_error
or
self
.
false_val
.
type
.
is_error
):
if
not
(
self
.
true_val
.
type
.
is_error
or
self
.
false_val
.
type
.
is_error
):
error
(
self
.
pos
,
"Incompatible types in conditional expression (%s; %s)"
%
error
(
self
.
pos
,
"Incompatible types in conditional expression (%s; %s)"
%
...
...
tests/run/if_else_expr.pyx
View file @
a819fc64
...
@@ -31,3 +31,14 @@ def test_cpp_pyobject_cast(Foo obj1, Foo obj2, cond):
...
@@ -31,3 +31,14 @@ def test_cpp_pyobject_cast(Foo obj1, Foo obj2, cond):
(<Foo>, None)
(<Foo>, None)
"""
"""
return
func
(
obj1
if
cond
else
obj2
,
obj1
.
data
if
cond
else
obj2
.
data
)
return
func
(
obj1
if
cond
else
obj2
,
obj1
.
data
if
cond
else
obj2
.
data
)
def
test_charptr_coercion
(
x
):
"""
>>> print(test_charptr_coercion(True))
abc
>>> print(test_charptr_coercion(False))
def
"""
cdef
char
*
s
=
'abc'
if
x
else
'def'
return
s
.
decode
(
'ascii'
)
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