Inada Naoki | c88fece | 2019-04-13 10:46:21 +0900 | [diff] [blame^] | 1 | #define PY_SSIZE_T_CLEAN |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 2 | #include <Python.h> |
| 3 | |
| 4 | typedef struct { |
| 5 | PyObject_HEAD |
| 6 | /* Type-specific fields go here. */ |
| 7 | } CustomObject; |
| 8 | |
| 9 | static PyTypeObject CustomType = { |
| 10 | PyVarObject_HEAD_INIT(NULL, 0) |
| 11 | .tp_name = "custom.Custom", |
| 12 | .tp_doc = "Custom objects", |
| 13 | .tp_basicsize = sizeof(CustomObject), |
| 14 | .tp_itemsize = 0, |
| 15 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 16 | .tp_new = PyType_GenericNew, |
| 17 | }; |
| 18 | |
| 19 | static PyModuleDef custommodule = { |
| 20 | PyModuleDef_HEAD_INIT, |
| 21 | .m_name = "custom", |
| 22 | .m_doc = "Example module that creates an extension type.", |
| 23 | .m_size = -1, |
| 24 | }; |
| 25 | |
| 26 | PyMODINIT_FUNC |
| 27 | PyInit_custom(void) |
| 28 | { |
| 29 | PyObject *m; |
| 30 | if (PyType_Ready(&CustomType) < 0) |
| 31 | return NULL; |
| 32 | |
| 33 | m = PyModule_Create(&custommodule); |
| 34 | if (m == NULL) |
| 35 | return NULL; |
| 36 | |
| 37 | Py_INCREF(&CustomType); |
| 38 | PyModule_AddObject(m, "Custom", (PyObject *) &CustomType); |
| 39 | return m; |
| 40 | } |