| 1 |
#include <Python.h> |
| 2 |
#include <stdio.h> |
| 3 |
|
| 4 |
PyObject* foo3type_tp_new(PyTypeObject* metatype, PyObject* args, PyObject* kwds) |
| 5 |
{ |
| 6 |
PyObject* newType; |
| 7 |
newType = PyType_Type.tp_new(metatype, args, kwds); |
| 8 |
return newType; |
| 9 |
} |
| 10 |
|
| 11 |
PyObject * typ = NULL; |
| 12 |
PyObject* datetimetype_tp_new(PyTypeObject* metatype, PyObject* args, PyObject* kwds) |
| 13 |
{ |
| 14 |
PyObject* newType; |
| 15 |
if (typ == NULL) |
| 16 |
{ |
| 17 |
PyErr_Format(PyExc_TypeError, "could not import datetime.datetime"); |
| 18 |
return NULL; |
| 19 |
} |
| 20 |
newType = ((PyTypeObject*)typ)->tp_new(metatype, args, kwds); |
| 21 |
return newType; |
| 22 |
} |
| 23 |
|
| 24 |
void datetimetype_tp_dealloc(PyObject* self) |
| 25 |
{ |
| 26 |
return ((PyTypeObject*)typ)->tp_dealloc(self); |
| 27 |
} |
| 28 |
|
| 29 |
#define BASEFLAGS Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 30 |
|
| 31 |
PyTypeObject footype = { |
| 32 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 33 |
"foo3.footype", |
| 34 |
sizeof(PyTypeObject), |
| 35 |
0, |
| 36 |
0, |
| 37 |
0, |
| 38 |
0, |
| 39 |
0, |
| 40 |
0, |
| 41 |
0, |
| 42 |
0, |
| 43 |
0, |
| 44 |
0, |
| 45 |
0, |
| 46 |
0, |
| 47 |
0, |
| 48 |
0, |
| 49 |
0, |
| 50 |
0, |
| 51 |
BASEFLAGS, |
| 52 |
0, |
| 53 |
0, |
| 54 |
0, |
| 55 |
0, |
| 56 |
0, |
| 57 |
0, |
| 58 |
0, |
| 59 |
0, |
| 60 |
0, |
| 61 |
0, |
| 62 |
0, |
| 63 |
0, |
| 64 |
0, |
| 65 |
0, |
| 66 |
0, |
| 67 |
0, |
| 68 |
0, |
| 69 |
foo3type_tp_new, |
| 70 |
0, |
| 71 |
0, |
| 72 |
0, |
| 73 |
0, |
| 74 |
0, |
| 75 |
0, |
| 76 |
0 |
| 77 |
}; |
| 78 |
|
| 79 |
PyTypeObject datetimetype = { |
| 80 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 81 |
"foo3.datetimetype", |
| 82 |
sizeof(PyTypeObject), |
| 83 |
0, |
| 84 |
datetimetype_tp_dealloc, |
| 85 |
0, |
| 86 |
0, |
| 87 |
0, |
| 88 |
0, |
| 89 |
0, |
| 90 |
0, |
| 91 |
0, |
| 92 |
0, |
| 93 |
0, |
| 94 |
0, |
| 95 |
0, |
| 96 |
0, |
| 97 |
0, |
| 98 |
0, |
| 99 |
BASEFLAGS, |
| 100 |
0, |
| 101 |
0, |
| 102 |
0, |
| 103 |
0, |
| 104 |
0, |
| 105 |
0, |
| 106 |
0, |
| 107 |
0, |
| 108 |
0, |
| 109 |
0, |
| 110 |
0, |
| 111 |
0, |
| 112 |
0, |
| 113 |
0, |
| 114 |
0, |
| 115 |
0, |
| 116 |
0, |
| 117 |
datetimetype_tp_new, |
| 118 |
0, |
| 119 |
0, |
| 120 |
0, |
| 121 |
0, |
| 122 |
0, |
| 123 |
0, |
| 124 |
0 |
| 125 |
}; |
| 126 |
|
| 127 |
static PyMethodDef sbkMethods[] = {{NULL, NULL, 0, NULL}}; |
| 128 |
|
| 129 |
static struct PyModuleDef moduledef = { |
| 130 |
PyModuleDef_HEAD_INIT, |
| 131 |
"foo", |
| 132 |
"Module Doc", |
| 133 |
-1, |
| 134 |
sbkMethods, |
| 135 |
NULL, |
| 136 |
NULL, |
| 137 |
NULL, |
| 138 |
NULL, |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
#ifdef __GNUC__ |
| 143 |
extern __attribute__((visibility("default"))) |
| 144 |
#endif |
| 145 |
|
| 146 |
PyMODINIT_FUNC |
| 147 |
PyInit_foo3(void) |
| 148 |
{ |
| 149 |
PyObject *mod, *d; |
| 150 |
PyObject *module = NULL; |
| 151 |
module = PyImport_ImportModule("datetime"); |
| 152 |
typ = PyObject_GetAttrString(module, "datetime"); |
| 153 |
Py_DECREF(module); |
| 154 |
if (!PyType_Check(typ)) { |
| 155 |
PyErr_Format(PyExc_TypeError, "datetime.datetime is not a type object"); |
| 156 |
return NULL; |
| 157 |
} |
| 158 |
datetimetype.tp_base = (PyTypeObject*)typ; |
| 159 |
PyType_Ready(&datetimetype); |
| 160 |
footype.tp_base = &PyType_Type; |
| 161 |
PyType_Ready(&footype); |
| 162 |
mod = PyModule_Create(&moduledef); |
| 163 |
if (mod == NULL) |
| 164 |
return NULL; |
| 165 |
d = PyModule_GetDict(mod); |
| 166 |
if (d == NULL) |
| 167 |
return NULL; |
| 168 |
if (PyDict_SetItemString(d, "footype", (PyObject *)&footype) < 0) |
| 169 |
return NULL; |
| 170 |
if (PyDict_SetItemString(d, "datetimetype", (PyObject *)&datetimetype) < 0) |
| 171 |
return NULL; |
| 172 |
Py_INCREF(&footype); |
| 173 |
return mod; |
| 174 |
} |