Skip to content

Commit 2e733b4

Browse files
committed
[GR-34916] Intrinsity python_cext - PyDict_XXX.
PullRequest: graalpython/2024
2 parents b8a90fc + a645767 commit 2e733b4

File tree

8 files changed

+922
-201
lines changed

8 files changed

+922
-201
lines changed

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

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5364
def _reference_get_item_with_error(args):
5465
d = args[0]
@@ -119,14 +130,20 @@ def _reference_clear(args):
119130

120131

121132
def _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

131148
class 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+
155177
class 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+
)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ImpModuleBuiltins.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import com.oracle.graal.python.builtins.objects.object.PythonObject;
7878
import com.oracle.graal.python.builtins.objects.str.PString;
7979
import com.oracle.graal.python.lib.PyObjectLookupAttr;
80+
import com.oracle.graal.python.lib.PyObjectStrAsJavaStringNode;
8081
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromDynamicObjectNode;
8182
import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
8283
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
@@ -192,7 +193,7 @@ protected byte[] getMagicNumberBytes(VirtualFrame frame) {
192193

193194
@Builtin(name = "__create_dynamic__", minNumOfPositionalArgs = 2)
194195
@GenerateNodeFactory
195-
public abstract static class CreateDynamic extends PythonBuiltinNode {
196+
public abstract static class CreateDynamic extends PythonBinaryBuiltinNode {
196197

197198
@Child private CheckFunctionResultNode checkResultNode;
198199
@Child private HPyCheckFunctionResultNode checkHPyResultNode;
@@ -436,4 +437,29 @@ Object run() {
436437
}
437438
}
438439

440+
@Builtin(name = "create_dynamic", minNumOfPositionalArgs = 1, parameterNames = {"moduleSpec", "fileName"})
441+
@GenerateNodeFactory
442+
public abstract static class CreateDynamicNode extends PythonBinaryBuiltinNode {
443+
@Specialization(guards = "isNoValue(fileName)")
444+
Object runNoFileName(VirtualFrame frame, PythonObject moduleSpec, @SuppressWarnings("unused") PNone fileName,
445+
@Cached PyObjectStrAsJavaStringNode asStrignNode,
446+
@Cached CreateDynamic createDynamicNode) {
447+
return run(frame, moduleSpec, PNone.NONE, asStrignNode, createDynamicNode);
448+
}
449+
450+
@Specialization(guards = "!isNoValue(fileName)")
451+
Object run(VirtualFrame frame, PythonObject moduleSpec, Object fileName,
452+
@Cached PyObjectStrAsJavaStringNode asStrignNode,
453+
@Cached CreateDynamic createDynamicNode) {
454+
PythonContext ctx = getContext();
455+
String oldPackageContext = ctx.getPyPackageContext();
456+
ctx.setPyPackageContext(asStrignNode.execute(frame, PyObjectLookupAttr.getUncached().execute(frame, moduleSpec, "name")));
457+
try {
458+
return createDynamicNode.execute(frame, moduleSpec, fileName);
459+
} finally {
460+
ctx.setPyPackageContext(oldPackageContext);
461+
}
462+
}
463+
}
464+
439465
}

0 commit comments

Comments
 (0)