Fred Drake | 2ab0a10 | 2002-03-28 23:32:53 +0000 | [diff] [blame] | 1 | #include <Python.h> |
| 2 | |
| 3 | staticforward PyTypeObject noddy_NoddyType; |
| 4 | |
| 5 | typedef struct { |
| 6 | PyObject_HEAD |
Fred Drake | ee48519 | 2002-04-12 16:17:06 +0000 | [diff] [blame] | 7 | /* Type-specific fields go here. */ |
Fred Drake | 2ab0a10 | 2002-03-28 23:32:53 +0000 | [diff] [blame] | 8 | } noddy_NoddyObject; |
| 9 | |
| 10 | static PyObject* |
| 11 | noddy_new_noddy(PyObject* self, PyObject* args) |
| 12 | { |
| 13 | noddy_NoddyObject* noddy; |
| 14 | |
Fred Drake | 2ab0a10 | 2002-03-28 23:32:53 +0000 | [diff] [blame] | 15 | noddy = PyObject_New(noddy_NoddyObject, &noddy_NoddyType); |
| 16 | |
Fred Drake | ee48519 | 2002-04-12 16:17:06 +0000 | [diff] [blame] | 17 | /* Initialize type-specific fields here. */ |
| 18 | |
Fred Drake | 2ab0a10 | 2002-03-28 23:32:53 +0000 | [diff] [blame] | 19 | return (PyObject*)noddy; |
| 20 | } |
| 21 | |
| 22 | static void |
| 23 | noddy_noddy_dealloc(PyObject* self) |
| 24 | { |
Fred Drake | ee48519 | 2002-04-12 16:17:06 +0000 | [diff] [blame] | 25 | /* Free any external resources here; |
| 26 | * if the instance owns references to any Python |
| 27 | * objects, call Py_DECREF() on them here. |
| 28 | */ |
Fred Drake | 2ab0a10 | 2002-03-28 23:32:53 +0000 | [diff] [blame] | 29 | PyObject_Del(self); |
| 30 | } |
| 31 | |
Fred Drake | ee48519 | 2002-04-12 16:17:06 +0000 | [diff] [blame] | 32 | statichere PyTypeObject noddy_NoddyType = { |
Fred Drake | 2ab0a10 | 2002-03-28 23:32:53 +0000 | [diff] [blame] | 33 | 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 */ |
| 48 | }; |
| 49 | |
| 50 | static PyMethodDef noddy_methods[] = { |
Fred Drake | ee48519 | 2002-04-12 16:17:06 +0000 | [diff] [blame] | 51 | {"new_noddy", noddy_new_noddy, METH_NOARGS, |
Fred Drake | 2ab0a10 | 2002-03-28 23:32:53 +0000 | [diff] [blame] | 52 | "Create a new Noddy object."}, |
Fred Drake | ee48519 | 2002-04-12 16:17:06 +0000 | [diff] [blame] | 53 | |
| 54 | {NULL} /* Sentinel */ |
Fred Drake | 2ab0a10 | 2002-03-28 23:32:53 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | DL_EXPORT(void) |
| 58 | initnoddy(void) |
| 59 | { |
| 60 | noddy_NoddyType.ob_type = &PyType_Type; |
Fred Drake | ee48519 | 2002-04-12 16:17:06 +0000 | [diff] [blame] | 61 | if (PyType_Ready(&noddy_NoddyType)) |
| 62 | return; |
Fred Drake | 2ab0a10 | 2002-03-28 23:32:53 +0000 | [diff] [blame] | 63 | |
Fred Drake | ee48519 | 2002-04-12 16:17:06 +0000 | [diff] [blame] | 64 | Py_InitModule3("noddy", noddy_methods |
| 65 | "Example module that creates an extension type."); |
Fred Drake | 2ab0a10 | 2002-03-28 23:32:53 +0000 | [diff] [blame] | 66 | } |