copapy/src/coparun/coparun_module.c

46 lines
1.4 KiB
C
Raw Normal View History

2025-10-03 20:49:07 +00:00
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "runmem.h"
2025-10-03 20:49:07 +00:00
/*
* coparun(PyObject *self, PyObject *args)
* Accepts a Python `bytes` (or objects supporting the buffer protocol).
* We use the "y#" format in PyArg_ParseTuple which returns a pointer to
* the internal bytes buffer and its length (Py_ssize_t). For safety and
* performance we pass that pointer directly to parse_commands which expects
* a uint8_t* buffer. If parse_commands needs the length, consider
* extending its API to accept a length parameter.
*/
static PyObject* coparun(PyObject* self, PyObject* args) {
const char *buf;
Py_ssize_t buf_len;
int result;
if (!PyArg_ParseTuple(args, "y#", &buf, &buf_len)) {
return NULL; /* TypeError set by PyArg_ParseTuple */
2025-10-03 20:49:07 +00:00
}
/* If parse_commands may run for a long time, release the GIL. */
Py_BEGIN_ALLOW_THREADS
result = parse_commands((uint8_t*)buf);
Py_END_ALLOW_THREADS
return PyLong_FromLong(result);
2025-10-03 20:49:07 +00:00
}
static PyMethodDef MyMethods[] = {
{"coparun", coparun, METH_VARARGS, "Pass raw command data to coparun"},
{NULL, NULL, 0, NULL}
2025-10-03 20:49:07 +00:00
};
static struct PyModuleDef coparun_module = {
2025-10-03 20:49:07 +00:00
PyModuleDef_HEAD_INIT,
"coparun_module", // Module name
2025-10-03 20:49:07 +00:00
NULL, // Documentation
-1, // Size of per-interpreter state (-1 for global)
MyMethods
};
PyMODINIT_FUNC PyInit_coparun_module(void) {
return PyModule_Create(&coparun_module);
2025-10-03 20:49:07 +00:00
}