*.c: Use Python `METH_NOARGS' methods where applicable.
[catacomb-python] / field.c
diff --git a/field.c b/field.c
index 417c550..987b47d 100644 (file)
--- a/field.c
+++ b/field.c
@@ -280,10 +280,9 @@ static void fe_pydealloc(PyObject *me)
 }
 
 #define UNOP(name, check)                                              \
-  static PyObject *femeth_##name(PyObject *me, PyObject *arg) {                \
+  static PyObject *femeth_##name(PyObject *me) {                       \
     field *f = FE_F(me);                                               \
     mp *x = FE_X(me);                                                  \
-    if (!PyArg_ParseTuple(arg, ":" #name)) return (0);                 \
     if (!f->ops->name) TYERR(#name " not supported for this field");   \
     check                                                              \
     x = f->ops->name(f, MP_NEW, x);                                    \
@@ -324,7 +323,7 @@ static PyObject *feget__value(PyObject *me, void *hunoz)
     return (mp_pywrap(x));
 }
 
-static PyGetSetDef fe_pygetset[] = {
+static const PyGetSetDef fe_pygetset[] = {
 #define GETSETNAME(op, name) fe##op##_##name
   GET  (field,         "X.field -> field containing X")
   GET  (value,         "X.value -> `natural' MP/GF representation of X")
@@ -333,21 +332,21 @@ static PyGetSetDef fe_pygetset[] = {
   { 0 }
 };
 
