*.c: Use the new `Py_TYPE' and `Py_SIZE' macros; define them if necessary.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 20 Oct 2019 23:19:44 +0000 (00:19 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 25 Nov 2019 17:43:08 +0000 (17:43 +0000)
Python 2.6 introduced macros to access object header fields, which is
handy because Python 3 changes the way object headers are
structured.  (I think the binary layout is unchanged, but the C-level
structuring is definitely different.)

This is the start of Python 3 porting work.

algorithms.c
bytestring.c
catacomb-python.h
mp.c
pyke/pyke.h

index 1b1545c..39449e9 100644 (file)
@@ -558,7 +558,7 @@ PyObject *gccipher_pywrap(gccipher *cc)
 static void gcipher_pydealloc(PyObject *me)
 {
   GC_DESTROY(GCIPHER_C(me));
-  Py_DECREF(me->ob_type);
+  Py_DECREF(Py_TYPE(me));
   FREEOBJ(me);
 }
 
@@ -933,7 +933,7 @@ static PyObject *gcaead_pywrap(gcaead *aec)
 }
 
 static void gaeadkey_pydealloc(PyObject *me)
-  { GAEAD_DESTROY(GAEADKEY_K(me)); Py_DECREF(me->ob_type); FREEOBJ(me); }
+  { GAEAD_DESTROY(GAEADKEY_K(me)); Py_DECREF(Py_TYPE(me)); FREEOBJ(me); }
 
 static PyObject *gcaeget_name(PyObject *me, void *hunoz)
   { return (PyString_FromString(GCAEAD_AEC(me)->name)); }
@@ -980,7 +980,7 @@ static PyObject *gaekmeth_aad(PyObject *me)
 
   if (k->ops->c->f&AEADF_AADNDEP)
     VALERR("aad must be associated with enc/dec op");
-  rc = gaeadaad_pywrap((PyObject *)GCAEAD_AAD(me->ob_type),
+  rc = gaeadaad_pywrap((PyObject *)GCAEAD_AAD(Py_TYPE(me)),
                       GAEAD_AAD(k), 0, 0);
 end:
   return (rc);
@@ -1031,7 +1031,7 @@ static PyObject *gaekmeth_enc(PyObject *me, PyObject *arg, PyObject *kw)
     goto end;
   e = GAEAD_ENC(GAEADKEY_K(me), n, nsz, hsz, msz, tsz);
   if (!e) VALERR("bad aead parameter combination");
-  rc = gaeadenc_pywrap((PyObject *)GCAEAD_ENC(me->ob_type),
+  rc = gaeadenc_pywrap((PyObject *)GCAEAD_ENC(Py_TYPE(me)),
                       e, f, hsz, msz, tsz);
 end:
   return (rc);
@@ -1056,7 +1056,7 @@ static PyObject *gaekmeth_dec(PyObject *me, PyObject *arg, PyObject *kw)
     goto end;
   d = GAEAD_DEC(GAEADKEY_K(me), n, nsz, hsz, csz, tsz);
   if (!d) VALERR("bad aead parameter combination");
-  rc = gaeaddec_pywrap((PyObject *)GCAEAD_DEC(me->ob_type),
+  rc = gaeaddec_pywrap((PyObject *)GCAEAD_DEC(Py_TYPE(me)),
                       d, f, hsz, csz, tsz);
 end:
   return (rc);
@@ -1087,7 +1087,7 @@ static void gaeadaad_pydealloc(PyObject *me)
   gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me;
 
   if (ga->a) GAEAD_DESTROY(ga->a);
-  Py_DECREF(me->ob_type); FREEOBJ(me);
+  Py_DECREF(Py_TYPE(me)); FREEOBJ(me);
 }
 
 static int gaea_check(PyObject *me)
@@ -1135,7 +1135,7 @@ static PyObject *gaeameth_copy(PyObject *me)
   if (gaea_check(me)) goto end;
   if (GAEADAAD_F(me)&AEADF_AADNDEP)
     VALERR("can't duplicate nonce-dependent aad");
-  rc = gaeadaad_pywrap((PyObject *)me->ob_type,
+  rc = gaeadaad_pywrap((PyObject *)Py_TYPE(me),
                       GAEAD_DUP(GAEADAAD_A(me)), 0, 0);
   GAEADAAD_HLEN(rc) = GAEADAAD_HLEN(me);
 end:
@@ -1232,7 +1232,7 @@ static void gaeadenc_pydealloc(PyObject *me)
   gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
 
   gaea_sever(&ge->aad); GAEAD_DESTROY(ge->e);
-  Py_DECREF(me->ob_type); FREEOBJ(me);
+  Py_DECREF(Py_TYPE(me)); FREEOBJ(me);
 }
 
 static PyObject *gaeeget_hsz(PyObject *me, void *hunoz)
@@ -1272,14 +1272,14 @@ static PyObject *gaeemeth_aad(PyObject *me)
   PyObject *rc = 0;
 
   if (!(ge->f&AEADF_AADNDEP))
-    rc = gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(ge->ob_type)->aad,
+    rc = gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(Py_TYPE(ge))->aad,
                         GAEAD_AAD(ge->e), 0, 0);
   else {
     if ((ge->f&AEADF_AADFIRST) && ge->mlen)
       VALERR("too late for aad");
     if (!ge->aad)
       ge->aad = (gaeadaad_pyobj *)
-       gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(ge->ob_type)->aad,
+       gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(Py_TYPE(ge))->aad,
                        GAEAD_AAD(ge->e), ge->f&(AEADF_PCHSZ | AEADF_NOAAD),
                        ge->hsz);
     Py_INCREF(ge->aad);
@@ -1352,7 +1352,7 @@ static PyObject *gaeemeth_done(PyObject *me, PyObject *arg, PyObject *kw)
   if (tszobj != Py_None && !convszt(tszobj, &tsz)) goto end;
   if (aad != Py_None &&
       !PyObject_TypeCheck(aad,
-                         (PyTypeObject *)GCAEADENC_KEY(me->ob_type)->aad))
+                         (PyTypeObject *)GCAEADENC_KEY(Py_TYPE(me))->aad))
     TYERR("wanted aad");
   if ((ge->f&AEADF_AADNDEP) && aad != Py_None && aad != (PyObject *)ge->aad)
     VALERR("mismatched aad");
