--- /dev/null
+/* Type object implementation */\r
+\r
+#include "Python.h"\r
+#include "structmember.h"\r
+\r
+#include <ctype.h>\r
+\r
+\r
+/* Support type attribute cache */\r
+\r
+/* The cache can keep references to the names alive for longer than\r
+ they normally would. This is why the maximum size is limited to\r
+ MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large\r
+ strings are used as attribute names. */\r
+#define MCACHE_MAX_ATTR_SIZE 100\r
+#define MCACHE_SIZE_EXP 10\r
+#define MCACHE_HASH(version, name_hash) \\r
+ (((unsigned int)(version) * (unsigned int)(name_hash)) \\r
+ >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP))\r
+#define MCACHE_HASH_METHOD(type, name) \\r
+ MCACHE_HASH((type)->tp_version_tag, \\r
+ ((PyStringObject *)(name))->ob_shash)\r
+#define MCACHE_CACHEABLE_NAME(name) \\r
+ PyString_CheckExact(name) && \\r
+ PyString_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE\r
+\r
+struct method_cache_entry {\r
+ unsigned int version;\r
+ PyObject *name; /* reference to exactly a str or None */\r
+ PyObject *value; /* borrowed */\r
+};\r
+\r
+static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];\r
+static unsigned int next_version_tag = 0;\r
+\r
+unsigned int\r
+PyType_ClearCache(void)\r
+{\r
+ Py_ssize_t i;\r
+ unsigned int cur_version_tag = next_version_tag - 1;\r
+\r
+ for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {\r
+ method_cache[i].version = 0;\r
+ Py_CLEAR(method_cache[i].name);\r
+ method_cache[i].value = NULL;\r
+ }\r
+ next_version_tag = 0;\r
+ /* mark all version tags as invalid */\r
+ PyType_Modified(&PyBaseObject_Type);\r
+ return cur_version_tag;\r
+}\r
+\r
+void\r
+PyType_Modified(PyTypeObject *type)\r
+{\r
+ /* Invalidate any cached data for the specified type and all\r
+ subclasses. This function is called after the base\r
+ classes, mro, or attributes of the type are altered.\r
+\r
+ Invariants:\r
+\r
+ - Py_TPFLAGS_VALID_VERSION_TAG is never set if\r
+ Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type\r
+ objects coming from non-recompiled extension modules)\r
+\r
+ - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,\r
+ it must first be set on all super types.\r
+\r
+ This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a\r
+ type (so it must first clear it on all subclasses). The\r
+ tp_version_tag value is meaningless unless this flag is set.\r
+ We don't assign new version tags eagerly, but only as\r
+ needed.\r
+ */\r
+ PyObject *raw, *ref;\r
+ Py_ssize_t i, n;\r
+\r
+ if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))\r
+ return;\r
+\r
+ raw = type->tp_subclasses;\r
+ if (raw != NULL) {\r
+ n = PyList_GET_SIZE(raw);\r
+ for (i = 0; i < n; i++) {\r
+ ref = PyList_GET_ITEM(raw, i);\r
+ ref = PyWeakref_GET_OBJECT(ref);\r
+ if (ref != Py_None) {\r
+ PyType_Modified((PyTypeObject *)ref);\r
+ }\r
+ }\r
+ }\r
+ type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;\r
+}\r
+\r
+static void\r
+type_mro_modified(PyTypeObject *type, PyObject *bases) {\r
+ /*\r
+ Check that all base classes or elements of the mro of type are\r
+ able to be cached. This function is called after the base\r
+ classes or mro of the type are altered.\r
+\r
+ Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type\r
+ inherits from an old-style class, either directly or if it\r
+ appears in the MRO of a new-style class. No support either for\r
+ custom MROs that include types that are not officially super\r
+ types.\r
+\r
+ Called from mro_internal, which will subsequently be called on\r
+ each subclass when their mro is recursively updated.\r
+ */\r
+ Py_ssize_t i, n;\r
+ int clear = 0;\r
+\r
+ if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))\r
+ return;\r
+\r
+ n = PyTuple_GET_SIZE(bases);\r
+ for (i = 0; i < n; i++) {\r
+ PyObject *b = PyTuple_GET_ITEM(bases, i);\r
+ PyTypeObject *cls;\r
+\r
+ if (!PyType_Check(b) ) {\r
+ clear = 1;\r
+ break;\r
+ }\r
+\r
+ cls = (PyTypeObject *)b;\r
+\r
+ if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) ||\r
+ !PyType_IsSubtype(type, cls)) {\r
+ clear = 1;\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (clear)\r
+ type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG|\r
+ Py_TPFLAGS_VALID_VERSION_TAG);\r
+}\r
+\r
+static int\r
+assign_version_tag(PyTypeObject *type)\r
+{\r
+ /* Ensure that the tp_version_tag is valid and set\r
+ Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this\r
+ must first be done on all super classes. Return 0 if this\r
+ cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.\r
+ */\r
+ Py_ssize_t i, n;\r
+ PyObject *bases;\r
+\r
+ if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))\r
+ return 1;\r
+ if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))\r
+ return 0;\r
+ if (!PyType_HasFeature(type, Py_TPFLAGS_READY))\r
+ return 0;\r
+\r
+ type->tp_version_tag = next_version_tag++;\r
+ /* for stress-testing: next_version_tag &= 0xFF; */\r
+\r
+ if (type->tp_version_tag == 0) {\r
+ /* wrap-around or just starting Python - clear the whole\r
+ cache by filling names with references to Py_None.\r
+ Values are also set to NULL for added protection, as they\r
+ are borrowed reference */\r
+ for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {\r
+ method_cache[i].value = NULL;\r
+ Py_XDECREF(method_cache[i].name);\r
+ method_cache[i].name = Py_None;\r
+ Py_INCREF(Py_None);\r
+ }\r
+ /* mark all version tags as invalid */\r
+ PyType_Modified(&PyBaseObject_Type);\r
+ return 1;\r
+ }\r
+ bases = type->tp_bases;\r
+ n = PyTuple_GET_SIZE(bases);\r
+ for (i = 0; i < n; i++) {\r
+ PyObject *b = PyTuple_GET_ITEM(bases, i);\r
+ assert(PyType_Check(b));\r
+ if (!assign_version_tag((PyTypeObject *)b))\r
+ return 0;\r
+ }\r
+ type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;\r
+ return 1;\r
+}\r
+\r
+\r
+static PyMemberDef type_members[] = {\r
+ {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY},\r
+ {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY},\r
+ {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},\r
+ {"__weakrefoffset__", T_LONG,\r
+ offsetof(PyTypeObject, tp_weaklistoffset), READONLY},\r
+ {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},\r
+ {"__dictoffset__", T_LONG,\r
+ offsetof(PyTypeObject, tp_dictoffset), READONLY},\r
+ {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},\r
+ {0}\r
+};\r
+\r
+static PyObject *\r
+type_name(PyTypeObject *type, void *context)\r
+{\r
+ const char *s;\r
+\r
+ if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {\r
+ PyHeapTypeObject* et = (PyHeapTypeObject*)type;\r
+\r
+ Py_INCREF(et->ht_name);\r
+ return et->ht_name;\r
+ }\r
+ else {\r
+ s = strrchr(type->tp_name, '.');\r
+ if (s == NULL)\r
+ s = type->tp_name;\r
+ else\r
+ s++;\r
+ return PyString_FromString(s);\r
+ }\r
+}\r
+\r
+static int\r
+type_set_name(PyTypeObject *type, PyObject *value, void *context)\r
+{\r
+ PyHeapTypeObject* et;\r
+ PyObject *tmp;\r
+\r
+ if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "can't set %s.__name__", type->tp_name);\r
+ return -1;\r
+ }\r
+ if (!value) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "can't delete %s.__name__", type->tp_name);\r
+ return -1;\r
+ }\r
+ if (!PyString_Check(value)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "can only assign string to %s.__name__, not '%s'",\r
+ type->tp_name, Py_TYPE(value)->tp_name);\r
+ return -1;\r
+ }\r
+ if (strlen(PyString_AS_STRING(value))\r
+ != (size_t)PyString_GET_SIZE(value)) {\r
+ PyErr_Format(PyExc_ValueError,\r
+ "__name__ must not contain null bytes");\r
+ return -1;\r
+ }\r
+\r
+ et = (PyHeapTypeObject*)type;\r
+\r
+ Py_INCREF(value);\r
+\r
+ /* Wait until et is a sane state before Py_DECREF'ing the old et->ht_name\r
+ value. (Bug #16447.) */\r
+ tmp = et->ht_name;\r
+ et->ht_name = value;\r
+\r
+ type->tp_name = PyString_AS_STRING(value);\r
+ Py_DECREF(tmp);\r
+\r
+ return 0;\r
+}\r
+\r
+static PyObject *\r
+type_module(PyTypeObject *type, void *context)\r
+{\r
+ PyObject *mod;\r
+ char *s;\r
+\r
+ if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {\r
+ mod = PyDict_GetItemString(type->tp_dict, "__module__");\r
+ if (!mod) {\r
+ PyErr_Format(PyExc_AttributeError, "__module__");\r
+ return 0;\r
+ }\r
+ Py_XINCREF(mod);\r
+ return mod;\r
+ }\r
+ else {\r
+ s = strrchr(type->tp_name, '.');\r
+ if (s != NULL)\r
+ return PyString_FromStringAndSize(\r
+ type->tp_name, (Py_ssize_t)(s - type->tp_name));\r
+ return PyString_FromString("__builtin__");\r
+ }\r
+}\r
+\r
+static int\r
+type_set_module(PyTypeObject *type, PyObject *value, void *context)\r
+{\r
+ if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "can't set %s.__module__", type->tp_name);\r
+ return -1;\r
+ }\r
+ if (!value) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "can't delete %s.__module__", type->tp_name);\r
+ return -1;\r
+ }\r
+\r
+ PyType_Modified(type);\r
+\r
+ return PyDict_SetItemString(type->tp_dict, "__module__", value);\r
+}\r
+\r
+static PyObject *\r
+type_abstractmethods(PyTypeObject *type, void *context)\r
+{\r
+ PyObject *mod = NULL;\r
+ /* type itself has an __abstractmethods__ descriptor (this). Don't return\r
+ that. */\r
+ if (type != &PyType_Type)\r
+ mod = PyDict_GetItemString(type->tp_dict, "__abstractmethods__");\r
+ if (!mod) {\r
+ PyErr_SetString(PyExc_AttributeError, "__abstractmethods__");\r
+ return NULL;\r
+ }\r
+ Py_XINCREF(mod);\r
+ return mod;\r
+}\r
+\r
+static int\r
+type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)\r
+{\r
+ /* __abstractmethods__ should only be set once on a type, in\r
+ abc.ABCMeta.__new__, so this function doesn't do anything\r
+ special to update subclasses.\r
+ */\r
+ int abstract, res;\r
+ if (value != NULL) {\r
+ abstract = PyObject_IsTrue(value);\r
+ if (abstract < 0)\r
+ return -1;\r
+ res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value);\r
+ }\r
+ else {\r
+ abstract = 0;\r
+ res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__");\r
+ if (res && PyErr_ExceptionMatches(PyExc_KeyError)) {\r
+ PyErr_SetString(PyExc_AttributeError, "__abstractmethods__");\r
+ return -1;\r
+ }\r
+ }\r
+ if (res == 0) {\r
+ PyType_Modified(type);\r
+ if (abstract)\r
+ type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;\r
+ else\r
+ type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;\r
+ }\r
+ return res;\r
+}\r
+\r
+static PyObject *\r
+type_get_bases(PyTypeObject *type, void *context)\r
+{\r
+ Py_INCREF(type->tp_bases);\r
+ return type->tp_bases;\r
+}\r
+\r
+static PyTypeObject *best_base(PyObject *);\r
+static int mro_internal(PyTypeObject *);\r
+static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);\r
+static int add_subclass(PyTypeObject*, PyTypeObject*);\r
+static void remove_subclass(PyTypeObject *, PyTypeObject *);\r
+static void update_all_slots(PyTypeObject *);\r
+\r
+typedef int (*update_callback)(PyTypeObject *, void *);\r
+static int update_subclasses(PyTypeObject *type, PyObject *name,\r
+ update_callback callback, void *data);\r
+static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,\r
+ update_callback callback, void *data);\r
+\r
+static int\r
+mro_subclasses(PyTypeObject *type, PyObject* temp)\r
+{\r
+ PyTypeObject *subclass;\r
+ PyObject *ref, *subclasses, *old_mro;\r
+ Py_ssize_t i, n;\r
+\r
+ subclasses = type->tp_subclasses;\r
+ if (subclasses == NULL)\r
+ return 0;\r
+ assert(PyList_Check(subclasses));\r
+ n = PyList_GET_SIZE(subclasses);\r
+ for (i = 0; i < n; i++) {\r
+ ref = PyList_GET_ITEM(subclasses, i);\r
+ assert(PyWeakref_CheckRef(ref));\r
+ subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);\r
+ assert(subclass != NULL);\r
+ if ((PyObject *)subclass == Py_None)\r
+ continue;\r
+ assert(PyType_Check(subclass));\r
+ old_mro = subclass->tp_mro;\r
+ if (mro_internal(subclass) < 0) {\r
+ subclass->tp_mro = old_mro;\r
+ return -1;\r
+ }\r
+ else {\r
+ PyObject* tuple;\r
+ tuple = PyTuple_Pack(2, subclass, old_mro);\r
+ Py_DECREF(old_mro);\r
+ if (!tuple)\r
+ return -1;\r
+ if (PyList_Append(temp, tuple) < 0)\r
+ return -1;\r
+ Py_DECREF(tuple);\r
+ }\r
+ if (mro_subclasses(subclass, temp) < 0)\r
+ return -1;\r
+ }\r
+ return 0;\r
+}\r
+\r
+static int\r
+type_set_bases(PyTypeObject *type, PyObject *value, void *context)\r
+{\r
+ Py_ssize_t i;\r
+ int r = 0;\r
+ PyObject *ob, *temp;\r
+ PyTypeObject *new_base, *old_base;\r
+ PyObject *old_bases, *old_mro;\r
+\r
+ if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "can't set %s.__bases__", type->tp_name);\r
+ return -1;\r
+ }\r
+ if (!value) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "can't delete %s.__bases__", type->tp_name);\r
+ return -1;\r
+ }\r
+ if (!PyTuple_Check(value)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "can only assign tuple to %s.__bases__, not %s",\r
+ type->tp_name, Py_TYPE(value)->tp_name);\r
+ return -1;\r
+ }\r
+ if (PyTuple_GET_SIZE(value) == 0) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "can only assign non-empty tuple to %s.__bases__, not ()",\r
+ type->tp_name);\r
+ return -1;\r
+ }\r
+ for (i = 0; i < PyTuple_GET_SIZE(value); i++) {\r
+ ob = PyTuple_GET_ITEM(value, i);\r
+ if (!PyClass_Check(ob) && !PyType_Check(ob)) {\r
+ PyErr_Format(\r
+ PyExc_TypeError,\r
+ "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",\r
+ type->tp_name, Py_TYPE(ob)->tp_name);\r
+ return -1;\r
+ }\r
+ if (PyType_Check(ob)) {\r
+ if (PyType_IsSubtype((PyTypeObject*)ob, type)) {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "a __bases__ item causes an inheritance cycle");\r
+ return -1;\r
+ }\r
+ }\r
+ }\r
+\r
+ new_base = best_base(value);\r
+\r
+ if (!new_base) {\r
+ return -1;\r
+ }\r
+\r
+ if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))\r
+ return -1;\r
+\r
+ Py_INCREF(new_base);\r
+ Py_INCREF(value);\r
+\r
+ old_bases = type->tp_bases;\r
+ old_base = type->tp_base;\r
+ old_mro = type->tp_mro;\r
+\r
+ type->tp_bases = value;\r
+ type->tp_base = new_base;\r
+\r
+ if (mro_internal(type) < 0) {\r
+ goto bail;\r
+ }\r
+\r
+ temp = PyList_New(0);\r
+ if (!temp)\r
+ goto bail;\r
+\r
+ r = mro_subclasses(type, temp);\r
+\r
+ if (r < 0) {\r
+ for (i = 0; i < PyList_Size(temp); i++) {\r
+ PyTypeObject* cls;\r
+ PyObject* mro;\r
+ PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),\r
+ "", 2, 2, &cls, &mro);\r
+ Py_INCREF(mro);\r
+ ob = cls->tp_mro;\r
+ cls->tp_mro = mro;\r
+ Py_DECREF(ob);\r
+ }\r
+ Py_DECREF(temp);\r
+ goto bail;\r
+ }\r
+\r
+ Py_DECREF(temp);\r
+\r
+ /* any base that was in __bases__ but now isn't, we\r
+ need to remove |type| from its tp_subclasses.\r
+ conversely, any class now in __bases__ that wasn't\r
+ needs to have |type| added to its subclasses. */\r
+\r
+ /* for now, sod that: just remove from all old_bases,\r
+ add to all new_bases */\r
+\r
+ for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {\r
+ ob = PyTuple_GET_ITEM(old_bases, i);\r
+ if (PyType_Check(ob)) {\r
+ remove_subclass(\r
+ (PyTypeObject*)ob, type);\r
+ }\r
+ }\r
+\r
+ for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {\r
+ ob = PyTuple_GET_ITEM(value, i);\r
+ if (PyType_Check(ob)) {\r
+ if (add_subclass((PyTypeObject*)ob, type) < 0)\r
+ r = -1;\r
+ }\r
+ }\r
+\r
+ update_all_slots(type);\r
+\r
+ Py_DECREF(old_bases);\r
+ Py_DECREF(old_base);\r
+ Py_DECREF(old_mro);\r
+\r
+ return r;\r
+\r
+ bail:\r
+ Py_DECREF(type->tp_bases);\r
+ Py_DECREF(type->tp_base);\r
+ if (type->tp_mro != old_mro) {\r
+ Py_DECREF(type->tp_mro);\r
+ }\r
+\r
+ type->tp_bases = old_bases;\r
+ type->tp_base = old_base;\r
+ type->tp_mro = old_mro;\r
+\r
+ return -1;\r
+}\r
+\r
+static PyObject *\r
+type_dict(PyTypeObject *type, void *context)\r
+{\r
+ if (type->tp_dict == NULL) {\r
+ Py_INCREF(Py_None);\r
+ return Py_None;\r
+ }\r
+ return PyDictProxy_New(type->tp_dict);\r
+}\r
+\r
+static PyObject *\r
+type_get_doc(PyTypeObject *type, void *context)\r
+{\r
+ PyObject *result;\r
+ if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)\r
+ return PyString_FromString(type->tp_doc);\r
+ result = PyDict_GetItemString(type->tp_dict, "__doc__");\r
+ if (result == NULL) {\r
+ result = Py_None;\r
+ Py_INCREF(result);\r
+ }\r
+ else if (Py_TYPE(result)->tp_descr_get) {\r
+ result = Py_TYPE(result)->tp_descr_get(result, NULL,\r
+ (PyObject *)type);\r
+ }\r
+ else {\r
+ Py_INCREF(result);\r
+ }\r
+ return result;\r
+}\r
+\r
+static PyObject *\r
+type___instancecheck__(PyObject *type, PyObject *inst)\r
+{\r
+ switch (_PyObject_RealIsInstance(inst, type)) {\r
+ case -1:\r
+ return NULL;\r
+ case 0:\r
+ Py_RETURN_FALSE;\r
+ default:\r
+ Py_RETURN_TRUE;\r
+ }\r
+}\r
+\r
+\r
+static PyObject *\r
+type___subclasscheck__(PyObject *type, PyObject *inst)\r
+{\r
+ switch (_PyObject_RealIsSubclass(inst, type)) {\r
+ case -1:\r
+ return NULL;\r
+ case 0:\r
+ Py_RETURN_FALSE;\r
+ default:\r
+ Py_RETURN_TRUE;\r
+ }\r
+}\r
+\r
+\r
+static PyGetSetDef type_getsets[] = {\r
+ {"__name__", (getter)type_name, (setter)type_set_name, NULL},\r
+ {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},\r
+ {"__module__", (getter)type_module, (setter)type_set_module, NULL},\r
+ {"__abstractmethods__", (getter)type_abstractmethods,\r
+ (setter)type_set_abstractmethods, NULL},\r
+ {"__dict__", (getter)type_dict, NULL, NULL},\r
+ {"__doc__", (getter)type_get_doc, NULL, NULL},\r
+ {0}\r
+};\r
+\r
+\r
+static PyObject*\r
+type_richcompare(PyObject *v, PyObject *w, int op)\r
+{\r
+ PyObject *result;\r
+ Py_uintptr_t vv, ww;\r
+ int c;\r
+\r
+ /* Make sure both arguments are types. */\r
+ if (!PyType_Check(v) || !PyType_Check(w) ||\r
+ /* If there is a __cmp__ method defined, let it be called instead\r
+ of our dumb function designed merely to warn. See bug\r
+ #7491. */\r
+ Py_TYPE(v)->tp_compare || Py_TYPE(w)->tp_compare) {\r
+ result = Py_NotImplemented;\r
+ goto out;\r
+ }\r
+\r
+ /* Py3K warning if comparison isn't == or != */\r
+ if (Py_Py3kWarningFlag && op != Py_EQ && op != Py_NE &&\r
+ PyErr_WarnEx(PyExc_DeprecationWarning,\r
+ "type inequality comparisons not supported "\r
+ "in 3.x", 1) < 0) {\r
+ return NULL;\r
+ }\r
+\r
+ /* Compare addresses */\r
+ vv = (Py_uintptr_t)v;\r
+ ww = (Py_uintptr_t)w;\r
+ switch (op) {\r
+ case Py_LT: c = vv < ww; break;\r
+ case Py_LE: c = vv <= ww; break;\r
+ case Py_EQ: c = vv == ww; break;\r
+ case Py_NE: c = vv != ww; break;\r
+ case Py_GT: c = vv > ww; break;\r
+ case Py_GE: c = vv >= ww; break;\r
+ default:\r
+ result = Py_NotImplemented;\r
+ goto out;\r
+ }\r
+ result = c ? Py_True : Py_False;\r
+\r
+ /* incref and return */\r
+ out:\r
+ Py_INCREF(result);\r
+ return result;\r
+}\r
+\r
+static PyObject *\r
+type_repr(PyTypeObject *type)\r
+{\r
+ PyObject *mod, *name, *rtn;\r
+ char *kind;\r
+\r
+ mod = type_module(type, NULL);\r
+ if (mod == NULL)\r
+ PyErr_Clear();\r
+ else if (!PyString_Check(mod)) {\r
+ Py_DECREF(mod);\r
+ mod = NULL;\r
+ }\r
+ name = type_name(type, NULL);\r
+ if (name == NULL) {\r
+ Py_XDECREF(mod);\r
+ return NULL;\r
+ }\r
+\r
+ if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)\r
+ kind = "class";\r
+ else\r
+ kind = "type";\r
+\r
+ if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {\r
+ rtn = PyString_FromFormat("<%s '%s.%s'>",\r
+ kind,\r
+ PyString_AS_STRING(mod),\r
+ PyString_AS_STRING(name));\r
+ }\r
+ else\r
+ rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);\r
+\r
+ Py_XDECREF(mod);\r
+ Py_DECREF(name);\r
+ return rtn;\r
+}\r
+\r
+static PyObject *\r
+type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
+{\r
+ PyObject *obj;\r
+\r
+ if (type->tp_new == NULL) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "cannot create '%.100s' instances",\r
+ type->tp_name);\r
+ return NULL;\r
+ }\r
+\r
+ obj = type->tp_new(type, args, kwds);\r
+ if (obj != NULL) {\r
+ /* Ugly exception: when the call was type(something),\r
+ don't call tp_init on the result. */\r
+ if (type == &PyType_Type &&\r
+ PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&\r
+ (kwds == NULL ||\r
+ (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))\r
+ return obj;\r
+ /* If the returned object is not an instance of type,\r
+ it won't be initialized. */\r
+ if (!PyType_IsSubtype(obj->ob_type, type))\r
+ return obj;\r
+ type = obj->ob_type;\r
+ if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&\r
+ type->tp_init != NULL &&\r
+ type->tp_init(obj, args, kwds) < 0) {\r
+ Py_DECREF(obj);\r
+ obj = NULL;\r
+ }\r
+ }\r
+ return obj;\r
+}\r
+\r
+PyObject *\r
+PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)\r
+{\r
+ PyObject *obj;\r
+ const size_t size = _PyObject_VAR_SIZE(type, nitems+1);\r
+ /* note that we need to add one, for the sentinel */\r
+\r
+ if (PyType_IS_GC(type))\r
+ obj = _PyObject_GC_Malloc(size);\r
+ else\r
+ obj = (PyObject *)PyObject_MALLOC(size);\r
+\r
+ if (obj == NULL)\r
+ return PyErr_NoMemory();\r
+\r
+ memset(obj, '\0', size);\r
+\r
+ if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)\r
+ Py_INCREF(type);\r
+\r
+ if (type->tp_itemsize == 0)\r
+ PyObject_INIT(obj, type);\r
+ else\r
+ (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);\r
+\r
+ if (PyType_IS_GC(type))\r
+ _PyObject_GC_TRACK(obj);\r
+ return obj;\r
+}\r
+\r
+PyObject *\r
+PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
+{\r
+ return type->tp_alloc(type, 0);\r
+}\r
+\r
+/* Helpers for subtyping */\r
+\r
+static int\r
+traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)\r
+{\r
+ Py_ssize_t i, n;\r
+ PyMemberDef *mp;\r
+\r
+ n = Py_SIZE(type);\r
+ mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);\r
+ for (i = 0; i < n; i++, mp++) {\r
+ if (mp->type == T_OBJECT_EX) {\r
+ char *addr = (char *)self + mp->offset;\r
+ PyObject *obj = *(PyObject **)addr;\r
+ if (obj != NULL) {\r
+ int err = visit(obj, arg);\r
+ if (err)\r
+ return err;\r
+ }\r
+ }\r
+ }\r
+ return 0;\r
+}\r
+\r
+static int\r
+subtype_traverse(PyObject *self, visitproc visit, void *arg)\r
+{\r
+ PyTypeObject *type, *base;\r
+ traverseproc basetraverse;\r
+\r
+ /* Find the nearest base with a different tp_traverse,\r
+ and traverse slots while we're at it */\r
+ type = Py_TYPE(self);\r
+ base = type;\r
+ while ((basetraverse = base->tp_traverse) == subtype_traverse) {\r
+ if (Py_SIZE(base)) {\r
+ int err = traverse_slots(base, self, visit, arg);\r
+ if (err)\r
+ return err;\r
+ }\r
+ base = base->tp_base;\r
+ assert(base);\r
+ }\r
+\r
+ if (type->tp_dictoffset != base->tp_dictoffset) {\r
+ PyObject **dictptr = _PyObject_GetDictPtr(self);\r
+ if (dictptr && *dictptr)\r
+ Py_VISIT(*dictptr);\r
+ }\r
+\r
+ if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)\r
+ /* For a heaptype, the instances count as references\r
+ to the type. Traverse the type so the collector\r
+ can find cycles involving this link. */\r
+ Py_VISIT(type);\r
+\r
+ if (basetraverse)\r
+ return basetraverse(self, visit, arg);\r
+ return 0;\r
+}\r
+\r
+static void\r
+clear_slots(PyTypeObject *type, PyObject *self)\r
+{\r
+ Py_ssize_t i, n;\r
+ PyMemberDef *mp;\r
+\r
+ n = Py_SIZE(type);\r
+ mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);\r
+ for (i = 0; i < n; i++, mp++) {\r
+ if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {\r
+ char *addr = (char *)self + mp->offset;\r
+ PyObject *obj = *(PyObject **)addr;\r
+ if (obj != NULL) {\r
+ *(PyObject **)addr = NULL;\r
+ Py_DECREF(obj);\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+static int\r
+subtype_clear(PyObject *self)\r
+{\r
+ PyTypeObject *type, *base;\r
+ inquiry baseclear;\r
+\r
+ /* Find the nearest base with a different tp_clear\r
+ and clear slots while we're at it */\r
+ type = Py_TYPE(self);\r
+ base = type;\r
+ while ((baseclear = base->tp_clear) == subtype_clear) {\r
+ if (Py_SIZE(base))\r
+ clear_slots(base, self);\r
+ base = base->tp_base;\r
+ assert(base);\r
+ }\r
+\r
+ /* Clear the instance dict (if any), to break cycles involving only\r
+ __dict__ slots (as in the case 'self.__dict__ is self'). */\r
+ if (type->tp_dictoffset != base->tp_dictoffset) {\r
+ PyObject **dictptr = _PyObject_GetDictPtr(self);\r
+ if (dictptr && *dictptr)\r
+ Py_CLEAR(*dictptr);\r
+ }\r
+\r
+ if (baseclear)\r
+ return baseclear(self);\r
+ return 0;\r
+}\r
+\r
+static void\r
+subtype_dealloc(PyObject *self)\r
+{\r
+ PyTypeObject *type, *base;\r
+ destructor basedealloc;\r
+ PyThreadState *tstate = PyThreadState_GET();\r
+\r
+ /* Extract the type; we expect it to be a heap type */\r
+ type = Py_TYPE(self);\r
+ assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);\r
+\r
+ /* Test whether the type has GC exactly once */\r
+\r
+ if (!PyType_IS_GC(type)) {\r
+ /* It's really rare to find a dynamic type that doesn't have\r
+ GC; it can only happen when deriving from 'object' and not\r
+ adding any slots or instance variables. This allows\r
+ certain simplifications: there's no need to call\r
+ clear_slots(), or DECREF the dict, or clear weakrefs. */\r
+\r
+ /* Maybe call finalizer; exit early if resurrected */\r
+ if (type->tp_del) {\r
+ type->tp_del(self);\r
+ if (self->ob_refcnt > 0)\r
+ return;\r
+ }\r
+\r
+ /* Find the nearest base with a different tp_dealloc */\r
+ base = type;\r
+ while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {\r
+ assert(Py_SIZE(base) == 0);\r
+ base = base->tp_base;\r
+ assert(base);\r
+ }\r
+\r
+ /* Extract the type again; tp_del may have changed it */\r
+ type = Py_TYPE(self);\r
+\r
+ /* Call the base tp_dealloc() */\r
+ assert(basedealloc);\r
+ basedealloc(self);\r
+\r
+ /* Can't reference self beyond this point */\r
+ Py_DECREF(type);\r
+\r
+ /* Done */\r
+ return;\r
+ }\r
+\r
+ /* We get here only if the type has GC */\r
+\r
+ /* UnTrack and re-Track around the trashcan macro, alas */\r
+ /* See explanation at end of function for full disclosure */\r
+ PyObject_GC_UnTrack(self);\r
+ ++_PyTrash_delete_nesting;\r
+ ++ tstate->trash_delete_nesting;\r
+ Py_TRASHCAN_SAFE_BEGIN(self);\r
+ --_PyTrash_delete_nesting;\r
+ -- tstate->trash_delete_nesting;\r
+ /* DO NOT restore GC tracking at this point. weakref callbacks\r
+ * (if any, and whether directly here or indirectly in something we\r
+ * call) may trigger GC, and if self is tracked at that point, it\r
+ * will look like trash to GC and GC will try to delete self again.\r
+ */\r
+\r
+ /* Find the nearest base with a different tp_dealloc */\r
+ base = type;\r
+ while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {\r
+ base = base->tp_base;\r
+ assert(base);\r
+ }\r
+\r
+ /* If we added a weaklist, we clear it. Do this *before* calling\r
+ the finalizer (__del__), clearing slots, or clearing the instance\r
+ dict. */\r
+\r
+ if (type->tp_weaklistoffset && !base->tp_weaklistoffset)\r
+ PyObject_ClearWeakRefs(self);\r
+\r
+ /* Maybe call finalizer; exit early if resurrected */\r
+ if (type->tp_del) {\r
+ _PyObject_GC_TRACK(self);\r
+ type->tp_del(self);\r
+ if (self->ob_refcnt > 0)\r
+ goto endlabel; /* resurrected */\r
+ else\r
+ _PyObject_GC_UNTRACK(self);\r
+ /* New weakrefs could be created during the finalizer call.\r
+ If this occurs, clear them out without calling their\r
+ finalizers since they might rely on part of the object\r
+ being finalized that has already been destroyed. */\r
+ if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {\r
+ /* Modeled after GET_WEAKREFS_LISTPTR() */\r
+ PyWeakReference **list = (PyWeakReference **) \\r
+ PyObject_GET_WEAKREFS_LISTPTR(self);\r
+ while (*list)\r
+ _PyWeakref_ClearRef(*list);\r
+ }\r
+ }\r
+\r
+ /* Clear slots up to the nearest base with a different tp_dealloc */\r
+ base = type;\r
+ while (base->tp_dealloc == subtype_dealloc) {\r
+ if (Py_SIZE(base))\r
+ clear_slots(base, self);\r
+ base = base->tp_base;\r
+ assert(base);\r
+ }\r
+\r
+ /* If we added a dict, DECREF it */\r
+ if (type->tp_dictoffset && !base->tp_dictoffset) {\r
+ PyObject **dictptr = _PyObject_GetDictPtr(self);\r
+ if (dictptr != NULL) {\r
+ PyObject *dict = *dictptr;\r
+ if (dict != NULL) {\r
+ Py_DECREF(dict);\r
+ *dictptr = NULL;\r
+ }\r
+ }\r
+ }\r
+\r
+ /* Extract the type again; tp_del may have changed it */\r
+ type = Py_TYPE(self);\r
+\r
+ /* Call the base tp_dealloc(); first retrack self if\r
+ * basedealloc knows about gc.\r
+ */\r
+ if (PyType_IS_GC(base))\r
+ _PyObject_GC_TRACK(self);\r
+ assert(basedealloc);\r
+ basedealloc(self);\r
+\r
+ /* Can't reference self beyond this point */\r
+ Py_DECREF(type);\r
+\r
+ endlabel:\r
+ ++_PyTrash_delete_nesting;\r
+ ++ tstate->trash_delete_nesting;\r
+ Py_TRASHCAN_SAFE_END(self);\r
+ --_PyTrash_delete_nesting;\r
+ -- tstate->trash_delete_nesting;\r
+\r
+ /* Explanation of the weirdness around the trashcan macros:\r
+\r
+ Q. What do the trashcan macros do?\r
+\r
+ A. Read the comment titled "Trashcan mechanism" in object.h.\r
+ For one, this explains why there must be a call to GC-untrack\r
+ before the trashcan begin macro. Without understanding the\r
+ trashcan code, the answers to the following questions don't make\r
+ sense.\r
+\r
+ Q. Why do we GC-untrack before the trashcan and then immediately\r
+ GC-track again afterward?\r
+\r
+ A. In the case that the base class is GC-aware, the base class\r
+ probably GC-untracks the object. If it does that using the\r
+ UNTRACK macro, this will crash when the object is already\r
+ untracked. Because we don't know what the base class does, the\r
+ only safe thing is to make sure the object is tracked when we\r
+ call the base class dealloc. But... The trashcan begin macro\r
+ requires that the object is *untracked* before it is called. So\r
+ the dance becomes:\r
+\r
+ GC untrack\r
+ trashcan begin\r
+ GC track\r
+\r
+ Q. Why did the last question say "immediately GC-track again"?\r
+ It's nowhere near immediately.\r
+\r
+ A. Because the code *used* to re-track immediately. Bad Idea.\r
+ self has a refcount of 0, and if gc ever gets its hands on it\r
+ (which can happen if any weakref callback gets invoked), it\r
+ looks like trash to gc too, and gc also tries to delete self\r
+ then. But we're already deleting self. Double deallocation is\r
+ a subtle disaster.\r
+\r
+ Q. Why the bizarre (net-zero) manipulation of\r
+ _PyTrash_delete_nesting around the trashcan macros?\r
+\r
+ A. Some base classes (e.g. list) also use the trashcan mechanism.\r
+ The following scenario used to be possible:\r
+\r
+ - suppose the trashcan level is one below the trashcan limit\r
+\r
+ - subtype_dealloc() is called\r
+\r
+ - the trashcan limit is not yet reached, so the trashcan level\r
+ is incremented and the code between trashcan begin and end is\r
+ executed\r
+\r
+ - this destroys much of the object's contents, including its\r
+ slots and __dict__\r
+\r
+ - basedealloc() is called; this is really list_dealloc(), or\r
+ some other type which also uses the trashcan macros\r
+\r
+ - the trashcan limit is now reached, so the object is put on the\r
+ trashcan's to-be-deleted-later list\r
+\r
+ - basedealloc() returns\r
+\r
+ - subtype_dealloc() decrefs the object's type\r
+\r
+ - subtype_dealloc() returns\r
+\r
+ - later, the trashcan code starts deleting the objects from its\r
+ to-be-deleted-later list\r
+\r
+ - subtype_dealloc() is called *AGAIN* for the same object\r
+\r
+ - at the very least (if the destroyed slots and __dict__ don't\r
+ cause problems) the object's type gets decref'ed a second\r
+ time, which is *BAD*!!!\r
+\r
+ The remedy is to make sure that if the code between trashcan\r
+ begin and end in subtype_dealloc() is called, the code between\r
+ trashcan begin and end in basedealloc() will also be called.\r
+ This is done by decrementing the level after passing into the\r
+ trashcan block, and incrementing it just before leaving the\r
+ block.\r
+\r
+ But now it's possible that a chain of objects consisting solely\r
+ of objects whose deallocator is subtype_dealloc() will defeat\r
+ the trashcan mechanism completely: the decremented level means\r
+ that the effective level never reaches the limit. Therefore, we\r
+ *increment* the level *before* entering the trashcan block, and\r
+ matchingly decrement it after leaving. This means the trashcan\r
+ code will trigger a little early, but that's no big deal.\r
+\r
+ Q. Are there any live examples of code in need of all this\r
+ complexity?\r
+\r
+ A. Yes. See SF bug 668433 for code that crashed (when Python was\r
+ compiled in debug mode) before the trashcan level manipulations\r
+ were added. For more discussion, see SF patches 581742, 575073\r
+ and bug 574207.\r
+ */\r
+}\r
+\r
+static PyTypeObject *solid_base(PyTypeObject *type);\r
+\r
+/* type test with subclassing support */\r
+\r
+int\r
+PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)\r
+{\r
+ PyObject *mro;\r
+\r
+ if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))\r
+ return b == a || b == &PyBaseObject_Type;\r
+\r
+ mro = a->tp_mro;\r
+ if (mro != NULL) {\r
+ /* Deal with multiple inheritance without recursion\r
+ by walking the MRO tuple */\r
+ Py_ssize_t i, n;\r
+ assert(PyTuple_Check(mro));\r
+ n = PyTuple_GET_SIZE(mro);\r
+ for (i = 0; i < n; i++) {\r
+ if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)\r
+ return 1;\r
+ }\r
+ return 0;\r
+ }\r
+ else {\r
+ /* a is not completely initilized yet; follow tp_base */\r
+ do {\r
+ if (a == b)\r
+ return 1;\r
+ a = a->tp_base;\r
+ } while (a != NULL);\r
+ return b == &PyBaseObject_Type;\r
+ }\r
+}\r
+\r
+/* Internal routines to do a method lookup in the type\r
+ without looking in the instance dictionary\r
+ (so we can't use PyObject_GetAttr) but still binding\r
+ it to the instance. The arguments are the object,\r
+ the method name as a C string, and the address of a\r
+ static variable used to cache the interned Python string.\r
+\r
+ Two variants:\r
+\r
+ - lookup_maybe() returns NULL without raising an exception\r
+ when the _PyType_Lookup() call fails;\r
+\r
+ - lookup_method() always raises an exception upon errors.\r
+\r
+ - _PyObject_LookupSpecial() exported for the benefit of other places.\r
+*/\r
+\r
+static PyObject *\r
+lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)\r
+{\r
+ PyObject *res;\r
+\r
+ if (*attrobj == NULL) {\r
+ *attrobj = PyString_InternFromString(attrstr);\r
+ if (*attrobj == NULL)\r
+ return NULL;\r
+ }\r
+ res = _PyType_Lookup(Py_TYPE(self), *attrobj);\r
+ if (res != NULL) {\r
+ descrgetfunc f;\r
+ if ((f = Py_TYPE(res)->tp_descr_get) == NULL)\r
+ Py_INCREF(res);\r
+ else\r
+ res = f(res, self, (PyObject *)(Py_TYPE(self)));\r
+ }\r
+ return res;\r
+}\r
+\r
+static PyObject *\r
+lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)\r
+{\r
+ PyObject *res = lookup_maybe(self, attrstr, attrobj);\r
+ if (res == NULL && !PyErr_Occurred())\r
+ PyErr_SetObject(PyExc_AttributeError, *attrobj);\r
+ return res;\r
+}\r
+\r
+PyObject *\r
+_PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj)\r
+{\r
+ assert(!PyInstance_Check(self));\r
+ return lookup_maybe(self, attrstr, attrobj);\r
+}\r
+\r
+/* A variation of PyObject_CallMethod that uses lookup_method()\r
+ instead of PyObject_GetAttrString(). This uses the same convention\r
+ as lookup_method to cache the interned name string object. */\r
+\r
+static PyObject *\r
+call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)\r
+{\r
+ va_list va;\r
+ PyObject *args, *func = 0, *retval;\r
+ va_start(va, format);\r
+\r
+ func = lookup_maybe(o, name, nameobj);\r
+ if (func == NULL) {\r
+ va_end(va);\r
+ if (!PyErr_Occurred())\r
+ PyErr_SetObject(PyExc_AttributeError, *nameobj);\r
+ return NULL;\r
+ }\r
+\r
+ if (format && *format)\r
+ args = Py_VaBuildValue(format, va);\r
+ else\r
+ args = PyTuple_New(0);\r
+\r
+ va_end(va);\r
+\r
+ if (args == NULL)\r
+ return NULL;\r
+\r
+ assert(PyTuple_Check(args));\r
+ retval = PyObject_Call(func, args, NULL);\r
+\r
+ Py_DECREF(args);\r
+ Py_DECREF(func);\r
+\r
+ return retval;\r
+}\r
+\r
+/* Clone of call_method() that returns NotImplemented when the lookup fails. */\r
+\r
+static PyObject *\r
+call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)\r
+{\r
+ va_list va;\r
+ PyObject *args, *func = 0, *retval;\r
+ va_start(va, format);\r
+\r
+ func = lookup_maybe(o, name, nameobj);\r
+ if (func == NULL) {\r
+ va_end(va);\r
+ if (!PyErr_Occurred()) {\r
+ Py_INCREF(Py_NotImplemented);\r
+ return Py_NotImplemented;\r
+ }\r
+ return NULL;\r
+ }\r
+\r
+ if (format && *format)\r
+ args = Py_VaBuildValue(format, va);\r
+ else\r
+ args = PyTuple_New(0);\r
+\r
+ va_end(va);\r
+\r
+ if (args == NULL)\r
+ return NULL;\r
+\r
+ assert(PyTuple_Check(args));\r
+ retval = PyObject_Call(func, args, NULL);\r
+\r
+ Py_DECREF(args);\r
+ Py_DECREF(func);\r
+\r
+ return retval;\r
+}\r
+\r
+static int\r
+fill_classic_mro(PyObject *mro, PyObject *cls)\r
+{\r
+ PyObject *bases, *base;\r
+ Py_ssize_t i, n;\r
+\r
+ assert(PyList_Check(mro));\r
+ assert(PyClass_Check(cls));\r
+ i = PySequence_Contains(mro, cls);\r
+ if (i < 0)\r
+ return -1;\r
+ if (!i) {\r
+ if (PyList_Append(mro, cls) < 0)\r
+ return -1;\r
+ }\r
+ bases = ((PyClassObject *)cls)->cl_bases;\r
+ assert(bases && PyTuple_Check(bases));\r
+ n = PyTuple_GET_SIZE(bases);\r
+ for (i = 0; i < n; i++) {\r
+ base = PyTuple_GET_ITEM(bases, i);\r
+ if (fill_classic_mro(mro, base) < 0)\r
+ return -1;\r
+ }\r
+ return 0;\r
+}\r
+\r
+static PyObject *\r
+classic_mro(PyObject *cls)\r
+{\r
+ PyObject *mro;\r
+\r
+ assert(PyClass_Check(cls));\r
+ mro = PyList_New(0);\r
+ if (mro != NULL) {\r
+ if (fill_classic_mro(mro, cls) == 0)\r
+ return mro;\r
+ Py_DECREF(mro);\r
+ }\r
+ return NULL;\r
+}\r
+\r
+/*\r
+ Method resolution order algorithm C3 described in\r
+ "A Monotonic Superclass Linearization for Dylan",\r
+ by Kim Barrett, Bob Cassel, Paul Haahr,\r
+ David A. Moon, Keith Playford, and P. Tucker Withington.\r
+ (OOPSLA 1996)\r
+\r
+ Some notes about the rules implied by C3:\r
+\r
+ No duplicate bases.\r
+ It isn't legal to repeat a class in a list of base classes.\r
+\r
+ The next three properties are the 3 constraints in "C3".\r
+\r
+ Local precendece order.\r
+ If A precedes B in C's MRO, then A will precede B in the MRO of all\r
+ subclasses of C.\r
+\r
+ Monotonicity.\r
+ The MRO of a class must be an extension without reordering of the\r
+ MRO of each of its superclasses.\r
+\r
+ Extended Precedence Graph (EPG).\r
+ Linearization is consistent if there is a path in the EPG from\r
+ each class to all its successors in the linearization. See\r
+ the paper for definition of EPG.\r
+ */\r
+\r
+static int\r
+tail_contains(PyObject *list, int whence, PyObject *o) {\r
+ Py_ssize_t j, size;\r
+ size = PyList_GET_SIZE(list);\r
+\r
+ for (j = whence+1; j < size; j++) {\r
+ if (PyList_GET_ITEM(list, j) == o)\r
+ return 1;\r
+ }\r
+ return 0;\r
+}\r
+\r
+static PyObject *\r
+class_name(PyObject *cls)\r
+{\r
+ PyObject *name = PyObject_GetAttrString(cls, "__name__");\r
+ if (name == NULL) {\r
+ PyErr_Clear();\r
+ Py_XDECREF(name);\r
+ name = PyObject_Repr(cls);\r
+ }\r
+ if (name == NULL)\r
+ return NULL;\r
+ if (!PyString_Check(name)) {\r
+ Py_DECREF(name);\r
+ return NULL;\r
+ }\r
+ return name;\r
+}\r
+\r
+static int\r
+check_duplicates(PyObject *list)\r
+{\r
+ Py_ssize_t i, j, n;\r
+ /* Let's use a quadratic time algorithm,\r
+ assuming that the bases lists is short.\r
+ */\r
+ n = PyList_GET_SIZE(list);\r
+ for (i = 0; i < n; i++) {\r
+ PyObject *o = PyList_GET_ITEM(list, i);\r
+ for (j = i + 1; j < n; j++) {\r
+ if (PyList_GET_ITEM(list, j) == o) {\r
+ o = class_name(o);\r
+ PyErr_Format(PyExc_TypeError,\r
+ "duplicate base class %s",\r
+ o ? PyString_AS_STRING(o) : "?");\r
+ Py_XDECREF(o);\r
+ return -1;\r
+ }\r
+ }\r
+ }\r
+ return 0;\r
+}\r
+\r
+/* Raise a TypeError for an MRO order disagreement.\r
+\r
+ It's hard to produce a good error message. In the absence of better\r
+ insight into error reporting, report the classes that were candidates\r
+ to be put next into the MRO. There is some conflict between the\r
+ order in which they should be put in the MRO, but it's hard to\r
+ diagnose what constraint can't be satisfied.\r
+*/\r
+\r
+static void\r
+set_mro_error(PyObject *to_merge, int *remain)\r
+{\r
+ Py_ssize_t i, n, off, to_merge_size;\r
+ char buf[1000];\r
+ PyObject *k, *v;\r
+ PyObject *set = PyDict_New();\r
+ if (!set) return;\r
+\r
+ to_merge_size = PyList_GET_SIZE(to_merge);\r
+ for (i = 0; i < to_merge_size; i++) {\r
+ PyObject *L = PyList_GET_ITEM(to_merge, i);\r
+ if (remain[i] < PyList_GET_SIZE(L)) {\r
+ PyObject *c = PyList_GET_ITEM(L, remain[i]);\r
+ if (PyDict_SetItem(set, c, Py_None) < 0) {\r
+ Py_DECREF(set);\r
+ return;\r
+ }\r
+ }\r
+ }\r
+ n = PyDict_Size(set);\r
+\r
+ off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \\r
+consistent method resolution\norder (MRO) for bases");\r
+ i = 0;\r
+ while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {\r
+ PyObject *name = class_name(k);\r
+ off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",\r
+ name ? PyString_AS_STRING(name) : "?");\r
+ Py_XDECREF(name);\r
+ if (--n && (size_t)(off+1) < sizeof(buf)) {\r
+ buf[off++] = ',';\r
+ buf[off] = '\0';\r
+ }\r
+ }\r
+ PyErr_SetString(PyExc_TypeError, buf);\r
+ Py_DECREF(set);\r
+}\r
+\r
+static int\r
+pmerge(PyObject *acc, PyObject* to_merge) {\r
+ Py_ssize_t i, j, to_merge_size, empty_cnt;\r
+ int *remain;\r
+ int ok;\r
+\r
+ to_merge_size = PyList_GET_SIZE(to_merge);\r
+\r
+ /* remain stores an index into each sublist of to_merge.\r
+ remain[i] is the index of the next base in to_merge[i]\r
+ that is not included in acc.\r
+ */\r
+ remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);\r
+ if (remain == NULL)\r
+ return -1;\r
+ for (i = 0; i < to_merge_size; i++)\r
+ remain[i] = 0;\r
+\r
+ again:\r
+ empty_cnt = 0;\r
+ for (i = 0; i < to_merge_size; i++) {\r
+ PyObject *candidate;\r
+\r
+ PyObject *cur_list = PyList_GET_ITEM(to_merge, i);\r
+\r
+ if (remain[i] >= PyList_GET_SIZE(cur_list)) {\r
+ empty_cnt++;\r
+ continue;\r
+ }\r
+\r
+ /* Choose next candidate for MRO.\r
+\r
+ The input sequences alone can determine the choice.\r
+ If not, choose the class which appears in the MRO\r
+ of the earliest direct superclass of the new class.\r
+ */\r
+\r
+ candidate = PyList_GET_ITEM(cur_list, remain[i]);\r
+ for (j = 0; j < to_merge_size; j++) {\r
+ PyObject *j_lst = PyList_GET_ITEM(to_merge, j);\r
+ if (tail_contains(j_lst, remain[j], candidate)) {\r
+ goto skip; /* continue outer loop */\r
+ }\r
+ }\r
+ ok = PyList_Append(acc, candidate);\r
+ if (ok < 0) {\r
+ PyMem_Free(remain);\r
+ return -1;\r
+ }\r
+ for (j = 0; j < to_merge_size; j++) {\r
+ PyObject *j_lst = PyList_GET_ITEM(to_merge, j);\r
+ if (remain[j] < PyList_GET_SIZE(j_lst) &&\r
+ PyList_GET_ITEM(j_lst, remain[j]) == candidate) {\r
+ remain[j]++;\r
+ }\r
+ }\r
+ goto again;\r
+ skip: ;\r
+ }\r
+\r
+ if (empty_cnt == to_merge_size) {\r
+ PyMem_FREE(remain);\r
+ return 0;\r
+ }\r
+ set_mro_error(to_merge, remain);\r
+ PyMem_FREE(remain);\r
+ return -1;\r
+}\r
+\r
+static PyObject *\r
+mro_implementation(PyTypeObject *type)\r
+{\r
+ Py_ssize_t i, n;\r
+ int ok;\r
+ PyObject *bases, *result;\r
+ PyObject *to_merge, *bases_aslist;\r
+\r
+ if (type->tp_dict == NULL) {\r
+ if (PyType_Ready(type) < 0)\r
+ return NULL;\r
+ }\r
+\r
+ /* Find a superclass linearization that honors the constraints\r
+ of the explicit lists of bases and the constraints implied by\r
+ each base class.\r
+\r
+ to_merge is a list of lists, where each list is a superclass\r
+ linearization implied by a base class. The last element of\r
+ to_merge is the declared list of bases.\r
+ */\r
+\r
+ bases = type->tp_bases;\r
+ n = PyTuple_GET_SIZE(bases);\r
+\r
+ to_merge = PyList_New(n+1);\r
+ if (to_merge == NULL)\r
+ return NULL;\r
+\r
+ for (i = 0; i < n; i++) {\r
+ PyObject *base = PyTuple_GET_ITEM(bases, i);\r
+ PyObject *parentMRO;\r
+ if (PyType_Check(base))\r
+ parentMRO = PySequence_List(\r
+ ((PyTypeObject*)base)->tp_mro);\r
+ else\r
+ parentMRO = classic_mro(base);\r
+ if (parentMRO == NULL) {\r
+ Py_DECREF(to_merge);\r
+ return NULL;\r
+ }\r
+\r
+ PyList_SET_ITEM(to_merge, i, parentMRO);\r
+ }\r
+\r
+ bases_aslist = PySequence_List(bases);\r
+ if (bases_aslist == NULL) {\r
+ Py_DECREF(to_merge);\r
+ return NULL;\r
+ }\r
+ /* This is just a basic sanity check. */\r
+ if (check_duplicates(bases_aslist) < 0) {\r
+ Py_DECREF(to_merge);\r
+ Py_DECREF(bases_aslist);\r
+ return NULL;\r
+ }\r
+ PyList_SET_ITEM(to_merge, n, bases_aslist);\r
+\r
+ result = Py_BuildValue("[O]", (PyObject *)type);\r
+ if (result == NULL) {\r
+ Py_DECREF(to_merge);\r
+ return NULL;\r
+ }\r
+\r
+ ok = pmerge(result, to_merge);\r
+ Py_DECREF(to_merge);\r
+ if (ok < 0) {\r
+ Py_DECREF(result);\r
+ return NULL;\r
+ }\r
+\r
+ return result;\r
+}\r
+\r
+static PyObject *\r
+mro_external(PyObject *self)\r
+{\r
+ PyTypeObject *type = (PyTypeObject *)self;\r
+\r
+ return mro_implementation(type);\r
+}\r
+\r
+static int\r
+mro_internal(PyTypeObject *type)\r
+{\r
+ PyObject *mro, *result, *tuple;\r
+ int checkit = 0;\r
+\r
+ if (Py_TYPE(type) == &PyType_Type) {\r
+ result = mro_implementation(type);\r
+ }\r
+ else {\r
+ static PyObject *mro_str;\r
+ checkit = 1;\r
+ mro = lookup_method((PyObject *)type, "mro", &mro_str);\r
+ if (mro == NULL)\r
+ return -1;\r
+ result = PyObject_CallObject(mro, NULL);\r
+ Py_DECREF(mro);\r
+ }\r
+ if (result == NULL)\r
+ return -1;\r
+ tuple = PySequence_Tuple(result);\r
+ Py_DECREF(result);\r
+ if (tuple == NULL)\r
+ return -1;\r
+ if (checkit) {\r
+ Py_ssize_t i, len;\r
+ PyObject *cls;\r
+ PyTypeObject *solid;\r
+\r
+ solid = solid_base(type);\r
+\r
+ len = PyTuple_GET_SIZE(tuple);\r
+\r
+ for (i = 0; i < len; i++) {\r
+ PyTypeObject *t;\r
+ cls = PyTuple_GET_ITEM(tuple, i);\r
+ if (PyClass_Check(cls))\r
+ continue;\r
+ else if (!PyType_Check(cls)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "mro() returned a non-class ('%.500s')",\r
+ Py_TYPE(cls)->tp_name);\r
+ Py_DECREF(tuple);\r
+ return -1;\r
+ }\r
+ t = (PyTypeObject*)cls;\r
+ if (!PyType_IsSubtype(solid, solid_base(t))) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "mro() returned base with unsuitable layout ('%.500s')",\r
+ t->tp_name);\r
+ Py_DECREF(tuple);\r
+ return -1;\r
+ }\r
+ }\r
+ }\r
+ type->tp_mro = tuple;\r
+\r
+ type_mro_modified(type, type->tp_mro);\r
+ /* corner case: the old-style super class might have been hidden\r
+ from the custom MRO */\r
+ type_mro_modified(type, type->tp_bases);\r
+\r
+ PyType_Modified(type);\r
+\r
+ return 0;\r
+}\r
+\r
+\r
+/* Calculate the best base amongst multiple base classes.\r
+ This is the first one that's on the path to the "solid base". */\r
+\r
+static PyTypeObject *\r
+best_base(PyObject *bases)\r
+{\r
+ Py_ssize_t i, n;\r
+ PyTypeObject *base, *winner, *candidate, *base_i;\r
+ PyObject *base_proto;\r
+\r
+ assert(PyTuple_Check(bases));\r
+ n = PyTuple_GET_SIZE(bases);\r
+ assert(n > 0);\r
+ base = NULL;\r
+ winner = NULL;\r
+ for (i = 0; i < n; i++) {\r
+ base_proto = PyTuple_GET_ITEM(bases, i);\r
+ if (PyClass_Check(base_proto))\r
+ continue;\r
+ if (!PyType_Check(base_proto)) {\r
+ PyErr_SetString(\r
+ PyExc_TypeError,\r
+ "bases must be types");\r
+ return NULL;\r
+ }\r
+ base_i = (PyTypeObject *)base_proto;\r
+ if (base_i->tp_dict == NULL) {\r
+ if (PyType_Ready(base_i) < 0)\r
+ return NULL;\r
+ }\r
+ candidate = solid_base(base_i);\r
+ if (winner == NULL) {\r
+ winner = candidate;\r
+ base = base_i;\r
+ }\r
+ else if (PyType_IsSubtype(winner, candidate))\r
+ ;\r
+ else if (PyType_IsSubtype(candidate, winner)) {\r
+ winner = candidate;\r
+ base = base_i;\r
+ }\r
+ else {\r
+ PyErr_SetString(\r
+ PyExc_TypeError,\r
+ "multiple bases have "\r
+ "instance lay-out conflict");\r
+ return NULL;\r
+ }\r
+ }\r
+ if (base == NULL)\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "a new-style class can't have only classic bases");\r
+ return base;\r
+}\r
+\r
+static int\r
+extra_ivars(PyTypeObject *type, PyTypeObject *base)\r
+{\r
+ size_t t_size = type->tp_basicsize;\r
+ size_t b_size = base->tp_basicsize;\r
+\r
+ assert(t_size >= b_size); /* Else type smaller than base! */\r
+ if (type->tp_itemsize || base->tp_itemsize) {\r
+ /* If itemsize is involved, stricter rules */\r
+ return t_size != b_size ||\r
+ type->tp_itemsize != base->tp_itemsize;\r
+ }\r
+ if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&\r
+ type->tp_weaklistoffset + sizeof(PyObject *) == t_size &&\r
+ type->tp_flags & Py_TPFLAGS_HEAPTYPE)\r
+ t_size -= sizeof(PyObject *);\r
+ if (type->tp_dictoffset && base->tp_dictoffset == 0 &&\r
+ type->tp_dictoffset + sizeof(PyObject *) == t_size &&\r
+ type->tp_flags & Py_TPFLAGS_HEAPTYPE)\r
+ t_size -= sizeof(PyObject *);\r
+\r
+ return t_size != b_size;\r
+}\r
+\r
+static PyTypeObject *\r
+solid_base(PyTypeObject *type)\r
+{\r
+ PyTypeObject *base;\r
+\r
+ if (type->tp_base)\r
+ base = solid_base(type->tp_base);\r
+ else\r
+ base = &PyBaseObject_Type;\r
+ if (extra_ivars(type, base))\r
+ return type;\r
+ else\r
+ return base;\r
+}\r
+\r
+static void object_dealloc(PyObject *);\r
+static int object_init(PyObject *, PyObject *, PyObject *);\r
+static int update_slot(PyTypeObject *, PyObject *);\r
+static void fixup_slot_dispatchers(PyTypeObject *);\r
+\r
+/*\r
+ * Helpers for __dict__ descriptor. We don't want to expose the dicts\r
+ * inherited from various builtin types. The builtin base usually provides\r
+ * its own __dict__ descriptor, so we use that when we can.\r
+ */\r
+static PyTypeObject *\r
+get_builtin_base_with_dict(PyTypeObject *type)\r
+{\r
+ while (type->tp_base != NULL) {\r
+ if (type->tp_dictoffset != 0 &&\r
+ !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))\r
+ return type;\r
+ type = type->tp_base;\r
+ }\r
+ return NULL;\r
+}\r
+\r
+static PyObject *\r
+get_dict_descriptor(PyTypeObject *type)\r
+{\r
+ static PyObject *dict_str;\r
+ PyObject *descr;\r
+\r
+ if (dict_str == NULL) {\r
+ dict_str = PyString_InternFromString("__dict__");\r
+ if (dict_str == NULL)\r
+ return NULL;\r
+ }\r
+ descr = _PyType_Lookup(type, dict_str);\r
+ if (descr == NULL || !PyDescr_IsData(descr))\r
+ return NULL;\r
+\r
+ return descr;\r
+}\r
+\r
+static void\r
+raise_dict_descr_error(PyObject *obj)\r
+{\r
+ PyErr_Format(PyExc_TypeError,\r
+ "this __dict__ descriptor does not support "\r
+ "'%.200s' objects", obj->ob_type->tp_name);\r
+}\r
+\r
+static PyObject *\r
+subtype_dict(PyObject *obj, void *context)\r
+{\r
+ PyObject **dictptr;\r
+ PyObject *dict;\r
+ PyTypeObject *base;\r
+\r
+ base = get_builtin_base_with_dict(obj->ob_type);\r
+ if (base != NULL) {\r
+ descrgetfunc func;\r
+ PyObject *descr = get_dict_descriptor(base);\r
+ if (descr == NULL) {\r
+ raise_dict_descr_error(obj);\r
+ return NULL;\r
+ }\r
+ func = descr->ob_type->tp_descr_get;\r
+ if (func == NULL) {\r
+ raise_dict_descr_error(obj);\r
+ return NULL;\r
+ }\r
+ return func(descr, obj, (PyObject *)(obj->ob_type));\r
+ }\r
+\r
+ dictptr = _PyObject_GetDictPtr(obj);\r
+ if (dictptr == NULL) {\r
+ PyErr_SetString(PyExc_AttributeError,\r
+ "This object has no __dict__");\r
+ return NULL;\r
+ }\r
+ dict = *dictptr;\r
+ if (dict == NULL)\r
+ *dictptr = dict = PyDict_New();\r
+ Py_XINCREF(dict);\r
+ return dict;\r
+}\r
+\r
+static int\r
+subtype_setdict(PyObject *obj, PyObject *value, void *context)\r
+{\r
+ PyObject **dictptr;\r
+ PyObject *dict;\r
+ PyTypeObject *base;\r
+\r
+ base = get_builtin_base_with_dict(obj->ob_type);\r
+ if (base != NULL) {\r
+ descrsetfunc func;\r
+ PyObject *descr = get_dict_descriptor(base);\r
+ if (descr == NULL) {\r
+ raise_dict_descr_error(obj);\r
+ return -1;\r
+ }\r
+ func = descr->ob_type->tp_descr_set;\r
+ if (func == NULL) {\r
+ raise_dict_descr_error(obj);\r
+ return -1;\r
+ }\r
+ return func(descr, obj, value);\r
+ }\r
+\r
+ dictptr = _PyObject_GetDictPtr(obj);\r
+ if (dictptr == NULL) {\r
+ PyErr_SetString(PyExc_AttributeError,\r
+ "This object has no __dict__");\r
+ return -1;\r
+ }\r
+ if (value != NULL && !PyDict_Check(value)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "__dict__ must be set to a dictionary, "\r
+ "not a '%.200s'", Py_TYPE(value)->tp_name);\r
+ return -1;\r
+ }\r
+ dict = *dictptr;\r
+ Py_XINCREF(value);\r
+ *dictptr = value;\r
+ Py_XDECREF(dict);\r
+ return 0;\r
+}\r
+\r
+static PyObject *\r
+subtype_getweakref(PyObject *obj, void *context)\r
+{\r
+ PyObject **weaklistptr;\r
+ PyObject *result;\r
+\r
+ if (Py_TYPE(obj)->tp_weaklistoffset == 0) {\r
+ PyErr_SetString(PyExc_AttributeError,\r
+ "This object has no __weakref__");\r
+ return NULL;\r
+ }\r
+ assert(Py_TYPE(obj)->tp_weaklistoffset > 0);\r
+ assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=\r
+ (size_t)(Py_TYPE(obj)->tp_basicsize));\r
+ weaklistptr = (PyObject **)\r
+ ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);\r
+ if (*weaklistptr == NULL)\r
+ result = Py_None;\r
+ else\r
+ result = *weaklistptr;\r
+ Py_INCREF(result);\r
+ return result;\r
+}\r
+\r
+/* Three variants on the subtype_getsets list. */\r
+\r
+static PyGetSetDef subtype_getsets_full[] = {\r
+ {"__dict__", subtype_dict, subtype_setdict,\r
+ PyDoc_STR("dictionary for instance variables (if defined)")},\r
+ {"__weakref__", subtype_getweakref, NULL,\r
+ PyDoc_STR("list of weak references to the object (if defined)")},\r
+ {0}\r
+};\r
+\r
+static PyGetSetDef subtype_getsets_dict_only[] = {\r
+ {"__dict__", subtype_dict, subtype_setdict,\r
+ PyDoc_STR("dictionary for instance variables (if defined)")},\r
+ {0}\r
+};\r
+\r
+static PyGetSetDef subtype_getsets_weakref_only[] = {\r
+ {"__weakref__", subtype_getweakref, NULL,\r
+ PyDoc_STR("list of weak references to the object (if defined)")},\r
+ {0}\r
+};\r
+\r
+static int\r
+valid_identifier(PyObject *s)\r
+{\r
+ unsigned char *p;\r
+ Py_ssize_t i, n;\r
+\r
+ if (!PyString_Check(s)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "__slots__ items must be strings, not '%.200s'",\r
+ Py_TYPE(s)->tp_name);\r
+ return 0;\r
+ }\r
+ p = (unsigned char *) PyString_AS_STRING(s);\r
+ n = PyString_GET_SIZE(s);\r
+ /* We must reject an empty name. As a hack, we bump the\r
+ length to 1 so that the loop will balk on the trailing \0. */\r
+ if (n == 0)\r
+ n = 1;\r
+ for (i = 0; i < n; i++, p++) {\r
+ if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "__slots__ must be identifiers");\r
+ return 0;\r
+ }\r
+ }\r
+ return 1;\r
+}\r
+\r
+#ifdef Py_USING_UNICODE\r
+/* Replace Unicode objects in slots. */\r
+\r
+static PyObject *\r
+_unicode_to_string(PyObject *slots, Py_ssize_t nslots)\r
+{\r
+ PyObject *tmp = NULL;\r
+ PyObject *slot_name, *new_name;\r
+ Py_ssize_t i;\r
+\r
+ for (i = 0; i < nslots; i++) {\r
+ if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) {\r
+ if (tmp == NULL) {\r
+ tmp = PySequence_List(slots);\r
+ if (tmp == NULL)\r
+ return NULL;\r
+ }\r
+ new_name = _PyUnicode_AsDefaultEncodedString(slot_name,\r
+ NULL);\r
+ if (new_name == NULL) {\r
+ Py_DECREF(tmp);\r
+ return NULL;\r
+ }\r
+ Py_INCREF(new_name);\r
+ PyList_SET_ITEM(tmp, i, new_name);\r
+ Py_DECREF(slot_name);\r
+ }\r
+ }\r
+ if (tmp != NULL) {\r
+ slots = PyList_AsTuple(tmp);\r
+ Py_DECREF(tmp);\r
+ }\r
+ return slots;\r
+}\r
+#endif\r
+\r
+/* Forward */\r
+static int\r
+object_init(PyObject *self, PyObject *args, PyObject *kwds);\r
+\r
+static int\r
+type_init(PyObject *cls, PyObject *args, PyObject *kwds)\r
+{\r
+ int res;\r
+\r
+ assert(args != NULL && PyTuple_Check(args));\r
+ assert(kwds == NULL || PyDict_Check(kwds));\r
+\r
+ if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "type.__init__() takes no keyword arguments");\r
+ return -1;\r
+ }\r
+\r
+ if (args != NULL && PyTuple_Check(args) &&\r
+ (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "type.__init__() takes 1 or 3 arguments");\r
+ return -1;\r
+ }\r
+\r
+ /* Call object.__init__(self) now. */\r
+ /* XXX Could call super(type, cls).__init__() but what's the point? */\r
+ args = PyTuple_GetSlice(args, 0, 0);\r
+ res = object_init(cls, args, NULL);\r
+ Py_DECREF(args);\r
+ return res;\r
+}\r
+\r
+static PyObject *\r
+type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)\r
+{\r
+ PyObject *name, *bases, *dict;\r
+ static char *kwlist[] = {"name", "bases", "dict", 0};\r
+ PyObject *slots, *tmp, *newslots;\r
+ PyTypeObject *type, *base, *tmptype, *winner;\r
+ PyHeapTypeObject *et;\r
+ PyMemberDef *mp;\r
+ Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;\r
+ int j, may_add_dict, may_add_weak;\r
+\r
+ assert(args != NULL && PyTuple_Check(args));\r
+ assert(kwds == NULL || PyDict_Check(kwds));\r
+\r
+ /* Special case: type(x) should return x->ob_type */\r
+ {\r
+ const Py_ssize_t nargs = PyTuple_GET_SIZE(args);\r
+ const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);\r
+\r
+ if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {\r
+ PyObject *x = PyTuple_GET_ITEM(args, 0);\r
+ Py_INCREF(Py_TYPE(x));\r
+ return (PyObject *) Py_TYPE(x);\r
+ }\r
+\r
+ /* SF bug 475327 -- if that didn't trigger, we need 3\r
+ arguments. but PyArg_ParseTupleAndKeywords below may give\r
+ a msg saying type() needs exactly 3. */\r
+ if (nargs + nkwds != 3) {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "type() takes 1 or 3 arguments");\r
+ return NULL;\r
+ }\r
+ }\r
+\r
+ /* Check arguments: (name, bases, dict) */\r
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,\r
+ &name,\r
+ &PyTuple_Type, &bases,\r
+ &PyDict_Type, &dict))\r
+ return NULL;\r
+\r
+ /* Determine the proper metatype to deal with this,\r
+ and check for metatype conflicts while we're at it.\r
+ Note that if some other metatype wins to contract,\r
+ it's possible that its instances are not types. */\r
+ nbases = PyTuple_GET_SIZE(bases);\r
+ winner = metatype;\r
+ for (i = 0; i < nbases; i++) {\r
+ tmp = PyTuple_GET_ITEM(bases, i);\r
+ tmptype = tmp->ob_type;\r
+ if (tmptype == &PyClass_Type)\r
+ continue; /* Special case classic classes */\r
+ if (PyType_IsSubtype(winner, tmptype))\r
+ continue;\r
+ if (PyType_IsSubtype(tmptype, winner)) {\r
+ winner = tmptype;\r
+ continue;\r
+ }\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "metaclass conflict: "\r
+ "the metaclass of a derived class "\r
+ "must be a (non-strict) subclass "\r
+ "of the metaclasses of all its bases");\r
+ return NULL;\r
+ }\r
+ if (winner != metatype) {\r
+ if (winner->tp_new != type_new) /* Pass it to the winner */\r
+ return winner->tp_new(winner, args, kwds);\r
+ metatype = winner;\r
+ }\r
+\r
+ /* Adjust for empty tuple bases */\r
+ if (nbases == 0) {\r
+ bases = PyTuple_Pack(1, &PyBaseObject_Type);\r
+ if (bases == NULL)\r
+ return NULL;\r
+ nbases = 1;\r
+ }\r
+ else\r
+ Py_INCREF(bases);\r
+\r
+ /* XXX From here until type is allocated, "return NULL" leaks bases! */\r
+\r
+ /* Calculate best base, and check that all bases are type objects */\r
+ base = best_base(bases);\r
+ if (base == NULL) {\r
+ Py_DECREF(bases);\r
+ return NULL;\r
+ }\r
+ if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "type '%.100s' is not an acceptable base type",\r
+ base->tp_name);\r
+ Py_DECREF(bases);\r
+ return NULL;\r
+ }\r
+\r
+ /* Check for a __slots__ sequence variable in dict, and count it */\r
+ slots = PyDict_GetItemString(dict, "__slots__");\r
+ nslots = 0;\r
+ add_dict = 0;\r
+ add_weak = 0;\r
+ may_add_dict = base->tp_dictoffset == 0;\r
+ may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;\r
+ if (slots == NULL) {\r
+ if (may_add_dict) {\r
+ add_dict++;\r
+ }\r
+ if (may_add_weak) {\r
+ add_weak++;\r
+ }\r
+ }\r
+ else {\r
+ /* Have slots */\r
+\r
+ /* Make it into a tuple */\r
+ if (PyString_Check(slots) || PyUnicode_Check(slots))\r
+ slots = PyTuple_Pack(1, slots);\r
+ else\r
+ slots = PySequence_Tuple(slots);\r
+ if (slots == NULL) {\r
+ Py_DECREF(bases);\r
+ return NULL;\r
+ }\r
+ assert(PyTuple_Check(slots));\r
+\r
+ /* Are slots allowed? */\r
+ nslots = PyTuple_GET_SIZE(slots);\r
+ if (nslots > 0 && base->tp_itemsize != 0) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "nonempty __slots__ "\r
+ "not supported for subtype of '%s'",\r
+ base->tp_name);\r
+ bad_slots:\r
+ Py_DECREF(bases);\r
+ Py_DECREF(slots);\r
+ return NULL;\r
+ }\r
+\r
+#ifdef Py_USING_UNICODE\r
+ tmp = _unicode_to_string(slots, nslots);\r
+ if (tmp == NULL)\r
+ goto bad_slots;\r
+ if (tmp != slots) {\r
+ Py_DECREF(slots);\r
+ slots = tmp;\r
+ }\r
+#endif\r
+ /* Check for valid slot names and two special cases */\r
+ for (i = 0; i < nslots; i++) {\r
+ PyObject *tmp = PyTuple_GET_ITEM(slots, i);\r
+ char *s;\r
+ if (!valid_identifier(tmp))\r
+ goto bad_slots;\r
+ assert(PyString_Check(tmp));\r
+ s = PyString_AS_STRING(tmp);\r
+ if (strcmp(s, "__dict__") == 0) {\r
+ if (!may_add_dict || add_dict) {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "__dict__ slot disallowed: "\r
+ "we already got one");\r
+ goto bad_slots;\r
+ }\r
+ add_dict++;\r
+ }\r
+ if (strcmp(s, "__weakref__") == 0) {\r
+ if (!may_add_weak || add_weak) {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "__weakref__ slot disallowed: "\r
+ "either we already got one, "\r
+ "or __itemsize__ != 0");\r
+ goto bad_slots;\r
+ }\r
+ add_weak++;\r
+ }\r
+ }\r
+\r
+ /* Copy slots into a list, mangle names and sort them.\r
+ Sorted names are needed for __class__ assignment.\r
+ Convert them back to tuple at the end.\r
+ */\r
+ newslots = PyList_New(nslots - add_dict - add_weak);\r
+ if (newslots == NULL)\r
+ goto bad_slots;\r
+ for (i = j = 0; i < nslots; i++) {\r
+ char *s;\r
+ tmp = PyTuple_GET_ITEM(slots, i);\r
+ s = PyString_AS_STRING(tmp);\r
+ if ((add_dict && strcmp(s, "__dict__") == 0) ||\r
+ (add_weak && strcmp(s, "__weakref__") == 0))\r
+ continue;\r
+ tmp =_Py_Mangle(name, tmp);\r
+ if (!tmp) {\r
+ Py_DECREF(newslots);\r
+ goto bad_slots;\r
+ }\r
+ PyList_SET_ITEM(newslots, j, tmp);\r
+ j++;\r
+ }\r
+ assert(j == nslots - add_dict - add_weak);\r
+ nslots = j;\r
+ Py_DECREF(slots);\r
+ if (PyList_Sort(newslots) == -1) {\r
+ Py_DECREF(bases);\r
+ Py_DECREF(newslots);\r
+ return NULL;\r
+ }\r
+ slots = PyList_AsTuple(newslots);\r
+ Py_DECREF(newslots);\r
+ if (slots == NULL) {\r
+ Py_DECREF(bases);\r
+ return NULL;\r
+ }\r
+\r
+ /* Secondary bases may provide weakrefs or dict */\r
+ if (nbases > 1 &&\r
+ ((may_add_dict && !add_dict) ||\r
+ (may_add_weak && !add_weak))) {\r
+ for (i = 0; i < nbases; i++) {\r
+ tmp = PyTuple_GET_ITEM(bases, i);\r
+ if (tmp == (PyObject *)base)\r
+ continue; /* Skip primary base */\r
+ if (PyClass_Check(tmp)) {\r
+ /* Classic base class provides both */\r
+ if (may_add_dict && !add_dict)\r
+ add_dict++;\r
+ if (may_add_weak && !add_weak)\r
+ add_weak++;\r
+ break;\r
+ }\r
+ assert(PyType_Check(tmp));\r
+ tmptype = (PyTypeObject *)tmp;\r
+ if (may_add_dict && !add_dict &&\r
+ tmptype->tp_dictoffset != 0)\r
+ add_dict++;\r
+ if (may_add_weak && !add_weak &&\r
+ tmptype->tp_weaklistoffset != 0)\r
+ add_weak++;\r
+ if (may_add_dict && !add_dict)\r
+ continue;\r
+ if (may_add_weak && !add_weak)\r
+ continue;\r
+ /* Nothing more to check */\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
+ /* XXX From here until type is safely allocated,\r
+ "return NULL" may leak slots! */\r
+\r
+ /* Allocate the type object */\r
+ type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);\r
+ if (type == NULL) {\r
+ Py_XDECREF(slots);\r
+ Py_DECREF(bases);\r
+ return NULL;\r
+ }\r
+\r
+ /* Keep name and slots alive in the extended type object */\r
+ et = (PyHeapTypeObject *)type;\r
+ Py_INCREF(name);\r
+ et->ht_name = name;\r
+ et->ht_slots = slots;\r
+\r
+ /* Initialize tp_flags */\r
+ type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |\r
+ Py_TPFLAGS_BASETYPE;\r
+ if (base->tp_flags & Py_TPFLAGS_HAVE_GC)\r
+ type->tp_flags |= Py_TPFLAGS_HAVE_GC;\r
+ if (base->tp_flags & Py_TPFLAGS_HAVE_NEWBUFFER)\r
+ type->tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;\r
+\r
+ /* It's a new-style number unless it specifically inherits any\r
+ old-style numeric behavior */\r
+ if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||\r
+ (base->tp_as_number == NULL))\r
+ type->tp_flags |= Py_TPFLAGS_CHECKTYPES;\r
+\r
+ /* Initialize essential fields */\r
+ type->tp_as_number = &et->as_number;\r
+ type->tp_as_sequence = &et->as_sequence;\r
+ type->tp_as_mapping = &et->as_mapping;\r
+ type->tp_as_buffer = &et->as_buffer;\r
+ type->tp_name = PyString_AS_STRING(name);\r
+\r
+ /* Set tp_base and tp_bases */\r
+ type->tp_bases = bases;\r
+ Py_INCREF(base);\r
+ type->tp_base = base;\r
+\r
+ /* Initialize tp_dict from passed-in dict */\r
+ type->tp_dict = dict = PyDict_Copy(dict);\r
+ if (dict == NULL) {\r
+ Py_DECREF(type);\r
+ return NULL;\r
+ }\r
+\r
+ /* Set __module__ in the dict */\r
+ if (PyDict_GetItemString(dict, "__module__") == NULL) {\r
+ tmp = PyEval_GetGlobals();\r
+ if (tmp != NULL) {\r
+ tmp = PyDict_GetItemString(tmp, "__name__");\r
+ if (tmp != NULL) {\r
+ if (PyDict_SetItemString(dict, "__module__",\r
+ tmp) < 0)\r
+ return NULL;\r
+ }\r
+ }\r
+ }\r
+\r
+ /* Set tp_doc to a copy of dict['__doc__'], if the latter is there\r
+ and is a string. The __doc__ accessor will first look for tp_doc;\r
+ if that fails, it will still look into __dict__.\r
+ */\r
+ {\r
+ PyObject *doc = PyDict_GetItemString(dict, "__doc__");\r
+ if (doc != NULL && PyString_Check(doc)) {\r
+ const size_t n = (size_t)PyString_GET_SIZE(doc);\r
+ char *tp_doc = (char *)PyObject_MALLOC(n+1);\r
+ if (tp_doc == NULL) {\r
+ Py_DECREF(type);\r
+ return NULL;\r
+ }\r
+ memcpy(tp_doc, PyString_AS_STRING(doc), n+1);\r
+ type->tp_doc = tp_doc;\r
+ }\r
+ }\r
+\r
+ /* Special-case __new__: if it's a plain function,\r
+ make it a static function */\r
+ tmp = PyDict_GetItemString(dict, "__new__");\r
+ if (tmp != NULL && PyFunction_Check(tmp)) {\r
+ tmp = PyStaticMethod_New(tmp);\r
+ if (tmp == NULL) {\r
+ Py_DECREF(type);\r
+ return NULL;\r
+ }\r
+ PyDict_SetItemString(dict, "__new__", tmp);\r
+ Py_DECREF(tmp);\r
+ }\r
+\r
+ /* Add descriptors for custom slots from __slots__, or for __dict__ */\r
+ mp = PyHeapType_GET_MEMBERS(et);\r
+ slotoffset = base->tp_basicsize;\r
+ if (slots != NULL) {\r
+ for (i = 0; i < nslots; i++, mp++) {\r
+ mp->name = PyString_AS_STRING(\r
+ PyTuple_GET_ITEM(slots, i));\r
+ mp->type = T_OBJECT_EX;\r
+ mp->offset = slotoffset;\r
+\r
+ /* __dict__ and __weakref__ are already filtered out */\r
+ assert(strcmp(mp->name, "__dict__") != 0);\r
+ assert(strcmp(mp->name, "__weakref__") != 0);\r
+\r
+ slotoffset += sizeof(PyObject *);\r
+ }\r
+ }\r
+ if (add_dict) {\r
+ if (base->tp_itemsize)\r
+ type->tp_dictoffset = -(long)sizeof(PyObject *);\r
+ else\r
+ type->tp_dictoffset = slotoffset;\r
+ slotoffset += sizeof(PyObject *);\r
+ }\r
+ if (add_weak) {\r
+ assert(!base->tp_itemsize);\r
+ type->tp_weaklistoffset = slotoffset;\r
+ slotoffset += sizeof(PyObject *);\r
+ }\r
+ type->tp_basicsize = slotoffset;\r
+ type->tp_itemsize = base->tp_itemsize;\r
+ type->tp_members = PyHeapType_GET_MEMBERS(et);\r
+\r
+ if (type->tp_weaklistoffset && type->tp_dictoffset)\r
+ type->tp_getset = subtype_getsets_full;\r
+ else if (type->tp_weaklistoffset && !type->tp_dictoffset)\r
+ type->tp_getset = subtype_getsets_weakref_only;\r
+ else if (!type->tp_weaklistoffset && type->tp_dictoffset)\r
+ type->tp_getset = subtype_getsets_dict_only;\r
+ else\r
+ type->tp_getset = NULL;\r
+\r
+ /* Special case some slots */\r
+ if (type->tp_dictoffset != 0 || nslots > 0) {\r
+ if (base->tp_getattr == NULL && base->tp_getattro == NULL)\r
+ type->tp_getattro = PyObject_GenericGetAttr;\r
+ if (base->tp_setattr == NULL && base->tp_setattro == NULL)\r
+ type->tp_setattro = PyObject_GenericSetAttr;\r
+ }\r
+ type->tp_dealloc = subtype_dealloc;\r
+\r
+ /* Enable GC unless there are really no instance variables possible */\r
+ if (!(type->tp_basicsize == sizeof(PyObject) &&\r
+ type->tp_itemsize == 0))\r
+ type->tp_flags |= Py_TPFLAGS_HAVE_GC;\r
+\r
+ /* Always override allocation strategy to use regular heap */\r
+ type->tp_alloc = PyType_GenericAlloc;\r
+ if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {\r
+ type->tp_free = PyObject_GC_Del;\r
+ type->tp_traverse = subtype_traverse;\r
+ type->tp_clear = subtype_clear;\r
+ }\r
+ else\r
+ type->tp_free = PyObject_Del;\r
+\r
+ /* Initialize the rest */\r
+ if (PyType_Ready(type) < 0) {\r
+ Py_DECREF(type);\r
+ return NULL;\r
+ }\r
+\r
+ /* Put the proper slots in place */\r
+ fixup_slot_dispatchers(type);\r
+\r
+ return (PyObject *)type;\r
+}\r
+\r
+/* Internal API to look for a name through the MRO.\r
+ This returns a borrowed reference, and doesn't set an exception! */\r
+PyObject *\r
+_PyType_Lookup(PyTypeObject *type, PyObject *name)\r
+{\r
+ Py_ssize_t i, n;\r
+ PyObject *mro, *res, *base, *dict;\r
+ unsigned int h;\r
+\r
+ if (MCACHE_CACHEABLE_NAME(name) &&\r
+ PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {\r
+ /* fast path */\r
+ h = MCACHE_HASH_METHOD(type, name);\r
+ if (method_cache[h].version == type->tp_version_tag &&\r
+ method_cache[h].name == name)\r
+ return method_cache[h].value;\r
+ }\r
+\r
+ /* Look in tp_dict of types in MRO */\r
+ mro = type->tp_mro;\r
+\r
+ /* If mro is NULL, the type is either not yet initialized\r
+ by PyType_Ready(), or already cleared by type_clear().\r
+ Either way the safest thing to do is to return NULL. */\r
+ if (mro == NULL)\r
+ return NULL;\r
+\r
+ res = NULL;\r
+ assert(PyTuple_Check(mro));\r
+ n = PyTuple_GET_SIZE(mro);\r
+ for (i = 0; i < n; i++) {\r
+ base = PyTuple_GET_ITEM(mro, i);\r
+ if (PyClass_Check(base))\r
+ dict = ((PyClassObject *)base)->cl_dict;\r
+ else {\r
+ assert(PyType_Check(base));\r
+ dict = ((PyTypeObject *)base)->tp_dict;\r
+ }\r
+ assert(dict && PyDict_Check(dict));\r
+ res = PyDict_GetItem(dict, name);\r
+ if (res != NULL)\r
+ break;\r
+ }\r
+\r
+ if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) {\r
+ h = MCACHE_HASH_METHOD(type, name);\r
+ method_cache[h].version = type->tp_version_tag;\r
+ method_cache[h].value = res; /* borrowed */\r
+ Py_INCREF(name);\r
+ Py_DECREF(method_cache[h].name);\r
+ method_cache[h].name = name;\r
+ }\r
+ return res;\r
+}\r
+\r
+/* This is similar to PyObject_GenericGetAttr(),\r
+ but uses _PyType_Lookup() instead of just looking in type->tp_dict. */\r
+static PyObject *\r
+type_getattro(PyTypeObject *type, PyObject *name)\r
+{\r
+ PyTypeObject *metatype = Py_TYPE(type);\r
+ PyObject *meta_attribute, *attribute;\r
+ descrgetfunc meta_get;\r
+\r
+ if (!PyString_Check(name)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "attribute name must be string, not '%.200s'",\r
+ name->ob_type->tp_name);\r
+ return NULL;\r
+ }\r
+\r
+ /* Initialize this type (we'll assume the metatype is initialized) */\r
+ if (type->tp_dict == NULL) {\r
+ if (PyType_Ready(type) < 0)\r
+ return NULL;\r
+ }\r
+\r
+ /* No readable descriptor found yet */\r
+ meta_get = NULL;\r
+\r
+ /* Look for the attribute in the metatype */\r
+ meta_attribute = _PyType_Lookup(metatype, name);\r
+\r
+ if (meta_attribute != NULL) {\r
+ meta_get = Py_TYPE(meta_attribute)->tp_descr_get;\r
+\r
+ if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {\r
+ /* Data descriptors implement tp_descr_set to intercept\r
+ * writes. Assume the attribute is not overridden in\r
+ * type's tp_dict (and bases): call the descriptor now.\r
+ */\r
+ return meta_get(meta_attribute, (PyObject *)type,\r
+ (PyObject *)metatype);\r
+ }\r
+ Py_INCREF(meta_attribute);\r
+ }\r
+\r
+ /* No data descriptor found on metatype. Look in tp_dict of this\r
+ * type and its bases */\r
+ attribute = _PyType_Lookup(type, name);\r
+ if (attribute != NULL) {\r
+ /* Implement descriptor functionality, if any */\r
+ descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;\r
+\r
+ Py_XDECREF(meta_attribute);\r
+\r
+ if (local_get != NULL) {\r
+ /* NULL 2nd argument indicates the descriptor was\r
+ * found on the target object itself (or a base) */\r
+ return local_get(attribute, (PyObject *)NULL,\r
+ (PyObject *)type);\r
+ }\r
+\r
+ Py_INCREF(attribute);\r
+ return attribute;\r
+ }\r
+\r
+ /* No attribute found in local __dict__ (or bases): use the\r
+ * descriptor from the metatype, if any */\r
+ if (meta_get != NULL) {\r
+ PyObject *res;\r
+ res = meta_get(meta_attribute, (PyObject *)type,\r
+ (PyObject *)metatype);\r
+ Py_DECREF(meta_attribute);\r
+ return res;\r
+ }\r
+\r
+ /* If an ordinary attribute was found on the metatype, return it now */\r
+ if (meta_attribute != NULL) {\r
+ return meta_attribute;\r
+ }\r
+\r
+ /* Give up */\r
+ PyErr_Format(PyExc_AttributeError,\r
+ "type object '%.50s' has no attribute '%.400s'",\r
+ type->tp_name, PyString_AS_STRING(name));\r
+ return NULL;\r
+}\r
+\r
+static int\r
+type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)\r
+{\r
+ if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {\r
+ PyErr_Format(\r
+ PyExc_TypeError,\r
+ "can't set attributes of built-in/extension type '%s'",\r
+ type->tp_name);\r
+ return -1;\r
+ }\r
+ if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)\r
+ return -1;\r
+ return update_slot(type, name);\r
+}\r
+\r
+static void\r
+type_dealloc(PyTypeObject *type)\r
+{\r
+ PyHeapTypeObject *et;\r
+\r
+ /* Assert this is a heap-allocated type object */\r
+ assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);\r
+ _PyObject_GC_UNTRACK(type);\r
+ PyObject_ClearWeakRefs((PyObject *)type);\r
+ et = (PyHeapTypeObject *)type;\r
+ Py_XDECREF(type->tp_base);\r
+ Py_XDECREF(type->tp_dict);\r
+ Py_XDECREF(type->tp_bases);\r
+ Py_XDECREF(type->tp_mro);\r
+ Py_XDECREF(type->tp_cache);\r
+ Py_XDECREF(type->tp_subclasses);\r
+ /* A type's tp_doc is heap allocated, unlike the tp_doc slots\r
+ * of most other objects. It's okay to cast it to char *.\r
+ */\r
+ PyObject_Free((char *)type->tp_doc);\r
+ Py_XDECREF(et->ht_name);\r
+ Py_XDECREF(et->ht_slots);\r
+ Py_TYPE(type)->tp_free((PyObject *)type);\r
+}\r
+\r
+static PyObject *\r
+type_subclasses(PyTypeObject *type, PyObject *args_ignored)\r
+{\r
+ PyObject *list, *raw, *ref;\r
+ Py_ssize_t i, n;\r
+\r
+ list = PyList_New(0);\r
+ if (list == NULL)\r
+ return NULL;\r
+ raw = type->tp_subclasses;\r
+ if (raw == NULL)\r
+ return list;\r
+ assert(PyList_Check(raw));\r
+ n = PyList_GET_SIZE(raw);\r
+ for (i = 0; i < n; i++) {\r
+ ref = PyList_GET_ITEM(raw, i);\r
+ assert(PyWeakref_CheckRef(ref));\r
+ ref = PyWeakref_GET_OBJECT(ref);\r
+ if (ref != Py_None) {\r
+ if (PyList_Append(list, ref) < 0) {\r
+ Py_DECREF(list);\r
+ return NULL;\r
+ }\r
+ }\r
+ }\r
+ return list;\r
+}\r
+\r
+static PyMethodDef type_methods[] = {\r
+ {"mro", (PyCFunction)mro_external, METH_NOARGS,\r
+ PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},\r
+ {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,\r
+ PyDoc_STR("__subclasses__() -> list of immediate subclasses")},\r
+ {"__instancecheck__", type___instancecheck__, METH_O,\r
+ PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance")},\r
+ {"__subclasscheck__", type___subclasscheck__, METH_O,\r
+ PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")},\r
+ {0}\r
+};\r
+\r
+PyDoc_STRVAR(type_doc,\r
+"type(object) -> the object's type\n"\r
+"type(name, bases, dict) -> a new type");\r
+\r
+static int\r
+type_traverse(PyTypeObject *type, visitproc visit, void *arg)\r
+{\r
+ /* Because of type_is_gc(), the collector only calls this\r
+ for heaptypes. */\r
+ assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);\r
+\r
+ Py_VISIT(type->tp_dict);\r
+ Py_VISIT(type->tp_cache);\r
+ Py_VISIT(type->tp_mro);\r
+ Py_VISIT(type->tp_bases);\r
+ Py_VISIT(type->tp_base);\r
+\r
+ /* There's no need to visit type->tp_subclasses or\r
+ ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved\r
+ in cycles; tp_subclasses is a list of weak references,\r
+ and slots is a tuple of strings. */\r
+\r
+ return 0;\r
+}\r
+\r
+static int\r
+type_clear(PyTypeObject *type)\r
+{\r
+ /* Because of type_is_gc(), the collector only calls this\r
+ for heaptypes. */\r
+ assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);\r
+\r
+ /* We need to invalidate the method cache carefully before clearing\r
+ the dict, so that other objects caught in a reference cycle\r
+ don't start calling destroyed methods.\r
+\r
+ Otherwise, the only field we need to clear is tp_mro, which is\r
+ part of a hard cycle (its first element is the class itself) that\r
+ won't be broken otherwise (it's a tuple and tuples don't have a\r
+ tp_clear handler). None of the other fields need to be\r
+ cleared, and here's why:\r
+\r
+ tp_cache:\r
+ Not used; if it were, it would be a dict.\r
+\r
+ tp_bases, tp_base:\r
+ If these are involved in a cycle, there must be at least\r
+ one other, mutable object in the cycle, e.g. a base\r
+ class's dict; the cycle will be broken that way.\r
+\r
+ tp_subclasses:\r
+ A list of weak references can't be part of a cycle; and\r
+ lists have their own tp_clear.\r
+\r
+ slots (in PyHeapTypeObject):\r
+ A tuple of strings can't be part of a cycle.\r
+ */\r
+\r
+ PyType_Modified(type);\r
+ if (type->tp_dict)\r
+ PyDict_Clear(type->tp_dict);\r
+ Py_CLEAR(type->tp_mro);\r
+\r
+ return 0;\r
+}\r
+\r
+static int\r
+type_is_gc(PyTypeObject *type)\r
+{\r
+ return type->tp_flags & Py_TPFLAGS_HEAPTYPE;\r
+}\r
+\r
+PyTypeObject PyType_Type = {\r
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)\r
+ "type", /* tp_name */\r
+ sizeof(PyHeapTypeObject), /* tp_basicsize */\r
+ sizeof(PyMemberDef), /* tp_itemsize */\r
+ (destructor)type_dealloc, /* tp_dealloc */\r
+ 0, /* tp_print */\r
+ 0, /* tp_getattr */\r
+ 0, /* tp_setattr */\r
+ 0, /* tp_compare */\r
+ (reprfunc)type_repr, /* tp_repr */\r
+ 0, /* tp_as_number */\r
+ 0, /* tp_as_sequence */\r
+ 0, /* tp_as_mapping */\r
+ (hashfunc)_Py_HashPointer, /* tp_hash */\r
+ (ternaryfunc)type_call, /* tp_call */\r
+ 0, /* tp_str */\r
+ (getattrofunc)type_getattro, /* tp_getattro */\r
+ (setattrofunc)type_setattro, /* tp_setattro */\r
+ 0, /* tp_as_buffer */\r
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |\r
+ Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */\r
+ type_doc, /* tp_doc */\r
+ (traverseproc)type_traverse, /* tp_traverse */\r
+ (inquiry)type_clear, /* tp_clear */\r
+ type_richcompare, /* tp_richcompare */\r
+ offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */\r
+ 0, /* tp_iter */\r
+ 0, /* tp_iternext */\r
+ type_methods, /* tp_methods */\r
+ type_members, /* tp_members */\r
+ type_getsets, /* tp_getset */\r
+ 0, /* tp_base */\r
+ 0, /* tp_dict */\r
+ 0, /* tp_descr_get */\r
+ 0, /* tp_descr_set */\r
+ offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */\r
+ type_init, /* tp_init */\r
+ 0, /* tp_alloc */\r
+ type_new, /* tp_new */\r
+ PyObject_GC_Del, /* tp_free */\r
+ (inquiry)type_is_gc, /* tp_is_gc */\r
+};\r
+\r
+\r
+/* The base type of all types (eventually)... except itself. */\r
+\r
+/* You may wonder why object.__new__() only complains about arguments\r
+ when object.__init__() is not overridden, and vice versa.\r
+\r
+ Consider the use cases:\r
+\r
+ 1. When neither is overridden, we want to hear complaints about\r
+ excess (i.e., any) arguments, since their presence could\r
+ indicate there's a bug.\r
+\r
+ 2. When defining an Immutable type, we are likely to override only\r
+ __new__(), since __init__() is called too late to initialize an\r
+ Immutable object. Since __new__() defines the signature for the\r
+ type, it would be a pain to have to override __init__() just to\r
+ stop it from complaining about excess arguments.\r
+\r
+ 3. When defining a Mutable type, we are likely to override only\r
+ __init__(). So here the converse reasoning applies: we don't\r
+ want to have to override __new__() just to stop it from\r
+ complaining.\r
+\r
+ 4. When __init__() is overridden, and the subclass __init__() calls\r
+ object.__init__(), the latter should complain about excess\r
+ arguments; ditto for __new__().\r
+\r
+ Use cases 2 and 3 make it unattractive to unconditionally check for\r
+ excess arguments. The best solution that addresses all four use\r
+ cases is as follows: __init__() complains about excess arguments\r
+ unless __new__() is overridden and __init__() is not overridden\r
+ (IOW, if __init__() is overridden or __new__() is not overridden);\r
+ symmetrically, __new__() complains about excess arguments unless\r
+ __init__() is overridden and __new__() is not overridden\r
+ (IOW, if __new__() is overridden or __init__() is not overridden).\r
+\r
+ However, for backwards compatibility, this breaks too much code.\r
+ Therefore, in 2.6, we'll *warn* about excess arguments when both\r
+ methods are overridden; for all other cases we'll use the above\r
+ rules.\r
+\r
+*/\r
+\r
+/* Forward */\r
+static PyObject *\r
+object_new(PyTypeObject *type, PyObject *args, PyObject *kwds);\r
+\r
+static int\r
+excess_args(PyObject *args, PyObject *kwds)\r
+{\r
+ return PyTuple_GET_SIZE(args) ||\r
+ (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));\r
+}\r
+\r
+static int\r
+object_init(PyObject *self, PyObject *args, PyObject *kwds)\r
+{\r
+ int err = 0;\r
+ if (excess_args(args, kwds)) {\r
+ PyTypeObject *type = Py_TYPE(self);\r
+ if (type->tp_init != object_init &&\r
+ type->tp_new != object_new)\r
+ {\r
+ err = PyErr_WarnEx(PyExc_DeprecationWarning,\r
+ "object.__init__() takes no parameters",\r
+ 1);\r
+ }\r
+ else if (type->tp_init != object_init ||\r
+ type->tp_new == object_new)\r
+ {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "object.__init__() takes no parameters");\r
+ err = -1;\r
+ }\r
+ }\r
+ return err;\r
+}\r
+\r
+static PyObject *\r
+object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
+{\r
+ int err = 0;\r
+ if (excess_args(args, kwds)) {\r
+ if (type->tp_new != object_new &&\r
+ type->tp_init != object_init)\r
+ {\r
+ err = PyErr_WarnEx(PyExc_DeprecationWarning,\r
+ "object() takes no parameters",\r
+ 1);\r
+ }\r
+ else if (type->tp_new != object_new ||\r
+ type->tp_init == object_init)\r
+ {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "object() takes no parameters");\r
+ err = -1;\r
+ }\r
+ }\r
+ if (err < 0)\r
+ return NULL;\r
+\r
+ if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {\r
+ static PyObject *comma = NULL;\r
+ PyObject *abstract_methods = NULL;\r
+ PyObject *builtins;\r
+ PyObject *sorted;\r
+ PyObject *sorted_methods = NULL;\r
+ PyObject *joined = NULL;\r
+ const char *joined_str;\r
+\r
+ /* Compute ", ".join(sorted(type.__abstractmethods__))\r
+ into joined. */\r
+ abstract_methods = type_abstractmethods(type, NULL);\r
+ if (abstract_methods == NULL)\r
+ goto error;\r
+ builtins = PyEval_GetBuiltins();\r
+ if (builtins == NULL)\r
+ goto error;\r
+ sorted = PyDict_GetItemString(builtins, "sorted");\r
+ if (sorted == NULL)\r
+ goto error;\r
+ sorted_methods = PyObject_CallFunctionObjArgs(sorted,\r
+ abstract_methods,\r
+ NULL);\r
+ if (sorted_methods == NULL)\r
+ goto error;\r
+ if (comma == NULL) {\r
+ comma = PyString_InternFromString(", ");\r
+ if (comma == NULL)\r
+ goto error;\r
+ }\r
+ joined = PyObject_CallMethod(comma, "join",\r
+ "O", sorted_methods);\r
+ if (joined == NULL)\r
+ goto error;\r
+ joined_str = PyString_AsString(joined);\r
+ if (joined_str == NULL)\r
+ goto error;\r
+\r
+ PyErr_Format(PyExc_TypeError,\r
+ "Can't instantiate abstract class %s "\r
+ "with abstract methods %s",\r
+ type->tp_name,\r
+ joined_str);\r
+ error:\r
+ Py_XDECREF(joined);\r
+ Py_XDECREF(sorted_methods);\r
+ Py_XDECREF(abstract_methods);\r
+ return NULL;\r
+ }\r
+ return type->tp_alloc(type, 0);\r
+}\r
+\r
+static void\r
+object_dealloc(PyObject *self)\r
+{\r
+ Py_TYPE(self)->tp_free(self);\r
+}\r
+\r
+static PyObject *\r
+object_repr(PyObject *self)\r
+{\r
+ PyTypeObject *type;\r
+ PyObject *mod, *name, *rtn;\r
+\r
+ type = Py_TYPE(self);\r
+ mod = type_module(type, NULL);\r
+ if (mod == NULL)\r
+ PyErr_Clear();\r
+ else if (!PyString_Check(mod)) {\r
+ Py_DECREF(mod);\r
+ mod = NULL;\r
+ }\r
+ name = type_name(type, NULL);\r
+ if (name == NULL) {\r
+ Py_XDECREF(mod);\r
+ return NULL;\r
+ }\r
+ if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))\r
+ rtn = PyString_FromFormat("<%s.%s object at %p>",\r
+ PyString_AS_STRING(mod),\r
+ PyString_AS_STRING(name),\r
+ self);\r
+ else\r
+ rtn = PyString_FromFormat("<%s object at %p>",\r
+ type->tp_name, self);\r
+ Py_XDECREF(mod);\r
+ Py_DECREF(name);\r
+ return rtn;\r
+}\r
+\r
+static PyObject *\r
+object_str(PyObject *self)\r
+{\r
+ unaryfunc f;\r
+\r
+ f = Py_TYPE(self)->tp_repr;\r
+ if (f == NULL)\r
+ f = object_repr;\r
+ return f(self);\r
+}\r
+\r
+static PyObject *\r
+object_get_class(PyObject *self, void *closure)\r
+{\r
+ Py_INCREF(Py_TYPE(self));\r
+ return (PyObject *)(Py_TYPE(self));\r
+}\r
+\r
+static int\r
+equiv_structs(PyTypeObject *a, PyTypeObject *b)\r
+{\r
+ return a == b ||\r
+ (a != NULL &&\r
+ b != NULL &&\r
+ a->tp_basicsize == b->tp_basicsize &&\r
+ a->tp_itemsize == b->tp_itemsize &&\r
+ a->tp_dictoffset == b->tp_dictoffset &&\r
+ a->tp_weaklistoffset == b->tp_weaklistoffset &&\r
+ ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==\r
+ (b->tp_flags & Py_TPFLAGS_HAVE_GC)));\r
+}\r
+\r
+static int\r
+same_slots_added(PyTypeObject *a, PyTypeObject *b)\r
+{\r
+ PyTypeObject *base = a->tp_base;\r
+ Py_ssize_t size;\r
+ PyObject *slots_a, *slots_b;\r
+\r
+ assert(base == b->tp_base);\r
+ size = base->tp_basicsize;\r
+ if (a->tp_dictoffset == size && b->tp_dictoffset == size)\r
+ size += sizeof(PyObject *);\r
+ if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)\r
+ size += sizeof(PyObject *);\r
+\r
+ /* Check slots compliance */\r
+ slots_a = ((PyHeapTypeObject *)a)->ht_slots;\r
+ slots_b = ((PyHeapTypeObject *)b)->ht_slots;\r
+ if (slots_a && slots_b) {\r
+ if (PyObject_Compare(slots_a, slots_b) != 0)\r
+ return 0;\r
+ size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);\r
+ }\r
+ return size == a->tp_basicsize && size == b->tp_basicsize;\r
+}\r
+\r
+static int\r
+compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)\r
+{\r
+ PyTypeObject *newbase, *oldbase;\r
+\r
+ if (newto->tp_dealloc != oldto->tp_dealloc ||\r
+ newto->tp_free != oldto->tp_free)\r
+ {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "%s assignment: "\r
+ "'%s' deallocator differs from '%s'",\r
+ attr,\r
+ newto->tp_name,\r
+ oldto->tp_name);\r
+ return 0;\r
+ }\r
+ newbase = newto;\r
+ oldbase = oldto;\r
+ while (equiv_structs(newbase, newbase->tp_base))\r
+ newbase = newbase->tp_base;\r
+ while (equiv_structs(oldbase, oldbase->tp_base))\r
+ oldbase = oldbase->tp_base;\r
+ if (newbase != oldbase &&\r
+ (newbase->tp_base != oldbase->tp_base ||\r
+ !same_slots_added(newbase, oldbase))) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "%s assignment: "\r
+ "'%s' object layout differs from '%s'",\r
+ attr,\r
+ newto->tp_name,\r
+ oldto->tp_name);\r
+ return 0;\r
+ }\r
+\r
+ return 1;\r
+}\r
+\r
+static int\r
+object_set_class(PyObject *self, PyObject *value, void *closure)\r
+{\r
+ PyTypeObject *oldto = Py_TYPE(self);\r
+ PyTypeObject *newto;\r
+\r
+ if (value == NULL) {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "can't delete __class__ attribute");\r
+ return -1;\r
+ }\r
+ if (!PyType_Check(value)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "__class__ must be set to new-style class, not '%s' object",\r
+ Py_TYPE(value)->tp_name);\r
+ return -1;\r
+ }\r
+ newto = (PyTypeObject *)value;\r
+ if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||\r
+ !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))\r
+ {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "__class__ assignment: only for heap types");\r
+ return -1;\r
+ }\r
+ if (compatible_for_assignment(newto, oldto, "__class__")) {\r
+ Py_INCREF(newto);\r
+ Py_TYPE(self) = newto;\r
+ Py_DECREF(oldto);\r
+ return 0;\r
+ }\r
+ else {\r
+ return -1;\r
+ }\r
+}\r
+\r
+static PyGetSetDef object_getsets[] = {\r
+ {"__class__", object_get_class, object_set_class,\r
+ PyDoc_STR("the object's class")},\r
+ {0}\r
+};\r
+\r
+\r
+/* Stuff to implement __reduce_ex__ for pickle protocols >= 2.\r
+ We fall back to helpers in copy_reg for:\r
+ - pickle protocols < 2\r
+ - calculating the list of slot names (done only once per class)\r
+ - the __newobj__ function (which is used as a token but never called)\r
+*/\r
+\r
+static PyObject *\r
+import_copyreg(void)\r
+{\r
+ static PyObject *copyreg_str;\r
+\r
+ if (!copyreg_str) {\r
+ copyreg_str = PyString_InternFromString("copy_reg");\r
+ if (copyreg_str == NULL)\r
+ return NULL;\r
+ }\r
+\r
+ return PyImport_Import(copyreg_str);\r
+}\r
+\r
+static PyObject *\r
+slotnames(PyObject *cls)\r
+{\r
+ PyObject *clsdict;\r
+ PyObject *copyreg;\r
+ PyObject *slotnames;\r
+\r
+ if (!PyType_Check(cls)) {\r
+ Py_INCREF(Py_None);\r
+ return Py_None;\r
+ }\r
+\r
+ clsdict = ((PyTypeObject *)cls)->tp_dict;\r
+ slotnames = PyDict_GetItemString(clsdict, "__slotnames__");\r
+ if (slotnames != NULL && PyList_Check(slotnames)) {\r
+ Py_INCREF(slotnames);\r
+ return slotnames;\r
+ }\r
+\r
+ copyreg = import_copyreg();\r
+ if (copyreg == NULL)\r
+ return NULL;\r
+\r
+ slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);\r
+ Py_DECREF(copyreg);\r
+ if (slotnames != NULL &&\r
+ slotnames != Py_None &&\r
+ !PyList_Check(slotnames))\r
+ {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "copy_reg._slotnames didn't return a list or None");\r
+ Py_DECREF(slotnames);\r
+ slotnames = NULL;\r
+ }\r
+\r
+ return slotnames;\r
+}\r
+\r
+static PyObject *\r
+reduce_2(PyObject *obj)\r
+{\r
+ PyObject *cls, *getnewargs;\r
+ PyObject *args = NULL, *args2 = NULL;\r
+ PyObject *getstate = NULL, *state = NULL, *names = NULL;\r
+ PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;\r
+ PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;\r
+ Py_ssize_t i, n;\r
+\r
+ cls = PyObject_GetAttrString(obj, "__class__");\r
+ if (cls == NULL)\r
+ return NULL;\r
+\r
+ getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");\r
+ if (getnewargs != NULL) {\r
+ args = PyObject_CallObject(getnewargs, NULL);\r
+ Py_DECREF(getnewargs);\r
+ if (args != NULL && !PyTuple_Check(args)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "__getnewargs__ should return a tuple, "\r
+ "not '%.200s'", Py_TYPE(args)->tp_name);\r
+ goto end;\r
+ }\r
+ }\r
+ else {\r
+ PyErr_Clear();\r
+ args = PyTuple_New(0);\r
+ }\r
+ if (args == NULL)\r
+ goto end;\r
+\r
+ getstate = PyObject_GetAttrString(obj, "__getstate__");\r
+ if (getstate != NULL) {\r
+ state = PyObject_CallObject(getstate, NULL);\r
+ Py_DECREF(getstate);\r
+ if (state == NULL)\r
+ goto end;\r
+ }\r
+ else {\r
+ PyErr_Clear();\r
+ state = PyObject_GetAttrString(obj, "__dict__");\r
+ if (state == NULL) {\r
+ PyErr_Clear();\r
+ state = Py_None;\r
+ Py_INCREF(state);\r
+ }\r
+ names = slotnames(cls);\r
+ if (names == NULL)\r
+ goto end;\r
+ if (names != Py_None) {\r
+ assert(PyList_Check(names));\r
+ slots = PyDict_New();\r
+ if (slots == NULL)\r
+ goto end;\r
+ n = 0;\r
+ /* Can't pre-compute the list size; the list\r
+ is stored on the class so accessible to other\r
+ threads, which may be run by DECREF */\r
+ for (i = 0; i < PyList_GET_SIZE(names); i++) {\r
+ PyObject *name, *value;\r
+ name = PyList_GET_ITEM(names, i);\r
+ value = PyObject_GetAttr(obj, name);\r
+ if (value == NULL)\r
+ PyErr_Clear();\r
+ else {\r
+ int err = PyDict_SetItem(slots, name,\r
+ value);\r
+ Py_DECREF(value);\r
+ if (err)\r
+ goto end;\r
+ n++;\r
+ }\r
+ }\r
+ if (n) {\r
+ state = Py_BuildValue("(NO)", state, slots);\r
+ if (state == NULL)\r
+ goto end;\r
+ }\r
+ }\r
+ }\r
+\r
+ if (!PyList_Check(obj)) {\r
+ listitems = Py_None;\r
+ Py_INCREF(listitems);\r
+ }\r
+ else {\r
+ listitems = PyObject_GetIter(obj);\r
+ if (listitems == NULL)\r
+ goto end;\r
+ }\r
+\r
+ if (!PyDict_Check(obj)) {\r
+ dictitems = Py_None;\r
+ Py_INCREF(dictitems);\r
+ }\r
+ else {\r
+ dictitems = PyObject_CallMethod(obj, "iteritems", "");\r
+ if (dictitems == NULL)\r
+ goto end;\r
+ }\r
+\r
+ copyreg = import_copyreg();\r
+ if (copyreg == NULL)\r
+ goto end;\r
+ newobj = PyObject_GetAttrString(copyreg, "__newobj__");\r
+ if (newobj == NULL)\r
+ goto end;\r
+\r
+ n = PyTuple_GET_SIZE(args);\r
+ args2 = PyTuple_New(n+1);\r
+ if (args2 == NULL)\r
+ goto end;\r
+ PyTuple_SET_ITEM(args2, 0, cls);\r
+ cls = NULL;\r
+ for (i = 0; i < n; i++) {\r
+ PyObject *v = PyTuple_GET_ITEM(args, i);\r
+ Py_INCREF(v);\r
+ PyTuple_SET_ITEM(args2, i+1, v);\r
+ }\r
+\r
+ res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);\r
+\r
+ end:\r
+ Py_XDECREF(cls);\r
+ Py_XDECREF(args);\r
+ Py_XDECREF(args2);\r
+ Py_XDECREF(slots);\r
+ Py_XDECREF(state);\r
+ Py_XDECREF(names);\r
+ Py_XDECREF(listitems);\r
+ Py_XDECREF(dictitems);\r
+ Py_XDECREF(copyreg);\r
+ Py_XDECREF(newobj);\r
+ return res;\r
+}\r
+\r
+/*\r
+ * There were two problems when object.__reduce__ and object.__reduce_ex__\r
+ * were implemented in the same function:\r
+ * - trying to pickle an object with a custom __reduce__ method that\r
+ * fell back to object.__reduce__ in certain circumstances led to\r
+ * infinite recursion at Python level and eventual RuntimeError.\r
+ * - Pickling objects that lied about their type by overwriting the\r
+ * __class__ descriptor could lead to infinite recursion at C level\r
+ * and eventual segfault.\r
+ *\r
+ * Because of backwards compatibility, the two methods still have to\r
+ * behave in the same way, even if this is not required by the pickle\r
+ * protocol. This common functionality was moved to the _common_reduce\r
+ * function.\r
+ */\r
+static PyObject *\r
+_common_reduce(PyObject *self, int proto)\r
+{\r
+ PyObject *copyreg, *res;\r
+\r
+ if (proto >= 2)\r
+ return reduce_2(self);\r
+\r
+ copyreg = import_copyreg();\r
+ if (!copyreg)\r
+ return NULL;\r
+\r
+ res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);\r
+ Py_DECREF(copyreg);\r
+\r
+ return res;\r
+}\r
+\r
+static PyObject *\r
+object_reduce(PyObject *self, PyObject *args)\r
+{\r
+ int proto = 0;\r
+\r
+ if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))\r
+ return NULL;\r
+\r
+ return _common_reduce(self, proto);\r
+}\r
+\r
+static PyObject *\r
+object_reduce_ex(PyObject *self, PyObject *args)\r
+{\r
+ PyObject *reduce, *res;\r
+ int proto = 0;\r
+\r
+ if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))\r
+ return NULL;\r
+\r
+ reduce = PyObject_GetAttrString(self, "__reduce__");\r
+ if (reduce == NULL)\r
+ PyErr_Clear();\r
+ else {\r
+ PyObject *cls, *clsreduce, *objreduce;\r
+ int override;\r
+ cls = PyObject_GetAttrString(self, "__class__");\r
+ if (cls == NULL) {\r
+ Py_DECREF(reduce);\r
+ return NULL;\r
+ }\r
+ clsreduce = PyObject_GetAttrString(cls, "__reduce__");\r
+ Py_DECREF(cls);\r
+ if (clsreduce == NULL) {\r
+ Py_DECREF(reduce);\r
+ return NULL;\r
+ }\r
+ objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,\r
+ "__reduce__");\r
+ override = (clsreduce != objreduce);\r
+ Py_DECREF(clsreduce);\r
+ if (override) {\r
+ res = PyObject_CallObject(reduce, NULL);\r
+ Py_DECREF(reduce);\r
+ return res;\r
+ }\r
+ else\r
+ Py_DECREF(reduce);\r
+ }\r
+\r
+ return _common_reduce(self, proto);\r
+}\r
+\r
+static PyObject *\r
+object_subclasshook(PyObject *cls, PyObject *args)\r
+{\r
+ Py_INCREF(Py_NotImplemented);\r
+ return Py_NotImplemented;\r
+}\r
+\r
+PyDoc_STRVAR(object_subclasshook_doc,\r
+"Abstract classes can override this to customize issubclass().\n"\r
+"\n"\r
+"This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"\r
+"It should return True, False or NotImplemented. If it returns\n"\r
+"NotImplemented, the normal algorithm is used. Otherwise, it\n"\r
+"overrides the normal algorithm (and the outcome is cached).\n");\r
+\r
+/*\r
+ from PEP 3101, this code implements:\r
+\r
+ class object:\r
+ def __format__(self, format_spec):\r
+ if isinstance(format_spec, str):\r
+ return format(str(self), format_spec)\r
+ elif isinstance(format_spec, unicode):\r
+ return format(unicode(self), format_spec)\r
+*/\r
+static PyObject *\r
+object_format(PyObject *self, PyObject *args)\r
+{\r
+ PyObject *format_spec;\r
+ PyObject *self_as_str = NULL;\r
+ PyObject *result = NULL;\r
+ Py_ssize_t format_len;\r
+\r
+ if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))\r
+ return NULL;\r
+#ifdef Py_USING_UNICODE\r
+ if (PyUnicode_Check(format_spec)) {\r
+ format_len = PyUnicode_GET_SIZE(format_spec);\r
+ self_as_str = PyObject_Unicode(self);\r
+ } else if (PyString_Check(format_spec)) {\r
+#else\r
+ if (PyString_Check(format_spec)) {\r
+#endif\r
+ format_len = PyString_GET_SIZE(format_spec);\r
+ self_as_str = PyObject_Str(self);\r
+ } else {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "argument to __format__ must be unicode or str");\r
+ return NULL;\r
+ }\r
+\r
+ if (self_as_str != NULL) {\r
+ /* Issue 7994: If we're converting to a string, we\r
+ should reject format specifications */\r
+ if (format_len > 0) {\r
+ if (PyErr_WarnEx(PyExc_PendingDeprecationWarning,\r
+ "object.__format__ with a non-empty format "\r
+ "string is deprecated", 1) < 0) {\r
+ goto done;\r
+ }\r
+ /* Eventually this will become an error:\r
+ PyErr_Format(PyExc_TypeError,\r
+ "non-empty format string passed to object.__format__");\r
+ goto done;\r
+ */\r
+ }\r
+ result = PyObject_Format(self_as_str, format_spec);\r
+ }\r
+\r
+done:\r
+ Py_XDECREF(self_as_str);\r
+\r
+ return result;\r
+}\r
+\r
+static PyObject *\r
+object_sizeof(PyObject *self, PyObject *args)\r
+{\r
+ Py_ssize_t res, isize;\r
+\r
+ res = 0;\r
+ isize = self->ob_type->tp_itemsize;\r
+ if (isize > 0)\r
+ res = Py_SIZE(self) * isize;\r
+ res += self->ob_type->tp_basicsize;\r
+\r
+ return PyInt_FromSsize_t(res);\r
+}\r
+\r
+static PyMethodDef object_methods[] = {\r
+ {"__reduce_ex__", object_reduce_ex, METH_VARARGS,\r
+ PyDoc_STR("helper for pickle")},\r
+ {"__reduce__", object_reduce, METH_VARARGS,\r
+ PyDoc_STR("helper for pickle")},\r
+ {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,\r
+ object_subclasshook_doc},\r
+ {"__format__", object_format, METH_VARARGS,\r
+ PyDoc_STR("default object formatter")},\r
+ {"__sizeof__", object_sizeof, METH_NOARGS,\r
+ PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")},\r
+ {0}\r
+};\r
+\r
+\r
+PyTypeObject PyBaseObject_Type = {\r
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)\r
+ "object", /* tp_name */\r
+ sizeof(PyObject), /* tp_basicsize */\r
+ 0, /* tp_itemsize */\r
+ object_dealloc, /* tp_dealloc */\r
+ 0, /* tp_print */\r
+ 0, /* tp_getattr */\r
+ 0, /* tp_setattr */\r
+ 0, /* tp_compare */\r
+ object_repr, /* tp_repr */\r
+ 0, /* tp_as_number */\r
+ 0, /* tp_as_sequence */\r
+ 0, /* tp_as_mapping */\r
+ (hashfunc)_Py_HashPointer, /* tp_hash */\r
+ 0, /* tp_call */\r
+ object_str, /* tp_str */\r
+ PyObject_GenericGetAttr, /* tp_getattro */\r
+ PyObject_GenericSetAttr, /* tp_setattro */\r
+ 0, /* tp_as_buffer */\r
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */\r
+ PyDoc_STR("The most base type"), /* tp_doc */\r
+ 0, /* tp_traverse */\r
+ 0, /* tp_clear */\r
+ 0, /* tp_richcompare */\r
+ 0, /* tp_weaklistoffset */\r
+ 0, /* tp_iter */\r
+ 0, /* tp_iternext */\r
+ object_methods, /* tp_methods */\r
+ 0, /* tp_members */\r
+ object_getsets, /* tp_getset */\r
+ 0, /* tp_base */\r
+ 0, /* tp_dict */\r
+ 0, /* tp_descr_get */\r
+ 0, /* tp_descr_set */\r
+ 0, /* tp_dictoffset */\r
+ object_init, /* tp_init */\r
+ PyType_GenericAlloc, /* tp_alloc */\r
+ object_new, /* tp_new */\r
+ PyObject_Del, /* tp_free */\r
+};\r
+\r
+\r
+/* Initialize the __dict__ in a type object */\r
+\r
+static int\r
+add_methods(PyTypeObject *type, PyMethodDef *meth)\r
+{\r
+ PyObject *dict = type->tp_dict;\r
+\r
+ for (; meth->ml_name != NULL; meth++) {\r
+ PyObject *descr;\r
+ int err;\r
+ if (PyDict_GetItemString(dict, meth->ml_name) &&\r
+ !(meth->ml_flags & METH_COEXIST))\r
+ continue;\r
+ if (meth->ml_flags & METH_CLASS) {\r
+ if (meth->ml_flags & METH_STATIC) {\r
+ PyErr_SetString(PyExc_ValueError,\r
+ "method cannot be both class and static");\r
+ return -1;\r
+ }\r
+ descr = PyDescr_NewClassMethod(type, meth);\r
+ }\r
+ else if (meth->ml_flags & METH_STATIC) {\r
+ PyObject *cfunc = PyCFunction_New(meth, NULL);\r
+ if (cfunc == NULL)\r
+ return -1;\r
+ descr = PyStaticMethod_New(cfunc);\r
+ Py_DECREF(cfunc);\r
+ }\r
+ else {\r
+ descr = PyDescr_NewMethod(type, meth);\r
+ }\r
+ if (descr == NULL)\r
+ return -1;\r
+ err = PyDict_SetItemString(dict, meth->ml_name, descr);\r
+ Py_DECREF(descr);\r
+ if (err < 0)\r
+ return -1;\r
+ }\r
+ return 0;\r
+}\r
+\r
+static int\r
+add_members(PyTypeObject *type, PyMemberDef *memb)\r
+{\r
+ PyObject *dict = type->tp_dict;\r
+\r
+ for (; memb->name != NULL; memb++) {\r
+ PyObject *descr;\r
+ if (PyDict_GetItemString(dict, memb->name))\r
+ continue;\r
+ descr = PyDescr_NewMember(type, memb);\r
+ if (descr == NULL)\r
+ return -1;\r
+ if (PyDict_SetItemString(dict, memb->name, descr) < 0)\r
+ return -1;\r
+ Py_DECREF(descr);\r
+ }\r
+ return 0;\r
+}\r
+\r
+static int\r
+add_getset(PyTypeObject *type, PyGetSetDef *gsp)\r
+{\r
+ PyObject *dict = type->tp_dict;\r
+\r
+ for (; gsp->name != NULL; gsp++) {\r
+ PyObject *descr;\r
+ if (PyDict_GetItemString(dict, gsp->name))\r
+ continue;\r
+ descr = PyDescr_NewGetSet(type, gsp);\r
+\r
+ if (descr == NULL)\r
+ return -1;\r
+ if (PyDict_SetItemString(dict, gsp->name, descr) < 0)\r
+ return -1;\r
+ Py_DECREF(descr);\r
+ }\r
+ return 0;\r
+}\r
+\r
+#define BUFFER_FLAGS (Py_TPFLAGS_HAVE_GETCHARBUFFER | Py_TPFLAGS_HAVE_NEWBUFFER)\r
+\r
+static void\r
+inherit_special(PyTypeObject *type, PyTypeObject *base)\r
+{\r
+ Py_ssize_t oldsize, newsize;\r
+\r
+ /* Special flag magic */\r
+ if (!type->tp_as_buffer && base->tp_as_buffer) {\r
+ type->tp_flags &= ~BUFFER_FLAGS;\r
+ type->tp_flags |=\r
+ base->tp_flags & BUFFER_FLAGS;\r
+ }\r
+ if (!type->tp_as_sequence && base->tp_as_sequence) {\r
+ type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;\r
+ type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;\r
+ }\r
+ if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=\r
+ (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {\r
+ if ((!type->tp_as_number && base->tp_as_number) ||\r
+ (!type->tp_as_sequence && base->tp_as_sequence)) {\r
+ type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;\r
+ if (!type->tp_as_number && !type->tp_as_sequence) {\r
+ type->tp_flags |= base->tp_flags &\r
+ Py_TPFLAGS_HAVE_INPLACEOPS;\r
+ }\r
+ }\r
+ /* Wow */\r
+ }\r
+ if (!type->tp_as_number && base->tp_as_number) {\r
+ type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;\r
+ type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;\r
+ }\r
+\r
+ /* Copying basicsize is connected to the GC flags */\r
+ oldsize = base->tp_basicsize;\r
+ newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;\r
+ if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&\r
+ (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&\r
+ (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&\r
+ (!type->tp_traverse && !type->tp_clear)) {\r
+ type->tp_flags |= Py_TPFLAGS_HAVE_GC;\r
+ if (type->tp_traverse == NULL)\r
+ type->tp_traverse = base->tp_traverse;\r
+ if (type->tp_clear == NULL)\r
+ type->tp_clear = base->tp_clear;\r
+ }\r
+ if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {\r
+ /* The condition below could use some explanation.\r
+ It appears that tp_new is not inherited for static types\r
+ whose base class is 'object'; this seems to be a precaution\r
+ so that old extension types don't suddenly become\r
+ callable (object.__new__ wouldn't insure the invariants\r
+ that the extension type's own factory function ensures).\r
+ Heap types, of course, are under our control, so they do\r
+ inherit tp_new; static extension types that specify some\r
+ other built-in type as the default are considered\r
+ new-style-aware so they also inherit object.__new__. */\r
+ if (base != &PyBaseObject_Type ||\r
+ (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {\r
+ if (type->tp_new == NULL)\r
+ type->tp_new = base->tp_new;\r
+ }\r
+ }\r
+ type->tp_basicsize = newsize;\r
+\r
+ /* Copy other non-function slots */\r
+\r
+#undef COPYVAL\r
+#define COPYVAL(SLOT) \\r
+ if (type->SLOT == 0) type->SLOT = base->SLOT\r
+\r
+ COPYVAL(tp_itemsize);\r
+ if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {\r
+ COPYVAL(tp_weaklistoffset);\r
+ }\r
+ if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {\r
+ COPYVAL(tp_dictoffset);\r
+ }\r
+\r
+ /* Setup fast subclass flags */\r
+ if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))\r
+ type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;\r
+ else if (PyType_IsSubtype(base, &PyType_Type))\r
+ type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;\r
+ else if (PyType_IsSubtype(base, &PyInt_Type))\r
+ type->tp_flags |= Py_TPFLAGS_INT_SUBCLASS;\r
+ else if (PyType_IsSubtype(base, &PyLong_Type))\r
+ type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;\r
+ else if (PyType_IsSubtype(base, &PyString_Type))\r
+ type->tp_flags |= Py_TPFLAGS_STRING_SUBCLASS;\r
+#ifdef Py_USING_UNICODE\r
+ else if (PyType_IsSubtype(base, &PyUnicode_Type))\r
+ type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;\r
+#endif\r
+ else if (PyType_IsSubtype(base, &PyTuple_Type))\r
+ type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;\r
+ else if (PyType_IsSubtype(base, &PyList_Type))\r
+ type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;\r
+ else if (PyType_IsSubtype(base, &PyDict_Type))\r
+ type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;\r
+}\r
+\r
+static int\r
+overrides_name(PyTypeObject *type, char *name)\r
+{\r
+ PyObject *dict = type->tp_dict;\r
+\r
+ assert(dict != NULL);\r
+ if (PyDict_GetItemString(dict, name) != NULL) {\r
+ return 1;\r
+ }\r
+ return 0;\r
+}\r
+\r
+#define OVERRIDES_HASH(x) overrides_name(x, "__hash__")\r
+#define OVERRIDES_EQ(x) overrides_name(x, "__eq__")\r
+\r
+static void\r
+inherit_slots(PyTypeObject *type, PyTypeObject *base)\r
+{\r
+ PyTypeObject *basebase;\r
+\r
+#undef SLOTDEFINED\r
+#undef COPYSLOT\r
+#undef COPYNUM\r
+#undef COPYSEQ\r
+#undef COPYMAP\r
+#undef COPYBUF\r
+\r
+#define SLOTDEFINED(SLOT) \\r
+ (base->SLOT != 0 && \\r
+ (basebase == NULL || base->SLOT != basebase->SLOT))\r
+\r
+#define COPYSLOT(SLOT) \\r
+ if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT\r
+\r
+#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)\r
+#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)\r
+#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)\r
+#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)\r
+\r
+ /* This won't inherit indirect slots (from tp_as_number etc.)\r
+ if type doesn't provide the space. */\r
+\r
+ if (type->tp_as_number != NULL && base->tp_as_number != NULL) {\r
+ basebase = base->tp_base;\r
+ if (basebase->tp_as_number == NULL)\r
+ basebase = NULL;\r
+ COPYNUM(nb_add);\r
+ COPYNUM(nb_subtract);\r
+ COPYNUM(nb_multiply);\r
+ COPYNUM(nb_divide);\r
+ COPYNUM(nb_remainder);\r
+ COPYNUM(nb_divmod);\r
+ COPYNUM(nb_power);\r
+ COPYNUM(nb_negative);\r
+ COPYNUM(nb_positive);\r
+ COPYNUM(nb_absolute);\r
+ COPYNUM(nb_nonzero);\r
+ COPYNUM(nb_invert);\r
+ COPYNUM(nb_lshift);\r
+ COPYNUM(nb_rshift);\r
+ COPYNUM(nb_and);\r
+ COPYNUM(nb_xor);\r
+ COPYNUM(nb_or);\r
+ COPYNUM(nb_coerce);\r
+ COPYNUM(nb_int);\r
+ COPYNUM(nb_long);\r
+ COPYNUM(nb_float);\r
+ COPYNUM(nb_oct);\r
+ COPYNUM(nb_hex);\r
+ COPYNUM(nb_inplace_add);\r
+ COPYNUM(nb_inplace_subtract);\r
+ COPYNUM(nb_inplace_multiply);\r
+ COPYNUM(nb_inplace_divide);\r
+ COPYNUM(nb_inplace_remainder);\r
+ COPYNUM(nb_inplace_power);\r
+ COPYNUM(nb_inplace_lshift);\r
+ COPYNUM(nb_inplace_rshift);\r
+ COPYNUM(nb_inplace_and);\r
+ COPYNUM(nb_inplace_xor);\r
+ COPYNUM(nb_inplace_or);\r
+ if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {\r
+ COPYNUM(nb_true_divide);\r
+ COPYNUM(nb_floor_divide);\r
+ COPYNUM(nb_inplace_true_divide);\r
+ COPYNUM(nb_inplace_floor_divide);\r
+ }\r
+ if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) {\r
+ COPYNUM(nb_index);\r
+ }\r
+ }\r
+\r
+ if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {\r
+ basebase = base->tp_base;\r
+ if (basebase->tp_as_sequence == NULL)\r
+ basebase = NULL;\r
+ COPYSEQ(sq_length);\r
+ COPYSEQ(sq_concat);\r
+ COPYSEQ(sq_repeat);\r
+ COPYSEQ(sq_item);\r
+ COPYSEQ(sq_slice);\r
+ COPYSEQ(sq_ass_item);\r
+ COPYSEQ(sq_ass_slice);\r
+ COPYSEQ(sq_contains);\r
+ COPYSEQ(sq_inplace_concat);\r
+ COPYSEQ(sq_inplace_repeat);\r
+ }\r
+\r
+ if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {\r
+ basebase = base->tp_base;\r
+ if (basebase->tp_as_mapping == NULL)\r
+ basebase = NULL;\r
+ COPYMAP(mp_length);\r
+ COPYMAP(mp_subscript);\r
+ COPYMAP(mp_ass_subscript);\r
+ }\r
+\r
+ if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {\r
+ basebase = base->tp_base;\r
+ if (basebase->tp_as_buffer == NULL)\r
+ basebase = NULL;\r
+ COPYBUF(bf_getreadbuffer);\r
+ COPYBUF(bf_getwritebuffer);\r
+ COPYBUF(bf_getsegcount);\r
+ COPYBUF(bf_getcharbuffer);\r
+ COPYBUF(bf_getbuffer);\r
+ COPYBUF(bf_releasebuffer);\r
+ }\r
+\r
+ basebase = base->tp_base;\r
+\r
+ COPYSLOT(tp_dealloc);\r
+ COPYSLOT(tp_print);\r
+ if (type->tp_getattr == NULL && type->tp_getattro == NULL) {\r
+ type->tp_getattr = base->tp_getattr;\r
+ type->tp_getattro = base->tp_getattro;\r
+ }\r
+ if (type->tp_setattr == NULL && type->tp_setattro == NULL) {\r
+ type->tp_setattr = base->tp_setattr;\r
+ type->tp_setattro = base->tp_setattro;\r
+ }\r
+ /* tp_compare see tp_richcompare */\r
+ COPYSLOT(tp_repr);\r
+ /* tp_hash see tp_richcompare */\r
+ COPYSLOT(tp_call);\r
+ COPYSLOT(tp_str);\r
+ if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {\r
+ if (type->tp_compare == NULL &&\r
+ type->tp_richcompare == NULL &&\r
+ type->tp_hash == NULL)\r
+ {\r
+ type->tp_compare = base->tp_compare;\r
+ type->tp_richcompare = base->tp_richcompare;\r
+ type->tp_hash = base->tp_hash;\r
+ /* Check for changes to inherited methods in Py3k*/\r
+ if (Py_Py3kWarningFlag) {\r
+ if (base->tp_hash &&\r
+ (base->tp_hash != PyObject_HashNotImplemented) &&\r
+ !OVERRIDES_HASH(type)) {\r
+ if (OVERRIDES_EQ(type)) {\r
+ if (PyErr_WarnPy3k("Overriding "\r
+ "__eq__ blocks inheritance "\r
+ "of __hash__ in 3.x",\r
+ 1) < 0)\r
+ /* XXX This isn't right. If the warning is turned\r
+ into an exception, we should be communicating\r
+ the error back to the caller, but figuring out\r
+ how to clean up in that case is tricky. See\r
+ issue 8627 for more. */\r
+ PyErr_Clear();\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ else {\r
+ COPYSLOT(tp_compare);\r
+ }\r
+ if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {\r
+ COPYSLOT(tp_iter);\r
+ COPYSLOT(tp_iternext);\r
+ }\r
+ if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {\r
+ COPYSLOT(tp_descr_get);\r
+ COPYSLOT(tp_descr_set);\r
+ COPYSLOT(tp_dictoffset);\r
+ COPYSLOT(tp_init);\r
+ COPYSLOT(tp_alloc);\r
+ COPYSLOT(tp_is_gc);\r
+ if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==\r
+ (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {\r
+ /* They agree about gc. */\r
+ COPYSLOT(tp_free);\r
+ }\r
+ else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&\r
+ type->tp_free == NULL &&\r
+ base->tp_free == _PyObject_Del) {\r
+ /* A bit of magic to plug in the correct default\r
+ * tp_free function when a derived class adds gc,\r
+ * didn't define tp_free, and the base uses the\r
+ * default non-gc tp_free.\r
+ */\r
+ type->tp_free = PyObject_GC_Del;\r
+ }\r
+ /* else they didn't agree about gc, and there isn't something\r
+ * obvious to be done -- the type is on its own.\r
+ */\r
+ }\r
+}\r
+\r
+static int add_operators(PyTypeObject *);\r
+\r
+int\r
+PyType_Ready(PyTypeObject *type)\r
+{\r
+ PyObject *dict, *bases;\r
+ PyTypeObject *base;\r
+ Py_ssize_t i, n;\r
+\r
+ if (type->tp_flags & Py_TPFLAGS_READY) {\r
+ assert(type->tp_dict != NULL);\r
+ return 0;\r
+ }\r
+ assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);\r
+\r
+ type->tp_flags |= Py_TPFLAGS_READYING;\r
+\r
+#ifdef Py_TRACE_REFS\r
+ /* PyType_Ready is the closest thing we have to a choke point\r
+ * for type objects, so is the best place I can think of to try\r
+ * to get type objects into the doubly-linked list of all objects.\r
+ * Still, not all type objects go thru PyType_Ready.\r
+ */\r
+ _Py_AddToAllObjects((PyObject *)type, 0);\r
+#endif\r
+\r
+ /* Initialize tp_base (defaults to BaseObject unless that's us) */\r
+ base = type->tp_base;\r
+ if (base == NULL && type != &PyBaseObject_Type) {\r
+ base = type->tp_base = &PyBaseObject_Type;\r
+ Py_INCREF(base);\r
+ }\r
+\r
+ /* Now the only way base can still be NULL is if type is\r
+ * &PyBaseObject_Type.\r
+ */\r
+\r
+ /* Initialize the base class */\r
+ if (base && base->tp_dict == NULL) {\r
+ if (PyType_Ready(base) < 0)\r
+ goto error;\r
+ }\r
+\r
+ /* Initialize ob_type if NULL. This means extensions that want to be\r
+ compilable separately on Windows can call PyType_Ready() instead of\r
+ initializing the ob_type field of their type objects. */\r
+ /* The test for base != NULL is really unnecessary, since base is only\r
+ NULL when type is &PyBaseObject_Type, and we know its ob_type is\r
+ not NULL (it's initialized to &PyType_Type). But coverity doesn't\r
+ know that. */\r
+ if (Py_TYPE(type) == NULL && base != NULL)\r
+ Py_TYPE(type) = Py_TYPE(base);\r
+\r
+ /* Initialize tp_bases */\r
+ bases = type->tp_bases;\r
+ if (bases == NULL) {\r
+ if (base == NULL)\r
+ bases = PyTuple_New(0);\r
+ else\r
+ bases = PyTuple_Pack(1, base);\r
+ if (bases == NULL)\r
+ goto error;\r
+ type->tp_bases = bases;\r
+ }\r
+\r
+ /* Initialize tp_dict */\r
+ dict = type->tp_dict;\r
+ if (dict == NULL) {\r
+ dict = PyDict_New();\r
+ if (dict == NULL)\r
+ goto error;\r
+ type->tp_dict = dict;\r
+ }\r
+\r
+ /* Add type-specific descriptors to tp_dict */\r
+ if (add_operators(type) < 0)\r
+ goto error;\r
+ if (type->tp_methods != NULL) {\r
+ if (add_methods(type, type->tp_methods) < 0)\r
+ goto error;\r
+ }\r
+ if (type->tp_members != NULL) {\r
+ if (add_members(type, type->tp_members) < 0)\r
+ goto error;\r
+ }\r
+ if (type->tp_getset != NULL) {\r
+ if (add_getset(type, type->tp_getset) < 0)\r
+ goto error;\r
+ }\r
+\r
+ /* Calculate method resolution order */\r
+ if (mro_internal(type) < 0) {\r
+ goto error;\r
+ }\r
+\r
+ /* Inherit special flags from dominant base */\r
+ if (type->tp_base != NULL)\r
+ inherit_special(type, type->tp_base);\r
+\r
+ /* Initialize tp_dict properly */\r
+ bases = type->tp_mro;\r
+ assert(bases != NULL);\r
+ assert(PyTuple_Check(bases));\r
+ n = PyTuple_GET_SIZE(bases);\r
+ for (i = 1; i < n; i++) {\r
+ PyObject *b = PyTuple_GET_ITEM(bases, i);\r
+ if (PyType_Check(b))\r
+ inherit_slots(type, (PyTypeObject *)b);\r
+ }\r
+\r
+ /* All bases of statically allocated type should be statically allocated */\r
+ if (Py_Py3kWarningFlag && !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))\r
+ for (i = 0; i < n; i++) {\r
+ PyObject *b = PyTuple_GET_ITEM(bases, i);\r
+ if (PyType_Check(b) &&\r
+ (((PyTypeObject *)b)->tp_flags & Py_TPFLAGS_HEAPTYPE)) {\r
+ char buf[300];\r
+ PyOS_snprintf(buf, sizeof(buf),\r
+ "type '%.100s' is not dynamically allocated but "\r
+ "its base type '%.100s' is dynamically allocated",\r
+ type->tp_name, ((PyTypeObject *)b)->tp_name);\r
+ if (PyErr_WarnPy3k(buf, 1) < 0)\r
+ goto error;\r
+ break;\r
+ }\r
+ }\r
+\r
+ /* Sanity check for tp_free. */\r
+ if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&\r
+ (type->tp_free == NULL || type->tp_free == PyObject_Del)) {\r
+ /* This base class needs to call tp_free, but doesn't have\r
+ * one, or its tp_free is for non-gc'ed objects.\r
+ */\r
+ PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "\r
+ "gc and is a base type but has inappropriate "\r
+ "tp_free slot",\r
+ type->tp_name);\r
+ goto error;\r
+ }\r
+\r
+ /* if the type dictionary doesn't contain a __doc__, set it from\r
+ the tp_doc slot.\r
+ */\r
+ if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {\r
+ if (type->tp_doc != NULL) {\r
+ PyObject *doc = PyString_FromString(type->tp_doc);\r
+ if (doc == NULL)\r
+ goto error;\r
+ PyDict_SetItemString(type->tp_dict, "__doc__", doc);\r
+ Py_DECREF(doc);\r
+ } else {\r
+ PyDict_SetItemString(type->tp_dict,\r
+ "__doc__", Py_None);\r
+ }\r
+ }\r
+\r
+ /* Some more special stuff */\r
+ base = type->tp_base;\r
+ if (base != NULL) {\r
+ if (type->tp_as_number == NULL)\r
+ type->tp_as_number = base->tp_as_number;\r
+ if (type->tp_as_sequence == NULL)\r
+ type->tp_as_sequence = base->tp_as_sequence;\r
+ if (type->tp_as_mapping == NULL)\r
+ type->tp_as_mapping = base->tp_as_mapping;\r
+ if (type->tp_as_buffer == NULL)\r
+ type->tp_as_buffer = base->tp_as_buffer;\r
+ }\r
+\r
+ /* Link into each base class's list of subclasses */\r
+ bases = type->tp_bases;\r
+ n = PyTuple_GET_SIZE(bases);\r
+ for (i = 0; i < n; i++) {\r
+ PyObject *b = PyTuple_GET_ITEM(bases, i);\r
+ if (PyType_Check(b) &&\r
+ add_subclass((PyTypeObject *)b, type) < 0)\r
+ goto error;\r
+ }\r
+\r
+ /* All done -- set the ready flag */\r
+ assert(type->tp_dict != NULL);\r
+ type->tp_flags =\r
+ (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;\r
+ return 0;\r
+\r
+ error:\r
+ type->tp_flags &= ~Py_TPFLAGS_READYING;\r
+ return -1;\r
+}\r
+\r
+static int\r
+add_subclass(PyTypeObject *base, PyTypeObject *type)\r
+{\r
+ Py_ssize_t i;\r
+ int result;\r
+ PyObject *list, *ref, *newobj;\r
+\r
+ list = base->tp_subclasses;\r
+ if (list == NULL) {\r
+ base->tp_subclasses = list = PyList_New(0);\r
+ if (list == NULL)\r
+ return -1;\r
+ }\r
+ assert(PyList_Check(list));\r
+ newobj = PyWeakref_NewRef((PyObject *)type, NULL);\r
+ i = PyList_GET_SIZE(list);\r
+ while (--i >= 0) {\r
+ ref = PyList_GET_ITEM(list, i);\r
+ assert(PyWeakref_CheckRef(ref));\r
+ if (PyWeakref_GET_OBJECT(ref) == Py_None)\r
+ return PyList_SetItem(list, i, newobj);\r
+ }\r
+ result = PyList_Append(list, newobj);\r
+ Py_DECREF(newobj);\r
+ return result;\r
+}\r
+\r
+static void\r
+remove_subclass(PyTypeObject *base, PyTypeObject *type)\r
+{\r
+ Py_ssize_t i;\r
+ PyObject *list, *ref;\r
+\r
+ list = base->tp_subclasses;\r
+ if (list == NULL) {\r
+ return;\r
+ }\r
+ assert(PyList_Check(list));\r
+ i = PyList_GET_SIZE(list);\r
+ while (--i >= 0) {\r
+ ref = PyList_GET_ITEM(list, i);\r
+ assert(PyWeakref_CheckRef(ref));\r
+ if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {\r
+ /* this can't fail, right? */\r
+ PySequence_DelItem(list, i);\r
+ return;\r
+ }\r
+ }\r
+}\r
+\r
+static int\r
+check_num_args(PyObject *ob, int n)\r
+{\r
+ if (!PyTuple_CheckExact(ob)) {\r
+ PyErr_SetString(PyExc_SystemError,\r
+ "PyArg_UnpackTuple() argument list is not a tuple");\r
+ return 0;\r
+ }\r
+ if (n == PyTuple_GET_SIZE(ob))\r
+ return 1;\r
+ PyErr_Format(\r
+ PyExc_TypeError,\r
+ "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));\r
+ return 0;\r
+}\r
+\r
+/* Generic wrappers for overloadable 'operators' such as __getitem__ */\r
+\r
+/* There's a wrapper *function* for each distinct function typedef used\r
+ for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a\r
+ wrapper *table* for each distinct operation (e.g. __len__, __add__).\r
+ Most tables have only one entry; the tables for binary operators have two\r
+ entries, one regular and one with reversed arguments. */\r
+\r
+static PyObject *\r
+wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ lenfunc func = (lenfunc)wrapped;\r
+ Py_ssize_t res;\r
+\r
+ if (!check_num_args(args, 0))\r
+ return NULL;\r
+ res = (*func)(self);\r
+ if (res == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ return PyInt_FromLong((long)res);\r
+}\r
+\r
+static PyObject *\r
+wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ inquiry func = (inquiry)wrapped;\r
+ int res;\r
+\r
+ if (!check_num_args(args, 0))\r
+ return NULL;\r
+ res = (*func)(self);\r
+ if (res == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ return PyBool_FromLong((long)res);\r
+}\r
+\r
+static PyObject *\r
+wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ binaryfunc func = (binaryfunc)wrapped;\r
+ PyObject *other;\r
+\r
+ if (!check_num_args(args, 1))\r
+ return NULL;\r
+ other = PyTuple_GET_ITEM(args, 0);\r
+ return (*func)(self, other);\r
+}\r
+\r
+static PyObject *\r
+wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ binaryfunc func = (binaryfunc)wrapped;\r
+ PyObject *other;\r
+\r
+ if (!check_num_args(args, 1))\r
+ return NULL;\r
+ other = PyTuple_GET_ITEM(args, 0);\r
+ if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&\r
+ !PyType_IsSubtype(other->ob_type, self->ob_type)) {\r
+ Py_INCREF(Py_NotImplemented);\r
+ return Py_NotImplemented;\r
+ }\r
+ return (*func)(self, other);\r
+}\r
+\r
+static PyObject *\r
+wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ binaryfunc func = (binaryfunc)wrapped;\r
+ PyObject *other;\r
+\r
+ if (!check_num_args(args, 1))\r
+ return NULL;\r
+ other = PyTuple_GET_ITEM(args, 0);\r
+ if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&\r
+ !PyType_IsSubtype(other->ob_type, self->ob_type)) {\r
+ Py_INCREF(Py_NotImplemented);\r
+ return Py_NotImplemented;\r
+ }\r
+ return (*func)(other, self);\r
+}\r
+\r
+static PyObject *\r
+wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ coercion func = (coercion)wrapped;\r
+ PyObject *other, *res;\r
+ int ok;\r
+\r
+ if (!check_num_args(args, 1))\r
+ return NULL;\r
+ other = PyTuple_GET_ITEM(args, 0);\r
+ ok = func(&self, &other);\r
+ if (ok < 0)\r
+ return NULL;\r
+ if (ok > 0) {\r
+ Py_INCREF(Py_NotImplemented);\r
+ return Py_NotImplemented;\r
+ }\r
+ res = PyTuple_New(2);\r
+ if (res == NULL) {\r
+ Py_DECREF(self);\r
+ Py_DECREF(other);\r
+ return NULL;\r
+ }\r
+ PyTuple_SET_ITEM(res, 0, self);\r
+ PyTuple_SET_ITEM(res, 1, other);\r
+ return res;\r
+}\r
+\r
+static PyObject *\r
+wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ ternaryfunc func = (ternaryfunc)wrapped;\r
+ PyObject *other;\r
+ PyObject *third = Py_None;\r
+\r
+ /* Note: This wrapper only works for __pow__() */\r
+\r
+ if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))\r
+ return NULL;\r
+ return (*func)(self, other, third);\r
+}\r
+\r
+static PyObject *\r
+wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ ternaryfunc func = (ternaryfunc)wrapped;\r
+ PyObject *other;\r
+ PyObject *third = Py_None;\r
+\r
+ /* Note: This wrapper only works for __pow__() */\r
+\r
+ if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))\r
+ return NULL;\r
+ return (*func)(other, self, third);\r
+}\r
+\r
+static PyObject *\r
+wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ unaryfunc func = (unaryfunc)wrapped;\r
+\r
+ if (!check_num_args(args, 0))\r
+ return NULL;\r
+ return (*func)(self);\r
+}\r
+\r
+static PyObject *\r
+wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ ssizeargfunc func = (ssizeargfunc)wrapped;\r
+ PyObject* o;\r
+ Py_ssize_t i;\r
+\r
+ if (!PyArg_UnpackTuple(args, "", 1, 1, &o))\r
+ return NULL;\r
+ i = PyNumber_AsSsize_t(o, PyExc_OverflowError);\r
+ if (i == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ return (*func)(self, i);\r
+}\r
+\r
+static Py_ssize_t\r
+getindex(PyObject *self, PyObject *arg)\r
+{\r
+ Py_ssize_t i;\r
+\r
+ i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);\r
+ if (i == -1 && PyErr_Occurred())\r
+ return -1;\r
+ if (i < 0) {\r
+ PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;\r
+ if (sq && sq->sq_length) {\r
+ Py_ssize_t n = (*sq->sq_length)(self);\r
+ if (n < 0)\r
+ return -1;\r
+ i += n;\r
+ }\r
+ }\r
+ return i;\r
+}\r
+\r
+static PyObject *\r
+wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ ssizeargfunc func = (ssizeargfunc)wrapped;\r
+ PyObject *arg;\r
+ Py_ssize_t i;\r
+\r
+ if (PyTuple_GET_SIZE(args) == 1) {\r
+ arg = PyTuple_GET_ITEM(args, 0);\r
+ i = getindex(self, arg);\r
+ if (i == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ return (*func)(self, i);\r
+ }\r
+ check_num_args(args, 1);\r
+ assert(PyErr_Occurred());\r
+ return NULL;\r
+}\r
+\r
+static PyObject *\r
+wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ ssizessizeargfunc func = (ssizessizeargfunc)wrapped;\r
+ Py_ssize_t i, j;\r
+\r
+ if (!PyArg_ParseTuple(args, "nn", &i, &j))\r
+ return NULL;\r
+ return (*func)(self, i, j);\r
+}\r
+\r
+static PyObject *\r
+wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ ssizeobjargproc func = (ssizeobjargproc)wrapped;\r
+ Py_ssize_t i;\r
+ int res;\r
+ PyObject *arg, *value;\r
+\r
+ if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))\r
+ return NULL;\r
+ i = getindex(self, arg);\r
+ if (i == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ res = (*func)(self, i, value);\r
+ if (res == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ Py_INCREF(Py_None);\r
+ return Py_None;\r
+}\r
+\r
+static PyObject *\r
+wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ ssizeobjargproc func = (ssizeobjargproc)wrapped;\r
+ Py_ssize_t i;\r
+ int res;\r
+ PyObject *arg;\r
+\r
+ if (!check_num_args(args, 1))\r
+ return NULL;\r
+ arg = PyTuple_GET_ITEM(args, 0);\r
+ i = getindex(self, arg);\r
+ if (i == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ res = (*func)(self, i, NULL);\r
+ if (res == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ Py_INCREF(Py_None);\r
+ return Py_None;\r
+}\r
+\r
+static PyObject *\r
+wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;\r
+ Py_ssize_t i, j;\r
+ int res;\r
+ PyObject *value;\r
+\r
+ if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))\r
+ return NULL;\r
+ res = (*func)(self, i, j, value);\r
+ if (res == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ Py_INCREF(Py_None);\r
+ return Py_None;\r
+}\r
+\r
+static PyObject *\r
+wrap_delslice(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;\r
+ Py_ssize_t i, j;\r
+ int res;\r
+\r
+ if (!PyArg_ParseTuple(args, "nn", &i, &j))\r
+ return NULL;\r
+ res = (*func)(self, i, j, NULL);\r
+ if (res == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ Py_INCREF(Py_None);\r
+ return Py_None;\r
+}\r
+\r
+/* XXX objobjproc is a misnomer; should be objargpred */\r
+static PyObject *\r
+wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ objobjproc func = (objobjproc)wrapped;\r
+ int res;\r
+ PyObject *value;\r
+\r
+ if (!check_num_args(args, 1))\r
+ return NULL;\r
+ value = PyTuple_GET_ITEM(args, 0);\r
+ res = (*func)(self, value);\r
+ if (res == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ else\r
+ return PyBool_FromLong(res);\r
+}\r
+\r
+static PyObject *\r
+wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ objobjargproc func = (objobjargproc)wrapped;\r
+ int res;\r
+ PyObject *key, *value;\r
+\r
+ if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))\r
+ return NULL;\r
+ res = (*func)(self, key, value);\r
+ if (res == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ Py_INCREF(Py_None);\r
+ return Py_None;\r
+}\r
+\r
+static PyObject *\r
+wrap_delitem(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ objobjargproc func = (objobjargproc)wrapped;\r
+ int res;\r
+ PyObject *key;\r
+\r
+ if (!check_num_args(args, 1))\r
+ return NULL;\r
+ key = PyTuple_GET_ITEM(args, 0);\r
+ res = (*func)(self, key, NULL);\r
+ if (res == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ Py_INCREF(Py_None);\r
+ return Py_None;\r
+}\r
+\r
+static PyObject *\r
+wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ cmpfunc func = (cmpfunc)wrapped;\r
+ int res;\r
+ PyObject *other;\r
+\r
+ if (!check_num_args(args, 1))\r
+ return NULL;\r
+ other = PyTuple_GET_ITEM(args, 0);\r
+ if (Py_TYPE(other)->tp_compare != func &&\r
+ !PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {\r
+ PyErr_Format(\r
+ PyExc_TypeError,\r
+ "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",\r
+ Py_TYPE(self)->tp_name,\r
+ Py_TYPE(self)->tp_name,\r
+ Py_TYPE(other)->tp_name);\r
+ return NULL;\r
+ }\r
+ res = (*func)(self, other);\r
+ if (PyErr_Occurred())\r
+ return NULL;\r
+ return PyInt_FromLong((long)res);\r
+}\r
+\r
+/* Helper to check for object.__setattr__ or __delattr__ applied to a type.\r
+ This is called the Carlo Verre hack after its discoverer. */\r
+static int\r
+hackcheck(PyObject *self, setattrofunc func, char *what)\r
+{\r
+ PyTypeObject *type = Py_TYPE(self);\r
+ while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)\r
+ type = type->tp_base;\r
+ /* If type is NULL now, this is a really weird type.\r
+ In the spirit of backwards compatibility (?), just shut up. */\r
+ if (type && type->tp_setattro != func) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "can't apply this %s to %s object",\r
+ what,\r
+ type->tp_name);\r
+ return 0;\r
+ }\r
+ return 1;\r
+}\r
+\r
+static PyObject *\r
+wrap_setattr(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ setattrofunc func = (setattrofunc)wrapped;\r
+ int res;\r
+ PyObject *name, *value;\r
+\r
+ if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))\r
+ return NULL;\r
+ if (!hackcheck(self, func, "__setattr__"))\r
+ return NULL;\r
+ res = (*func)(self, name, value);\r
+ if (res < 0)\r
+ return NULL;\r
+ Py_INCREF(Py_None);\r
+ return Py_None;\r
+}\r
+\r
+static PyObject *\r
+wrap_delattr(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ setattrofunc func = (setattrofunc)wrapped;\r
+ int res;\r
+ PyObject *name;\r
+\r
+ if (!check_num_args(args, 1))\r
+ return NULL;\r
+ name = PyTuple_GET_ITEM(args, 0);\r
+ if (!hackcheck(self, func, "__delattr__"))\r
+ return NULL;\r
+ res = (*func)(self, name, NULL);\r
+ if (res < 0)\r
+ return NULL;\r
+ Py_INCREF(Py_None);\r
+ return Py_None;\r
+}\r
+\r
+static PyObject *\r
+wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ hashfunc func = (hashfunc)wrapped;\r
+ long res;\r
+\r
+ if (!check_num_args(args, 0))\r
+ return NULL;\r
+ res = (*func)(self);\r
+ if (res == -1 && PyErr_Occurred())\r
+ return NULL;\r
+ return PyInt_FromLong(res);\r
+}\r
+\r
+static PyObject *\r
+wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)\r
+{\r
+ ternaryfunc func = (ternaryfunc)wrapped;\r
+\r
+ return (*func)(self, args, kwds);\r
+}\r
+\r
+static PyObject *\r
+wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)\r
+{\r
+ richcmpfunc func = (richcmpfunc)wrapped;\r
+ PyObject *other;\r
+\r
+ if (!check_num_args(args, 1))\r
+ return NULL;\r
+ other = PyTuple_GET_ITEM(args, 0);\r
+ return (*func)(self, other, op);\r
+}\r
+\r
+#undef RICHCMP_WRAPPER\r
+#define RICHCMP_WRAPPER(NAME, OP) \\r
+static PyObject * \\r
+richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \\r
+{ \\r
+ return wrap_richcmpfunc(self, args, wrapped, OP); \\r
+}\r
+\r
+RICHCMP_WRAPPER(lt, Py_LT)\r
+RICHCMP_WRAPPER(le, Py_LE)\r
+RICHCMP_WRAPPER(eq, Py_EQ)\r
+RICHCMP_WRAPPER(ne, Py_NE)\r
+RICHCMP_WRAPPER(gt, Py_GT)\r
+RICHCMP_WRAPPER(ge, Py_GE)\r
+\r
+static PyObject *\r
+wrap_next(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ unaryfunc func = (unaryfunc)wrapped;\r
+ PyObject *res;\r
+\r
+ if (!check_num_args(args, 0))\r
+ return NULL;\r
+ res = (*func)(self);\r
+ if (res == NULL && !PyErr_Occurred())\r
+ PyErr_SetNone(PyExc_StopIteration);\r
+ return res;\r
+}\r
+\r
+static PyObject *\r
+wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ descrgetfunc func = (descrgetfunc)wrapped;\r
+ PyObject *obj;\r
+ PyObject *type = NULL;\r
+\r
+ if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))\r
+ return NULL;\r
+ if (obj == Py_None)\r
+ obj = NULL;\r
+ if (type == Py_None)\r
+ type = NULL;\r
+ if (type == NULL &&obj == NULL) {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "__get__(None, None) is invalid");\r
+ return NULL;\r
+ }\r
+ return (*func)(self, obj, type);\r
+}\r
+\r
+static PyObject *\r
+wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ descrsetfunc func = (descrsetfunc)wrapped;\r
+ PyObject *obj, *value;\r
+ int ret;\r
+\r
+ if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))\r
+ return NULL;\r
+ ret = (*func)(self, obj, value);\r
+ if (ret < 0)\r
+ return NULL;\r
+ Py_INCREF(Py_None);\r
+ return Py_None;\r
+}\r
+\r
+static PyObject *\r
+wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)\r
+{\r
+ descrsetfunc func = (descrsetfunc)wrapped;\r
+ PyObject *obj;\r
+ int ret;\r
+\r
+ if (!check_num_args(args, 1))\r
+ return NULL;\r
+ obj = PyTuple_GET_ITEM(args, 0);\r
+ ret = (*func)(self, obj, NULL);\r
+ if (ret < 0)\r
+ return NULL;\r
+ Py_INCREF(Py_None);\r
+ return Py_None;\r
+}\r
+\r
+static PyObject *\r
+wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)\r
+{\r
+ initproc func = (initproc)wrapped;\r
+\r
+ if (func(self, args, kwds) < 0)\r
+ return NULL;\r
+ Py_INCREF(Py_None);\r
+ return Py_None;\r
+}\r
+\r
+static PyObject *\r
+tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)\r
+{\r
+ PyTypeObject *type, *subtype, *staticbase;\r
+ PyObject *arg0, *res;\r
+\r
+ if (self == NULL || !PyType_Check(self))\r
+ Py_FatalError("__new__() called with non-type 'self'");\r
+ type = (PyTypeObject *)self;\r
+ if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "%s.__new__(): not enough arguments",\r
+ type->tp_name);\r
+ return NULL;\r
+ }\r
+ arg0 = PyTuple_GET_ITEM(args, 0);\r
+ if (!PyType_Check(arg0)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "%s.__new__(X): X is not a type object (%s)",\r
+ type->tp_name,\r
+ Py_TYPE(arg0)->tp_name);\r
+ return NULL;\r
+ }\r
+ subtype = (PyTypeObject *)arg0;\r
+ if (!PyType_IsSubtype(subtype, type)) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "%s.__new__(%s): %s is not a subtype of %s",\r
+ type->tp_name,\r
+ subtype->tp_name,\r
+ subtype->tp_name,\r
+ type->tp_name);\r
+ return NULL;\r
+ }\r
+\r
+ /* Check that the use doesn't do something silly and unsafe like\r
+ object.__new__(dict). To do this, we check that the\r
+ most derived base that's not a heap type is this type. */\r
+ staticbase = subtype;\r
+ while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))\r
+ staticbase = staticbase->tp_base;\r
+ /* If staticbase is NULL now, it is a really weird type.\r
+ In the spirit of backwards compatibility (?), just shut up. */\r
+ if (staticbase && staticbase->tp_new != type->tp_new) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "%s.__new__(%s) is not safe, use %s.__new__()",\r
+ type->tp_name,\r
+ subtype->tp_name,\r
+ staticbase->tp_name);\r
+ return NULL;\r
+ }\r
+\r
+ args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));\r
+ if (args == NULL)\r
+ return NULL;\r
+ res = type->tp_new(subtype, args, kwds);\r
+ Py_DECREF(args);\r
+ return res;\r
+}\r
+\r
+static struct PyMethodDef tp_new_methoddef[] = {\r
+ {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS,\r
+ PyDoc_STR("T.__new__(S, ...) -> "\r
+ "a new object with type S, a subtype of T")},\r
+ {0}\r
+};\r
+\r
+static int\r
+add_tp_new_wrapper(PyTypeObject *type)\r
+{\r
+ PyObject *func;\r
+\r
+ if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)\r
+ return 0;\r
+ func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);\r
+ if (func == NULL)\r
+ return -1;\r
+ if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {\r
+ Py_DECREF(func);\r
+ return -1;\r
+ }\r
+ Py_DECREF(func);\r
+ return 0;\r
+}\r
+\r
+/* Slot wrappers that call the corresponding __foo__ slot. See comments\r
+ below at override_slots() for more explanation. */\r
+\r
+#define SLOT0(FUNCNAME, OPSTR) \\r
+static PyObject * \\r
+FUNCNAME(PyObject *self) \\r
+{ \\r
+ static PyObject *cache_str; \\r
+ return call_method(self, OPSTR, &cache_str, "()"); \\r
+}\r
+\r
+#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \\r
+static PyObject * \\r
+FUNCNAME(PyObject *self, ARG1TYPE arg1) \\r
+{ \\r
+ static PyObject *cache_str; \\r
+ return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \\r
+}\r
+\r
+/* Boolean helper for SLOT1BINFULL().\r
+ right.__class__ is a nontrivial subclass of left.__class__. */\r
+static int\r
+method_is_overloaded(PyObject *left, PyObject *right, char *name)\r
+{\r
+ PyObject *a, *b;\r
+ int ok;\r
+\r
+ b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name);\r
+ if (b == NULL) {\r
+ PyErr_Clear();\r
+ /* If right doesn't have it, it's not overloaded */\r
+ return 0;\r
+ }\r
+\r
+ a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name);\r
+ if (a == NULL) {\r
+ PyErr_Clear();\r
+ Py_DECREF(b);\r
+ /* If right has it but left doesn't, it's overloaded */\r
+ return 1;\r
+ }\r
+\r
+ ok = PyObject_RichCompareBool(a, b, Py_NE);\r
+ Py_DECREF(a);\r
+ Py_DECREF(b);\r
+ if (ok < 0) {\r
+ PyErr_Clear();\r
+ return 0;\r
+ }\r
+\r
+ return ok;\r
+}\r
+\r
+\r
+#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \\r
+static PyObject * \\r
+FUNCNAME(PyObject *self, PyObject *other) \\r
+{ \\r
+ static PyObject *cache_str, *rcache_str; \\r
+ int do_other = Py_TYPE(self) != Py_TYPE(other) && \\r
+ Py_TYPE(other)->tp_as_number != NULL && \\r
+ Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \\r
+ if (Py_TYPE(self)->tp_as_number != NULL && \\r
+ Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \\r
+ PyObject *r; \\r
+ if (do_other && \\r
+ PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \\r
+ method_is_overloaded(self, other, ROPSTR)) { \\r
+ r = call_maybe( \\r
+ other, ROPSTR, &rcache_str, "(O)", self); \\r
+ if (r != Py_NotImplemented) \\r
+ return r; \\r
+ Py_DECREF(r); \\r
+ do_other = 0; \\r
+ } \\r
+ r = call_maybe( \\r
+ self, OPSTR, &cache_str, "(O)", other); \\r
+ if (r != Py_NotImplemented || \\r
+ Py_TYPE(other) == Py_TYPE(self)) \\r
+ return r; \\r
+ Py_DECREF(r); \\r
+ } \\r
+ if (do_other) { \\r
+ return call_maybe( \\r
+ other, ROPSTR, &rcache_str, "(O)", self); \\r
+ } \\r
+ Py_INCREF(Py_NotImplemented); \\r
+ return Py_NotImplemented; \\r
+}\r
+\r
+#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \\r
+ SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)\r
+\r
+#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \\r
+static PyObject * \\r
+FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \\r
+{ \\r
+ static PyObject *cache_str; \\r
+ return call_method(self, OPSTR, &cache_str, \\r
+ "(" ARGCODES ")", arg1, arg2); \\r
+}\r
+\r
+static Py_ssize_t\r
+slot_sq_length(PyObject *self)\r
+{\r
+ static PyObject *len_str;\r
+ PyObject *res = call_method(self, "__len__", &len_str, "()");\r
+ Py_ssize_t len;\r
+\r
+ if (res == NULL)\r
+ return -1;\r
+ len = PyInt_AsSsize_t(res);\r
+ Py_DECREF(res);\r
+ if (len < 0) {\r
+ if (!PyErr_Occurred())\r
+ PyErr_SetString(PyExc_ValueError,\r
+ "__len__() should return >= 0");\r
+ return -1;\r
+ }\r
+ return len;\r
+}\r
+\r
+/* Super-optimized version of slot_sq_item.\r
+ Other slots could do the same... */\r
+static PyObject *\r
+slot_sq_item(PyObject *self, Py_ssize_t i)\r
+{\r
+ static PyObject *getitem_str;\r
+ PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;\r
+ descrgetfunc f;\r
+\r
+ if (getitem_str == NULL) {\r
+ getitem_str = PyString_InternFromString("__getitem__");\r
+ if (getitem_str == NULL)\r
+ return NULL;\r
+ }\r
+ func = _PyType_Lookup(Py_TYPE(self), getitem_str);\r
+ if (func != NULL) {\r
+ if ((f = Py_TYPE(func)->tp_descr_get) == NULL)\r
+ Py_INCREF(func);\r
+ else {\r
+ func = f(func, self, (PyObject *)(Py_TYPE(self)));\r
+ if (func == NULL) {\r
+ return NULL;\r
+ }\r
+ }\r
+ ival = PyInt_FromSsize_t(i);\r
+ if (ival != NULL) {\r
+ args = PyTuple_New(1);\r
+ if (args != NULL) {\r
+ PyTuple_SET_ITEM(args, 0, ival);\r
+ retval = PyObject_Call(func, args, NULL);\r
+ Py_XDECREF(args);\r
+ Py_XDECREF(func);\r
+ return retval;\r
+ }\r
+ }\r
+ }\r
+ else {\r
+ PyErr_SetObject(PyExc_AttributeError, getitem_str);\r
+ }\r
+ Py_XDECREF(args);\r
+ Py_XDECREF(ival);\r
+ Py_XDECREF(func);\r
+ return NULL;\r
+}\r
+\r
+static PyObject*\r
+slot_sq_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j)\r
+{\r
+ static PyObject *getslice_str;\r
+\r
+ if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; "\r
+ "use __getitem__", 1) < 0)\r
+ return NULL;\r
+ return call_method(self, "__getslice__", &getslice_str,\r
+ "nn", i, j);\r
+}\r
+\r
+static int\r
+slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)\r
+{\r
+ PyObject *res;\r
+ static PyObject *delitem_str, *setitem_str;\r
+\r
+ if (value == NULL)\r
+ res = call_method(self, "__delitem__", &delitem_str,\r
+ "(n)", index);\r
+ else\r
+ res = call_method(self, "__setitem__", &setitem_str,\r
+ "(nO)", index, value);\r
+ if (res == NULL)\r
+ return -1;\r
+ Py_DECREF(res);\r
+ return 0;\r
+}\r
+\r
+static int\r
+slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value)\r
+{\r
+ PyObject *res;\r
+ static PyObject *delslice_str, *setslice_str;\r
+\r
+ if (value == NULL) {\r
+ if (PyErr_WarnPy3k("in 3.x, __delslice__ has been removed; "\r
+ "use __delitem__", 1) < 0)\r
+ return -1;\r
+ res = call_method(self, "__delslice__", &delslice_str,\r
+ "(nn)", i, j);\r
+ }\r
+ else {\r
+ if (PyErr_WarnPy3k("in 3.x, __setslice__ has been removed; "\r
+ "use __setitem__", 1) < 0)\r
+ return -1;\r
+ res = call_method(self, "__setslice__", &setslice_str,\r
+ "(nnO)", i, j, value);\r
+ }\r
+ if (res == NULL)\r
+ return -1;\r
+ Py_DECREF(res);\r
+ return 0;\r
+}\r
+\r
+static int\r
+slot_sq_contains(PyObject *self, PyObject *value)\r
+{\r
+ PyObject *func, *res, *args;\r
+ int result = -1;\r
+\r
+ static PyObject *contains_str;\r
+\r
+ func = lookup_maybe(self, "__contains__", &contains_str);\r
+ if (func != NULL) {\r
+ args = PyTuple_Pack(1, value);\r
+ if (args == NULL)\r
+ res = NULL;\r
+ else {\r
+ res = PyObject_Call(func, args, NULL);\r
+ Py_DECREF(args);\r
+ }\r
+ Py_DECREF(func);\r
+ if (res != NULL) {\r
+ result = PyObject_IsTrue(res);\r
+ Py_DECREF(res);\r
+ }\r
+ }\r
+ else if (! PyErr_Occurred()) {\r
+ /* Possible results: -1 and 1 */\r
+ result = (int)_PySequence_IterSearch(self, value,\r
+ PY_ITERSEARCH_CONTAINS);\r
+ }\r
+ return result;\r
+}\r
+\r
+#define slot_mp_length slot_sq_length\r
+\r
+SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")\r
+\r
+static int\r
+slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)\r
+{\r
+ PyObject *res;\r
+ static PyObject *delitem_str, *setitem_str;\r
+\r
+ if (value == NULL)\r
+ res = call_method(self, "__delitem__", &delitem_str,\r
+ "(O)", key);\r
+ else\r
+ res = call_method(self, "__setitem__", &setitem_str,\r
+ "(OO)", key, value);\r
+ if (res == NULL)\r
+ return -1;\r
+ Py_DECREF(res);\r
+ return 0;\r
+}\r
+\r
+SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")\r
+SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")\r
+SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")\r
+SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")\r
+SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")\r
+SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")\r
+\r
+static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);\r
+\r
+SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,\r
+ nb_power, "__pow__", "__rpow__")\r
+\r
+static PyObject *\r
+slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)\r
+{\r
+ static PyObject *pow_str;\r
+\r
+ if (modulus == Py_None)\r
+ return slot_nb_power_binary(self, other);\r
+ /* Three-arg power doesn't use __rpow__. But ternary_op\r
+ can call this when the second argument's type uses\r
+ slot_nb_power, so check before calling self.__pow__. */\r
+ if (Py_TYPE(self)->tp_as_number != NULL &&\r
+ Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) {\r
+ return call_method(self, "__pow__", &pow_str,\r
+ "(OO)", other, modulus);\r
+ }\r
+ Py_INCREF(Py_NotImplemented);\r
+ return Py_NotImplemented;\r
+}\r
+\r
+SLOT0(slot_nb_negative, "__neg__")\r
+SLOT0(slot_nb_positive, "__pos__")\r
+SLOT0(slot_nb_absolute, "__abs__")\r
+\r
+static int\r
+slot_nb_nonzero(PyObject *self)\r
+{\r
+ PyObject *func, *args;\r
+ static PyObject *nonzero_str, *len_str;\r
+ int result = -1;\r
+ int using_len = 0;\r
+\r
+ func = lookup_maybe(self, "__nonzero__", &nonzero_str);\r
+ if (func == NULL) {\r
+ if (PyErr_Occurred())\r
+ return -1;\r
+ func = lookup_maybe(self, "__len__", &len_str);\r
+ if (func == NULL)\r
+ return PyErr_Occurred() ? -1 : 1;\r
+ using_len = 1;\r
+ }\r
+ args = PyTuple_New(0);\r
+ if (args != NULL) {\r
+ PyObject *temp = PyObject_Call(func, args, NULL);\r
+ Py_DECREF(args);\r
+ if (temp != NULL) {\r
+ if (PyInt_CheckExact(temp) || PyBool_Check(temp))\r
+ result = PyObject_IsTrue(temp);\r
+ else {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "%s should return "\r
+ "bool or int, returned %s",\r
+ (using_len ? "__len__"\r
+ : "__nonzero__"),\r
+ temp->ob_type->tp_name);\r
+ result = -1;\r
+ }\r
+ Py_DECREF(temp);\r
+ }\r
+ }\r
+ Py_DECREF(func);\r
+ return result;\r
+}\r
+\r
+\r
+static PyObject *\r
+slot_nb_index(PyObject *self)\r
+{\r
+ static PyObject *index_str;\r
+ return call_method(self, "__index__", &index_str, "()");\r
+}\r
+\r
+\r
+SLOT0(slot_nb_invert, "__invert__")\r
+SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")\r
+SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")\r
+SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")\r
+SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")\r
+SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")\r
+\r
+static int\r
+slot_nb_coerce(PyObject **a, PyObject **b)\r
+{\r
+ static PyObject *coerce_str;\r
+ PyObject *self = *a, *other = *b;\r
+\r
+ if (self->ob_type->tp_as_number != NULL &&\r
+ self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {\r
+ PyObject *r;\r
+ r = call_maybe(\r
+ self, "__coerce__", &coerce_str, "(O)", other);\r
+ if (r == NULL)\r
+ return -1;\r
+ if (r == Py_NotImplemented) {\r
+ Py_DECREF(r);\r
+ }\r
+ else {\r
+ if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "__coerce__ didn't return a 2-tuple");\r
+ Py_DECREF(r);\r
+ return -1;\r
+ }\r
+ *a = PyTuple_GET_ITEM(r, 0);\r
+ Py_INCREF(*a);\r
+ *b = PyTuple_GET_ITEM(r, 1);\r
+ Py_INCREF(*b);\r
+ Py_DECREF(r);\r
+ return 0;\r
+ }\r
+ }\r
+ if (other->ob_type->tp_as_number != NULL &&\r
+ other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {\r
+ PyObject *r;\r
+ r = call_maybe(\r
+ other, "__coerce__", &coerce_str, "(O)", self);\r
+ if (r == NULL)\r
+ return -1;\r
+ if (r == Py_NotImplemented) {\r
+ Py_DECREF(r);\r
+ return 1;\r
+ }\r
+ if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "__coerce__ didn't return a 2-tuple");\r
+ Py_DECREF(r);\r
+ return -1;\r
+ }\r
+ *a = PyTuple_GET_ITEM(r, 1);\r
+ Py_INCREF(*a);\r
+ *b = PyTuple_GET_ITEM(r, 0);\r
+ Py_INCREF(*b);\r
+ Py_DECREF(r);\r
+ return 0;\r
+ }\r
+ return 1;\r
+}\r
+\r
+SLOT0(slot_nb_int, "__int__")\r
+SLOT0(slot_nb_long, "__long__")\r
+SLOT0(slot_nb_float, "__float__")\r
+SLOT0(slot_nb_oct, "__oct__")\r
+SLOT0(slot_nb_hex, "__hex__")\r
+SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")\r
+SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")\r
+SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")\r
+SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")\r
+SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")\r
+/* Can't use SLOT1 here, because nb_inplace_power is ternary */\r
+static PyObject *\r
+slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)\r
+{\r
+ static PyObject *cache_str;\r
+ return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);\r
+}\r
+SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")\r
+SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")\r
+SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")\r
+SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")\r
+SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")\r
+SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,\r
+ "__floordiv__", "__rfloordiv__")\r
+SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")\r
+SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")\r
+SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")\r
+\r
+static int\r
+half_compare(PyObject *self, PyObject *other)\r
+{\r
+ PyObject *func, *args, *res;\r
+ static PyObject *cmp_str;\r
+ Py_ssize_t c;\r
+\r
+ func = lookup_method(self, "__cmp__", &cmp_str);\r
+ if (func == NULL) {\r
+ PyErr_Clear();\r
+ }\r
+ else {\r
+ args = PyTuple_Pack(1, other);\r
+ if (args == NULL)\r
+ res = NULL;\r
+ else {\r
+ res = PyObject_Call(func, args, NULL);\r
+ Py_DECREF(args);\r
+ }\r
+ Py_DECREF(func);\r
+ if (res != Py_NotImplemented) {\r
+ if (res == NULL)\r
+ return -2;\r
+ c = PyInt_AsLong(res);\r
+ Py_DECREF(res);\r
+ if (c == -1 && PyErr_Occurred())\r
+ return -2;\r
+ return (c < 0) ? -1 : (c > 0) ? 1 : 0;\r
+ }\r
+ Py_DECREF(res);\r
+ }\r
+ return 2;\r
+}\r
+\r
+/* This slot is published for the benefit of try_3way_compare in object.c */\r
+int\r
+_PyObject_SlotCompare(PyObject *self, PyObject *other)\r
+{\r
+ int c;\r
+\r
+ if (Py_TYPE(self)->tp_compare == _PyObject_SlotCompare) {\r
+ c = half_compare(self, other);\r
+ if (c <= 1)\r
+ return c;\r
+ }\r
+ if (Py_TYPE(other)->tp_compare == _PyObject_SlotCompare) {\r
+ c = half_compare(other, self);\r
+ if (c < -1)\r
+ return -2;\r
+ if (c <= 1)\r
+ return -c;\r
+ }\r
+ return (void *)self < (void *)other ? -1 :\r
+ (void *)self > (void *)other ? 1 : 0;\r
+}\r
+\r
+static PyObject *\r
+slot_tp_repr(PyObject *self)\r
+{\r
+ PyObject *func, *res;\r
+ static PyObject *repr_str;\r
+\r
+ func = lookup_method(self, "__repr__", &repr_str);\r
+ if (func != NULL) {\r
+ res = PyEval_CallObject(func, NULL);\r
+ Py_DECREF(func);\r
+ return res;\r
+ }\r
+ PyErr_Clear();\r
+ return PyString_FromFormat("<%s object at %p>",\r
+ Py_TYPE(self)->tp_name, self);\r
+}\r
+\r
+static PyObject *\r
+slot_tp_str(PyObject *self)\r
+{\r
+ PyObject *func, *res;\r
+ static PyObject *str_str;\r
+\r
+ func = lookup_method(self, "__str__", &str_str);\r
+ if (func != NULL) {\r
+ res = PyEval_CallObject(func, NULL);\r
+ Py_DECREF(func);\r
+ return res;\r
+ }\r
+ else {\r
+ PyErr_Clear();\r
+ return slot_tp_repr(self);\r
+ }\r
+}\r
+\r
+static long\r
+slot_tp_hash(PyObject *self)\r
+{\r
+ PyObject *func;\r
+ static PyObject *hash_str, *eq_str, *cmp_str;\r
+ long h;\r
+\r
+ func = lookup_method(self, "__hash__", &hash_str);\r
+\r
+ if (func != NULL && func != Py_None) {\r
+ PyObject *res = PyEval_CallObject(func, NULL);\r
+ Py_DECREF(func);\r
+ if (res == NULL)\r
+ return -1;\r
+ if (PyLong_Check(res))\r
+ h = PyLong_Type.tp_hash(res);\r
+ else\r
+ h = PyInt_AsLong(res);\r
+ Py_DECREF(res);\r
+ }\r
+ else {\r
+ Py_XDECREF(func); /* may be None */\r
+ PyErr_Clear();\r
+ func = lookup_method(self, "__eq__", &eq_str);\r
+ if (func == NULL) {\r
+ PyErr_Clear();\r
+ func = lookup_method(self, "__cmp__", &cmp_str);\r
+ }\r
+ if (func != NULL) {\r
+ Py_DECREF(func);\r
+ return PyObject_HashNotImplemented(self);\r
+ }\r
+ PyErr_Clear();\r
+ h = _Py_HashPointer((void *)self);\r
+ }\r
+ if (h == -1 && !PyErr_Occurred())\r
+ h = -2;\r
+ return h;\r
+}\r
+\r
+static PyObject *\r
+slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)\r
+{\r
+ static PyObject *call_str;\r
+ PyObject *meth = lookup_method(self, "__call__", &call_str);\r
+ PyObject *res;\r
+\r
+ if (meth == NULL)\r
+ return NULL;\r
+\r
+ res = PyObject_Call(meth, args, kwds);\r
+\r
+ Py_DECREF(meth);\r
+ return res;\r
+}\r
+\r
+/* There are two slot dispatch functions for tp_getattro.\r
+\r
+ - slot_tp_getattro() is used when __getattribute__ is overridden\r
+ but no __getattr__ hook is present;\r
+\r
+ - slot_tp_getattr_hook() is used when a __getattr__ hook is present.\r
+\r
+ The code in update_one_slot() always installs slot_tp_getattr_hook(); this\r
+ detects the absence of __getattr__ and then installs the simpler slot if\r
+ necessary. */\r
+\r
+static PyObject *\r
+slot_tp_getattro(PyObject *self, PyObject *name)\r
+{\r
+ static PyObject *getattribute_str = NULL;\r
+ return call_method(self, "__getattribute__", &getattribute_str,\r
+ "(O)", name);\r
+}\r
+\r
+static PyObject *\r
+call_attribute(PyObject *self, PyObject *attr, PyObject *name)\r
+{\r
+ PyObject *res, *descr = NULL;\r
+ descrgetfunc f = Py_TYPE(attr)->tp_descr_get;\r
+\r
+ if (f != NULL) {\r
+ descr = f(attr, self, (PyObject *)(Py_TYPE(self)));\r
+ if (descr == NULL)\r
+ return NULL;\r
+ else\r
+ attr = descr;\r
+ }\r
+ res = PyObject_CallFunctionObjArgs(attr, name, NULL);\r
+ Py_XDECREF(descr);\r
+ return res;\r
+}\r
+\r
+static PyObject *\r
+slot_tp_getattr_hook(PyObject *self, PyObject *name)\r
+{\r
+ PyTypeObject *tp = Py_TYPE(self);\r
+ PyObject *getattr, *getattribute, *res;\r
+ static PyObject *getattribute_str = NULL;\r
+ static PyObject *getattr_str = NULL;\r
+\r
+ if (getattr_str == NULL) {\r
+ getattr_str = PyString_InternFromString("__getattr__");\r
+ if (getattr_str == NULL)\r
+ return NULL;\r
+ }\r
+ if (getattribute_str == NULL) {\r
+ getattribute_str =\r
+ PyString_InternFromString("__getattribute__");\r
+ if (getattribute_str == NULL)\r
+ return NULL;\r
+ }\r
+ /* speed hack: we could use lookup_maybe, but that would resolve the\r
+ method fully for each attribute lookup for classes with\r
+ __getattr__, even when the attribute is present. So we use\r
+ _PyType_Lookup and create the method only when needed, with\r
+ call_attribute. */\r
+ getattr = _PyType_Lookup(tp, getattr_str);\r
+ if (getattr == NULL) {\r
+ /* No __getattr__ hook: use a simpler dispatcher */\r
+ tp->tp_getattro = slot_tp_getattro;\r
+ return slot_tp_getattro(self, name);\r
+ }\r
+ Py_INCREF(getattr);\r
+ /* speed hack: we could use lookup_maybe, but that would resolve the\r
+ method fully for each attribute lookup for classes with\r
+ __getattr__, even when self has the default __getattribute__\r
+ method. So we use _PyType_Lookup and create the method only when\r
+ needed, with call_attribute. */\r
+ getattribute = _PyType_Lookup(tp, getattribute_str);\r
+ if (getattribute == NULL ||\r
+ (Py_TYPE(getattribute) == &PyWrapperDescr_Type &&\r
+ ((PyWrapperDescrObject *)getattribute)->d_wrapped ==\r
+ (void *)PyObject_GenericGetAttr))\r
+ res = PyObject_GenericGetAttr(self, name);\r
+ else {\r
+ Py_INCREF(getattribute);\r
+ res = call_attribute(self, getattribute, name);\r
+ Py_DECREF(getattribute);\r
+ }\r
+ if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {\r
+ PyErr_Clear();\r
+ res = call_attribute(self, getattr, name);\r
+ }\r
+ Py_DECREF(getattr);\r
+ return res;\r
+}\r
+\r
+static int\r
+slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)\r
+{\r
+ PyObject *res;\r
+ static PyObject *delattr_str, *setattr_str;\r
+\r
+ if (value == NULL)\r
+ res = call_method(self, "__delattr__", &delattr_str,\r
+ "(O)", name);\r
+ else\r
+ res = call_method(self, "__setattr__", &setattr_str,\r
+ "(OO)", name, value);\r
+ if (res == NULL)\r
+ return -1;\r
+ Py_DECREF(res);\r
+ return 0;\r
+}\r
+\r
+static char *name_op[] = {\r
+ "__lt__",\r
+ "__le__",\r
+ "__eq__",\r
+ "__ne__",\r
+ "__gt__",\r
+ "__ge__",\r
+};\r
+\r
+static PyObject *\r
+half_richcompare(PyObject *self, PyObject *other, int op)\r
+{\r
+ PyObject *func, *args, *res;\r
+ static PyObject *op_str[6];\r
+\r
+ func = lookup_method(self, name_op[op], &op_str[op]);\r
+ if (func == NULL) {\r
+ PyErr_Clear();\r
+ Py_INCREF(Py_NotImplemented);\r
+ return Py_NotImplemented;\r
+ }\r
+ args = PyTuple_Pack(1, other);\r
+ if (args == NULL)\r
+ res = NULL;\r
+ else {\r
+ res = PyObject_Call(func, args, NULL);\r
+ Py_DECREF(args);\r
+ }\r
+ Py_DECREF(func);\r
+ return res;\r
+}\r
+\r
+static PyObject *\r
+slot_tp_richcompare(PyObject *self, PyObject *other, int op)\r
+{\r
+ PyObject *res;\r
+\r
+ if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) {\r
+ res = half_richcompare(self, other, op);\r
+ if (res != Py_NotImplemented)\r
+ return res;\r
+ Py_DECREF(res);\r
+ }\r
+ if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) {\r
+ res = half_richcompare(other, self, _Py_SwappedOp[op]);\r
+ if (res != Py_NotImplemented) {\r
+ return res;\r
+ }\r
+ Py_DECREF(res);\r
+ }\r
+ Py_INCREF(Py_NotImplemented);\r
+ return Py_NotImplemented;\r
+}\r
+\r
+static PyObject *\r
+slot_tp_iter(PyObject *self)\r
+{\r
+ PyObject *func, *res;\r
+ static PyObject *iter_str, *getitem_str;\r
+\r
+ func = lookup_method(self, "__iter__", &iter_str);\r
+ if (func != NULL) {\r
+ PyObject *args;\r
+ args = res = PyTuple_New(0);\r
+ if (args != NULL) {\r
+ res = PyObject_Call(func, args, NULL);\r
+ Py_DECREF(args);\r
+ }\r
+ Py_DECREF(func);\r
+ return res;\r
+ }\r
+ PyErr_Clear();\r
+ func = lookup_method(self, "__getitem__", &getitem_str);\r
+ if (func == NULL) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "'%.200s' object is not iterable",\r
+ Py_TYPE(self)->tp_name);\r
+ return NULL;\r
+ }\r
+ Py_DECREF(func);\r
+ return PySeqIter_New(self);\r
+}\r
+\r
+static PyObject *\r
+slot_tp_iternext(PyObject *self)\r
+{\r
+ static PyObject *next_str;\r
+ return call_method(self, "next", &next_str, "()");\r
+}\r
+\r
+static PyObject *\r
+slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)\r
+{\r
+ PyTypeObject *tp = Py_TYPE(self);\r
+ PyObject *get;\r
+ static PyObject *get_str = NULL;\r
+\r
+ if (get_str == NULL) {\r
+ get_str = PyString_InternFromString("__get__");\r
+ if (get_str == NULL)\r
+ return NULL;\r
+ }\r
+ get = _PyType_Lookup(tp, get_str);\r
+ if (get == NULL) {\r
+ /* Avoid further slowdowns */\r
+ if (tp->tp_descr_get == slot_tp_descr_get)\r
+ tp->tp_descr_get = NULL;\r
+ Py_INCREF(self);\r
+ return self;\r
+ }\r
+ if (obj == NULL)\r
+ obj = Py_None;\r
+ if (type == NULL)\r
+ type = Py_None;\r
+ return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);\r
+}\r
+\r
+static int\r
+slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)\r
+{\r
+ PyObject *res;\r
+ static PyObject *del_str, *set_str;\r
+\r
+ if (value == NULL)\r
+ res = call_method(self, "__delete__", &del_str,\r
+ "(O)", target);\r
+ else\r
+ res = call_method(self, "__set__", &set_str,\r
+ "(OO)", target, value);\r
+ if (res == NULL)\r
+ return -1;\r
+ Py_DECREF(res);\r
+ return 0;\r
+}\r
+\r
+static int\r
+slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)\r
+{\r
+ static PyObject *init_str;\r
+ PyObject *meth = lookup_method(self, "__init__", &init_str);\r
+ PyObject *res;\r
+\r
+ if (meth == NULL)\r
+ return -1;\r
+ res = PyObject_Call(meth, args, kwds);\r
+ Py_DECREF(meth);\r
+ if (res == NULL)\r
+ return -1;\r
+ if (res != Py_None) {\r
+ PyErr_Format(PyExc_TypeError,\r
+ "__init__() should return None, not '%.200s'",\r
+ Py_TYPE(res)->tp_name);\r
+ Py_DECREF(res);\r
+ return -1;\r
+ }\r
+ Py_DECREF(res);\r
+ return 0;\r
+}\r
+\r
+static PyObject *\r
+slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
+{\r
+ static PyObject *new_str;\r
+ PyObject *func;\r
+ PyObject *newargs, *x;\r
+ Py_ssize_t i, n;\r
+\r
+ if (new_str == NULL) {\r
+ new_str = PyString_InternFromString("__new__");\r
+ if (new_str == NULL)\r
+ return NULL;\r
+ }\r
+ func = PyObject_GetAttr((PyObject *)type, new_str);\r
+ if (func == NULL)\r
+ return NULL;\r
+ assert(PyTuple_Check(args));\r
+ n = PyTuple_GET_SIZE(args);\r
+ newargs = PyTuple_New(n+1);\r
+ if (newargs == NULL)\r
+ return NULL;\r
+ Py_INCREF(type);\r
+ PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);\r
+ for (i = 0; i < n; i++) {\r
+ x = PyTuple_GET_ITEM(args, i);\r
+ Py_INCREF(x);\r
+ PyTuple_SET_ITEM(newargs, i+1, x);\r
+ }\r
+ x = PyObject_Call(func, newargs, kwds);\r
+ Py_DECREF(newargs);\r
+ Py_DECREF(func);\r
+ return x;\r
+}\r
+\r
+static void\r
+slot_tp_del(PyObject *self)\r
+{\r
+ static PyObject *del_str = NULL;\r
+ PyObject *del, *res;\r
+ PyObject *error_type, *error_value, *error_traceback;\r
+\r
+ /* Temporarily resurrect the object. */\r
+ assert(self->ob_refcnt == 0);\r
+ self->ob_refcnt = 1;\r
+\r
+ /* Save the current exception, if any. */\r
+ PyErr_Fetch(&error_type, &error_value, &error_traceback);\r
+\r
+ /* Execute __del__ method, if any. */\r
+ del = lookup_maybe(self, "__del__", &del_str);\r
+ if (del != NULL) {\r
+ res = PyEval_CallObject(del, NULL);\r
+ if (res == NULL)\r
+ PyErr_WriteUnraisable(del);\r
+ else\r
+ Py_DECREF(res);\r
+ Py_DECREF(del);\r
+ }\r
+\r
+ /* Restore the saved exception. */\r
+ PyErr_Restore(error_type, error_value, error_traceback);\r
+\r
+ /* Undo the temporary resurrection; can't use DECREF here, it would\r
+ * cause a recursive call.\r
+ */\r
+ assert(self->ob_refcnt > 0);\r
+ if (--self->ob_refcnt == 0)\r
+ return; /* this is the normal path out */\r
+\r
+ /* __del__ resurrected it! Make it look like the original Py_DECREF\r
+ * never happened.\r
+ */\r
+ {\r
+ Py_ssize_t refcnt = self->ob_refcnt;\r
+ _Py_NewReference(self);\r
+ self->ob_refcnt = refcnt;\r
+ }\r
+ assert(!PyType_IS_GC(Py_TYPE(self)) ||\r
+ _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);\r
+ /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so\r
+ * we need to undo that. */\r
+ _Py_DEC_REFTOTAL;\r
+ /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object\r
+ * chain, so no more to do there.\r
+ * If COUNT_ALLOCS, the original decref bumped tp_frees, and\r
+ * _Py_NewReference bumped tp_allocs: both of those need to be\r
+ * undone.\r
+ */\r
+#ifdef COUNT_ALLOCS\r
+ --Py_TYPE(self)->tp_frees;\r
+ --Py_TYPE(self)->tp_allocs;\r
+#endif\r
+}\r
+\r
+\r
+/*\r
+Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper functions.\r
+\r
+The table is ordered by offsets relative to the 'PyHeapTypeObject' structure,\r
+which incorporates the additional structures used for numbers, sequences and\r
+mappings. Note that multiple names may map to the same slot (e.g. __eq__,\r
+__ne__ etc. all map to tp_richcompare) and one name may map to multiple slots\r
+(e.g. __str__ affects tp_str as well as tp_repr). The table is terminated with\r
+an all-zero entry. (This table is further initialized in init_slotdefs().)\r
+*/\r
+\r
+typedef struct wrapperbase slotdef;\r
+\r
+#undef TPSLOT\r
+#undef FLSLOT\r
+#undef ETSLOT\r
+#undef SQSLOT\r
+#undef MPSLOT\r
+#undef NBSLOT\r
+#undef UNSLOT\r
+#undef IBSLOT\r
+#undef BINSLOT\r
+#undef RBINSLOT\r
+\r
+#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r
+ {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \\r
+ PyDoc_STR(DOC)}\r
+#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \\r
+ {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \\r
+ PyDoc_STR(DOC), FLAGS}\r
+#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r
+ {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \\r
+ PyDoc_STR(DOC)}\r
+#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r
+ ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)\r
+#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r
+ ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)\r
+#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)\r
+#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \\r
+ "x." NAME "() <==> " DOC)\r
+#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \\r
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \\r
+ "x." NAME "(y) <==> x" DOC "y")\r
+#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \\r
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \\r
+ "x." NAME "(y) <==> x" DOC "y")\r
+#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \\r
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \\r
+ "x." NAME "(y) <==> y" DOC "x")\r
+#define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \\r
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \\r
+ "x." NAME "(y) <==> " DOC)\r
+#define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \\r
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \\r
+ "x." NAME "(y) <==> " DOC)\r
+\r
+static slotdef slotdefs[] = {\r
+ TPSLOT("__str__", tp_print, NULL, NULL, ""),\r
+ TPSLOT("__repr__", tp_print, NULL, NULL, ""),\r
+ TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),\r
+ TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),\r
+ TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),\r
+ TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),\r
+ TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,\r
+ "x.__cmp__(y) <==> cmp(x,y)"),\r
+ TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,\r
+ "x.__repr__() <==> repr(x)"),\r
+ TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,\r
+ "x.__hash__() <==> hash(x)"),\r
+ FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,\r
+ "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),\r
+ TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,\r
+ "x.__str__() <==> str(x)"),\r
+ TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,\r
+ wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),\r
+ TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),\r
+ TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,\r
+ "x.__setattr__('name', value) <==> x.name = value"),\r
+ TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,\r
+ "x.__delattr__('name') <==> del x.name"),\r
+ TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,\r
+ "x.__lt__(y) <==> x<y"),\r
+ TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,\r
+ "x.__le__(y) <==> x<=y"),\r
+ TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,\r
+ "x.__eq__(y) <==> x==y"),\r
+ TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,\r
+ "x.__ne__(y) <==> x!=y"),\r
+ TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,\r
+ "x.__gt__(y) <==> x>y"),\r
+ TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,\r
+ "x.__ge__(y) <==> x>=y"),\r
+ TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,\r
+ "x.__iter__() <==> iter(x)"),\r
+ TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,\r
+ "x.next() -> the next value, or raise StopIteration"),\r
+ TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,\r
+ "descr.__get__(obj[, type]) -> value"),\r
+ TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,\r
+ "descr.__set__(obj, value)"),\r
+ TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,\r
+ wrap_descr_delete, "descr.__delete__(obj)"),\r
+ FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,\r
+ "x.__init__(...) initializes x; "\r
+ "see help(type(x)) for signature",\r
+ PyWrapperFlag_KEYWORDS),\r
+ TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),\r
+ TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),\r
+ BINSLOT("__add__", nb_add, slot_nb_add,\r
+ "+"),\r
+ RBINSLOT("__radd__", nb_add, slot_nb_add,\r
+ "+"),\r
+ BINSLOT("__sub__", nb_subtract, slot_nb_subtract,\r
+ "-"),\r
+ RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,\r
+ "-"),\r
+ BINSLOT("__mul__", nb_multiply, slot_nb_multiply,\r
+ "*"),\r
+ RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,\r
+ "*"),\r
+ BINSLOT("__div__", nb_divide, slot_nb_divide,\r
+ "/"),\r
+ RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,\r
+ "/"),\r
+ BINSLOT("__mod__", nb_remainder, slot_nb_remainder,\r
+ "%"),\r
+ RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,\r
+ "%"),\r
+ BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,\r
+ "divmod(x, y)"),\r
+ RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,\r
+ "divmod(y, x)"),\r
+ NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,\r
+ "x.__pow__(y[, z]) <==> pow(x, y[, z])"),\r
+ NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,\r
+ "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),\r
+ UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),\r
+ UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),\r
+ UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,\r
+ "abs(x)"),\r
+ UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred,\r
+ "x != 0"),\r
+ UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),\r
+ BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),\r
+ RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),\r
+ BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),\r
+ RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),\r
+ BINSLOT("__and__", nb_and, slot_nb_and, "&"),\r
+ RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),\r
+ BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),\r
+ RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),\r
+ BINSLOT("__or__", nb_or, slot_nb_or, "|"),\r
+ RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),\r
+ NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,\r
+ "x.__coerce__(y) <==> coerce(x, y)"),\r
+ UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,\r
+ "int(x)"),\r
+ UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,\r
+ "long(x)"),\r
+ UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,\r
+ "float(x)"),\r
+ UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,\r
+ "oct(x)"),\r
+ UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,\r
+ "hex(x)"),\r
+ IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,\r
+ wrap_binaryfunc, "+="),\r
+ IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,\r
+ wrap_binaryfunc, "-="),\r
+ IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,\r
+ wrap_binaryfunc, "*="),\r
+ IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,\r
+ wrap_binaryfunc, "/="),\r
+ IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,\r
+ wrap_binaryfunc, "%="),\r
+ IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,\r
+ wrap_binaryfunc, "**="),\r
+ IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,\r
+ wrap_binaryfunc, "<<="),\r
+ IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,\r
+ wrap_binaryfunc, ">>="),\r
+ IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,\r
+ wrap_binaryfunc, "&="),\r
+ IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,\r
+ wrap_binaryfunc, "^="),\r
+ IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,\r
+ wrap_binaryfunc, "|="),\r
+ BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),\r
+ RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),\r
+ BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),\r
+ RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),\r
+ IBSLOT("__ifloordiv__", nb_inplace_floor_divide,\r
+ slot_nb_inplace_floor_divide, wrap_binaryfunc, "//="),\r
+ IBSLOT("__itruediv__", nb_inplace_true_divide,\r
+ slot_nb_inplace_true_divide, wrap_binaryfunc, "/="),\r
+ NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,\r
+ "x[y:z] <==> x[y.__index__():z.__index__()]"),\r
+ MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,\r
+ "x.__len__() <==> len(x)"),\r
+ MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,\r
+ wrap_binaryfunc,\r
+ "x.__getitem__(y) <==> x[y]"),\r
+ MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,\r
+ wrap_objobjargproc,\r
+ "x.__setitem__(i, y) <==> x[i]=y"),\r
+ MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,\r
+ wrap_delitem,\r
+ "x.__delitem__(y) <==> del x[y]"),\r
+ SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,\r
+ "x.__len__() <==> len(x)"),\r
+ /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.\r
+ The logic in abstract.c always falls back to nb_add/nb_multiply in\r
+ this case. Defining both the nb_* and the sq_* slots to call the\r
+ user-defined methods has unexpected side-effects, as shown by\r
+ test_descr.notimplemented() */\r
+ SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,\r
+ "x.__add__(y) <==> x+y"),\r
+ SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,\r
+ "x.__mul__(n) <==> x*n"),\r
+ SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,\r
+ "x.__rmul__(n) <==> n*x"),\r
+ SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,\r
+ "x.__getitem__(y) <==> x[y]"),\r
+ SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,\r
+ "x.__getslice__(i, j) <==> x[i:j]\n\\r
+ \n\\r
+ Use of negative indices is not supported."),\r
+ SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,\r
+ "x.__setitem__(i, y) <==> x[i]=y"),\r
+ SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,\r
+ "x.__delitem__(y) <==> del x[y]"),\r
+ SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,\r
+ wrap_ssizessizeobjargproc,\r
+ "x.__setslice__(i, j, y) <==> x[i:j]=y\n\\r
+ \n\\r
+ Use of negative indices is not supported."),\r
+ SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,\r
+ "x.__delslice__(i, j) <==> del x[i:j]\n\\r
+ \n\\r
+ Use of negative indices is not supported."),\r
+ SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,\r
+ "x.__contains__(y) <==> y in x"),\r
+ SQSLOT("__iadd__", sq_inplace_concat, NULL,\r
+ wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),\r
+ SQSLOT("__imul__", sq_inplace_repeat, NULL,\r
+ wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),\r
+ {NULL}\r
+};\r
+\r
+/* Given a type pointer and an offset gotten from a slotdef entry, return a\r
+ pointer to the actual slot. This is not quite the same as simply adding\r
+ the offset to the type pointer, since it takes care to indirect through the\r
+ proper indirection pointer (as_buffer, etc.); it returns NULL if the\r
+ indirection pointer is NULL. */\r
+static void **\r
+slotptr(PyTypeObject *type, int ioffset)\r
+{\r
+ char *ptr;\r
+ long offset = ioffset;\r
+\r
+ /* Note: this depends on the order of the members of PyHeapTypeObject! */\r
+ assert(offset >= 0);\r
+ assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));\r
+ if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {\r
+ ptr = (char *)type->tp_as_sequence;\r
+ offset -= offsetof(PyHeapTypeObject, as_sequence);\r
+ }\r
+ else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {\r
+ ptr = (char *)type->tp_as_mapping;\r
+ offset -= offsetof(PyHeapTypeObject, as_mapping);\r
+ }\r
+ else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {\r
+ ptr = (char *)type->tp_as_number;\r
+ offset -= offsetof(PyHeapTypeObject, as_number);\r
+ }\r
+ else {\r
+ ptr = (char *)type;\r
+ }\r
+ if (ptr != NULL)\r
+ ptr += offset;\r
+ return (void **)ptr;\r
+}\r
+\r
+/* Length of array of slotdef pointers used to store slots with the\r
+ same __name__. There should be at most MAX_EQUIV-1 slotdef entries with\r
+ the same __name__, for any __name__. Since that's a static property, it is\r
+ appropriate to declare fixed-size arrays for this. */\r
+#define MAX_EQUIV 10\r
+\r
+/* Return a slot pointer for a given name, but ONLY if the attribute has\r
+ exactly one slot function. The name must be an interned string. */\r
+static void **\r
+resolve_slotdups(PyTypeObject *type, PyObject *name)\r
+{\r
+ /* XXX Maybe this could be optimized more -- but is it worth it? */\r
+\r
+ /* pname and ptrs act as a little cache */\r
+ static PyObject *pname;\r
+ static slotdef *ptrs[MAX_EQUIV];\r
+ slotdef *p, **pp;\r
+ void **res, **ptr;\r
+\r
+ if (pname != name) {\r
+ /* Collect all slotdefs that match name into ptrs. */\r
+ pname = name;\r
+ pp = ptrs;\r
+ for (p = slotdefs; p->name_strobj; p++) {\r
+ if (p->name_strobj == name)\r
+ *pp++ = p;\r
+ }\r
+ *pp = NULL;\r
+ }\r
+\r
+ /* Look in all matching slots of the type; if exactly one of these has\r
+ a filled-in slot, return its value. Otherwise return NULL. */\r
+ res = NULL;\r
+ for (pp = ptrs; *pp; pp++) {\r
+ ptr = slotptr(type, (*pp)->offset);\r
+ if (ptr == NULL || *ptr == NULL)\r
+ continue;\r
+ if (res != NULL)\r
+ return NULL;\r
+ res = ptr;\r
+ }\r
+ return res;\r
+}\r
+\r
+/* Common code for update_slots_callback() and fixup_slot_dispatchers(). This\r
+ does some incredibly complex thinking and then sticks something into the\r
+ slot. (It sees if the adjacent slotdefs for the same slot have conflicting\r
+ interests, and then stores a generic wrapper or a specific function into\r
+ the slot.) Return a pointer to the next slotdef with a different offset,\r
+ because that's convenient for fixup_slot_dispatchers(). */\r
+static slotdef *\r
+update_one_slot(PyTypeObject *type, slotdef *p)\r
+{\r
+ PyObject *descr;\r
+ PyWrapperDescrObject *d;\r
+ void *generic = NULL, *specific = NULL;\r
+ int use_generic = 0;\r
+ int offset = p->offset;\r
+ void **ptr = slotptr(type, offset);\r
+\r
+ if (ptr == NULL) {\r
+ do {\r
+ ++p;\r
+ } while (p->offset == offset);\r
+ return p;\r
+ }\r
+ do {\r
+ descr = _PyType_Lookup(type, p->name_strobj);\r
+ if (descr == NULL) {\r
+ if (ptr == (void**)&type->tp_iternext) {\r
+ specific = _PyObject_NextNotImplemented;\r
+ }\r
+ continue;\r
+ }\r
+ if (Py_TYPE(descr) == &PyWrapperDescr_Type &&\r
+ ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_strobj) {\r
+ void **tptr = resolve_slotdups(type, p->name_strobj);\r
+ if (tptr == NULL || tptr == ptr)\r
+ generic = p->function;\r
+ d = (PyWrapperDescrObject *)descr;\r
+ if (d->d_base->wrapper == p->wrapper &&\r
+ PyType_IsSubtype(type, d->d_type))\r
+ {\r
+ if (specific == NULL ||\r
+ specific == d->d_wrapped)\r
+ specific = d->d_wrapped;\r
+ else\r
+ use_generic = 1;\r
+ }\r
+ }\r
+ else if (Py_TYPE(descr) == &PyCFunction_Type &&\r
+ PyCFunction_GET_FUNCTION(descr) ==\r
+ (PyCFunction)tp_new_wrapper &&\r
+ ptr == (void**)&type->tp_new)\r
+ {\r
+ /* The __new__ wrapper is not a wrapper descriptor,\r
+ so must be special-cased differently.\r
+ If we don't do this, creating an instance will\r
+ always use slot_tp_new which will look up\r
+ __new__ in the MRO which will call tp_new_wrapper\r
+ which will look through the base classes looking\r
+ for a static base and call its tp_new (usually\r
+ PyType_GenericNew), after performing various\r
+ sanity checks and constructing a new argument\r
+ list. Cut all that nonsense short -- this speeds\r
+ up instance creation tremendously. */\r
+ specific = (void *)type->tp_new;\r
+ /* XXX I'm not 100% sure that there isn't a hole\r
+ in this reasoning that requires additional\r
+ sanity checks. I'll buy the first person to\r
+ point out a bug in this reasoning a beer. */\r
+ }\r
+ else if (descr == Py_None &&\r
+ ptr == (void**)&type->tp_hash) {\r
+ /* We specifically allow __hash__ to be set to None\r
+ to prevent inheritance of the default\r
+ implementation from object.__hash__ */\r
+ specific = PyObject_HashNotImplemented;\r
+ }\r
+ else {\r
+ use_generic = 1;\r
+ generic = p->function;\r
+ }\r
+ } while ((++p)->offset == offset);\r
+ if (specific && !use_generic)\r
+ *ptr = specific;\r
+ else\r
+ *ptr = generic;\r
+ return p;\r
+}\r
+\r
+/* In the type, update the slots whose slotdefs are gathered in the pp array.\r
+ This is a callback for update_subclasses(). */\r
+static int\r
+update_slots_callback(PyTypeObject *type, void *data)\r
+{\r
+ slotdef **pp = (slotdef **)data;\r
+\r
+ for (; *pp; pp++)\r
+ update_one_slot(type, *pp);\r
+ return 0;\r
+}\r
+\r
+/* Initialize the slotdefs table by adding interned string objects for the\r
+ names and sorting the entries. */\r
+static void\r
+init_slotdefs(void)\r
+{\r
+ slotdef *p;\r
+ static int initialized = 0;\r
+\r
+ if (initialized)\r
+ return;\r
+ for (p = slotdefs; p->name; p++) {\r
+ /* Slots must be ordered by their offset in the PyHeapTypeObject. */\r
+ assert(!p[1].name || p->offset <= p[1].offset);\r
+ p->name_strobj = PyString_InternFromString(p->name);\r
+ if (!p->name_strobj)\r
+ Py_FatalError("Out of memory interning slotdef names");\r
+ }\r
+ initialized = 1;\r
+}\r
+\r
+/* Update the slots after assignment to a class (type) attribute. */\r
+static int\r
+update_slot(PyTypeObject *type, PyObject *name)\r
+{\r
+ slotdef *ptrs[MAX_EQUIV];\r
+ slotdef *p;\r
+ slotdef **pp;\r
+ int offset;\r
+\r
+ /* Clear the VALID_VERSION flag of 'type' and all its\r
+ subclasses. This could possibly be unified with the\r
+ update_subclasses() recursion below, but carefully:\r
+ they each have their own conditions on which to stop\r
+ recursing into subclasses. */\r
+ PyType_Modified(type);\r
+\r
+ init_slotdefs();\r
+ pp = ptrs;\r
+ for (p = slotdefs; p->name; p++) {\r
+ /* XXX assume name is interned! */\r
+ if (p->name_strobj == name)\r
+ *pp++ = p;\r
+ }\r
+ *pp = NULL;\r
+ for (pp = ptrs; *pp; pp++) {\r
+ p = *pp;\r
+ offset = p->offset;\r
+ while (p > slotdefs && (p-1)->offset == offset)\r
+ --p;\r
+ *pp = p;\r
+ }\r
+ if (ptrs[0] == NULL)\r
+ return 0; /* Not an attribute that affects any slots */\r
+ return update_subclasses(type, name,\r
+ update_slots_callback, (void *)ptrs);\r
+}\r
+\r
+/* Store the proper functions in the slot dispatches at class (type)\r
+ definition time, based upon which operations the class overrides in its\r
+ dict. */\r
+static void\r
+fixup_slot_dispatchers(PyTypeObject *type)\r
+{\r
+ slotdef *p;\r
+\r
+ init_slotdefs();\r
+ for (p = slotdefs; p->name; )\r
+ p = update_one_slot(type, p);\r
+}\r
+\r
+static void\r
+update_all_slots(PyTypeObject* type)\r
+{\r
+ slotdef *p;\r
+\r
+ init_slotdefs();\r
+ for (p = slotdefs; p->name; p++) {\r
+ /* update_slot returns int but can't actually fail */\r
+ update_slot(type, p->name_strobj);\r
+ }\r
+}\r
+\r
+/* recurse_down_subclasses() and update_subclasses() are mutually\r
+ recursive functions to call a callback for all subclasses,\r
+ but refraining from recursing into subclasses that define 'name'. */\r
+\r
+static int\r
+update_subclasses(PyTypeObject *type, PyObject *name,\r
+ update_callback callback, void *data)\r
+{\r
+ if (callback(type, data) < 0)\r
+ return -1;\r
+ return recurse_down_subclasses(type, name, callback, data);\r
+}\r
+\r
+static int\r
+recurse_down_subclasses(PyTypeObject *type, PyObject *name,\r
+ update_callback callback, void *data)\r
+{\r
+ PyTypeObject *subclass;\r
+ PyObject *ref, *subclasses, *dict;\r
+ Py_ssize_t i, n;\r
+\r
+ subclasses = type->tp_subclasses;\r
+ if (subclasses == NULL)\r
+ return 0;\r
+ assert(PyList_Check(subclasses));\r
+ n = PyList_GET_SIZE(subclasses);\r
+ for (i = 0; i < n; i++) {\r
+ ref = PyList_GET_ITEM(subclasses, i);\r
+ assert(PyWeakref_CheckRef(ref));\r
+ subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);\r
+ assert(subclass != NULL);\r
+ if ((PyObject *)subclass == Py_None)\r
+ continue;\r
+ assert(PyType_Check(subclass));\r
+ /* Avoid recursing down into unaffected classes */\r
+ dict = subclass->tp_dict;\r
+ if (dict != NULL && PyDict_Check(dict) &&\r
+ PyDict_GetItem(dict, name) != NULL)\r
+ continue;\r
+ if (update_subclasses(subclass, name, callback, data) < 0)\r
+ return -1;\r
+ }\r
+ return 0;\r
+}\r
+\r
+/* This function is called by PyType_Ready() to populate the type's\r
+ dictionary with method descriptors for function slots. For each\r
+ function slot (like tp_repr) that's defined in the type, one or more\r
+ corresponding descriptors are added in the type's tp_dict dictionary\r
+ under the appropriate name (like __repr__). Some function slots\r
+ cause more than one descriptor to be added (for example, the nb_add\r
+ slot adds both __add__ and __radd__ descriptors) and some function\r
+ slots compete for the same descriptor (for example both sq_item and\r
+ mp_subscript generate a __getitem__ descriptor).\r
+\r
+ In the latter case, the first slotdef entry encountered wins. Since\r
+ slotdef entries are sorted by the offset of the slot in the\r
+ PyHeapTypeObject, this gives us some control over disambiguating\r
+ between competing slots: the members of PyHeapTypeObject are listed\r
+ from most general to least general, so the most general slot is\r
+ preferred. In particular, because as_mapping comes before as_sequence,\r
+ for a type that defines both mp_subscript and sq_item, mp_subscript\r
+ wins.\r
+\r
+ This only adds new descriptors and doesn't overwrite entries in\r
+ tp_dict that were previously defined. The descriptors contain a\r
+ reference to the C function they must call, so that it's safe if they\r
+ are copied into a subtype's __dict__ and the subtype has a different\r
+ C function in its slot -- calling the method defined by the\r
+ descriptor will call the C function that was used to create it,\r
+ rather than the C function present in the slot when it is called.\r
+ (This is important because a subtype may have a C function in the\r
+ slot that calls the method from the dictionary, and we want to avoid\r
+ infinite recursion here.) */\r
+\r
+static int\r
+add_operators(PyTypeObject *type)\r
+{\r
+ PyObject *dict = type->tp_dict;\r
+ slotdef *p;\r
+ PyObject *descr;\r
+ void **ptr;\r
+\r
+ init_slotdefs();\r
+ for (p = slotdefs; p->name; p++) {\r
+ if (p->wrapper == NULL)\r
+ continue;\r
+ ptr = slotptr(type, p->offset);\r
+ if (!ptr || !*ptr)\r
+ continue;\r
+ if (PyDict_GetItem(dict, p->name_strobj))\r
+ continue;\r
+ if (*ptr == PyObject_HashNotImplemented) {\r
+ /* Classes may prevent the inheritance of the tp_hash\r
+ slot by storing PyObject_HashNotImplemented in it. Make it\r
+ visible as a None value for the __hash__ attribute. */\r
+ if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)\r
+ return -1;\r
+ }\r
+ else {\r
+ descr = PyDescr_NewWrapper(type, p, *ptr);\r
+ if (descr == NULL)\r
+ return -1;\r
+ if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)\r
+ return -1;\r
+ Py_DECREF(descr);\r
+ }\r
+ }\r
+ if (type->tp_new != NULL) {\r
+ if (add_tp_new_wrapper(type) < 0)\r
+ return -1;\r
+ }\r
+ return 0;\r
+}\r
+\r
+\r
+/* Cooperative 'super' */\r
+\r
+typedef struct {\r
+ PyObject_HEAD\r
+ PyTypeObject *type;\r
+ PyObject *obj;\r
+ PyTypeObject *obj_type;\r
+} superobject;\r
+\r
+static PyMemberDef super_members[] = {\r
+ {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,\r
+ "the class invoking super()"},\r
+ {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,\r
+ "the instance invoking super(); may be None"},\r
+ {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,\r
+ "the type of the instance invoking super(); may be None"},\r
+ {0}\r
+};\r
+\r
+static void\r
+super_dealloc(PyObject *self)\r
+{\r
+ superobject *su = (superobject *)self;\r
+\r
+ _PyObject_GC_UNTRACK(self);\r
+ Py_XDECREF(su->obj);\r
+ Py_XDECREF(su->type);\r
+ Py_XDECREF(su->obj_type);\r
+ Py_TYPE(self)->tp_free(self);\r
+}\r
+\r
+static PyObject *\r
+super_repr(PyObject *self)\r
+{\r
+ superobject *su = (superobject *)self;\r
+\r
+ if (su->obj_type)\r
+ return PyString_FromFormat(\r
+ "<super: <class '%s'>, <%s object>>",\r
+ su->type ? su->type->tp_name : "NULL",\r
+ su->obj_type->tp_name);\r
+ else\r
+ return PyString_FromFormat(\r
+ "<super: <class '%s'>, NULL>",\r
+ su->type ? su->type->tp_name : "NULL");\r
+}\r
+\r
+static PyObject *\r
+super_getattro(PyObject *self, PyObject *name)\r
+{\r
+ superobject *su = (superobject *)self;\r
+ int skip = su->obj_type == NULL;\r
+\r
+ if (!skip) {\r
+ /* We want __class__ to return the class of the super object\r
+ (i.e. super, or a subclass), not the class of su->obj. */\r
+ skip = (PyString_Check(name) &&\r
+ PyString_GET_SIZE(name) == 9 &&\r
+ strcmp(PyString_AS_STRING(name), "__class__") == 0);\r
+ }\r
+\r
+ if (!skip) {\r
+ PyObject *mro, *res, *tmp, *dict;\r
+ PyTypeObject *starttype;\r
+ descrgetfunc f;\r
+ Py_ssize_t i, n;\r
+\r
+ starttype = su->obj_type;\r
+ mro = starttype->tp_mro;\r
+\r
+ if (mro == NULL)\r
+ n = 0;\r
+ else {\r
+ assert(PyTuple_Check(mro));\r
+ n = PyTuple_GET_SIZE(mro);\r
+ }\r
+ for (i = 0; i < n; i++) {\r
+ if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))\r
+ break;\r
+ }\r
+ i++;\r
+ res = NULL;\r
+ for (; i < n; i++) {\r
+ tmp = PyTuple_GET_ITEM(mro, i);\r
+ if (PyType_Check(tmp))\r
+ dict = ((PyTypeObject *)tmp)->tp_dict;\r
+ else if (PyClass_Check(tmp))\r
+ dict = ((PyClassObject *)tmp)->cl_dict;\r
+ else\r
+ continue;\r
+ res = PyDict_GetItem(dict, name);\r
+ if (res != NULL) {\r
+ Py_INCREF(res);\r
+ f = Py_TYPE(res)->tp_descr_get;\r
+ if (f != NULL) {\r
+ tmp = f(res,\r
+ /* Only pass 'obj' param if\r
+ this is instance-mode super\r
+ (See SF ID #743627)\r
+ */\r
+ (su->obj == (PyObject *)\r
+ su->obj_type\r
+ ? (PyObject *)NULL\r
+ : su->obj),\r
+ (PyObject *)starttype);\r
+ Py_DECREF(res);\r
+ res = tmp;\r
+ }\r
+ return res;\r
+ }\r
+ }\r
+ }\r
+ return PyObject_GenericGetAttr(self, name);\r
+}\r
+\r
+static PyTypeObject *\r
+supercheck(PyTypeObject *type, PyObject *obj)\r
+{\r
+ /* Check that a super() call makes sense. Return a type object.\r
+\r
+ obj can be a new-style class, or an instance of one:\r
+\r
+ - If it is a class, it must be a subclass of 'type'. This case is\r
+ used for class methods; the return value is obj.\r
+\r
+ - If it is an instance, it must be an instance of 'type'. This is\r
+ the normal case; the return value is obj.__class__.\r
+\r
+ But... when obj is an instance, we want to allow for the case where\r
+ Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!\r
+ This will allow using super() with a proxy for obj.\r
+ */\r
+\r
+ /* Check for first bullet above (special case) */\r
+ if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {\r
+ Py_INCREF(obj);\r
+ return (PyTypeObject *)obj;\r
+ }\r
+\r
+ /* Normal case */\r
+ if (PyType_IsSubtype(Py_TYPE(obj), type)) {\r
+ Py_INCREF(Py_TYPE(obj));\r
+ return Py_TYPE(obj);\r
+ }\r
+ else {\r
+ /* Try the slow way */\r
+ static PyObject *class_str = NULL;\r
+ PyObject *class_attr;\r
+\r
+ if (class_str == NULL) {\r
+ class_str = PyString_FromString("__class__");\r
+ if (class_str == NULL)\r
+ return NULL;\r
+ }\r
+\r
+ class_attr = PyObject_GetAttr(obj, class_str);\r
+\r
+ if (class_attr != NULL &&\r
+ PyType_Check(class_attr) &&\r
+ (PyTypeObject *)class_attr != Py_TYPE(obj))\r
+ {\r
+ int ok = PyType_IsSubtype(\r
+ (PyTypeObject *)class_attr, type);\r
+ if (ok)\r
+ return (PyTypeObject *)class_attr;\r
+ }\r
+\r
+ if (class_attr == NULL)\r
+ PyErr_Clear();\r
+ else\r
+ Py_DECREF(class_attr);\r
+ }\r
+\r
+ PyErr_SetString(PyExc_TypeError,\r
+ "super(type, obj): "\r
+ "obj must be an instance or subtype of type");\r
+ return NULL;\r
+}\r
+\r
+static PyObject *\r
+super_descr_get(PyObject *self, PyObject *obj, PyObject *type)\r
+{\r
+ superobject *su = (superobject *)self;\r
+ superobject *newobj;\r
+\r
+ if (obj == NULL || obj == Py_None || su->obj != NULL) {\r
+ /* Not binding to an object, or already bound */\r
+ Py_INCREF(self);\r
+ return self;\r
+ }\r
+ if (Py_TYPE(su) != &PySuper_Type)\r
+ /* If su is an instance of a (strict) subclass of super,\r
+ call its type */\r
+ return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),\r
+ su->type, obj, NULL);\r
+ else {\r
+ /* Inline the common case */\r
+ PyTypeObject *obj_type = supercheck(su->type, obj);\r
+ if (obj_type == NULL)\r
+ return NULL;\r
+ newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,\r
+ NULL, NULL);\r
+ if (newobj == NULL)\r
+ return NULL;\r
+ Py_INCREF(su->type);\r
+ Py_INCREF(obj);\r
+ newobj->type = su->type;\r
+ newobj->obj = obj;\r
+ newobj->obj_type = obj_type;\r
+ return (PyObject *)newobj;\r
+ }\r
+}\r
+\r
+static int\r
+super_init(PyObject *self, PyObject *args, PyObject *kwds)\r
+{\r
+ superobject *su = (superobject *)self;\r
+ PyTypeObject *type;\r
+ PyObject *obj = NULL;\r
+ PyTypeObject *obj_type = NULL;\r
+\r
+ if (!_PyArg_NoKeywords("super", kwds))\r
+ return -1;\r
+ if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))\r
+ return -1;\r
+ if (obj == Py_None)\r
+ obj = NULL;\r
+ if (obj != NULL) {\r
+ obj_type = supercheck(type, obj);\r
+ if (obj_type == NULL)\r
+ return -1;\r
+ Py_INCREF(obj);\r
+ }\r
+ Py_INCREF(type);\r
+ su->type = type;\r
+ su->obj = obj;\r
+ su->obj_type = obj_type;\r
+ return 0;\r
+}\r
+\r
+PyDoc_STRVAR(super_doc,\r
+"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"\r
+"super(type) -> unbound super object\n"\r
+"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"\r
+"Typical use to call a cooperative superclass method:\n"\r
+"class C(B):\n"\r
+" def meth(self, arg):\n"\r
+" super(C, self).meth(arg)");\r
+\r
+static int\r
+super_traverse(PyObject *self, visitproc visit, void *arg)\r
+{\r
+ superobject *su = (superobject *)self;\r
+\r
+ Py_VISIT(su->obj);\r
+ Py_VISIT(su->type);\r
+ Py_VISIT(su->obj_type);\r
+\r
+ return 0;\r
+}\r
+\r
+PyTypeObject PySuper_Type = {\r
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)\r
+ "super", /* tp_name */\r
+ sizeof(superobject), /* tp_basicsize */\r
+ 0, /* tp_itemsize */\r
+ /* methods */\r
+ super_dealloc, /* tp_dealloc */\r
+ 0, /* tp_print */\r
+ 0, /* tp_getattr */\r
+ 0, /* tp_setattr */\r
+ 0, /* tp_compare */\r
+ super_repr, /* tp_repr */\r
+ 0, /* tp_as_number */\r
+ 0, /* tp_as_sequence */\r
+ 0, /* tp_as_mapping */\r
+ 0, /* tp_hash */\r
+ 0, /* tp_call */\r
+ 0, /* tp_str */\r
+ super_getattro, /* tp_getattro */\r
+ 0, /* tp_setattro */\r
+ 0, /* tp_as_buffer */\r
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |\r
+ Py_TPFLAGS_BASETYPE, /* tp_flags */\r
+ super_doc, /* tp_doc */\r
+ super_traverse, /* tp_traverse */\r
+ 0, /* tp_clear */\r
+ 0, /* tp_richcompare */\r
+ 0, /* tp_weaklistoffset */\r
+ 0, /* tp_iter */\r
+ 0, /* tp_iternext */\r
+ 0, /* tp_methods */\r
+ super_members, /* tp_members */\r
+ 0, /* tp_getset */\r
+ 0, /* tp_base */\r
+ 0, /* tp_dict */\r
+ super_descr_get, /* tp_descr_get */\r
+ 0, /* tp_descr_set */\r
+ 0, /* tp_dictoffset */\r
+ super_init, /* tp_init */\r
+ PyType_GenericAlloc, /* tp_alloc */\r
+ PyType_GenericNew, /* tp_new */\r
+ PyObject_GC_Del, /* tp_free */\r
+};\r