-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspam.c
More file actions
81 lines (66 loc) · 1.5 KB
/
spam.c
File metadata and controls
81 lines (66 loc) · 1.5 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
#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);
}
static PyMethodDef SpamMethods[] = {
{"system", spam_system, METH_VARARGS,
"Execute a shell command."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef spammodule = {
PyModuleDef_HEAD_INIT,
"spam", /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
SpamMethods
};
#endif
PyMODINIT_FUNC
#if PY_MAJOR_VERSION >= 3
PyInit_spam(void)
#else
initspam(void)
#endif
{
PyObject *m;
#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&spammodule);
#else
m = Py_InitModule("spam", SpamMethods);
#endif
if ( m == NULL )
#if PY_MAJOR_VERSION >= 3
return NULL;
#else
return;
#endif
SpamError = PyErr_NewException("spam.error", NULL, NULL);
Py_INCREF(SpamError);
PyModule_AddObject(m, "error", SpamError);
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}
#if PY_MAJOR_VERSION < 3
int
main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]);
Py_Initialize();
initspam();
}
#endif