-
Notifications
You must be signed in to change notification settings - Fork 0
/
spammodule.c
45 lines (38 loc) · 834 Bytes
/
spammodule.c
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
#include <Python.h>
static PyObject * SpamError;
static PyObject *
spam_system(PyObject *self, PyObject * args)
{
const char *command;
int sts;
if(!PyArg_ParseTuple(args, "s", &command))
return NULL;
sts = system(command);
if ( sts < 0 ) {
PyErr_SetString(SpamError, "System command failed");
return NULL;
}
return PyLong_FromLong(sts);
}
PyMODINIT_FUNC
initspam(void)
{
PyObject *m;
static PyMethodDef SpamMethods[] = {
{"system", spam_system, METH_VARARGS, "Execute a shell command."},
{NULL, NULL, 0, NULL}
};
m = Py_InitModule("spam", SpamMethods);
if ( m == NULL )
return;
SpamError = PyErr_NewException("spam.error", NULL, NULL);
Py_INCREF(SpamError);
PyModule_AddObject(m, "error", SpamError);
}
int
main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]);
Py_Initialize();
initspam();
}