@@ -1406,7 +1406,7 @@ static void gaeaddec_pydealloc(PyObject *me)
   gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
 
   gaea_sever(&gd->aad); GAEAD_DESTROY(GAEADDEC_D(me));
-  Py_DECREF(me->ob_type); FREEOBJ(me);
+  Py_DECREF(Py_TYPE(me)); FREEOBJ(me);
 }
 
 static PyObject *gaedget_hsz(PyObject *me, void *hunoz)
@@ -1445,12 +1445,12 @@ static PyObject *gaedmeth_aad(PyObject *me)
   gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
 
   if (!(gd->f&AEADF_AADNDEP))
-    return (gaeadaad_pywrap((PyObject *)GCAEADDEC_KEY(gd->ob_type)->aad,
+    return (gaeadaad_pywrap((PyObject *)GCAEADDEC_KEY(Py_TYPE(gd))->aad,
                            GAEAD_AAD(gd->d), 0, 0));
   else {
     if (!gd->aad)
       gd->aad = (gaeadaad_pyobj *)
-       gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(gd->ob_type)->aad,
+       gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(Py_TYPE(gd))->aad,
                        GAEAD_AAD(gd->d), gd->f&(AEADF_PCHSZ | AEADF_NOAAD),
                        gd->hsz);
     Py_INCREF(gd->aad);
@@ -1520,7 +1520,7 @@ static PyObject *gaedmeth_done(PyObject *me, PyObject *arg, PyObject *kw)
     goto end;
   if (aad != Py_None &&
       !PyObject_TypeCheck(aad,
-                         (PyTypeObject *)GCAEADENC_KEY(me->ob_type)->aad))
+                         (PyTypeObject *)GCAEADENC_KEY(Py_TYPE(me))->aad))
     TYERR("wanted aad");
   if ((gd->f&AEADF_AADNDEP) && aad != Py_None && aad != (PyObject *)gd->aad)
     VALERR("mismatched aad");
@@ -1993,7 +1993,7 @@ PyObject *ghash_pywrap(PyObject *cobj, ghash *h)
 static void ghash_pydealloc(PyObject *me)
 {
   GH_DESTROY(GHASH_H(me));
-  Py_DECREF(me->ob_type);
+  Py_DECREF(Py_TYPE(me));
   FREEOBJ(me);
 }
 
@@ -2007,7 +2007,7 @@ static PyObject *gchget_bufsz(PyObject *me, void *hunoz)
   { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); }
 
 static PyObject *ghmeth_copy(PyObject *me)
