-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
Expand file tree
/
Copy pathimport.c
More file actions
109 lines (94 loc) · 2.95 KB
/
import.c
File metadata and controls
109 lines (94 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "parts.h"
#include "util.h"
// Test PyImport_ImportModuleAttr()
static PyObject *
pyimport_importmoduleattr(PyObject *self, PyObject *args)
{
PyObject *mod_name, *attr_name;
if (!PyArg_ParseTuple(args, "OO", &mod_name, &attr_name)) {
return NULL;
}
NULLABLE(mod_name);
NULLABLE(attr_name);
return PyImport_ImportModuleAttr(mod_name, attr_name);
}
// Test PyImport_ImportModuleAttrString()
static PyObject *
pyimport_importmoduleattrstring(PyObject *self, PyObject *args)
{
const char *mod_name, *attr_name;
Py_ssize_t len;
if (!PyArg_ParseTuple(args, "z#z#", &mod_name, &len, &attr_name, &len)) {
return NULL;
}
return PyImport_ImportModuleAttrString(mod_name, attr_name);
}
static PyObject *
pyimport_setlazyimportsmode(PyObject *self, PyObject *args)
{
PyObject *mode;
if (!PyArg_ParseTuple(args, "U", &mode)) {
return NULL;
}
if (strcmp(PyUnicode_AsUTF8(mode), "normal") == 0) {
PyImport_SetLazyImportsMode(PyImport_LAZY_NORMAL);
} else if (strcmp(PyUnicode_AsUTF8(mode), "all") == 0) {
PyImport_SetLazyImportsMode(PyImport_LAZY_ALL);
} else if (strcmp(PyUnicode_AsUTF8(mode), "none") == 0) {
PyImport_SetLazyImportsMode(PyImport_LAZY_NONE);
} else {
PyErr_SetString(PyExc_ValueError, "invalid mode");
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
pyimport_getlazyimportsmode(PyObject *self, PyObject *args)
{
switch (PyImport_GetLazyImportsMode()) {
case PyImport_LAZY_NORMAL:
return PyUnicode_FromString("normal");
case PyImport_LAZY_ALL:
return PyUnicode_FromString("all");
case PyImport_LAZY_NONE:
return PyUnicode_FromString("none");
default:
PyErr_SetString(PyExc_ValueError, "unknown mode");
return NULL;
}
}
static PyObject *
pyimport_setlazyimportsfilter(PyObject *self, PyObject *args)
{
PyObject *filter;
if (!PyArg_ParseTuple(args, "O", &filter)) {
return NULL;
}
if (PyImport_SetLazyImportsFilter(filter) < 0) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
pyimport_getlazyimportsfilter(PyObject *self, PyObject *args)
{
PyObject *res = PyImport_GetLazyImportsFilter();
if (res == NULL) {
Py_RETURN_NONE;
}
return res;
}
static PyMethodDef test_methods[] = {
{"PyImport_ImportModuleAttr", pyimport_importmoduleattr, METH_VARARGS},
{"PyImport_ImportModuleAttrString", pyimport_importmoduleattrstring, METH_VARARGS},
{"PyImport_SetLazyImportsMode", pyimport_setlazyimportsmode, METH_VARARGS},
{"PyImport_GetLazyImportsMode", pyimport_getlazyimportsmode, METH_NOARGS},
{"PyImport_SetLazyImportsFilter", pyimport_setlazyimportsfilter, METH_VARARGS},
{"PyImport_GetLazyImportsFilter", pyimport_getlazyimportsfilter, METH_NOARGS},
{NULL},
};
int
_PyTestCapi_Init_Import(PyObject *m)
{
return PyModule_AddFunctions(m, test_methods);
}