????1??python??????????
??????python?У??????Щ???????????????????????????飬????????????C??????????????????????漸???????
????· init???????????????init+????????????????????????飬????????????е????????????????????????????????????????????????е???ε?????????????????????????????
????· ??????鷽????????????????static?????PyMethodDef???????????????????ж?????????
????· C???????壬??Щ????????????????з????????????
????· ???????ж????????????????????????????????????
????2?????python?????????
????????????????????????????????????????????飬?????ù?????????????????????????python?????飬??????????????????python??????????????
// spam.c
1 #include "Python.h"
2
3 static PyObject *SpamError;
4
5 static PyObject *
6     spam_system(PyObject *self?? PyObject *args)
7 {
8     const char *command;
9     int sts;
10
11     if (!PyArg_ParseTuple(args?? "s"?? &command))
12         return NULL;
13     sts = system(command);
14     if (sts < 0) {
15         PyErr_SetString(SpamError?? "System command failed");
16         return NULL;
17     }
18     return PyLong_FromLong(sts);
19 }
20
21 static PyMethodDef SpamMethods[] = {
22     {"system"??  spam_system?? METH_VARARGS??
23     "Execute a shell command."}??
24     {NULL?? NULL?? 0?? NULL}        /* Sentinel */
25 };
26
27 PyMODINIT_FUNC
28     initspam(void)
29 {
30     PyObject *m;
31
32     m = Py_InitModule("spam"?? SpamMethods);
33     if (m == NULL)
34         return;
35
36     SpamError = PyErr_NewException("spam.error"?? NULL?? NULL);
37     Py_INCREF(SpamError);
38     PyModule_AddObject(m?? "error"?? SpamError);
39 }
?????????initspam?????????????????????????????Py_InitModule?????????????spam????飬???????????????SpamMethods??????????????и????system????????????????c/c++?????spam_system????????spam_system??????????????????system??????д?python?????????????????????????????????python??????????????????????????????????????????python????import?????????????????????Windows?μ???vs?????У???????vs??????dll?????????????1??????????????????python????漰????????·???????????????????£?????VC++????????include??lib·???????????????????????????????python27.lib??

???????úú????????????????????????dll???????????pyd??????????python???????import???????????????????????????????

????3?????python???????ж?????
????????????????????ж??庯???????????????????????????????????ж??????????????????????????????????£?
// spam.c
1 #include "Python.h"
2
3 static PyObject *SpamError;
4
5 static PyObject *
6     spam_system(PyObject *self?? PyObject *args)
7 {
8     const char *command;
9     int sts;
10
11     if (!PyArg_ParseTuple(args?? "s"?? &command))
12         return NULL;
13     sts = system(command);
14     if (sts < 0) {
15         PyErr_SetString(SpamError?? "System command failed");
16         return NULL;
17     }
18     return PyLong_FromLong(sts);
19 }
20
21 static PyMethodDef SpamMethods[] = {
22     {"system"??  spam_system?? METH_VARARGS??
23     "Execute a shell command."}??
24     {NULL?? NULL?? 0?? NULL}        /* Sentinel */
25 };
26
27 PyTypeObject *SpamType = NULL;
28
29 PyMODINIT_FUNC
30     initspam(void)
31 {
32     static PyTypeObject _SpamType = {
33         PyObject_HEAD_INIT(NULL)
34         0??                                // ob_size
35         "spam.Spam"??                // tp_name
36         sizeof(PyObject)??                // tp_basicsize
37         0??                                // tp_itemsize
38         0??        // tp_dealloc
39         0??                                // tp_print
40         0??                                // tp_getattr
41         0??                                // tp_setattr
42         0??                                // tp_compare
43         0??            // tp_repr
44         0??                                // tp_as_number
45         0??                                // tp_as_sequence
46         0??                                // tp_as_mapping
47         0??                                // tp_hash
48         0??                                // tp_call
49         0??            // tp_str
50         0??                                // tp_getattro
51         0??                                // tp_setattro
52         0??                                // tp_as_buffer
53         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE ??    // tp_flags
54         0??                    // tp_doc
55         0??                                // tp_traverse
56         0??                                // tp_clear
57         0??                                // tp_richcompare
58         0??                                // tp_weaklistoffset
59         0??                                // tp_iter
60         0??                                // tp_iternext
61         SpamMethods??                    // tp_methods
62         0??                                // tp_members
63         0??                // tp_getset
64         0??                                // tp_base
65         0??                                // tp_dict
66         0??                                // tp_descr_get
67         0??                                // tp_descr_set
68         0??                                // tp_dictoffset
69         0??            // tp_init
70         0??                                // tp_alloc
71         PyType_GenericNew??                // tp_new
72     };
73
74     PyObject *m;
75
76     m = Py_InitModule("spam"?? NULL);
77     if (m == NULL)
78         return;
79     if (PyType_Ready(&_SpamType) < 0)
80         return;
81     SpamType = &_SpamType;
82     Py_INCREF(SpamType);
83     PyModule_AddObject(m?? "Spam"?? (PyObject*)SpamType);
84     SpamError = PyErr_NewException("spam.error"?? NULL?? NULL);
85     Py_INCREF(SpamError);
86     PyModule_AddObject(m?? "error"?? SpamError);
87 }