-  { return (ghash_pywrap((PyObject *)me->ob_type, GH_COPY(GHASH_H(me)))); }
+  { return (ghash_pywrap((PyObject *)Py_TYPE(me), GH_COPY(GHASH_H(me)))); }
 
 static PyObject *ghmeth_hash(PyObject *me, PyObject *arg)
 {
@@ -2278,7 +2278,7 @@ static PyObject *gmac_pywrap(PyObject *cobj, gmac *m)
 static void gmac_pydealloc(PyObject *me)
 {
   GM_DESTROY(GMAC_M(me));
-  Py_DECREF(me->ob_type);
+  Py_DECREF(Py_TYPE(me));
   PyType_Type.tp_dealloc(me);
 }
 
@@ -2534,9 +2534,9 @@ static PyObject *poly1305clsget_tagsz(PyObject *me, void *hunoz)
 static PyObject *polymeth_copy(PyObject *me)
 {
   poly1305hash_pyobj *ph;
-  ph = PyObject_NEW(poly1305hash_pyobj, me->ob_type);
+  ph = PyObject_NEW(poly1305hash_pyobj, Py_TYPE(me));
   poly1305_copy(&ph->ctx, P1305_CTX(me));
-  Py_INCREF(me->ob_type);
+  Py_INCREF(Py_TYPE(me));
   return ((PyObject *)ph);
 }
 
@@ -2597,7 +2597,7 @@ static PyObject *polymeth_concat(PyObject *me, PyObject *arg)
   if (!PyObject_TypeCheck(pre, poly1305hash_pytype) ||
       !PyObject_TypeCheck(suff, poly1305hash_pytype))
     TYERR("wanted a poly1305hash");
-  if (me->ob_type != pre->ob_type || me->ob_type != suff->ob_type)
+  if (Py_TYPE(me) != Py_TYPE(pre) || Py_TYPE(me) != Py_TYPE(suff))
     TYERR("key mismatch");
   if (P1305_CTX(pre)->nbuf) VALERR("prefix is not block-aligned");
   poly1305_concat(P1305_CTX(me), P1305_CTX(pre), P1305_CTX(suff));
@@ -2848,7 +2848,7 @@ end:
 static PyObject *kxvikmeth_copy(PyObject *me)
 {
   kxvik_pyobj *k = (kxvik_pyobj *)me, *rc = 0;
-  rc = (kxvik_pyobj *)k->ob_type->tp_alloc(k->ob_type, 0);
+  rc = (kxvik_pyobj *)Py_TYPE(k)->tp_alloc(Py_TYPE(k), 0);
   rc->s = k->s; rc->n = k->n;
   return ((PyObject *)rc);
 }
@@ -3093,7 +3093,7 @@ static PyObject *shakemeth_copy(PyObject *me)
 {
   shake_pyobj *rc = 0;
 
-  rc = PyObject_NEW(shake_pyobj, me->ob_type);
+  rc = PyObject_NEW(shake_pyobj, Py_TYPE(me));
   rc->h = *SHAKE_H(me);
   rc->st = SHAKE_ST(me);
   return ((PyObject *)rc);
@@ -3389,7 +3389,7 @@ end:
 }
 
 static void gprp_pydealloc(PyObject *me)
-  { Py_DECREF(me->ob_type); FREEOBJ(me); }
+  { Py_DECREF(Py_TYPE(me)); FREEOBJ(me); }
 
 static PyObject *gcprp_pywrap(const prpinfo *prp)
 {
index 3bf1b25..5fdd5d9 100644 (file)
@@ -192,7 +192,7 @@ static PyObject *bytestring_pyslice(PyObject *me, Py_ssize_t i, Py_ssize_t j)
   if (j < 0) j = 0;
   else if (j > n) j = n;
   if (j < i) i = j = 0;
-  if (i == 0 && j == n && me->ob_type == bytestring_pytype)
+  if (i == 0 && j == n && Py_TYPE(me) == bytestring_pytype)
     { Py_INCREF(me); rc = me; goto end; }
   rc = bytestring_pywrap(PyString_AS_STRING(me) + i, j - i);
 end:
index da6a8fe..cae27c5 100644 (file)
@@ -213,7 +213,7 @@ typedef struct fe_pyobj {
 extern PyTypeObject *fe_pytype;
 #define FE_PYCHECK(o) PyObject_TypeCheck((o), fe_pytype)
 #define FE_F(o) (((fe_pyobj *)(o))->f)
-#define FE_FOBJ(o) ((PyObject *)(o)->ob_type)
+#define FE_FOBJ(o) ((PyObject *)Py_TYPE(o))
 #define FE_X(o) (((fe_pyobj *)(o))->x)
 extern PyObject *fe_pywrap(PyObject *, mp *);
 
@@ -246,7 +246,7 @@ extern PyTypeObject *ecpt_pytype, *ecptcurve_pytype;
 #define ECPT_PYCHECK(o) PyObject_TypeCheck((o), ecpt_pytype)
 #define ECPTCURVE_PYCHECK(o) PyObject_TypeCheck((o), ecptcurve_pytype)
 #define ECPT_C(o) (((ecpt_pyobj *)(o))->c)
-#define ECPT_COBJ(o) ((PyObject *)(o)->ob_type)
+#define ECPT_COBJ(o) ((PyObject *)Py_TYPE(o))
 #define ECPT_FOBJ(o) ECCURVE_FOBJ(ECPT_COBJ((o)))
 #define ECPT_P(o) (&((ecpt_pyobj *)(o))->p)
 extern PyObject *ecpt_pywrap(PyObject *, ec *);
@@ -281,7 +281,7 @@ extern PyTypeObject *ge_pytype;
 #define GE_PYCHECK(o) PyObject_TypeCheck((o), ge_pytype)
 #define GE_X(o) (((ge_pyobj *)(o))->x)
 #define GE_G(o) (((ge_pyobj *)(o))->g)
-#define GE_GOBJ(o) ((PyObject *)(group_pyobj *)(o)->ob_type)
+#define GE_GOBJ(o) (PyObject *)(Py_TYPE(o))
 extern PyObject *ge_pywrap(PyObject *, ge *);
 
 typedef struct group_pyobj {
diff --git a/mp.c b/mp.c
index 2f8594d..1513974 100644 (file)
--- a/mp.c
+++ b/mp.c
@@ -45,12 +45,12 @@ mp *mp_frompylong(PyObject *obj)
   mp *x;
   mpw *p;
 
-  sz = l->ob_size;
+  sz = Py_SIZE(l);
   if (sz < 0) sz = -sz;
   assert(MPW_BITS >= SHIFT);
   bits = (unsigned long)sz * SHIFT;
   w = (bits + MPW_BITS - 1)/MPW_BITS;
-  x = mp_new(w, l->ob_size < 0 ? MP_NEG : 0);
+  x = mp_new(w, Py_SIZE(l) < 0 ? MP_NEG : 0);
   p = x->v;
   for (i = 0; i < sz; i++) {
     r |= (mpd)l->ob_digit[i] << b;
@@ -94,7 +94,7 @@ PyObject *mp_topylong(mp *x)
     l->ob_digit[i++] = r & MASK;
     r >>= SHIFT;
   }
-  l->ob_size = (x->f & MP_NEG) ? -sz : sz;
+  Py_SIZE(l) = (x->f & MP_NEG) ? -sz : sz;
   return ((PyObject *)l);
 }
 
@@ -238,7 +238,7 @@ mp *getmp(PyObject *o)
   if (!o) return (0);
   if ((x = tomp(o)) == 0) {
     PyErr_Format(PyExc_TypeError, "can't convert %.100s to mp",
-                o->ob_type->tp_name);
+                Py_TYPE(o)->tp_name);
   }
   return (x);
 }
@@ -257,7 +257,7 @@ mp *getgf(PyObject *o)
   if (!o) return (0);
   if ((x = tomp(o)) == 0) {
     PyErr_Format(PyExc_TypeError, "can't convert %.100s to gf",
-                o->ob_type->tp_name);
+                Py_TYPE(o)->tp_name);
   }
   return (x);
 }
