]>
Commit | Line | Data |
---|---|---|
53b2ba57 DM |
1 | /* Type object implementation */\r |
2 | \r | |
3 | #include "Python.h"\r | |
4 | #include "structmember.h"\r | |
5 | \r | |
6 | #include <ctype.h>\r | |
7 | \r | |
8 | \r | |
9 | /* Support type attribute cache */\r | |
10 | \r | |
11 | /* The cache can keep references to the names alive for longer than\r | |
12 | they normally would. This is why the maximum size is limited to\r | |
13 | MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large\r | |
14 | strings are used as attribute names. */\r | |
15 | #define MCACHE_MAX_ATTR_SIZE 100\r | |
16 | #define MCACHE_SIZE_EXP 10\r | |
17 | #define MCACHE_HASH(version, name_hash) \\r | |
18 | (((unsigned int)(version) * (unsigned int)(name_hash)) \\r | |
19 | >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP))\r | |
20 | #define MCACHE_HASH_METHOD(type, name) \\r | |
21 | MCACHE_HASH((type)->tp_version_tag, \\r | |
22 | ((PyStringObject *)(name))->ob_shash)\r | |
23 | #define MCACHE_CACHEABLE_NAME(name) \\r | |
24 | PyString_CheckExact(name) && \\r | |
25 | PyString_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE\r | |
26 | \r | |
27 | struct method_cache_entry {\r | |
28 | unsigned int version;\r | |
29 | PyObject *name; /* reference to exactly a str or None */\r | |
30 | PyObject *value; /* borrowed */\r | |
31 | };\r | |
32 | \r | |
33 | static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];\r | |
34 | static unsigned int next_version_tag = 0;\r | |
35 | \r | |
36 | unsigned int\r | |
37 | PyType_ClearCache(void)\r | |
38 | {\r | |
39 | Py_ssize_t i;\r | |
40 | unsigned int cur_version_tag = next_version_tag - 1;\r | |
41 | \r | |
42 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {\r | |
43 | method_cache[i].version = 0;\r | |
44 | Py_CLEAR(method_cache[i].name);\r | |
45 | method_cache[i].value = NULL;\r | |
46 | }\r | |
47 | next_version_tag = 0;\r | |
48 | /* mark all version tags as invalid */\r | |
49 | PyType_Modified(&PyBaseObject_Type);\r | |
50 | return cur_version_tag;\r | |
51 | }\r | |
52 | \r | |
53 | void\r | |
54 | PyType_Modified(PyTypeObject *type)\r | |
55 | {\r | |
56 | /* Invalidate any cached data for the specified type and all\r | |
57 | subclasses. This function is called after the base\r | |
58 | classes, mro, or attributes of the type are altered.\r | |
59 | \r | |
60 | Invariants:\r | |
61 | \r | |
62 | - Py_TPFLAGS_VALID_VERSION_TAG is never set if\r | |
63 | Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type\r | |
64 | objects coming from non-recompiled extension modules)\r | |
65 | \r | |
66 | - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,\r | |
67 | it must first be set on all super types.\r | |
68 | \r | |
69 | This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a\r | |
70 | type (so it must first clear it on all subclasses). The\r | |
71 | tp_version_tag value is meaningless unless this flag is set.\r | |
72 | We don't assign new version tags eagerly, but only as\r | |
73 | needed.\r | |
74 | */\r | |
75 | PyObject *raw, *ref;\r | |
76 | Py_ssize_t i, n;\r | |
77 | \r | |
78 | if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))\r | |
79 | return;\r | |
80 | \r | |
81 | raw = type->tp_subclasses;\r | |
82 | if (raw != NULL) {\r | |
83 | n = PyList_GET_SIZE(raw);\r | |
84 | for (i = 0; i < n; i++) {\r | |
85 | ref = PyList_GET_ITEM(raw, i);\r | |
86 | ref = PyWeakref_GET_OBJECT(ref);\r | |
87 | if (ref != Py_None) {\r | |
88 | PyType_Modified((PyTypeObject *)ref);\r | |
89 | }\r | |
90 | }\r | |
91 | }\r | |
92 | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;\r | |
93 | }\r | |
94 | \r | |
95 | static void\r | |
96 | type_mro_modified(PyTypeObject *type, PyObject *bases) {\r | |
97 | /*\r | |
98 | Check that all base classes or elements of the mro of type are\r | |
99 | able to be cached. This function is called after the base\r | |
100 | classes or mro of the type are altered.\r | |
101 | \r | |
102 | Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type\r | |
103 | inherits from an old-style class, either directly or if it\r | |
104 | appears in the MRO of a new-style class. No support either for\r | |
105 | custom MROs that include types that are not officially super\r | |
106 | types.\r | |
107 | \r | |
108 | Called from mro_internal, which will subsequently be called on\r | |
109 | each subclass when their mro is recursively updated.\r | |
110 | */\r | |
111 | Py_ssize_t i, n;\r | |
112 | int clear = 0;\r | |
113 | \r | |
114 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))\r | |
115 | return;\r | |
116 | \r | |
117 | n = PyTuple_GET_SIZE(bases);\r | |
118 | for (i = 0; i < n; i++) {\r | |
119 | PyObject *b = PyTuple_GET_ITEM(bases, i);\r | |
120 | PyTypeObject *cls;\r | |
121 | \r | |
122 | if (!PyType_Check(b) ) {\r | |
123 | clear = 1;\r | |
124 | break;\r | |
125 | }\r | |
126 | \r | |
127 | cls = (PyTypeObject *)b;\r | |
128 | \r | |
129 | if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) ||\r | |
130 | !PyType_IsSubtype(type, cls)) {\r | |
131 | clear = 1;\r | |
132 | break;\r | |
133 | }\r | |
134 | }\r | |
135 | \r | |
136 | if (clear)\r | |
137 | type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG|\r | |
138 | Py_TPFLAGS_VALID_VERSION_TAG);\r | |
139 | }\r | |
140 | \r | |
141 | static int\r | |
142 | assign_version_tag(PyTypeObject *type)\r | |
143 | {\r | |
144 | /* Ensure that the tp_version_tag is valid and set\r | |
145 | Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this\r | |
146 | must first be done on all super classes. Return 0 if this\r | |
147 | cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.\r | |
148 | */\r | |
149 | Py_ssize_t i, n;\r | |
150 | PyObject *bases;\r | |
151 | \r | |
152 | if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))\r | |
153 | return 1;\r | |
154 | if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))\r | |
155 | return 0;\r | |
156 | if (!PyType_HasFeature(type, Py_TPFLAGS_READY))\r | |
157 | return 0;\r | |
158 | \r | |
159 | type->tp_version_tag = next_version_tag++;\r | |
160 | /* for stress-testing: next_version_tag &= 0xFF; */\r | |
161 | \r | |
162 | if (type->tp_version_tag == 0) {\r | |
163 | /* wrap-around or just starting Python - clear the whole\r | |
164 | cache by filling names with references to Py_None.\r | |
165 | Values are also set to NULL for added protection, as they\r | |
166 | are borrowed reference */\r | |
167 | for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {\r | |
168 | method_cache[i].value = NULL;\r | |
169 | Py_XDECREF(method_cache[i].name);\r | |
170 | method_cache[i].name = Py_None;\r | |
171 | Py_INCREF(Py_None);\r | |
172 | }\r | |
173 | /* mark all version tags as invalid */\r | |
174 | PyType_Modified(&PyBaseObject_Type);\r | |
175 | return 1;\r | |
176 | }\r | |
177 | bases = type->tp_bases;\r | |
178 | n = PyTuple_GET_SIZE(bases);\r | |
179 | for (i = 0; i < n; i++) {\r | |
180 | PyObject *b = PyTuple_GET_ITEM(bases, i);\r | |
181 | assert(PyType_Check(b));\r | |
182 | if (!assign_version_tag((PyTypeObject *)b))\r | |
183 | return 0;\r | |
184 | }\r | |
185 | type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;\r | |
186 | return 1;\r | |
187 | }\r | |
188 | \r | |
189 | \r | |
190 | static PyMemberDef type_members[] = {\r | |
191 | {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY},\r | |
192 | {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY},\r | |
193 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},\r | |
194 | {"__weakrefoffset__", T_LONG,\r | |
195 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY},\r | |
196 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},\r | |
197 | {"__dictoffset__", T_LONG,\r | |
198 | offsetof(PyTypeObject, tp_dictoffset), READONLY},\r | |
199 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},\r | |
200 | {0}\r | |
201 | };\r | |
202 | \r | |
203 | static PyObject *\r | |
204 | type_name(PyTypeObject *type, void *context)\r | |
205 | {\r | |
206 | const char *s;\r | |
207 | \r | |
208 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {\r | |
209 | PyHeapTypeObject* et = (PyHeapTypeObject*)type;\r | |
210 | \r | |
211 | Py_INCREF(et->ht_name);\r | |
212 | return et->ht_name;\r | |
213 | }\r | |
214 | else {\r | |
215 | s = strrchr(type->tp_name, '.');\r | |
216 | if (s == NULL)\r | |
217 | s = type->tp_name;\r | |
218 | else\r | |
219 | s++;\r | |
220 | return PyString_FromString(s);\r | |
221 | }\r | |
222 | }\r | |
223 | \r | |
224 | static int\r | |
225 | type_set_name(PyTypeObject *type, PyObject *value, void *context)\r | |
226 | {\r | |
227 | PyHeapTypeObject* et;\r | |
228 | PyObject *tmp;\r | |
229 | \r | |
230 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {\r | |
231 | PyErr_Format(PyExc_TypeError,\r | |
232 | "can't set %s.__name__", type->tp_name);\r | |
233 | return -1;\r | |
234 | }\r | |
235 | if (!value) {\r | |
236 | PyErr_Format(PyExc_TypeError,\r | |
237 | "can't delete %s.__name__", type->tp_name);\r | |
238 | return -1;\r | |
239 | }\r | |
240 | if (!PyString_Check(value)) {\r | |
241 | PyErr_Format(PyExc_TypeError,\r | |
242 | "can only assign string to %s.__name__, not '%s'",\r | |
243 | type->tp_name, Py_TYPE(value)->tp_name);\r | |
244 | return -1;\r | |
245 | }\r | |
246 | if (strlen(PyString_AS_STRING(value))\r | |
247 | != (size_t)PyString_GET_SIZE(value)) {\r | |
248 | PyErr_Format(PyExc_ValueError,\r | |
249 | "__name__ must not contain null bytes");\r | |
250 | return -1;\r | |
251 | }\r | |
252 | \r | |
253 | et = (PyHeapTypeObject*)type;\r | |
254 | \r | |
255 | Py_INCREF(value);\r | |
256 | \r | |
257 | /* Wait until et is a sane state before Py_DECREF'ing the old et->ht_name\r | |
258 | value. (Bug #16447.) */\r | |
259 | tmp = et->ht_name;\r | |
260 | et->ht_name = value;\r | |
261 | \r | |
262 | type->tp_name = PyString_AS_STRING(value);\r | |
263 | Py_DECREF(tmp);\r | |
264 | \r | |
265 | return 0;\r | |
266 | }\r | |
267 | \r | |
268 | static PyObject *\r | |
269 | type_module(PyTypeObject *type, void *context)\r | |
270 | {\r | |
271 | PyObject *mod;\r | |
272 | char *s;\r | |
273 | \r | |
274 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {\r | |
275 | mod = PyDict_GetItemString(type->tp_dict, "__module__");\r | |
276 | if (!mod) {\r | |
277 | PyErr_Format(PyExc_AttributeError, "__module__");\r | |
278 | return 0;\r | |
279 | }\r | |
280 | Py_XINCREF(mod);\r | |
281 | return mod;\r | |
282 | }\r | |
283 | else {\r | |
284 | s = strrchr(type->tp_name, '.');\r | |
285 | if (s != NULL)\r | |
286 | return PyString_FromStringAndSize(\r | |
287 | type->tp_name, (Py_ssize_t)(s - type->tp_name));\r | |
288 | return PyString_FromString("__builtin__");\r | |
289 | }\r | |
290 | }\r | |
291 | \r | |
292 | static int\r | |
293 | type_set_module(PyTypeObject *type, PyObject *value, void *context)\r | |
294 | {\r | |
295 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {\r | |
296 | PyErr_Format(PyExc_TypeError,\r | |
297 | "can't set %s.__module__", type->tp_name);\r | |
298 | return -1;\r | |
299 | }\r | |
300 | if (!value) {\r | |
301 | PyErr_Format(PyExc_TypeError,\r | |
302 | "can't delete %s.__module__", type->tp_name);\r | |
303 | return -1;\r | |
304 | }\r | |
305 | \r | |
306 | PyType_Modified(type);\r | |
307 | \r | |
308 | return PyDict_SetItemString(type->tp_dict, "__module__", value);\r | |
309 | }\r | |
310 | \r | |
311 | static PyObject *\r | |
312 | type_abstractmethods(PyTypeObject *type, void *context)\r | |
313 | {\r | |
314 | PyObject *mod = NULL;\r | |
315 | /* type itself has an __abstractmethods__ descriptor (this). Don't return\r | |
316 | that. */\r | |
317 | if (type != &PyType_Type)\r | |
318 | mod = PyDict_GetItemString(type->tp_dict, "__abstractmethods__");\r | |
319 | if (!mod) {\r | |
320 | PyErr_SetString(PyExc_AttributeError, "__abstractmethods__");\r | |
321 | return NULL;\r | |
322 | }\r | |
323 | Py_XINCREF(mod);\r | |
324 | return mod;\r | |
325 | }\r | |
326 | \r | |
327 | static int\r | |
328 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)\r | |
329 | {\r | |
330 | /* __abstractmethods__ should only be set once on a type, in\r | |
331 | abc.ABCMeta.__new__, so this function doesn't do anything\r | |
332 | special to update subclasses.\r | |
333 | */\r | |
334 | int abstract, res;\r | |
335 | if (value != NULL) {\r | |
336 | abstract = PyObject_IsTrue(value);\r | |
337 | if (abstract < 0)\r | |
338 | return -1;\r | |
339 | res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value);\r | |
340 | }\r | |
341 | else {\r | |
342 | abstract = 0;\r | |
343 | res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__");\r | |
344 | if (res && PyErr_ExceptionMatches(PyExc_KeyError)) {\r | |
345 | PyErr_SetString(PyExc_AttributeError, "__abstractmethods__");\r | |
346 | return -1;\r | |
347 | }\r | |
348 | }\r | |
349 | if (res == 0) {\r | |
350 | PyType_Modified(type);\r | |
351 | if (abstract)\r | |
352 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;\r | |
353 | else\r | |
354 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;\r | |
355 | }\r | |
356 | return res;\r | |
357 | }\r | |
358 | \r | |
359 | static PyObject *\r | |
360 | type_get_bases(PyTypeObject *type, void *context)\r | |
361 | {\r | |
362 | Py_INCREF(type->tp_bases);\r | |
363 | return type->tp_bases;\r | |
364 | }\r | |
365 | \r | |
366 | static PyTypeObject *best_base(PyObject *);\r | |
367 | static int mro_internal(PyTypeObject *);\r | |
368 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);\r | |
369 | static int add_subclass(PyTypeObject*, PyTypeObject*);\r | |
370 | static void remove_subclass(PyTypeObject *, PyTypeObject *);\r | |
371 | static void update_all_slots(PyTypeObject *);\r | |
372 | \r | |
373 | typedef int (*update_callback)(PyTypeObject *, void *);\r | |
374 | static int update_subclasses(PyTypeObject *type, PyObject *name,\r | |
375 | update_callback callback, void *data);\r | |
376 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,\r | |
377 | update_callback callback, void *data);\r | |
378 | \r | |
379 | static int\r | |
380 | mro_subclasses(PyTypeObject *type, PyObject* temp)\r | |
381 | {\r | |
382 | PyTypeObject *subclass;\r | |
383 | PyObject *ref, *subclasses, *old_mro;\r | |
384 | Py_ssize_t i, n;\r | |
385 | \r | |
386 | subclasses = type->tp_subclasses;\r | |
387 | if (subclasses == NULL)\r | |
388 | return 0;\r | |
389 | assert(PyList_Check(subclasses));\r | |
390 | n = PyList_GET_SIZE(subclasses);\r | |
391 | for (i = 0; i < n; i++) {\r | |
392 | ref = PyList_GET_ITEM(subclasses, i);\r | |
393 | assert(PyWeakref_CheckRef(ref));\r | |
394 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);\r | |
395 | assert(subclass != NULL);\r | |
396 | if ((PyObject *)subclass == Py_None)\r | |
397 | continue;\r | |
398 | assert(PyType_Check(subclass));\r | |
399 | old_mro = subclass->tp_mro;\r | |
400 | if (mro_internal(subclass) < 0) {\r | |
401 | subclass->tp_mro = old_mro;\r | |
402 | return -1;\r | |
403 | }\r | |
404 | else {\r | |
405 | PyObject* tuple;\r | |
406 | tuple = PyTuple_Pack(2, subclass, old_mro);\r | |
407 | Py_DECREF(old_mro);\r | |
408 | if (!tuple)\r | |
409 | return -1;\r | |
410 | if (PyList_Append(temp, tuple) < 0)\r | |
411 | return -1;\r | |
412 | Py_DECREF(tuple);\r | |
413 | }\r | |
414 | if (mro_subclasses(subclass, temp) < 0)\r | |
415 | return -1;\r | |
416 | }\r | |
417 | return 0;\r | |
418 | }\r | |
419 | \r | |
420 | static int\r | |
421 | type_set_bases(PyTypeObject *type, PyObject *value, void *context)\r | |
422 | {\r | |
423 | Py_ssize_t i;\r | |
424 | int r = 0;\r | |
425 | PyObject *ob, *temp;\r | |
426 | PyTypeObject *new_base, *old_base;\r | |
427 | PyObject *old_bases, *old_mro;\r | |
428 | \r | |
429 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {\r | |
430 | PyErr_Format(PyExc_TypeError,\r | |
431 | "can't set %s.__bases__", type->tp_name);\r | |
432 | return -1;\r | |
433 | }\r | |
434 | if (!value) {\r | |
435 | PyErr_Format(PyExc_TypeError,\r | |
436 | "can't delete %s.__bases__", type->tp_name);\r | |
437 | return -1;\r | |
438 | }\r | |
439 | if (!PyTuple_Check(value)) {\r | |
440 | PyErr_Format(PyExc_TypeError,\r | |
441 | "can only assign tuple to %s.__bases__, not %s",\r | |
442 | type->tp_name, Py_TYPE(value)->tp_name);\r | |
443 | return -1;\r | |
444 | }\r | |
445 | if (PyTuple_GET_SIZE(value) == 0) {\r | |
446 | PyErr_Format(PyExc_TypeError,\r | |
447 | "can only assign non-empty tuple to %s.__bases__, not ()",\r | |
448 | type->tp_name);\r | |
449 | return -1;\r | |
450 | }\r | |
451 | for (i = 0; i < PyTuple_GET_SIZE(value); i++) {\r | |
452 | ob = PyTuple_GET_ITEM(value, i);\r | |
453 | if (!PyClass_Check(ob) && !PyType_Check(ob)) {\r | |
454 | PyErr_Format(\r | |
455 | PyExc_TypeError,\r | |
456 | "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",\r | |
457 | type->tp_name, Py_TYPE(ob)->tp_name);\r | |
458 | return -1;\r | |
459 | }\r | |
460 | if (PyType_Check(ob)) {\r | |
461 | if (PyType_IsSubtype((PyTypeObject*)ob, type)) {\r | |
462 | PyErr_SetString(PyExc_TypeError,\r | |
463 | "a __bases__ item causes an inheritance cycle");\r | |
464 | return -1;\r | |
465 | }\r | |
466 | }\r | |
467 | }\r | |
468 | \r | |
469 | new_base = best_base(value);\r | |
470 | \r | |
471 | if (!new_base) {\r | |
472 | return -1;\r | |
473 | }\r | |
474 | \r | |
475 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))\r | |
476 | return -1;\r | |
477 | \r | |
478 | Py_INCREF(new_base);\r | |
479 | Py_INCREF(value);\r | |
480 | \r | |
481 | old_bases = type->tp_bases;\r | |
482 | old_base = type->tp_base;\r | |
483 | old_mro = type->tp_mro;\r | |
484 | \r | |
485 | type->tp_bases = value;\r | |
486 | type->tp_base = new_base;\r | |
487 | \r | |
488 | if (mro_internal(type) < 0) {\r | |
489 | goto bail;\r | |
490 | }\r | |
491 | \r | |
492 | temp = PyList_New(0);\r | |
493 | if (!temp)\r | |
494 | goto bail;\r | |
495 | \r | |
496 | r = mro_subclasses(type, temp);\r | |
497 | \r | |
498 | if (r < 0) {\r | |
499 | for (i = 0; i < PyList_Size(temp); i++) {\r | |
500 | PyTypeObject* cls;\r | |
501 | PyObject* mro;\r | |
502 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),\r | |
503 | "", 2, 2, &cls, &mro);\r | |
504 | Py_INCREF(mro);\r | |
505 | ob = cls->tp_mro;\r | |
506 | cls->tp_mro = mro;\r | |
507 | Py_DECREF(ob);\r | |
508 | }\r | |
509 | Py_DECREF(temp);\r | |
510 | goto bail;\r | |
511 | }\r | |
512 | \r | |
513 | Py_DECREF(temp);\r | |
514 | \r | |
515 | /* any base that was in __bases__ but now isn't, we\r | |
516 | need to remove |type| from its tp_subclasses.\r | |
517 | conversely, any class now in __bases__ that wasn't\r | |
518 | needs to have |type| added to its subclasses. */\r | |
519 | \r | |
520 | /* for now, sod that: just remove from all old_bases,\r | |
521 | add to all new_bases */\r | |
522 | \r | |
523 | for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {\r | |
524 | ob = PyTuple_GET_ITEM(old_bases, i);\r | |
525 | if (PyType_Check(ob)) {\r | |
526 | remove_subclass(\r | |
527 | (PyTypeObject*)ob, type);\r | |
528 | }\r | |
529 | }\r | |
530 | \r | |
531 | for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {\r | |
532 | ob = PyTuple_GET_ITEM(value, i);\r | |
533 | if (PyType_Check(ob)) {\r | |
534 | if (add_subclass((PyTypeObject*)ob, type) < 0)\r | |
535 | r = -1;\r | |
536 | }\r | |
537 | }\r | |
538 | \r | |
539 | update_all_slots(type);\r | |
540 | \r | |
541 | Py_DECREF(old_bases);\r | |
542 | Py_DECREF(old_base);\r | |
543 | Py_DECREF(old_mro);\r | |
544 | \r | |
545 | return r;\r | |
546 | \r | |
547 | bail:\r | |
548 | Py_DECREF(type->tp_bases);\r | |
549 | Py_DECREF(type->tp_base);\r | |
550 | if (type->tp_mro != old_mro) {\r | |
551 | Py_DECREF(type->tp_mro);\r | |
552 | }\r | |
553 | \r | |
554 | type->tp_bases = old_bases;\r | |
555 | type->tp_base = old_base;\r | |
556 | type->tp_mro = old_mro;\r | |
557 | \r | |
558 | return -1;\r | |
559 | }\r | |
560 | \r | |
561 | static PyObject *\r | |
562 | type_dict(PyTypeObject *type, void *context)\r | |
563 | {\r | |
564 | if (type->tp_dict == NULL) {\r | |
565 | Py_INCREF(Py_None);\r | |
566 | return Py_None;\r | |
567 | }\r | |
568 | return PyDictProxy_New(type->tp_dict);\r | |
569 | }\r | |
570 | \r | |
571 | static PyObject *\r | |
572 | type_get_doc(PyTypeObject *type, void *context)\r | |
573 | {\r | |
574 | PyObject *result;\r | |
575 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)\r | |
576 | return PyString_FromString(type->tp_doc);\r | |
577 | result = PyDict_GetItemString(type->tp_dict, "__doc__");\r | |
578 | if (result == NULL) {\r | |
579 | result = Py_None;\r | |
580 | Py_INCREF(result);\r | |
581 | }\r | |
582 | else if (Py_TYPE(result)->tp_descr_get) {\r | |
583 | result = Py_TYPE(result)->tp_descr_get(result, NULL,\r | |
584 | (PyObject *)type);\r | |
585 | }\r | |
586 | else {\r | |
587 | Py_INCREF(result);\r | |
588 | }\r | |
589 | return result;\r | |
590 | }\r | |
591 | \r | |
592 | static PyObject *\r | |
593 | type___instancecheck__(PyObject *type, PyObject *inst)\r | |
594 | {\r | |
595 | switch (_PyObject_RealIsInstance(inst, type)) {\r | |
596 | case -1:\r | |
597 | return NULL;\r | |
598 | case 0:\r | |
599 | Py_RETURN_FALSE;\r | |
600 | default:\r | |
601 | Py_RETURN_TRUE;\r | |
602 | }\r | |
603 | }\r | |
604 | \r | |
605 | \r | |
606 | static PyObject *\r | |
607 | type___subclasscheck__(PyObject *type, PyObject *inst)\r | |
608 | {\r | |
609 | switch (_PyObject_RealIsSubclass(inst, type)) {\r | |
610 | case -1:\r | |
611 | return NULL;\r | |
612 | case 0:\r | |
613 | Py_RETURN_FALSE;\r | |
614 | default:\r | |
615 | Py_RETURN_TRUE;\r | |
616 | }\r | |
617 | }\r | |
618 | \r | |
619 | \r | |
620 | static PyGetSetDef type_getsets[] = {\r | |
621 | {"__name__", (getter)type_name, (setter)type_set_name, NULL},\r | |
622 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},\r | |
623 | {"__module__", (getter)type_module, (setter)type_set_module, NULL},\r | |
624 | {"__abstractmethods__", (getter)type_abstractmethods,\r | |
625 | (setter)type_set_abstractmethods, NULL},\r | |
626 | {"__dict__", (getter)type_dict, NULL, NULL},\r | |
627 | {"__doc__", (getter)type_get_doc, NULL, NULL},\r | |
628 | {0}\r | |
629 | };\r | |
630 | \r | |
631 | \r | |
632 | static PyObject*\r | |
633 | type_richcompare(PyObject *v, PyObject *w, int op)\r | |
634 | {\r | |
635 | PyObject *result;\r | |
636 | Py_uintptr_t vv, ww;\r | |
637 | int c;\r | |
638 | \r | |
639 | /* Make sure both arguments are types. */\r | |
640 | if (!PyType_Check(v) || !PyType_Check(w) ||\r | |
641 | /* If there is a __cmp__ method defined, let it be called instead\r | |
642 | of our dumb function designed merely to warn. See bug\r | |
643 | #7491. */\r | |
644 | Py_TYPE(v)->tp_compare || Py_TYPE(w)->tp_compare) {\r | |
645 | result = Py_NotImplemented;\r | |
646 | goto out;\r | |
647 | }\r | |
648 | \r | |
649 | /* Py3K warning if comparison isn't == or != */\r | |
650 | if (Py_Py3kWarningFlag && op != Py_EQ && op != Py_NE &&\r | |
651 | PyErr_WarnEx(PyExc_DeprecationWarning,\r | |
652 | "type inequality comparisons not supported "\r | |
653 | "in 3.x", 1) < 0) {\r | |
654 | return NULL;\r | |
655 | }\r | |
656 | \r | |
657 | /* Compare addresses */\r | |
658 | vv = (Py_uintptr_t)v;\r | |
659 | ww = (Py_uintptr_t)w;\r | |
660 | switch (op) {\r | |
661 | case Py_LT: c = vv < ww; break;\r | |
662 | case Py_LE: c = vv <= ww; break;\r | |
663 | case Py_EQ: c = vv == ww; break;\r | |
664 | case Py_NE: c = vv != ww; break;\r | |
665 | case Py_GT: c = vv > ww; break;\r | |
666 | case Py_GE: c = vv >= ww; break;\r | |
667 | default:\r | |
668 | result = Py_NotImplemented;\r | |
669 | goto out;\r | |
670 | }\r | |
671 | result = c ? Py_True : Py_False;\r | |
672 | \r | |
673 | /* incref and return */\r | |
674 | out:\r | |
675 | Py_INCREF(result);\r | |
676 | return result;\r | |
677 | }\r | |
678 | \r | |
679 | static PyObject *\r | |
680 | type_repr(PyTypeObject *type)\r | |
681 | {\r | |
682 | PyObject *mod, *name, *rtn;\r | |
683 | char *kind;\r | |
684 | \r | |
685 | mod = type_module(type, NULL);\r | |
686 | if (mod == NULL)\r | |
687 | PyErr_Clear();\r | |
688 | else if (!PyString_Check(mod)) {\r | |
689 | Py_DECREF(mod);\r | |
690 | mod = NULL;\r | |
691 | }\r | |
692 | name = type_name(type, NULL);\r | |
693 | if (name == NULL) {\r | |
694 | Py_XDECREF(mod);\r | |
695 | return NULL;\r | |
696 | }\r | |
697 | \r | |
698 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)\r | |
699 | kind = "class";\r | |
700 | else\r | |
701 | kind = "type";\r | |
702 | \r | |
703 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {\r | |
704 | rtn = PyString_FromFormat("<%s '%s.%s'>",\r | |
705 | kind,\r | |
706 | PyString_AS_STRING(mod),\r | |
707 | PyString_AS_STRING(name));\r | |
708 | }\r | |
709 | else\r | |
710 | rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);\r | |
711 | \r | |
712 | Py_XDECREF(mod);\r | |
713 | Py_DECREF(name);\r | |
714 | return rtn;\r | |
715 | }\r | |
716 | \r | |
717 | static PyObject *\r | |
718 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)\r | |
719 | {\r | |
720 | PyObject *obj;\r | |
721 | \r | |
722 | if (type->tp_new == NULL) {\r | |
723 | PyErr_Format(PyExc_TypeError,\r | |
724 | "cannot create '%.100s' instances",\r | |
725 | type->tp_name);\r | |
726 | return NULL;\r | |
727 | }\r | |
728 | \r | |
729 | obj = type->tp_new(type, args, kwds);\r | |
730 | if (obj != NULL) {\r | |
731 | /* Ugly exception: when the call was type(something),\r | |
732 | don't call tp_init on the result. */\r | |
733 | if (type == &PyType_Type &&\r | |
734 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&\r | |
735 | (kwds == NULL ||\r | |
736 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))\r | |
737 | return obj;\r | |
738 | /* If the returned object is not an instance of type,\r | |
739 | it won't be initialized. */\r | |
740 | if (!PyType_IsSubtype(obj->ob_type, type))\r | |
741 | return obj;\r | |
742 | type = obj->ob_type;\r | |
743 | if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&\r | |
744 | type->tp_init != NULL &&\r | |
745 | type->tp_init(obj, args, kwds) < 0) {\r | |
746 | Py_DECREF(obj);\r | |
747 | obj = NULL;\r | |
748 | }\r | |
749 | }\r | |
750 | return obj;\r | |
751 | }\r | |
752 | \r | |
753 | PyObject *\r | |
754 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)\r | |
755 | {\r | |
756 | PyObject *obj;\r | |
757 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1);\r | |
758 | /* note that we need to add one, for the sentinel */\r | |
759 | \r | |
760 | if (PyType_IS_GC(type))\r | |
761 | obj = _PyObject_GC_Malloc(size);\r | |
762 | else\r | |
763 | obj = (PyObject *)PyObject_MALLOC(size);\r | |
764 | \r | |
765 | if (obj == NULL)\r | |
766 | return PyErr_NoMemory();\r | |
767 | \r | |
768 | memset(obj, '\0', size);\r | |
769 | \r | |
770 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)\r | |
771 | Py_INCREF(type);\r | |
772 | \r | |
773 | if (type->tp_itemsize == 0)\r | |
774 | PyObject_INIT(obj, type);\r | |
775 | else\r | |
776 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);\r | |
777 | \r | |
778 | if (PyType_IS_GC(type))\r | |
779 | _PyObject_GC_TRACK(obj);\r | |
780 | return obj;\r | |
781 | }\r | |
782 | \r | |
783 | PyObject *\r | |
784 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)\r | |
785 | {\r | |
786 | return type->tp_alloc(type, 0);\r | |
787 | }\r | |
788 | \r | |
789 | /* Helpers for subtyping */\r | |
790 | \r | |
791 | static int\r | |
792 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)\r | |
793 | {\r | |
794 | Py_ssize_t i, n;\r | |
795 | PyMemberDef *mp;\r | |
796 | \r | |
797 | n = Py_SIZE(type);\r | |
798 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);\r | |
799 | for (i = 0; i < n; i++, mp++) {\r | |
800 | if (mp->type == T_OBJECT_EX) {\r | |
801 | char *addr = (char *)self + mp->offset;\r | |
802 | PyObject *obj = *(PyObject **)addr;\r | |
803 | if (obj != NULL) {\r | |
804 | int err = visit(obj, arg);\r | |
805 | if (err)\r | |
806 | return err;\r | |
807 | }\r | |
808 | }\r | |
809 | }\r | |
810 | return 0;\r | |
811 | }\r | |
812 | \r | |
813 | static int\r | |
814 | subtype_traverse(PyObject *self, visitproc visit, void *arg)\r | |
815 | {\r | |
816 | PyTypeObject *type, *base;\r | |
817 | traverseproc basetraverse;\r | |
818 | \r | |
819 | /* Find the nearest base with a different tp_traverse,\r | |
820 | and traverse slots while we're at it */\r | |
821 | type = Py_TYPE(self);\r | |
822 | base = type;\r | |
823 | while ((basetraverse = base->tp_traverse) == subtype_traverse) {\r | |
824 | if (Py_SIZE(base)) {\r | |
825 | int err = traverse_slots(base, self, visit, arg);\r | |
826 | if (err)\r | |
827 | return err;\r | |
828 | }\r | |
829 | base = base->tp_base;\r | |
830 | assert(base);\r | |
831 | }\r | |
832 | \r | |
833 | if (type->tp_dictoffset != base->tp_dictoffset) {\r | |
834 | PyObject **dictptr = _PyObject_GetDictPtr(self);\r | |
835 | if (dictptr && *dictptr)\r | |
836 | Py_VISIT(*dictptr);\r | |
837 | }\r | |
838 | \r | |
839 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)\r | |
840 | /* For a heaptype, the instances count as references\r | |
841 | to the type. Traverse the type so the collector\r | |
842 | can find cycles involving this link. */\r | |
843 | Py_VISIT(type);\r | |
844 | \r | |
845 | if (basetraverse)\r | |
846 | return basetraverse(self, visit, arg);\r | |
847 | return 0;\r | |
848 | }\r | |
849 | \r | |
850 | static void\r | |
851 | clear_slots(PyTypeObject *type, PyObject *self)\r | |
852 | {\r | |
853 | Py_ssize_t i, n;\r | |
854 | PyMemberDef *mp;\r | |
855 | \r | |
856 | n = Py_SIZE(type);\r | |
857 | mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);\r | |
858 | for (i = 0; i < n; i++, mp++) {\r | |
859 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {\r | |
860 | char *addr = (char *)self + mp->offset;\r | |
861 | PyObject *obj = *(PyObject **)addr;\r | |
862 | if (obj != NULL) {\r | |
863 | *(PyObject **)addr = NULL;\r | |
864 | Py_DECREF(obj);\r | |
865 | }\r | |
866 | }\r | |
867 | }\r | |
868 | }\r | |
869 | \r | |
870 | static int\r | |
871 | subtype_clear(PyObject *self)\r | |
872 | {\r | |
873 | PyTypeObject *type, *base;\r | |
874 | inquiry baseclear;\r | |
875 | \r | |
876 | /* Find the nearest base with a different tp_clear\r | |
877 | and clear slots while we're at it */\r | |
878 | type = Py_TYPE(self);\r | |
879 | base = type;\r | |
880 | while ((baseclear = base->tp_clear) == subtype_clear) {\r | |
881 | if (Py_SIZE(base))\r | |
882 | clear_slots(base, self);\r | |
883 | base = base->tp_base;\r | |
884 | assert(base);\r | |
885 | }\r | |
886 | \r | |
887 | /* Clear the instance dict (if any), to break cycles involving only\r | |
888 | __dict__ slots (as in the case 'self.__dict__ is self'). */\r | |
889 | if (type->tp_dictoffset != base->tp_dictoffset) {\r | |
890 | PyObject **dictptr = _PyObject_GetDictPtr(self);\r | |
891 | if (dictptr && *dictptr)\r | |
892 | Py_CLEAR(*dictptr);\r | |
893 | }\r | |
894 | \r | |
895 | if (baseclear)\r | |
896 | return baseclear(self);\r | |
897 | return 0;\r | |
898 | }\r | |
899 | \r | |
900 | static void\r | |
901 | subtype_dealloc(PyObject *self)\r | |
902 | {\r | |
903 | PyTypeObject *type, *base;\r | |
904 | destructor basedealloc;\r | |
905 | PyThreadState *tstate = PyThreadState_GET();\r | |
906 | \r | |
907 | /* Extract the type; we expect it to be a heap type */\r | |
908 | type = Py_TYPE(self);\r | |
909 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);\r | |
910 | \r | |
911 | /* Test whether the type has GC exactly once */\r | |
912 | \r | |
913 | if (!PyType_IS_GC(type)) {\r | |
914 | /* It's really rare to find a dynamic type that doesn't have\r | |
915 | GC; it can only happen when deriving from 'object' and not\r | |
916 | adding any slots or instance variables. This allows\r | |
917 | certain simplifications: there's no need to call\r | |
918 | clear_slots(), or DECREF the dict, or clear weakrefs. */\r | |
919 | \r | |
920 | /* Maybe call finalizer; exit early if resurrected */\r | |
921 | if (type->tp_del) {\r | |
922 | type->tp_del(self);\r | |
923 | if (self->ob_refcnt > 0)\r | |
924 | return;\r | |
925 | }\r | |
926 | \r | |
927 | /* Find the nearest base with a different tp_dealloc */\r | |
928 | base = type;\r | |
929 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {\r | |
930 | assert(Py_SIZE(base) == 0);\r | |
931 | base = base->tp_base;\r | |
932 | assert(base);\r | |
933 | }\r | |
934 | \r | |
935 | /* Extract the type again; tp_del may have changed it */\r | |
936 | type = Py_TYPE(self);\r | |
937 | \r | |
938 | /* Call the base tp_dealloc() */\r | |
939 | assert(basedealloc);\r | |
940 | basedealloc(self);\r | |
941 | \r | |
942 | /* Can't reference self beyond this point */\r | |
943 | Py_DECREF(type);\r | |
944 | \r | |
945 | /* Done */\r | |
946 | return;\r | |
947 | }\r | |
948 | \r | |
949 | /* We get here only if the type has GC */\r | |
950 | \r | |
951 | /* UnTrack and re-Track around the trashcan macro, alas */\r | |
952 | /* See explanation at end of function for full disclosure */\r | |
953 | PyObject_GC_UnTrack(self);\r | |
954 | ++_PyTrash_delete_nesting;\r | |
955 | ++ tstate->trash_delete_nesting;\r | |
956 | Py_TRASHCAN_SAFE_BEGIN(self);\r | |
957 | --_PyTrash_delete_nesting;\r | |
958 | -- tstate->trash_delete_nesting;\r | |
959 | /* DO NOT restore GC tracking at this point. weakref callbacks\r | |
960 | * (if any, and whether directly here or indirectly in something we\r | |
961 | * call) may trigger GC, and if self is tracked at that point, it\r | |
962 | * will look like trash to GC and GC will try to delete self again.\r | |
963 | */\r | |
964 | \r | |
965 | /* Find the nearest base with a different tp_dealloc */\r | |
966 | base = type;\r | |
967 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {\r | |
968 | base = base->tp_base;\r | |
969 | assert(base);\r | |
970 | }\r | |
971 | \r | |
972 | /* If we added a weaklist, we clear it. Do this *before* calling\r | |
973 | the finalizer (__del__), clearing slots, or clearing the instance\r | |
974 | dict. */\r | |
975 | \r | |
976 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset)\r | |
977 | PyObject_ClearWeakRefs(self);\r | |
978 | \r | |
979 | /* Maybe call finalizer; exit early if resurrected */\r | |
980 | if (type->tp_del) {\r | |
981 | _PyObject_GC_TRACK(self);\r | |
982 | type->tp_del(self);\r | |
983 | if (self->ob_refcnt > 0)\r | |
984 | goto endlabel; /* resurrected */\r | |
985 | else\r | |
986 | _PyObject_GC_UNTRACK(self);\r | |
987 | /* New weakrefs could be created during the finalizer call.\r | |
988 | If this occurs, clear them out without calling their\r | |
989 | finalizers since they might rely on part of the object\r | |
990 | being finalized that has already been destroyed. */\r | |
991 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {\r | |
992 | /* Modeled after GET_WEAKREFS_LISTPTR() */\r | |
993 | PyWeakReference **list = (PyWeakReference **) \\r | |
994 | PyObject_GET_WEAKREFS_LISTPTR(self);\r | |
995 | while (*list)\r | |
996 | _PyWeakref_ClearRef(*list);\r | |
997 | }\r | |
998 | }\r | |
999 | \r | |
1000 | /* Clear slots up to the nearest base with a different tp_dealloc */\r | |
1001 | base = type;\r | |
1002 | while (base->tp_dealloc == subtype_dealloc) {\r | |
1003 | if (Py_SIZE(base))\r | |
1004 | clear_slots(base, self);\r | |
1005 | base = base->tp_base;\r | |
1006 | assert(base);\r | |
1007 | }\r | |
1008 | \r | |
1009 | /* If we added a dict, DECREF it */\r | |
1010 | if (type->tp_dictoffset && !base->tp_dictoffset) {\r | |
1011 | PyObject **dictptr = _PyObject_GetDictPtr(self);\r | |
1012 | if (dictptr != NULL) {\r | |
1013 | PyObject *dict = *dictptr;\r | |
1014 | if (dict != NULL) {\r | |
1015 | Py_DECREF(dict);\r | |
1016 | *dictptr = NULL;\r | |
1017 | }\r | |
1018 | }\r | |
1019 | }\r | |
1020 | \r | |
1021 | /* Extract the type again; tp_del may have changed it */\r | |
1022 | type = Py_TYPE(self);\r | |
1023 | \r | |
1024 | /* Call the base tp_dealloc(); first retrack self if\r | |
1025 | * basedealloc knows about gc.\r | |
1026 | */\r | |
1027 | if (PyType_IS_GC(base))\r | |
1028 | _PyObject_GC_TRACK(self);\r | |
1029 | assert(basedealloc);\r | |
1030 | basedealloc(self);\r | |
1031 | \r | |
1032 | /* Can't reference self beyond this point */\r | |
1033 | Py_DECREF(type);\r | |
1034 | \r | |
1035 | endlabel:\r | |
1036 | ++_PyTrash_delete_nesting;\r | |
1037 | ++ tstate->trash_delete_nesting;\r | |
1038 | Py_TRASHCAN_SAFE_END(self);\r | |
1039 | --_PyTrash_delete_nesting;\r | |
1040 | -- tstate->trash_delete_nesting;\r | |
1041 | \r | |
1042 | /* Explanation of the weirdness around the trashcan macros:\r | |
1043 | \r | |
1044 | Q. What do the trashcan macros do?\r | |
1045 | \r | |
1046 | A. Read the comment titled "Trashcan mechanism" in object.h.\r | |
1047 | For one, this explains why there must be a call to GC-untrack\r | |
1048 | before the trashcan begin macro. Without understanding the\r | |
1049 | trashcan code, the answers to the following questions don't make\r | |
1050 | sense.\r | |
1051 | \r | |
1052 | Q. Why do we GC-untrack before the trashcan and then immediately\r | |
1053 | GC-track again afterward?\r | |
1054 | \r | |
1055 | A. In the case that the base class is GC-aware, the base class\r | |
1056 | probably GC-untracks the object. If it does that using the\r | |
1057 | UNTRACK macro, this will crash when the object is already\r | |
1058 | untracked. Because we don't know what the base class does, the\r | |
1059 | only safe thing is to make sure the object is tracked when we\r | |
1060 | call the base class dealloc. But... The trashcan begin macro\r | |
1061 | requires that the object is *untracked* before it is called. So\r | |
1062 | the dance becomes:\r | |
1063 | \r | |
1064 | GC untrack\r | |
1065 | trashcan begin\r | |
1066 | GC track\r | |
1067 | \r | |
1068 | Q. Why did the last question say "immediately GC-track again"?\r | |
1069 | It's nowhere near immediately.\r | |
1070 | \r | |
1071 | A. Because the code *used* to re-track immediately. Bad Idea.\r | |
1072 | self has a refcount of 0, and if gc ever gets its hands on it\r | |
1073 | (which can happen if any weakref callback gets invoked), it\r | |
1074 | looks like trash to gc too, and gc also tries to delete self\r | |
1075 | then. But we're already deleting self. Double deallocation is\r | |
1076 | a subtle disaster.\r | |
1077 | \r | |
1078 | Q. Why the bizarre (net-zero) manipulation of\r | |
1079 | _PyTrash_delete_nesting around the trashcan macros?\r | |
1080 | \r | |
1081 | A. Some base classes (e.g. list) also use the trashcan mechanism.\r | |
1082 | The following scenario used to be possible:\r | |
1083 | \r | |
1084 | - suppose the trashcan level is one below the trashcan limit\r | |
1085 | \r | |
1086 | - subtype_dealloc() is called\r | |
1087 | \r | |
1088 | - the trashcan limit is not yet reached, so the trashcan level\r | |
1089 | is incremented and the code between trashcan begin and end is\r | |
1090 | executed\r | |
1091 | \r | |
1092 | - this destroys much of the object's contents, including its\r | |
1093 | slots and __dict__\r | |
1094 | \r | |
1095 | - basedealloc() is called; this is really list_dealloc(), or\r | |
1096 | some other type which also uses the trashcan macros\r | |
1097 | \r | |
1098 | - the trashcan limit is now reached, so the object is put on the\r | |
1099 | trashcan's to-be-deleted-later list\r | |
1100 | \r | |
1101 | - basedealloc() returns\r | |
1102 | \r | |
1103 | - subtype_dealloc() decrefs the object's type\r | |
1104 | \r | |
1105 | - subtype_dealloc() returns\r | |
1106 | \r | |
1107 | - later, the trashcan code starts deleting the objects from its\r | |
1108 | to-be-deleted-later list\r | |
1109 | \r | |
1110 | - subtype_dealloc() is called *AGAIN* for the same object\r | |
1111 | \r | |
1112 | - at the very least (if the destroyed slots and __dict__ don't\r | |
1113 | cause problems) the object's type gets decref'ed a second\r | |
1114 | time, which is *BAD*!!!\r | |
1115 | \r | |
1116 | The remedy is to make sure that if the code between trashcan\r | |
1117 | begin and end in subtype_dealloc() is called, the code between\r | |
1118 | trashcan begin and end in basedealloc() will also be called.\r | |
1119 | This is done by decrementing the level after passing into the\r | |
1120 | trashcan block, and incrementing it just before leaving the\r | |
1121 | block.\r | |
1122 | \r | |
1123 | But now it's possible that a chain of objects consisting solely\r | |
1124 | of objects whose deallocator is subtype_dealloc() will defeat\r | |
1125 | the trashcan mechanism completely: the decremented level means\r | |
1126 | that the effective level never reaches the limit. Therefore, we\r | |
1127 | *increment* the level *before* entering the trashcan block, and\r | |
1128 | matchingly decrement it after leaving. This means the trashcan\r | |
1129 | code will trigger a little early, but that's no big deal.\r | |
1130 | \r | |
1131 | Q. Are there any live examples of code in need of all this\r | |
1132 | complexity?\r | |
1133 | \r | |
1134 | A. Yes. See SF bug 668433 for code that crashed (when Python was\r | |
1135 | compiled in debug mode) before the trashcan level manipulations\r | |
1136 | were added. For more discussion, see SF patches 581742, 575073\r | |
1137 | and bug 574207.\r | |
1138 | */\r | |
1139 | }\r | |
1140 | \r | |
1141 | static PyTypeObject *solid_base(PyTypeObject *type);\r | |
1142 | \r | |
1143 | /* type test with subclassing support */\r | |
1144 | \r | |
1145 | int\r | |
1146 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)\r | |
1147 | {\r | |
1148 | PyObject *mro;\r | |
1149 | \r | |
1150 | if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))\r | |
1151 | return b == a || b == &PyBaseObject_Type;\r | |
1152 | \r | |
1153 | mro = a->tp_mro;\r | |
1154 | if (mro != NULL) {\r | |
1155 | /* Deal with multiple inheritance without recursion\r | |
1156 | by walking the MRO tuple */\r | |
1157 | Py_ssize_t i, n;\r | |
1158 | assert(PyTuple_Check(mro));\r | |
1159 | n = PyTuple_GET_SIZE(mro);\r | |
1160 | for (i = 0; i < n; i++) {\r | |
1161 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)\r | |
1162 | return 1;\r | |
1163 | }\r | |
1164 | return 0;\r | |
1165 | }\r | |
1166 | else {\r | |
1167 | /* a is not completely initilized yet; follow tp_base */\r | |
1168 | do {\r | |
1169 | if (a == b)\r | |
1170 | return 1;\r | |
1171 | a = a->tp_base;\r | |
1172 | } while (a != NULL);\r | |
1173 | return b == &PyBaseObject_Type;\r | |
1174 | }\r | |
1175 | }\r | |
1176 | \r | |
1177 | /* Internal routines to do a method lookup in the type\r | |
1178 | without looking in the instance dictionary\r | |
1179 | (so we can't use PyObject_GetAttr) but still binding\r | |
1180 | it to the instance. The arguments are the object,\r | |
1181 | the method name as a C string, and the address of a\r | |
1182 | static variable used to cache the interned Python string.\r | |
1183 | \r | |
1184 | Two variants:\r | |
1185 | \r | |
1186 | - lookup_maybe() returns NULL without raising an exception\r | |
1187 | when the _PyType_Lookup() call fails;\r | |
1188 | \r | |
1189 | - lookup_method() always raises an exception upon errors.\r | |
1190 | \r | |
1191 | - _PyObject_LookupSpecial() exported for the benefit of other places.\r | |
1192 | */\r | |
1193 | \r | |
1194 | static PyObject *\r | |
1195 | lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)\r | |
1196 | {\r | |
1197 | PyObject *res;\r | |
1198 | \r | |
1199 | if (*attrobj == NULL) {\r | |
1200 | *attrobj = PyString_InternFromString(attrstr);\r | |
1201 | if (*attrobj == NULL)\r | |
1202 | return NULL;\r | |
1203 | }\r | |
1204 | res = _PyType_Lookup(Py_TYPE(self), *attrobj);\r | |
1205 | if (res != NULL) {\r | |
1206 | descrgetfunc f;\r | |
1207 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL)\r | |
1208 | Py_INCREF(res);\r | |
1209 | else\r | |
1210 | res = f(res, self, (PyObject *)(Py_TYPE(self)));\r | |
1211 | }\r | |
1212 | return res;\r | |
1213 | }\r | |
1214 | \r | |
1215 | static PyObject *\r | |
1216 | lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)\r | |
1217 | {\r | |
1218 | PyObject *res = lookup_maybe(self, attrstr, attrobj);\r | |
1219 | if (res == NULL && !PyErr_Occurred())\r | |
1220 | PyErr_SetObject(PyExc_AttributeError, *attrobj);\r | |
1221 | return res;\r | |
1222 | }\r | |
1223 | \r | |
1224 | PyObject *\r | |
1225 | _PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj)\r | |
1226 | {\r | |
1227 | assert(!PyInstance_Check(self));\r | |
1228 | return lookup_maybe(self, attrstr, attrobj);\r | |
1229 | }\r | |
1230 | \r | |
1231 | /* A variation of PyObject_CallMethod that uses lookup_method()\r | |
1232 | instead of PyObject_GetAttrString(). This uses the same convention\r | |
1233 | as lookup_method to cache the interned name string object. */\r | |
1234 | \r | |
1235 | static PyObject *\r | |
1236 | call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)\r | |
1237 | {\r | |
1238 | va_list va;\r | |
1239 | PyObject *args, *func = 0, *retval;\r | |
1240 | va_start(va, format);\r | |
1241 | \r | |
1242 | func = lookup_maybe(o, name, nameobj);\r | |
1243 | if (func == NULL) {\r | |
1244 | va_end(va);\r | |
1245 | if (!PyErr_Occurred())\r | |
1246 | PyErr_SetObject(PyExc_AttributeError, *nameobj);\r | |
1247 | return NULL;\r | |
1248 | }\r | |
1249 | \r | |
1250 | if (format && *format)\r | |
1251 | args = Py_VaBuildValue(format, va);\r | |
1252 | else\r | |
1253 | args = PyTuple_New(0);\r | |
1254 | \r | |
1255 | va_end(va);\r | |
1256 | \r | |
1257 | if (args == NULL)\r | |
1258 | return NULL;\r | |
1259 | \r | |
1260 | assert(PyTuple_Check(args));\r | |
1261 | retval = PyObject_Call(func, args, NULL);\r | |
1262 | \r | |
1263 | Py_DECREF(args);\r | |
1264 | Py_DECREF(func);\r | |
1265 | \r | |
1266 | return retval;\r | |
1267 | }\r | |
1268 | \r | |
1269 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */\r | |
1270 | \r | |
1271 | static PyObject *\r | |
1272 | call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)\r | |
1273 | {\r | |
1274 | va_list va;\r | |
1275 | PyObject *args, *func = 0, *retval;\r | |
1276 | va_start(va, format);\r | |
1277 | \r | |
1278 | func = lookup_maybe(o, name, nameobj);\r | |
1279 | if (func == NULL) {\r | |
1280 | va_end(va);\r | |
1281 | if (!PyErr_Occurred()) {\r | |
1282 | Py_INCREF(Py_NotImplemented);\r | |
1283 | return Py_NotImplemented;\r | |
1284 | }\r | |
1285 | return NULL;\r | |
1286 | }\r | |
1287 | \r | |
1288 | if (format && *format)\r | |
1289 | args = Py_VaBuildValue(format, va);\r | |
1290 | else\r | |
1291 | args = PyTuple_New(0);\r | |
1292 | \r | |
1293 | va_end(va);\r | |
1294 | \r | |
1295 | if (args == NULL)\r | |
1296 | return NULL;\r | |
1297 | \r | |
1298 | assert(PyTuple_Check(args));\r | |
1299 | retval = PyObject_Call(func, args, NULL);\r | |
1300 | \r | |
1301 | Py_DECREF(args);\r | |
1302 | Py_DECREF(func);\r | |
1303 | \r | |
1304 | return retval;\r | |
1305 | }\r | |
1306 | \r | |
1307 | static int\r | |
1308 | fill_classic_mro(PyObject *mro, PyObject *cls)\r | |
1309 | {\r | |
1310 | PyObject *bases, *base;\r | |
1311 | Py_ssize_t i, n;\r | |
1312 | \r | |
1313 | assert(PyList_Check(mro));\r | |
1314 | assert(PyClass_Check(cls));\r | |
1315 | i = PySequence_Contains(mro, cls);\r | |
1316 | if (i < 0)\r | |
1317 | return -1;\r | |
1318 | if (!i) {\r | |
1319 | if (PyList_Append(mro, cls) < 0)\r | |
1320 | return -1;\r | |
1321 | }\r | |
1322 | bases = ((PyClassObject *)cls)->cl_bases;\r | |
1323 | assert(bases && PyTuple_Check(bases));\r | |
1324 | n = PyTuple_GET_SIZE(bases);\r | |
1325 | for (i = 0; i < n; i++) {\r | |
1326 | base = PyTuple_GET_ITEM(bases, i);\r | |
1327 | if (fill_classic_mro(mro, base) < 0)\r | |
1328 | return -1;\r | |
1329 | }\r | |
1330 | return 0;\r | |
1331 | }\r | |
1332 | \r | |
1333 | static PyObject *\r | |
1334 | classic_mro(PyObject *cls)\r | |
1335 | {\r | |
1336 | PyObject *mro;\r | |
1337 | \r | |
1338 | assert(PyClass_Check(cls));\r | |
1339 | mro = PyList_New(0);\r | |
1340 | if (mro != NULL) {\r | |
1341 | if (fill_classic_mro(mro, cls) == 0)\r | |
1342 | return mro;\r | |
1343 | Py_DECREF(mro);\r | |
1344 | }\r | |
1345 | return NULL;\r | |
1346 | }\r | |
1347 | \r | |
1348 | /*\r | |
1349 | Method resolution order algorithm C3 described in\r | |
1350 | "A Monotonic Superclass Linearization for Dylan",\r | |
1351 | by Kim Barrett, Bob Cassel, Paul Haahr,\r | |
1352 | David A. Moon, Keith Playford, and P. Tucker Withington.\r | |
1353 | (OOPSLA 1996)\r | |
1354 | \r | |
1355 | Some notes about the rules implied by C3:\r | |
1356 | \r | |
1357 | No duplicate bases.\r | |
1358 | It isn't legal to repeat a class in a list of base classes.\r | |
1359 | \r | |
1360 | The next three properties are the 3 constraints in "C3".\r | |
1361 | \r | |
1362 | Local precendece order.\r | |
1363 | If A precedes B in C's MRO, then A will precede B in the MRO of all\r | |
1364 | subclasses of C.\r | |
1365 | \r | |
1366 | Monotonicity.\r | |
1367 | The MRO of a class must be an extension without reordering of the\r | |
1368 | MRO of each of its superclasses.\r | |
1369 | \r | |
1370 | Extended Precedence Graph (EPG).\r | |
1371 | Linearization is consistent if there is a path in the EPG from\r | |
1372 | each class to all its successors in the linearization. See\r | |
1373 | the paper for definition of EPG.\r | |
1374 | */\r | |
1375 | \r | |
1376 | static int\r | |
1377 | tail_contains(PyObject *list, int whence, PyObject *o) {\r | |
1378 | Py_ssize_t j, size;\r | |
1379 | size = PyList_GET_SIZE(list);\r | |
1380 | \r | |
1381 | for (j = whence+1; j < size; j++) {\r | |
1382 | if (PyList_GET_ITEM(list, j) == o)\r | |
1383 | return 1;\r | |
1384 | }\r | |
1385 | return 0;\r | |
1386 | }\r | |
1387 | \r | |
1388 | static PyObject *\r | |
1389 | class_name(PyObject *cls)\r | |
1390 | {\r | |
1391 | PyObject *name = PyObject_GetAttrString(cls, "__name__");\r | |
1392 | if (name == NULL) {\r | |
1393 | PyErr_Clear();\r | |
1394 | Py_XDECREF(name);\r | |
1395 | name = PyObject_Repr(cls);\r | |
1396 | }\r | |
1397 | if (name == NULL)\r | |
1398 | return NULL;\r | |
1399 | if (!PyString_Check(name)) {\r | |
1400 | Py_DECREF(name);\r | |
1401 | return NULL;\r | |
1402 | }\r | |
1403 | return name;\r | |
1404 | }\r | |
1405 | \r | |
1406 | static int\r | |
1407 | check_duplicates(PyObject *list)\r | |
1408 | {\r | |
1409 | Py_ssize_t i, j, n;\r | |
1410 | /* Let's use a quadratic time algorithm,\r | |
1411 | assuming that the bases lists is short.\r | |
1412 | */\r | |
1413 | n = PyList_GET_SIZE(list);\r | |
1414 | for (i = 0; i < n; i++) {\r | |
1415 | PyObject *o = PyList_GET_ITEM(list, i);\r | |
1416 | for (j = i + 1; j < n; j++) {\r | |
1417 | if (PyList_GET_ITEM(list, j) == o) {\r | |
1418 | o = class_name(o);\r | |
1419 | PyErr_Format(PyExc_TypeError,\r | |
1420 | "duplicate base class %s",\r | |
1421 | o ? PyString_AS_STRING(o) : "?");\r | |
1422 | Py_XDECREF(o);\r | |
1423 | return -1;\r | |
1424 | }\r | |
1425 | }\r | |
1426 | }\r | |
1427 | return 0;\r | |
1428 | }\r | |
1429 | \r | |
1430 | /* Raise a TypeError for an MRO order disagreement.\r | |
1431 | \r | |
1432 | It's hard to produce a good error message. In the absence of better\r | |
1433 | insight into error reporting, report the classes that were candidates\r | |
1434 | to be put next into the MRO. There is some conflict between the\r | |
1435 | order in which they should be put in the MRO, but it's hard to\r | |
1436 | diagnose what constraint can't be satisfied.\r | |
1437 | */\r | |
1438 | \r | |
1439 | static void\r | |
1440 | set_mro_error(PyObject *to_merge, int *remain)\r | |
1441 | {\r | |
1442 | Py_ssize_t i, n, off, to_merge_size;\r | |
1443 | char buf[1000];\r | |
1444 | PyObject *k, *v;\r | |
1445 | PyObject *set = PyDict_New();\r | |
1446 | if (!set) return;\r | |
1447 | \r | |
1448 | to_merge_size = PyList_GET_SIZE(to_merge);\r | |
1449 | for (i = 0; i < to_merge_size; i++) {\r | |
1450 | PyObject *L = PyList_GET_ITEM(to_merge, i);\r | |
1451 | if (remain[i] < PyList_GET_SIZE(L)) {\r | |
1452 | PyObject *c = PyList_GET_ITEM(L, remain[i]);\r | |
1453 | if (PyDict_SetItem(set, c, Py_None) < 0) {\r | |
1454 | Py_DECREF(set);\r | |
1455 | return;\r | |
1456 | }\r | |
1457 | }\r | |
1458 | }\r | |
1459 | n = PyDict_Size(set);\r | |
1460 | \r | |
1461 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \\r | |
1462 | consistent method resolution\norder (MRO) for bases");\r | |
1463 | i = 0;\r | |
1464 | while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {\r | |
1465 | PyObject *name = class_name(k);\r | |
1466 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",\r | |
1467 | name ? PyString_AS_STRING(name) : "?");\r | |
1468 | Py_XDECREF(name);\r | |
1469 | if (--n && (size_t)(off+1) < sizeof(buf)) {\r | |
1470 | buf[off++] = ',';\r | |
1471 | buf[off] = '\0';\r | |
1472 | }\r | |
1473 | }\r | |
1474 | PyErr_SetString(PyExc_TypeError, buf);\r | |
1475 | Py_DECREF(set);\r | |
1476 | }\r | |
1477 | \r | |
1478 | static int\r | |
1479 | pmerge(PyObject *acc, PyObject* to_merge) {\r | |
1480 | Py_ssize_t i, j, to_merge_size, empty_cnt;\r | |
1481 | int *remain;\r | |
1482 | int ok;\r | |
1483 | \r | |
1484 | to_merge_size = PyList_GET_SIZE(to_merge);\r | |
1485 | \r | |
1486 | /* remain stores an index into each sublist of to_merge.\r | |
1487 | remain[i] is the index of the next base in to_merge[i]\r | |
1488 | that is not included in acc.\r | |
1489 | */\r | |
1490 | remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);\r | |
1491 | if (remain == NULL)\r | |
1492 | return -1;\r | |
1493 | for (i = 0; i < to_merge_size; i++)\r | |
1494 | remain[i] = 0;\r | |
1495 | \r | |
1496 | again:\r | |
1497 | empty_cnt = 0;\r | |
1498 | for (i = 0; i < to_merge_size; i++) {\r | |
1499 | PyObject *candidate;\r | |
1500 | \r | |
1501 | PyObject *cur_list = PyList_GET_ITEM(to_merge, i);\r | |
1502 | \r | |
1503 | if (remain[i] >= PyList_GET_SIZE(cur_list)) {\r | |
1504 | empty_cnt++;\r | |
1505 | continue;\r | |
1506 | }\r | |
1507 | \r | |
1508 | /* Choose next candidate for MRO.\r | |
1509 | \r | |
1510 | The input sequences alone can determine the choice.\r | |
1511 | If not, choose the class which appears in the MRO\r | |
1512 | of the earliest direct superclass of the new class.\r | |
1513 | */\r | |
1514 | \r | |
1515 | candidate = PyList_GET_ITEM(cur_list, remain[i]);\r | |
1516 | for (j = 0; j < to_merge_size; j++) {\r | |
1517 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j);\r | |
1518 | if (tail_contains(j_lst, remain[j], candidate)) {\r | |
1519 | goto skip; /* continue outer loop */\r | |
1520 | }\r | |
1521 | }\r | |
1522 | ok = PyList_Append(acc, candidate);\r | |
1523 | if (ok < 0) {\r | |
1524 | PyMem_Free(remain);\r | |
1525 | return -1;\r | |
1526 | }\r | |
1527 | for (j = 0; j < to_merge_size; j++) {\r | |
1528 | PyObject *j_lst = PyList_GET_ITEM(to_merge, j);\r | |
1529 | if (remain[j] < PyList_GET_SIZE(j_lst) &&\r | |
1530 | PyList_GET_ITEM(j_lst, remain[j]) == candidate) {\r | |
1531 | remain[j]++;\r | |
1532 | }\r | |
1533 | }\r | |
1534 | goto again;\r | |
1535 | skip: ;\r | |
1536 | }\r | |
1537 | \r | |
1538 | if (empty_cnt == to_merge_size) {\r | |
1539 | PyMem_FREE(remain);\r | |
1540 | return 0;\r | |
1541 | }\r | |
1542 | set_mro_error(to_merge, remain);\r | |
1543 | PyMem_FREE(remain);\r | |
1544 | return -1;\r | |
1545 | }\r | |
1546 | \r | |
1547 | static PyObject *\r | |
1548 | mro_implementation(PyTypeObject *type)\r | |
1549 | {\r | |
1550 | Py_ssize_t i, n;\r | |
1551 | int ok;\r | |
1552 | PyObject *bases, *result;\r | |
1553 | PyObject *to_merge, *bases_aslist;\r | |
1554 | \r | |
1555 | if (type->tp_dict == NULL) {\r | |
1556 | if (PyType_Ready(type) < 0)\r | |
1557 | return NULL;\r | |
1558 | }\r | |
1559 | \r | |
1560 | /* Find a superclass linearization that honors the constraints\r | |
1561 | of the explicit lists of bases and the constraints implied by\r | |
1562 | each base class.\r | |
1563 | \r | |
1564 | to_merge is a list of lists, where each list is a superclass\r | |
1565 | linearization implied by a base class. The last element of\r | |
1566 | to_merge is the declared list of bases.\r | |
1567 | */\r | |
1568 | \r | |
1569 | bases = type->tp_bases;\r | |
1570 | n = PyTuple_GET_SIZE(bases);\r | |
1571 | \r | |
1572 | to_merge = PyList_New(n+1);\r | |
1573 | if (to_merge == NULL)\r | |
1574 | return NULL;\r | |
1575 | \r | |
1576 | for (i = 0; i < n; i++) {\r | |
1577 | PyObject *base = PyTuple_GET_ITEM(bases, i);\r | |
1578 | PyObject *parentMRO;\r | |
1579 | if (PyType_Check(base))\r | |
1580 | parentMRO = PySequence_List(\r | |
1581 | ((PyTypeObject*)base)->tp_mro);\r | |
1582 | else\r | |
1583 | parentMRO = classic_mro(base);\r | |
1584 | if (parentMRO == NULL) {\r | |
1585 | Py_DECREF(to_merge);\r | |
1586 | return NULL;\r | |
1587 | }\r | |
1588 | \r | |
1589 | PyList_SET_ITEM(to_merge, i, parentMRO);\r | |
1590 | }\r | |
1591 | \r | |
1592 | bases_aslist = PySequence_List(bases);\r | |
1593 | if (bases_aslist == NULL) {\r | |
1594 | Py_DECREF(to_merge);\r | |
1595 | return NULL;\r | |
1596 | }\r | |
1597 | /* This is just a basic sanity check. */\r | |
1598 | if (check_duplicates(bases_aslist) < 0) {\r | |
1599 | Py_DECREF(to_merge);\r | |
1600 | Py_DECREF(bases_aslist);\r | |
1601 | return NULL;\r | |
1602 | }\r | |
1603 | PyList_SET_ITEM(to_merge, n, bases_aslist);\r | |
1604 | \r | |
1605 | result = Py_BuildValue("[O]", (PyObject *)type);\r | |
1606 | if (result == NULL) {\r | |
1607 | Py_DECREF(to_merge);\r | |
1608 | return NULL;\r | |
1609 | }\r | |
1610 | \r | |
1611 | ok = pmerge(result, to_merge);\r | |
1612 | Py_DECREF(to_merge);\r | |
1613 | if (ok < 0) {\r | |
1614 | Py_DECREF(result);\r | |
1615 | return NULL;\r | |
1616 | }\r | |
1617 | \r | |
1618 | return result;\r | |
1619 | }\r | |
1620 | \r | |
1621 | static PyObject *\r | |
1622 | mro_external(PyObject *self)\r | |
1623 | {\r | |
1624 | PyTypeObject *type = (PyTypeObject *)self;\r | |
1625 | \r | |
1626 | return mro_implementation(type);\r | |
1627 | }\r | |
1628 | \r | |
1629 | static int\r | |
1630 | mro_internal(PyTypeObject *type)\r | |
1631 | {\r | |
1632 | PyObject *mro, *result, *tuple;\r | |
1633 | int checkit = 0;\r | |
1634 | \r | |
1635 | if (Py_TYPE(type) == &PyType_Type) {\r | |
1636 | result = mro_implementation(type);\r | |
1637 | }\r | |
1638 | else {\r | |
1639 | static PyObject *mro_str;\r | |
1640 | checkit = 1;\r | |
1641 | mro = lookup_method((PyObject *)type, "mro", &mro_str);\r | |
1642 | if (mro == NULL)\r | |
1643 | return -1;\r | |
1644 | result = PyObject_CallObject(mro, NULL);\r | |
1645 | Py_DECREF(mro);\r | |
1646 | }\r | |
1647 | if (result == NULL)\r | |
1648 | return -1;\r | |
1649 | tuple = PySequence_Tuple(result);\r | |
1650 | Py_DECREF(result);\r | |
1651 | if (tuple == NULL)\r | |
1652 | return -1;\r | |
1653 | if (checkit) {\r | |
1654 | Py_ssize_t i, len;\r | |
1655 | PyObject *cls;\r | |
1656 | PyTypeObject *solid;\r | |
1657 | \r | |
1658 | solid = solid_base(type);\r | |
1659 | \r | |
1660 | len = PyTuple_GET_SIZE(tuple);\r | |
1661 | \r | |
1662 | for (i = 0; i < len; i++) {\r | |
1663 | PyTypeObject *t;\r | |
1664 | cls = PyTuple_GET_ITEM(tuple, i);\r | |
1665 | if (PyClass_Check(cls))\r | |
1666 | continue;\r | |
1667 | else if (!PyType_Check(cls)) {\r | |
1668 | PyErr_Format(PyExc_TypeError,\r | |
1669 | "mro() returned a non-class ('%.500s')",\r | |
1670 | Py_TYPE(cls)->tp_name);\r | |
1671 | Py_DECREF(tuple);\r | |
1672 | return -1;\r | |
1673 | }\r | |
1674 | t = (PyTypeObject*)cls;\r | |
1675 | if (!PyType_IsSubtype(solid, solid_base(t))) {\r | |
1676 | PyErr_Format(PyExc_TypeError,\r | |
1677 | "mro() returned base with unsuitable layout ('%.500s')",\r | |
1678 | t->tp_name);\r | |
1679 | Py_DECREF(tuple);\r | |
1680 | return -1;\r | |
1681 | }\r | |
1682 | }\r | |
1683 | }\r | |
1684 | type->tp_mro = tuple;\r | |
1685 | \r | |
1686 | type_mro_modified(type, type->tp_mro);\r | |
1687 | /* corner case: the old-style super class might have been hidden\r | |
1688 | from the custom MRO */\r | |
1689 | type_mro_modified(type, type->tp_bases);\r | |
1690 | \r | |
1691 | PyType_Modified(type);\r | |
1692 | \r | |
1693 | return 0;\r | |
1694 | }\r | |
1695 | \r | |
1696 | \r | |
1697 | /* Calculate the best base amongst multiple base classes.\r | |
1698 | This is the first one that's on the path to the "solid base". */\r | |
1699 | \r | |
1700 | static PyTypeObject *\r | |
1701 | best_base(PyObject *bases)\r | |
1702 | {\r | |
1703 | Py_ssize_t i, n;\r | |
1704 | PyTypeObject *base, *winner, *candidate, *base_i;\r | |
1705 | PyObject *base_proto;\r | |
1706 | \r | |
1707 | assert(PyTuple_Check(bases));\r | |
1708 | n = PyTuple_GET_SIZE(bases);\r | |
1709 | assert(n > 0);\r | |
1710 | base = NULL;\r | |
1711 | winner = NULL;\r | |
1712 | for (i = 0; i < n; i++) {\r | |
1713 | base_proto = PyTuple_GET_ITEM(bases, i);\r | |
1714 | if (PyClass_Check(base_proto))\r | |
1715 | continue;\r | |
1716 | if (!PyType_Check(base_proto)) {\r | |
1717 | PyErr_SetString(\r | |
1718 | PyExc_TypeError,\r | |
1719 | "bases must be types");\r | |
1720 | return NULL;\r | |
1721 | }\r | |
1722 | base_i = (PyTypeObject *)base_proto;\r | |
1723 | if (base_i->tp_dict == NULL) {\r | |
1724 | if (PyType_Ready(base_i) < 0)\r | |
1725 | return NULL;\r | |
1726 | }\r | |
1727 | candidate = solid_base(base_i);\r | |
1728 | if (winner == NULL) {\r | |
1729 | winner = candidate;\r | |
1730 | base = base_i;\r | |
1731 | }\r | |
1732 | else if (PyType_IsSubtype(winner, candidate))\r | |
1733 | ;\r | |
1734 | else if (PyType_IsSubtype(candidate, winner)) {\r | |
1735 | winner = candidate;\r | |
1736 | base = base_i;\r | |
1737 | }\r | |
1738 | else {\r | |
1739 | PyErr_SetString(\r | |
1740 | PyExc_TypeError,\r | |
1741 | "multiple bases have "\r | |
1742 | "instance lay-out conflict");\r | |
1743 | return NULL;\r | |
1744 | }\r | |
1745 | }\r | |
1746 | if (base == NULL)\r | |
1747 | PyErr_SetString(PyExc_TypeError,\r | |
1748 | "a new-style class can't have only classic bases");\r | |
1749 | return base;\r | |
1750 | }\r | |
1751 | \r | |
1752 | static int\r | |
1753 | extra_ivars(PyTypeObject *type, PyTypeObject *base)\r | |
1754 | {\r | |
1755 | size_t t_size = type->tp_basicsize;\r | |
1756 | size_t b_size = base->tp_basicsize;\r | |
1757 | \r | |
1758 | assert(t_size >= b_size); /* Else type smaller than base! */\r | |
1759 | if (type->tp_itemsize || base->tp_itemsize) {\r | |
1760 | /* If itemsize is involved, stricter rules */\r | |
1761 | return t_size != b_size ||\r | |
1762 | type->tp_itemsize != base->tp_itemsize;\r | |
1763 | }\r | |
1764 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&\r | |
1765 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size &&\r | |
1766 | type->tp_flags & Py_TPFLAGS_HEAPTYPE)\r | |
1767 | t_size -= sizeof(PyObject *);\r | |
1768 | if (type->tp_dictoffset && base->tp_dictoffset == 0 &&\r | |
1769 | type->tp_dictoffset + sizeof(PyObject *) == t_size &&\r | |
1770 | type->tp_flags & Py_TPFLAGS_HEAPTYPE)\r | |
1771 | t_size -= sizeof(PyObject *);\r | |
1772 | \r | |
1773 | return t_size != b_size;\r | |
1774 | }\r | |
1775 | \r | |
1776 | static PyTypeObject *\r | |
1777 | solid_base(PyTypeObject *type)\r | |
1778 | {\r | |
1779 | PyTypeObject *base;\r | |
1780 | \r | |
1781 | if (type->tp_base)\r | |
1782 | base = solid_base(type->tp_base);\r | |
1783 | else\r | |
1784 | base = &PyBaseObject_Type;\r | |
1785 | if (extra_ivars(type, base))\r | |
1786 | return type;\r | |
1787 | else\r | |
1788 | return base;\r | |
1789 | }\r | |
1790 | \r | |
1791 | static void object_dealloc(PyObject *);\r | |
1792 | static int object_init(PyObject *, PyObject *, PyObject *);\r | |
1793 | static int update_slot(PyTypeObject *, PyObject *);\r | |
1794 | static void fixup_slot_dispatchers(PyTypeObject *);\r | |
1795 | \r | |
1796 | /*\r | |
1797 | * Helpers for __dict__ descriptor. We don't want to expose the dicts\r | |
1798 | * inherited from various builtin types. The builtin base usually provides\r | |
1799 | * its own __dict__ descriptor, so we use that when we can.\r | |
1800 | */\r | |
1801 | static PyTypeObject *\r | |
1802 | get_builtin_base_with_dict(PyTypeObject *type)\r | |
1803 | {\r | |
1804 | while (type->tp_base != NULL) {\r | |
1805 | if (type->tp_dictoffset != 0 &&\r | |
1806 | !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))\r | |
1807 | return type;\r | |
1808 | type = type->tp_base;\r | |
1809 | }\r | |
1810 | return NULL;\r | |
1811 | }\r | |
1812 | \r | |
1813 | static PyObject *\r | |
1814 | get_dict_descriptor(PyTypeObject *type)\r | |
1815 | {\r | |
1816 | static PyObject *dict_str;\r | |
1817 | PyObject *descr;\r | |
1818 | \r | |
1819 | if (dict_str == NULL) {\r | |
1820 | dict_str = PyString_InternFromString("__dict__");\r | |
1821 | if (dict_str == NULL)\r | |
1822 | return NULL;\r | |
1823 | }\r | |
1824 | descr = _PyType_Lookup(type, dict_str);\r | |
1825 | if (descr == NULL || !PyDescr_IsData(descr))\r | |
1826 | return NULL;\r | |
1827 | \r | |
1828 | return descr;\r | |
1829 | }\r | |
1830 | \r | |
1831 | static void\r | |
1832 | raise_dict_descr_error(PyObject *obj)\r | |
1833 | {\r | |
1834 | PyErr_Format(PyExc_TypeError,\r | |
1835 | "this __dict__ descriptor does not support "\r | |
1836 | "'%.200s' objects", obj->ob_type->tp_name);\r | |
1837 | }\r | |
1838 | \r | |
1839 | static PyObject *\r | |
1840 | subtype_dict(PyObject *obj, void *context)\r | |
1841 | {\r | |
1842 | PyObject **dictptr;\r | |
1843 | PyObject *dict;\r | |
1844 | PyTypeObject *base;\r | |
1845 | \r | |
1846 | base = get_builtin_base_with_dict(obj->ob_type);\r | |
1847 | if (base != NULL) {\r | |
1848 | descrgetfunc func;\r | |
1849 | PyObject *descr = get_dict_descriptor(base);\r | |
1850 | if (descr == NULL) {\r | |
1851 | raise_dict_descr_error(obj);\r | |
1852 | return NULL;\r | |
1853 | }\r | |
1854 | func = descr->ob_type->tp_descr_get;\r | |
1855 | if (func == NULL) {\r | |
1856 | raise_dict_descr_error(obj);\r | |
1857 | return NULL;\r | |
1858 | }\r | |
1859 | return func(descr, obj, (PyObject *)(obj->ob_type));\r | |
1860 | }\r | |
1861 | \r | |
1862 | dictptr = _PyObject_GetDictPtr(obj);\r | |
1863 | if (dictptr == NULL) {\r | |
1864 | PyErr_SetString(PyExc_AttributeError,\r | |
1865 | "This object has no __dict__");\r | |
1866 | return NULL;\r | |
1867 | }\r | |
1868 | dict = *dictptr;\r | |
1869 | if (dict == NULL)\r | |
1870 | *dictptr = dict = PyDict_New();\r | |
1871 | Py_XINCREF(dict);\r | |
1872 | return dict;\r | |
1873 | }\r | |
1874 | \r | |
1875 | static int\r | |
1876 | subtype_setdict(PyObject *obj, PyObject *value, void *context)\r | |
1877 | {\r | |
1878 | PyObject **dictptr;\r | |
1879 | PyObject *dict;\r | |
1880 | PyTypeObject *base;\r | |
1881 | \r | |
1882 | base = get_builtin_base_with_dict(obj->ob_type);\r | |
1883 | if (base != NULL) {\r | |
1884 | descrsetfunc func;\r | |
1885 | PyObject *descr = get_dict_descriptor(base);\r | |
1886 | if (descr == NULL) {\r | |
1887 | raise_dict_descr_error(obj);\r | |
1888 | return -1;\r | |
1889 | }\r | |
1890 | func = descr->ob_type->tp_descr_set;\r | |
1891 | if (func == NULL) {\r | |
1892 | raise_dict_descr_error(obj);\r | |
1893 | return -1;\r | |
1894 | }\r | |
1895 | return func(descr, obj, value);\r | |
1896 | }\r | |
1897 | \r | |
1898 | dictptr = _PyObject_GetDictPtr(obj);\r | |
1899 | if (dictptr == NULL) {\r | |
1900 | PyErr_SetString(PyExc_AttributeError,\r | |
1901 | "This object has no __dict__");\r | |
1902 | return -1;\r | |
1903 | }\r | |
1904 | if (value != NULL && !PyDict_Check(value)) {\r | |
1905 | PyErr_Format(PyExc_TypeError,\r | |
1906 | "__dict__ must be set to a dictionary, "\r | |
1907 | "not a '%.200s'", Py_TYPE(value)->tp_name);\r | |
1908 | return -1;\r | |
1909 | }\r | |
1910 | dict = *dictptr;\r | |
1911 | Py_XINCREF(value);\r | |
1912 | *dictptr = value;\r | |
1913 | Py_XDECREF(dict);\r | |
1914 | return 0;\r | |
1915 | }\r | |
1916 | \r | |
1917 | static PyObject *\r | |
1918 | subtype_getweakref(PyObject *obj, void *context)\r | |
1919 | {\r | |
1920 | PyObject **weaklistptr;\r | |
1921 | PyObject *result;\r | |
1922 | \r | |
1923 | if (Py_TYPE(obj)->tp_weaklistoffset == 0) {\r | |
1924 | PyErr_SetString(PyExc_AttributeError,\r | |
1925 | "This object has no __weakref__");\r | |
1926 | return NULL;\r | |
1927 | }\r | |
1928 | assert(Py_TYPE(obj)->tp_weaklistoffset > 0);\r | |
1929 | assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=\r | |
1930 | (size_t)(Py_TYPE(obj)->tp_basicsize));\r | |
1931 | weaklistptr = (PyObject **)\r | |
1932 | ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);\r | |
1933 | if (*weaklistptr == NULL)\r | |
1934 | result = Py_None;\r | |
1935 | else\r | |
1936 | result = *weaklistptr;\r | |
1937 | Py_INCREF(result);\r | |
1938 | return result;\r | |
1939 | }\r | |
1940 | \r | |
1941 | /* Three variants on the subtype_getsets list. */\r | |
1942 | \r | |
1943 | static PyGetSetDef subtype_getsets_full[] = {\r | |
1944 | {"__dict__", subtype_dict, subtype_setdict,\r | |
1945 | PyDoc_STR("dictionary for instance variables (if defined)")},\r | |
1946 | {"__weakref__", subtype_getweakref, NULL,\r | |
1947 | PyDoc_STR("list of weak references to the object (if defined)")},\r | |
1948 | {0}\r | |
1949 | };\r | |
1950 | \r | |
1951 | static PyGetSetDef subtype_getsets_dict_only[] = {\r | |
1952 | {"__dict__", subtype_dict, subtype_setdict,\r | |
1953 | PyDoc_STR("dictionary for instance variables (if defined)")},\r | |
1954 | {0}\r | |
1955 | };\r | |
1956 | \r | |
1957 | static PyGetSetDef subtype_getsets_weakref_only[] = {\r | |
1958 | {"__weakref__", subtype_getweakref, NULL,\r | |
1959 | PyDoc_STR("list of weak references to the object (if defined)")},\r | |
1960 | {0}\r | |
1961 | };\r | |
1962 | \r | |
1963 | static int\r | |
1964 | valid_identifier(PyObject *s)\r | |
1965 | {\r | |
1966 | unsigned char *p;\r | |
1967 | Py_ssize_t i, n;\r | |
1968 | \r | |
1969 | if (!PyString_Check(s)) {\r | |
1970 | PyErr_Format(PyExc_TypeError,\r | |
1971 | "__slots__ items must be strings, not '%.200s'",\r | |
1972 | Py_TYPE(s)->tp_name);\r | |
1973 | return 0;\r | |
1974 | }\r | |
1975 | p = (unsigned char *) PyString_AS_STRING(s);\r | |
1976 | n = PyString_GET_SIZE(s);\r | |
1977 | /* We must reject an empty name. As a hack, we bump the\r | |
1978 | length to 1 so that the loop will balk on the trailing \0. */\r | |
1979 | if (n == 0)\r | |
1980 | n = 1;\r | |
1981 | for (i = 0; i < n; i++, p++) {\r | |
1982 | if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {\r | |
1983 | PyErr_SetString(PyExc_TypeError,\r | |
1984 | "__slots__ must be identifiers");\r | |
1985 | return 0;\r | |
1986 | }\r | |
1987 | }\r | |
1988 | return 1;\r | |
1989 | }\r | |
1990 | \r | |
1991 | #ifdef Py_USING_UNICODE\r | |
1992 | /* Replace Unicode objects in slots. */\r | |
1993 | \r | |
1994 | static PyObject *\r | |
1995 | _unicode_to_string(PyObject *slots, Py_ssize_t nslots)\r | |
1996 | {\r | |
1997 | PyObject *tmp = NULL;\r | |
1998 | PyObject *slot_name, *new_name;\r | |
1999 | Py_ssize_t i;\r | |
2000 | \r | |
2001 | for (i = 0; i < nslots; i++) {\r | |
2002 | if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) {\r | |
2003 | if (tmp == NULL) {\r | |
2004 | tmp = PySequence_List(slots);\r | |
2005 | if (tmp == NULL)\r | |
2006 | return NULL;\r | |
2007 | }\r | |
2008 | new_name = _PyUnicode_AsDefaultEncodedString(slot_name,\r | |
2009 | NULL);\r | |
2010 | if (new_name == NULL) {\r | |
2011 | Py_DECREF(tmp);\r | |
2012 | return NULL;\r | |
2013 | }\r | |
2014 | Py_INCREF(new_name);\r | |
2015 | PyList_SET_ITEM(tmp, i, new_name);\r | |
2016 | Py_DECREF(slot_name);\r | |
2017 | }\r | |
2018 | }\r | |
2019 | if (tmp != NULL) {\r | |
2020 | slots = PyList_AsTuple(tmp);\r | |
2021 | Py_DECREF(tmp);\r | |
2022 | }\r | |
2023 | return slots;\r | |
2024 | }\r | |
2025 | #endif\r | |
2026 | \r | |
2027 | /* Forward */\r | |
2028 | static int\r | |
2029 | object_init(PyObject *self, PyObject *args, PyObject *kwds);\r | |
2030 | \r | |
2031 | static int\r | |
2032 | type_init(PyObject *cls, PyObject *args, PyObject *kwds)\r | |
2033 | {\r | |
2034 | int res;\r | |
2035 | \r | |
2036 | assert(args != NULL && PyTuple_Check(args));\r | |
2037 | assert(kwds == NULL || PyDict_Check(kwds));\r | |
2038 | \r | |
2039 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {\r | |
2040 | PyErr_SetString(PyExc_TypeError,\r | |
2041 | "type.__init__() takes no keyword arguments");\r | |
2042 | return -1;\r | |
2043 | }\r | |
2044 | \r | |
2045 | if (args != NULL && PyTuple_Check(args) &&\r | |
2046 | (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {\r | |
2047 | PyErr_SetString(PyExc_TypeError,\r | |
2048 | "type.__init__() takes 1 or 3 arguments");\r | |
2049 | return -1;\r | |
2050 | }\r | |
2051 | \r | |
2052 | /* Call object.__init__(self) now. */\r | |
2053 | /* XXX Could call super(type, cls).__init__() but what's the point? */\r | |
2054 | args = PyTuple_GetSlice(args, 0, 0);\r | |
2055 | res = object_init(cls, args, NULL);\r | |
2056 | Py_DECREF(args);\r | |
2057 | return res;\r | |
2058 | }\r | |
2059 | \r | |
2060 | static PyObject *\r | |
2061 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)\r | |
2062 | {\r | |
2063 | PyObject *name, *bases, *dict;\r | |
2064 | static char *kwlist[] = {"name", "bases", "dict", 0};\r | |
2065 | PyObject *slots, *tmp, *newslots;\r | |
2066 | PyTypeObject *type, *base, *tmptype, *winner;\r | |
2067 | PyHeapTypeObject *et;\r | |
2068 | PyMemberDef *mp;\r | |
2069 | Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;\r | |
2070 | int j, may_add_dict, may_add_weak;\r | |
2071 | \r | |
2072 | assert(args != NULL && PyTuple_Check(args));\r | |
2073 | assert(kwds == NULL || PyDict_Check(kwds));\r | |
2074 | \r | |
2075 | /* Special case: type(x) should return x->ob_type */\r | |
2076 | {\r | |
2077 | const Py_ssize_t nargs = PyTuple_GET_SIZE(args);\r | |
2078 | const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);\r | |
2079 | \r | |
2080 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {\r | |
2081 | PyObject *x = PyTuple_GET_ITEM(args, 0);\r | |
2082 | Py_INCREF(Py_TYPE(x));\r | |
2083 | return (PyObject *) Py_TYPE(x);\r | |
2084 | }\r | |
2085 | \r | |
2086 | /* SF bug 475327 -- if that didn't trigger, we need 3\r | |
2087 | arguments. but PyArg_ParseTupleAndKeywords below may give\r | |
2088 | a msg saying type() needs exactly 3. */\r | |
2089 | if (nargs + nkwds != 3) {\r | |
2090 | PyErr_SetString(PyExc_TypeError,\r | |
2091 | "type() takes 1 or 3 arguments");\r | |
2092 | return NULL;\r | |
2093 | }\r | |
2094 | }\r | |
2095 | \r | |
2096 | /* Check arguments: (name, bases, dict) */\r | |
2097 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,\r | |
2098 | &name,\r | |
2099 | &PyTuple_Type, &bases,\r | |
2100 | &PyDict_Type, &dict))\r | |
2101 | return NULL;\r | |
2102 | \r | |
2103 | /* Determine the proper metatype to deal with this,\r | |
2104 | and check for metatype conflicts while we're at it.\r | |
2105 | Note that if some other metatype wins to contract,\r | |
2106 | it's possible that its instances are not types. */\r | |
2107 | nbases = PyTuple_GET_SIZE(bases);\r | |
2108 | winner = metatype;\r | |
2109 | for (i = 0; i < nbases; i++) {\r | |
2110 | tmp = PyTuple_GET_ITEM(bases, i);\r | |
2111 | tmptype = tmp->ob_type;\r | |
2112 | if (tmptype == &PyClass_Type)\r | |
2113 | continue; /* Special case classic classes */\r | |
2114 | if (PyType_IsSubtype(winner, tmptype))\r | |
2115 | continue;\r | |
2116 | if (PyType_IsSubtype(tmptype, winner)) {\r | |
2117 | winner = tmptype;\r | |
2118 | continue;\r | |
2119 | }\r | |
2120 | PyErr_SetString(PyExc_TypeError,\r | |
2121 | "metaclass conflict: "\r | |
2122 | "the metaclass of a derived class "\r | |
2123 | "must be a (non-strict) subclass "\r | |
2124 | "of the metaclasses of all its bases");\r | |
2125 | return NULL;\r | |
2126 | }\r | |
2127 | if (winner != metatype) {\r | |
2128 | if (winner->tp_new != type_new) /* Pass it to the winner */\r | |
2129 | return winner->tp_new(winner, args, kwds);\r | |
2130 | metatype = winner;\r | |
2131 | }\r | |
2132 | \r | |
2133 | /* Adjust for empty tuple bases */\r | |
2134 | if (nbases == 0) {\r | |
2135 | bases = PyTuple_Pack(1, &PyBaseObject_Type);\r | |
2136 | if (bases == NULL)\r | |
2137 | return NULL;\r | |
2138 | nbases = 1;\r | |
2139 | }\r | |
2140 | else\r | |
2141 | Py_INCREF(bases);\r | |
2142 | \r | |
2143 | /* XXX From here until type is allocated, "return NULL" leaks bases! */\r | |
2144 | \r | |
2145 | /* Calculate best base, and check that all bases are type objects */\r | |
2146 | base = best_base(bases);\r | |
2147 | if (base == NULL) {\r | |
2148 | Py_DECREF(bases);\r | |
2149 | return NULL;\r | |
2150 | }\r | |
2151 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {\r | |
2152 | PyErr_Format(PyExc_TypeError,\r | |
2153 | "type '%.100s' is not an acceptable base type",\r | |
2154 | base->tp_name);\r | |
2155 | Py_DECREF(bases);\r | |
2156 | return NULL;\r | |
2157 | }\r | |
2158 | \r | |
2159 | /* Check for a __slots__ sequence variable in dict, and count it */\r | |
2160 | slots = PyDict_GetItemString(dict, "__slots__");\r | |
2161 | nslots = 0;\r | |
2162 | add_dict = 0;\r | |
2163 | add_weak = 0;\r | |
2164 | may_add_dict = base->tp_dictoffset == 0;\r | |
2165 | may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;\r | |
2166 | if (slots == NULL) {\r | |
2167 | if (may_add_dict) {\r | |
2168 | add_dict++;\r | |
2169 | }\r | |
2170 | if (may_add_weak) {\r | |
2171 | add_weak++;\r | |
2172 | }\r | |
2173 | }\r | |
2174 | else {\r | |
2175 | /* Have slots */\r | |
2176 | \r | |
2177 | /* Make it into a tuple */\r | |
2178 | if (PyString_Check(slots) || PyUnicode_Check(slots))\r | |
2179 | slots = PyTuple_Pack(1, slots);\r | |
2180 | else\r | |
2181 | slots = PySequence_Tuple(slots);\r | |
2182 | if (slots == NULL) {\r | |
2183 | Py_DECREF(bases);\r | |
2184 | return NULL;\r | |
2185 | }\r | |
2186 | assert(PyTuple_Check(slots));\r | |
2187 | \r | |
2188 | /* Are slots allowed? */\r | |
2189 | nslots = PyTuple_GET_SIZE(slots);\r | |
2190 | if (nslots > 0 && base->tp_itemsize != 0) {\r | |
2191 | PyErr_Format(PyExc_TypeError,\r | |
2192 | "nonempty __slots__ "\r | |
2193 | "not supported for subtype of '%s'",\r | |
2194 | base->tp_name);\r | |
2195 | bad_slots:\r | |
2196 | Py_DECREF(bases);\r | |
2197 | Py_DECREF(slots);\r | |
2198 | return NULL;\r | |
2199 | }\r | |
2200 | \r | |
2201 | #ifdef Py_USING_UNICODE\r | |
2202 | tmp = _unicode_to_string(slots, nslots);\r | |
2203 | if (tmp == NULL)\r | |
2204 | goto bad_slots;\r | |
2205 | if (tmp != slots) {\r | |
2206 | Py_DECREF(slots);\r | |
2207 | slots = tmp;\r | |
2208 | }\r | |
2209 | #endif\r | |
2210 | /* Check for valid slot names and two special cases */\r | |
2211 | for (i = 0; i < nslots; i++) {\r | |
2212 | PyObject *tmp = PyTuple_GET_ITEM(slots, i);\r | |
2213 | char *s;\r | |
2214 | if (!valid_identifier(tmp))\r | |
2215 | goto bad_slots;\r | |
2216 | assert(PyString_Check(tmp));\r | |
2217 | s = PyString_AS_STRING(tmp);\r | |
2218 | if (strcmp(s, "__dict__") == 0) {\r | |
2219 | if (!may_add_dict || add_dict) {\r | |
2220 | PyErr_SetString(PyExc_TypeError,\r | |
2221 | "__dict__ slot disallowed: "\r | |
2222 | "we already got one");\r | |
2223 | goto bad_slots;\r | |
2224 | }\r | |
2225 | add_dict++;\r | |
2226 | }\r | |
2227 | if (strcmp(s, "__weakref__") == 0) {\r | |
2228 | if (!may_add_weak || add_weak) {\r | |
2229 | PyErr_SetString(PyExc_TypeError,\r | |
2230 | "__weakref__ slot disallowed: "\r | |
2231 | "either we already got one, "\r | |
2232 | "or __itemsize__ != 0");\r | |
2233 | goto bad_slots;\r | |
2234 | }\r | |
2235 | add_weak++;\r | |
2236 | }\r | |
2237 | }\r | |
2238 | \r | |
2239 | /* Copy slots into a list, mangle names and sort them.\r | |
2240 | Sorted names are needed for __class__ assignment.\r | |
2241 | Convert them back to tuple at the end.\r | |
2242 | */\r | |
2243 | newslots = PyList_New(nslots - add_dict - add_weak);\r | |
2244 | if (newslots == NULL)\r | |
2245 | goto bad_slots;\r | |
2246 | for (i = j = 0; i < nslots; i++) {\r | |
2247 | char *s;\r | |
2248 | tmp = PyTuple_GET_ITEM(slots, i);\r | |
2249 | s = PyString_AS_STRING(tmp);\r | |
2250 | if ((add_dict && strcmp(s, "__dict__") == 0) ||\r | |
2251 | (add_weak && strcmp(s, "__weakref__") == 0))\r | |
2252 | continue;\r | |
2253 | tmp =_Py_Mangle(name, tmp);\r | |
2254 | if (!tmp) {\r | |
2255 | Py_DECREF(newslots);\r | |
2256 | goto bad_slots;\r | |
2257 | }\r | |
2258 | PyList_SET_ITEM(newslots, j, tmp);\r | |
2259 | j++;\r | |
2260 | }\r | |
2261 | assert(j == nslots - add_dict - add_weak);\r | |
2262 | nslots = j;\r | |
2263 | Py_DECREF(slots);\r | |
2264 | if (PyList_Sort(newslots) == -1) {\r | |
2265 | Py_DECREF(bases);\r | |
2266 | Py_DECREF(newslots);\r | |
2267 | return NULL;\r | |
2268 | }\r | |
2269 | slots = PyList_AsTuple(newslots);\r | |
2270 | Py_DECREF(newslots);\r | |
2271 | if (slots == NULL) {\r | |
2272 | Py_DECREF(bases);\r | |
2273 | return NULL;\r | |
2274 | }\r | |
2275 | \r | |
2276 | /* Secondary bases may provide weakrefs or dict */\r | |
2277 | if (nbases > 1 &&\r | |
2278 | ((may_add_dict && !add_dict) ||\r | |
2279 | (may_add_weak && !add_weak))) {\r | |
2280 | for (i = 0; i < nbases; i++) {\r | |
2281 | tmp = PyTuple_GET_ITEM(bases, i);\r | |
2282 | if (tmp == (PyObject *)base)\r | |
2283 | continue; /* Skip primary base */\r | |
2284 | if (PyClass_Check(tmp)) {\r | |
2285 | /* Classic base class provides both */\r | |
2286 | if (may_add_dict && !add_dict)\r | |
2287 | add_dict++;\r | |
2288 | if (may_add_weak && !add_weak)\r | |
2289 | add_weak++;\r | |
2290 | break;\r | |
2291 | }\r | |
2292 | assert(PyType_Check(tmp));\r | |
2293 | tmptype = (PyTypeObject *)tmp;\r | |
2294 | if (may_add_dict && !add_dict &&\r | |
2295 | tmptype->tp_dictoffset != 0)\r | |
2296 | add_dict++;\r | |
2297 | if (may_add_weak && !add_weak &&\r | |
2298 | tmptype->tp_weaklistoffset != 0)\r | |
2299 | add_weak++;\r | |
2300 | if (may_add_dict && !add_dict)\r | |
2301 | continue;\r | |
2302 | if (may_add_weak && !add_weak)\r | |
2303 | continue;\r | |
2304 | /* Nothing more to check */\r | |
2305 | break;\r | |
2306 | }\r | |
2307 | }\r | |
2308 | }\r | |
2309 | \r | |
2310 | /* XXX From here until type is safely allocated,\r | |
2311 | "return NULL" may leak slots! */\r | |
2312 | \r | |
2313 | /* Allocate the type object */\r | |
2314 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);\r | |
2315 | if (type == NULL) {\r | |
2316 | Py_XDECREF(slots);\r | |
2317 | Py_DECREF(bases);\r | |
2318 | return NULL;\r | |
2319 | }\r | |
2320 | \r | |
2321 | /* Keep name and slots alive in the extended type object */\r | |
2322 | et = (PyHeapTypeObject *)type;\r | |
2323 | Py_INCREF(name);\r | |
2324 | et->ht_name = name;\r | |
2325 | et->ht_slots = slots;\r | |
2326 | \r | |
2327 | /* Initialize tp_flags */\r | |
2328 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |\r | |
2329 | Py_TPFLAGS_BASETYPE;\r | |
2330 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC)\r | |
2331 | type->tp_flags |= Py_TPFLAGS_HAVE_GC;\r | |
2332 | if (base->tp_flags & Py_TPFLAGS_HAVE_NEWBUFFER)\r | |
2333 | type->tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;\r | |
2334 | \r | |
2335 | /* It's a new-style number unless it specifically inherits any\r | |
2336 | old-style numeric behavior */\r | |
2337 | if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||\r | |
2338 | (base->tp_as_number == NULL))\r | |
2339 | type->tp_flags |= Py_TPFLAGS_CHECKTYPES;\r | |
2340 | \r | |
2341 | /* Initialize essential fields */\r | |
2342 | type->tp_as_number = &et->as_number;\r | |
2343 | type->tp_as_sequence = &et->as_sequence;\r | |
2344 | type->tp_as_mapping = &et->as_mapping;\r | |
2345 | type->tp_as_buffer = &et->as_buffer;\r | |
2346 | type->tp_name = PyString_AS_STRING(name);\r | |
2347 | \r | |
2348 | /* Set tp_base and tp_bases */\r | |
2349 | type->tp_bases = bases;\r | |
2350 | Py_INCREF(base);\r | |
2351 | type->tp_base = base;\r | |
2352 | \r | |
2353 | /* Initialize tp_dict from passed-in dict */\r | |
2354 | type->tp_dict = dict = PyDict_Copy(dict);\r | |
2355 | if (dict == NULL) {\r | |
2356 | Py_DECREF(type);\r | |
2357 | return NULL;\r | |
2358 | }\r | |
2359 | \r | |
2360 | /* Set __module__ in the dict */\r | |
2361 | if (PyDict_GetItemString(dict, "__module__") == NULL) {\r | |
2362 | tmp = PyEval_GetGlobals();\r | |
2363 | if (tmp != NULL) {\r | |
2364 | tmp = PyDict_GetItemString(tmp, "__name__");\r | |
2365 | if (tmp != NULL) {\r | |
2366 | if (PyDict_SetItemString(dict, "__module__",\r | |
2367 | tmp) < 0)\r | |
2368 | return NULL;\r | |
2369 | }\r | |
2370 | }\r | |
2371 | }\r | |
2372 | \r | |
2373 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there\r | |
2374 | and is a string. The __doc__ accessor will first look for tp_doc;\r | |
2375 | if that fails, it will still look into __dict__.\r | |
2376 | */\r | |
2377 | {\r | |
2378 | PyObject *doc = PyDict_GetItemString(dict, "__doc__");\r | |
2379 | if (doc != NULL && PyString_Check(doc)) {\r | |
2380 | const size_t n = (size_t)PyString_GET_SIZE(doc);\r | |
2381 | char *tp_doc = (char *)PyObject_MALLOC(n+1);\r | |
2382 | if (tp_doc == NULL) {\r | |
2383 | Py_DECREF(type);\r | |
2384 | return NULL;\r | |
2385 | }\r | |
2386 | memcpy(tp_doc, PyString_AS_STRING(doc), n+1);\r | |
2387 | type->tp_doc = tp_doc;\r | |
2388 | }\r | |
2389 | }\r | |
2390 | \r | |
2391 | /* Special-case __new__: if it's a plain function,\r | |
2392 | make it a static function */\r | |
2393 | tmp = PyDict_GetItemString(dict, "__new__");\r | |
2394 | if (tmp != NULL && PyFunction_Check(tmp)) {\r | |
2395 | tmp = PyStaticMethod_New(tmp);\r | |
2396 | if (tmp == NULL) {\r | |
2397 | Py_DECREF(type);\r | |
2398 | return NULL;\r | |
2399 | }\r | |
2400 | PyDict_SetItemString(dict, "__new__", tmp);\r | |
2401 | Py_DECREF(tmp);\r | |
2402 | }\r | |
2403 | \r | |
2404 | /* Add descriptors for custom slots from __slots__, or for __dict__ */\r | |
2405 | mp = PyHeapType_GET_MEMBERS(et);\r | |
2406 | slotoffset = base->tp_basicsize;\r | |
2407 | if (slots != NULL) {\r | |
2408 | for (i = 0; i < nslots; i++, mp++) {\r | |
2409 | mp->name = PyString_AS_STRING(\r | |
2410 | PyTuple_GET_ITEM(slots, i));\r | |
2411 | mp->type = T_OBJECT_EX;\r | |
2412 | mp->offset = slotoffset;\r | |
2413 | \r | |
2414 | /* __dict__ and __weakref__ are already filtered out */\r | |
2415 | assert(strcmp(mp->name, "__dict__") != 0);\r | |
2416 | assert(strcmp(mp->name, "__weakref__") != 0);\r | |
2417 | \r | |
2418 | slotoffset += sizeof(PyObject *);\r | |
2419 | }\r | |
2420 | }\r | |
2421 | if (add_dict) {\r | |
2422 | if (base->tp_itemsize)\r | |
2423 | type->tp_dictoffset = -(long)sizeof(PyObject *);\r | |
2424 | else\r | |
2425 | type->tp_dictoffset = slotoffset;\r | |
2426 | slotoffset += sizeof(PyObject *);\r | |
2427 | }\r | |
2428 | if (add_weak) {\r | |
2429 | assert(!base->tp_itemsize);\r | |
2430 | type->tp_weaklistoffset = slotoffset;\r | |
2431 | slotoffset += sizeof(PyObject *);\r | |
2432 | }\r | |
2433 | type->tp_basicsize = slotoffset;\r | |
2434 | type->tp_itemsize = base->tp_itemsize;\r | |
2435 | type->tp_members = PyHeapType_GET_MEMBERS(et);\r | |
2436 | \r | |
2437 | if (type->tp_weaklistoffset && type->tp_dictoffset)\r | |
2438 | type->tp_getset = subtype_getsets_full;\r | |
2439 | else if (type->tp_weaklistoffset && !type->tp_dictoffset)\r | |
2440 | type->tp_getset = subtype_getsets_weakref_only;\r | |
2441 | else if (!type->tp_weaklistoffset && type->tp_dictoffset)\r | |
2442 | type->tp_getset = subtype_getsets_dict_only;\r | |
2443 | else\r | |
2444 | type->tp_getset = NULL;\r | |
2445 | \r | |
2446 | /* Special case some slots */\r | |
2447 | if (type->tp_dictoffset != 0 || nslots > 0) {\r | |
2448 | if (base->tp_getattr == NULL && base->tp_getattro == NULL)\r | |
2449 | type->tp_getattro = PyObject_GenericGetAttr;\r | |
2450 | if (base->tp_setattr == NULL && base->tp_setattro == NULL)\r | |
2451 | type->tp_setattro = PyObject_GenericSetAttr;\r | |
2452 | }\r | |
2453 | type->tp_dealloc = subtype_dealloc;\r | |
2454 | \r | |
2455 | /* Enable GC unless there are really no instance variables possible */\r | |
2456 | if (!(type->tp_basicsize == sizeof(PyObject) &&\r | |
2457 | type->tp_itemsize == 0))\r | |
2458 | type->tp_flags |= Py_TPFLAGS_HAVE_GC;\r | |
2459 | \r | |
2460 | /* Always override allocation strategy to use regular heap */\r | |
2461 | type->tp_alloc = PyType_GenericAlloc;\r | |
2462 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {\r | |
2463 | type->tp_free = PyObject_GC_Del;\r | |
2464 | type->tp_traverse = subtype_traverse;\r | |
2465 | type->tp_clear = subtype_clear;\r | |
2466 | }\r | |
2467 | else\r | |
2468 | type->tp_free = PyObject_Del;\r | |
2469 | \r | |
2470 | /* Initialize the rest */\r | |
2471 | if (PyType_Ready(type) < 0) {\r | |
2472 | Py_DECREF(type);\r | |
2473 | return NULL;\r | |
2474 | }\r | |
2475 | \r | |
2476 | /* Put the proper slots in place */\r | |
2477 | fixup_slot_dispatchers(type);\r | |
2478 | \r | |
2479 | return (PyObject *)type;\r | |
2480 | }\r | |
2481 | \r | |
2482 | /* Internal API to look for a name through the MRO.\r | |
2483 | This returns a borrowed reference, and doesn't set an exception! */\r | |
2484 | PyObject *\r | |
2485 | _PyType_Lookup(PyTypeObject *type, PyObject *name)\r | |
2486 | {\r | |
2487 | Py_ssize_t i, n;\r | |
2488 | PyObject *mro, *res, *base, *dict;\r | |
2489 | unsigned int h;\r | |
2490 | \r | |
2491 | if (MCACHE_CACHEABLE_NAME(name) &&\r | |
2492 | PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {\r | |
2493 | /* fast path */\r | |
2494 | h = MCACHE_HASH_METHOD(type, name);\r | |
2495 | if (method_cache[h].version == type->tp_version_tag &&\r | |
2496 | method_cache[h].name == name)\r | |
2497 | return method_cache[h].value;\r | |
2498 | }\r | |
2499 | \r | |
2500 | /* Look in tp_dict of types in MRO */\r | |
2501 | mro = type->tp_mro;\r | |
2502 | \r | |
2503 | /* If mro is NULL, the type is either not yet initialized\r | |
2504 | by PyType_Ready(), or already cleared by type_clear().\r | |
2505 | Either way the safest thing to do is to return NULL. */\r | |
2506 | if (mro == NULL)\r | |
2507 | return NULL;\r | |
2508 | \r | |
2509 | res = NULL;\r | |
2510 | assert(PyTuple_Check(mro));\r | |
2511 | n = PyTuple_GET_SIZE(mro);\r | |
2512 | for (i = 0; i < n; i++) {\r | |
2513 | base = PyTuple_GET_ITEM(mro, i);\r | |
2514 | if (PyClass_Check(base))\r | |
2515 | dict = ((PyClassObject *)base)->cl_dict;\r | |
2516 | else {\r | |
2517 | assert(PyType_Check(base));\r | |
2518 | dict = ((PyTypeObject *)base)->tp_dict;\r | |
2519 | }\r | |
2520 | assert(dict && PyDict_Check(dict));\r | |
2521 | res = PyDict_GetItem(dict, name);\r | |
2522 | if (res != NULL)\r | |
2523 | break;\r | |
2524 | }\r | |
2525 | \r | |
2526 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) {\r | |
2527 | h = MCACHE_HASH_METHOD(type, name);\r | |
2528 | method_cache[h].version = type->tp_version_tag;\r | |
2529 | method_cache[h].value = res; /* borrowed */\r | |
2530 | Py_INCREF(name);\r | |
2531 | Py_DECREF(method_cache[h].name);\r | |
2532 | method_cache[h].name = name;\r | |
2533 | }\r | |
2534 | return res;\r | |
2535 | }\r | |
2536 | \r | |
2537 | /* This is similar to PyObject_GenericGetAttr(),\r | |
2538 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */\r | |
2539 | static PyObject *\r | |
2540 | type_getattro(PyTypeObject *type, PyObject *name)\r | |
2541 | {\r | |
2542 | PyTypeObject *metatype = Py_TYPE(type);\r | |
2543 | PyObject *meta_attribute, *attribute;\r | |
2544 | descrgetfunc meta_get;\r | |
2545 | \r | |
2546 | if (!PyString_Check(name)) {\r | |
2547 | PyErr_Format(PyExc_TypeError,\r | |
2548 | "attribute name must be string, not '%.200s'",\r | |
2549 | name->ob_type->tp_name);\r | |
2550 | return NULL;\r | |
2551 | }\r | |
2552 | \r | |
2553 | /* Initialize this type (we'll assume the metatype is initialized) */\r | |
2554 | if (type->tp_dict == NULL) {\r | |
2555 | if (PyType_Ready(type) < 0)\r | |
2556 | return NULL;\r | |
2557 | }\r | |
2558 | \r | |
2559 | /* No readable descriptor found yet */\r | |
2560 | meta_get = NULL;\r | |
2561 | \r | |
2562 | /* Look for the attribute in the metatype */\r | |
2563 | meta_attribute = _PyType_Lookup(metatype, name);\r | |
2564 | \r | |
2565 | if (meta_attribute != NULL) {\r | |
2566 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get;\r | |
2567 | \r | |
2568 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {\r | |
2569 | /* Data descriptors implement tp_descr_set to intercept\r | |
2570 | * writes. Assume the attribute is not overridden in\r | |
2571 | * type's tp_dict (and bases): call the descriptor now.\r | |
2572 | */\r | |
2573 | return meta_get(meta_attribute, (PyObject *)type,\r | |
2574 | (PyObject *)metatype);\r | |
2575 | }\r | |
2576 | Py_INCREF(meta_attribute);\r | |
2577 | }\r | |
2578 | \r | |
2579 | /* No data descriptor found on metatype. Look in tp_dict of this\r | |
2580 | * type and its bases */\r | |
2581 | attribute = _PyType_Lookup(type, name);\r | |
2582 | if (attribute != NULL) {\r | |
2583 | /* Implement descriptor functionality, if any */\r | |
2584 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;\r | |
2585 | \r | |
2586 | Py_XDECREF(meta_attribute);\r | |
2587 | \r | |
2588 | if (local_get != NULL) {\r | |
2589 | /* NULL 2nd argument indicates the descriptor was\r | |
2590 | * found on the target object itself (or a base) */\r | |
2591 | return local_get(attribute, (PyObject *)NULL,\r | |
2592 | (PyObject *)type);\r | |
2593 | }\r | |
2594 | \r | |
2595 | Py_INCREF(attribute);\r | |
2596 | return attribute;\r | |
2597 | }\r | |
2598 | \r | |
2599 | /* No attribute found in local __dict__ (or bases): use the\r | |
2600 | * descriptor from the metatype, if any */\r | |
2601 | if (meta_get != NULL) {\r | |
2602 | PyObject *res;\r | |
2603 | res = meta_get(meta_attribute, (PyObject *)type,\r | |
2604 | (PyObject *)metatype);\r | |
2605 | Py_DECREF(meta_attribute);\r | |
2606 | return res;\r | |
2607 | }\r | |
2608 | \r | |
2609 | /* If an ordinary attribute was found on the metatype, return it now */\r | |
2610 | if (meta_attribute != NULL) {\r | |
2611 | return meta_attribute;\r | |
2612 | }\r | |
2613 | \r | |
2614 | /* Give up */\r | |
2615 | PyErr_Format(PyExc_AttributeError,\r | |
2616 | "type object '%.50s' has no attribute '%.400s'",\r | |
2617 | type->tp_name, PyString_AS_STRING(name));\r | |
2618 | return NULL;\r | |
2619 | }\r | |
2620 | \r | |
2621 | static int\r | |
2622 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)\r | |
2623 | {\r | |
2624 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {\r | |
2625 | PyErr_Format(\r | |
2626 | PyExc_TypeError,\r | |
2627 | "can't set attributes of built-in/extension type '%s'",\r | |
2628 | type->tp_name);\r | |
2629 | return -1;\r | |
2630 | }\r | |
2631 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)\r | |
2632 | return -1;\r | |
2633 | return update_slot(type, name);\r | |
2634 | }\r | |
2635 | \r | |
2636 | static void\r | |
2637 | type_dealloc(PyTypeObject *type)\r | |
2638 | {\r | |
2639 | PyHeapTypeObject *et;\r | |
2640 | \r | |
2641 | /* Assert this is a heap-allocated type object */\r | |
2642 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);\r | |
2643 | _PyObject_GC_UNTRACK(type);\r | |
2644 | PyObject_ClearWeakRefs((PyObject *)type);\r | |
2645 | et = (PyHeapTypeObject *)type;\r | |
2646 | Py_XDECREF(type->tp_base);\r | |
2647 | Py_XDECREF(type->tp_dict);\r | |
2648 | Py_XDECREF(type->tp_bases);\r | |
2649 | Py_XDECREF(type->tp_mro);\r | |
2650 | Py_XDECREF(type->tp_cache);\r | |
2651 | Py_XDECREF(type->tp_subclasses);\r | |
2652 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots\r | |
2653 | * of most other objects. It's okay to cast it to char *.\r | |
2654 | */\r | |
2655 | PyObject_Free((char *)type->tp_doc);\r | |
2656 | Py_XDECREF(et->ht_name);\r | |
2657 | Py_XDECREF(et->ht_slots);\r | |
2658 | Py_TYPE(type)->tp_free((PyObject *)type);\r | |
2659 | }\r | |
2660 | \r | |
2661 | static PyObject *\r | |
2662 | type_subclasses(PyTypeObject *type, PyObject *args_ignored)\r | |
2663 | {\r | |
2664 | PyObject *list, *raw, *ref;\r | |
2665 | Py_ssize_t i, n;\r | |
2666 | \r | |
2667 | list = PyList_New(0);\r | |
2668 | if (list == NULL)\r | |
2669 | return NULL;\r | |
2670 | raw = type->tp_subclasses;\r | |
2671 | if (raw == NULL)\r | |
2672 | return list;\r | |
2673 | assert(PyList_Check(raw));\r | |
2674 | n = PyList_GET_SIZE(raw);\r | |
2675 | for (i = 0; i < n; i++) {\r | |
2676 | ref = PyList_GET_ITEM(raw, i);\r | |
2677 | assert(PyWeakref_CheckRef(ref));\r | |
2678 | ref = PyWeakref_GET_OBJECT(ref);\r | |
2679 | if (ref != Py_None) {\r | |
2680 | if (PyList_Append(list, ref) < 0) {\r | |
2681 | Py_DECREF(list);\r | |
2682 | return NULL;\r | |
2683 | }\r | |
2684 | }\r | |
2685 | }\r | |
2686 | return list;\r | |
2687 | }\r | |
2688 | \r | |
2689 | static PyMethodDef type_methods[] = {\r | |
2690 | {"mro", (PyCFunction)mro_external, METH_NOARGS,\r | |
2691 | PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},\r | |
2692 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,\r | |
2693 | PyDoc_STR("__subclasses__() -> list of immediate subclasses")},\r | |
2694 | {"__instancecheck__", type___instancecheck__, METH_O,\r | |
2695 | PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance")},\r | |
2696 | {"__subclasscheck__", type___subclasscheck__, METH_O,\r | |
2697 | PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")},\r | |
2698 | {0}\r | |
2699 | };\r | |
2700 | \r | |
2701 | PyDoc_STRVAR(type_doc,\r | |
2702 | "type(object) -> the object's type\n"\r | |
2703 | "type(name, bases, dict) -> a new type");\r | |
2704 | \r | |
2705 | static int\r | |
2706 | type_traverse(PyTypeObject *type, visitproc visit, void *arg)\r | |
2707 | {\r | |
2708 | /* Because of type_is_gc(), the collector only calls this\r | |
2709 | for heaptypes. */\r | |
2710 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);\r | |
2711 | \r | |
2712 | Py_VISIT(type->tp_dict);\r | |
2713 | Py_VISIT(type->tp_cache);\r | |
2714 | Py_VISIT(type->tp_mro);\r | |
2715 | Py_VISIT(type->tp_bases);\r | |
2716 | Py_VISIT(type->tp_base);\r | |
2717 | \r | |
2718 | /* There's no need to visit type->tp_subclasses or\r | |
2719 | ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved\r | |
2720 | in cycles; tp_subclasses is a list of weak references,\r | |
2721 | and slots is a tuple of strings. */\r | |
2722 | \r | |
2723 | return 0;\r | |
2724 | }\r | |
2725 | \r | |
2726 | static int\r | |
2727 | type_clear(PyTypeObject *type)\r | |
2728 | {\r | |
2729 | /* Because of type_is_gc(), the collector only calls this\r | |
2730 | for heaptypes. */\r | |
2731 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);\r | |
2732 | \r | |
2733 | /* We need to invalidate the method cache carefully before clearing\r | |
2734 | the dict, so that other objects caught in a reference cycle\r | |
2735 | don't start calling destroyed methods.\r | |
2736 | \r | |
2737 | Otherwise, the only field we need to clear is tp_mro, which is\r | |
2738 | part of a hard cycle (its first element is the class itself) that\r | |
2739 | won't be broken otherwise (it's a tuple and tuples don't have a\r | |
2740 | tp_clear handler). None of the other fields need to be\r | |
2741 | cleared, and here's why:\r | |
2742 | \r | |
2743 | tp_cache:\r | |
2744 | Not used; if it were, it would be a dict.\r | |
2745 | \r | |
2746 | tp_bases, tp_base:\r | |
2747 | If these are involved in a cycle, there must be at least\r | |
2748 | one other, mutable object in the cycle, e.g. a base\r | |
2749 | class's dict; the cycle will be broken that way.\r | |
2750 | \r | |
2751 | tp_subclasses:\r | |
2752 | A list of weak references can't be part of a cycle; and\r | |
2753 | lists have their own tp_clear.\r | |
2754 | \r | |
2755 | slots (in PyHeapTypeObject):\r | |
2756 | A tuple of strings can't be part of a cycle.\r | |
2757 | */\r | |
2758 | \r | |
2759 | PyType_Modified(type);\r | |
2760 | if (type->tp_dict)\r | |
2761 | PyDict_Clear(type->tp_dict);\r | |
2762 | Py_CLEAR(type->tp_mro);\r | |
2763 | \r | |
2764 | return 0;\r | |
2765 | }\r | |
2766 | \r | |
2767 | static int\r | |
2768 | type_is_gc(PyTypeObject *type)\r | |
2769 | {\r | |
2770 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE;\r | |
2771 | }\r | |
2772 | \r | |
2773 | PyTypeObject PyType_Type = {\r | |
2774 | PyVarObject_HEAD_INIT(&PyType_Type, 0)\r | |
2775 | "type", /* tp_name */\r | |
2776 | sizeof(PyHeapTypeObject), /* tp_basicsize */\r | |
2777 | sizeof(PyMemberDef), /* tp_itemsize */\r | |
2778 | (destructor)type_dealloc, /* tp_dealloc */\r | |
2779 | 0, /* tp_print */\r | |
2780 | 0, /* tp_getattr */\r | |
2781 | 0, /* tp_setattr */\r | |
2782 | 0, /* tp_compare */\r | |
2783 | (reprfunc)type_repr, /* tp_repr */\r | |
2784 | 0, /* tp_as_number */\r | |
2785 | 0, /* tp_as_sequence */\r | |
2786 | 0, /* tp_as_mapping */\r | |
2787 | (hashfunc)_Py_HashPointer, /* tp_hash */\r | |
2788 | (ternaryfunc)type_call, /* tp_call */\r | |
2789 | 0, /* tp_str */\r | |
2790 | (getattrofunc)type_getattro, /* tp_getattro */\r | |
2791 | (setattrofunc)type_setattro, /* tp_setattro */\r | |
2792 | 0, /* tp_as_buffer */\r | |
2793 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |\r | |
2794 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */\r | |
2795 | type_doc, /* tp_doc */\r | |
2796 | (traverseproc)type_traverse, /* tp_traverse */\r | |
2797 | (inquiry)type_clear, /* tp_clear */\r | |
2798 | type_richcompare, /* tp_richcompare */\r | |
2799 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */\r | |
2800 | 0, /* tp_iter */\r | |
2801 | 0, /* tp_iternext */\r | |
2802 | type_methods, /* tp_methods */\r | |
2803 | type_members, /* tp_members */\r | |
2804 | type_getsets, /* tp_getset */\r | |
2805 | 0, /* tp_base */\r | |
2806 | 0, /* tp_dict */\r | |
2807 | 0, /* tp_descr_get */\r | |
2808 | 0, /* tp_descr_set */\r | |
2809 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */\r | |
2810 | type_init, /* tp_init */\r | |
2811 | 0, /* tp_alloc */\r | |
2812 | type_new, /* tp_new */\r | |
2813 | PyObject_GC_Del, /* tp_free */\r | |
2814 | (inquiry)type_is_gc, /* tp_is_gc */\r | |
2815 | };\r | |
2816 | \r | |
2817 | \r | |
2818 | /* The base type of all types (eventually)... except itself. */\r | |
2819 | \r | |
2820 | /* You may wonder why object.__new__() only complains about arguments\r | |
2821 | when object.__init__() is not overridden, and vice versa.\r | |
2822 | \r | |
2823 | Consider the use cases:\r | |
2824 | \r | |
2825 | 1. When neither is overridden, we want to hear complaints about\r | |
2826 | excess (i.e., any) arguments, since their presence could\r | |
2827 | indicate there's a bug.\r | |
2828 | \r | |
2829 | 2. When defining an Immutable type, we are likely to override only\r | |
2830 | __new__(), since __init__() is called too late to initialize an\r | |
2831 | Immutable object. Since __new__() defines the signature for the\r | |
2832 | type, it would be a pain to have to override __init__() just to\r | |
2833 | stop it from complaining about excess arguments.\r | |
2834 | \r | |
2835 | 3. When defining a Mutable type, we are likely to override only\r | |
2836 | __init__(). So here the converse reasoning applies: we don't\r | |
2837 | want to have to override __new__() just to stop it from\r | |
2838 | complaining.\r | |
2839 | \r | |
2840 | 4. When __init__() is overridden, and the subclass __init__() calls\r | |
2841 | object.__init__(), the latter should complain about excess\r | |
2842 | arguments; ditto for __new__().\r | |
2843 | \r | |
2844 | Use cases 2 and 3 make it unattractive to unconditionally check for\r | |
2845 | excess arguments. The best solution that addresses all four use\r | |
2846 | cases is as follows: __init__() complains about excess arguments\r | |
2847 | unless __new__() is overridden and __init__() is not overridden\r | |
2848 | (IOW, if __init__() is overridden or __new__() is not overridden);\r | |
2849 | symmetrically, __new__() complains about excess arguments unless\r | |
2850 | __init__() is overridden and __new__() is not overridden\r | |
2851 | (IOW, if __new__() is overridden or __init__() is not overridden).\r | |
2852 | \r | |
2853 | However, for backwards compatibility, this breaks too much code.\r | |
2854 | Therefore, in 2.6, we'll *warn* about excess arguments when both\r | |
2855 | methods are overridden; for all other cases we'll use the above\r | |
2856 | rules.\r | |
2857 | \r | |
2858 | */\r | |
2859 | \r | |
2860 | /* Forward */\r | |
2861 | static PyObject *\r | |
2862 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds);\r | |
2863 | \r | |
2864 | static int\r | |
2865 | excess_args(PyObject *args, PyObject *kwds)\r | |
2866 | {\r | |
2867 | return PyTuple_GET_SIZE(args) ||\r | |
2868 | (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));\r | |
2869 | }\r | |
2870 | \r | |
2871 | static int\r | |
2872 | object_init(PyObject *self, PyObject *args, PyObject *kwds)\r | |
2873 | {\r | |
2874 | int err = 0;\r | |
2875 | if (excess_args(args, kwds)) {\r | |
2876 | PyTypeObject *type = Py_TYPE(self);\r | |
2877 | if (type->tp_init != object_init &&\r | |
2878 | type->tp_new != object_new)\r | |
2879 | {\r | |
2880 | err = PyErr_WarnEx(PyExc_DeprecationWarning,\r | |
2881 | "object.__init__() takes no parameters",\r | |
2882 | 1);\r | |
2883 | }\r | |
2884 | else if (type->tp_init != object_init ||\r | |
2885 | type->tp_new == object_new)\r | |
2886 | {\r | |
2887 | PyErr_SetString(PyExc_TypeError,\r | |
2888 | "object.__init__() takes no parameters");\r | |
2889 | err = -1;\r | |
2890 | }\r | |
2891 | }\r | |
2892 | return err;\r | |
2893 | }\r | |
2894 | \r | |
2895 | static PyObject *\r | |
2896 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r | |
2897 | {\r | |
2898 | int err = 0;\r | |
2899 | if (excess_args(args, kwds)) {\r | |
2900 | if (type->tp_new != object_new &&\r | |
2901 | type->tp_init != object_init)\r | |
2902 | {\r | |
2903 | err = PyErr_WarnEx(PyExc_DeprecationWarning,\r | |
2904 | "object() takes no parameters",\r | |
2905 | 1);\r | |
2906 | }\r | |
2907 | else if (type->tp_new != object_new ||\r | |
2908 | type->tp_init == object_init)\r | |
2909 | {\r | |
2910 | PyErr_SetString(PyExc_TypeError,\r | |
2911 | "object() takes no parameters");\r | |
2912 | err = -1;\r | |
2913 | }\r | |
2914 | }\r | |
2915 | if (err < 0)\r | |
2916 | return NULL;\r | |
2917 | \r | |
2918 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {\r | |
2919 | static PyObject *comma = NULL;\r | |
2920 | PyObject *abstract_methods = NULL;\r | |
2921 | PyObject *builtins;\r | |
2922 | PyObject *sorted;\r | |
2923 | PyObject *sorted_methods = NULL;\r | |
2924 | PyObject *joined = NULL;\r | |
2925 | const char *joined_str;\r | |
2926 | \r | |
2927 | /* Compute ", ".join(sorted(type.__abstractmethods__))\r | |
2928 | into joined. */\r | |
2929 | abstract_methods = type_abstractmethods(type, NULL);\r | |
2930 | if (abstract_methods == NULL)\r | |
2931 | goto error;\r | |
2932 | builtins = PyEval_GetBuiltins();\r | |
2933 | if (builtins == NULL)\r | |
2934 | goto error;\r | |
2935 | sorted = PyDict_GetItemString(builtins, "sorted");\r | |
2936 | if (sorted == NULL)\r | |
2937 | goto error;\r | |
2938 | sorted_methods = PyObject_CallFunctionObjArgs(sorted,\r | |
2939 | abstract_methods,\r | |
2940 | NULL);\r | |
2941 | if (sorted_methods == NULL)\r | |
2942 | goto error;\r | |
2943 | if (comma == NULL) {\r | |
2944 | comma = PyString_InternFromString(", ");\r | |
2945 | if (comma == NULL)\r | |
2946 | goto error;\r | |
2947 | }\r | |
2948 | joined = PyObject_CallMethod(comma, "join",\r | |
2949 | "O", sorted_methods);\r | |
2950 | if (joined == NULL)\r | |
2951 | goto error;\r | |
2952 | joined_str = PyString_AsString(joined);\r | |
2953 | if (joined_str == NULL)\r | |
2954 | goto error;\r | |
2955 | \r | |
2956 | PyErr_Format(PyExc_TypeError,\r | |
2957 | "Can't instantiate abstract class %s "\r | |
2958 | "with abstract methods %s",\r | |
2959 | type->tp_name,\r | |
2960 | joined_str);\r | |
2961 | error:\r | |
2962 | Py_XDECREF(joined);\r | |
2963 | Py_XDECREF(sorted_methods);\r | |
2964 | Py_XDECREF(abstract_methods);\r | |
2965 | return NULL;\r | |
2966 | }\r | |
2967 | return type->tp_alloc(type, 0);\r | |
2968 | }\r | |
2969 | \r | |
2970 | static void\r | |
2971 | object_dealloc(PyObject *self)\r | |
2972 | {\r | |
2973 | Py_TYPE(self)->tp_free(self);\r | |
2974 | }\r | |
2975 | \r | |
2976 | static PyObject *\r | |
2977 | object_repr(PyObject *self)\r | |
2978 | {\r | |
2979 | PyTypeObject *type;\r | |
2980 | PyObject *mod, *name, *rtn;\r | |
2981 | \r | |
2982 | type = Py_TYPE(self);\r | |
2983 | mod = type_module(type, NULL);\r | |
2984 | if (mod == NULL)\r | |
2985 | PyErr_Clear();\r | |
2986 | else if (!PyString_Check(mod)) {\r | |
2987 | Py_DECREF(mod);\r | |
2988 | mod = NULL;\r | |
2989 | }\r | |
2990 | name = type_name(type, NULL);\r | |
2991 | if (name == NULL) {\r | |
2992 | Py_XDECREF(mod);\r | |
2993 | return NULL;\r | |
2994 | }\r | |
2995 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))\r | |
2996 | rtn = PyString_FromFormat("<%s.%s object at %p>",\r | |
2997 | PyString_AS_STRING(mod),\r | |
2998 | PyString_AS_STRING(name),\r | |
2999 | self);\r | |
3000 | else\r | |
3001 | rtn = PyString_FromFormat("<%s object at %p>",\r | |
3002 | type->tp_name, self);\r | |
3003 | Py_XDECREF(mod);\r | |
3004 | Py_DECREF(name);\r | |
3005 | return rtn;\r | |
3006 | }\r | |
3007 | \r | |
3008 | static PyObject *\r | |
3009 | object_str(PyObject *self)\r | |
3010 | {\r | |
3011 | unaryfunc f;\r | |
3012 | \r | |
3013 | f = Py_TYPE(self)->tp_repr;\r | |
3014 | if (f == NULL)\r | |
3015 | f = object_repr;\r | |
3016 | return f(self);\r | |
3017 | }\r | |
3018 | \r | |
3019 | static PyObject *\r | |
3020 | object_get_class(PyObject *self, void *closure)\r | |
3021 | {\r | |
3022 | Py_INCREF(Py_TYPE(self));\r | |
3023 | return (PyObject *)(Py_TYPE(self));\r | |
3024 | }\r | |
3025 | \r | |
3026 | static int\r | |
3027 | equiv_structs(PyTypeObject *a, PyTypeObject *b)\r | |
3028 | {\r | |
3029 | return a == b ||\r | |
3030 | (a != NULL &&\r | |
3031 | b != NULL &&\r | |
3032 | a->tp_basicsize == b->tp_basicsize &&\r | |
3033 | a->tp_itemsize == b->tp_itemsize &&\r | |
3034 | a->tp_dictoffset == b->tp_dictoffset &&\r | |
3035 | a->tp_weaklistoffset == b->tp_weaklistoffset &&\r | |
3036 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==\r | |
3037 | (b->tp_flags & Py_TPFLAGS_HAVE_GC)));\r | |
3038 | }\r | |
3039 | \r | |
3040 | static int\r | |
3041 | same_slots_added(PyTypeObject *a, PyTypeObject *b)\r | |
3042 | {\r | |
3043 | PyTypeObject *base = a->tp_base;\r | |
3044 | Py_ssize_t size;\r | |
3045 | PyObject *slots_a, *slots_b;\r | |
3046 | \r | |
3047 | assert(base == b->tp_base);\r | |
3048 | size = base->tp_basicsize;\r | |
3049 | if (a->tp_dictoffset == size && b->tp_dictoffset == size)\r | |
3050 | size += sizeof(PyObject *);\r | |
3051 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)\r | |
3052 | size += sizeof(PyObject *);\r | |
3053 | \r | |
3054 | /* Check slots compliance */\r | |
3055 | slots_a = ((PyHeapTypeObject *)a)->ht_slots;\r | |
3056 | slots_b = ((PyHeapTypeObject *)b)->ht_slots;\r | |
3057 | if (slots_a && slots_b) {\r | |
3058 | if (PyObject_Compare(slots_a, slots_b) != 0)\r | |
3059 | return 0;\r | |
3060 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);\r | |
3061 | }\r | |
3062 | return size == a->tp_basicsize && size == b->tp_basicsize;\r | |
3063 | }\r | |
3064 | \r | |
3065 | static int\r | |
3066 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)\r | |
3067 | {\r | |
3068 | PyTypeObject *newbase, *oldbase;\r | |
3069 | \r | |
3070 | if (newto->tp_dealloc != oldto->tp_dealloc ||\r | |
3071 | newto->tp_free != oldto->tp_free)\r | |
3072 | {\r | |
3073 | PyErr_Format(PyExc_TypeError,\r | |
3074 | "%s assignment: "\r | |
3075 | "'%s' deallocator differs from '%s'",\r | |
3076 | attr,\r | |
3077 | newto->tp_name,\r | |
3078 | oldto->tp_name);\r | |
3079 | return 0;\r | |
3080 | }\r | |
3081 | newbase = newto;\r | |
3082 | oldbase = oldto;\r | |
3083 | while (equiv_structs(newbase, newbase->tp_base))\r | |
3084 | newbase = newbase->tp_base;\r | |
3085 | while (equiv_structs(oldbase, oldbase->tp_base))\r | |
3086 | oldbase = oldbase->tp_base;\r | |
3087 | if (newbase != oldbase &&\r | |
3088 | (newbase->tp_base != oldbase->tp_base ||\r | |
3089 | !same_slots_added(newbase, oldbase))) {\r | |
3090 | PyErr_Format(PyExc_TypeError,\r | |
3091 | "%s assignment: "\r | |
3092 | "'%s' object layout differs from '%s'",\r | |
3093 | attr,\r | |
3094 | newto->tp_name,\r | |
3095 | oldto->tp_name);\r | |
3096 | return 0;\r | |
3097 | }\r | |
3098 | \r | |
3099 | return 1;\r | |
3100 | }\r | |
3101 | \r | |
3102 | static int\r | |
3103 | object_set_class(PyObject *self, PyObject *value, void *closure)\r | |
3104 | {\r | |
3105 | PyTypeObject *oldto = Py_TYPE(self);\r | |
3106 | PyTypeObject *newto;\r | |
3107 | \r | |
3108 | if (value == NULL) {\r | |
3109 | PyErr_SetString(PyExc_TypeError,\r | |
3110 | "can't delete __class__ attribute");\r | |
3111 | return -1;\r | |
3112 | }\r | |
3113 | if (!PyType_Check(value)) {\r | |
3114 | PyErr_Format(PyExc_TypeError,\r | |
3115 | "__class__ must be set to new-style class, not '%s' object",\r | |
3116 | Py_TYPE(value)->tp_name);\r | |
3117 | return -1;\r | |
3118 | }\r | |
3119 | newto = (PyTypeObject *)value;\r | |
3120 | if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||\r | |
3121 | !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))\r | |
3122 | {\r | |
3123 | PyErr_Format(PyExc_TypeError,\r | |
3124 | "__class__ assignment: only for heap types");\r | |
3125 | return -1;\r | |
3126 | }\r | |
3127 | if (compatible_for_assignment(newto, oldto, "__class__")) {\r | |
3128 | Py_INCREF(newto);\r | |
3129 | Py_TYPE(self) = newto;\r | |
3130 | Py_DECREF(oldto);\r | |
3131 | return 0;\r | |
3132 | }\r | |
3133 | else {\r | |
3134 | return -1;\r | |
3135 | }\r | |
3136 | }\r | |
3137 | \r | |
3138 | static PyGetSetDef object_getsets[] = {\r | |
3139 | {"__class__", object_get_class, object_set_class,\r | |
3140 | PyDoc_STR("the object's class")},\r | |
3141 | {0}\r | |
3142 | };\r | |
3143 | \r | |
3144 | \r | |
3145 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.\r | |
3146 | We fall back to helpers in copy_reg for:\r | |
3147 | - pickle protocols < 2\r | |
3148 | - calculating the list of slot names (done only once per class)\r | |
3149 | - the __newobj__ function (which is used as a token but never called)\r | |
3150 | */\r | |
3151 | \r | |
3152 | static PyObject *\r | |
3153 | import_copyreg(void)\r | |
3154 | {\r | |
3155 | static PyObject *copyreg_str;\r | |
3156 | \r | |
3157 | if (!copyreg_str) {\r | |
3158 | copyreg_str = PyString_InternFromString("copy_reg");\r | |
3159 | if (copyreg_str == NULL)\r | |
3160 | return NULL;\r | |
3161 | }\r | |
3162 | \r | |
3163 | return PyImport_Import(copyreg_str);\r | |
3164 | }\r | |
3165 | \r | |
3166 | static PyObject *\r | |
3167 | slotnames(PyObject *cls)\r | |
3168 | {\r | |
3169 | PyObject *clsdict;\r | |
3170 | PyObject *copyreg;\r | |
3171 | PyObject *slotnames;\r | |
3172 | \r | |
3173 | if (!PyType_Check(cls)) {\r | |
3174 | Py_INCREF(Py_None);\r | |
3175 | return Py_None;\r | |
3176 | }\r | |
3177 | \r | |
3178 | clsdict = ((PyTypeObject *)cls)->tp_dict;\r | |
3179 | slotnames = PyDict_GetItemString(clsdict, "__slotnames__");\r | |
3180 | if (slotnames != NULL && PyList_Check(slotnames)) {\r | |
3181 | Py_INCREF(slotnames);\r | |
3182 | return slotnames;\r | |
3183 | }\r | |
3184 | \r | |
3185 | copyreg = import_copyreg();\r | |
3186 | if (copyreg == NULL)\r | |
3187 | return NULL;\r | |
3188 | \r | |
3189 | slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);\r | |
3190 | Py_DECREF(copyreg);\r | |
3191 | if (slotnames != NULL &&\r | |
3192 | slotnames != Py_None &&\r | |
3193 | !PyList_Check(slotnames))\r | |
3194 | {\r | |
3195 | PyErr_SetString(PyExc_TypeError,\r | |
3196 | "copy_reg._slotnames didn't return a list or None");\r | |
3197 | Py_DECREF(slotnames);\r | |
3198 | slotnames = NULL;\r | |
3199 | }\r | |
3200 | \r | |
3201 | return slotnames;\r | |
3202 | }\r | |
3203 | \r | |
3204 | static PyObject *\r | |
3205 | reduce_2(PyObject *obj)\r | |
3206 | {\r | |
3207 | PyObject *cls, *getnewargs;\r | |
3208 | PyObject *args = NULL, *args2 = NULL;\r | |
3209 | PyObject *getstate = NULL, *state = NULL, *names = NULL;\r | |
3210 | PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;\r | |
3211 | PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;\r | |
3212 | Py_ssize_t i, n;\r | |
3213 | \r | |
3214 | cls = PyObject_GetAttrString(obj, "__class__");\r | |
3215 | if (cls == NULL)\r | |
3216 | return NULL;\r | |
3217 | \r | |
3218 | getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");\r | |
3219 | if (getnewargs != NULL) {\r | |
3220 | args = PyObject_CallObject(getnewargs, NULL);\r | |
3221 | Py_DECREF(getnewargs);\r | |
3222 | if (args != NULL && !PyTuple_Check(args)) {\r | |
3223 | PyErr_Format(PyExc_TypeError,\r | |
3224 | "__getnewargs__ should return a tuple, "\r | |
3225 | "not '%.200s'", Py_TYPE(args)->tp_name);\r | |
3226 | goto end;\r | |
3227 | }\r | |
3228 | }\r | |
3229 | else {\r | |
3230 | PyErr_Clear();\r | |
3231 | args = PyTuple_New(0);\r | |
3232 | }\r | |
3233 | if (args == NULL)\r | |
3234 | goto end;\r | |
3235 | \r | |
3236 | getstate = PyObject_GetAttrString(obj, "__getstate__");\r | |
3237 | if (getstate != NULL) {\r | |
3238 | state = PyObject_CallObject(getstate, NULL);\r | |
3239 | Py_DECREF(getstate);\r | |
3240 | if (state == NULL)\r | |
3241 | goto end;\r | |
3242 | }\r | |
3243 | else {\r | |
3244 | PyErr_Clear();\r | |
3245 | state = PyObject_GetAttrString(obj, "__dict__");\r | |
3246 | if (state == NULL) {\r | |
3247 | PyErr_Clear();\r | |
3248 | state = Py_None;\r | |
3249 | Py_INCREF(state);\r | |
3250 | }\r | |
3251 | names = slotnames(cls);\r | |
3252 | if (names == NULL)\r | |
3253 | goto end;\r | |
3254 | if (names != Py_None) {\r | |
3255 | assert(PyList_Check(names));\r | |
3256 | slots = PyDict_New();\r | |
3257 | if (slots == NULL)\r | |
3258 | goto end;\r | |
3259 | n = 0;\r | |
3260 | /* Can't pre-compute the list size; the list\r | |
3261 | is stored on the class so accessible to other\r | |
3262 | threads, which may be run by DECREF */\r | |
3263 | for (i = 0; i < PyList_GET_SIZE(names); i++) {\r | |
3264 | PyObject *name, *value;\r | |
3265 | name = PyList_GET_ITEM(names, i);\r | |
3266 | value = PyObject_GetAttr(obj, name);\r | |
3267 | if (value == NULL)\r | |
3268 | PyErr_Clear();\r | |
3269 | else {\r | |
3270 | int err = PyDict_SetItem(slots, name,\r | |
3271 | value);\r | |
3272 | Py_DECREF(value);\r | |
3273 | if (err)\r | |
3274 | goto end;\r | |
3275 | n++;\r | |
3276 | }\r | |
3277 | }\r | |
3278 | if (n) {\r | |
3279 | state = Py_BuildValue("(NO)", state, slots);\r | |
3280 | if (state == NULL)\r | |
3281 | goto end;\r | |
3282 | }\r | |
3283 | }\r | |
3284 | }\r | |
3285 | \r | |
3286 | if (!PyList_Check(obj)) {\r | |
3287 | listitems = Py_None;\r | |
3288 | Py_INCREF(listitems);\r | |
3289 | }\r | |
3290 | else {\r | |
3291 | listitems = PyObject_GetIter(obj);\r | |
3292 | if (listitems == NULL)\r | |
3293 | goto end;\r | |
3294 | }\r | |
3295 | \r | |
3296 | if (!PyDict_Check(obj)) {\r | |
3297 | dictitems = Py_None;\r | |
3298 | Py_INCREF(dictitems);\r | |
3299 | }\r | |
3300 | else {\r | |
3301 | dictitems = PyObject_CallMethod(obj, "iteritems", "");\r | |
3302 | if (dictitems == NULL)\r | |
3303 | goto end;\r | |
3304 | }\r | |
3305 | \r | |
3306 | copyreg = import_copyreg();\r | |
3307 | if (copyreg == NULL)\r | |
3308 | goto end;\r | |
3309 | newobj = PyObject_GetAttrString(copyreg, "__newobj__");\r | |
3310 | if (newobj == NULL)\r | |
3311 | goto end;\r | |
3312 | \r | |
3313 | n = PyTuple_GET_SIZE(args);\r | |
3314 | args2 = PyTuple_New(n+1);\r | |
3315 | if (args2 == NULL)\r | |
3316 | goto end;\r | |
3317 | PyTuple_SET_ITEM(args2, 0, cls);\r | |
3318 | cls = NULL;\r | |
3319 | for (i = 0; i < n; i++) {\r | |
3320 | PyObject *v = PyTuple_GET_ITEM(args, i);\r | |
3321 | Py_INCREF(v);\r | |
3322 | PyTuple_SET_ITEM(args2, i+1, v);\r | |
3323 | }\r | |
3324 | \r | |
3325 | res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);\r | |
3326 | \r | |
3327 | end:\r | |
3328 | Py_XDECREF(cls);\r | |
3329 | Py_XDECREF(args);\r | |
3330 | Py_XDECREF(args2);\r | |
3331 | Py_XDECREF(slots);\r | |
3332 | Py_XDECREF(state);\r | |
3333 | Py_XDECREF(names);\r | |
3334 | Py_XDECREF(listitems);\r | |
3335 | Py_XDECREF(dictitems);\r | |
3336 | Py_XDECREF(copyreg);\r | |
3337 | Py_XDECREF(newobj);\r | |
3338 | return res;\r | |
3339 | }\r | |
3340 | \r | |
3341 | /*\r | |
3342 | * There were two problems when object.__reduce__ and object.__reduce_ex__\r | |
3343 | * were implemented in the same function:\r | |
3344 | * - trying to pickle an object with a custom __reduce__ method that\r | |
3345 | * fell back to object.__reduce__ in certain circumstances led to\r | |
3346 | * infinite recursion at Python level and eventual RuntimeError.\r | |
3347 | * - Pickling objects that lied about their type by overwriting the\r | |
3348 | * __class__ descriptor could lead to infinite recursion at C level\r | |
3349 | * and eventual segfault.\r | |
3350 | *\r | |
3351 | * Because of backwards compatibility, the two methods still have to\r | |
3352 | * behave in the same way, even if this is not required by the pickle\r | |
3353 | * protocol. This common functionality was moved to the _common_reduce\r | |
3354 | * function.\r | |
3355 | */\r | |
3356 | static PyObject *\r | |
3357 | _common_reduce(PyObject *self, int proto)\r | |
3358 | {\r | |
3359 | PyObject *copyreg, *res;\r | |
3360 | \r | |
3361 | if (proto >= 2)\r | |
3362 | return reduce_2(self);\r | |
3363 | \r | |
3364 | copyreg = import_copyreg();\r | |
3365 | if (!copyreg)\r | |
3366 | return NULL;\r | |
3367 | \r | |
3368 | res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);\r | |
3369 | Py_DECREF(copyreg);\r | |
3370 | \r | |
3371 | return res;\r | |
3372 | }\r | |
3373 | \r | |
3374 | static PyObject *\r | |
3375 | object_reduce(PyObject *self, PyObject *args)\r | |
3376 | {\r | |
3377 | int proto = 0;\r | |
3378 | \r | |
3379 | if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))\r | |
3380 | return NULL;\r | |
3381 | \r | |
3382 | return _common_reduce(self, proto);\r | |
3383 | }\r | |
3384 | \r | |
3385 | static PyObject *\r | |
3386 | object_reduce_ex(PyObject *self, PyObject *args)\r | |
3387 | {\r | |
3388 | PyObject *reduce, *res;\r | |
3389 | int proto = 0;\r | |
3390 | \r | |
3391 | if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))\r | |
3392 | return NULL;\r | |
3393 | \r | |
3394 | reduce = PyObject_GetAttrString(self, "__reduce__");\r | |
3395 | if (reduce == NULL)\r | |
3396 | PyErr_Clear();\r | |
3397 | else {\r | |
3398 | PyObject *cls, *clsreduce, *objreduce;\r | |
3399 | int override;\r | |
3400 | cls = PyObject_GetAttrString(self, "__class__");\r | |
3401 | if (cls == NULL) {\r | |
3402 | Py_DECREF(reduce);\r | |
3403 | return NULL;\r | |
3404 | }\r | |
3405 | clsreduce = PyObject_GetAttrString(cls, "__reduce__");\r | |
3406 | Py_DECREF(cls);\r | |
3407 | if (clsreduce == NULL) {\r | |
3408 | Py_DECREF(reduce);\r | |
3409 | return NULL;\r | |
3410 | }\r | |
3411 | objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,\r | |
3412 | "__reduce__");\r | |
3413 | override = (clsreduce != objreduce);\r | |
3414 | Py_DECREF(clsreduce);\r | |
3415 | if (override) {\r | |
3416 | res = PyObject_CallObject(reduce, NULL);\r | |
3417 | Py_DECREF(reduce);\r | |
3418 | return res;\r | |
3419 | }\r | |
3420 | else\r | |
3421 | Py_DECREF(reduce);\r | |
3422 | }\r | |
3423 | \r | |
3424 | return _common_reduce(self, proto);\r | |
3425 | }\r | |
3426 | \r | |
3427 | static PyObject *\r | |
3428 | object_subclasshook(PyObject *cls, PyObject *args)\r | |
3429 | {\r | |
3430 | Py_INCREF(Py_NotImplemented);\r | |
3431 | return Py_NotImplemented;\r | |
3432 | }\r | |
3433 | \r | |
3434 | PyDoc_STRVAR(object_subclasshook_doc,\r | |
3435 | "Abstract classes can override this to customize issubclass().\n"\r | |
3436 | "\n"\r | |
3437 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"\r | |
3438 | "It should return True, False or NotImplemented. If it returns\n"\r | |
3439 | "NotImplemented, the normal algorithm is used. Otherwise, it\n"\r | |
3440 | "overrides the normal algorithm (and the outcome is cached).\n");\r | |
3441 | \r | |
3442 | /*\r | |
3443 | from PEP 3101, this code implements:\r | |
3444 | \r | |
3445 | class object:\r | |
3446 | def __format__(self, format_spec):\r | |
3447 | if isinstance(format_spec, str):\r | |
3448 | return format(str(self), format_spec)\r | |
3449 | elif isinstance(format_spec, unicode):\r | |
3450 | return format(unicode(self), format_spec)\r | |
3451 | */\r | |
3452 | static PyObject *\r | |
3453 | object_format(PyObject *self, PyObject *args)\r | |
3454 | {\r | |
3455 | PyObject *format_spec;\r | |
3456 | PyObject *self_as_str = NULL;\r | |
3457 | PyObject *result = NULL;\r | |
3458 | Py_ssize_t format_len;\r | |
3459 | \r | |
3460 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))\r | |
3461 | return NULL;\r | |
3462 | #ifdef Py_USING_UNICODE\r | |
3463 | if (PyUnicode_Check(format_spec)) {\r | |
3464 | format_len = PyUnicode_GET_SIZE(format_spec);\r | |
3465 | self_as_str = PyObject_Unicode(self);\r | |
3466 | } else if (PyString_Check(format_spec)) {\r | |
3467 | #else\r | |
3468 | if (PyString_Check(format_spec)) {\r | |
3469 | #endif\r | |
3470 | format_len = PyString_GET_SIZE(format_spec);\r | |
3471 | self_as_str = PyObject_Str(self);\r | |
3472 | } else {\r | |
3473 | PyErr_SetString(PyExc_TypeError,\r | |
3474 | "argument to __format__ must be unicode or str");\r | |
3475 | return NULL;\r | |
3476 | }\r | |
3477 | \r | |
3478 | if (self_as_str != NULL) {\r | |
3479 | /* Issue 7994: If we're converting to a string, we\r | |
3480 | should reject format specifications */\r | |
3481 | if (format_len > 0) {\r | |
3482 | if (PyErr_WarnEx(PyExc_PendingDeprecationWarning,\r | |
3483 | "object.__format__ with a non-empty format "\r | |
3484 | "string is deprecated", 1) < 0) {\r | |
3485 | goto done;\r | |
3486 | }\r | |
3487 | /* Eventually this will become an error:\r | |
3488 | PyErr_Format(PyExc_TypeError,\r | |
3489 | "non-empty format string passed to object.__format__");\r | |
3490 | goto done;\r | |
3491 | */\r | |
3492 | }\r | |
3493 | result = PyObject_Format(self_as_str, format_spec);\r | |
3494 | }\r | |
3495 | \r | |
3496 | done:\r | |
3497 | Py_XDECREF(self_as_str);\r | |
3498 | \r | |
3499 | return result;\r | |
3500 | }\r | |
3501 | \r | |
3502 | static PyObject *\r | |
3503 | object_sizeof(PyObject *self, PyObject *args)\r | |
3504 | {\r | |
3505 | Py_ssize_t res, isize;\r | |
3506 | \r | |
3507 | res = 0;\r | |
3508 | isize = self->ob_type->tp_itemsize;\r | |
3509 | if (isize > 0)\r | |
3510 | res = Py_SIZE(self) * isize;\r | |
3511 | res += self->ob_type->tp_basicsize;\r | |
3512 | \r | |
3513 | return PyInt_FromSsize_t(res);\r | |
3514 | }\r | |
3515 | \r | |
3516 | static PyMethodDef object_methods[] = {\r | |
3517 | {"__reduce_ex__", object_reduce_ex, METH_VARARGS,\r | |
3518 | PyDoc_STR("helper for pickle")},\r | |
3519 | {"__reduce__", object_reduce, METH_VARARGS,\r | |
3520 | PyDoc_STR("helper for pickle")},\r | |
3521 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,\r | |
3522 | object_subclasshook_doc},\r | |
3523 | {"__format__", object_format, METH_VARARGS,\r | |
3524 | PyDoc_STR("default object formatter")},\r | |
3525 | {"__sizeof__", object_sizeof, METH_NOARGS,\r | |
3526 | PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")},\r | |
3527 | {0}\r | |
3528 | };\r | |
3529 | \r | |
3530 | \r | |
3531 | PyTypeObject PyBaseObject_Type = {\r | |
3532 | PyVarObject_HEAD_INIT(&PyType_Type, 0)\r | |
3533 | "object", /* tp_name */\r | |
3534 | sizeof(PyObject), /* tp_basicsize */\r | |
3535 | 0, /* tp_itemsize */\r | |
3536 | object_dealloc, /* tp_dealloc */\r | |
3537 | 0, /* tp_print */\r | |
3538 | 0, /* tp_getattr */\r | |
3539 | 0, /* tp_setattr */\r | |
3540 | 0, /* tp_compare */\r | |
3541 | object_repr, /* tp_repr */\r | |
3542 | 0, /* tp_as_number */\r | |
3543 | 0, /* tp_as_sequence */\r | |
3544 | 0, /* tp_as_mapping */\r | |
3545 | (hashfunc)_Py_HashPointer, /* tp_hash */\r | |
3546 | 0, /* tp_call */\r | |
3547 | object_str, /* tp_str */\r | |
3548 | PyObject_GenericGetAttr, /* tp_getattro */\r | |
3549 | PyObject_GenericSetAttr, /* tp_setattro */\r | |
3550 | 0, /* tp_as_buffer */\r | |
3551 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */\r | |
3552 | PyDoc_STR("The most base type"), /* tp_doc */\r | |
3553 | 0, /* tp_traverse */\r | |
3554 | 0, /* tp_clear */\r | |
3555 | 0, /* tp_richcompare */\r | |
3556 | 0, /* tp_weaklistoffset */\r | |
3557 | 0, /* tp_iter */\r | |
3558 | 0, /* tp_iternext */\r | |
3559 | object_methods, /* tp_methods */\r | |
3560 | 0, /* tp_members */\r | |
3561 | object_getsets, /* tp_getset */\r | |
3562 | 0, /* tp_base */\r | |
3563 | 0, /* tp_dict */\r | |
3564 | 0, /* tp_descr_get */\r | |
3565 | 0, /* tp_descr_set */\r | |
3566 | 0, /* tp_dictoffset */\r | |
3567 | object_init, /* tp_init */\r | |
3568 | PyType_GenericAlloc, /* tp_alloc */\r | |
3569 | object_new, /* tp_new */\r | |
3570 | PyObject_Del, /* tp_free */\r | |
3571 | };\r | |
3572 | \r | |
3573 | \r | |
3574 | /* Initialize the __dict__ in a type object */\r | |
3575 | \r | |
3576 | static int\r | |
3577 | add_methods(PyTypeObject *type, PyMethodDef *meth)\r | |
3578 | {\r | |
3579 | PyObject *dict = type->tp_dict;\r | |
3580 | \r | |
3581 | for (; meth->ml_name != NULL; meth++) {\r | |
3582 | PyObject *descr;\r | |
3583 | int err;\r | |
3584 | if (PyDict_GetItemString(dict, meth->ml_name) &&\r | |
3585 | !(meth->ml_flags & METH_COEXIST))\r | |
3586 | continue;\r | |
3587 | if (meth->ml_flags & METH_CLASS) {\r | |
3588 | if (meth->ml_flags & METH_STATIC) {\r | |
3589 | PyErr_SetString(PyExc_ValueError,\r | |
3590 | "method cannot be both class and static");\r | |
3591 | return -1;\r | |
3592 | }\r | |
3593 | descr = PyDescr_NewClassMethod(type, meth);\r | |
3594 | }\r | |
3595 | else if (meth->ml_flags & METH_STATIC) {\r | |
3596 | PyObject *cfunc = PyCFunction_New(meth, NULL);\r | |
3597 | if (cfunc == NULL)\r | |
3598 | return -1;\r | |
3599 | descr = PyStaticMethod_New(cfunc);\r | |
3600 | Py_DECREF(cfunc);\r | |
3601 | }\r | |
3602 | else {\r | |
3603 | descr = PyDescr_NewMethod(type, meth);\r | |
3604 | }\r | |
3605 | if (descr == NULL)\r | |
3606 | return -1;\r | |
3607 | err = PyDict_SetItemString(dict, meth->ml_name, descr);\r | |
3608 | Py_DECREF(descr);\r | |
3609 | if (err < 0)\r | |
3610 | return -1;\r | |
3611 | }\r | |
3612 | return 0;\r | |
3613 | }\r | |
3614 | \r | |
3615 | static int\r | |
3616 | add_members(PyTypeObject *type, PyMemberDef *memb)\r | |
3617 | {\r | |
3618 | PyObject *dict = type->tp_dict;\r | |
3619 | \r | |
3620 | for (; memb->name != NULL; memb++) {\r | |
3621 | PyObject *descr;\r | |
3622 | if (PyDict_GetItemString(dict, memb->name))\r | |
3623 | continue;\r | |
3624 | descr = PyDescr_NewMember(type, memb);\r | |
3625 | if (descr == NULL)\r | |
3626 | return -1;\r | |
3627 | if (PyDict_SetItemString(dict, memb->name, descr) < 0)\r | |
3628 | return -1;\r | |
3629 | Py_DECREF(descr);\r | |
3630 | }\r | |
3631 | return 0;\r | |
3632 | }\r | |
3633 | \r | |
3634 | static int\r | |
3635 | add_getset(PyTypeObject *type, PyGetSetDef *gsp)\r | |
3636 | {\r | |
3637 | PyObject *dict = type->tp_dict;\r | |
3638 | \r | |
3639 | for (; gsp->name != NULL; gsp++) {\r | |
3640 | PyObject *descr;\r | |
3641 | if (PyDict_GetItemString(dict, gsp->name))\r | |
3642 | continue;\r | |
3643 | descr = PyDescr_NewGetSet(type, gsp);\r | |
3644 | \r | |
3645 | if (descr == NULL)\r | |
3646 | return -1;\r | |
3647 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0)\r | |
3648 | return -1;\r | |
3649 | Py_DECREF(descr);\r | |
3650 | }\r | |
3651 | return 0;\r | |
3652 | }\r | |
3653 | \r | |
3654 | #define BUFFER_FLAGS (Py_TPFLAGS_HAVE_GETCHARBUFFER | Py_TPFLAGS_HAVE_NEWBUFFER)\r | |
3655 | \r | |
3656 | static void\r | |
3657 | inherit_special(PyTypeObject *type, PyTypeObject *base)\r | |
3658 | {\r | |
3659 | Py_ssize_t oldsize, newsize;\r | |
3660 | \r | |
3661 | /* Special flag magic */\r | |
3662 | if (!type->tp_as_buffer && base->tp_as_buffer) {\r | |
3663 | type->tp_flags &= ~BUFFER_FLAGS;\r | |
3664 | type->tp_flags |=\r | |
3665 | base->tp_flags & BUFFER_FLAGS;\r | |
3666 | }\r | |
3667 | if (!type->tp_as_sequence && base->tp_as_sequence) {\r | |
3668 | type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;\r | |
3669 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;\r | |
3670 | }\r | |
3671 | if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=\r | |
3672 | (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {\r | |
3673 | if ((!type->tp_as_number && base->tp_as_number) ||\r | |
3674 | (!type->tp_as_sequence && base->tp_as_sequence)) {\r | |
3675 | type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;\r | |
3676 | if (!type->tp_as_number && !type->tp_as_sequence) {\r | |
3677 | type->tp_flags |= base->tp_flags &\r | |
3678 | Py_TPFLAGS_HAVE_INPLACEOPS;\r | |
3679 | }\r | |
3680 | }\r | |
3681 | /* Wow */\r | |
3682 | }\r | |
3683 | if (!type->tp_as_number && base->tp_as_number) {\r | |
3684 | type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;\r | |
3685 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;\r | |
3686 | }\r | |
3687 | \r | |
3688 | /* Copying basicsize is connected to the GC flags */\r | |
3689 | oldsize = base->tp_basicsize;\r | |
3690 | newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;\r | |
3691 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&\r | |
3692 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&\r | |
3693 | (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&\r | |
3694 | (!type->tp_traverse && !type->tp_clear)) {\r | |
3695 | type->tp_flags |= Py_TPFLAGS_HAVE_GC;\r | |
3696 | if (type->tp_traverse == NULL)\r | |
3697 | type->tp_traverse = base->tp_traverse;\r | |
3698 | if (type->tp_clear == NULL)\r | |
3699 | type->tp_clear = base->tp_clear;\r | |
3700 | }\r | |
3701 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {\r | |
3702 | /* The condition below could use some explanation.\r | |
3703 | It appears that tp_new is not inherited for static types\r | |
3704 | whose base class is 'object'; this seems to be a precaution\r | |
3705 | so that old extension types don't suddenly become\r | |
3706 | callable (object.__new__ wouldn't insure the invariants\r | |
3707 | that the extension type's own factory function ensures).\r | |
3708 | Heap types, of course, are under our control, so they do\r | |
3709 | inherit tp_new; static extension types that specify some\r | |
3710 | other built-in type as the default are considered\r | |
3711 | new-style-aware so they also inherit object.__new__. */\r | |
3712 | if (base != &PyBaseObject_Type ||\r | |
3713 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {\r | |
3714 | if (type->tp_new == NULL)\r | |
3715 | type->tp_new = base->tp_new;\r | |
3716 | }\r | |
3717 | }\r | |
3718 | type->tp_basicsize = newsize;\r | |
3719 | \r | |
3720 | /* Copy other non-function slots */\r | |
3721 | \r | |
3722 | #undef COPYVAL\r | |
3723 | #define COPYVAL(SLOT) \\r | |
3724 | if (type->SLOT == 0) type->SLOT = base->SLOT\r | |
3725 | \r | |
3726 | COPYVAL(tp_itemsize);\r | |
3727 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {\r | |
3728 | COPYVAL(tp_weaklistoffset);\r | |
3729 | }\r | |
3730 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {\r | |
3731 | COPYVAL(tp_dictoffset);\r | |
3732 | }\r | |
3733 | \r | |
3734 | /* Setup fast subclass flags */\r | |
3735 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))\r | |
3736 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;\r | |
3737 | else if (PyType_IsSubtype(base, &PyType_Type))\r | |
3738 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;\r | |
3739 | else if (PyType_IsSubtype(base, &PyInt_Type))\r | |
3740 | type->tp_flags |= Py_TPFLAGS_INT_SUBCLASS;\r | |
3741 | else if (PyType_IsSubtype(base, &PyLong_Type))\r | |
3742 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;\r | |
3743 | else if (PyType_IsSubtype(base, &PyString_Type))\r | |
3744 | type->tp_flags |= Py_TPFLAGS_STRING_SUBCLASS;\r | |
3745 | #ifdef Py_USING_UNICODE\r | |
3746 | else if (PyType_IsSubtype(base, &PyUnicode_Type))\r | |
3747 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;\r | |
3748 | #endif\r | |
3749 | else if (PyType_IsSubtype(base, &PyTuple_Type))\r | |
3750 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;\r | |
3751 | else if (PyType_IsSubtype(base, &PyList_Type))\r | |
3752 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;\r | |
3753 | else if (PyType_IsSubtype(base, &PyDict_Type))\r | |
3754 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;\r | |
3755 | }\r | |
3756 | \r | |
3757 | static int\r | |
3758 | overrides_name(PyTypeObject *type, char *name)\r | |
3759 | {\r | |
3760 | PyObject *dict = type->tp_dict;\r | |
3761 | \r | |
3762 | assert(dict != NULL);\r | |
3763 | if (PyDict_GetItemString(dict, name) != NULL) {\r | |
3764 | return 1;\r | |
3765 | }\r | |
3766 | return 0;\r | |
3767 | }\r | |
3768 | \r | |
3769 | #define OVERRIDES_HASH(x) overrides_name(x, "__hash__")\r | |
3770 | #define OVERRIDES_EQ(x) overrides_name(x, "__eq__")\r | |
3771 | \r | |
3772 | static void\r | |
3773 | inherit_slots(PyTypeObject *type, PyTypeObject *base)\r | |
3774 | {\r | |
3775 | PyTypeObject *basebase;\r | |
3776 | \r | |
3777 | #undef SLOTDEFINED\r | |
3778 | #undef COPYSLOT\r | |
3779 | #undef COPYNUM\r | |
3780 | #undef COPYSEQ\r | |
3781 | #undef COPYMAP\r | |
3782 | #undef COPYBUF\r | |
3783 | \r | |
3784 | #define SLOTDEFINED(SLOT) \\r | |
3785 | (base->SLOT != 0 && \\r | |
3786 | (basebase == NULL || base->SLOT != basebase->SLOT))\r | |
3787 | \r | |
3788 | #define COPYSLOT(SLOT) \\r | |
3789 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT\r | |
3790 | \r | |
3791 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)\r | |
3792 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)\r | |
3793 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)\r | |
3794 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)\r | |
3795 | \r | |
3796 | /* This won't inherit indirect slots (from tp_as_number etc.)\r | |
3797 | if type doesn't provide the space. */\r | |
3798 | \r | |
3799 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) {\r | |
3800 | basebase = base->tp_base;\r | |
3801 | if (basebase->tp_as_number == NULL)\r | |
3802 | basebase = NULL;\r | |
3803 | COPYNUM(nb_add);\r | |
3804 | COPYNUM(nb_subtract);\r | |
3805 | COPYNUM(nb_multiply);\r | |
3806 | COPYNUM(nb_divide);\r | |
3807 | COPYNUM(nb_remainder);\r | |
3808 | COPYNUM(nb_divmod);\r | |
3809 | COPYNUM(nb_power);\r | |
3810 | COPYNUM(nb_negative);\r | |
3811 | COPYNUM(nb_positive);\r | |
3812 | COPYNUM(nb_absolute);\r | |
3813 | COPYNUM(nb_nonzero);\r | |
3814 | COPYNUM(nb_invert);\r | |
3815 | COPYNUM(nb_lshift);\r | |
3816 | COPYNUM(nb_rshift);\r | |
3817 | COPYNUM(nb_and);\r | |
3818 | COPYNUM(nb_xor);\r | |
3819 | COPYNUM(nb_or);\r | |
3820 | COPYNUM(nb_coerce);\r | |
3821 | COPYNUM(nb_int);\r | |
3822 | COPYNUM(nb_long);\r | |
3823 | COPYNUM(nb_float);\r | |
3824 | COPYNUM(nb_oct);\r | |
3825 | COPYNUM(nb_hex);\r | |
3826 | COPYNUM(nb_inplace_add);\r | |
3827 | COPYNUM(nb_inplace_subtract);\r | |
3828 | COPYNUM(nb_inplace_multiply);\r | |
3829 | COPYNUM(nb_inplace_divide);\r | |
3830 | COPYNUM(nb_inplace_remainder);\r | |
3831 | COPYNUM(nb_inplace_power);\r | |
3832 | COPYNUM(nb_inplace_lshift);\r | |
3833 | COPYNUM(nb_inplace_rshift);\r | |
3834 | COPYNUM(nb_inplace_and);\r | |
3835 | COPYNUM(nb_inplace_xor);\r | |
3836 | COPYNUM(nb_inplace_or);\r | |
3837 | if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {\r | |
3838 | COPYNUM(nb_true_divide);\r | |
3839 | COPYNUM(nb_floor_divide);\r | |
3840 | COPYNUM(nb_inplace_true_divide);\r | |
3841 | COPYNUM(nb_inplace_floor_divide);\r | |
3842 | }\r | |
3843 | if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) {\r | |
3844 | COPYNUM(nb_index);\r | |
3845 | }\r | |
3846 | }\r | |
3847 | \r | |
3848 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {\r | |
3849 | basebase = base->tp_base;\r | |
3850 | if (basebase->tp_as_sequence == NULL)\r | |
3851 | basebase = NULL;\r | |
3852 | COPYSEQ(sq_length);\r | |
3853 | COPYSEQ(sq_concat);\r | |
3854 | COPYSEQ(sq_repeat);\r | |
3855 | COPYSEQ(sq_item);\r | |
3856 | COPYSEQ(sq_slice);\r | |
3857 | COPYSEQ(sq_ass_item);\r | |
3858 | COPYSEQ(sq_ass_slice);\r | |
3859 | COPYSEQ(sq_contains);\r | |
3860 | COPYSEQ(sq_inplace_concat);\r | |
3861 | COPYSEQ(sq_inplace_repeat);\r | |
3862 | }\r | |
3863 | \r | |
3864 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {\r | |
3865 | basebase = base->tp_base;\r | |
3866 | if (basebase->tp_as_mapping == NULL)\r | |
3867 | basebase = NULL;\r | |
3868 | COPYMAP(mp_length);\r | |
3869 | COPYMAP(mp_subscript);\r | |
3870 | COPYMAP(mp_ass_subscript);\r | |
3871 | }\r | |
3872 | \r | |
3873 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {\r | |
3874 | basebase = base->tp_base;\r | |
3875 | if (basebase->tp_as_buffer == NULL)\r | |
3876 | basebase = NULL;\r | |
3877 | COPYBUF(bf_getreadbuffer);\r | |
3878 | COPYBUF(bf_getwritebuffer);\r | |
3879 | COPYBUF(bf_getsegcount);\r | |
3880 | COPYBUF(bf_getcharbuffer);\r | |
3881 | COPYBUF(bf_getbuffer);\r | |
3882 | COPYBUF(bf_releasebuffer);\r | |
3883 | }\r | |
3884 | \r | |
3885 | basebase = base->tp_base;\r | |
3886 | \r | |
3887 | COPYSLOT(tp_dealloc);\r | |
3888 | COPYSLOT(tp_print);\r | |
3889 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) {\r | |
3890 | type->tp_getattr = base->tp_getattr;\r | |
3891 | type->tp_getattro = base->tp_getattro;\r | |
3892 | }\r | |
3893 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) {\r | |
3894 | type->tp_setattr = base->tp_setattr;\r | |
3895 | type->tp_setattro = base->tp_setattro;\r | |
3896 | }\r | |
3897 | /* tp_compare see tp_richcompare */\r | |
3898 | COPYSLOT(tp_repr);\r | |
3899 | /* tp_hash see tp_richcompare */\r | |
3900 | COPYSLOT(tp_call);\r | |
3901 | COPYSLOT(tp_str);\r | |
3902 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {\r | |
3903 | if (type->tp_compare == NULL &&\r | |
3904 | type->tp_richcompare == NULL &&\r | |
3905 | type->tp_hash == NULL)\r | |
3906 | {\r | |
3907 | type->tp_compare = base->tp_compare;\r | |
3908 | type->tp_richcompare = base->tp_richcompare;\r | |
3909 | type->tp_hash = base->tp_hash;\r | |
3910 | /* Check for changes to inherited methods in Py3k*/\r | |
3911 | if (Py_Py3kWarningFlag) {\r | |
3912 | if (base->tp_hash &&\r | |
3913 | (base->tp_hash != PyObject_HashNotImplemented) &&\r | |
3914 | !OVERRIDES_HASH(type)) {\r | |
3915 | if (OVERRIDES_EQ(type)) {\r | |
3916 | if (PyErr_WarnPy3k("Overriding "\r | |
3917 | "__eq__ blocks inheritance "\r | |
3918 | "of __hash__ in 3.x",\r | |
3919 | 1) < 0)\r | |
3920 | /* XXX This isn't right. If the warning is turned\r | |
3921 | into an exception, we should be communicating\r | |
3922 | the error back to the caller, but figuring out\r | |
3923 | how to clean up in that case is tricky. See\r | |
3924 | issue 8627 for more. */\r | |
3925 | PyErr_Clear();\r | |
3926 | }\r | |
3927 | }\r | |
3928 | }\r | |
3929 | }\r | |
3930 | }\r | |
3931 | else {\r | |
3932 | COPYSLOT(tp_compare);\r | |
3933 | }\r | |
3934 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {\r | |
3935 | COPYSLOT(tp_iter);\r | |
3936 | COPYSLOT(tp_iternext);\r | |
3937 | }\r | |
3938 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {\r | |
3939 | COPYSLOT(tp_descr_get);\r | |
3940 | COPYSLOT(tp_descr_set);\r | |
3941 | COPYSLOT(tp_dictoffset);\r | |
3942 | COPYSLOT(tp_init);\r | |
3943 | COPYSLOT(tp_alloc);\r | |
3944 | COPYSLOT(tp_is_gc);\r | |
3945 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==\r | |
3946 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {\r | |
3947 | /* They agree about gc. */\r | |
3948 | COPYSLOT(tp_free);\r | |
3949 | }\r | |
3950 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&\r | |
3951 | type->tp_free == NULL &&\r | |
3952 | base->tp_free == _PyObject_Del) {\r | |
3953 | /* A bit of magic to plug in the correct default\r | |
3954 | * tp_free function when a derived class adds gc,\r | |
3955 | * didn't define tp_free, and the base uses the\r | |
3956 | * default non-gc tp_free.\r | |
3957 | */\r | |
3958 | type->tp_free = PyObject_GC_Del;\r | |
3959 | }\r | |
3960 | /* else they didn't agree about gc, and there isn't something\r | |
3961 | * obvious to be done -- the type is on its own.\r | |
3962 | */\r | |
3963 | }\r | |
3964 | }\r | |
3965 | \r | |
3966 | static int add_operators(PyTypeObject *);\r | |
3967 | \r | |
3968 | int\r | |
3969 | PyType_Ready(PyTypeObject *type)\r | |
3970 | {\r | |
3971 | PyObject *dict, *bases;\r | |
3972 | PyTypeObject *base;\r | |
3973 | Py_ssize_t i, n;\r | |
3974 | \r | |
3975 | if (type->tp_flags & Py_TPFLAGS_READY) {\r | |
3976 | assert(type->tp_dict != NULL);\r | |
3977 | return 0;\r | |
3978 | }\r | |
3979 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);\r | |
3980 | \r | |
3981 | type->tp_flags |= Py_TPFLAGS_READYING;\r | |
3982 | \r | |
3983 | #ifdef Py_TRACE_REFS\r | |
3984 | /* PyType_Ready is the closest thing we have to a choke point\r | |
3985 | * for type objects, so is the best place I can think of to try\r | |
3986 | * to get type objects into the doubly-linked list of all objects.\r | |
3987 | * Still, not all type objects go thru PyType_Ready.\r | |
3988 | */\r | |
3989 | _Py_AddToAllObjects((PyObject *)type, 0);\r | |
3990 | #endif\r | |
3991 | \r | |
3992 | /* Initialize tp_base (defaults to BaseObject unless that's us) */\r | |
3993 | base = type->tp_base;\r | |
3994 | if (base == NULL && type != &PyBaseObject_Type) {\r | |
3995 | base = type->tp_base = &PyBaseObject_Type;\r | |
3996 | Py_INCREF(base);\r | |
3997 | }\r | |
3998 | \r | |
3999 | /* Now the only way base can still be NULL is if type is\r | |
4000 | * &PyBaseObject_Type.\r | |
4001 | */\r | |
4002 | \r | |
4003 | /* Initialize the base class */\r | |
4004 | if (base && base->tp_dict == NULL) {\r | |
4005 | if (PyType_Ready(base) < 0)\r | |
4006 | goto error;\r | |
4007 | }\r | |
4008 | \r | |
4009 | /* Initialize ob_type if NULL. This means extensions that want to be\r | |
4010 | compilable separately on Windows can call PyType_Ready() instead of\r | |
4011 | initializing the ob_type field of their type objects. */\r | |
4012 | /* The test for base != NULL is really unnecessary, since base is only\r | |
4013 | NULL when type is &PyBaseObject_Type, and we know its ob_type is\r | |
4014 | not NULL (it's initialized to &PyType_Type). But coverity doesn't\r | |
4015 | know that. */\r | |
4016 | if (Py_TYPE(type) == NULL && base != NULL)\r | |
4017 | Py_TYPE(type) = Py_TYPE(base);\r | |
4018 | \r | |
4019 | /* Initialize tp_bases */\r | |
4020 | bases = type->tp_bases;\r | |
4021 | if (bases == NULL) {\r | |
4022 | if (base == NULL)\r | |
4023 | bases = PyTuple_New(0);\r | |
4024 | else\r | |
4025 | bases = PyTuple_Pack(1, base);\r | |
4026 | if (bases == NULL)\r | |
4027 | goto error;\r | |
4028 | type->tp_bases = bases;\r | |
4029 | }\r | |
4030 | \r | |
4031 | /* Initialize tp_dict */\r | |
4032 | dict = type->tp_dict;\r | |
4033 | if (dict == NULL) {\r | |
4034 | dict = PyDict_New();\r | |
4035 | if (dict == NULL)\r | |
4036 | goto error;\r | |
4037 | type->tp_dict = dict;\r | |
4038 | }\r | |
4039 | \r | |
4040 | /* Add type-specific descriptors to tp_dict */\r | |
4041 | if (add_operators(type) < 0)\r | |
4042 | goto error;\r | |
4043 | if (type->tp_methods != NULL) {\r | |
4044 | if (add_methods(type, type->tp_methods) < 0)\r | |
4045 | goto error;\r | |
4046 | }\r | |
4047 | if (type->tp_members != NULL) {\r | |
4048 | if (add_members(type, type->tp_members) < 0)\r | |
4049 | goto error;\r | |
4050 | }\r | |
4051 | if (type->tp_getset != NULL) {\r | |
4052 | if (add_getset(type, type->tp_getset) < 0)\r | |
4053 | goto error;\r | |
4054 | }\r | |
4055 | \r | |
4056 | /* Calculate method resolution order */\r | |
4057 | if (mro_internal(type) < 0) {\r | |
4058 | goto error;\r | |
4059 | }\r | |
4060 | \r | |
4061 | /* Inherit special flags from dominant base */\r | |
4062 | if (type->tp_base != NULL)\r | |
4063 | inherit_special(type, type->tp_base);\r | |
4064 | \r | |
4065 | /* Initialize tp_dict properly */\r | |
4066 | bases = type->tp_mro;\r | |
4067 | assert(bases != NULL);\r | |
4068 | assert(PyTuple_Check(bases));\r | |
4069 | n = PyTuple_GET_SIZE(bases);\r | |
4070 | for (i = 1; i < n; i++) {\r | |
4071 | PyObject *b = PyTuple_GET_ITEM(bases, i);\r | |
4072 | if (PyType_Check(b))\r | |
4073 | inherit_slots(type, (PyTypeObject *)b);\r | |
4074 | }\r | |
4075 | \r | |
4076 | /* All bases of statically allocated type should be statically allocated */\r | |
4077 | if (Py_Py3kWarningFlag && !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))\r | |
4078 | for (i = 0; i < n; i++) {\r | |
4079 | PyObject *b = PyTuple_GET_ITEM(bases, i);\r | |
4080 | if (PyType_Check(b) &&\r | |
4081 | (((PyTypeObject *)b)->tp_flags & Py_TPFLAGS_HEAPTYPE)) {\r | |
4082 | char buf[300];\r | |
4083 | PyOS_snprintf(buf, sizeof(buf),\r | |
4084 | "type '%.100s' is not dynamically allocated but "\r | |
4085 | "its base type '%.100s' is dynamically allocated",\r | |
4086 | type->tp_name, ((PyTypeObject *)b)->tp_name);\r | |
4087 | if (PyErr_WarnPy3k(buf, 1) < 0)\r | |
4088 | goto error;\r | |
4089 | break;\r | |
4090 | }\r | |
4091 | }\r | |
4092 | \r | |
4093 | /* Sanity check for tp_free. */\r | |
4094 | if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&\r | |
4095 | (type->tp_free == NULL || type->tp_free == PyObject_Del)) {\r | |
4096 | /* This base class needs to call tp_free, but doesn't have\r | |
4097 | * one, or its tp_free is for non-gc'ed objects.\r | |
4098 | */\r | |
4099 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "\r | |
4100 | "gc and is a base type but has inappropriate "\r | |
4101 | "tp_free slot",\r | |
4102 | type->tp_name);\r | |
4103 | goto error;\r | |
4104 | }\r | |
4105 | \r | |
4106 | /* if the type dictionary doesn't contain a __doc__, set it from\r | |
4107 | the tp_doc slot.\r | |
4108 | */\r | |
4109 | if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {\r | |
4110 | if (type->tp_doc != NULL) {\r | |
4111 | PyObject *doc = PyString_FromString(type->tp_doc);\r | |
4112 | if (doc == NULL)\r | |
4113 | goto error;\r | |
4114 | PyDict_SetItemString(type->tp_dict, "__doc__", doc);\r | |
4115 | Py_DECREF(doc);\r | |
4116 | } else {\r | |
4117 | PyDict_SetItemString(type->tp_dict,\r | |
4118 | "__doc__", Py_None);\r | |
4119 | }\r | |
4120 | }\r | |
4121 | \r | |
4122 | /* Some more special stuff */\r | |
4123 | base = type->tp_base;\r | |
4124 | if (base != NULL) {\r | |
4125 | if (type->tp_as_number == NULL)\r | |
4126 | type->tp_as_number = base->tp_as_number;\r | |
4127 | if (type->tp_as_sequence == NULL)\r | |
4128 | type->tp_as_sequence = base->tp_as_sequence;\r | |
4129 | if (type->tp_as_mapping == NULL)\r | |
4130 | type->tp_as_mapping = base->tp_as_mapping;\r | |
4131 | if (type->tp_as_buffer == NULL)\r | |
4132 | type->tp_as_buffer = base->tp_as_buffer;\r | |
4133 | }\r | |
4134 | \r | |
4135 | /* Link into each base class's list of subclasses */\r | |
4136 | bases = type->tp_bases;\r | |
4137 | n = PyTuple_GET_SIZE(bases);\r | |
4138 | for (i = 0; i < n; i++) {\r | |
4139 | PyObject *b = PyTuple_GET_ITEM(bases, i);\r | |
4140 | if (PyType_Check(b) &&\r | |
4141 | add_subclass((PyTypeObject *)b, type) < 0)\r | |
4142 | goto error;\r | |
4143 | }\r | |
4144 | \r | |
4145 | /* All done -- set the ready flag */\r | |
4146 | assert(type->tp_dict != NULL);\r | |
4147 | type->tp_flags =\r | |
4148 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;\r | |
4149 | return 0;\r | |
4150 | \r | |
4151 | error:\r | |
4152 | type->tp_flags &= ~Py_TPFLAGS_READYING;\r | |
4153 | return -1;\r | |
4154 | }\r | |
4155 | \r | |
4156 | static int\r | |
4157 | add_subclass(PyTypeObject *base, PyTypeObject *type)\r | |
4158 | {\r | |
4159 | Py_ssize_t i;\r | |
4160 | int result;\r | |
4161 | PyObject *list, *ref, *newobj;\r | |
4162 | \r | |
4163 | list = base->tp_subclasses;\r | |
4164 | if (list == NULL) {\r | |
4165 | base->tp_subclasses = list = PyList_New(0);\r | |
4166 | if (list == NULL)\r | |
4167 | return -1;\r | |
4168 | }\r | |
4169 | assert(PyList_Check(list));\r | |
4170 | newobj = PyWeakref_NewRef((PyObject *)type, NULL);\r | |
4171 | i = PyList_GET_SIZE(list);\r | |
4172 | while (--i >= 0) {\r | |
4173 | ref = PyList_GET_ITEM(list, i);\r | |
4174 | assert(PyWeakref_CheckRef(ref));\r | |
4175 | if (PyWeakref_GET_OBJECT(ref) == Py_None)\r | |
4176 | return PyList_SetItem(list, i, newobj);\r | |
4177 | }\r | |
4178 | result = PyList_Append(list, newobj);\r | |
4179 | Py_DECREF(newobj);\r | |
4180 | return result;\r | |
4181 | }\r | |
4182 | \r | |
4183 | static void\r | |
4184 | remove_subclass(PyTypeObject *base, PyTypeObject *type)\r | |
4185 | {\r | |
4186 | Py_ssize_t i;\r | |
4187 | PyObject *list, *ref;\r | |
4188 | \r | |
4189 | list = base->tp_subclasses;\r | |
4190 | if (list == NULL) {\r | |
4191 | return;\r | |
4192 | }\r | |
4193 | assert(PyList_Check(list));\r | |
4194 | i = PyList_GET_SIZE(list);\r | |
4195 | while (--i >= 0) {\r | |
4196 | ref = PyList_GET_ITEM(list, i);\r | |
4197 | assert(PyWeakref_CheckRef(ref));\r | |
4198 | if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {\r | |
4199 | /* this can't fail, right? */\r | |
4200 | PySequence_DelItem(list, i);\r | |
4201 | return;\r | |
4202 | }\r | |
4203 | }\r | |
4204 | }\r | |
4205 | \r | |
4206 | static int\r | |
4207 | check_num_args(PyObject *ob, int n)\r | |
4208 | {\r | |
4209 | if (!PyTuple_CheckExact(ob)) {\r | |
4210 | PyErr_SetString(PyExc_SystemError,\r | |
4211 | "PyArg_UnpackTuple() argument list is not a tuple");\r | |
4212 | return 0;\r | |
4213 | }\r | |
4214 | if (n == PyTuple_GET_SIZE(ob))\r | |
4215 | return 1;\r | |
4216 | PyErr_Format(\r | |
4217 | PyExc_TypeError,\r | |
4218 | "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));\r | |
4219 | return 0;\r | |
4220 | }\r | |
4221 | \r | |
4222 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */\r | |
4223 | \r | |
4224 | /* There's a wrapper *function* for each distinct function typedef used\r | |
4225 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a\r | |
4226 | wrapper *table* for each distinct operation (e.g. __len__, __add__).\r | |
4227 | Most tables have only one entry; the tables for binary operators have two\r | |
4228 | entries, one regular and one with reversed arguments. */\r | |
4229 | \r | |
4230 | static PyObject *\r | |
4231 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)\r | |
4232 | {\r | |
4233 | lenfunc func = (lenfunc)wrapped;\r | |
4234 | Py_ssize_t res;\r | |
4235 | \r | |
4236 | if (!check_num_args(args, 0))\r | |
4237 | return NULL;\r | |
4238 | res = (*func)(self);\r | |
4239 | if (res == -1 && PyErr_Occurred())\r | |
4240 | return NULL;\r | |
4241 | return PyInt_FromLong((long)res);\r | |
4242 | }\r | |
4243 | \r | |
4244 | static PyObject *\r | |
4245 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)\r | |
4246 | {\r | |
4247 | inquiry func = (inquiry)wrapped;\r | |
4248 | int res;\r | |
4249 | \r | |
4250 | if (!check_num_args(args, 0))\r | |
4251 | return NULL;\r | |
4252 | res = (*func)(self);\r | |
4253 | if (res == -1 && PyErr_Occurred())\r | |
4254 | return NULL;\r | |
4255 | return PyBool_FromLong((long)res);\r | |
4256 | }\r | |
4257 | \r | |
4258 | static PyObject *\r | |
4259 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)\r | |
4260 | {\r | |
4261 | binaryfunc func = (binaryfunc)wrapped;\r | |
4262 | PyObject *other;\r | |
4263 | \r | |
4264 | if (!check_num_args(args, 1))\r | |
4265 | return NULL;\r | |
4266 | other = PyTuple_GET_ITEM(args, 0);\r | |
4267 | return (*func)(self, other);\r | |
4268 | }\r | |
4269 | \r | |
4270 | static PyObject *\r | |
4271 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)\r | |
4272 | {\r | |
4273 | binaryfunc func = (binaryfunc)wrapped;\r | |
4274 | PyObject *other;\r | |
4275 | \r | |
4276 | if (!check_num_args(args, 1))\r | |
4277 | return NULL;\r | |
4278 | other = PyTuple_GET_ITEM(args, 0);\r | |
4279 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&\r | |
4280 | !PyType_IsSubtype(other->ob_type, self->ob_type)) {\r | |
4281 | Py_INCREF(Py_NotImplemented);\r | |
4282 | return Py_NotImplemented;\r | |
4283 | }\r | |
4284 | return (*func)(self, other);\r | |
4285 | }\r | |
4286 | \r | |
4287 | static PyObject *\r | |
4288 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)\r | |
4289 | {\r | |
4290 | binaryfunc func = (binaryfunc)wrapped;\r | |
4291 | PyObject *other;\r | |
4292 | \r | |
4293 | if (!check_num_args(args, 1))\r | |
4294 | return NULL;\r | |
4295 | other = PyTuple_GET_ITEM(args, 0);\r | |
4296 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&\r | |
4297 | !PyType_IsSubtype(other->ob_type, self->ob_type)) {\r | |
4298 | Py_INCREF(Py_NotImplemented);\r | |
4299 | return Py_NotImplemented;\r | |
4300 | }\r | |
4301 | return (*func)(other, self);\r | |
4302 | }\r | |
4303 | \r | |
4304 | static PyObject *\r | |
4305 | wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)\r | |
4306 | {\r | |
4307 | coercion func = (coercion)wrapped;\r | |
4308 | PyObject *other, *res;\r | |
4309 | int ok;\r | |
4310 | \r | |
4311 | if (!check_num_args(args, 1))\r | |
4312 | return NULL;\r | |
4313 | other = PyTuple_GET_ITEM(args, 0);\r | |
4314 | ok = func(&self, &other);\r | |
4315 | if (ok < 0)\r | |
4316 | return NULL;\r | |
4317 | if (ok > 0) {\r | |
4318 | Py_INCREF(Py_NotImplemented);\r | |
4319 | return Py_NotImplemented;\r | |
4320 | }\r | |
4321 | res = PyTuple_New(2);\r | |
4322 | if (res == NULL) {\r | |
4323 | Py_DECREF(self);\r | |
4324 | Py_DECREF(other);\r | |
4325 | return NULL;\r | |
4326 | }\r | |
4327 | PyTuple_SET_ITEM(res, 0, self);\r | |
4328 | PyTuple_SET_ITEM(res, 1, other);\r | |
4329 | return res;\r | |
4330 | }\r | |
4331 | \r | |
4332 | static PyObject *\r | |
4333 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)\r | |
4334 | {\r | |
4335 | ternaryfunc func = (ternaryfunc)wrapped;\r | |
4336 | PyObject *other;\r | |
4337 | PyObject *third = Py_None;\r | |
4338 | \r | |
4339 | /* Note: This wrapper only works for __pow__() */\r | |
4340 | \r | |
4341 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))\r | |
4342 | return NULL;\r | |
4343 | return (*func)(self, other, third);\r | |
4344 | }\r | |
4345 | \r | |
4346 | static PyObject *\r | |
4347 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)\r | |
4348 | {\r | |
4349 | ternaryfunc func = (ternaryfunc)wrapped;\r | |
4350 | PyObject *other;\r | |
4351 | PyObject *third = Py_None;\r | |
4352 | \r | |
4353 | /* Note: This wrapper only works for __pow__() */\r | |
4354 | \r | |
4355 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))\r | |
4356 | return NULL;\r | |
4357 | return (*func)(other, self, third);\r | |
4358 | }\r | |
4359 | \r | |
4360 | static PyObject *\r | |
4361 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)\r | |
4362 | {\r | |
4363 | unaryfunc func = (unaryfunc)wrapped;\r | |
4364 | \r | |
4365 | if (!check_num_args(args, 0))\r | |
4366 | return NULL;\r | |
4367 | return (*func)(self);\r | |
4368 | }\r | |
4369 | \r | |
4370 | static PyObject *\r | |
4371 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)\r | |
4372 | {\r | |
4373 | ssizeargfunc func = (ssizeargfunc)wrapped;\r | |
4374 | PyObject* o;\r | |
4375 | Py_ssize_t i;\r | |
4376 | \r | |
4377 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o))\r | |
4378 | return NULL;\r | |
4379 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError);\r | |
4380 | if (i == -1 && PyErr_Occurred())\r | |
4381 | return NULL;\r | |
4382 | return (*func)(self, i);\r | |
4383 | }\r | |
4384 | \r | |
4385 | static Py_ssize_t\r | |
4386 | getindex(PyObject *self, PyObject *arg)\r | |
4387 | {\r | |
4388 | Py_ssize_t i;\r | |
4389 | \r | |
4390 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);\r | |
4391 | if (i == -1 && PyErr_Occurred())\r | |
4392 | return -1;\r | |
4393 | if (i < 0) {\r | |
4394 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;\r | |
4395 | if (sq && sq->sq_length) {\r | |
4396 | Py_ssize_t n = (*sq->sq_length)(self);\r | |
4397 | if (n < 0)\r | |
4398 | return -1;\r | |
4399 | i += n;\r | |
4400 | }\r | |
4401 | }\r | |
4402 | return i;\r | |
4403 | }\r | |
4404 | \r | |
4405 | static PyObject *\r | |
4406 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)\r | |
4407 | {\r | |
4408 | ssizeargfunc func = (ssizeargfunc)wrapped;\r | |
4409 | PyObject *arg;\r | |
4410 | Py_ssize_t i;\r | |
4411 | \r | |
4412 | if (PyTuple_GET_SIZE(args) == 1) {\r | |
4413 | arg = PyTuple_GET_ITEM(args, 0);\r | |
4414 | i = getindex(self, arg);\r | |
4415 | if (i == -1 && PyErr_Occurred())\r | |
4416 | return NULL;\r | |
4417 | return (*func)(self, i);\r | |
4418 | }\r | |
4419 | check_num_args(args, 1);\r | |
4420 | assert(PyErr_Occurred());\r | |
4421 | return NULL;\r | |
4422 | }\r | |
4423 | \r | |
4424 | static PyObject *\r | |
4425 | wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)\r | |
4426 | {\r | |
4427 | ssizessizeargfunc func = (ssizessizeargfunc)wrapped;\r | |
4428 | Py_ssize_t i, j;\r | |
4429 | \r | |
4430 | if (!PyArg_ParseTuple(args, "nn", &i, &j))\r | |
4431 | return NULL;\r | |
4432 | return (*func)(self, i, j);\r | |
4433 | }\r | |
4434 | \r | |
4435 | static PyObject *\r | |
4436 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)\r | |
4437 | {\r | |
4438 | ssizeobjargproc func = (ssizeobjargproc)wrapped;\r | |
4439 | Py_ssize_t i;\r | |
4440 | int res;\r | |
4441 | PyObject *arg, *value;\r | |
4442 | \r | |
4443 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))\r | |
4444 | return NULL;\r | |
4445 | i = getindex(self, arg);\r | |
4446 | if (i == -1 && PyErr_Occurred())\r | |
4447 | return NULL;\r | |
4448 | res = (*func)(self, i, value);\r | |
4449 | if (res == -1 && PyErr_Occurred())\r | |
4450 | return NULL;\r | |
4451 | Py_INCREF(Py_None);\r | |
4452 | return Py_None;\r | |
4453 | }\r | |
4454 | \r | |
4455 | static PyObject *\r | |
4456 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)\r | |
4457 | {\r | |
4458 | ssizeobjargproc func = (ssizeobjargproc)wrapped;\r | |
4459 | Py_ssize_t i;\r | |
4460 | int res;\r | |
4461 | PyObject *arg;\r | |
4462 | \r | |
4463 | if (!check_num_args(args, 1))\r | |
4464 | return NULL;\r | |
4465 | arg = PyTuple_GET_ITEM(args, 0);\r | |
4466 | i = getindex(self, arg);\r | |
4467 | if (i == -1 && PyErr_Occurred())\r | |
4468 | return NULL;\r | |
4469 | res = (*func)(self, i, NULL);\r | |
4470 | if (res == -1 && PyErr_Occurred())\r | |
4471 | return NULL;\r | |
4472 | Py_INCREF(Py_None);\r | |
4473 | return Py_None;\r | |
4474 | }\r | |
4475 | \r | |
4476 | static PyObject *\r | |
4477 | wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)\r | |
4478 | {\r | |
4479 | ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;\r | |
4480 | Py_ssize_t i, j;\r | |
4481 | int res;\r | |
4482 | PyObject *value;\r | |
4483 | \r | |
4484 | if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))\r | |
4485 | return NULL;\r | |
4486 | res = (*func)(self, i, j, value);\r | |
4487 | if (res == -1 && PyErr_Occurred())\r | |
4488 | return NULL;\r | |
4489 | Py_INCREF(Py_None);\r | |
4490 | return Py_None;\r | |
4491 | }\r | |
4492 | \r | |
4493 | static PyObject *\r | |
4494 | wrap_delslice(PyObject *self, PyObject *args, void *wrapped)\r | |
4495 | {\r | |
4496 | ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;\r | |
4497 | Py_ssize_t i, j;\r | |
4498 | int res;\r | |
4499 | \r | |
4500 | if (!PyArg_ParseTuple(args, "nn", &i, &j))\r | |
4501 | return NULL;\r | |
4502 | res = (*func)(self, i, j, NULL);\r | |
4503 | if (res == -1 && PyErr_Occurred())\r | |
4504 | return NULL;\r | |
4505 | Py_INCREF(Py_None);\r | |
4506 | return Py_None;\r | |
4507 | }\r | |
4508 | \r | |
4509 | /* XXX objobjproc is a misnomer; should be objargpred */\r | |
4510 | static PyObject *\r | |
4511 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)\r | |
4512 | {\r | |
4513 | objobjproc func = (objobjproc)wrapped;\r | |
4514 | int res;\r | |
4515 | PyObject *value;\r | |
4516 | \r | |
4517 | if (!check_num_args(args, 1))\r | |
4518 | return NULL;\r | |
4519 | value = PyTuple_GET_ITEM(args, 0);\r | |
4520 | res = (*func)(self, value);\r | |
4521 | if (res == -1 && PyErr_Occurred())\r | |
4522 | return NULL;\r | |
4523 | else\r | |
4524 | return PyBool_FromLong(res);\r | |
4525 | }\r | |
4526 | \r | |
4527 | static PyObject *\r | |
4528 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)\r | |
4529 | {\r | |
4530 | objobjargproc func = (objobjargproc)wrapped;\r | |
4531 | int res;\r | |
4532 | PyObject *key, *value;\r | |
4533 | \r | |
4534 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))\r | |
4535 | return NULL;\r | |
4536 | res = (*func)(self, key, value);\r | |
4537 | if (res == -1 && PyErr_Occurred())\r | |
4538 | return NULL;\r | |
4539 | Py_INCREF(Py_None);\r | |
4540 | return Py_None;\r | |
4541 | }\r | |
4542 | \r | |
4543 | static PyObject *\r | |
4544 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped)\r | |
4545 | {\r | |
4546 | objobjargproc func = (objobjargproc)wrapped;\r | |
4547 | int res;\r | |
4548 | PyObject *key;\r | |
4549 | \r | |
4550 | if (!check_num_args(args, 1))\r | |
4551 | return NULL;\r | |
4552 | key = PyTuple_GET_ITEM(args, 0);\r | |
4553 | res = (*func)(self, key, NULL);\r | |
4554 | if (res == -1 && PyErr_Occurred())\r | |
4555 | return NULL;\r | |
4556 | Py_INCREF(Py_None);\r | |
4557 | return Py_None;\r | |
4558 | }\r | |
4559 | \r | |
4560 | static PyObject *\r | |
4561 | wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)\r | |
4562 | {\r | |
4563 | cmpfunc func = (cmpfunc)wrapped;\r | |
4564 | int res;\r | |
4565 | PyObject *other;\r | |
4566 | \r | |
4567 | if (!check_num_args(args, 1))\r | |
4568 | return NULL;\r | |
4569 | other = PyTuple_GET_ITEM(args, 0);\r | |
4570 | if (Py_TYPE(other)->tp_compare != func &&\r | |
4571 | !PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {\r | |
4572 | PyErr_Format(\r | |
4573 | PyExc_TypeError,\r | |
4574 | "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",\r | |
4575 | Py_TYPE(self)->tp_name,\r | |
4576 | Py_TYPE(self)->tp_name,\r | |
4577 | Py_TYPE(other)->tp_name);\r | |
4578 | return NULL;\r | |
4579 | }\r | |
4580 | res = (*func)(self, other);\r | |
4581 | if (PyErr_Occurred())\r | |
4582 | return NULL;\r | |
4583 | return PyInt_FromLong((long)res);\r | |
4584 | }\r | |
4585 | \r | |
4586 | /* Helper to check for object.__setattr__ or __delattr__ applied to a type.\r | |
4587 | This is called the Carlo Verre hack after its discoverer. */\r | |
4588 | static int\r | |
4589 | hackcheck(PyObject *self, setattrofunc func, char *what)\r | |
4590 | {\r | |
4591 | PyTypeObject *type = Py_TYPE(self);\r | |
4592 | while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)\r | |
4593 | type = type->tp_base;\r | |
4594 | /* If type is NULL now, this is a really weird type.\r | |
4595 | In the spirit of backwards compatibility (?), just shut up. */\r | |
4596 | if (type && type->tp_setattro != func) {\r | |
4597 | PyErr_Format(PyExc_TypeError,\r | |
4598 | "can't apply this %s to %s object",\r | |
4599 | what,\r | |
4600 | type->tp_name);\r | |
4601 | return 0;\r | |
4602 | }\r | |
4603 | return 1;\r | |
4604 | }\r | |
4605 | \r | |
4606 | static PyObject *\r | |
4607 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped)\r | |
4608 | {\r | |
4609 | setattrofunc func = (setattrofunc)wrapped;\r | |
4610 | int res;\r | |
4611 | PyObject *name, *value;\r | |
4612 | \r | |
4613 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))\r | |
4614 | return NULL;\r | |
4615 | if (!hackcheck(self, func, "__setattr__"))\r | |
4616 | return NULL;\r | |
4617 | res = (*func)(self, name, value);\r | |
4618 | if (res < 0)\r | |
4619 | return NULL;\r | |
4620 | Py_INCREF(Py_None);\r | |
4621 | return Py_None;\r | |
4622 | }\r | |
4623 | \r | |
4624 | static PyObject *\r | |
4625 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped)\r | |
4626 | {\r | |
4627 | setattrofunc func = (setattrofunc)wrapped;\r | |
4628 | int res;\r | |
4629 | PyObject *name;\r | |
4630 | \r | |
4631 | if (!check_num_args(args, 1))\r | |
4632 | return NULL;\r | |
4633 | name = PyTuple_GET_ITEM(args, 0);\r | |
4634 | if (!hackcheck(self, func, "__delattr__"))\r | |
4635 | return NULL;\r | |
4636 | res = (*func)(self, name, NULL);\r | |
4637 | if (res < 0)\r | |
4638 | return NULL;\r | |
4639 | Py_INCREF(Py_None);\r | |
4640 | return Py_None;\r | |
4641 | }\r | |
4642 | \r | |
4643 | static PyObject *\r | |
4644 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)\r | |
4645 | {\r | |
4646 | hashfunc func = (hashfunc)wrapped;\r | |
4647 | long res;\r | |
4648 | \r | |
4649 | if (!check_num_args(args, 0))\r | |
4650 | return NULL;\r | |
4651 | res = (*func)(self);\r | |
4652 | if (res == -1 && PyErr_Occurred())\r | |
4653 | return NULL;\r | |
4654 | return PyInt_FromLong(res);\r | |
4655 | }\r | |
4656 | \r | |
4657 | static PyObject *\r | |
4658 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)\r | |
4659 | {\r | |
4660 | ternaryfunc func = (ternaryfunc)wrapped;\r | |
4661 | \r | |
4662 | return (*func)(self, args, kwds);\r | |
4663 | }\r | |
4664 | \r | |
4665 | static PyObject *\r | |
4666 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)\r | |
4667 | {\r | |
4668 | richcmpfunc func = (richcmpfunc)wrapped;\r | |
4669 | PyObject *other;\r | |
4670 | \r | |
4671 | if (!check_num_args(args, 1))\r | |
4672 | return NULL;\r | |
4673 | other = PyTuple_GET_ITEM(args, 0);\r | |
4674 | return (*func)(self, other, op);\r | |
4675 | }\r | |
4676 | \r | |
4677 | #undef RICHCMP_WRAPPER\r | |
4678 | #define RICHCMP_WRAPPER(NAME, OP) \\r | |
4679 | static PyObject * \\r | |
4680 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \\r | |
4681 | { \\r | |
4682 | return wrap_richcmpfunc(self, args, wrapped, OP); \\r | |
4683 | }\r | |
4684 | \r | |
4685 | RICHCMP_WRAPPER(lt, Py_LT)\r | |
4686 | RICHCMP_WRAPPER(le, Py_LE)\r | |
4687 | RICHCMP_WRAPPER(eq, Py_EQ)\r | |
4688 | RICHCMP_WRAPPER(ne, Py_NE)\r | |
4689 | RICHCMP_WRAPPER(gt, Py_GT)\r | |
4690 | RICHCMP_WRAPPER(ge, Py_GE)\r | |
4691 | \r | |
4692 | static PyObject *\r | |
4693 | wrap_next(PyObject *self, PyObject *args, void *wrapped)\r | |
4694 | {\r | |
4695 | unaryfunc func = (unaryfunc)wrapped;\r | |
4696 | PyObject *res;\r | |
4697 | \r | |
4698 | if (!check_num_args(args, 0))\r | |
4699 | return NULL;\r | |
4700 | res = (*func)(self);\r | |
4701 | if (res == NULL && !PyErr_Occurred())\r | |
4702 | PyErr_SetNone(PyExc_StopIteration);\r | |
4703 | return res;\r | |
4704 | }\r | |
4705 | \r | |
4706 | static PyObject *\r | |
4707 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)\r | |
4708 | {\r | |
4709 | descrgetfunc func = (descrgetfunc)wrapped;\r | |
4710 | PyObject *obj;\r | |
4711 | PyObject *type = NULL;\r | |
4712 | \r | |
4713 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))\r | |
4714 | return NULL;\r | |
4715 | if (obj == Py_None)\r | |
4716 | obj = NULL;\r | |
4717 | if (type == Py_None)\r | |
4718 | type = NULL;\r | |
4719 | if (type == NULL &&obj == NULL) {\r | |
4720 | PyErr_SetString(PyExc_TypeError,\r | |
4721 | "__get__(None, None) is invalid");\r | |
4722 | return NULL;\r | |
4723 | }\r | |
4724 | return (*func)(self, obj, type);\r | |
4725 | }\r | |
4726 | \r | |
4727 | static PyObject *\r | |
4728 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)\r | |
4729 | {\r | |
4730 | descrsetfunc func = (descrsetfunc)wrapped;\r | |
4731 | PyObject *obj, *value;\r | |
4732 | int ret;\r | |
4733 | \r | |
4734 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))\r | |
4735 | return NULL;\r | |
4736 | ret = (*func)(self, obj, value);\r | |
4737 | if (ret < 0)\r | |
4738 | return NULL;\r | |
4739 | Py_INCREF(Py_None);\r | |
4740 | return Py_None;\r | |
4741 | }\r | |
4742 | \r | |
4743 | static PyObject *\r | |
4744 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)\r | |
4745 | {\r | |
4746 | descrsetfunc func = (descrsetfunc)wrapped;\r | |
4747 | PyObject *obj;\r | |
4748 | int ret;\r | |
4749 | \r | |
4750 | if (!check_num_args(args, 1))\r | |
4751 | return NULL;\r | |
4752 | obj = PyTuple_GET_ITEM(args, 0);\r | |
4753 | ret = (*func)(self, obj, NULL);\r | |
4754 | if (ret < 0)\r | |
4755 | return NULL;\r | |
4756 | Py_INCREF(Py_None);\r | |
4757 | return Py_None;\r | |
4758 | }\r | |
4759 | \r | |
4760 | static PyObject *\r | |
4761 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)\r | |
4762 | {\r | |
4763 | initproc func = (initproc)wrapped;\r | |
4764 | \r | |
4765 | if (func(self, args, kwds) < 0)\r | |
4766 | return NULL;\r | |
4767 | Py_INCREF(Py_None);\r | |
4768 | return Py_None;\r | |
4769 | }\r | |
4770 | \r | |
4771 | static PyObject *\r | |
4772 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)\r | |
4773 | {\r | |
4774 | PyTypeObject *type, *subtype, *staticbase;\r | |
4775 | PyObject *arg0, *res;\r | |
4776 | \r | |
4777 | if (self == NULL || !PyType_Check(self))\r | |
4778 | Py_FatalError("__new__() called with non-type 'self'");\r | |
4779 | type = (PyTypeObject *)self;\r | |
4780 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {\r | |
4781 | PyErr_Format(PyExc_TypeError,\r | |
4782 | "%s.__new__(): not enough arguments",\r | |
4783 | type->tp_name);\r | |
4784 | return NULL;\r | |
4785 | }\r | |
4786 | arg0 = PyTuple_GET_ITEM(args, 0);\r | |
4787 | if (!PyType_Check(arg0)) {\r | |
4788 | PyErr_Format(PyExc_TypeError,\r | |
4789 | "%s.__new__(X): X is not a type object (%s)",\r | |
4790 | type->tp_name,\r | |
4791 | Py_TYPE(arg0)->tp_name);\r | |
4792 | return NULL;\r | |
4793 | }\r | |
4794 | subtype = (PyTypeObject *)arg0;\r | |
4795 | if (!PyType_IsSubtype(subtype, type)) {\r | |
4796 | PyErr_Format(PyExc_TypeError,\r | |
4797 | "%s.__new__(%s): %s is not a subtype of %s",\r | |
4798 | type->tp_name,\r | |
4799 | subtype->tp_name,\r | |
4800 | subtype->tp_name,\r | |
4801 | type->tp_name);\r | |
4802 | return NULL;\r | |
4803 | }\r | |
4804 | \r | |
4805 | /* Check that the use doesn't do something silly and unsafe like\r | |
4806 | object.__new__(dict). To do this, we check that the\r | |
4807 | most derived base that's not a heap type is this type. */\r | |
4808 | staticbase = subtype;\r | |
4809 | while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))\r | |
4810 | staticbase = staticbase->tp_base;\r | |
4811 | /* If staticbase is NULL now, it is a really weird type.\r | |
4812 | In the spirit of backwards compatibility (?), just shut up. */\r | |
4813 | if (staticbase && staticbase->tp_new != type->tp_new) {\r | |
4814 | PyErr_Format(PyExc_TypeError,\r | |
4815 | "%s.__new__(%s) is not safe, use %s.__new__()",\r | |
4816 | type->tp_name,\r | |
4817 | subtype->tp_name,\r | |
4818 | staticbase->tp_name);\r | |
4819 | return NULL;\r | |
4820 | }\r | |
4821 | \r | |
4822 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));\r | |
4823 | if (args == NULL)\r | |
4824 | return NULL;\r | |
4825 | res = type->tp_new(subtype, args, kwds);\r | |
4826 | Py_DECREF(args);\r | |
4827 | return res;\r | |
4828 | }\r | |
4829 | \r | |
4830 | static struct PyMethodDef tp_new_methoddef[] = {\r | |
4831 | {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS,\r | |
4832 | PyDoc_STR("T.__new__(S, ...) -> "\r | |
4833 | "a new object with type S, a subtype of T")},\r | |
4834 | {0}\r | |
4835 | };\r | |
4836 | \r | |
4837 | static int\r | |
4838 | add_tp_new_wrapper(PyTypeObject *type)\r | |
4839 | {\r | |
4840 | PyObject *func;\r | |
4841 | \r | |
4842 | if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)\r | |
4843 | return 0;\r | |
4844 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);\r | |
4845 | if (func == NULL)\r | |
4846 | return -1;\r | |
4847 | if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {\r | |
4848 | Py_DECREF(func);\r | |
4849 | return -1;\r | |
4850 | }\r | |
4851 | Py_DECREF(func);\r | |
4852 | return 0;\r | |
4853 | }\r | |
4854 | \r | |
4855 | /* Slot wrappers that call the corresponding __foo__ slot. See comments\r | |
4856 | below at override_slots() for more explanation. */\r | |
4857 | \r | |
4858 | #define SLOT0(FUNCNAME, OPSTR) \\r | |
4859 | static PyObject * \\r | |
4860 | FUNCNAME(PyObject *self) \\r | |
4861 | { \\r | |
4862 | static PyObject *cache_str; \\r | |
4863 | return call_method(self, OPSTR, &cache_str, "()"); \\r | |
4864 | }\r | |
4865 | \r | |
4866 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \\r | |
4867 | static PyObject * \\r | |
4868 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \\r | |
4869 | { \\r | |
4870 | static PyObject *cache_str; \\r | |
4871 | return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \\r | |
4872 | }\r | |
4873 | \r | |
4874 | /* Boolean helper for SLOT1BINFULL().\r | |
4875 | right.__class__ is a nontrivial subclass of left.__class__. */\r | |
4876 | static int\r | |
4877 | method_is_overloaded(PyObject *left, PyObject *right, char *name)\r | |
4878 | {\r | |
4879 | PyObject *a, *b;\r | |
4880 | int ok;\r | |
4881 | \r | |
4882 | b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name);\r | |
4883 | if (b == NULL) {\r | |
4884 | PyErr_Clear();\r | |
4885 | /* If right doesn't have it, it's not overloaded */\r | |
4886 | return 0;\r | |
4887 | }\r | |
4888 | \r | |
4889 | a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name);\r | |
4890 | if (a == NULL) {\r | |
4891 | PyErr_Clear();\r | |
4892 | Py_DECREF(b);\r | |
4893 | /* If right has it but left doesn't, it's overloaded */\r | |
4894 | return 1;\r | |
4895 | }\r | |
4896 | \r | |
4897 | ok = PyObject_RichCompareBool(a, b, Py_NE);\r | |
4898 | Py_DECREF(a);\r | |
4899 | Py_DECREF(b);\r | |
4900 | if (ok < 0) {\r | |
4901 | PyErr_Clear();\r | |
4902 | return 0;\r | |
4903 | }\r | |
4904 | \r | |
4905 | return ok;\r | |
4906 | }\r | |
4907 | \r | |
4908 | \r | |
4909 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \\r | |
4910 | static PyObject * \\r | |
4911 | FUNCNAME(PyObject *self, PyObject *other) \\r | |
4912 | { \\r | |
4913 | static PyObject *cache_str, *rcache_str; \\r | |
4914 | int do_other = Py_TYPE(self) != Py_TYPE(other) && \\r | |
4915 | Py_TYPE(other)->tp_as_number != NULL && \\r | |
4916 | Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \\r | |
4917 | if (Py_TYPE(self)->tp_as_number != NULL && \\r | |
4918 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \\r | |
4919 | PyObject *r; \\r | |
4920 | if (do_other && \\r | |
4921 | PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \\r | |
4922 | method_is_overloaded(self, other, ROPSTR)) { \\r | |
4923 | r = call_maybe( \\r | |
4924 | other, ROPSTR, &rcache_str, "(O)", self); \\r | |
4925 | if (r != Py_NotImplemented) \\r | |
4926 | return r; \\r | |
4927 | Py_DECREF(r); \\r | |
4928 | do_other = 0; \\r | |
4929 | } \\r | |
4930 | r = call_maybe( \\r | |
4931 | self, OPSTR, &cache_str, "(O)", other); \\r | |
4932 | if (r != Py_NotImplemented || \\r | |
4933 | Py_TYPE(other) == Py_TYPE(self)) \\r | |
4934 | return r; \\r | |
4935 | Py_DECREF(r); \\r | |
4936 | } \\r | |
4937 | if (do_other) { \\r | |
4938 | return call_maybe( \\r | |
4939 | other, ROPSTR, &rcache_str, "(O)", self); \\r | |
4940 | } \\r | |
4941 | Py_INCREF(Py_NotImplemented); \\r | |
4942 | return Py_NotImplemented; \\r | |
4943 | }\r | |
4944 | \r | |
4945 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \\r | |
4946 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)\r | |
4947 | \r | |
4948 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \\r | |
4949 | static PyObject * \\r | |
4950 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \\r | |
4951 | { \\r | |
4952 | static PyObject *cache_str; \\r | |
4953 | return call_method(self, OPSTR, &cache_str, \\r | |
4954 | "(" ARGCODES ")", arg1, arg2); \\r | |
4955 | }\r | |
4956 | \r | |
4957 | static Py_ssize_t\r | |
4958 | slot_sq_length(PyObject *self)\r | |
4959 | {\r | |
4960 | static PyObject *len_str;\r | |
4961 | PyObject *res = call_method(self, "__len__", &len_str, "()");\r | |
4962 | Py_ssize_t len;\r | |
4963 | \r | |
4964 | if (res == NULL)\r | |
4965 | return -1;\r | |
4966 | len = PyInt_AsSsize_t(res);\r | |
4967 | Py_DECREF(res);\r | |
4968 | if (len < 0) {\r | |
4969 | if (!PyErr_Occurred())\r | |
4970 | PyErr_SetString(PyExc_ValueError,\r | |
4971 | "__len__() should return >= 0");\r | |
4972 | return -1;\r | |
4973 | }\r | |
4974 | return len;\r | |
4975 | }\r | |
4976 | \r | |
4977 | /* Super-optimized version of slot_sq_item.\r | |
4978 | Other slots could do the same... */\r | |
4979 | static PyObject *\r | |
4980 | slot_sq_item(PyObject *self, Py_ssize_t i)\r | |
4981 | {\r | |
4982 | static PyObject *getitem_str;\r | |
4983 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;\r | |
4984 | descrgetfunc f;\r | |
4985 | \r | |
4986 | if (getitem_str == NULL) {\r | |
4987 | getitem_str = PyString_InternFromString("__getitem__");\r | |
4988 | if (getitem_str == NULL)\r | |
4989 | return NULL;\r | |
4990 | }\r | |
4991 | func = _PyType_Lookup(Py_TYPE(self), getitem_str);\r | |
4992 | if (func != NULL) {\r | |
4993 | if ((f = Py_TYPE(func)->tp_descr_get) == NULL)\r | |
4994 | Py_INCREF(func);\r | |
4995 | else {\r | |
4996 | func = f(func, self, (PyObject *)(Py_TYPE(self)));\r | |
4997 | if (func == NULL) {\r | |
4998 | return NULL;\r | |
4999 | }\r | |
5000 | }\r | |
5001 | ival = PyInt_FromSsize_t(i);\r | |
5002 | if (ival != NULL) {\r | |
5003 | args = PyTuple_New(1);\r | |
5004 | if (args != NULL) {\r | |
5005 | PyTuple_SET_ITEM(args, 0, ival);\r | |
5006 | retval = PyObject_Call(func, args, NULL);\r | |
5007 | Py_XDECREF(args);\r | |
5008 | Py_XDECREF(func);\r | |
5009 | return retval;\r | |
5010 | }\r | |
5011 | }\r | |
5012 | }\r | |
5013 | else {\r | |
5014 | PyErr_SetObject(PyExc_AttributeError, getitem_str);\r | |
5015 | }\r | |
5016 | Py_XDECREF(args);\r | |
5017 | Py_XDECREF(ival);\r | |
5018 | Py_XDECREF(func);\r | |
5019 | return NULL;\r | |
5020 | }\r | |
5021 | \r | |
5022 | static PyObject*\r | |
5023 | slot_sq_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j)\r | |
5024 | {\r | |
5025 | static PyObject *getslice_str;\r | |
5026 | \r | |
5027 | if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; "\r | |
5028 | "use __getitem__", 1) < 0)\r | |
5029 | return NULL;\r | |
5030 | return call_method(self, "__getslice__", &getslice_str,\r | |
5031 | "nn", i, j);\r | |
5032 | }\r | |
5033 | \r | |
5034 | static int\r | |
5035 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)\r | |
5036 | {\r | |
5037 | PyObject *res;\r | |
5038 | static PyObject *delitem_str, *setitem_str;\r | |
5039 | \r | |
5040 | if (value == NULL)\r | |
5041 | res = call_method(self, "__delitem__", &delitem_str,\r | |
5042 | "(n)", index);\r | |
5043 | else\r | |
5044 | res = call_method(self, "__setitem__", &setitem_str,\r | |
5045 | "(nO)", index, value);\r | |
5046 | if (res == NULL)\r | |
5047 | return -1;\r | |
5048 | Py_DECREF(res);\r | |
5049 | return 0;\r | |
5050 | }\r | |
5051 | \r | |
5052 | static int\r | |
5053 | slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value)\r | |
5054 | {\r | |
5055 | PyObject *res;\r | |
5056 | static PyObject *delslice_str, *setslice_str;\r | |
5057 | \r | |
5058 | if (value == NULL) {\r | |
5059 | if (PyErr_WarnPy3k("in 3.x, __delslice__ has been removed; "\r | |
5060 | "use __delitem__", 1) < 0)\r | |
5061 | return -1;\r | |
5062 | res = call_method(self, "__delslice__", &delslice_str,\r | |
5063 | "(nn)", i, j);\r | |
5064 | }\r | |
5065 | else {\r | |
5066 | if (PyErr_WarnPy3k("in 3.x, __setslice__ has been removed; "\r | |
5067 | "use __setitem__", 1) < 0)\r | |
5068 | return -1;\r | |
5069 | res = call_method(self, "__setslice__", &setslice_str,\r | |
5070 | "(nnO)", i, j, value);\r | |
5071 | }\r | |
5072 | if (res == NULL)\r | |
5073 | return -1;\r | |
5074 | Py_DECREF(res);\r | |
5075 | return 0;\r | |
5076 | }\r | |
5077 | \r | |
5078 | static int\r | |
5079 | slot_sq_contains(PyObject *self, PyObject *value)\r | |
5080 | {\r | |
5081 | PyObject *func, *res, *args;\r | |
5082 | int result = -1;\r | |
5083 | \r | |
5084 | static PyObject *contains_str;\r | |
5085 | \r | |
5086 | func = lookup_maybe(self, "__contains__", &contains_str);\r | |
5087 | if (func != NULL) {\r | |
5088 | args = PyTuple_Pack(1, value);\r | |
5089 | if (args == NULL)\r | |
5090 | res = NULL;\r | |
5091 | else {\r | |
5092 | res = PyObject_Call(func, args, NULL);\r | |
5093 | Py_DECREF(args);\r | |
5094 | }\r | |
5095 | Py_DECREF(func);\r | |
5096 | if (res != NULL) {\r | |
5097 | result = PyObject_IsTrue(res);\r | |
5098 | Py_DECREF(res);\r | |
5099 | }\r | |
5100 | }\r | |
5101 | else if (! PyErr_Occurred()) {\r | |
5102 | /* Possible results: -1 and 1 */\r | |
5103 | result = (int)_PySequence_IterSearch(self, value,\r | |
5104 | PY_ITERSEARCH_CONTAINS);\r | |
5105 | }\r | |
5106 | return result;\r | |
5107 | }\r | |
5108 | \r | |
5109 | #define slot_mp_length slot_sq_length\r | |
5110 | \r | |
5111 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")\r | |
5112 | \r | |
5113 | static int\r | |
5114 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)\r | |
5115 | {\r | |
5116 | PyObject *res;\r | |
5117 | static PyObject *delitem_str, *setitem_str;\r | |
5118 | \r | |
5119 | if (value == NULL)\r | |
5120 | res = call_method(self, "__delitem__", &delitem_str,\r | |
5121 | "(O)", key);\r | |
5122 | else\r | |
5123 | res = call_method(self, "__setitem__", &setitem_str,\r | |
5124 | "(OO)", key, value);\r | |
5125 | if (res == NULL)\r | |
5126 | return -1;\r | |
5127 | Py_DECREF(res);\r | |
5128 | return 0;\r | |
5129 | }\r | |
5130 | \r | |
5131 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")\r | |
5132 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")\r | |
5133 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")\r | |
5134 | SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")\r | |
5135 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")\r | |
5136 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")\r | |
5137 | \r | |
5138 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);\r | |
5139 | \r | |
5140 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,\r | |
5141 | nb_power, "__pow__", "__rpow__")\r | |
5142 | \r | |
5143 | static PyObject *\r | |
5144 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)\r | |
5145 | {\r | |
5146 | static PyObject *pow_str;\r | |
5147 | \r | |
5148 | if (modulus == Py_None)\r | |
5149 | return slot_nb_power_binary(self, other);\r | |
5150 | /* Three-arg power doesn't use __rpow__. But ternary_op\r | |
5151 | can call this when the second argument's type uses\r | |
5152 | slot_nb_power, so check before calling self.__pow__. */\r | |
5153 | if (Py_TYPE(self)->tp_as_number != NULL &&\r | |
5154 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) {\r | |
5155 | return call_method(self, "__pow__", &pow_str,\r | |
5156 | "(OO)", other, modulus);\r | |
5157 | }\r | |
5158 | Py_INCREF(Py_NotImplemented);\r | |
5159 | return Py_NotImplemented;\r | |
5160 | }\r | |
5161 | \r | |
5162 | SLOT0(slot_nb_negative, "__neg__")\r | |
5163 | SLOT0(slot_nb_positive, "__pos__")\r | |
5164 | SLOT0(slot_nb_absolute, "__abs__")\r | |
5165 | \r | |
5166 | static int\r | |
5167 | slot_nb_nonzero(PyObject *self)\r | |
5168 | {\r | |
5169 | PyObject *func, *args;\r | |
5170 | static PyObject *nonzero_str, *len_str;\r | |
5171 | int result = -1;\r | |
5172 | int using_len = 0;\r | |
5173 | \r | |
5174 | func = lookup_maybe(self, "__nonzero__", &nonzero_str);\r | |
5175 | if (func == NULL) {\r | |
5176 | if (PyErr_Occurred())\r | |
5177 | return -1;\r | |
5178 | func = lookup_maybe(self, "__len__", &len_str);\r | |
5179 | if (func == NULL)\r | |
5180 | return PyErr_Occurred() ? -1 : 1;\r | |
5181 | using_len = 1;\r | |
5182 | }\r | |
5183 | args = PyTuple_New(0);\r | |
5184 | if (args != NULL) {\r | |
5185 | PyObject *temp = PyObject_Call(func, args, NULL);\r | |
5186 | Py_DECREF(args);\r | |
5187 | if (temp != NULL) {\r | |
5188 | if (PyInt_CheckExact(temp) || PyBool_Check(temp))\r | |
5189 | result = PyObject_IsTrue(temp);\r | |
5190 | else {\r | |
5191 | PyErr_Format(PyExc_TypeError,\r | |
5192 | "%s should return "\r | |
5193 | "bool or int, returned %s",\r | |
5194 | (using_len ? "__len__"\r | |
5195 | : "__nonzero__"),\r | |
5196 | temp->ob_type->tp_name);\r | |
5197 | result = -1;\r | |
5198 | }\r | |
5199 | Py_DECREF(temp);\r | |
5200 | }\r | |
5201 | }\r | |
5202 | Py_DECREF(func);\r | |
5203 | return result;\r | |
5204 | }\r | |
5205 | \r | |
5206 | \r | |
5207 | static PyObject *\r | |
5208 | slot_nb_index(PyObject *self)\r | |
5209 | {\r | |
5210 | static PyObject *index_str;\r | |
5211 | return call_method(self, "__index__", &index_str, "()");\r | |
5212 | }\r | |
5213 | \r | |
5214 | \r | |
5215 | SLOT0(slot_nb_invert, "__invert__")\r | |
5216 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")\r | |
5217 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")\r | |
5218 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")\r | |
5219 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")\r | |
5220 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")\r | |
5221 | \r | |
5222 | static int\r | |
5223 | slot_nb_coerce(PyObject **a, PyObject **b)\r | |
5224 | {\r | |
5225 | static PyObject *coerce_str;\r | |
5226 | PyObject *self = *a, *other = *b;\r | |
5227 | \r | |
5228 | if (self->ob_type->tp_as_number != NULL &&\r | |
5229 | self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {\r | |
5230 | PyObject *r;\r | |
5231 | r = call_maybe(\r | |
5232 | self, "__coerce__", &coerce_str, "(O)", other);\r | |
5233 | if (r == NULL)\r | |
5234 | return -1;\r | |
5235 | if (r == Py_NotImplemented) {\r | |
5236 | Py_DECREF(r);\r | |
5237 | }\r | |
5238 | else {\r | |
5239 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {\r | |
5240 | PyErr_SetString(PyExc_TypeError,\r | |
5241 | "__coerce__ didn't return a 2-tuple");\r | |
5242 | Py_DECREF(r);\r | |
5243 | return -1;\r | |
5244 | }\r | |
5245 | *a = PyTuple_GET_ITEM(r, 0);\r | |
5246 | Py_INCREF(*a);\r | |
5247 | *b = PyTuple_GET_ITEM(r, 1);\r | |
5248 | Py_INCREF(*b);\r | |
5249 | Py_DECREF(r);\r | |
5250 | return 0;\r | |
5251 | }\r | |
5252 | }\r | |
5253 | if (other->ob_type->tp_as_number != NULL &&\r | |
5254 | other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {\r | |
5255 | PyObject *r;\r | |
5256 | r = call_maybe(\r | |
5257 | other, "__coerce__", &coerce_str, "(O)", self);\r | |
5258 | if (r == NULL)\r | |
5259 | return -1;\r | |
5260 | if (r == Py_NotImplemented) {\r | |
5261 | Py_DECREF(r);\r | |
5262 | return 1;\r | |
5263 | }\r | |
5264 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {\r | |
5265 | PyErr_SetString(PyExc_TypeError,\r | |
5266 | "__coerce__ didn't return a 2-tuple");\r | |
5267 | Py_DECREF(r);\r | |
5268 | return -1;\r | |
5269 | }\r | |
5270 | *a = PyTuple_GET_ITEM(r, 1);\r | |
5271 | Py_INCREF(*a);\r | |
5272 | *b = PyTuple_GET_ITEM(r, 0);\r | |
5273 | Py_INCREF(*b);\r | |
5274 | Py_DECREF(r);\r | |
5275 | return 0;\r | |
5276 | }\r | |
5277 | return 1;\r | |
5278 | }\r | |
5279 | \r | |
5280 | SLOT0(slot_nb_int, "__int__")\r | |
5281 | SLOT0(slot_nb_long, "__long__")\r | |
5282 | SLOT0(slot_nb_float, "__float__")\r | |
5283 | SLOT0(slot_nb_oct, "__oct__")\r | |
5284 | SLOT0(slot_nb_hex, "__hex__")\r | |
5285 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")\r | |
5286 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")\r | |
5287 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")\r | |
5288 | SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")\r | |
5289 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")\r | |
5290 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */\r | |
5291 | static PyObject *\r | |
5292 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)\r | |
5293 | {\r | |
5294 | static PyObject *cache_str;\r | |
5295 | return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);\r | |
5296 | }\r | |
5297 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")\r | |
5298 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")\r | |
5299 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")\r | |
5300 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")\r | |
5301 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")\r | |
5302 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,\r | |
5303 | "__floordiv__", "__rfloordiv__")\r | |
5304 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")\r | |
5305 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")\r | |
5306 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")\r | |
5307 | \r | |
5308 | static int\r | |
5309 | half_compare(PyObject *self, PyObject *other)\r | |
5310 | {\r | |
5311 | PyObject *func, *args, *res;\r | |
5312 | static PyObject *cmp_str;\r | |
5313 | Py_ssize_t c;\r | |
5314 | \r | |
5315 | func = lookup_method(self, "__cmp__", &cmp_str);\r | |
5316 | if (func == NULL) {\r | |
5317 | PyErr_Clear();\r | |
5318 | }\r | |
5319 | else {\r | |
5320 | args = PyTuple_Pack(1, other);\r | |
5321 | if (args == NULL)\r | |
5322 | res = NULL;\r | |
5323 | else {\r | |
5324 | res = PyObject_Call(func, args, NULL);\r | |
5325 | Py_DECREF(args);\r | |
5326 | }\r | |
5327 | Py_DECREF(func);\r | |
5328 | if (res != Py_NotImplemented) {\r | |
5329 | if (res == NULL)\r | |
5330 | return -2;\r | |
5331 | c = PyInt_AsLong(res);\r | |
5332 | Py_DECREF(res);\r | |
5333 | if (c == -1 && PyErr_Occurred())\r | |
5334 | return -2;\r | |
5335 | return (c < 0) ? -1 : (c > 0) ? 1 : 0;\r | |
5336 | }\r | |
5337 | Py_DECREF(res);\r | |
5338 | }\r | |
5339 | return 2;\r | |
5340 | }\r | |
5341 | \r | |
5342 | /* This slot is published for the benefit of try_3way_compare in object.c */\r | |
5343 | int\r | |
5344 | _PyObject_SlotCompare(PyObject *self, PyObject *other)\r | |
5345 | {\r | |
5346 | int c;\r | |
5347 | \r | |
5348 | if (Py_TYPE(self)->tp_compare == _PyObject_SlotCompare) {\r | |
5349 | c = half_compare(self, other);\r | |
5350 | if (c <= 1)\r | |
5351 | return c;\r | |
5352 | }\r | |
5353 | if (Py_TYPE(other)->tp_compare == _PyObject_SlotCompare) {\r | |
5354 | c = half_compare(other, self);\r | |
5355 | if (c < -1)\r | |
5356 | return -2;\r | |
5357 | if (c <= 1)\r | |
5358 | return -c;\r | |
5359 | }\r | |
5360 | return (void *)self < (void *)other ? -1 :\r | |
5361 | (void *)self > (void *)other ? 1 : 0;\r | |
5362 | }\r | |
5363 | \r | |
5364 | static PyObject *\r | |
5365 | slot_tp_repr(PyObject *self)\r | |
5366 | {\r | |
5367 | PyObject *func, *res;\r | |
5368 | static PyObject *repr_str;\r | |
5369 | \r | |
5370 | func = lookup_method(self, "__repr__", &repr_str);\r | |
5371 | if (func != NULL) {\r | |
5372 | res = PyEval_CallObject(func, NULL);\r | |
5373 | Py_DECREF(func);\r | |
5374 | return res;\r | |
5375 | }\r | |
5376 | PyErr_Clear();\r | |
5377 | return PyString_FromFormat("<%s object at %p>",\r | |
5378 | Py_TYPE(self)->tp_name, self);\r | |
5379 | }\r | |
5380 | \r | |
5381 | static PyObject *\r | |
5382 | slot_tp_str(PyObject *self)\r | |
5383 | {\r | |
5384 | PyObject *func, *res;\r | |
5385 | static PyObject *str_str;\r | |
5386 | \r | |
5387 | func = lookup_method(self, "__str__", &str_str);\r | |
5388 | if (func != NULL) {\r | |
5389 | res = PyEval_CallObject(func, NULL);\r | |
5390 | Py_DECREF(func);\r | |
5391 | return res;\r | |
5392 | }\r | |
5393 | else {\r | |
5394 | PyErr_Clear();\r | |
5395 | return slot_tp_repr(self);\r | |
5396 | }\r | |
5397 | }\r | |
5398 | \r | |
5399 | static long\r | |
5400 | slot_tp_hash(PyObject *self)\r | |
5401 | {\r | |
5402 | PyObject *func;\r | |
5403 | static PyObject *hash_str, *eq_str, *cmp_str;\r | |
5404 | long h;\r | |
5405 | \r | |
5406 | func = lookup_method(self, "__hash__", &hash_str);\r | |
5407 | \r | |
5408 | if (func != NULL && func != Py_None) {\r | |
5409 | PyObject *res = PyEval_CallObject(func, NULL);\r | |
5410 | Py_DECREF(func);\r | |
5411 | if (res == NULL)\r | |
5412 | return -1;\r | |
5413 | if (PyLong_Check(res))\r | |
5414 | h = PyLong_Type.tp_hash(res);\r | |
5415 | else\r | |
5416 | h = PyInt_AsLong(res);\r | |
5417 | Py_DECREF(res);\r | |
5418 | }\r | |
5419 | else {\r | |
5420 | Py_XDECREF(func); /* may be None */\r | |
5421 | PyErr_Clear();\r | |
5422 | func = lookup_method(self, "__eq__", &eq_str);\r | |
5423 | if (func == NULL) {\r | |
5424 | PyErr_Clear();\r | |
5425 | func = lookup_method(self, "__cmp__", &cmp_str);\r | |
5426 | }\r | |
5427 | if (func != NULL) {\r | |
5428 | Py_DECREF(func);\r | |
5429 | return PyObject_HashNotImplemented(self);\r | |
5430 | }\r | |
5431 | PyErr_Clear();\r | |
5432 | h = _Py_HashPointer((void *)self);\r | |
5433 | }\r | |
5434 | if (h == -1 && !PyErr_Occurred())\r | |
5435 | h = -2;\r | |
5436 | return h;\r | |
5437 | }\r | |
5438 | \r | |
5439 | static PyObject *\r | |
5440 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)\r | |
5441 | {\r | |
5442 | static PyObject *call_str;\r | |
5443 | PyObject *meth = lookup_method(self, "__call__", &call_str);\r | |
5444 | PyObject *res;\r | |
5445 | \r | |
5446 | if (meth == NULL)\r | |
5447 | return NULL;\r | |
5448 | \r | |
5449 | res = PyObject_Call(meth, args, kwds);\r | |
5450 | \r | |
5451 | Py_DECREF(meth);\r | |
5452 | return res;\r | |
5453 | }\r | |
5454 | \r | |
5455 | /* There are two slot dispatch functions for tp_getattro.\r | |
5456 | \r | |
5457 | - slot_tp_getattro() is used when __getattribute__ is overridden\r | |
5458 | but no __getattr__ hook is present;\r | |
5459 | \r | |
5460 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present.\r | |
5461 | \r | |
5462 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this\r | |
5463 | detects the absence of __getattr__ and then installs the simpler slot if\r | |
5464 | necessary. */\r | |
5465 | \r | |
5466 | static PyObject *\r | |
5467 | slot_tp_getattro(PyObject *self, PyObject *name)\r | |
5468 | {\r | |
5469 | static PyObject *getattribute_str = NULL;\r | |
5470 | return call_method(self, "__getattribute__", &getattribute_str,\r | |
5471 | "(O)", name);\r | |
5472 | }\r | |
5473 | \r | |
5474 | static PyObject *\r | |
5475 | call_attribute(PyObject *self, PyObject *attr, PyObject *name)\r | |
5476 | {\r | |
5477 | PyObject *res, *descr = NULL;\r | |
5478 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get;\r | |
5479 | \r | |
5480 | if (f != NULL) {\r | |
5481 | descr = f(attr, self, (PyObject *)(Py_TYPE(self)));\r | |
5482 | if (descr == NULL)\r | |
5483 | return NULL;\r | |
5484 | else\r | |
5485 | attr = descr;\r | |
5486 | }\r | |
5487 | res = PyObject_CallFunctionObjArgs(attr, name, NULL);\r | |
5488 | Py_XDECREF(descr);\r | |
5489 | return res;\r | |
5490 | }\r | |
5491 | \r | |
5492 | static PyObject *\r | |
5493 | slot_tp_getattr_hook(PyObject *self, PyObject *name)\r | |
5494 | {\r | |
5495 | PyTypeObject *tp = Py_TYPE(self);\r | |
5496 | PyObject *getattr, *getattribute, *res;\r | |
5497 | static PyObject *getattribute_str = NULL;\r | |
5498 | static PyObject *getattr_str = NULL;\r | |
5499 | \r | |
5500 | if (getattr_str == NULL) {\r | |
5501 | getattr_str = PyString_InternFromString("__getattr__");\r | |
5502 | if (getattr_str == NULL)\r | |
5503 | return NULL;\r | |
5504 | }\r | |
5505 | if (getattribute_str == NULL) {\r | |
5506 | getattribute_str =\r | |
5507 | PyString_InternFromString("__getattribute__");\r | |
5508 | if (getattribute_str == NULL)\r | |
5509 | return NULL;\r | |
5510 | }\r | |
5511 | /* speed hack: we could use lookup_maybe, but that would resolve the\r | |
5512 | method fully for each attribute lookup for classes with\r | |
5513 | __getattr__, even when the attribute is present. So we use\r | |
5514 | _PyType_Lookup and create the method only when needed, with\r | |
5515 | call_attribute. */\r | |
5516 | getattr = _PyType_Lookup(tp, getattr_str);\r | |
5517 | if (getattr == NULL) {\r | |
5518 | /* No __getattr__ hook: use a simpler dispatcher */\r | |
5519 | tp->tp_getattro = slot_tp_getattro;\r | |
5520 | return slot_tp_getattro(self, name);\r | |
5521 | }\r | |
5522 | Py_INCREF(getattr);\r | |
5523 | /* speed hack: we could use lookup_maybe, but that would resolve the\r | |
5524 | method fully for each attribute lookup for classes with\r | |
5525 | __getattr__, even when self has the default __getattribute__\r | |
5526 | method. So we use _PyType_Lookup and create the method only when\r | |
5527 | needed, with call_attribute. */\r | |
5528 | getattribute = _PyType_Lookup(tp, getattribute_str);\r | |
5529 | if (getattribute == NULL ||\r | |
5530 | (Py_TYPE(getattribute) == &PyWrapperDescr_Type &&\r | |
5531 | ((PyWrapperDescrObject *)getattribute)->d_wrapped ==\r | |
5532 | (void *)PyObject_GenericGetAttr))\r | |
5533 | res = PyObject_GenericGetAttr(self, name);\r | |
5534 | else {\r | |
5535 | Py_INCREF(getattribute);\r | |
5536 | res = call_attribute(self, getattribute, name);\r | |
5537 | Py_DECREF(getattribute);\r | |
5538 | }\r | |
5539 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {\r | |
5540 | PyErr_Clear();\r | |
5541 | res = call_attribute(self, getattr, name);\r | |
5542 | }\r | |
5543 | Py_DECREF(getattr);\r | |
5544 | return res;\r | |
5545 | }\r | |
5546 | \r | |
5547 | static int\r | |
5548 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)\r | |
5549 | {\r | |
5550 | PyObject *res;\r | |
5551 | static PyObject *delattr_str, *setattr_str;\r | |
5552 | \r | |
5553 | if (value == NULL)\r | |
5554 | res = call_method(self, "__delattr__", &delattr_str,\r | |
5555 | "(O)", name);\r | |
5556 | else\r | |
5557 | res = call_method(self, "__setattr__", &setattr_str,\r | |
5558 | "(OO)", name, value);\r | |
5559 | if (res == NULL)\r | |
5560 | return -1;\r | |
5561 | Py_DECREF(res);\r | |
5562 | return 0;\r | |
5563 | }\r | |
5564 | \r | |
5565 | static char *name_op[] = {\r | |
5566 | "__lt__",\r | |
5567 | "__le__",\r | |
5568 | "__eq__",\r | |
5569 | "__ne__",\r | |
5570 | "__gt__",\r | |
5571 | "__ge__",\r | |
5572 | };\r | |
5573 | \r | |
5574 | static PyObject *\r | |
5575 | half_richcompare(PyObject *self, PyObject *other, int op)\r | |
5576 | {\r | |
5577 | PyObject *func, *args, *res;\r | |
5578 | static PyObject *op_str[6];\r | |
5579 | \r | |
5580 | func = lookup_method(self, name_op[op], &op_str[op]);\r | |
5581 | if (func == NULL) {\r | |
5582 | PyErr_Clear();\r | |
5583 | Py_INCREF(Py_NotImplemented);\r | |
5584 | return Py_NotImplemented;\r | |
5585 | }\r | |
5586 | args = PyTuple_Pack(1, other);\r | |
5587 | if (args == NULL)\r | |
5588 | res = NULL;\r | |
5589 | else {\r | |
5590 | res = PyObject_Call(func, args, NULL);\r | |
5591 | Py_DECREF(args);\r | |
5592 | }\r | |
5593 | Py_DECREF(func);\r | |
5594 | return res;\r | |
5595 | }\r | |
5596 | \r | |
5597 | static PyObject *\r | |
5598 | slot_tp_richcompare(PyObject *self, PyObject *other, int op)\r | |
5599 | {\r | |
5600 | PyObject *res;\r | |
5601 | \r | |
5602 | if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) {\r | |
5603 | res = half_richcompare(self, other, op);\r | |
5604 | if (res != Py_NotImplemented)\r | |
5605 | return res;\r | |
5606 | Py_DECREF(res);\r | |
5607 | }\r | |
5608 | if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) {\r | |
5609 | res = half_richcompare(other, self, _Py_SwappedOp[op]);\r | |
5610 | if (res != Py_NotImplemented) {\r | |
5611 | return res;\r | |
5612 | }\r | |
5613 | Py_DECREF(res);\r | |
5614 | }\r | |
5615 | Py_INCREF(Py_NotImplemented);\r | |
5616 | return Py_NotImplemented;\r | |
5617 | }\r | |
5618 | \r | |
5619 | static PyObject *\r | |
5620 | slot_tp_iter(PyObject *self)\r | |
5621 | {\r | |
5622 | PyObject *func, *res;\r | |
5623 | static PyObject *iter_str, *getitem_str;\r | |
5624 | \r | |
5625 | func = lookup_method(self, "__iter__", &iter_str);\r | |
5626 | if (func != NULL) {\r | |
5627 | PyObject *args;\r | |
5628 | args = res = PyTuple_New(0);\r | |
5629 | if (args != NULL) {\r | |
5630 | res = PyObject_Call(func, args, NULL);\r | |
5631 | Py_DECREF(args);\r | |
5632 | }\r | |
5633 | Py_DECREF(func);\r | |
5634 | return res;\r | |
5635 | }\r | |
5636 | PyErr_Clear();\r | |
5637 | func = lookup_method(self, "__getitem__", &getitem_str);\r | |
5638 | if (func == NULL) {\r | |
5639 | PyErr_Format(PyExc_TypeError,\r | |
5640 | "'%.200s' object is not iterable",\r | |
5641 | Py_TYPE(self)->tp_name);\r | |
5642 | return NULL;\r | |
5643 | }\r | |
5644 | Py_DECREF(func);\r | |
5645 | return PySeqIter_New(self);\r | |
5646 | }\r | |
5647 | \r | |
5648 | static PyObject *\r | |
5649 | slot_tp_iternext(PyObject *self)\r | |
5650 | {\r | |
5651 | static PyObject *next_str;\r | |
5652 | return call_method(self, "next", &next_str, "()");\r | |
5653 | }\r | |
5654 | \r | |
5655 | static PyObject *\r | |
5656 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)\r | |
5657 | {\r | |
5658 | PyTypeObject *tp = Py_TYPE(self);\r | |
5659 | PyObject *get;\r | |
5660 | static PyObject *get_str = NULL;\r | |
5661 | \r | |
5662 | if (get_str == NULL) {\r | |
5663 | get_str = PyString_InternFromString("__get__");\r | |
5664 | if (get_str == NULL)\r | |
5665 | return NULL;\r | |
5666 | }\r | |
5667 | get = _PyType_Lookup(tp, get_str);\r | |
5668 | if (get == NULL) {\r | |
5669 | /* Avoid further slowdowns */\r | |
5670 | if (tp->tp_descr_get == slot_tp_descr_get)\r | |
5671 | tp->tp_descr_get = NULL;\r | |
5672 | Py_INCREF(self);\r | |
5673 | return self;\r | |
5674 | }\r | |
5675 | if (obj == NULL)\r | |
5676 | obj = Py_None;\r | |
5677 | if (type == NULL)\r | |
5678 | type = Py_None;\r | |
5679 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);\r | |
5680 | }\r | |
5681 | \r | |
5682 | static int\r | |
5683 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)\r | |
5684 | {\r | |
5685 | PyObject *res;\r | |
5686 | static PyObject *del_str, *set_str;\r | |
5687 | \r | |
5688 | if (value == NULL)\r | |
5689 | res = call_method(self, "__delete__", &del_str,\r | |
5690 | "(O)", target);\r | |
5691 | else\r | |
5692 | res = call_method(self, "__set__", &set_str,\r | |
5693 | "(OO)", target, value);\r | |
5694 | if (res == NULL)\r | |
5695 | return -1;\r | |
5696 | Py_DECREF(res);\r | |
5697 | return 0;\r | |
5698 | }\r | |
5699 | \r | |
5700 | static int\r | |
5701 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)\r | |
5702 | {\r | |
5703 | static PyObject *init_str;\r | |
5704 | PyObject *meth = lookup_method(self, "__init__", &init_str);\r | |
5705 | PyObject *res;\r | |
5706 | \r | |
5707 | if (meth == NULL)\r | |
5708 | return -1;\r | |
5709 | res = PyObject_Call(meth, args, kwds);\r | |
5710 | Py_DECREF(meth);\r | |
5711 | if (res == NULL)\r | |
5712 | return -1;\r | |
5713 | if (res != Py_None) {\r | |
5714 | PyErr_Format(PyExc_TypeError,\r | |
5715 | "__init__() should return None, not '%.200s'",\r | |
5716 | Py_TYPE(res)->tp_name);\r | |
5717 | Py_DECREF(res);\r | |
5718 | return -1;\r | |
5719 | }\r | |
5720 | Py_DECREF(res);\r | |
5721 | return 0;\r | |
5722 | }\r | |
5723 | \r | |
5724 | static PyObject *\r | |
5725 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r | |
5726 | {\r | |
5727 | static PyObject *new_str;\r | |
5728 | PyObject *func;\r | |
5729 | PyObject *newargs, *x;\r | |
5730 | Py_ssize_t i, n;\r | |
5731 | \r | |
5732 | if (new_str == NULL) {\r | |
5733 | new_str = PyString_InternFromString("__new__");\r | |
5734 | if (new_str == NULL)\r | |
5735 | return NULL;\r | |
5736 | }\r | |
5737 | func = PyObject_GetAttr((PyObject *)type, new_str);\r | |
5738 | if (func == NULL)\r | |
5739 | return NULL;\r | |
5740 | assert(PyTuple_Check(args));\r | |
5741 | n = PyTuple_GET_SIZE(args);\r | |
5742 | newargs = PyTuple_New(n+1);\r | |
5743 | if (newargs == NULL)\r | |
5744 | return NULL;\r | |
5745 | Py_INCREF(type);\r | |
5746 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);\r | |
5747 | for (i = 0; i < n; i++) {\r | |
5748 | x = PyTuple_GET_ITEM(args, i);\r | |
5749 | Py_INCREF(x);\r | |
5750 | PyTuple_SET_ITEM(newargs, i+1, x);\r | |
5751 | }\r | |
5752 | x = PyObject_Call(func, newargs, kwds);\r | |
5753 | Py_DECREF(newargs);\r | |
5754 | Py_DECREF(func);\r | |
5755 | return x;\r | |
5756 | }\r | |
5757 | \r | |
5758 | static void\r | |
5759 | slot_tp_del(PyObject *self)\r | |
5760 | {\r | |
5761 | static PyObject *del_str = NULL;\r | |
5762 | PyObject *del, *res;\r | |
5763 | PyObject *error_type, *error_value, *error_traceback;\r | |
5764 | \r | |
5765 | /* Temporarily resurrect the object. */\r | |
5766 | assert(self->ob_refcnt == 0);\r | |
5767 | self->ob_refcnt = 1;\r | |
5768 | \r | |
5769 | /* Save the current exception, if any. */\r | |
5770 | PyErr_Fetch(&error_type, &error_value, &error_traceback);\r | |
5771 | \r | |
5772 | /* Execute __del__ method, if any. */\r | |
5773 | del = lookup_maybe(self, "__del__", &del_str);\r | |
5774 | if (del != NULL) {\r | |
5775 | res = PyEval_CallObject(del, NULL);\r | |
5776 | if (res == NULL)\r | |
5777 | PyErr_WriteUnraisable(del);\r | |
5778 | else\r | |
5779 | Py_DECREF(res);\r | |
5780 | Py_DECREF(del);\r | |
5781 | }\r | |
5782 | \r | |
5783 | /* Restore the saved exception. */\r | |
5784 | PyErr_Restore(error_type, error_value, error_traceback);\r | |
5785 | \r | |
5786 | /* Undo the temporary resurrection; can't use DECREF here, it would\r | |
5787 | * cause a recursive call.\r | |
5788 | */\r | |
5789 | assert(self->ob_refcnt > 0);\r | |
5790 | if (--self->ob_refcnt == 0)\r | |
5791 | return; /* this is the normal path out */\r | |
5792 | \r | |
5793 | /* __del__ resurrected it! Make it look like the original Py_DECREF\r | |
5794 | * never happened.\r | |
5795 | */\r | |
5796 | {\r | |
5797 | Py_ssize_t refcnt = self->ob_refcnt;\r | |
5798 | _Py_NewReference(self);\r | |
5799 | self->ob_refcnt = refcnt;\r | |
5800 | }\r | |
5801 | assert(!PyType_IS_GC(Py_TYPE(self)) ||\r | |
5802 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);\r | |
5803 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so\r | |
5804 | * we need to undo that. */\r | |
5805 | _Py_DEC_REFTOTAL;\r | |
5806 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object\r | |
5807 | * chain, so no more to do there.\r | |
5808 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and\r | |
5809 | * _Py_NewReference bumped tp_allocs: both of those need to be\r | |
5810 | * undone.\r | |
5811 | */\r | |
5812 | #ifdef COUNT_ALLOCS\r | |
5813 | --Py_TYPE(self)->tp_frees;\r | |
5814 | --Py_TYPE(self)->tp_allocs;\r | |
5815 | #endif\r | |
5816 | }\r | |
5817 | \r | |
5818 | \r | |
5819 | /*\r | |
5820 | Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper functions.\r | |
5821 | \r | |
5822 | The table is ordered by offsets relative to the 'PyHeapTypeObject' structure,\r | |
5823 | which incorporates the additional structures used for numbers, sequences and\r | |
5824 | mappings. Note that multiple names may map to the same slot (e.g. __eq__,\r | |
5825 | __ne__ etc. all map to tp_richcompare) and one name may map to multiple slots\r | |
5826 | (e.g. __str__ affects tp_str as well as tp_repr). The table is terminated with\r | |
5827 | an all-zero entry. (This table is further initialized in init_slotdefs().)\r | |
5828 | */\r | |
5829 | \r | |
5830 | typedef struct wrapperbase slotdef;\r | |
5831 | \r | |
5832 | #undef TPSLOT\r | |
5833 | #undef FLSLOT\r | |
5834 | #undef ETSLOT\r | |
5835 | #undef SQSLOT\r | |
5836 | #undef MPSLOT\r | |
5837 | #undef NBSLOT\r | |
5838 | #undef UNSLOT\r | |
5839 | #undef IBSLOT\r | |
5840 | #undef BINSLOT\r | |
5841 | #undef RBINSLOT\r | |
5842 | \r | |
5843 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r | |
5844 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \\r | |
5845 | PyDoc_STR(DOC)}\r | |
5846 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \\r | |
5847 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \\r | |
5848 | PyDoc_STR(DOC), FLAGS}\r | |
5849 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r | |
5850 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \\r | |
5851 | PyDoc_STR(DOC)}\r | |
5852 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r | |
5853 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)\r | |
5854 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r | |
5855 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)\r | |
5856 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r | |
5857 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)\r | |
5858 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r | |
5859 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \\r | |
5860 | "x." NAME "() <==> " DOC)\r | |
5861 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r | |
5862 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \\r | |
5863 | "x." NAME "(y) <==> x" DOC "y")\r | |
5864 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \\r | |
5865 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \\r | |
5866 | "x." NAME "(y) <==> x" DOC "y")\r | |
5867 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \\r | |
5868 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \\r | |
5869 | "x." NAME "(y) <==> y" DOC "x")\r | |
5870 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \\r | |
5871 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \\r | |
5872 | "x." NAME "(y) <==> " DOC)\r | |
5873 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \\r | |
5874 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \\r | |
5875 | "x." NAME "(y) <==> " DOC)\r | |
5876 | \r | |
5877 | static slotdef slotdefs[] = {\r | |
5878 | TPSLOT("__str__", tp_print, NULL, NULL, ""),\r | |
5879 | TPSLOT("__repr__", tp_print, NULL, NULL, ""),\r | |
5880 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),\r | |
5881 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),\r | |
5882 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),\r | |
5883 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),\r | |
5884 | TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,\r | |
5885 | "x.__cmp__(y) <==> cmp(x,y)"),\r | |
5886 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,\r | |
5887 | "x.__repr__() <==> repr(x)"),\r | |
5888 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,\r | |
5889 | "x.__hash__() <==> hash(x)"),\r | |
5890 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,\r | |
5891 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),\r | |
5892 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,\r | |
5893 | "x.__str__() <==> str(x)"),\r | |
5894 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,\r | |
5895 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),\r | |
5896 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),\r | |
5897 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,\r | |
5898 | "x.__setattr__('name', value) <==> x.name = value"),\r | |
5899 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,\r | |
5900 | "x.__delattr__('name') <==> del x.name"),\r | |
5901 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,\r | |
5902 | "x.__lt__(y) <==> x<y"),\r | |
5903 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,\r | |
5904 | "x.__le__(y) <==> x<=y"),\r | |
5905 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,\r | |
5906 | "x.__eq__(y) <==> x==y"),\r | |
5907 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,\r | |
5908 | "x.__ne__(y) <==> x!=y"),\r | |
5909 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,\r | |
5910 | "x.__gt__(y) <==> x>y"),\r | |
5911 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,\r | |
5912 | "x.__ge__(y) <==> x>=y"),\r | |
5913 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,\r | |
5914 | "x.__iter__() <==> iter(x)"),\r | |
5915 | TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,\r | |
5916 | "x.next() -> the next value, or raise StopIteration"),\r | |
5917 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,\r | |
5918 | "descr.__get__(obj[, type]) -> value"),\r | |
5919 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,\r | |
5920 | "descr.__set__(obj, value)"),\r | |
5921 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,\r | |
5922 | wrap_descr_delete, "descr.__delete__(obj)"),\r | |
5923 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,\r | |
5924 | "x.__init__(...) initializes x; "\r | |
5925 | "see help(type(x)) for signature",\r | |
5926 | PyWrapperFlag_KEYWORDS),\r | |
5927 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),\r | |
5928 | TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),\r | |
5929 | BINSLOT("__add__", nb_add, slot_nb_add,\r | |
5930 | "+"),\r | |
5931 | RBINSLOT("__radd__", nb_add, slot_nb_add,\r | |
5932 | "+"),\r | |
5933 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract,\r | |
5934 | "-"),\r | |
5935 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,\r | |
5936 | "-"),\r | |
5937 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply,\r | |
5938 | "*"),\r | |
5939 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,\r | |
5940 | "*"),\r | |
5941 | BINSLOT("__div__", nb_divide, slot_nb_divide,\r | |
5942 | "/"),\r | |
5943 | RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,\r | |
5944 | "/"),\r | |
5945 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder,\r | |
5946 | "%"),\r | |
5947 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,\r | |
5948 | "%"),\r | |
5949 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,\r | |
5950 | "divmod(x, y)"),\r | |
5951 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,\r | |
5952 | "divmod(y, x)"),\r | |
5953 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,\r | |
5954 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"),\r | |
5955 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,\r | |
5956 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),\r | |
5957 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),\r | |
5958 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),\r | |
5959 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,\r | |
5960 | "abs(x)"),\r | |
5961 | UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred,\r | |
5962 | "x != 0"),\r | |
5963 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),\r | |
5964 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),\r | |
5965 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),\r | |
5966 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),\r | |
5967 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),\r | |
5968 | BINSLOT("__and__", nb_and, slot_nb_and, "&"),\r | |
5969 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),\r | |
5970 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),\r | |
5971 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),\r | |
5972 | BINSLOT("__or__", nb_or, slot_nb_or, "|"),\r | |
5973 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),\r | |
5974 | NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,\r | |
5975 | "x.__coerce__(y) <==> coerce(x, y)"),\r | |
5976 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,\r | |
5977 | "int(x)"),\r | |
5978 | UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,\r | |
5979 | "long(x)"),\r | |
5980 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,\r | |
5981 | "float(x)"),\r | |
5982 | UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,\r | |
5983 | "oct(x)"),\r | |
5984 | UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,\r | |
5985 | "hex(x)"),\r | |
5986 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,\r | |
5987 | wrap_binaryfunc, "+="),\r | |
5988 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,\r | |
5989 | wrap_binaryfunc, "-="),\r | |
5990 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,\r | |
5991 | wrap_binaryfunc, "*="),\r | |
5992 | IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,\r | |
5993 | wrap_binaryfunc, "/="),\r | |
5994 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,\r | |
5995 | wrap_binaryfunc, "%="),\r | |
5996 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,\r | |
5997 | wrap_binaryfunc, "**="),\r | |
5998 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,\r | |
5999 | wrap_binaryfunc, "<<="),\r | |
6000 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,\r | |
6001 | wrap_binaryfunc, ">>="),\r | |
6002 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,\r | |
6003 | wrap_binaryfunc, "&="),\r | |
6004 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,\r | |
6005 | wrap_binaryfunc, "^="),\r | |
6006 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,\r | |
6007 | wrap_binaryfunc, "|="),\r | |
6008 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),\r | |
6009 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),\r | |
6010 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),\r | |
6011 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),\r | |
6012 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide,\r | |
6013 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//="),\r | |
6014 | IBSLOT("__itruediv__", nb_inplace_true_divide,\r | |
6015 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/="),\r | |
6016 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,\r | |
6017 | "x[y:z] <==> x[y.__index__():z.__index__()]"),\r | |
6018 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,\r | |
6019 | "x.__len__() <==> len(x)"),\r | |
6020 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,\r | |
6021 | wrap_binaryfunc,\r | |
6022 | "x.__getitem__(y) <==> x[y]"),\r | |
6023 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,\r | |
6024 | wrap_objobjargproc,\r | |
6025 | "x.__setitem__(i, y) <==> x[i]=y"),\r | |
6026 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,\r | |
6027 | wrap_delitem,\r | |
6028 | "x.__delitem__(y) <==> del x[y]"),\r | |
6029 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,\r | |
6030 | "x.__len__() <==> len(x)"),\r | |
6031 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.\r | |
6032 | The logic in abstract.c always falls back to nb_add/nb_multiply in\r | |
6033 | this case. Defining both the nb_* and the sq_* slots to call the\r | |
6034 | user-defined methods has unexpected side-effects, as shown by\r | |
6035 | test_descr.notimplemented() */\r | |
6036 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,\r | |
6037 | "x.__add__(y) <==> x+y"),\r | |
6038 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,\r | |
6039 | "x.__mul__(n) <==> x*n"),\r | |
6040 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,\r | |
6041 | "x.__rmul__(n) <==> n*x"),\r | |
6042 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,\r | |
6043 | "x.__getitem__(y) <==> x[y]"),\r | |
6044 | SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,\r | |
6045 | "x.__getslice__(i, j) <==> x[i:j]\n\\r | |
6046 | \n\\r | |
6047 | Use of negative indices is not supported."),\r | |
6048 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,\r | |
6049 | "x.__setitem__(i, y) <==> x[i]=y"),\r | |
6050 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,\r | |
6051 | "x.__delitem__(y) <==> del x[y]"),\r | |
6052 | SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,\r | |
6053 | wrap_ssizessizeobjargproc,\r | |
6054 | "x.__setslice__(i, j, y) <==> x[i:j]=y\n\\r | |
6055 | \n\\r | |
6056 | Use of negative indices is not supported."),\r | |
6057 | SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,\r | |
6058 | "x.__delslice__(i, j) <==> del x[i:j]\n\\r | |
6059 | \n\\r | |
6060 | Use of negative indices is not supported."),\r | |
6061 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,\r | |
6062 | "x.__contains__(y) <==> y in x"),\r | |
6063 | SQSLOT("__iadd__", sq_inplace_concat, NULL,\r | |
6064 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),\r | |
6065 | SQSLOT("__imul__", sq_inplace_repeat, NULL,\r | |
6066 | wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),\r | |
6067 | {NULL}\r | |
6068 | };\r | |
6069 | \r | |
6070 | /* Given a type pointer and an offset gotten from a slotdef entry, return a\r | |
6071 | pointer to the actual slot. This is not quite the same as simply adding\r | |
6072 | the offset to the type pointer, since it takes care to indirect through the\r | |
6073 | proper indirection pointer (as_buffer, etc.); it returns NULL if the\r | |
6074 | indirection pointer is NULL. */\r | |
6075 | static void **\r | |
6076 | slotptr(PyTypeObject *type, int ioffset)\r | |
6077 | {\r | |
6078 | char *ptr;\r | |
6079 | long offset = ioffset;\r | |
6080 | \r | |
6081 | /* Note: this depends on the order of the members of PyHeapTypeObject! */\r | |
6082 | assert(offset >= 0);\r | |
6083 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));\r | |
6084 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {\r | |
6085 | ptr = (char *)type->tp_as_sequence;\r | |
6086 | offset -= offsetof(PyHeapTypeObject, as_sequence);\r | |
6087 | }\r | |
6088 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {\r | |
6089 | ptr = (char *)type->tp_as_mapping;\r | |
6090 | offset -= offsetof(PyHeapTypeObject, as_mapping);\r | |
6091 | }\r | |
6092 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {\r | |
6093 | ptr = (char *)type->tp_as_number;\r | |
6094 | offset -= offsetof(PyHeapTypeObject, as_number);\r | |
6095 | }\r | |
6096 | else {\r | |
6097 | ptr = (char *)type;\r | |
6098 | }\r | |
6099 | if (ptr != NULL)\r | |
6100 | ptr += offset;\r | |
6101 | return (void **)ptr;\r | |
6102 | }\r | |
6103 | \r | |
6104 | /* Length of array of slotdef pointers used to store slots with the\r | |
6105 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with\r | |
6106 | the same __name__, for any __name__. Since that's a static property, it is\r | |
6107 | appropriate to declare fixed-size arrays for this. */\r | |
6108 | #define MAX_EQUIV 10\r | |
6109 | \r | |
6110 | /* Return a slot pointer for a given name, but ONLY if the attribute has\r | |
6111 | exactly one slot function. The name must be an interned string. */\r | |
6112 | static void **\r | |
6113 | resolve_slotdups(PyTypeObject *type, PyObject *name)\r | |
6114 | {\r | |
6115 | /* XXX Maybe this could be optimized more -- but is it worth it? */\r | |
6116 | \r | |
6117 | /* pname and ptrs act as a little cache */\r | |
6118 | static PyObject *pname;\r | |
6119 | static slotdef *ptrs[MAX_EQUIV];\r | |
6120 | slotdef *p, **pp;\r | |
6121 | void **res, **ptr;\r | |
6122 | \r | |
6123 | if (pname != name) {\r | |
6124 | /* Collect all slotdefs that match name into ptrs. */\r | |
6125 | pname = name;\r | |
6126 | pp = ptrs;\r | |
6127 | for (p = slotdefs; p->name_strobj; p++) {\r | |
6128 | if (p->name_strobj == name)\r | |
6129 | *pp++ = p;\r | |
6130 | }\r | |
6131 | *pp = NULL;\r | |
6132 | }\r | |
6133 | \r | |
6134 | /* Look in all matching slots of the type; if exactly one of these has\r | |
6135 | a filled-in slot, return its value. Otherwise return NULL. */\r | |
6136 | res = NULL;\r | |
6137 | for (pp = ptrs; *pp; pp++) {\r | |
6138 | ptr = slotptr(type, (*pp)->offset);\r | |
6139 | if (ptr == NULL || *ptr == NULL)\r | |
6140 | continue;\r | |
6141 | if (res != NULL)\r | |
6142 | return NULL;\r | |
6143 | res = ptr;\r | |
6144 | }\r | |
6145 | return res;\r | |
6146 | }\r | |
6147 | \r | |
6148 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This\r | |
6149 | does some incredibly complex thinking and then sticks something into the\r | |
6150 | slot. (It sees if the adjacent slotdefs for the same slot have conflicting\r | |
6151 | interests, and then stores a generic wrapper or a specific function into\r | |
6152 | the slot.) Return a pointer to the next slotdef with a different offset,\r | |
6153 | because that's convenient for fixup_slot_dispatchers(). */\r | |
6154 | static slotdef *\r | |
6155 | update_one_slot(PyTypeObject *type, slotdef *p)\r | |
6156 | {\r | |
6157 | PyObject *descr;\r | |
6158 | PyWrapperDescrObject *d;\r | |
6159 | void *generic = NULL, *specific = NULL;\r | |
6160 | int use_generic = 0;\r | |
6161 | int offset = p->offset;\r | |
6162 | void **ptr = slotptr(type, offset);\r | |
6163 | \r | |
6164 | if (ptr == NULL) {\r | |
6165 | do {\r | |
6166 | ++p;\r | |
6167 | } while (p->offset == offset);\r | |
6168 | return p;\r | |
6169 | }\r | |
6170 | do {\r | |
6171 | descr = _PyType_Lookup(type, p->name_strobj);\r | |
6172 | if (descr == NULL) {\r | |
6173 | if (ptr == (void**)&type->tp_iternext) {\r | |
6174 | specific = _PyObject_NextNotImplemented;\r | |
6175 | }\r | |
6176 | continue;\r | |
6177 | }\r | |
6178 | if (Py_TYPE(descr) == &PyWrapperDescr_Type &&\r | |
6179 | ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_strobj) {\r | |
6180 | void **tptr = resolve_slotdups(type, p->name_strobj);\r | |
6181 | if (tptr == NULL || tptr == ptr)\r | |
6182 | generic = p->function;\r | |
6183 | d = (PyWrapperDescrObject *)descr;\r | |
6184 | if (d->d_base->wrapper == p->wrapper &&\r | |
6185 | PyType_IsSubtype(type, d->d_type))\r | |
6186 | {\r | |
6187 | if (specific == NULL ||\r | |
6188 | specific == d->d_wrapped)\r | |
6189 | specific = d->d_wrapped;\r | |
6190 | else\r | |
6191 | use_generic = 1;\r | |
6192 | }\r | |
6193 | }\r | |
6194 | else if (Py_TYPE(descr) == &PyCFunction_Type &&\r | |
6195 | PyCFunction_GET_FUNCTION(descr) ==\r | |
6196 | (PyCFunction)tp_new_wrapper &&\r | |
6197 | ptr == (void**)&type->tp_new)\r | |
6198 | {\r | |
6199 | /* The __new__ wrapper is not a wrapper descriptor,\r | |
6200 | so must be special-cased differently.\r | |
6201 | If we don't do this, creating an instance will\r | |
6202 | always use slot_tp_new which will look up\r | |
6203 | __new__ in the MRO which will call tp_new_wrapper\r | |
6204 | which will look through the base classes looking\r | |
6205 | for a static base and call its tp_new (usually\r | |
6206 | PyType_GenericNew), after performing various\r | |
6207 | sanity checks and constructing a new argument\r | |
6208 | list. Cut all that nonsense short -- this speeds\r | |
6209 | up instance creation tremendously. */\r | |
6210 | specific = (void *)type->tp_new;\r | |
6211 | /* XXX I'm not 100% sure that there isn't a hole\r | |
6212 | in this reasoning that requires additional\r | |
6213 | sanity checks. I'll buy the first person to\r | |
6214 | point out a bug in this reasoning a beer. */\r | |
6215 | }\r | |
6216 | else if (descr == Py_None &&\r | |
6217 | ptr == (void**)&type->tp_hash) {\r | |
6218 | /* We specifically allow __hash__ to be set to None\r | |
6219 | to prevent inheritance of the default\r | |
6220 | implementation from object.__hash__ */\r | |
6221 | specific = PyObject_HashNotImplemented;\r | |
6222 | }\r | |
6223 | else {\r | |
6224 | use_generic = 1;\r | |
6225 | generic = p->function;\r | |
6226 | }\r | |
6227 | } while ((++p)->offset == offset);\r | |
6228 | if (specific && !use_generic)\r | |
6229 | *ptr = specific;\r | |
6230 | else\r | |
6231 | *ptr = generic;\r | |
6232 | return p;\r | |
6233 | }\r | |
6234 | \r | |
6235 | /* In the type, update the slots whose slotdefs are gathered in the pp array.\r | |
6236 | This is a callback for update_subclasses(). */\r | |
6237 | static int\r | |
6238 | update_slots_callback(PyTypeObject *type, void *data)\r | |
6239 | {\r | |
6240 | slotdef **pp = (slotdef **)data;\r | |
6241 | \r | |
6242 | for (; *pp; pp++)\r | |
6243 | update_one_slot(type, *pp);\r | |
6244 | return 0;\r | |
6245 | }\r | |
6246 | \r | |
6247 | /* Initialize the slotdefs table by adding interned string objects for the\r | |
6248 | names and sorting the entries. */\r | |
6249 | static void\r | |
6250 | init_slotdefs(void)\r | |
6251 | {\r | |
6252 | slotdef *p;\r | |
6253 | static int initialized = 0;\r | |
6254 | \r | |
6255 | if (initialized)\r | |
6256 | return;\r | |
6257 | for (p = slotdefs; p->name; p++) {\r | |
6258 | /* Slots must be ordered by their offset in the PyHeapTypeObject. */\r | |
6259 | assert(!p[1].name || p->offset <= p[1].offset);\r | |
6260 | p->name_strobj = PyString_InternFromString(p->name);\r | |
6261 | if (!p->name_strobj)\r | |
6262 | Py_FatalError("Out of memory interning slotdef names");\r | |
6263 | }\r | |
6264 | initialized = 1;\r | |
6265 | }\r | |
6266 | \r | |
6267 | /* Update the slots after assignment to a class (type) attribute. */\r | |
6268 | static int\r | |
6269 | update_slot(PyTypeObject *type, PyObject *name)\r | |
6270 | {\r | |
6271 | slotdef *ptrs[MAX_EQUIV];\r | |
6272 | slotdef *p;\r | |
6273 | slotdef **pp;\r | |
6274 | int offset;\r | |
6275 | \r | |
6276 | /* Clear the VALID_VERSION flag of 'type' and all its\r | |
6277 | subclasses. This could possibly be unified with the\r | |
6278 | update_subclasses() recursion below, but carefully:\r | |
6279 | they each have their own conditions on which to stop\r | |
6280 | recursing into subclasses. */\r | |
6281 | PyType_Modified(type);\r | |
6282 | \r | |
6283 | init_slotdefs();\r | |
6284 | pp = ptrs;\r | |
6285 | for (p = slotdefs; p->name; p++) {\r | |
6286 | /* XXX assume name is interned! */\r | |
6287 | if (p->name_strobj == name)\r | |
6288 | *pp++ = p;\r | |
6289 | }\r | |
6290 | *pp = NULL;\r | |
6291 | for (pp = ptrs; *pp; pp++) {\r | |
6292 | p = *pp;\r | |
6293 | offset = p->offset;\r | |
6294 | while (p > slotdefs && (p-1)->offset == offset)\r | |
6295 | --p;\r | |
6296 | *pp = p;\r | |
6297 | }\r | |
6298 | if (ptrs[0] == NULL)\r | |
6299 | return 0; /* Not an attribute that affects any slots */\r | |
6300 | return update_subclasses(type, name,\r | |
6301 | update_slots_callback, (void *)ptrs);\r | |
6302 | }\r | |
6303 | \r | |
6304 | /* Store the proper functions in the slot dispatches at class (type)\r | |
6305 | definition time, based upon which operations the class overrides in its\r | |
6306 | dict. */\r | |
6307 | static void\r | |
6308 | fixup_slot_dispatchers(PyTypeObject *type)\r | |
6309 | {\r | |
6310 | slotdef *p;\r | |
6311 | \r | |
6312 | init_slotdefs();\r | |
6313 | for (p = slotdefs; p->name; )\r | |
6314 | p = update_one_slot(type, p);\r | |
6315 | }\r | |
6316 | \r | |
6317 | static void\r | |
6318 | update_all_slots(PyTypeObject* type)\r | |
6319 | {\r | |
6320 | slotdef *p;\r | |
6321 | \r | |
6322 | init_slotdefs();\r | |
6323 | for (p = slotdefs; p->name; p++) {\r | |
6324 | /* update_slot returns int but can't actually fail */\r | |
6325 | update_slot(type, p->name_strobj);\r | |
6326 | }\r | |
6327 | }\r | |
6328 | \r | |
6329 | /* recurse_down_subclasses() and update_subclasses() are mutually\r | |
6330 | recursive functions to call a callback for all subclasses,\r | |
6331 | but refraining from recursing into subclasses that define 'name'. */\r | |
6332 | \r | |
6333 | static int\r | |
6334 | update_subclasses(PyTypeObject *type, PyObject *name,\r | |
6335 | update_callback callback, void *data)\r | |
6336 | {\r | |
6337 | if (callback(type, data) < 0)\r | |
6338 | return -1;\r | |
6339 | return recurse_down_subclasses(type, name, callback, data);\r | |
6340 | }\r | |
6341 | \r | |
6342 | static int\r | |
6343 | recurse_down_subclasses(PyTypeObject *type, PyObject *name,\r | |
6344 | update_callback callback, void *data)\r | |
6345 | {\r | |
6346 | PyTypeObject *subclass;\r | |
6347 | PyObject *ref, *subclasses, *dict;\r | |
6348 | Py_ssize_t i, n;\r | |
6349 | \r | |
6350 | subclasses = type->tp_subclasses;\r | |
6351 | if (subclasses == NULL)\r | |
6352 | return 0;\r | |
6353 | assert(PyList_Check(subclasses));\r | |
6354 | n = PyList_GET_SIZE(subclasses);\r | |
6355 | for (i = 0; i < n; i++) {\r | |
6356 | ref = PyList_GET_ITEM(subclasses, i);\r | |
6357 | assert(PyWeakref_CheckRef(ref));\r | |
6358 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);\r | |
6359 | assert(subclass != NULL);\r | |
6360 | if ((PyObject *)subclass == Py_None)\r | |
6361 | continue;\r | |
6362 | assert(PyType_Check(subclass));\r | |
6363 | /* Avoid recursing down into unaffected classes */\r | |
6364 | dict = subclass->tp_dict;\r | |
6365 | if (dict != NULL && PyDict_Check(dict) &&\r | |
6366 | PyDict_GetItem(dict, name) != NULL)\r | |
6367 | continue;\r | |
6368 | if (update_subclasses(subclass, name, callback, data) < 0)\r | |
6369 | return -1;\r | |
6370 | }\r | |
6371 | return 0;\r | |
6372 | }\r | |
6373 | \r | |
6374 | /* This function is called by PyType_Ready() to populate the type's\r | |
6375 | dictionary with method descriptors for function slots. For each\r | |
6376 | function slot (like tp_repr) that's defined in the type, one or more\r | |
6377 | corresponding descriptors are added in the type's tp_dict dictionary\r | |
6378 | under the appropriate name (like __repr__). Some function slots\r | |
6379 | cause more than one descriptor to be added (for example, the nb_add\r | |
6380 | slot adds both __add__ and __radd__ descriptors) and some function\r | |
6381 | slots compete for the same descriptor (for example both sq_item and\r | |
6382 | mp_subscript generate a __getitem__ descriptor).\r | |
6383 | \r | |
6384 | In the latter case, the first slotdef entry encountered wins. Since\r | |
6385 | slotdef entries are sorted by the offset of the slot in the\r | |
6386 | PyHeapTypeObject, this gives us some control over disambiguating\r | |
6387 | between competing slots: the members of PyHeapTypeObject are listed\r | |
6388 | from most general to least general, so the most general slot is\r | |
6389 | preferred. In particular, because as_mapping comes before as_sequence,\r | |
6390 | for a type that defines both mp_subscript and sq_item, mp_subscript\r | |
6391 | wins.\r | |
6392 | \r | |
6393 | This only adds new descriptors and doesn't overwrite entries in\r | |
6394 | tp_dict that were previously defined. The descriptors contain a\r | |
6395 | reference to the C function they must call, so that it's safe if they\r | |
6396 | are copied into a subtype's __dict__ and the subtype has a different\r | |
6397 | C function in its slot -- calling the method defined by the\r | |
6398 | descriptor will call the C function that was used to create it,\r | |
6399 | rather than the C function present in the slot when it is called.\r | |
6400 | (This is important because a subtype may have a C function in the\r | |
6401 | slot that calls the method from the dictionary, and we want to avoid\r | |
6402 | infinite recursion here.) */\r | |
6403 | \r | |
6404 | static int\r | |
6405 | add_operators(PyTypeObject *type)\r | |
6406 | {\r | |
6407 | PyObject *dict = type->tp_dict;\r | |
6408 | slotdef *p;\r | |
6409 | PyObject *descr;\r | |
6410 | void **ptr;\r | |
6411 | \r | |
6412 | init_slotdefs();\r | |
6413 | for (p = slotdefs; p->name; p++) {\r | |
6414 | if (p->wrapper == NULL)\r | |
6415 | continue;\r | |
6416 | ptr = slotptr(type, p->offset);\r | |
6417 | if (!ptr || !*ptr)\r | |
6418 | continue;\r | |
6419 | if (PyDict_GetItem(dict, p->name_strobj))\r | |
6420 | continue;\r | |
6421 | if (*ptr == PyObject_HashNotImplemented) {\r | |
6422 | /* Classes may prevent the inheritance of the tp_hash\r | |
6423 | slot by storing PyObject_HashNotImplemented in it. Make it\r | |
6424 | visible as a None value for the __hash__ attribute. */\r | |
6425 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)\r | |
6426 | return -1;\r | |
6427 | }\r | |
6428 | else {\r | |
6429 | descr = PyDescr_NewWrapper(type, p, *ptr);\r | |
6430 | if (descr == NULL)\r | |
6431 | return -1;\r | |
6432 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)\r | |
6433 | return -1;\r | |
6434 | Py_DECREF(descr);\r | |
6435 | }\r | |
6436 | }\r | |
6437 | if (type->tp_new != NULL) {\r | |
6438 | if (add_tp_new_wrapper(type) < 0)\r | |
6439 | return -1;\r | |
6440 | }\r | |
6441 | return 0;\r | |
6442 | }\r | |
6443 | \r | |
6444 | \r | |
6445 | /* Cooperative 'super' */\r | |
6446 | \r | |
6447 | typedef struct {\r | |
6448 | PyObject_HEAD\r | |
6449 | PyTypeObject *type;\r | |
6450 | PyObject *obj;\r | |
6451 | PyTypeObject *obj_type;\r | |
6452 | } superobject;\r | |
6453 | \r | |
6454 | static PyMemberDef super_members[] = {\r | |
6455 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,\r | |
6456 | "the class invoking super()"},\r | |
6457 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,\r | |
6458 | "the instance invoking super(); may be None"},\r | |
6459 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,\r | |
6460 | "the type of the instance invoking super(); may be None"},\r | |
6461 | {0}\r | |
6462 | };\r | |
6463 | \r | |
6464 | static void\r | |
6465 | super_dealloc(PyObject *self)\r | |
6466 | {\r | |
6467 | superobject *su = (superobject *)self;\r | |
6468 | \r | |
6469 | _PyObject_GC_UNTRACK(self);\r | |
6470 | Py_XDECREF(su->obj);\r | |
6471 | Py_XDECREF(su->type);\r | |
6472 | Py_XDECREF(su->obj_type);\r | |
6473 | Py_TYPE(self)->tp_free(self);\r | |
6474 | }\r | |
6475 | \r | |
6476 | static PyObject *\r | |
6477 | super_repr(PyObject *self)\r | |
6478 | {\r | |
6479 | superobject *su = (superobject *)self;\r | |
6480 | \r | |
6481 | if (su->obj_type)\r | |
6482 | return PyString_FromFormat(\r | |
6483 | "<super: <class '%s'>, <%s object>>",\r | |
6484 | su->type ? su->type->tp_name : "NULL",\r | |
6485 | su->obj_type->tp_name);\r | |
6486 | else\r | |
6487 | return PyString_FromFormat(\r | |
6488 | "<super: <class '%s'>, NULL>",\r | |
6489 | su->type ? su->type->tp_name : "NULL");\r | |
6490 | }\r | |
6491 | \r | |
6492 | static PyObject *\r | |
6493 | super_getattro(PyObject *self, PyObject *name)\r | |
6494 | {\r | |
6495 | superobject *su = (superobject *)self;\r | |
6496 | int skip = su->obj_type == NULL;\r | |
6497 | \r | |
6498 | if (!skip) {\r | |
6499 | /* We want __class__ to return the class of the super object\r | |
6500 | (i.e. super, or a subclass), not the class of su->obj. */\r | |
6501 | skip = (PyString_Check(name) &&\r | |
6502 | PyString_GET_SIZE(name) == 9 &&\r | |
6503 | strcmp(PyString_AS_STRING(name), "__class__") == 0);\r | |
6504 | }\r | |
6505 | \r | |
6506 | if (!skip) {\r | |
6507 | PyObject *mro, *res, *tmp, *dict;\r | |
6508 | PyTypeObject *starttype;\r | |
6509 | descrgetfunc f;\r | |
6510 | Py_ssize_t i, n;\r | |
6511 | \r | |
6512 | starttype = su->obj_type;\r | |
6513 | mro = starttype->tp_mro;\r | |
6514 | \r | |
6515 | if (mro == NULL)\r | |
6516 | n = 0;\r | |
6517 | else {\r | |
6518 | assert(PyTuple_Check(mro));\r | |
6519 | n = PyTuple_GET_SIZE(mro);\r | |
6520 | }\r | |
6521 | for (i = 0; i < n; i++) {\r | |
6522 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))\r | |
6523 | break;\r | |
6524 | }\r | |
6525 | i++;\r | |
6526 | res = NULL;\r | |
6527 | for (; i < n; i++) {\r | |
6528 | tmp = PyTuple_GET_ITEM(mro, i);\r | |
6529 | if (PyType_Check(tmp))\r | |
6530 | dict = ((PyTypeObject *)tmp)->tp_dict;\r | |
6531 | else if (PyClass_Check(tmp))\r | |
6532 | dict = ((PyClassObject *)tmp)->cl_dict;\r | |
6533 | else\r | |
6534 | continue;\r | |
6535 | res = PyDict_GetItem(dict, name);\r | |
6536 | if (res != NULL) {\r | |
6537 | Py_INCREF(res);\r | |
6538 | f = Py_TYPE(res)->tp_descr_get;\r | |
6539 | if (f != NULL) {\r | |
6540 | tmp = f(res,\r | |
6541 | /* Only pass 'obj' param if\r | |
6542 | this is instance-mode super\r | |
6543 | (See SF ID #743627)\r | |
6544 | */\r | |
6545 | (su->obj == (PyObject *)\r | |
6546 | su->obj_type\r | |
6547 | ? (PyObject *)NULL\r | |
6548 | : su->obj),\r | |
6549 | (PyObject *)starttype);\r | |
6550 | Py_DECREF(res);\r | |
6551 | res = tmp;\r | |
6552 | }\r | |
6553 | return res;\r | |
6554 | }\r | |
6555 | }\r | |
6556 | }\r | |
6557 | return PyObject_GenericGetAttr(self, name);\r | |
6558 | }\r | |
6559 | \r | |
6560 | static PyTypeObject *\r | |
6561 | supercheck(PyTypeObject *type, PyObject *obj)\r | |
6562 | {\r | |
6563 | /* Check that a super() call makes sense. Return a type object.\r | |
6564 | \r | |
6565 | obj can be a new-style class, or an instance of one:\r | |
6566 | \r | |
6567 | - If it is a class, it must be a subclass of 'type'. This case is\r | |
6568 | used for class methods; the return value is obj.\r | |
6569 | \r | |
6570 | - If it is an instance, it must be an instance of 'type'. This is\r | |
6571 | the normal case; the return value is obj.__class__.\r | |
6572 | \r | |
6573 | But... when obj is an instance, we want to allow for the case where\r | |
6574 | Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!\r | |
6575 | This will allow using super() with a proxy for obj.\r | |
6576 | */\r | |
6577 | \r | |
6578 | /* Check for first bullet above (special case) */\r | |
6579 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {\r | |
6580 | Py_INCREF(obj);\r | |
6581 | return (PyTypeObject *)obj;\r | |
6582 | }\r | |
6583 | \r | |
6584 | /* Normal case */\r | |
6585 | if (PyType_IsSubtype(Py_TYPE(obj), type)) {\r | |
6586 | Py_INCREF(Py_TYPE(obj));\r | |
6587 | return Py_TYPE(obj);\r | |
6588 | }\r | |
6589 | else {\r | |
6590 | /* Try the slow way */\r | |
6591 | static PyObject *class_str = NULL;\r | |
6592 | PyObject *class_attr;\r | |
6593 | \r | |
6594 | if (class_str == NULL) {\r | |
6595 | class_str = PyString_FromString("__class__");\r | |
6596 | if (class_str == NULL)\r | |
6597 | return NULL;\r | |
6598 | }\r | |
6599 | \r | |
6600 | class_attr = PyObject_GetAttr(obj, class_str);\r | |
6601 | \r | |
6602 | if (class_attr != NULL &&\r | |
6603 | PyType_Check(class_attr) &&\r | |
6604 | (PyTypeObject *)class_attr != Py_TYPE(obj))\r | |
6605 | {\r | |
6606 | int ok = PyType_IsSubtype(\r | |
6607 | (PyTypeObject *)class_attr, type);\r | |
6608 | if (ok)\r | |
6609 | return (PyTypeObject *)class_attr;\r | |
6610 | }\r | |
6611 | \r | |
6612 | if (class_attr == NULL)\r | |
6613 | PyErr_Clear();\r | |
6614 | else\r | |
6615 | Py_DECREF(class_attr);\r | |
6616 | }\r | |
6617 | \r | |
6618 | PyErr_SetString(PyExc_TypeError,\r | |
6619 | "super(type, obj): "\r | |
6620 | "obj must be an instance or subtype of type");\r | |
6621 | return NULL;\r | |
6622 | }\r | |
6623 | \r | |
6624 | static PyObject *\r | |
6625 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type)\r | |
6626 | {\r | |
6627 | superobject *su = (superobject *)self;\r | |
6628 | superobject *newobj;\r | |
6629 | \r | |
6630 | if (obj == NULL || obj == Py_None || su->obj != NULL) {\r | |
6631 | /* Not binding to an object, or already bound */\r | |
6632 | Py_INCREF(self);\r | |
6633 | return self;\r | |
6634 | }\r | |
6635 | if (Py_TYPE(su) != &PySuper_Type)\r | |
6636 | /* If su is an instance of a (strict) subclass of super,\r | |
6637 | call its type */\r | |
6638 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),\r | |
6639 | su->type, obj, NULL);\r | |
6640 | else {\r | |
6641 | /* Inline the common case */\r | |
6642 | PyTypeObject *obj_type = supercheck(su->type, obj);\r | |
6643 | if (obj_type == NULL)\r | |
6644 | return NULL;\r | |
6645 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,\r | |
6646 | NULL, NULL);\r | |
6647 | if (newobj == NULL)\r | |
6648 | return NULL;\r | |
6649 | Py_INCREF(su->type);\r | |
6650 | Py_INCREF(obj);\r | |
6651 | newobj->type = su->type;\r | |
6652 | newobj->obj = obj;\r | |
6653 | newobj->obj_type = obj_type;\r | |
6654 | return (PyObject *)newobj;\r | |
6655 | }\r | |
6656 | }\r | |
6657 | \r | |
6658 | static int\r | |
6659 | super_init(PyObject *self, PyObject *args, PyObject *kwds)\r | |
6660 | {\r | |
6661 | superobject *su = (superobject *)self;\r | |
6662 | PyTypeObject *type;\r | |
6663 | PyObject *obj = NULL;\r | |
6664 | PyTypeObject *obj_type = NULL;\r | |
6665 | \r | |
6666 | if (!_PyArg_NoKeywords("super", kwds))\r | |
6667 | return -1;\r | |
6668 | if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))\r | |
6669 | return -1;\r | |
6670 | if (obj == Py_None)\r | |
6671 | obj = NULL;\r | |
6672 | if (obj != NULL) {\r | |
6673 | obj_type = supercheck(type, obj);\r | |
6674 | if (obj_type == NULL)\r | |
6675 | return -1;\r | |
6676 | Py_INCREF(obj);\r | |
6677 | }\r | |
6678 | Py_INCREF(type);\r | |
6679 | su->type = type;\r | |
6680 | su->obj = obj;\r | |
6681 | su->obj_type = obj_type;\r | |
6682 | return 0;\r | |
6683 | }\r | |
6684 | \r | |
6685 | PyDoc_STRVAR(super_doc,\r | |
6686 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"\r | |
6687 | "super(type) -> unbound super object\n"\r | |
6688 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"\r | |
6689 | "Typical use to call a cooperative superclass method:\n"\r | |
6690 | "class C(B):\n"\r | |
6691 | " def meth(self, arg):\n"\r | |
6692 | " super(C, self).meth(arg)");\r | |
6693 | \r | |
6694 | static int\r | |
6695 | super_traverse(PyObject *self, visitproc visit, void *arg)\r | |
6696 | {\r | |
6697 | superobject *su = (superobject *)self;\r | |
6698 | \r | |
6699 | Py_VISIT(su->obj);\r | |
6700 | Py_VISIT(su->type);\r | |
6701 | Py_VISIT(su->obj_type);\r | |
6702 | \r | |
6703 | return 0;\r | |
6704 | }\r | |
6705 | \r | |
6706 | PyTypeObject PySuper_Type = {\r | |
6707 | PyVarObject_HEAD_INIT(&PyType_Type, 0)\r | |
6708 | "super", /* tp_name */\r | |
6709 | sizeof(superobject), /* tp_basicsize */\r | |
6710 | 0, /* tp_itemsize */\r | |
6711 | /* methods */\r | |
6712 | super_dealloc, /* tp_dealloc */\r | |
6713 | 0, /* tp_print */\r | |
6714 | 0, /* tp_getattr */\r | |
6715 | 0, /* tp_setattr */\r | |
6716 | 0, /* tp_compare */\r | |
6717 | super_repr, /* tp_repr */\r | |
6718 | 0, /* tp_as_number */\r | |
6719 | 0, /* tp_as_sequence */\r | |
6720 | 0, /* tp_as_mapping */\r | |
6721 | 0, /* tp_hash */\r | |
6722 | 0, /* tp_call */\r | |
6723 | 0, /* tp_str */\r | |
6724 | super_getattro, /* tp_getattro */\r | |
6725 | 0, /* tp_setattro */\r | |
6726 | 0, /* tp_as_buffer */\r | |
6727 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |\r | |
6728 | Py_TPFLAGS_BASETYPE, /* tp_flags */\r | |
6729 | super_doc, /* tp_doc */\r | |
6730 | super_traverse, /* tp_traverse */\r | |
6731 | 0, /* tp_clear */\r | |
6732 | 0, /* tp_richcompare */\r | |
6733 | 0, /* tp_weaklistoffset */\r | |
6734 | 0, /* tp_iter */\r | |
6735 | 0, /* tp_iternext */\r | |
6736 | 0, /* tp_methods */\r | |
6737 | super_members, /* tp_members */\r | |
6738 | 0, /* tp_getset */\r | |
6739 | 0, /* tp_base */\r | |
6740 | 0, /* tp_dict */\r | |
6741 | super_descr_get, /* tp_descr_get */\r | |
6742 | 0, /* tp_descr_set */\r | |
6743 | 0, /* tp_dictoffset */\r | |
6744 | super_init, /* tp_init */\r | |
6745 | PyType_GenericAlloc, /* tp_alloc */\r | |
6746 | PyType_GenericNew, /* tp_new */\r | |
6747 | PyObject_GC_Del, /* tp_free */\r | |
6748 | };\r |