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
Boxiang Sun
cython
Commits
9534cbd1
Commit
9534cbd1
authored
Oct 07, 2008
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Coercion of dict to struct.
parent
04f9fda6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
10 deletions
+61
-10
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+61
-10
No files found.
Cython/Compiler/ExprNodes.py
View file @
9534cbd1
...
...
@@ -2880,6 +2880,8 @@ class DictNode(ExprNode):
# Dictionary constructor.
#
# key_value_pairs [DictItemNode]
#
# obj_conversion_errors [PyrexError] used internally
subexprs
=
[
'key_value_pairs'
]
...
...
@@ -2892,11 +2894,52 @@ class DictNode(ExprNode):
self
.
compile_time_value_error
(
e
)
def
analyse_types
(
self
,
env
):
hold_errors
()
for
item
in
self
.
key_value_pairs
:
item
.
analyse_types
(
env
)
self
.
type
=
dict_type
self
.
gil_check
(
env
)
self
.
obj_conversion_errors
=
held_errors
()
release_errors
(
ignore
=
True
)
self
.
type
=
dict_type
self
.
is_temp
=
1
def
coerce_to
(
self
,
dst_type
,
env
):
if
dst_type
.
is_pyobject
:
self
.
release_errors
()
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
:
self
.
type
=
dst_type
if
not
dst_type
.
is_struct
and
len
(
self
.
key_value_pairs
)
!=
1
:
error
(
self
.
pos
,
"Exactly one field must be specified to convert to union '%s'"
%
dst_type
)
elif
dst_type
.
is_struct
and
len
(
self
.
key_value_pairs
)
<
len
(
dst_type
.
scope
.
var_entries
):
warning
(
self
.
pos
,
"Not all members given for struct '%s'"
%
dst_type
,
1
)
for
item
in
self
.
key_value_pairs
:
if
isinstance
(
item
.
key
,
CoerceToPyTypeNode
):
item
.
key
=
item
.
key
.
arg
if
isinstance
(
item
.
key
,
(
StringNode
,
IdentifierStringNode
)):
item
.
key
=
NameNode
(
pos
=
item
.
key
.
pos
,
name
=
item
.
key
.
value
)
if
not
isinstance
(
item
.
key
,
NameNode
):
print
item
.
key
error
(
item
.
key
.
pos
,
"Struct field must be a name"
)
else
:
member
=
dst_type
.
scope
.
lookup_here
(
item
.
key
.
name
)
if
not
member
:
error
(
item
.
key
.
pos
,
"struct '%s' has no field '%s'"
%
(
dst_type
,
item
.
key
.
name
))
else
:
value
=
item
.
value
if
isinstance
(
value
,
CoerceToPyTypeNode
):
value
=
value
.
arg
item
.
value
=
value
.
coerce_to
(
member
.
type
,
env
)
else
:
self
.
type
=
error_type
error
(
self
.
pos
,
"Cannot interpret dict as type '%s'"
%
dst_type
)
return
self
def
release_errors
(
self
):
for
err
in
self
.
obj_conversion_errors
:
report_error
(
err
)
self
.
obj_conversion_errors
=
[]
gil_message
=
"Constructing Python dict"
...
...
@@ -2913,17 +2956,25 @@ class DictNode(ExprNode):
def
generate_evaluation_code
(
self
,
code
):
# Custom method used here because key-value
# pairs are evaluated and used one at a time.
code
.
putln
(
"%s = PyDict_New(); %s"
%
(
self
.
result
(),
code
.
error_goto_if_null
(
self
.
result
(),
self
.
pos
)))
if
self
.
type
.
is_pyobject
:
self
.
release_errors
()
code
.
putln
(
"%s = PyDict_New(); %s"
%
(
self
.
result
(),
code
.
error_goto_if_null
(
self
.
result
(),
self
.
pos
)))
for
item
in
self
.
key_value_pairs
:
item
.
generate_evaluation_code
(
code
)
code
.
put_error_if_neg
(
self
.
pos
,
"PyDict_SetItem(%s, %s, %s)"
%
(
self
.
result
(),
item
.
key
.
py_result
(),
item
.
value
.
py_result
()))
if
self
.
type
.
is_pyobject
:
code
.
put_error_if_neg
(
self
.
pos
,
"PyDict_SetItem(%s, %s, %s)"
%
(
self
.
result
(),
item
.
key
.
py_result
(),
item
.
value
.
py_result
()))
else
:
code
.
putln
(
"%s.%s = %s;"
%
(
self
.
result
(),
item
.
key
.
name
,
item
.
value
.
result
()))
item
.
generate_disposal_code
(
code
)
def
annotate
(
self
,
code
):
...
...
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