python: add multiphase module init support and error check (#93)
This commit is contained in:
parent
39de9d651d
commit
98c36e5f23
@ -15,24 +15,28 @@ static PyObject *ruapu_supports_py(PyObject *self, PyObject *args, PyObject *kwa
|
|||||||
Py_RETURN_FALSE;
|
Py_RETURN_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *get_isa_items_py(PyObject *self, PyObject *args, PyObject *kwargs)
|
static PyObject *get_isa_items_py(PyObject *self, PyObject *Py_UNUSED(args))
|
||||||
{
|
{
|
||||||
const char* const* isa_supported = ruapu_rua();
|
const char* const* isa_supported = ruapu_rua();
|
||||||
|
const char* const* old_ptr = isa_supported;
|
||||||
int total = 0;
|
int total = 0;
|
||||||
while(*isa_supported)
|
while(*isa_supported)
|
||||||
{
|
{
|
||||||
total++;
|
total++;
|
||||||
isa_supported++;
|
isa_supported++;
|
||||||
}
|
}
|
||||||
|
|
||||||
isa_supported = ruapu_rua();
|
|
||||||
PyObject* supported_isa_py = PyTuple_New(total);
|
PyObject* supported_isa_py = PyTuple_New(total);
|
||||||
|
if(supported_isa_py==NULL)
|
||||||
int tuple_idx = 0;
|
|
||||||
while(*isa_supported)
|
|
||||||
{
|
{
|
||||||
PyTuple_SetItem(supported_isa_py, tuple_idx++, PyUnicode_FromString(*isa_supported));
|
return PyErr_NoMemory();
|
||||||
isa_supported++;
|
}
|
||||||
|
|
||||||
|
Py_ssize_t tuple_idx = 0;
|
||||||
|
while(*old_ptr)
|
||||||
|
{
|
||||||
|
PyTuple_SetItem(supported_isa_py, tuple_idx++, PyUnicode_FromString(*old_ptr));
|
||||||
|
old_ptr++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return supported_isa_py;
|
return supported_isa_py;
|
||||||
@ -40,22 +44,34 @@ static PyObject *get_isa_items_py(PyObject *self, PyObject *args, PyObject *kwar
|
|||||||
|
|
||||||
static PyMethodDef ruapu_methods[] =
|
static PyMethodDef ruapu_methods[] =
|
||||||
{
|
{
|
||||||
{"supports", ruapu_supports_py, METH_VARARGS | METH_KEYWORDS, "Check if the CPU supports an instruction set"},
|
{"supports", (PyCFunction)ruapu_supports_py, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("Check if the CPU supports an instruction set")},
|
||||||
{"rua", get_isa_items_py, METH_VARARGS | METH_KEYWORDS, "Get the instruction sets supported by the current CPU"},
|
{"rua", (PyCFunction)get_isa_items_py, METH_NOARGS, PyDoc_STR("Get the instruction sets supported by the current CPU")},
|
||||||
{NULL, NULL, 0, NULL}
|
{NULL, NULL, 0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static int
|
||||||
|
ruapu_exec(PyObject *mod)
|
||||||
|
{
|
||||||
|
ruapu_init();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PyModuleDef_Slot ruapu_slots[] = {
|
||||||
|
{Py_mod_exec, ruapu_exec},
|
||||||
|
{0, NULL}
|
||||||
|
};
|
||||||
|
/* PEP 489 – Multi-phase extension module */
|
||||||
static struct PyModuleDef ruapu_module =
|
static struct PyModuleDef ruapu_module =
|
||||||
{
|
{
|
||||||
PyModuleDef_HEAD_INIT,
|
PyModuleDef_HEAD_INIT,
|
||||||
"ruapu",
|
"ruapu",
|
||||||
"ruapu module",
|
PyDoc_STR("ruapu module"),
|
||||||
-1,
|
0,
|
||||||
ruapu_methods
|
ruapu_methods,
|
||||||
|
ruapu_slots
|
||||||
};
|
};
|
||||||
|
|
||||||
PyMODINIT_FUNC PyInit_ruapu(void)
|
PyMODINIT_FUNC PyInit_ruapu(void)
|
||||||
{
|
{
|
||||||
ruapu_init();
|
return PyModuleDef_Init(&ruapu_module);
|
||||||
return PyModule_Create(&ruapu_module);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user