Skip to content

Commit f5515e6

Browse files
committed
[GR-26653] Add _PyDict_GetItemStringWithError
PullRequest: graalpython/1321
2 parents cc8f028 + 0da1ca8 commit f5515e6

File tree

2 files changed

+85
-14
lines changed

2 files changed

+85
-14
lines changed

graalpython/com.oracle.graal.python.cext/src/dictobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ PyObject* PyDict_GetItem(PyObject* d, PyObject* k) {
6464

6565
UPCALL_ID(PyDict_GetItemWithError);
6666
PyObject* PyDict_GetItemWithError(PyObject* d, PyObject* k) {
67-
return UPCALL_CEXT_O(_jls_PyDict_GetItemWithError, native_to_java(d), native_to_java(k));
67+
return UPCALL_CEXT_BORROWED(_jls_PyDict_GetItemWithError, native_to_java(d), native_to_java(k));
6868
}
6969

7070
PyObject* _PyDict_GetItemId(PyObject* d, _Py_Identifier* id) {
@@ -127,6 +127,10 @@ PyObject * PyDict_GetItemString(PyObject *d, const char *key) {
127127
return UPCALL_CEXT_BORROWED(_jls_PyDict_GetItem, native_to_java(d), polyglot_from_string(key, SRC_CS));
128128
}
129129

130+
PyObject*_PyDict_GetItemStringWithError(PyObject *d, const char *k){
131+
return UPCALL_CEXT_BORROWED(_jls_PyDict_GetItemWithError, native_to_java(d), polyglot_from_string(k, SRC_CS));
132+
}
133+
130134
int PyDict_SetItemString(PyObject *d, const char *key, PyObject *item) {
131135
UPCALL_CEXT_I(_jls_PyDict_SetItem, native_to_java(d), polyglot_from_string(key, SRC_CS), native_to_java(item));
132136
return 0;

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_dict.py

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@
4545
def _reference_get_item(args):
4646
try:
4747
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])
5156

5257

5358
def _reference_set_item(args):
@@ -136,6 +141,17 @@ def fresh_dict():
136141
return ExampleDict
137142

138143

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+
139155
class TestPyDict(CPyExtTestCase):
140156

141157
def compile_module(self, name):
@@ -160,18 +176,44 @@ def compile_module(self, name):
160176
# PyDict_GetItem
161177
test_PyDict_GetItem = CPyExtFunction(
162178
_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) {
165181
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;
167187
}''',
168188
resultspec="O",
169-
argspec='OOO',
170-
arguments=("PyObject* dict", "PyObject* key", "PyObject* defaultVal"),
189+
argspec='OO',
190+
arguments=("PyObject* dict", "PyObject* key"),
171191
callfunction="wrap_PyDict_GetItem",
172192
cmpfunc=unhandled_error_compare
173193
)
174194

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+
175217
# PyDict_DelItem
176218
test_PyDict_DelItem = CPyExtFunction(
177219
_reference_del_item,
@@ -194,23 +236,48 @@ def compile_module(self, name):
194236
# PyDict_GetItemString
195237
test_PyDict_GetItemString = CPyExtFunctionOutVars(
196238
_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) {
199241
PyObject* result = PyDict_GetItemString(dict, key);
200242
if (result != NULL) {
201243
Py_INCREF(result);
244+
return result;
202245
}
203-
return result;
246+
Py_RETURN_NONE;
204247
}
205248
''',
206249
resultspec="O",
207-
argspec='OsO',
208-
arguments=("PyObject* dict", "char* key", "PyObject* defaultValue"),
250+
argspec='Os',
251+
arguments=("PyObject* dict", "char* key"),
209252
resulttype="PyObject*",
210253
callfunction="wrap_PyDict_GetItemString",
211254
cmpfunc=unhandled_error_compare
212255
)
213256

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+
214281
# PyDict_DelItemString
215282
test_PyDict_DelItemString = CPyExtFunction(
216283
_reference_del_item,

0 commit comments

Comments
 (0)