Merge branches/pep-0384.
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 6ca096a..32b8bb5 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -389,7 +389,7 @@
     }
     else if (PySlice_Check(index)) {
         Py_ssize_t start, stop, step, slicelength, cur, i;
-        if (PySlice_GetIndicesEx((PySliceObject *)index,
+        if (PySlice_GetIndicesEx(index,
                                  PyByteArray_GET_SIZE(self),
                                  &start, &stop, &step, &slicelength) < 0) {
             return NULL;
@@ -573,7 +573,7 @@
         }
     }
     else if (PySlice_Check(index)) {
-        if (PySlice_GetIndicesEx((PySliceObject *)index,
+        if (PySlice_GetIndicesEx(index,
                                  PyByteArray_GET_SIZE(self),
                                  &start, &stop, &step, &slicelen) < 0) {
             return -1;
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index d3b8a4f..4aa0748 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -911,7 +911,7 @@
         char* result_buf;
         PyObject* result;
 
-        if (PySlice_GetIndicesEx((PySliceObject*)item,
+        if (PySlice_GetIndicesEx(item,
                          PyBytes_GET_SIZE(self),
                          &start, &stop, &step, &slicelength) < 0) {
             return NULL;
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 221f368..45f9f57 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -627,7 +627,7 @@
     }
 
     result = PyEval_EvalCodeEx(
-        (PyCodeObject *)PyFunction_GET_CODE(func),
+        PyFunction_GET_CODE(func),
         PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
         &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
         k, nk, d, nd,
diff --git a/Objects/listobject.c b/Objects/listobject.c
index fbc05fd..bcc6bc0 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2397,7 +2397,7 @@
         PyObject* it;
         PyObject **src, **dest;
 
-        if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
+        if (PySlice_GetIndicesEx(item, Py_SIZE(self),
                          &start, &stop, &step, &slicelength) < 0) {
             return NULL;
         }
@@ -2446,7 +2446,7 @@
     else if (PySlice_Check(item)) {
         Py_ssize_t start, stop, step, slicelength;
 
-        if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
+        if (PySlice_GetIndicesEx(item, Py_SIZE(self),
                          &start, &stop, &step, &slicelength) < 0) {
             return -1;
         }
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c
index ba9e08b..a05b97b 100644
--- a/Objects/memoryobject.c
+++ b/Objects/memoryobject.c
@@ -599,7 +599,7 @@
     else if (PySlice_Check(key)) {
         Py_ssize_t start, stop, step, slicelength;
 
-        if (PySlice_GetIndicesEx((PySliceObject*)key, get_shape0(view),
+        if (PySlice_GetIndicesEx(key, get_shape0(view),
                                  &start, &stop, &step, &slicelength) < 0) {
             return NULL;
         }
@@ -678,7 +678,7 @@
     else if (PySlice_Check(key)) {
         Py_ssize_t stop, step;
 
-        if (PySlice_GetIndicesEx((PySliceObject*)key, get_shape0(view),
+        if (PySlice_GetIndicesEx(key, get_shape0(view),
                          &start, &stop, &step, &len) < 0) {
             return -1;
         }
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 1e3349d..2c095a0 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -74,7 +74,7 @@
         module->m_base.m_index = max_module_number;
     }
     name = module->m_name;
-    if (module_api_version != PYTHON_API_VERSION) {
+    if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
         int err;
         err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
             "Python C API version mismatch for module %.100s: "
diff --git a/Objects/object.c b/Objects/object.c
index 082dd78..17e5069 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1755,7 +1755,6 @@
 
 #endif
 
-
 /* Hack to force loading of pycapsule.o */
 PyTypeObject *_PyCapsule_hack = &PyCapsule_Type;
 
@@ -1900,6 +1899,19 @@
     }
 }
 
+#ifndef Py_TRACE_REFS
+/* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
+   Define this here, so we can undefine the macro. */
+#undef _Py_Dealloc
+PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
+void
+_Py_Dealloc(PyObject *op)
+{
+    _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
+    (*Py_TYPE(op)->tp_dealloc)(op);
+}
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index bf42b1e..3ac829a 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -431,7 +431,6 @@
         return range_item(self, i);
     }
     if (PySlice_Check(item)) {
-        PySliceObject *slice = (PySliceObject*)item;
         Py_ssize_t start, stop, step, len, rlen;
         rangeobject *result;
         PyObject *substart = NULL, *substep = NULL, *substop = NULL;
@@ -441,7 +440,7 @@
             return NULL;
         }
 
-        if (PySlice_GetIndicesEx(slice, rlen,
+        if (PySlice_GetIndicesEx(item, rlen,
                                 &start, &stop, &step, &len) < 0) {
             return NULL;
         }
@@ -450,7 +449,7 @@
             Py_INCREF(substep);
         } else {
             /* NB: slice step != Py_None here */
-            substep = PyNumber_Multiply(self->step, slice->step);
+            substep = PyNumber_Multiply(self->step, ((PySliceObject*)item)->step);
             if (substep == NULL)
                 goto fail;
         }
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index b617891..51c53a8 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -99,9 +99,10 @@
 }
 
 int
-PySlice_GetIndices(PySliceObject *r, Py_ssize_t length,
+PySlice_GetIndices(PyObject *_r, Py_ssize_t length,
                    Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
 {
+    PySliceObject *r = (PySliceObject*)_r;
     /* XXX support long ints */
     if (r->step == Py_None) {
         *step = 1;
@@ -130,10 +131,11 @@
 }
 
 int
-PySlice_GetIndicesEx(PySliceObject *r, Py_ssize_t length,
+PySlice_GetIndicesEx(PyObject *_r, Py_ssize_t length,
                      Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
                      Py_ssize_t *slicelength)
 {
+    PySliceObject *r = (PySliceObject*)_r;
     /* this is harder to get right than you might think */
 
     Py_ssize_t defstart, defstop;
@@ -256,7 +258,7 @@
         return NULL;
     }
 
-    if (PySlice_GetIndicesEx(self, ilen, &start, &stop,
+    if (PySlice_GetIndicesEx((PyObject*)self, ilen, &start, &stop,
                              &step, &slicelength) < 0) {
         return NULL;
     }
diff --git a/Objects/structseq.c b/Objects/structseq.c
index 75e2250..ef17f49 100644
--- a/Objects/structseq.c
+++ b/Objects/structseq.c
@@ -43,6 +43,18 @@
     return (PyObject*)obj;
 }
 
+void
+PyStructSequence_SetItem(PyObject* op, Py_ssize_t i, PyObject* v)
+{
+    PyStructSequence_SET_ITEM(op, i, v);
+}
+
+PyObject*
+PyStructSequence_GetItem(PyObject* op, Py_ssize_t i)
+{
+    return PyStructSequence_GET_ITEM(op, i);
+}
+
 static void
 structseq_dealloc(PyStructSequence *obj)
 {
@@ -365,3 +377,11 @@
     SET_DICT_FROM_INT(real_length_key, n_members);
     SET_DICT_FROM_INT(unnamed_fields_key, n_unnamed_members);
 }
+
+PyTypeObject*
+PyStructSequence_NewType(PyStructSequence_Desc *desc)
+{
+    PyTypeObject *result = (PyTypeObject*)PyType_GenericAlloc(&PyType_Type, 0);
+    PyStructSequence_InitType(result, desc);
+    return result;
+}
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index 390b670..72b79c9 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -689,7 +689,7 @@
         PyObject* it;
         PyObject **src, **dest;
 
-        if (PySlice_GetIndicesEx((PySliceObject*)item,
+        if (PySlice_GetIndicesEx(item,
                          PyTuple_GET_SIZE(self),
                          &start, &stop, &step, &slicelength) < 0) {
             return NULL;
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 @@
     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 *
diff --git a/Objects/typeslots.inc b/Objects/typeslots.inc
new file mode 100644
index 0000000..79e3db8
--- /dev/null
+++ b/Objects/typeslots.inc
@@ -0,0 +1,71 @@
+offsetof(PyHeapTypeObject, as_buffer.bf_getbuffer),
+offsetof(PyHeapTypeObject, as_buffer.bf_releasebuffer),
+offsetof(PyHeapTypeObject, as_mapping.mp_ass_subscript),
+offsetof(PyHeapTypeObject, as_mapping.mp_length),
+offsetof(PyHeapTypeObject, as_mapping.mp_subscript),
+offsetof(PyHeapTypeObject, as_number.nb_absolute),
+offsetof(PyHeapTypeObject, as_number.nb_add),
+offsetof(PyHeapTypeObject, as_number.nb_and),
+offsetof(PyHeapTypeObject, as_number.nb_bool),
+offsetof(PyHeapTypeObject, as_number.nb_divmod),
+offsetof(PyHeapTypeObject, as_number.nb_float),
+offsetof(PyHeapTypeObject, as_number.nb_floor_divide),
+offsetof(PyHeapTypeObject, as_number.nb_index),
+offsetof(PyHeapTypeObject, as_number.nb_inplace_add),
+offsetof(PyHeapTypeObject, as_number.nb_inplace_and),
+offsetof(PyHeapTypeObject, as_number.nb_inplace_floor_divide),
+offsetof(PyHeapTypeObject, as_number.nb_inplace_lshift),
+offsetof(PyHeapTypeObject, as_number.nb_inplace_multiply),
+offsetof(PyHeapTypeObject, as_number.nb_inplace_or),
+offsetof(PyHeapTypeObject, as_number.nb_inplace_power),
+offsetof(PyHeapTypeObject, as_number.nb_inplace_remainder),
+offsetof(PyHeapTypeObject, as_number.nb_inplace_rshift),
+offsetof(PyHeapTypeObject, as_number.nb_inplace_subtract),
+offsetof(PyHeapTypeObject, as_number.nb_inplace_true_divide),
+offsetof(PyHeapTypeObject, as_number.nb_inplace_xor),
+offsetof(PyHeapTypeObject, as_number.nb_int),
+offsetof(PyHeapTypeObject, as_number.nb_invert),
+offsetof(PyHeapTypeObject, as_number.nb_lshift),
+offsetof(PyHeapTypeObject, as_number.nb_multiply),
+offsetof(PyHeapTypeObject, as_number.nb_negative),
+offsetof(PyHeapTypeObject, as_number.nb_or),
+offsetof(PyHeapTypeObject, as_number.nb_positive),
+offsetof(PyHeapTypeObject, as_number.nb_power),
+offsetof(PyHeapTypeObject, as_number.nb_remainder),
+offsetof(PyHeapTypeObject, as_number.nb_rshift),
+offsetof(PyHeapTypeObject, as_number.nb_subtract),
+offsetof(PyHeapTypeObject, as_number.nb_true_divide),
+offsetof(PyHeapTypeObject, as_number.nb_xor),
+offsetof(PyHeapTypeObject, as_sequence.sq_ass_item),
+offsetof(PyHeapTypeObject, as_sequence.sq_concat),
+offsetof(PyHeapTypeObject, as_sequence.sq_contains),
+offsetof(PyHeapTypeObject, as_sequence.sq_inplace_concat),
+offsetof(PyHeapTypeObject, as_sequence.sq_inplace_repeat),
+offsetof(PyHeapTypeObject, as_sequence.sq_item),
+offsetof(PyHeapTypeObject, as_sequence.sq_length),
+offsetof(PyHeapTypeObject, as_sequence.sq_repeat),
+offsetof(PyHeapTypeObject, ht_type.tp_alloc),
+offsetof(PyHeapTypeObject, ht_type.tp_base),
+offsetof(PyHeapTypeObject, ht_type.tp_bases),
+offsetof(PyHeapTypeObject, ht_type.tp_call),
+offsetof(PyHeapTypeObject, ht_type.tp_clear),
+offsetof(PyHeapTypeObject, ht_type.tp_dealloc),
+offsetof(PyHeapTypeObject, ht_type.tp_del),
+offsetof(PyHeapTypeObject, ht_type.tp_descr_get),
+offsetof(PyHeapTypeObject, ht_type.tp_descr_set),
+offsetof(PyHeapTypeObject, ht_type.tp_doc),
+offsetof(PyHeapTypeObject, ht_type.tp_getattr),
+offsetof(PyHeapTypeObject, ht_type.tp_getattro),
+offsetof(PyHeapTypeObject, ht_type.tp_hash),
+offsetof(PyHeapTypeObject, ht_type.tp_init),
+offsetof(PyHeapTypeObject, ht_type.tp_is_gc),
+offsetof(PyHeapTypeObject, ht_type.tp_iter),
+offsetof(PyHeapTypeObject, ht_type.tp_iternext),
+offsetof(PyHeapTypeObject, ht_type.tp_methods),
+offsetof(PyHeapTypeObject, ht_type.tp_new),
+offsetof(PyHeapTypeObject, ht_type.tp_repr),
+offsetof(PyHeapTypeObject, ht_type.tp_richcompare),
+offsetof(PyHeapTypeObject, ht_type.tp_setattr),
+offsetof(PyHeapTypeObject, ht_type.tp_setattro),
+offsetof(PyHeapTypeObject, ht_type.tp_str),
+offsetof(PyHeapTypeObject, ht_type.tp_traverse),
diff --git a/Objects/typeslots.py b/Objects/typeslots.py
new file mode 100644
index 0000000..bcc3196
--- /dev/null
+++ b/Objects/typeslots.py
@@ -0,0 +1,24 @@
+#!/usr/bin/python
+# Usage: typeslots.py < Include/typeslots.h > typeslots.inc
+
+import sys, re
+
+res = {}
+for line in sys.stdin:
+    m = re.match("#define Py_([a-z_]+) ([0-9]+)", line)
+    member = m.group(1)
+    if member.startswith("tp_"):
+        member = "ht_type."+member
+    elif member.startswith("nb_"):
+        member = "as_number."+member
+    elif member.startswith("mp_"):
+        member = "as_mapping."+member
+    elif member.startswith("sq_"):
+        member = "as_sequence."+member
+    elif member.startswith("bf_"):
+        member = "as_buffer."+member
+    res[int(m.group(2))] = member
+
+M = max(res.keys())+1
+for i in range(1,M):
+    print "offsetof(PyHeapTypeObject, %s)," % res[i]
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index f66773e..d3a2d1b 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1263,7 +1263,7 @@
 }
 
 Py_ssize_t
-PyUnicode_AsWideChar(PyUnicodeObject *unicode,
+PyUnicode_AsWideChar(PyObject *unicode,
                      wchar_t *w,
                      Py_ssize_t size)
 {
@@ -1271,7 +1271,7 @@
         PyErr_BadInternalCall();
         return -1;
     }
-    return unicode_aswidechar(unicode, w, size);
+    return unicode_aswidechar((PyUnicodeObject*)unicode, w, size);
 }
 
 wchar_t*
@@ -9222,7 +9222,7 @@
         Py_UNICODE* result_buf;
         PyObject* result;
 
-        if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self),
+        if (PySlice_GetIndicesEx(item, PyUnicode_GET_SIZE(self),
                                  &start, &stop, &step, &slicelength) < 0) {
             return NULL;
         }