More fixing for modern Pythons. No longer works with 2.2. Sorry.
[pyke] / catacomb.c
index acc0e35..0e1c711 100644 (file)
@@ -236,18 +236,35 @@ void addmethods(const PyMethodDef *m)
 
 static const PyTypeObject emptytype = { 0 };
 
-void *newtype(PyTypeObject *metaty, const PyTypeObject *skel)
+void *newtype(PyTypeObject *metaty,
+             const PyTypeObject *skel,
+             const char *name)
 {
-  PyTypeObject *ty =
-#if PY_VERSION_HEX < 0x02030000
-    (PyTypeObject *)_PyObject_GC_Malloc(metaty, 0);
-#else
-    (PyTypeObject *)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty, 0));
-#endif
+  PyHeapTypeObject *ty =
+    (PyHeapTypeObject *)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty, 0));
   if (!skel) skel = &emptytype;
   memcpy(ty, skel, sizeof(*skel));
-  if (ty->tp_base) Py_INCREF(ty->tp_base);
-  PyObject_INIT(ty, metaty);
+  if (ty->type.tp_base) Py_INCREF(ty->type.tp_base);
+#define COPY(blah) do {                                                        \
+    if (ty->type.tp_as_##blah) {                                       \
+      memcpy(&ty->as_##blah,                                           \
+            ty->type.tp_as_##blah,                                     \
+            sizeof(ty->as_##blah));                                    \
+      ty->type.tp_as_##blah = &ty->as_##blah;                          \
+    }                                                                  \
+  } while (0)
+  COPY(number);
+  COPY(sequence);
+  COPY(mapping);
+  COPY(buffer);
+#undef COPY
+  if (name)
+    ty->name = PyString_FromString(name);
+  else if (ty->type.tp_name)
+    ty->name = PyString_FromString(ty->type.tp_name);
+  if (ty->name)
+    ty->type.tp_name = PyString_AS_STRING(ty->name);
+  PyObject_INIT(&ty->type, metaty);
   Py_INCREF(metaty);
   return (ty);
 }
@@ -264,7 +281,7 @@ static PyObject *smallprimes(void)
 
 PyTypeObject *inittype(PyTypeObject *tyskel)
 {
-  PyTypeObject *ty = newtype(&PyType_Type, tyskel);
+  PyTypeObject *ty = newtype(&PyType_Type, tyskel, 0);
   ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
   PyType_Ready(ty);
   return (ty);