Debian Codegraph @

Modules/_testlimitedcapi/import.c

raw
1 // Need limited C API version 3.13 for PyImport_AddModuleRef()
2 #include "pyconfig.h" // Py_GIL_DISABLED
3 #if !defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API)
4 # define Py_LIMITED_API 0x030d0000
5 #endif
6
7 #include "parts.h"
8 #include "util.h"
9
10
11 /* Test PyImport_GetMagicNumber() */
12 static PyObject *
13 pyimport_getmagicnumber(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
14 {
15 long magic = PyImport_GetMagicNumber();
16 return PyLong_FromLong(magic);
17 }
18
19
20 /* Test PyImport_GetMagicTag() */
21 static PyObject *
22 pyimport_getmagictag(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
23 {
24 const char *tag = PyImport_GetMagicTag();
25 return PyUnicode_FromString(tag);
26 }
27
28
29 /* Test PyImport_GetModuleDict() */
30 static PyObject *
31 pyimport_getmoduledict(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
32 {
33 return Py_XNewRef(PyImport_GetModuleDict());
34 }
35
36
37 /* Test PyImport_GetModule() */
38 static PyObject *
39 pyimport_getmodule(PyObject *Py_UNUSED(module), PyObject *name)
40 {
41 assert(!PyErr_Occurred());
42 NULLABLE(name);
43 PyObject *module = PyImport_GetModule(name);
44 if (module == NULL && !PyErr_Occurred()) {
45 return Py_NewRef(PyExc_KeyError);
46 }
47 return module;
48 }
49
50
51 /* Test PyImport_AddModuleObject() */
52 static PyObject *
53 pyimport_addmoduleobject(PyObject *Py_UNUSED(module), PyObject *name)
54 {
55 NULLABLE(name);
56 return Py_XNewRef(PyImport_AddModuleObject(name));
57 }
58
59
60 /* Test PyImport_AddModule() */
61 static PyObject *
62 pyimport_addmodule(PyObject *Py_UNUSED(module), PyObject *args)
63 {
64 const char *name;
65 Py_ssize_t size;
66 if (!PyArg_ParseTuple(args, "z#", &name, &size)) {
67 return NULL;
68 }
69
70 return Py_XNewRef(PyImport_AddModule(name));
71 }
72
73
74 /* Test PyImport_AddModuleRef() */
75 static PyObject *
76 pyimport_addmoduleref(PyObject *Py_UNUSED(module), PyObject *args)
77 {
78 const char *name;
79 Py_ssize_t size;
80 if (!PyArg_ParseTuple(args, "z#", &name, &size)) {
81 return NULL;
82 }
83
84 return PyImport_AddModuleRef(name);
85 }
86
87
88 /* Test PyImport_Import() */
89 static PyObject *
90 pyimport_import(PyObject *Py_UNUSED(module), PyObject *name)
91 {
92 NULLABLE(name);
93 return PyImport_Import(name);
94 }
95
96
97 /* Test PyImport_ImportModule() */
98 static PyObject *
99 pyimport_importmodule(PyObject *Py_UNUSED(module), PyObject *args)
100 {
101 const char *name;
102 Py_ssize_t size;
103 if (!PyArg_ParseTuple(args, "z#", &name, &size)) {
104 return NULL;
105 }
106
107 return PyImport_ImportModule(name);
108 }
109
110
111 /* Test PyImport_ImportModuleNoBlock() */
112 static PyObject *
113 pyimport_importmodulenoblock(PyObject *Py_UNUSED(module), PyObject *args)
114 {
115 const char *name;
116 Py_ssize_t size;
117 if (!PyArg_ParseTuple(args, "z#", &name, &size)) {
118 return NULL;
119 }
120
121 _Py_COMP_DIAG_PUSH
122 _Py_COMP_DIAG_IGNORE_DEPR_DECLS
123 return PyImport_ImportModuleNoBlock(name);
124 _Py_COMP_DIAG_POP
125 }
126
127
128 /* Test PyImport_ImportModuleEx() */
129 static PyObject *
130 pyimport_importmoduleex(PyObject *Py_UNUSED(module), PyObject *args)
131 {
132 const char *name;
133 Py_ssize_t size;
134 PyObject *globals, *locals, *fromlist;
135 if (!PyArg_ParseTuple(args, "z#OOO",
136 &name, &size, &globals, &locals, &fromlist)) {
137 return NULL;
138 }
139 NULLABLE(globals);
140 NULLABLE(locals);
141 NULLABLE(fromlist);
142
143 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
144 }
145
146
147 /* Test PyImport_ImportModuleLevel() */
148 static PyObject *
149 pyimport_importmodulelevel(PyObject *Py_UNUSED(module), PyObject *args)
150 {
151 const char *name;
152 Py_ssize_t size;
153 PyObject *globals, *locals, *fromlist;
154 int level;
155 if (!PyArg_ParseTuple(args, "z#OOOi",
156 &name, &size, &globals, &locals, &fromlist, &level)) {
157 return NULL;
158 }
159 NULLABLE(globals);
160 NULLABLE(locals);
161 NULLABLE(fromlist);
162
163 return PyImport_ImportModuleLevel(name, globals, locals, fromlist, level);
164 }
165
166
167 /* Test PyImport_ImportModuleLevelObject() */
168 static PyObject *
169 pyimport_importmodulelevelobject(PyObject *Py_UNUSED(module), PyObject *args)
170 {
171 PyObject *name, *globals, *locals, *fromlist;
172 int level;
173 if (!PyArg_ParseTuple(args, "OOOOi",
174 &name, &globals, &locals, &fromlist, &level)) {
175 return NULL;
176 }
177 NULLABLE(name);
178 NULLABLE(globals);
179 NULLABLE(locals);
180 NULLABLE(fromlist);
181
182 return PyImport_ImportModuleLevelObject(name, globals, locals, fromlist, level);
183 }
184
185
186 /* Test PyImport_ImportFrozenModule() */
187 static PyObject *
188 pyimport_importfrozenmodule(PyObject *Py_UNUSED(module), PyObject *args)
189 {
190 const char *name;
191 Py_ssize_t size;
192 if (!PyArg_ParseTuple(args, "z#", &name, &size)) {
193 return NULL;
194 }
195
196 RETURN_INT(PyImport_ImportFrozenModule(name));
197 }
198
199
200 /* Test PyImport_ImportFrozenModuleObject() */
201 static PyObject *
202 pyimport_importfrozenmoduleobject(PyObject *Py_UNUSED(module), PyObject *name)
203 {
204 NULLABLE(name);
205 RETURN_INT(PyImport_ImportFrozenModuleObject(name));
206 }
207
208
209 /* Test PyImport_ExecCodeModule() */
210 static PyObject *
211 pyimport_executecodemodule(PyObject *Py_UNUSED(module), PyObject *args)
212 {
213 const char *name;
214 Py_ssize_t size;
215 PyObject *code;
216 if (!PyArg_ParseTuple(args, "z#O", &name, &size, &code)) {
217 return NULL;
218 }
219 NULLABLE(code);
220
221 return PyImport_ExecCodeModule(name, code);
222 }
223
224
225 /* Test PyImport_ExecCodeModuleEx() */
226 static PyObject *
227 pyimport_executecodemoduleex(PyObject *Py_UNUSED(module), PyObject *args)
228 {
229 const char *name;
230 Py_ssize_t size;
231 PyObject *code;
232 const char *pathname;
233 if (!PyArg_ParseTuple(args, "z#Oz#", &name, &size, &code, &pathname, &size)) {
234 return NULL;
235 }
236 NULLABLE(code);
237
238 return PyImport_ExecCodeModuleEx(name, code, pathname);
239 }
240
241
242 /* Test PyImport_ExecCodeModuleWithPathnames() */
243 static PyObject *
244 pyimport_executecodemodulewithpathnames(PyObject *Py_UNUSED(module), PyObject *args)
245 {
246 const char *name;
247 Py_ssize_t size;
248 PyObject *code;
249 const char *pathname;
250 const char *cpathname;
251 if (!PyArg_ParseTuple(args, "z#Oz#z#", &name, &size, &code, &pathname, &size, &cpathname, &size)) {
252 return NULL;
253 }
254 NULLABLE(code);
255
256 return PyImport_ExecCodeModuleWithPathnames(name, code,
257 pathname, cpathname);
258 }
259
260
261 /* Test PyImport_ExecCodeModuleObject() */
262 static PyObject *
263 pyimport_executecodemoduleobject(PyObject *Py_UNUSED(module), PyObject *args)
264 {
265 PyObject *name, *code, *pathname, *cpathname;
266 if (!PyArg_ParseTuple(args, "OOOO", &name, &code, &pathname, &cpathname)) {
267 return NULL;
268 }
269 NULLABLE(name);
270 NULLABLE(code);
271 NULLABLE(pathname);
272 NULLABLE(cpathname);
273
274 return PyImport_ExecCodeModuleObject(name, code, pathname, cpathname);
275 }
276
277
278 static PyMethodDef test_methods[] = {
279 {"PyImport_GetMagicNumber", pyimport_getmagicnumber, METH_NOARGS},
280 {"PyImport_GetMagicTag", pyimport_getmagictag, METH_NOARGS},
281 {"PyImport_GetModuleDict", pyimport_getmoduledict, METH_NOARGS},
282 {"PyImport_GetModule", pyimport_getmodule, METH_O},
283 {"PyImport_AddModuleObject", pyimport_addmoduleobject, METH_O},
284 {"PyImport_AddModule", pyimport_addmodule, METH_VARARGS},
285 {"PyImport_AddModuleRef", pyimport_addmoduleref, METH_VARARGS},
286 {"PyImport_Import", pyimport_import, METH_O},
287 {"PyImport_ImportModule", pyimport_importmodule, METH_VARARGS},
288 {"PyImport_ImportModuleNoBlock", pyimport_importmodulenoblock, METH_VARARGS},
289 {"PyImport_ImportModuleEx", pyimport_importmoduleex, METH_VARARGS},
290 {"PyImport_ImportModuleLevel", pyimport_importmodulelevel, METH_VARARGS},
291 {"PyImport_ImportModuleLevelObject", pyimport_importmodulelevelobject, METH_VARARGS},
292 {"PyImport_ImportFrozenModule", pyimport_importfrozenmodule, METH_VARARGS},
293 {"PyImport_ImportFrozenModuleObject", pyimport_importfrozenmoduleobject, METH_O},
294 {"PyImport_ExecCodeModule", pyimport_executecodemodule, METH_VARARGS},
295 {"PyImport_ExecCodeModuleEx", pyimport_executecodemoduleex, METH_VARARGS},
296 {"PyImport_ExecCodeModuleWithPathnames", pyimport_executecodemodulewithpathnames, METH_VARARGS},
297 {"PyImport_ExecCodeModuleObject", pyimport_executecodemoduleobject, METH_VARARGS},
298 {NULL},
299 };
300
301
302 int
303 _PyTestLimitedCAPI_Init_Import(PyObject *module)
304 {
305 return PyModule_AddFunctions(module, test_methods);
306 }