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
c25a6167
Commit
c25a6167
authored
Apr 06, 2015
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow struct constructors to coerce directly to Python dicts without intermediate assignment
parent
460177f4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
0 deletions
+34
-0
CHANGES.rst
CHANGES.rst
+3
-0
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+7
-0
tests/run/struct_conversion.pyx
tests/run/struct_conversion.pyx
+24
-0
No files found.
CHANGES.rst
View file @
c25a6167
...
...
@@ -32,6 +32,9 @@ Bugs fixed
* Relative cimports could accidentally fall back to trying an absolute cimport
on failure.
* The result of calling a C struct constructor no longer requires an intermediate
assignment when coercing to a Python dict.
* Runtime reported file paths of source files (e.g for profiling and tracing)
are now relative to the build root directory instead of the main source file.
...
...
Cython/Compiler/ExprNodes.py
View file @
c25a6167
...
...
@@ -7164,6 +7164,13 @@ class DictNode(ExprNode):
def
coerce_to
(
self
,
dst_type
,
env
):
if
dst_type
.
is_pyobject
:
self
.
release_errors
()
if
self
.
type
.
is_struct_or_union
:
if
not
dict_type
.
subtype_of
(
dst_type
):
error
(
self
.
pos
,
"Cannot interpret struct as non-dict type '%s'"
%
dst_type
)
return
DictNode
(
self
.
pos
,
key_value_pairs
=
[
DictItemNode
(
item
.
pos
,
key
=
item
.
key
.
coerce_to_pyobject
(
env
),
value
=
item
.
value
.
coerce_to_pyobject
(
env
))
for
item
in
self
.
key_value_pairs
])
if
not
self
.
type
.
subtype_of
(
dst_type
):
error
(
self
.
pos
,
"Cannot interpret dict as type '%s'"
%
dst_type
)
elif
dst_type
.
is_struct_or_union
:
...
...
tests/run/struct_conversion.pyx
View file @
c25a6167
...
...
@@ -13,6 +13,17 @@ def test_constructor(x, y, color):
cdef
Point
p
=
Point
(
x
,
y
,
color
)
return
p
def
return_constructor
(
x
,
y
,
color
):
"""
>>> sorted(return_constructor(1,2,255).items())
[('color', 255), ('x', 1.0), ('y', 2.0)]
>>> try: return_constructor(1, None, 255)
... except TypeError: pass
"""
return
Point
(
x
,
y
,
color
)
def
test_constructor_kwds
(
x
,
y
,
color
):
"""
>>> sorted(test_constructor_kwds(1.25, 2.5, 128).items())
...
...
@@ -25,6 +36,19 @@ def test_constructor_kwds(x, y, color):
cdef
Point
p
=
Point
(
x
=
x
,
y
=
y
,
color
=
color
)
return
p
def
return_constructor_kwds
(
x
,
y
,
color
):
"""
>>> sorted(return_constructor_kwds(1.25, 2.5, 128).items())
[('color', 128), ('x', 1.25), ('y', 2.5)]
>>> return_constructor_kwds(1.25, 2.5, None)
Traceback (most recent call last):
...
TypeError: an integer is required
"""
return
Point
(
x
=
x
,
y
=
y
,
color
=
color
)
def
test_dict_construction
(
x
,
y
,
color
):
"""
>>> sorted(test_dict_construction(4, 5, 64).items())
...
...
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