Debian Codegraph @

pypy/module/cpyext/test/issue2482.c

raw symbols SCIP
1
2 #include "Python.h"
3 //#define ISSUE_2482
4
5 typedef struct {
6 PyObject_HEAD
7 // Some extra storage:
8 char blank[500];
9 } instance;
10
11 static PyObject * get_basicsize(PyObject *self, PyObject * arg)
12 {
13 return PyLong_FromLong(((PyTypeObject*)arg)->tp_basicsize);
14 }
15
16 const char *name = "issue2482_object";
17 static
18 PyObject *make_object_base_type(void) {
19
20 PyHeapTypeObject *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
21
22 PyTypeObject *type = &heap_type->ht_type;
23 if (!heap_type) return NULL;
24 heap_type->ht_name = PyUnicode_FromString(name);
25 type->tp_name = name;
26 #ifdef ISSUE_2482
27 type->tp_base = &PyBaseObject_Type; /*fails */
28 #else
29 type->tp_base = &PyType_Type;
30 #endif
31 type->tp_basicsize = sizeof(instance);
32 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
33
34 if (PyType_Ready(type) < 0)
35 return NULL;
36
37 return (PyObject *) heap_type;
38 };
39
40 static PyMethodDef issue2482_functions[] = {
41 {"get_basicsize", (PyCFunction)get_basicsize, METH_O, NULL},
42 {NULL, NULL} /* Sentinel */
43 };
44
45 #if PY_MAJOR_VERSION >= 3
46 static struct PyModuleDef moduledef = {
47 PyModuleDef_HEAD_INIT,
48 "issue2482",
49 "Module Doc",
50 -1,
51 issue2482_functions,
52 NULL,
53 NULL,
54 NULL,
55 NULL,
56 };
57 #define INITERROR return NULL
58
59 /* Initialize this module. */
60 #ifdef __GNUC__
61 extern __attribute__((visibility("default")))
62 #else
63 extern __declspec(dllexport)
64 #endif
65
66 PyMODINIT_FUNC
67 PyInit_issue2482(void)
68
69 #else
70
71 #define INITERROR return
72
73 /* Initialize this module. */
74 #ifdef __GNUC__
75 extern __attribute__((visibility("default")))
76 #else
77 extern __declspec(dllexport)
78 #endif
79
80 PyMODINIT_FUNC
81 initissue2482(void)
82 #endif
83 {
84 #if PY_MAJOR_VERSION >= 3
85 PyObject *module = PyModule_Create(&moduledef);
86 #else
87 PyObject *module = Py_InitModule("issue2482", issue2482_functions);
88 #endif
89 PyHeapTypeObject *heap_type;
90 PyTypeObject *type;
91 PyObject * base;
92 if (module == NULL)
93 INITERROR;
94
95 heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
96 if (!heap_type) INITERROR;
97
98 type = &heap_type->ht_type;
99 type->tp_name = name;
100 heap_type->ht_name = PyUnicode_FromString(name);
101
102 base = make_object_base_type();
103 if (! base) INITERROR;
104 Py_INCREF(base);
105 type->tp_base = (PyTypeObject *) base;
106 type->tp_basicsize = ((PyTypeObject *) base)->tp_basicsize;
107 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
108
109 if (PyType_Ready(type) < 0) INITERROR;
110
111 PyModule_AddObject(module, name, (PyObject *) type);
112 #if PY_MAJOR_VERSION >= 3
113 return module;
114 #endif
115 };