@@ -536,7 +536,7 @@ static PyObject *mp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
   if (!good_radix_p(radix, 1)) VALERR("bad radix");
   if ((z = mp_frompyobject(x, radix)) == 0) {
     PyErr_Format(PyExc_TypeError, "can't convert %.100s to mp",
-                x->ob_type->tp_name);
+                Py_TYPE(x)->tp_name);
     goto end;
   }
   zz = (mp_pyobj *)ty->tp_alloc(ty, 0);
@@ -1907,7 +1907,7 @@ static PyObject *gf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
   if (!good_radix_p(radix, 1)) VALERR("radix out of range");
   if ((z = mp_frompyobject(x, radix)) == 0) {
     PyErr_Format(PyExc_TypeError, "can't convert %.100s to gf",
-                x->ob_type->tp_name);
+                Py_TYPE(x)->tp_name);
     goto end;
   }
   if (MP_NEGP(z)) {
index 581e444..c453a9c 100644 (file)
 
 PRIVATE_SYMBOLS;
 
+/*----- Python version compatibility hacks --------------------------------*/
+
+/* The handy `Py_TYPE' and `Py_SIZE' macros turned up in 2.6.  Define them if
+ * they're not already here.
+ */
+#ifndef Py_TYPE
+#  define Py_TYPE(obj) (((PyObject *)(obj))->ob_type)
+#endif
+#ifndef Py_SIZE
+#  define Py_SIZE(obj) (((PyVarObject *)(obj))->ob_size)
+#endif
+
 /*----- Utilities for returning values and exceptions ---------------------*/
 
 /* Returning values. */
@@ -153,8 +165,7 @@ extern PyObject *getulong(unsigned long); /* any kind of unsigned integer */
 
 /*----- Miscellaneous utilities -------------------------------------------*/
 
-#define FREEOBJ(obj)                                                   \
-  (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj)))
+#define FREEOBJ(obj) (Py_TYPE(obj)->tp_free((PyObject *)(obj)))
   /* Actually free OBJ, e.g., in a deallocation function. */
 
 extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *);