X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/827f89d7920979b763e228970132c3624a921b13..f281293ca70de172b275ecb3224ea36839f2f830:/pgen.c diff --git a/pgen.c b/pgen.c index 18b0c26..dd2d827 100644 --- a/pgen.c +++ b/pgen.c @@ -134,7 +134,7 @@ static PyObject *pfget_x(PyObject *me, void *hunoz) static PyObject *pfget_status(PyObject *me, void *hunoz) { return (PyInt_FromLong(PFILT_ST(me))); } -static PyGetSetDef pfilt_pygetset[] = { +static const PyGetSetDef pfilt_pygetset[] = { #define GETSETNAME(op, name) pf##op##_##name GET (x, "F.x -> current position of filter") GET (status, "F.status -> primality status of filter") @@ -142,7 +142,7 @@ static PyGetSetDef pfilt_pygetset[] = { { 0 } }; -static PyMethodDef pfilt_pymethods[] = { +static const PyMethodDef pfilt_pymethods[] = { #define METHNAME(name) pfmeth_##name METH (step, "F.step(N)") METH (muladd, "F.muladd(M, A)") @@ -151,7 +151,7 @@ static PyMethodDef pfilt_pymethods[] = { { 0 } }; -static PyNumberMethods pfilt_pynumber = { +static const PyNumberMethods pfilt_pynumber = { 0, /* @nb_add@ */ 0, /* @nb_subtract@ */ 0, /* @nb_multiply@ */ @@ -206,7 +206,7 @@ static PyTypeObject pfilt_pytype_skel = { 0, /* @tp_setattr@ */ 0, /* @tp_compare@ */ 0, /* @tp_repr@ */ - &pfilt_pynumber, /* @tp_as_number@ */ + PYNUMBER(pfilt), /* @tp_as_number@ */ 0, /* @tp_as_sequence@ */ 0, /* @tp_as_mapping@ */ 0, /* @tp_hash@ */ @@ -219,7 +219,7 @@ static PyTypeObject pfilt_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"PrimeFilter(X): small-primes filter.", + "PrimeFilter(X): small-primes filter.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -227,9 +227,9 @@ static PyTypeObject pfilt_pytype_skel = { 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ - pfilt_pymethods, /* @tp_methods@ */ + PYMETHODS(pfilt), /* @tp_methods@ */ 0, /* @tp_members@ */ - pfilt_pygetset, /* @tp_getset@ */ + PYGETSET(pfilt), /* @tp_getset@ */ 0, /* @tp_base@ */ 0, /* @tp_dict@ */ 0, /* @tp_descr_get@ */ @@ -311,7 +311,7 @@ static PyObject *meth__RabinMiller_iters(PyObject *me, PyObject *arg) return (PyInt_FromLong(rabin_iters(n))); } -static PyGetSetDef rabin_pygetset[] = { +static const PyGetSetDef rabin_pygetset[] = { #define GETSETNAME(op, name) r##op##_##name GET (x, "R.x -> number under test") GET (niters, "R.niters -> suggested number of tests") @@ -319,7 +319,7 @@ static PyGetSetDef rabin_pygetset[] = { { 0 } }; -static PyMethodDef rabin_pymethods[] = { +static const PyMethodDef rabin_pymethods[] = { #define METHNAME(name) rmeth_##name METH (test, "R.test(W) -> PGST") METH (rtest, "R.rtest(W) -> PGST") @@ -352,7 +352,7 @@ static PyTypeObject rabin_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"RabinMiller(X): Rabin-Miller strong primality test.", + "RabinMiller(X): Rabin-Miller strong primality test.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -360,9 +360,9 @@ static PyTypeObject rabin_pytype_skel = { 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ - rabin_pymethods, /* @tp_methods@ */ + PYMETHODS(rabin), /* @tp_methods@ */ 0, /* @tp_members@ */ - rabin_pygetset, /* @tp_getset@ */ + PYGETSET(rabin), /* @tp_getset@ */ 0, /* @tp_base@ */ 0, /* @tp_dict@ */ 0, /* @tp_descr_get@ */ @@ -406,8 +406,7 @@ static void pgevent_kill(PyObject *me) static void pgevent_pydealloc(PyObject *me) { pgevent_pyobj *ev = (pgevent_pyobj *)me; - if (ev->r) Py_DECREF(ev->r); - FREEOBJ(me); + Py_XDECREF(ev->r); FREEOBJ(me); } #define PGEVENT_CHECK(me) do { \ @@ -443,6 +442,7 @@ static int peset_x(PyObject *me, PyObject *xobj, void *hunoz) mp *x = 0; pgen_event *ev = PGEVENT_EV(me); int rc = -1; + if (!xobj) NIERR("__del__"); PGEVENT_CHECK(me); if ((x = getmp(xobj)) == 0) goto end; mp_drop(ev->m); @@ -453,7 +453,7 @@ end: return (rc); } -static PyGetSetDef pgevent_pygetset[] = { +static const PyGetSetDef pgevent_pygetset[] = { #define GETSETNAME(op, name) pe##op##_##name GET (name, "EV.name -> value being generated") GETSET(x, "EV.x -> value under test") @@ -489,7 +489,7 @@ static PyTypeObject pgevent_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Prime-generation event.", + "Prime-generation event.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -499,7 +499,7 @@ static PyTypeObject pgevent_pytype_skel = { 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ 0, /* @tp_members@ */ - pgevent_pygetset, /* @tp_getset@ */ + PYGETSET(pgevent), /* @tp_getset@ */ 0, /* @tp_base@ */ 0, /* @tp_dict@ */ 0, /* @tp_descr_get@ */ @@ -543,7 +543,7 @@ static PyTypeObject *pgtest_pytype; static int pgev_python(int rq, pgen_event *ev, void *p) { - PyObject *py = p; + pypgev *pg = p; PyObject *pyev = 0; PyObject *rc = 0; int st = PGEN_ABORT; @@ -551,11 +551,10 @@ static int pgev_python(int rq, pgen_event *ev, void *p) static const char *const meth[] = { "pg_abort", "pg_done", "pg_begin", "pg_try", "pg_fail", "pg_pass" }; - Py_INCREF(py); rq++; if (rq > N(meth)) SYSERR("event code out of range"); pyev = pgevent_pywrap(ev); - if ((rc = PyObject_CallMethod(py, (/*unconst*/ char *)meth[rq], + if ((rc = PyObject_CallMethod(pg->obj, (/*unconst*/ char *)meth[rq], "(O)", pyev)) == 0) goto end; if (rc == Py_None) @@ -567,12 +566,13 @@ static int pgev_python(int rq, pgen_event *ev, void *p) else st = l; end: + if (PyErr_Occurred()) + stash_exception(pg->exc, "exception from `pgen' handler"); if (pyev) { pgevent_kill(pyev); Py_DECREF(pyev); } Py_XDECREF(rc); - Py_DECREF(py); return (st); } @@ -587,24 +587,22 @@ static PyObject *pgev_pywrap(const pgev *pg) int convpgev(PyObject *o, void *p) { - pgev *pg = p; + pypgev *pg = p; if (PGEV_PYCHECK(o)) - *pg = *PGEV_PG(o); + pg->ev = *PGEV_PG(o); else { - pg->proc = pgev_python; - pg->ctx = o; - Py_INCREF(o); + pg->ev.proc = pgev_python; + pg->ev.ctx = pg; + pg->obj = o; Py_INCREF(o); } return (1); } -void droppgev(pgev *p) +void droppgev(pypgev *pg) { - if (p->proc == pgev_python) { - PyObject *py = p->ctx; - Py_DECREF(py); - } + if (pg->ev.proc == pgev_python) + { assert(pg->ev.ctx == pg); Py_DECREF(pg->obj); } } static PyObject *pgmeth_common(PyObject *me, PyObject *arg, int rq) @@ -633,14 +631,14 @@ PGMETH(fail, FAIL) static PyObject *pgev_stdev(pgen_proc *proc) { pgev pg; pg.proc = proc; pg.ctx = 0; return (pgev_pywrap(&pg)); } -static PyMethodDef pgev_pymethods[] = { +static const PyMethodDef pgev_pymethods[] = { #define METHNAME(name) pgmeth_##name - METH (pg_abort, "E.pg_abort() -> PGRC -- prime generation aborted") - METH (pg_done, "E.pg_done() -> PGRC -- prime generation finished") - METH (pg_begin, "E.pg_begin() -> PGRC -- commence stepping/testing") - METH (pg_try, "E.pg_try() -> PGRC -- found new candidate") - METH (pg_pass, "E.pg_pass() -> PGRC -- passed primality test") - METH (pg_fail, "E.pg_fail() -> PGRC -- failed primality test") + METH (pg_abort, "E.pg_abort(EV) -> PGST -- prime generation aborted") + METH (pg_done, "E.pg_done(EV) -> PGST -- prime generation finished") + METH (pg_begin, "E.pg_begin(EV) -> PGST -- commence stepping/testing") + METH (pg_try, "E.pg_try(EV) -> PGST -- found new candidate") + METH (pg_pass, "E.pg_pass(EV) -> PGST -- passed primality test") + METH (pg_fail, "E.pg_fail(EV) -> PGST -- failed primality test") #undef METHNAME { 0 } }; @@ -670,7 +668,7 @@ static PyTypeObject pgev_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Built-in prime-generation event handler, base class.", + "Built-in prime-generation event handler, base class.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -678,7 +676,7 @@ static PyTypeObject pgev_pytype_skel = { 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ - pgev_pymethods, /* @tp_methods@ */ + PYMETHODS(pgev), /* @tp_methods@ */ 0, /* @tp_members@ */ 0, /* @tp_getset@ */ 0, /* @tp_base@ */ @@ -712,7 +710,7 @@ end: static PyObject *psget_step(PyObject *me, void *hunoz) { return (PyInt_FromLong(PGSTEP_STEP(me))); } -static PyGetSetDef pgstep_pygetset[] = { +static const PyGetSetDef pgstep_pygetset[] = { #define GETSETNAME(op, name) ps##op##_##name GET (step, "S.step -> step size for the stepper") #undef GETSETNAME @@ -744,7 +742,7 @@ static PyTypeObject pgstep_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"PrimeGenStepper(STEP): simple stepper with small-factors filter.", + "PrimeGenStepper(STEP): simple stepper with small-factors filter.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -754,7 +752,7 @@ static PyTypeObject pgstep_pytype_skel = { 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ 0, /* @tp_members@ */ - pgstep_pygetset, /* @tp_getset@ */ + PYGETSET(pgstep), /* @tp_getset@ */ 0, /* @tp_base@ */ 0, /* @tp_dict@ */ 0, /* @tp_descr_get@ */ @@ -794,7 +792,7 @@ static void pgjump_pydealloc(PyObject *me) static PyObject *pjget_jump(PyObject *me, void *hunoz) { RETURN_OBJ(PGJUMP_FOBJ(me)); } -static PyGetSetDef pgjump_pygetset[] = { +static const PyGetSetDef pgjump_pygetset[] = { #define GETSETNAME(op, name) pj##op##_##name GET (jump, "S.jump -> jump size for the stepper") #undef GETSETNAME @@ -826,7 +824,8 @@ static PyTypeObject pgjump_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"PrimeGenJumper(JUMP): stepper for larger steps with small-factors filter.", + "PrimeGenJumper(JUMP): " + "stepper for larger steps with small-factors filter.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -836,7 +835,7 @@ static PyTypeObject pgjump_pytype_skel = { 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ 0, /* @tp_members@ */ - pgjump_pygetset, /* @tp_getset@ */ + PYGETSET(pgjump), /* @tp_getset@ */ 0, /* @tp_base@ */ 0, /* @tp_dict@ */ 0, /* @tp_descr_get@ */ @@ -887,7 +886,7 @@ static PyTypeObject pgtest_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"PrimeGenTester(): Rabin-Miller tester.", + "PrimeGenTester(): Rabin-Miller tester.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -912,10 +911,10 @@ static PyTypeObject pgtest_pytype_skel = { /*----- Prime generation functions ----------------------------------------*/ -void pgenerr(void) +void pgenerr(struct excinfo *exc) { - if (!PyErr_Occurred()) - PyErr_SetString(PyExc_ValueError, "prime generation failed"); + if (exc->ty) RESTORE_EXCINFO(exc); + else PyErr_SetString(PyExc_ValueError, "prime generation failed"); } static PyObject *meth_pgen(PyObject *me, PyObject *arg, PyObject *kw) @@ -926,26 +925,26 @@ static PyObject *meth_pgen(PyObject *me, PyObject *arg, PyObject *kw) char *p = "p"; pgen_filterctx fc = { 2 }; rabin tc; - pgev step = { 0 }, test = { 0 }, evt = { 0 }; + struct excinfo exc = EXCINFO_INIT; + pypgev step = { { 0 } }, test = { { 0 } }, evt = { { 0 } }; unsigned nsteps = 0, ntests = 0; static const char *const kwlist[] = { "start", "name", "stepper", "tester", "event", "nsteps", "ntests", 0 }; - step.proc = pgen_filter; step.ctx = &fc; - test.proc = pgen_test; test.ctx = &tc; + step.exc = &exc; step.ev.proc = pgen_filter; step.ev.ctx = &fc; + test.exc = &exc; test.ev.proc = pgen_test; test.ev.ctx = &tc; + evt.exc = &exc; if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|sO&O&O&O&O&:pgen", KWLIST, convmp, &x, &p, convpgev, &step, convpgev, &test, convpgev, &evt, convuint, &nsteps, convuint, &ntests)) goto end; if (!ntests) ntests = rabin_iters(mp_bits(x)); - if ((r = pgen(p, MP_NEW, x, evt.proc, evt.ctx, - nsteps, step.proc, step.ctx, - ntests, test.proc, test.ctx)) == 0) - PGENERR; - if (PyErr_Occurred()) goto end; - rc = mp_pywrap(r); - r = 0; + if ((r = pgen(p, MP_NEW, x, evt.ev.proc, evt.ev.ctx, + nsteps, step.ev.proc, step.ev.ctx, + ntests, test.ev.proc, test.ev.ctx)) == 0) + PGENERR(&exc); + rc = mp_pywrap(r); r = 0; end: mp_drop(r); mp_drop(x); droppgev(&step); droppgev(&test); droppgev(&evt); @@ -961,19 +960,21 @@ static PyObject *meth_strongprime_setup(PyObject *me, unsigned nbits; char *name = "p"; unsigned n = 0; - pgev evt = { 0 }; + struct excinfo exc = EXCINFO_INIT; + pypgev evt = { { 0 } }; PyObject *rc = 0; static const char *const kwlist[] = { "nbits", "name", "event", "rng", "nsteps", 0 }; + evt.exc = &exc; if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|sO&O&O&", KWLIST, convuint, &nbits, &name, convpgev, &evt, convgrand, &r, convuint, &n)) goto end; if ((x = strongprime_setup(name, MP_NEW, &f, nbits, - r, n, evt.proc, evt.ctx)) == 0) - PGENERR; + r, n, evt.ev.proc, evt.ev.ctx)) == 0) + PGENERR(&exc); rc = Py_BuildValue("(NN)", mp_pywrap(x), pfilt_pywrap(&f)); x = 0; end: @@ -989,19 +990,21 @@ static PyObject *meth_strongprime(PyObject *me, PyObject *arg, PyObject *kw) unsigned nbits; char *name = "p"; unsigned n = 0; - pgev evt = { 0 }; + struct excinfo exc = EXCINFO_INIT; + pypgev evt = { { 0 } }; PyObject *rc = 0; static const char *const kwlist[] = { "nbits", "name", "event", "rng", "nsteps", 0 }; + evt.exc = &exc; if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|sO&O&O&", KWLIST, convuint, &nbits, &name, convpgev, &evt, convgrand, &r, convuint, &n)) goto end; if ((x = strongprime(name, MP_NEW, nbits, - r, n, evt.proc, evt.ctx)) == 0) - PGENERR; + r, n, evt.ev.proc, evt.ev.ctx)) == 0) + PGENERR(&exc); rc = mp_pywrap(x); x = 0; end: @@ -1013,7 +1016,8 @@ end: static PyObject *meth_limlee(PyObject *me, PyObject *arg, PyObject *kw) { char *p = "p"; - pgev ie = { 0 }, oe = { 0 }; + struct excinfo exc = EXCINFO_INIT; + pypgev ie = { { 0 } }, oe = { { 0 } }; unsigned ql, pl; grand *r = &rand_global; unsigned on = 0; @@ -1023,17 +1027,19 @@ static PyObject *meth_limlee(PyObject *me, PyObject *arg, PyObject *kw) { "pbits", "qbits", "name", "event", "ievent", "rng", "nsteps", 0 }; mp *x = 0, **v = 0; + ie.exc = oe.exc = &exc; if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|sO&O&O&O&:limlee", KWLIST, convuint, &pl, convuint, &ql, &p, convpgev, &oe, convpgev, &ie, convgrand, &r, convuint, &on)) goto end; if ((x = limlee(p, MP_NEW, MP_NEW, ql, pl, r, on, - oe.proc, oe.ctx, ie.proc, ie.ctx, &nf, &v)) == 0) - PGENERR; + oe.ev.proc, oe.ev.ctx, ie.ev.proc, ie.ev.ctx, + &nf, &v)) == 0) + PGENERR(&exc);; vec = PyList_New(nf); for (i = 0; i < nf; i++) - PyList_SetItem(vec, i, mp_pywrap(v[i])); + PyList_SET_ITEM(vec, i, mp_pywrap(v[i])); xfree(v); rc = Py_BuildValue("(NN)", mp_pywrap(x), vec); end: @@ -1043,23 +1049,24 @@ end: /*----- Global stuff ------------------------------------------------------*/ -static PyMethodDef methods[] = { +static const PyMethodDef methods[] = { #define METHNAME(name) meth_##name - METH (_PrimeFilter_smallfactor, "smallfactor(X) -> PGRC") - METH (_RabinMiller_iters, "iters(NBITS) -> NITERS") - KWMETH(pgen, "\ -pgen(START, [name = 'p', stepper = PrimeGenStepper(2),\n\ - tester = PrimeGenTester(), event = pgen_nullev,\n\ - nsteps = 0, ntests = RabinMiller.iters(START.nbits)]) -> P") - KWMETH(strongprime_setup, "\ -strongprime_setup(NBITS, [name = 'p', event = pgen_nullev,\n\ - rng = rand, nsteps = 0]) -> (START, JUMP)") - KWMETH(strongprime, "\ -strongprime(NBITS, [name = 'p', event = pgen_nullev,\n\ - rng = rand, nsteps = 0]) -> P") - KWMETH(limlee, "\ -limlee(PBITS, QBITS, [name = 'p', event = pgen_nullev,\n\ - ievent = pgen_nullev, rng = rand, nsteps = 0]) -> (P, [Q, ...])") + METH (_PrimeFilter_smallfactor, "smallfactor(X) -> PGRC") + METH (_RabinMiller_iters, "iters(NBITS) -> NITERS") + KWMETH(pgen, + "pgen(START, [name = 'p'], [stepper = PrimeGenStepper(2)],\n" + " [tester = PrimeGenTester()], [event = pgen_nullev],\n" + " [nsteps = 0], [ntests = RabinMiller.iters(START.nbits)]) -> P") + KWMETH(strongprime_setup, + "strongprime_setup(NBITS, [name = 'p'], [event = pgen_nullev],\n" + " [rng = rand], [nsteps = 0]) -> (START, JUMP)") + KWMETH(strongprime, + "strongprime(NBITS, [name = 'p'], [event = pgen_nullev],\n" + " [rng = rand], [nsteps = 0]) -> P") + KWMETH(limlee, + "limlee(PBITS, QBITS, [name = 'p'], [event = pgen_nullev],\n" + " [ievent = pgen_nullev], [rng = rand], [nsteps = 0]) " + "-> (P, [Q, ...])") #undef METHNAME { 0 } };