More fixing for modern Pythons. No longer works with 2.2. Sorry.
authormdw <mdw>
Fri, 29 Apr 2005 13:11:19 +0000 (13:11 +0000)
committermdw <mdw>
Fri, 29 Apr 2005 13:11:19 +0000 (13:11 +0000)
catacomb-python.h
catacomb.c

index 368c7bb..ff99a78 100644 (file)
@@ -221,7 +221,7 @@ extern int mp_tolong_checked(mp *, long *);
 /*----- Abstract fields ---------------------------------------------------*/
 
 typedef struct field_pyobj {
-  PyTypeObject ty;
+  PyHeapTypeObject ty;
   field *f;
 } field_pyobj;
 
@@ -274,7 +274,7 @@ extern void getecptout(ec *, PyObject *);
 extern int convec(PyObject *, void *);
 
 typedef struct eccurve_pyobj {
-  PyTypeObject ty;
+  PyHeapTypeObject ty;
   ec_curve *c;
   PyObject *fobj;
 } eccurve_pyobj;
@@ -328,7 +328,7 @@ extern PyTypeObject *ge_pytype;
 extern PyObject *ge_pywrap(PyObject *, ge *);
 
 typedef struct group_pyobj {
-  PyTypeObject ty;
+  PyHeapTypeObject ty;
   group *g;
 } group_pyobj;
 
@@ -383,7 +383,7 @@ extern PyObject *keysz_pywrap(const octet *);
 /*----- Symmetric cryptography --------------------------------------------*/
 
 typedef struct gccipher_pyobj {
-  PyTypeObject ty;
+  PyHeapTypeObject ty;
   gccipher *cc;
 } gccipher_pyobj;
 
@@ -409,7 +409,7 @@ extern PyObject *gcipher_pywrap(PyObject *, gcipher *, unsigned);
 extern int convgcipher(PyObject *, void *);
 
 typedef struct gchash_pyobj {
-  PyTypeObject ty;
+  PyHeapTypeObject ty;
   gchash *ch;
 } gchash_pyobj;
 
@@ -436,7 +436,7 @@ extern int convghash(PyObject *, void *);
 extern int convgmhash(PyObject *, void *);
 
 typedef struct gcmac_pyobj {
-  PyTypeObject ty;
+  PyHeapTypeObject ty;
   gcmac *cm;
 } gcmac_pyobj;
 
@@ -448,16 +448,14 @@ extern PyObject *gcmac_pywrap(gcmac *);
 extern int convgcmac(PyObject *, void *);
 
 typedef struct gmac_pyobj {
-  PyTypeObject ty;
+  PyHeapTypeObject ty;
   unsigned f;
   gmac *m;
-  PyObject *nameobj;
 } gmac_pyobj;
 
 extern PyTypeObject *gmac_pytype;
 #define GMAC_PYCHECK(o) PyObject_TypeCheck((o), gmac_pytype)
 #define GMAC_M(o) (((gmac_pyobj *)(o))->m)
-#define GMAC_NAMEOBJ(o) (((gmac_pyobj *)(o))->nameobj)
 #define GMAC_F(o) (((gmac_pyobj *)(o))->f)
 extern PyObject *gmac_pywrap(PyObject *, gmac *, unsigned);
 extern int convgmac(PyObject *, void *);
@@ -510,7 +508,7 @@ extern int convbool(PyObject *, void *);
 extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *);
 extern PyObject *getbool(int);
 extern PyObject *getu32(uint32);
-extern void *newtype(PyTypeObject *, const PyTypeObject *);
+extern void *newtype(PyTypeObject *, const PyTypeObject *, const char *);
 extern PyTypeObject *inittype(PyTypeObject *);
 extern void addmethods(const PyMethodDef *);
 
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);