45
45
def _reference_get_item (args ):
46
46
try :
47
47
d = args [0 ]
48
- return d [args [1 ]]
49
- except :
50
- raise SystemError
48
+ return d .get (args [1 ])
49
+ except Exception :
50
+ return None
51
+
52
+
53
+ def _reference_get_item_with_error (args ):
54
+ d = args [0 ]
55
+ return d .get (args [1 ])
51
56
52
57
53
58
def _reference_set_item (args ):
@@ -136,6 +141,17 @@ def fresh_dict():
136
141
return ExampleDict
137
142
138
143
144
+ class BadEq :
145
+ def __init__ (self , s ):
146
+ self .s = s
147
+
148
+ def __eq__ (self , other ):
149
+ raise RuntimeError ("boom" )
150
+
151
+ def __hash__ (self ):
152
+ return hash (self .s )
153
+
154
+
139
155
class TestPyDict (CPyExtTestCase ):
140
156
141
157
def compile_module (self , name ):
@@ -160,18 +176,44 @@ def compile_module(self, name):
160
176
# PyDict_GetItem
161
177
test_PyDict_GetItem = CPyExtFunction (
162
178
_reference_get_item ,
163
- lambda : (({}, "a" , "dflt" ), ({'a' : "hello" }, "a" , "dflt" ), ({'a' : "hello" }, "b" , "dflt " )),
164
- code = '''PyObject* wrap_PyDict_GetItem(PyObject* dict, PyObject* key, PyObject* defaultVal ) {
179
+ lambda : (({}, "a" ), ({'a' : "hello" }, "a" ), ({'a' : "hello" }, "b" ), ({ BadEq ( 'a' ): "hello" }, "a " )),
180
+ code = '''PyObject* wrap_PyDict_GetItem(PyObject* dict, PyObject* key) {
165
181
PyObject* result = PyDict_GetItem(dict, key);
166
- return result;
182
+ if (result != NULL) {
183
+ Py_INCREF(result);
184
+ return result;
185
+ }
186
+ Py_RETURN_NONE;
167
187
}''' ,
168
188
resultspec = "O" ,
169
- argspec = 'OOO ' ,
170
- arguments = ("PyObject* dict" , "PyObject* key" , "PyObject* defaultVal" ),
189
+ argspec = 'OO ' ,
190
+ arguments = ("PyObject* dict" , "PyObject* key" ),
171
191
callfunction = "wrap_PyDict_GetItem" ,
172
192
cmpfunc = unhandled_error_compare
173
193
)
174
194
195
+ # PyDict_GetItemWithError
196
+ test_PyDict_GetItemWithError = CPyExtFunction (
197
+ _reference_get_item_with_error ,
198
+ lambda : (({}, "a" ), ({'a' : "hello" }, "a" ), ({'a' : "hello" }, "b" ), ({BadEq ('a' ): "hello" }, "a" )),
199
+ code = '''PyObject* wrap_PyDict_GetItemWithError(PyObject* dict, PyObject* key) {
200
+ PyObject* result = PyDict_GetItemWithError(dict, key);
201
+ if (result != NULL) {
202
+ Py_INCREF(result);
203
+ return result;
204
+ }
205
+ if (PyErr_Occurred()) {
206
+ return NULL;
207
+ }
208
+ Py_RETURN_NONE;
209
+ }''' ,
210
+ resultspec = "O" ,
211
+ argspec = 'OO' ,
212
+ arguments = ("PyObject* dict" , "PyObject* key" ),
213
+ callfunction = "wrap_PyDict_GetItemWithError" ,
214
+ cmpfunc = unhandled_error_compare
215
+ )
216
+
175
217
# PyDict_DelItem
176
218
test_PyDict_DelItem = CPyExtFunction (
177
219
_reference_del_item ,
@@ -194,23 +236,48 @@ def compile_module(self, name):
194
236
# PyDict_GetItemString
195
237
test_PyDict_GetItemString = CPyExtFunctionOutVars (
196
238
_reference_get_item ,
197
- lambda : (({}, "a" , "dflt" ), ({'a' : "hello" }, "a" , "dflt" ), ({'a' : "hello" }, "b" , "dflt " )),
198
- code = '''PyObject* wrap_PyDict_GetItemString(PyObject* dict, char* key, PyObject* defaultValue ) {
239
+ lambda : (({}, "a" ), ({'a' : "hello" }, "a" ), ({'a' : "hello" }, "b" ), ({ BadEq ( 'a' ): "hello" }, "a " )),
240
+ code = '''PyObject* wrap_PyDict_GetItemString(PyObject* dict, char* key) {
199
241
PyObject* result = PyDict_GetItemString(dict, key);
200
242
if (result != NULL) {
201
243
Py_INCREF(result);
244
+ return result;
202
245
}
203
- return result ;
246
+ Py_RETURN_NONE ;
204
247
}
205
248
''' ,
206
249
resultspec = "O" ,
207
- argspec = 'OsO ' ,
208
- arguments = ("PyObject* dict" , "char* key" , "PyObject* defaultValue" ),
250
+ argspec = 'Os ' ,
251
+ arguments = ("PyObject* dict" , "char* key" ),
209
252
resulttype = "PyObject*" ,
210
253
callfunction = "wrap_PyDict_GetItemString" ,
211
254
cmpfunc = unhandled_error_compare
212
255
)
213
256
257
+ # _PyDict_GetItemStringWithError
258
+ test_PyDict_GetItemStringWithError = CPyExtFunctionOutVars (
259
+ _reference_get_item_with_error ,
260
+ lambda : (({}, "a" ), ({'a' : "hello" }, "a" ), ({'a' : "hello" }, "b" ), ({BadEq ('a' ): "hello" }, "a" )),
261
+ code = '''PyObject* wrap_PyDict_GetItemStringWithError(PyObject* dict, char* key) {
262
+ PyObject* result = _PyDict_GetItemStringWithError(dict, key);
263
+ if (result != NULL) {
264
+ Py_INCREF(result);
265
+ return result;
266
+ }
267
+ if (PyErr_Occurred()) {
268
+ return NULL;
269
+ }
270
+ Py_RETURN_NONE;
271
+ }
272
+ ''' ,
273
+ resultspec = "O" ,
274
+ argspec = 'Os' ,
275
+ arguments = ("PyObject* dict" , "char* key" ),
276
+ resulttype = "PyObject*" ,
277
+ callfunction = "wrap_PyDict_GetItemStringWithError" ,
278
+ cmpfunc = unhandled_error_compare
279
+ )
280
+
214
281
# PyDict_DelItemString
215
282
test_PyDict_DelItemString = CPyExtFunction (
216
283
_reference_del_item ,
0 commit comments