X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/c90f712e989f2ee557842c18fdb9a4df469c1859..31ff254edd8d9c6927a67838ec4418ad8ba42652:/field.c?ds=sidebyside diff --git a/field.c b/field.c index 8091843..ba7badb 100644 --- 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); \ @@ -302,9 +301,6 @@ UNOP(qdl, ; ) UNOP(hlv, ; ) #undef UNOP -static PyObject *feget_field(PyObject *me, void *hunoz) - { RETURN_OBJ(FE_FOBJ(me)); } - static PyObject *feget_value(PyObject *me, void *hunoz) { mp *x = F_OUT(FE_F(me), MP_NEW, FE_X(me)); @@ -324,9 +320,16 @@ static PyObject *feget__value(PyObject *me, void *hunoz) return (mp_pywrap(x)); } +static const PyMemberDef fe_pymembers[] = { +#define MEMBERSTRUCT fe_pyobj + MEMRNM(field, T_OBJECT, ob_type, READONLY, + "X.field -> field containing X") +#undef MEMBERSTRUCT + { 0 } +}; + 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") GET (_value, "X._value -> internal MP/GF representation of X") #undef GETSETNAME @@ -335,14 +338,14 @@ static const PyGetSetDef fe_pygetset[] = { 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 } }; @@ -390,7 +393,7 @@ static const PyNumberMethods fe_pynumber = { 0, /* @nb_inplace_true_divide@ */ }; -static PyTypeObject fe_pytype_skel = { +static const PyTypeObject fe_pytype_skel = { PyObject_HEAD_INIT(0) 0, /* Header */ "FE", /* @tp_name@ */ sizeof(fe_pyobj), /* @tp_basicsize@ */ @@ -425,7 +428,7 @@ static PyTypeObject fe_pytype_skel = { 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ PYMETHODS(fe), /* @tp_methods@ */ - 0, /* @tp_members@ */ + PYMEMBERS(fe), /* @tp_members@ */ PYGETSET(fe), /* @tp_getset@ */ 0, /* @tp_base@ */ 0, /* @tp_dict@ */ @@ -472,6 +475,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)); @@ -516,11 +534,12 @@ 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 } }; -static PyTypeObject field_pytype_skel = { +static const PyTypeObject field_pytype_skel = { PyObject_HEAD_INIT(0) 0, /* Header */ "Field", /* @tp_name@ */ sizeof(field_pyobj), /* @tp_basicsize@ */ @@ -598,7 +617,7 @@ static const PyGetSetDef primefield_pygetset[] = { #undef GETSETNAME }; -static PyTypeObject primefield_pytype_skel = { +static const PyTypeObject primefield_pytype_skel = { PyObject_HEAD_INIT(0) 0, /* Header */ "PrimeField", /* @tp_name@ */ sizeof(field_pyobj), /* @tp_basicsize@ */ @@ -665,7 +684,7 @@ end: return (0); } -static PyTypeObject niceprimefield_pytype_skel = { +static const PyTypeObject niceprimefield_pytype_skel = { PyObject_HEAD_INIT(0) 0, /* Header */ "NicePrimeField", /* @tp_name@ */ sizeof(field_pyobj), /* @tp_basicsize@ */ @@ -729,7 +748,7 @@ static const PyGetSetDef binfield_pygetset[] = { { 0 } }; -static PyTypeObject binfield_pytype_skel = { +static const PyTypeObject binfield_pytype_skel = { PyObject_HEAD_INIT(0) 0, /* Header */ "BinField", /* @tp_name@ */ sizeof(field_pyobj), /* @tp_basicsize@ */ @@ -795,7 +814,7 @@ end: return (0); } -static PyTypeObject binpolyfield_pytype_skel = { +static const PyTypeObject binpolyfield_pytype_skel = { PyObject_HEAD_INIT(0) 0, /* Header */ "BinPolyField", /* @tp_name@ */ sizeof(field_pyobj), /* @tp_basicsize@ */ @@ -874,7 +893,7 @@ static const PyGetSetDef binnormfield_pygetset[] = { { 0 } }; -static PyTypeObject binnormfield_pytype_skel = { +static const PyTypeObject binnormfield_pytype_skel = { PyObject_HEAD_INIT(0) 0, /* Header */ "BinNormField", /* @tp_name@ */ sizeof(field_pyobj), /* @tp_basicsize@ */ @@ -924,28 +943,8 @@ 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 const PyMethodDef methods[] = { -#define METHNAME(func) meth_##func - METH (_Field_parse, "parse(STR) -> F, REST") -#undef METHNAME +static const struct nameval consts[] = { + CONST(FTY_PRIME), CONST(FTY_BINARY), { 0 } }; @@ -958,7 +957,6 @@ void field_pyinit(void) INITTYPE(binfield, field); INITTYPE(binpolyfield, binfield); INITTYPE(binnormfield, binfield); - addmethods(methods); } void field_pyinsert(PyObject *mod) @@ -970,6 +968,7 @@ void field_pyinsert(PyObject *mod) INSERT("BinField", binfield_pytype); INSERT("BinPolyField", binpolyfield_pytype); INSERT("BinNormField", binnormfield_pytype); + setconstants(mod, consts); } /*----- That's all, folks -------------------------------------------------*/