@@ -48,7 +48,18 @@ def _reference_get_item(args):
4848 return d .get (args [1 ])
4949 except Exception :
5050 return None
51-
51+
52+ def _reference_values (args ):
53+ d = args [0 ]
54+ return list (d .values ())
55+
56+ def _reference_pop (args ):
57+ d = args [0 ]
58+ if (len (args ) == 2 ):
59+ return d .pop (args [1 ])
60+ else :
61+ return d .pop (args [1 ], args [2 ])
62+
5263
5364def _reference_get_item_with_error (args ):
5465 d = args [0 ]
@@ -119,14 +130,20 @@ def _reference_clear(args):
119130
120131
121132def _reference_merge (args ):
122- a , b , override = args
123- if override :
124- a .update (b )
125- else :
126- for k in b :
127- if not k in a :
128- a [k ] = b [k ]
129- return 0
133+ try :
134+ a , b , override = args
135+ if override :
136+ a .update (b )
137+ else :
138+ for k in b .keys ():
139+ if not k in a :
140+ a [k ] = b [k ]
141+ return 0
142+ except :
143+ if sys .version_info .minor >= 6 :
144+ raise SystemError
145+ else :
146+ return - 1
130147
131148class SubDict (dict ):
132149 pass
@@ -151,13 +168,28 @@ def __eq__(self, other):
151168 def __hash__ (self ):
152169 return hash (self .s )
153170
154-
171+ class MappingObj :
172+ def keys (self ):
173+ return "ab"
174+ def __getitem__ (self , key ):
175+ return key
176+
155177class TestPyDict (CPyExtTestCase ):
156178
157179 def compile_module (self , name ):
158180 type (self ).mro ()[1 ].__dict__ ["test_%s" % name ].create_module (name )
159181 super (TestPyDict , self ).compile_module (name )
160182
183+ # PyDict_Pop
184+ test__PyDict_Pop = CPyExtFunction (
185+ _reference_pop ,
186+ lambda : (({}, "a" , "42" ), ({'a' : "hello" }, "a" , "42" ), ({'a' : "hello" }, "b" , "42" ), ({BadEq ('a' ): "hello" }, "a" , "42" )),
187+ resultspec = "O" ,
188+ argspec = 'OOO' ,
189+ arguments = ("PyObject* dict" , "PyObject* key" , "PyObject* deflt" ),
190+ cmpfunc = unhandled_error_compare
191+ )
192+
161193 # PyDict_SetItem
162194 test_PyDict_SetItem = CPyExtFunction (
163195 _reference_set_item ,
@@ -191,7 +223,7 @@ def compile_module(self, name):
191223 callfunction = "wrap_PyDict_GetItem" ,
192224 cmpfunc = unhandled_error_compare
193225 )
194-
226+
195227 # PyDict_GetItemWithError
196228 test_PyDict_GetItemWithError = CPyExtFunction (
197229 _reference_get_item_with_error ,
@@ -332,12 +364,10 @@ def compile_module(self, name):
332364 Py_ssize_t ppos = 0;
333365 PyObject* key;
334366 PyObject* value;
335- Py_hash_t phash;
336-
337- int res = 0;
367+ Py_hash_t phash;
338368
339369 _PyDict_Next(dict, &ppos, &key, &value, &phash);
340- res = _PyDict_SetItem_KnownHash(result, key, value, phash);
370+ _PyDict_SetItem_KnownHash(result, key, value, phash);
341371 return result;
342372 }
343373 ''' ,
@@ -466,9 +496,22 @@ def compile_module(self, name):
466496 (dict (), {"b" : 2 }, 0 ),
467497 (dict ({"a" : 1 }), {"a" : 2 }, 0 ),
468498 (dict ({"a" : 1 }), {"a" : 2 }, 1 ),
499+ (dict ({"a" : 1 }), MappingObj (), 0 ),
500+ (dict ({"a" : 1 }), MappingObj (), 1 ),
501+ (dict ({"a" : 1 }), 1 , 1 ),
502+ (dict ({"a" : 1 }), 1 , 1 ),
469503 ),
470504 resultspec = "i" ,
471505 argspec = "OOi" ,
472506 arguments = ["PyObject* a" , "PyObject* b" , "int override" ],
473507 cmpfunc = unhandled_error_compare
474508 )
509+
510+ # PyDict_Values
511+ test_PyDict_Values = CPyExtFunction (
512+ _reference_values ,
513+ lambda : (({},), ({'a' : "hello" },)),
514+ resultspec = "O" ,
515+ argspec = "O" ,
516+ cmpfunc = unhandled_error_compare
517+ )
0 commit comments