Skip to content

Commit 89c6d5c

Browse files
committed
add various functions around PyImport_AddModule
1 parent ce1a316 commit 89c6d5c

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,33 @@ PyObject* PyImport_ImportModule(const char *name) {
4545
PyObject* PyImport_GetModuleDict() {
4646
return UPCALL_CEXT_O("PyImport_GetModuleDict");
4747
}
48+
49+
PyObject* PyImport_AddModuleObject(PyObject *name) {
50+
return _PyImport_AddModuleObject(name, PyImport_GetModuleDict());
51+
}
52+
53+
PyObject* PyImport_AddModule(const char *name) {
54+
PyObject *nameobj = PyUnicode_FromString(name);
55+
if (nameobj == NULL) {
56+
return NULL;
57+
}
58+
return PyImport_AddModuleObject(nameobj);
59+
}
60+
61+
PyObject* _PyImport_AddModuleObject(PyObject *name, PyObject *modules) {
62+
PyObject* m = PyObject_GetItem(modules, name);
63+
if (PyErr_Occurred()) {
64+
return NULL;
65+
}
66+
if (m != NULL && PyModule_Check(m)) {
67+
return m;
68+
}
69+
m = PyModule_NewObject(name);
70+
if (m == NULL) {
71+
return NULL;
72+
}
73+
if (PyObject_SetItem(modules, name, m) != 0) {
74+
return NULL;
75+
}
76+
return m;
77+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,7 @@ PyObject* PyModule_GetDict(PyObject* o) {
123123
}
124124
return ((PyModuleObject*)o)->md_dict;
125125
}
126+
127+
PyObject* PyModule_NewObject(PyObject* name) {
128+
return UPCALL_CEXT_O("PyModule_New", name);
129+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/DictBuiltins.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ Object getItem(PDict self, Object key,
264264
@Builtin(name = __MISSING__, fixedNumOfArguments = 2)
265265
@GenerateNodeFactory
266266
public abstract static class MissingNode extends PythonBuiltinNode {
267+
@SuppressWarnings("unused")
268+
@Specialization
269+
Object run(Object self, PString key) {
270+
throw raise(KeyError, "%s", key.getValue());
271+
}
272+
267273
@SuppressWarnings("unused")
268274
@Specialization
269275
Object run(Object self, String key) {

graalpython/lib-graalpython/python_cext.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ def PyModule_SetDocString(module, string):
104104
module.__doc__ = string
105105

106106

107+
def PyModule_NewObject(name):
108+
return moduletype(name)
109+
110+
107111
##################### DICT
108112

109113
def PyDict_New():

0 commit comments

Comments
 (0)