-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_wrapper.c
132 lines (104 loc) · 3.79 KB
/
base_wrapper.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Base Python Wrapper
* ===================
*/
#include <Python.h>
#include <bytesobject.h>
#include "structmember.h"
#include <string.h>
#include "base.h"
#include "py_utils.h"
#include "protocol_wrapper.h"
#include "InternalMessage_wrapper.h"
#include "flags_wrapper.h"
#ifdef _cplusplus
extern "C" {
#endif
static PyMethodDef BaseMethods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef basemodule = {
PyModuleDef_HEAD_INIT,
"cbase", /* name of module */
"A C implementation of select features from the py2p.base module",/* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
BaseMethods
};
PyMODINIT_FUNC PyInit_cbase() {
PyObject *cbase, *flags_wrapper;
pmessage_wrapper_type.tp_new = PyType_GenericNew;
if (PyType_Ready(&pmessage_wrapper_type) < 0)
return NULL;
protocol_wrapper_type.tp_new = PyType_GenericNew;
if (PyType_Ready(&protocol_wrapper_type) < 0)
return NULL;
cbase = PyModule_Create(&basemodule);
if (cbase == NULL)
return NULL;
flags_wrapper = PyModule_Create(&flagsmodule);
if (flags_wrapper == NULL)
return NULL;
Py_INCREF(&protocol_wrapper_type);
PyModule_AddObject(cbase, "protocol", (PyObject *)&protocol_wrapper_type);
Py_INCREF(&pmessage_wrapper_type);
PyModule_AddObject(cbase, "InternalMessage", (PyObject *)&pmessage_wrapper_type);
addConstants(cbase, flags_wrapper);
return cbase;
}
int main(int argc, char *argv[]) {
#if PY_MINOR_VERSION >= 5
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
#else
size_t size = strlen(argv[0]) + 1;
wchar_t *program = (wchar_t *) malloc(sizeof(wchar_t) * size);
mbstowcs(program, argv[0], size);
#endif
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
PyImport_AppendInittab("cbase", PyInit_cbase);
Py_SetProgramName(program);
Py_Initialize();
PyImport_ImportModule("cbase");
#if PY_MINOR_VERSION >= 5
PyMem_RawFree(program);
#else
free(program);
#endif
return 0;
}
#else
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC initcbase() {
PyObject *cbase, *flags_wrapper;
pmessage_wrapper_type.tp_new = PyType_GenericNew;
if (PyType_Ready(&pmessage_wrapper_type) < 0)
return;
protocol_wrapper_type.tp_new = PyType_GenericNew;
if (PyType_Ready(&protocol_wrapper_type) < 0)
return;
cbase = Py_InitModule3("cbase", BaseMethods,
"C++ implementation of some base functions");
flags_wrapper = Py_InitModule3("flags", FlagsMethods,
"Storage container for protocol level flags");
Py_INCREF(&pmessage_wrapper_type);
PyModule_AddObject(cbase, "InternalMessage", (PyObject *)&pmessage_wrapper_type);
Py_INCREF(&protocol_wrapper_type);
PyModule_AddObject(cbase, "protocol", (PyObject *)&protocol_wrapper_type);
addConstants(cbase, flags_wrapper);
}
int main(int argc, char *argv[]) {
Py_SetProgramName(argv[0]);
Py_Initialize();
initcbase();
return 0;
}
#ifdef _cplusplus
}
#endif
#endif