-static PyMethodDef fe_pymethods[] = {
+static const PyMethodDef fe_pymethods[] = {
 #define METHNAME(func) femeth_##func
-  METH (inv,           "X.inv() -> X^{-1}")
-  METH (sqr,           "X.sqr() -> X^2")
-  METH (sqrt,          "X.sqrt() -> sqrt(X)")
-  METH (quadsolve,     "X.quadsolve() -> Y where Y^2 + Y = X (binary only)")
-  METH (dbl,           "X.dbl() -> 2 * X (prime only)")
-  METH (tpl,           "X.tpl() -> 3 * X (prime only)")
-  METH (qdl,           "X.qdl() -> 4 * X (prime only)")
-  METH (hlv,           "X.hlv() -> X/2 (prime only)")
+  NAMETH(inv,          "X.inv() -> X^{-1}")
+  NAMETH(sqr,          "X.sqr() -> X^2")
+  NAMETH(sqrt,         "X.sqrt() -> sqrt(X)")
+  NAMETH(quadsolve,    "X.quadsolve() -> Y where Y^2 + Y = X (binary only)")
+  NAMETH(dbl,          "X.dbl() -> 2 * X (prime only)")
+  NAMETH(tpl,          "X.tpl() -> 3 * X (prime only)")
+  NAMETH(qdl,          "X.qdl() -> 4 * X (prime only)")
+  NAMETH(hlv,          "X.hlv() -> X/2 (prime only)")
 #undef METHNAME
   { 0 }
 };
 
-static PyNumberMethods fe_pynumber = {
+static const PyNumberMethods fe_pynumber = {
   fe_pyadd,                            /* @nb_add@ */
   fe_pysub,                            /* @nb_subtract@ */
   fe_pymul,                            /* @nb_multiply@ */
@@ -402,7 +401,7 @@ static PyTypeObject fe_pytype_skel = {
   0,                                   /* @tp_setattr@ */
   0,                                   /* @tp_compare@ */
   0,                                   /* @tp_repr@ */
-  &fe_pynumber,                                /* @tp_as_number@ */
+  PYNUMBER(fe),                                /* @tp_as_number@ */
   0,                                   /* @tp_as_sequence@ */
   0,                                   /* @tp_as_mapping@ */
   fe_pyhash,                           /* @tp_hash@ */
@@ -416,7 +415,7 @@ static PyTypeObject fe_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Finite field elements, abstract base class.",
+  "Finite field elements, abstract base class.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -424,9 +423,9 @@ static PyTypeObject fe_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  fe_pymethods,                                /* @tp_methods@ */
+  PYMETHODS(fe),                       /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  fe_pygetset,                         /* @tp_getset@ */
+  PYGETSET(fe),                                /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -472,6 +471,21 @@ static PyObject *fmeth__adopt(PyObject *me, PyObject *arg)
   return (fe_pywrap(me, xx));
 }
 
+static PyObject *fmeth_parse(PyObject *me, PyObject *arg)
+{
+  field *f;
+  char *p;
+  PyObject *rc = 0;
+  qd_parse qd;
+
+  if (!PyArg_ParseTuple(arg, "s:parse", &p)) goto end;
+  qd.p = p; qd.e = 0;
+  if ((f = field_parse(&qd)) == 0) VALERR(qd.e);
+  rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p);
+end:
+  return (rc);
+}
+
 static void field_pydealloc(PyObject *me)
 {
   F_DESTROY(FIELD_F(me));
@@ -499,7 +513,7 @@ static PyObject *fget_name(PyObject *me, void *hunoz)
 static PyObject *fget_type(PyObject *me, void *hunoz)
   { return (PyInt_FromLong(F_TYPE(FIELD_F(me)))); }
 
-static PyGetSetDef field_pygetset[] = {
+static const PyGetSetDef field_pygetset[] = {
 #define GETSETNAME(op, name) f##op##_##name
   GET  (zero,          "F.zero -> field additive identity")
   GET  (one,           "F.one -> field multiplicative identity")
@@ -512,10 +526,11 @@ static PyGetSetDef field_pygetset[] = {
   { 0 }
 };
 
-static PyMethodDef field_pymethods[] = {
+static const PyMethodDef field_pymethods[] = {
 #define METHNAME(name) fmeth_##name
   METH (_adopt,        "F._adopt(X) -> FE")
   KWMETH(rand,         "F.rand([rng = rand]) -> FE, uniformly distributed")
+  SMTH (parse,         "parse(STR) -> F, REST")
 #undef METHNAME
   { 0 }
 };
@@ -545,7 +560,7 @@ static PyTypeObject field_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"An abstract field.  This is an abstract type.",
+  "An abstract field.  This is an abstract type.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -553,9 +568,9 @@ static PyTypeObject field_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  field_pymethods,                     /* @tp_methods@ */
+  PYMETHODS(field),                    /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  field_pygetset,                      /* @tp_getset@ */
+  PYGETSET(field),                     /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -592,7 +607,7 @@ end:
 static PyObject *pfget_p(PyObject *me, void *hunoz)
   { return (mp_pywrap(MP_COPY(FIELD_F(me)->m))); }
 
-static PyGetSetDef primefield_pygetset[] = {
+static const PyGetSetDef primefield_pygetset[] = {
 #define GETSETNAME(op, name) pf##op##_##name
   GET  (p,             "F.p -> prime field characteristic")
 #undef GETSETNAME
@@ -623,7 +638,7 @@ static PyTypeObject primefield_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"PrimeField(P): prime fields.",
+  "PrimeField(P): prime fields.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -633,7 +648,7 @@ static PyTypeObject primefield_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  primefield_pygetset,                 /* @tp_getset@ */
+  PYGETSET(primefield),                        /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -690,7 +705,7 @@ static PyTypeObject niceprimefield_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"NicePrimeField(P): prime field using Solinas reduction.",
+  "NicePrimeField(P): prime field using Solinas reduction.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -718,9 +733,13 @@ static PyTypeObject niceprimefield_pytype_skel = {
 static PyObject *bfget_m(PyObject *me, void *hunoz)
   { return (PyInt_FromLong(FIELD_F(me)->nbits)); }
 
-static PyGetSetDef binfield_pygetset[] = {
+static PyObject *bfget_p(PyObject *me, void *hunoz)
+  { return (gf_pywrap(MP_COPY(FIELD_F(me)->m))); }
+
+static const PyGetSetDef binfield_pygetset[] = {
 #define GETSETNAME(op, name) bf##op##_##name
   GET  (m,             "F.m -> field polynomial degree")
+  GET  (p,             "F.p -> field polynomial")
 #undef GETSETNAME
   { 0 }
 };
@@ -750,7 +769,7 @@ static PyTypeObject binfield_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Binary fields.  Abstract class.",
+  "Binary fields.  Abstract class.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -760,7 +779,7 @@ static PyTypeObject binfield_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  binfield_pygetset,                   /* @tp_getset@ */
+  PYGETSET(binfield),                  /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -791,16 +810,6 @@ end:
   return (0);
 }
 
-static PyObject *bfget_p(PyObject *me, void *hunoz)
-  { return (gf_pywrap(MP_COPY(FIELD_F(me)->m))); }
-
-static PyGetSetDef binpolyfield_pygetset[] = {
-#define GETSETNAME(op, name) bf##op##_##name
-  GET  (p,             "F.p -> field polynomial")
-#undef GETSETNAME
-  { 0 }
-};
-
 static PyTypeObject binpolyfield_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
   "BinPolyField",                      /* @tp_name@ */
@@ -826,7 +835,7 @@ static PyTypeObject binpolyfield_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"BinPolyField(P): binary fields with polynomial basis representation.",
+  "BinPolyField(P): binary fields with polynomial basis representation.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -836,7 +845,7 @@ static PyTypeObject binpolyfield_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  binpolyfield_pygetset,               /* @tp_getset@ */
+  0,                                   /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -873,10 +882,7 @@ static PyObject *bnfget_beta(PyObject *me, void *hunoz)
   return (gf_pywrap(MP_COPY(fc->ntop.r[fc->ntop.n - 1])));
 }
 
-static PyGetSetDef binnormfield_pygetset[] = {
-#define GETSETNAME(op, name) bf##op##_##name
-  GET  (p,             "F.p -> field polynomial")
-#undef GETSETNAME
+static const PyGetSetDef binnormfield_pygetset[] = {
 #define GETSETNAME(op, name) bnf##op##_##name
   GET  (beta,          "F.beta -> conversion factor")
 #undef GETSETNAME
@@ -908,7 +914,7 @@ static PyTypeObject binnormfield_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"BinNormField(P, BETA): binary fields with normal basis representation.",
+  "BinNormField(P, BETA): binary fields with normal basis representation.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -918,7 +924,7 @@ static PyTypeObject binnormfield_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  binnormfield_pygetset,               /* @tp_getset@ */
+  PYGETSET(binnormfield),              /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -933,31 +939,6 @@ static PyTypeObject binnormfield_pytype_skel = {
 
 /*----- Setup -------------------------------------------------------------*/
 
-static PyObject *meth__Field_parse(PyObject *me, PyObject *arg)
-{
-  field *f;
-  char *p;
-  PyObject *rc = 0;
-  qd_parse qd;
-
-  if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
-    goto end;
-  qd.p = p;
-  qd.e = 0;
-  if ((f = field_parse(&qd)) == 0)
-    VALERR(qd.e);
-  rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p);
-end:
-  return (rc);
-}
-
-static PyMethodDef methods[] = {
-#define METHNAME(func) meth_##func
-  METH (_Field_parse,          "parse(STR) -> F, REST")
-#undef METHNAME
-  { 0 }
-};
-
 void field_pyinit(void)
 {
   INITTYPE(fe, root);
@@ -967,7 +948,6 @@ void field_pyinit(void)
   INITTYPE(binfield, field);
   INITTYPE(binpolyfield, binfield);
   INITTYPE(binnormfield, binfield);
-  addmethods(methods);
 }
 
 void field_pyinsert(PyObject *mod)