~mdw
/
catacomb-python
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
ec.c, catacomb/__init__.py: Return tail from `parse'; provide `fromstring'.
[catacomb-python]
/
field.c
diff --git
a/field.c
b/field.c
index
987b47d
..
2e99efc
100644
(file)
--- a/
field.c
+++ b/
field.c
@@
-107,23
+107,18
@@
PyObject *fe_pywrap(PyObject *fobj, mp *x)
return ((PyObject *)z);
}
return ((PyObject *)z);
}
-static mp *
to
fe(field *f, PyObject *o)
+static mp *
implicit
fe(field *f, PyObject *o)
{
{
- mp *x = 0, *y = 0;
-
- if (FE_PYCHECK(o)) {
- if (FE_F(o) != f && !field_samep(FE_F(o), f)) return (0);
- y = MP_COPY(FE_X(o));
- } else if ((x = tomp(o)) != 0) {
- if (MP_ZEROP(x))
- y = MP_COPY(f->zero);
- else if (MP_EQ(x, MP_ONE))
- y = MP_COPY(f->one);
- else
- y = F_IN(f, MP_NEW, x);
- MP_DROP(x);
+ mp *x;
+
+ if (FE_PYCHECK(o) && FE_F(o) == f) return (MP_COPY(FE_X(o)));
+ switch (f->ops->ty) {
+ case FTY_PRIME: x = implicitmp(o); break;
+ case FTY_BINARY: x = implicitgf(o); break;
+ default: assert(!"huh?");
}
}
- return (y);
+ if (!x) return (0);
+ return (F_IN(f, MP_NEW, x));
}
/*----- Field elements ----------------------------------------------------*/
}
/*----- Field elements ----------------------------------------------------*/
@@
-135,9
+130,9
@@
static int febinop(PyObject *x, PyObject *y,
else if (FE_PYCHECK(y)) *fobj = FE_FOBJ(y);
else return (-1);
*f = FIELD_F(*fobj);
else if (FE_PYCHECK(y)) *fobj = FE_FOBJ(y);
else return (-1);
*f = FIELD_F(*fobj);
- if ((*xx =
to
fe(*f, x)) == 0)
+ if ((*xx =
implicit
fe(*f, x)) == 0)
return (-1);
return (-1);
- if ((*yy =
to
fe(*f, y)) == 0) {
+ if ((*yy =
implicit
fe(*f, y)) == 0) {
MP_DROP(*xx);
return (-1);
}
MP_DROP(*xx);
return (-1);
}
@@
-179,7
+174,10
@@
static PyObject *fe_pyexp(PyObject *x, PyObject *y, PyObject *z)
field *ff;
mp *xx, *yy;
field *ff;
mp *xx, *yy;
- if (z != Py_None || !FE_PYCHECK(x) || (yy = tomp(y)) == 0)
+ if (z != Py_None || !FE_PYCHECK(x)) RETURN_NOTIMPL;
+ if (FE_PYCHECK(y) && FE_F(y)->ops->ty == FTY_PRIME)
+ yy = F_OUT(FE_F(y), MP_NEW, FE_X(y));
+ else if ((yy = implicitmp(y)) == 0)
RETURN_NOTIMPL;
ff = FE_F(x); xx = FE_X(x); MP_COPY(xx);
if (MP_NEGP(yy) && F_ZEROP(ff, xx)) ZDIVERR("division by zero");
RETURN_NOTIMPL;
ff = FE_F(x); xx = FE_X(x); MP_COPY(xx);
if (MP_NEGP(yy) && F_ZEROP(ff, xx)) ZDIVERR("division by zero");
@@
-218,9
+216,10
@@
end:
return (rc);
}
return (rc);
}
-static
long
fe_pyhash(PyObject *me)
+static
Py_hash_t
fe_pyhash(PyObject *me)
{ return (mphash(FE_X(me))); }
{ return (mphash(FE_X(me))); }
+#ifdef PY2
static int fe_pycoerce(PyObject **x, PyObject **y)
{
mp *z;
static int fe_pycoerce(PyObject **x, PyObject **y)
{
mp *z;
@@
-231,7
+230,7
@@
static int fe_pycoerce(PyObject **x, PyObject **y)
Py_INCREF(*x); Py_INCREF(*y);
return (0);
}
Py_INCREF(*x); Py_INCREF(*y);
return (0);
}
- if ((z =
to
fe(FE_F(*x), *y)) != 0) {
+ if ((z =
implicit
fe(FE_F(*x), *y)) != 0) {
Py_INCREF(*x);
*y = fe_pywrap(FE_FOBJ(*x), z);
return (0);
Py_INCREF(*x);
*y = fe_pywrap(FE_FOBJ(*x), z);
return (0);
@@
-241,6
+240,7
@@
static int fe_pycoerce(PyObject **x, PyObject **y)
end:
return (-1);
}
end:
return (-1);
}
+#endif
static PyObject *fe_pyint(PyObject *x)
{
static PyObject *fe_pyint(PyObject *x)
{
@@
-253,6
+253,7
@@
static PyObject *fe_pyint(PyObject *x)
return (rc);
}
return (rc);
}
+#ifdef PY2
static PyObject *fe_pylong(PyObject *x)
{
mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
static PyObject *fe_pylong(PyObject *x)
{
mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
@@
-260,6
+261,7
@@
static PyObject *fe_pylong(PyObject *x)
MP_DROP(xx);
return (rc);
}
MP_DROP(xx);
return (rc);
}
+#endif
#define BASEOP(name, radix, pre) \
static PyObject *fe_py##name(PyObject *x) { \
#define BASEOP(name, radix, pre) \
static PyObject *fe_py##name(PyObject *x) { \
@@
-268,7
+270,9
@@
static PyObject *fe_pylong(PyObject *x)
MP_DROP(xx); \
return (rc); \
}
MP_DROP(xx); \
return (rc); \
}
+#ifdef PY2
BASEOP(oct, 8, "0");
BASEOP(oct, 8, "0");
+#endif
BASEOP(hex, 16, "0x");
#undef BASEOP
BASEOP(hex, 16, "0x");
#undef BASEOP
@@
-301,9
+305,6
@@
UNOP(qdl, ; )
UNOP(hlv, ; )
#undef UNOP
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));
static PyObject *feget_value(PyObject *me, void *hunoz)
{
mp *x = F_OUT(FE_F(me), MP_NEW, FE_X(me));
@@
-323,9
+324,16
@@
static PyObject *feget__value(PyObject *me, void *hunoz)
return (mp_pywrap(x));
}
return (mp_pywrap(x));
}
+static const PyMemberDef fe_pymembers[] = {
+#define MEMBERSTRUCT fe_pyobj
+ MEMRNM(field, T_OBJECT, PY23(ob_type, ob_base.ob_type), READONLY,
+ "X.field -> field containing X")
+#undef MEMBERSTRUCT
+ { 0 }
+};
+
static const PyGetSetDef fe_pygetset[] = {
#define GETSETNAME(op, name) fe##op##_##name
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
GET (value, "X.value -> `natural' MP/GF representation of X")
GET (_value, "X._value -> internal MP/GF representation of X")
#undef GETSETNAME
@@
-350,7
+358,9
@@
static const PyNumberMethods fe_pynumber = {
fe_pyadd, /* @nb_add@ */
fe_pysub, /* @nb_subtract@ */
fe_pymul, /* @nb_multiply@ */
fe_pyadd, /* @nb_add@ */
fe_pysub, /* @nb_subtract@ */
fe_pymul, /* @nb_multiply@ */
+#ifdef PY2
fe_pydiv, /* @nb_divide@ */
fe_pydiv, /* @nb_divide@ */
+#endif
0, /* @nb_remainder@ */
0, /* @nb_divmod@ */
fe_pyexp, /* @nb_power@ */
0, /* @nb_remainder@ */
0, /* @nb_divmod@ */
fe_pyexp, /* @nb_power@ */
@@
-364,17
+374,23
@@
static const PyNumberMethods fe_pynumber = {
0, /* @nb_and@ */
0, /* @nb_xor@ */
0, /* @nb_or@ */
0, /* @nb_and@ */
0, /* @nb_xor@ */
0, /* @nb_or@ */
+#ifdef PY2
fe_pycoerce, /* @nb_coerce@ */
fe_pycoerce, /* @nb_coerce@ */
+#endif
fe_pyint, /* @nb_int@ */
fe_pyint, /* @nb_int@ */
-
fe_pylong,
/* @nb_long@ */
+
PY23(fe_pylong, 0),
/* @nb_long@ */
0 /* meaningless */, /* @nb_float@ */
0 /* meaningless */, /* @nb_float@ */
+#ifdef PY2
fe_pyoct, /* @nb_oct@ */
fe_pyhex, /* @nb_hex@ */
fe_pyoct, /* @nb_oct@ */
fe_pyhex, /* @nb_hex@ */
+#endif
0, /* @nb_inplace_add@ */
0, /* @nb_inplace_subtract@ */
0, /* @nb_inplace_multiply@ */
0, /* @nb_inplace_add@ */
0, /* @nb_inplace_subtract@ */
0, /* @nb_inplace_multiply@ */
+#ifdef PY2
0, /* @nb_inplace_divide@ */
0, /* @nb_inplace_divide@ */
+#endif
0, /* @nb_inplace_remainder@ */
0, /* @nb_inplace_power@ */
0, /* @nb_inplace_lshift@ */
0, /* @nb_inplace_remainder@ */
0, /* @nb_inplace_power@ */
0, /* @nb_inplace_lshift@ */
@@
-387,10
+403,12
@@
static const PyNumberMethods fe_pynumber = {
fe_pydiv, /* @nb_true_divide@ */
0, /* @nb_inplace_floor_divide@ */
0, /* @nb_inplace_true_divide@ */
fe_pydiv, /* @nb_true_divide@ */
0, /* @nb_inplace_floor_divide@ */
0, /* @nb_inplace_true_divide@ */
+
+ fe_pyint, /* @nb_index@ */
};
};
-static PyTypeObject fe_pytype_skel = {
- Py
Object_HEAD_INIT(0) 0,
/* Header */
+static
const
PyTypeObject fe_pytype_skel = {
+ Py
VarObject_HEAD_INIT(0, 0)
/* Header */
"FE", /* @tp_name@ */
sizeof(fe_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
"FE", /* @tp_name@ */
sizeof(fe_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-424,7
+442,7
@@
static PyTypeObject fe_pytype_skel = {
0, /* @tp_iter@ */
0, /* @tp_iternext@ */
PYMETHODS(fe), /* @tp_methods@ */
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@ */
PYGETSET(fe), /* @tp_getset@ */
0, /* @tp_base@ */
0, /* @tp_dict@ */
@@
-508,7
+526,7
@@
static PyObject *fget_noctets(PyObject *me, void *hunoz)
{ return (PyInt_FromLong(FIELD_F(me)->noctets)); }
static PyObject *fget_name(PyObject *me, void *hunoz)
{ return (PyInt_FromLong(FIELD_F(me)->noctets)); }
static PyObject *fget_name(PyObject *me, void *hunoz)
- { return (
PyString_FromString
(F_NAME(FIELD_F(me)))); }
+ { return (
TEXT_FROMSTR
(F_NAME(FIELD_F(me)))); }
static PyObject *fget_type(PyObject *me, void *hunoz)
{ return (PyInt_FromLong(F_TYPE(FIELD_F(me)))); }
static PyObject *fget_type(PyObject *me, void *hunoz)
{ return (PyInt_FromLong(F_TYPE(FIELD_F(me)))); }
@@
-535,8
+553,8
@@
static const PyMethodDef field_pymethods[] = {
{ 0 }
};
{ 0 }
};
-static PyTypeObject field_pytype_skel = {
- Py
Object_HEAD_INIT(0) 0,
/* Header */
+static
const
PyTypeObject field_pytype_skel = {
+ Py
VarObject_HEAD_INIT(0, 0)
/* Header */
"Field", /* @tp_name@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
"Field", /* @tp_name@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-613,8
+631,8
@@
static const PyGetSetDef primefield_pygetset[] = {
#undef GETSETNAME
};
#undef GETSETNAME
};
-static PyTypeObject primefield_pytype_skel = {
- Py
Object_HEAD_INIT(0) 0,
/* Header */
+static
const
PyTypeObject primefield_pytype_skel = {
+ Py
VarObject_HEAD_INIT(0, 0)
/* Header */
"PrimeField", /* @tp_name@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
"PrimeField", /* @tp_name@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-642,7
+660,7
@@
static PyTypeObject primefield_pytype_skel = {
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
-
field_pyrichcompare,
/* @tp_richcompare@ */
+
0,
/* @tp_richcompare@ */
0, /* @tp_weaklistoffset@ */
0, /* @tp_iter@ */
0, /* @tp_iternext@ */
0, /* @tp_weaklistoffset@ */
0, /* @tp_iter@ */
0, /* @tp_iternext@ */
@@
-680,8
+698,8
@@
end:
return (0);
}
return (0);
}
-static PyTypeObject niceprimefield_pytype_skel = {
- Py
Object_HEAD_INIT(0) 0,
/* Header */
+static
const
PyTypeObject niceprimefield_pytype_skel = {
+ Py
VarObject_HEAD_INIT(0, 0)
/* Header */
"NicePrimeField", /* @tp_name@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
"NicePrimeField", /* @tp_name@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-709,7
+727,7
@@
static PyTypeObject niceprimefield_pytype_skel = {
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
-
field_pyrichcompare,
/* @tp_richcompare@ */
+
0,
/* @tp_richcompare@ */
0, /* @tp_weaklistoffset@ */
0, /* @tp_iter@ */
0, /* @tp_iternext@ */
0, /* @tp_weaklistoffset@ */
0, /* @tp_iter@ */
0, /* @tp_iternext@ */
@@
-744,8
+762,8
@@
static const PyGetSetDef binfield_pygetset[] = {
{ 0 }
};
{ 0 }
};
-static PyTypeObject binfield_pytype_skel = {
- Py
Object_HEAD_INIT(0) 0,
/* Header */
+static
const
PyTypeObject binfield_pytype_skel = {
+ Py
VarObject_HEAD_INIT(0, 0)
/* Header */
"BinField", /* @tp_name@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
"BinField", /* @tp_name@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-773,7
+791,7
@@
static PyTypeObject binfield_pytype_skel = {
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
-
field_pyrichcompare,
/* @tp_richcompare@ */
+
0,
/* @tp_richcompare@ */
0, /* @tp_weaklistoffset@ */
0, /* @tp_iter@ */
0, /* @tp_iternext@ */
0, /* @tp_weaklistoffset@ */
0, /* @tp_iter@ */
0, /* @tp_iternext@ */
@@
-810,8
+828,8
@@
end:
return (0);
}
return (0);
}
-static PyTypeObject binpolyfield_pytype_skel = {
- Py
Object_HEAD_INIT(0) 0,
/* Header */
+static
const
PyTypeObject binpolyfield_pytype_skel = {
+ Py
VarObject_HEAD_INIT(0, 0)
/* Header */
"BinPolyField", /* @tp_name@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
"BinPolyField", /* @tp_name@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-839,7
+857,7
@@
static PyTypeObject binpolyfield_pytype_skel = {
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
-
field_pyrichcompare,
/* @tp_richcompare@ */
+
0,
/* @tp_richcompare@ */
0, /* @tp_weaklistoffset@ */
0, /* @tp_iter@ */
0, /* @tp_iternext@ */
0, /* @tp_weaklistoffset@ */
0, /* @tp_iter@ */
0, /* @tp_iternext@ */
@@
-889,8
+907,8
@@
static const PyGetSetDef binnormfield_pygetset[] = {
{ 0 }
};
{ 0 }
};
-static PyTypeObject binnormfield_pytype_skel = {
- Py
Object_HEAD_INIT(0) 0,
/* Header */
+static
const
PyTypeObject binnormfield_pytype_skel = {
+ Py
VarObject_HEAD_INIT(0, 0)
/* Header */
"BinNormField", /* @tp_name@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
"BinNormField", /* @tp_name@ */
sizeof(field_pyobj), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
@@
-918,7
+936,7
@@
static PyTypeObject binnormfield_pytype_skel = {
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
-
field_pyrichcompare,
/* @tp_richcompare@ */
+
0,
/* @tp_richcompare@ */
0, /* @tp_weaklistoffset@ */
0, /* @tp_iter@ */
0, /* @tp_iternext@ */
0, /* @tp_weaklistoffset@ */
0, /* @tp_iter@ */
0, /* @tp_iternext@ */
@@
-939,6
+957,11
@@
static PyTypeObject binnormfield_pytype_skel = {
/*----- Setup -------------------------------------------------------------*/
/*----- Setup -------------------------------------------------------------*/
+static const struct nameval consts[] = {
+ CONST(FTY_PRIME), CONST(FTY_BINARY),
+ { 0 }
+};
+
void field_pyinit(void)
{
INITTYPE(fe, root);
void field_pyinit(void)
{
INITTYPE(fe, root);
@@
-959,6
+982,7
@@
void field_pyinsert(PyObject *mod)
INSERT("BinField", binfield_pytype);
INSERT("BinPolyField", binpolyfield_pytype);
INSERT("BinNormField", binnormfield_pytype);
INSERT("BinField", binfield_pytype);
INSERT("BinPolyField", binpolyfield_pytype);
INSERT("BinNormField", binnormfield_pytype);
+ setconstants(mod, consts);
}
/*----- That's all, folks -------------------------------------------------*/
}
/*----- That's all, folks -------------------------------------------------*/