Repository navigation
Expand file tree
/
Copy pathextensibletype.h
More file actions
170 lines (150 loc) · 4.65 KB
/
Copy pathextensibletype.h
File metadata and controls
170 lines (150 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef Py_EXTENSIBLETYPE_H
#define Py_EXTENSIBLETYPE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "customslots.h"
/*
The metaclass definition. Do not use directly, but instead call
PyExtensibleType_Import.
*/
static PyObject *_PyExtensibleType_new(PyTypeObject *t, PyObject *a, PyObject *k) {
PyHeapExtensibleTypeObject* new_type, *base_type;
PyObject *o = (*PyType_Type.tp_new)(t, a, k);
if (!o) return 0;
new_type = (PyHeapExtensibleTypeObject*)o;
base_type = (PyHeapExtensibleTypeObject*)((PyTypeObject*)o)->tp_base;
new_type->etp_custom_slot_count = base_type->etp_custom_slot_count;
new_type->etp_custom_slot_table = base_type->etp_custom_slot_table;
((PyTypeObject*)new_type)->tp_flags |= PyExtensibleType_TPFLAGS_IS_EXTENSIBLE;
return o;
}
static PyTypeObject _PyExtensibleType_Type_Candidate = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
#if PY_VERSION_HEX < 0x02050000
(char *)"extensibletype", /*tp_name*/
#else
"extensibletype", /*tp_name*/
#endif
sizeof(PyHeapExtensibleTypeObject), /* tp_basicsize */
sizeof(PyMemberDef), /* tp_itemsize */
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
#if PY_MAJOR_VERSION < 3
0, /*tp_compare*/
#else
0, /*reserved*/
#endif
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_TYPE_SUBCLASS, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
0, /*tp_methods*/
0, /*tp_members*/
0, /*tp_getset*/
&PyType_Type, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
0, /*tp_init*/
0, /*tp_alloc*/
&_PyExtensibleType_new, /*tp_new*/
0, /*tp_free*/
0, /*tp_is_gc*/
0, /*tp_bases*/
0, /*tp_mro*/
0, /*tp_cache*/
0, /*tp_subclasses*/
0, /*tp_weaklist*/
0, /*tp_del*/
#if PY_VERSION_HEX >= 0x02060000
0, /*tp_version_tag*/
#endif
};
static PyTypeObject *PyExtensibleType_TypePtr = 0;
static PyTypeObject *
PyExtensibleType_Import(void) {
/* Performs roughly the equivalent of:
d = sys.modules.setdefault('_extensibletype', {})
return d.setdefault('extensibletype', our_extensibletype);
If another module got to sys.modules first, the
static PyExtensibleType_Type defined above is left unused.
*/
PyObject *module = 0;
PyObject *extensibletype = 0;
PyTypeObject *retval;
if (PyExtensibleType_TypePtr != 0) {
return PyExtensibleType_TypePtr;
}
module = PyImport_AddModule("_extensibletype"); /* borrowed ref */
if (!module) goto bad;
if (PyObject_HasAttrString(module, "extensibletype_v1")) {
extensibletype = PyObject_GetAttrString(module, "extensibletype_v1");
if (!extensibletype) goto bad;
if (!PyType_Check(extensibletype) ||
((PyTypeObject*)extensibletype)->tp_basicsize !=
sizeof(PyHeapExtensibleTypeObject)) {
PyErr_SetString(PyExc_TypeError,
"'extensibletype' found but is wrong type or size");
}
retval = (PyTypeObject*)extensibletype;
} else {
/* not found; create it */
if (PyType_Ready(&_PyExtensibleType_Type_Candidate) < 0) goto bad;
if (PyObject_SetAttrString(module, "extensibletype_v1",
(PyObject*)&_PyExtensibleType_Type_Candidate) < 0) goto bad;
retval = (PyTypeObject*)&_PyExtensibleType_Type_Candidate;
Py_INCREF((PyObject*)retval);
}
/* Initialize the global variable used in macros */
PyExtensibleType_TypePtr = retval;
goto ret;
bad:
retval = NULL;
ret:
/* module is borrowed */
Py_XDECREF(extensibletype);
return retval;
}
static int
PyExtensibleType_Ready(PyHeapExtensibleTypeObject *type,
Py_ssize_t slot_table_size) {
PyTypeObject *base = ((PyTypeObject*)type)->tp_base;
if (base != 0 && (base->tp_flags & PyExtensibleType_TPFLAGS_IS_EXTENSIBLE)) {
PyErr_SetString(PyExc_NotImplementedError,
"PyExtensibleType_Ready: Support for subclasses not "
"implemented yet");
return -1;
}
/* Import metaclass and assign it to ob_type */
PyTypeObject *metaclass = PyExtensibleType_Import();
if (!metaclass) return -1;
Py_INCREF(metaclass);
((PyObject*)type)->ob_type = metaclass;
/* PyType_Ready */
if (PyType_Ready((PyTypeObject*)type) < 0) return -1;
/* Set flag bit */
((PyTypeObject*)type)->tp_flags |= PyExtensibleType_TPFLAGS_IS_EXTENSIBLE;
return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* !Py_EXTENSIBLETYPE_H */