summaryrefslogtreecommitdiffstats
path: root/Objects/typeobject.c
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2010-12-03 20:14:31 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2010-12-03 20:14:31 (GMT)
commit4d0d471a8031de90a2b1ce99c4ac4780e60b3bc9 (patch)
treec8f1fef715f8d158e58f17cab14af65455de1d77 /Objects/typeobject.c
parentc4df7845143f9afe0d20f4421a41904f3cbb991a (diff)
downloadcpython-4d0d471a8031de90a2b1ce99c4ac4780e60b3bc9.zip
cpython-4d0d471a8031de90a2b1ce99c4ac4780e60b3bc9.tar.gz
cpython-4d0d471a8031de90a2b1ce99c4ac4780e60b3bc9.tar.bz2
Merge branches/pep-0384.
Diffstat (limited to 'Objects/typeobject.c')
-rw-r--r--Objects/typeobject.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index e0001db..a5863dd 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -2304,6 +2304,44 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
return (PyObject *)type;
}
+static short slotoffsets[] = {
+ -1, /* invalid slot */
+#include "typeslots.inc"
+};
+
+PyObject* PyType_FromSpec(PyType_Spec *spec)
+{
+ PyHeapTypeObject *res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0);
+ char *res_start = (char*)res;
+ PyType_Slot *slot;
+
+ res->ht_name = PyUnicode_FromString(spec->name);
+ if (!res->ht_name)
+ goto fail;
+ res->ht_type.tp_name = _PyUnicode_AsString(res->ht_name);
+ if (!res->ht_type.tp_name)
+ goto fail;
+
+ res->ht_type.tp_basicsize = spec->basicsize;
+ res->ht_type.tp_itemsize = spec->itemsize;
+ res->ht_type.tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE;
+
+ for (slot = spec->slots; slot->slot; slot++) {
+ if (slot->slot >= sizeof(slotoffsets)/sizeof(slotoffsets[0])) {
+ PyErr_SetString(PyExc_RuntimeError, "invalid slot offset");
+ goto fail;
+ }
+ *(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc;
+ }
+
+ return (PyObject*)res;
+
+ fail:
+ Py_DECREF(res);
+ return NULL;
+}
+
+
/* Internal API to look for a name through the MRO.
This returns a borrowed reference, and doesn't set an exception! */
PyObject *