1
+ #define PY_SSIZE_T_CLEAN
2
+ #include <Python/Python.h>
3
+
4
+ #define SPAM_MODULE
5
+ #include "spammodule.h"
6
+
7
+ // Method Table
8
+ static PyMethodDef SpamMethods [] = {
9
+ {"system" , spam_system , METH_VARARGS ,
10
+ "Execute a shell command." },
11
+ {NULL , NULL , 0 , NULL } /* Sentinel */
12
+ };
13
+
14
+ //// Error
15
+ //PyMODINIT_FUNC
16
+ //initspam(void)
17
+ //{
18
+ // PyObject *m;
19
+ //
20
+ // m = Py_InitModule("spam", SpamMethods);
21
+ // if (m == NULL)
22
+ // return;
23
+ //
24
+ // SpamError = PyErr_NewException("spam.error", NULL, NULL);
25
+ // Py_INCREF(SpamError);
26
+ // PyModule_AddObject(m, "error", SpamError);
27
+ //}
28
+
29
+ // Main
30
+ static PyObject *
31
+ spam_system (PyObject * self , PyObject * args )
32
+ {
33
+ const char * command ;
34
+ int sts ;
35
+
36
+ if (!PyArg_ParseTuple (args , "s" , & command ))
37
+ return NULL ;
38
+ sts = system (command );
39
+ // if (sts < 0) {
40
+ // PyErr_SetString(SpamError, "System command failed");
41
+ // return NULL;
42
+ // }
43
+ return Py_BuildValue ("i" , sts );
44
+ }
45
+
46
+ // Module definition
47
+ //static struct PyModuleDef spammodule = {
48
+ // PyModuleDef_HEAD_INIT,
49
+ // "spam", /* name of module */
50
+ // spam_doc, /* module documentation, may be NULL */
51
+ // -1, /* size of per-interpreter state of the module,
52
+ // or -1 if the module keeps state in global variables. */
53
+ // SpamMethods
54
+ //};
55
+
56
+ // Initialization function
57
+ //PyMODINIT_FUNC
58
+ //PyInit_spam(void)
59
+ //{
60
+ // return PyModule_Create(&spammodule);
61
+ //}
62
+
63
+ // Module definition and initializer
64
+ PyMODINIT_FUNC
65
+ initspam (void )
66
+ {
67
+ (void ) Py_InitModule ("spam" , SpamMethods );
68
+ }
69
+
70
+ // Initialization Table
71
+ int
72
+ main (int argc , char * argv [])
73
+ {
74
+ /* Pass argv[0] to the Python interpreter */
75
+ Py_SetProgramName (argv [0 ]);
76
+
77
+ /* Initialize the Python interpreter. Required. */
78
+ Py_Initialize ();
79
+
80
+ /* Add a static module */
81
+ initspam ();
82
+
83
+ }
0 commit comments