Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1 | /* This extension module exposes some types that are only available at the |
| 2 | * C level. It should not be used directly, but instead through the Python |
| 3 | * level types modules, which imports this. |
| 4 | */ |
| 5 | |
| 6 | #include "Python.h" |
| 7 | #include "structmember.h" |
| 8 | |
| 9 | typedef struct |
| 10 | { |
| 11 | PyObject_HEAD |
| 12 | int member; |
| 13 | } Helper; |
| 14 | |
| 15 | static PyMemberDef helper_members[] = { |
| 16 | { "member", T_INT, offsetof(Helper, member), READONLY, |
| 17 | PyDoc_STR("A member descriptor") |
| 18 | }, |
| 19 | { NULL } |
| 20 | }; |
| 21 | |
| 22 | static PyObject * |
| 23 | helper_getter(Helper *self, void *unused) |
| 24 | { |
| 25 | Py_RETURN_NONE; |
| 26 | } |
| 27 | |
| 28 | static PyGetSetDef helper_getset[] = { |
| 29 | { "getter", (getter)helper_getter, NULL, |
| 30 | PyDoc_STR("A getset descriptor"), |
| 31 | }, |
| 32 | { NULL } |
| 33 | }; |
| 34 | |
| 35 | static PyTypeObject HelperType = { |
| 36 | PyObject_HEAD_INIT(NULL) |
| 37 | 0, /* ob_size */ |
| 38 | "_types.Helper", /* tp_name */ |
| 39 | sizeof(Helper), /* tp_basicsize */ |
| 40 | 0, /* tp_itemsize */ |
| 41 | 0, /* tp_dealloc */ |
| 42 | 0, /* tp_print */ |
| 43 | 0, /* tp_getattr */ |
| 44 | 0, /* tp_setattr */ |
| 45 | 0, /* tp_compare */ |
| 46 | 0, /* tp_repr */ |
| 47 | 0, /* tp_as_number */ |
| 48 | 0, /* tp_as_sequence */ |
| 49 | 0, /* tp_as_mapping */ |
| 50 | 0, /* tp_hash */ |
| 51 | 0, /* tp_call */ |
| 52 | 0, /* tp_str */ |
| 53 | 0, /* tp_getattro */ |
| 54 | 0, /* tp_setattro */ |
| 55 | 0, /* tp_as_buffer */ |
| 56 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 57 | 0, /* tp_doc */ |
| 58 | 0, /* tp_traverse */ |
| 59 | 0, /* tp_clear */ |
| 60 | 0, /* tp_richcompare */ |
| 61 | 0, /* tp_weaklistoffset */ |
| 62 | 0, /* tp_iter */ |
| 63 | 0, /* tp_iternext */ |
| 64 | 0, /* tp_methods */ |
| 65 | helper_members, /* tp_members */ |
| 66 | helper_getset, /* tp_getset */ |
| 67 | 0, /* tp_base */ |
| 68 | 0, /* tp_dict */ |
| 69 | 0, /* tp_descr_get */ |
| 70 | 0, /* tp_descr_set */ |
| 71 | 0, /* tp_dictoffset */ |
| 72 | 0, /* tp_init */ |
| 73 | 0, /* tp_alloc */ |
| 74 | 0, /* tp_new */ |
| 75 | 0, /* tp_free */ |
| 76 | }; |
| 77 | |
| 78 | PyMODINIT_FUNC |
| 79 | init_types(void) |
| 80 | { |
| 81 | PyObject *m; |
| 82 | |
| 83 | m = Py_InitModule3("_types", NULL, "A types module helper"); |
| 84 | if (!m) |
| 85 | return; |
| 86 | |
| 87 | if (PyType_Ready(&HelperType) < 0) |
| 88 | return; |
| 89 | |
| 90 | Py_INCREF(&HelperType); |
| 91 | PyModule_AddObject(m, "Helper", (PyObject *)&HelperType); |
| 92 | } |
| 93 | |
| 94 | |