|
1 | 1 | #include <Python.h> |
2 | 2 |
|
3 | | -static PyTypeObject noddy_NoddyType; |
4 | | - |
5 | 3 | typedef struct { |
6 | 4 | PyObject_HEAD |
7 | 5 | /* Type-specific fields go here. */ |
8 | 6 | } noddy_NoddyObject; |
9 | 7 |
|
10 | | -static PyObject* |
11 | | -noddy_new_noddy(PyObject* self, PyObject* args) |
12 | | -{ |
13 | | - noddy_NoddyObject* noddy; |
14 | | - |
15 | | - noddy = PyObject_New(noddy_NoddyObject, &noddy_NoddyType); |
16 | | - |
17 | | - /* Initialize type-specific fields here. */ |
18 | | - |
19 | | - return (PyObject*)noddy; |
20 | | -} |
21 | | - |
22 | | -static void |
23 | | -noddy_noddy_dealloc(PyObject* self) |
24 | | -{ |
25 | | - /* Free any external resources here; |
26 | | - * if the instance owns references to any Python |
27 | | - * objects, call Py_DECREF() on them here. |
28 | | - */ |
29 | | - PyObject_Del(self); |
30 | | -} |
31 | | - |
32 | 8 | static PyTypeObject noddy_NoddyType = { |
33 | 9 | PyObject_HEAD_INIT(NULL) |
34 | | - 0, |
35 | | - "Noddy", |
36 | | - sizeof(noddy_NoddyObject), |
37 | | - 0, |
38 | | - noddy_noddy_dealloc, /*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 */ |
| 10 | + 0, /*ob_size*/ |
| 11 | + "noddy.Noddy", /*tp_name*/ |
| 12 | + sizeof(noddy_NoddyObject), /*tp_basicsize*/ |
| 13 | + 0, /*tp_itemsize*/ |
| 14 | + 0, /*tp_dealloc*/ |
| 15 | + 0, /*tp_print*/ |
| 16 | + 0, /*tp_getattr*/ |
| 17 | + 0, /*tp_setattr*/ |
| 18 | + 0, /*tp_compare*/ |
| 19 | + 0, /*tp_repr*/ |
| 20 | + 0, /*tp_as_number*/ |
| 21 | + 0, /*tp_as_sequence*/ |
| 22 | + 0, /*tp_as_mapping*/ |
| 23 | + 0, /*tp_hash */ |
| 24 | + 0, /*tp_call*/ |
| 25 | + 0, /*tp_str*/ |
| 26 | + 0, /*tp_getattro*/ |
| 27 | + 0, /*tp_setattro*/ |
| 28 | + 0, /*tp_as_buffer*/ |
| 29 | + Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 30 | + "Noddy objects", /* tp_doc */ |
| 31 | + 0, /* tp_traverse */ |
| 32 | + 0, /* tp_clear */ |
| 33 | + 0, /* tp_richcompare */ |
| 34 | + 0, /* tp_weaklistoffset */ |
| 35 | + 0, /* tp_iter */ |
| 36 | + 0, /* tp_iternext */ |
| 37 | + 0, /* tp_methods */ |
| 38 | + 0, /* tp_members */ |
| 39 | + 0, /* tp_getset */ |
| 40 | + 0, /* tp_base */ |
| 41 | + 0, /* tp_dict */ |
| 42 | + 0, /* tp_descr_get */ |
| 43 | + 0, /* tp_descr_set */ |
| 44 | + 0, /* tp_dictoffset */ |
| 45 | + 0, /* tp_init */ |
| 46 | + 0, /* tp_alloc */ |
| 47 | + PyType_GenericNew, /* tp_new */ |
48 | 48 | }; |
49 | 49 |
|
50 | 50 | static PyMethodDef noddy_methods[] = { |
51 | | - {"new_noddy", noddy_new_noddy, METH_NOARGS, |
52 | | - "Create a new Noddy object."}, |
53 | | - |
54 | 51 | {NULL} /* Sentinel */ |
55 | 52 | }; |
56 | 53 |
|
57 | 54 | PyMODINIT_FUNC |
58 | 55 | initnoddy(void) |
59 | 56 | { |
60 | | - noddy_NoddyType.ob_type = &PyType_Type; |
61 | | - if (PyType_Ready(&noddy_NoddyType)) |
| 57 | + PyObject* m; |
| 58 | + |
| 59 | + if (PyType_Ready(&noddy_NoddyType) < 0) |
62 | 60 | return; |
63 | 61 |
|
64 | | - Py_InitModule3("noddy", noddy_methods |
65 | | - "Example module that creates an extension type."); |
| 62 | + m = Py_InitModule3("noddy", noddy_methods, |
| 63 | + "Example module that creates an extension type."); |
| 64 | + |
| 65 | + PyModule_AddObject(m, "Noddy", (PyObject *)&noddy_NoddyType); |
66 | 66 | } |
0 commit comments