diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2010-12-03 20:14:31 +0000 |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2010-12-03 20:14:31 +0000 |
commit | 4d0d471a8031de90a2b1ce99c4ac4780e60b3bc9 (patch) | |
tree | c8f1fef715f8d158e58f17cab14af65455de1d77 /Objects/typeobject.c | |
parent | c4df7845143f9afe0d20f4421a41904f3cbb991a (diff) | |
download | cpython-4d0d471a8031de90a2b1ce99c4ac4780e60b3bc9.tar.gz cpython-4d0d471a8031de90a2b1ce99c4ac4780e60b3bc9.zip |
Merge branches/pep-0384.
Diffstat (limited to 'Objects/typeobject.c')
-rw-r--r-- | Objects/typeobject.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c index e0001db189e..a5863dd0a4f 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 * |