Skip to content

Commit 2a23ccb

Browse files
vsakkas96vsakkas
authored andcommitted
Implement dict copy primitive
1 parent 849a7f7 commit 2a23ccb

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

mypyc/primitives/dict_ops.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@
142142
c_function_name='CPyDict_ItemsView',
143143
error_kind=ERR_MAGIC)
144144

145+
# dict.copy()
146+
c_method_op(
147+
name='copy',
148+
arg_types=[dict_rprimitive],
149+
return_type=dict_rprimitive,
150+
c_function_name='PyDict_Copy',
151+
error_kind=ERR_MAGIC,
152+
)
153+
145154
# list(dict.keys())
146155
dict_keys_op = custom_op(
147156
arg_types=[dict_rprimitive],

mypyc/test-data/irbuild-dict.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,14 @@ L0:
312312
r0 = load_address PyDict_Type
313313
x = r0
314314
return 1
315+
316+
[case testDictCopy]
317+
from typing import Dict
318+
def f(d: Dict[int, int]) -> Dict[int, int]:
319+
return d.copy() # type: ignore
320+
[out]
321+
def f(d):
322+
d, r0 :: dict
323+
L0:
324+
r0 = PyDict_Copy(d)
325+
return r0

mypyc/test-data/run-dicts.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ od.move_to_end(1)
9191
assert get_content(od) == ([3, 1], [4, 2], [(3, 4), (1, 2)])
9292
assert get_content_set({1: 2}) == ({1}, {2}, {(1, 2)})
9393
assert get_content_set(od) == ({1, 3}, {2, 4}, {(1, 2), (3, 4)})
94+
95+
d = {}
96+
assert d.copy() == d
97+
d = {'a': 1, 'b': 2}
98+
assert d.copy() == d
9499
[typing fixtures/typing-full.pyi]
95100

96101
[case testDictIterationMethodsRun]

0 commit comments

Comments
 (0)