X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/bc243788edd0e564df8b67d0750a238d978c954b..e95512be8777eb7038fa26bffa0709013dc08f19:/pgen.c diff --git a/pgen.c b/pgen.c index 108fa31..18b0c26 100644 --- a/pgen.c +++ b/pgen.c @@ -57,10 +57,10 @@ end: static PyObject *pfilt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { - char *kwlist[] = { "x", 0 }; + static const char *const kwlist[] = { "x", 0 }; PyObject *xobj; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", kwlist, &xobj)) + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", KWLIST, &xobj)) return (0); return (pfilt_pymake(ty, xobj)); } @@ -219,7 +219,7 @@ static PyTypeObject pfilt_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Small-primes filter.", +"PrimeFilter(X): small-primes filter.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -256,9 +256,9 @@ static PyObject *rabin_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { mp *x = 0; rabin_pyobj *o = 0; - char *kwlist[] = { "x", 0 }; + static const char *const kwlist[] = { "x", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", kwlist, convmp, &x)) + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convmp, &x)) goto end; if (!MP_POSP(x) || MP_EVENP(x)) VALERR("must be positive and odd"); o = (rabin_pyobj *)ty->tp_alloc(ty, 0); @@ -352,7 +352,7 @@ static PyTypeObject rabin_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Rabin-Miller strong primality test.", +"RabinMiller(X): Rabin-Miller strong primality test.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -379,6 +379,7 @@ static PyTypeObject rabin_pytype_skel = { typedef struct pgevent_pyobj { PyObject_HEAD + PyObject *r; pgen_event *ev; } pgevent_pyobj; @@ -388,18 +389,30 @@ static PyTypeObject *pgevent_pytype; static PyObject *pgevent_pywrap(pgen_event *ev) { pgevent_pyobj *o = PyObject_New(pgevent_pyobj, pgevent_pytype); - o->ev = ev; + o->ev = ev; o->r = 0; return ((PyObject *)o); } static CONVFUNC(pgevent, pgen_event *, PGEVENT_EV) -static void pgevent_kill(PyObject *me) { PGEVENT_EV(me) = 0; } -static void pgevent_pydealloc(PyObject *me) { FREEOBJ(me); } +static void pgevent_kill(PyObject *me) +{ + pgevent_pyobj *ev = (pgevent_pyobj *)me; + + ev->ev = 0; + if (ev->r) GRAND_R(ev->r) = 0; +} + +static void pgevent_pydealloc(PyObject *me) +{ + pgevent_pyobj *ev = (pgevent_pyobj *)me; + if (ev->r) Py_DECREF(ev->r); + FREEOBJ(me); +} #define PGEVENT_CHECK(me) do { \ if (!PGEVENT_EV(me)) { \ - PyErr_SetString(PyExc_ValueError, "event object is dead"); \ + PyErr_SetString(PyExc_ValueError, "event object is no longer valid"); \ return (0); \ } \ } while (0) @@ -417,7 +430,13 @@ static PyObject *peget_tests(PyObject *me, void *hunoz) { PGEVENT_CHECK(me); return (PyInt_FromLong(PGEVENT_EV(me)->tests)); } static PyObject *peget_rng(PyObject *me, void *hunoz) - { PGEVENT_CHECK(me); return (grand_pywrap(PGEVENT_EV(me)->r, 0)); } +{ + pgevent_pyobj *ev = (pgevent_pyobj *)me; + + PGEVENT_CHECK(me); + if (!ev->r) ev->r = grand_pywrap(ev->ev->r, 0); + Py_INCREF(ev->r); return ((PyObject *)ev->r); +} static int peset_x(PyObject *me, PyObject *xobj, void *hunoz) { @@ -529,15 +548,15 @@ static int pgev_python(int rq, pgen_event *ev, void *p) PyObject *rc = 0; int st = PGEN_ABORT; long l; - char *meth[] = { - "pg_abort", "pg_done", "pg_begin", "pg_try", "pg_fail", "pg_pass" - }; + 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, meth[rq], "(O)", pyev)) == 0) + if ((rc = PyObject_CallMethod(py, (/*unconst*/ char *)meth[rq], + "(O)", pyev)) == 0) goto end; if (rc == Py_None) st = PGEN_TRY; @@ -678,9 +697,9 @@ static PyObject *pgstep_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { mpw s; pgstep_pyobj *rc = 0; - char *kwlist[] = { "step", 0 }; + static const char *const kwlist[] = { "step", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", kwlist, convmpw, &s)) + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convmpw, &s)) goto end; rc = (pgstep_pyobj *)ty->tp_alloc(ty, 0); rc->f.step = s; @@ -725,7 +744,7 @@ static PyTypeObject pgstep_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ - "Simple prime-number stepper with small-factors filter.", +"PrimeGenStepper(STEP): simple stepper with small-factors filter.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -752,9 +771,9 @@ static PyObject *pgjump_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { PyObject *o, *fobj; pgjump_pyobj *rc = 0; - char *kwlist[] = { "jump", 0 }; + static const char *const kwlist[] = { "jump", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", kwlist, &o) || + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", KWLIST, &o) || (fobj = pfilt_pymake(pfilt_pytype, o)) == 0) goto end; rc = (pgjump_pyobj *)ty->tp_alloc(ty, 0); @@ -807,7 +826,7 @@ static PyTypeObject pgjump_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Stepper for larger steps, with small-factors filter.", +"PrimeGenJumper(JUMP): stepper for larger steps with small-factors filter.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -833,9 +852,9 @@ static PyTypeObject pgjump_pytype_skel = { static PyObject *pgtest_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { pgtest_pyobj *rc = 0; - char *kwlist[] = { 0 }; + static const char *const kwlist[] = { 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) goto end; + if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST)) goto end; rc = (pgtest_pyobj *)ty->tp_alloc(ty, 0); rc->pg.proc = pgen_test; rc->pg.ctx = &rc->r; @@ -868,7 +887,7 @@ static PyTypeObject pgtest_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Rabin-Miller tester.", +"PrimeGenTester(): Rabin-Miller tester.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -909,12 +928,12 @@ static PyObject *meth_pgen(PyObject *me, PyObject *arg, PyObject *kw) rabin tc; pgev step = { 0 }, test = { 0 }, evt = { 0 }; unsigned nsteps = 0, ntests = 0; - char *kwlist[] = { "start", "name", "stepper", "tester", "event", - "nsteps", "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; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|sO&O&O&O&O&:pgen", kwlist, + 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)) @@ -944,9 +963,10 @@ static PyObject *meth_strongprime_setup(PyObject *me, unsigned n = 0; pgev evt = { 0 }; PyObject *rc = 0; - char *kwlist[] = { "nbits", "name", "event", "rng", "nsteps", 0 }; + static const char *const kwlist[] = + { "nbits", "name", "event", "rng", "nsteps", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|sO&O&O&", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|sO&O&O&", KWLIST, convuint, &nbits, &name, convpgev, &evt, convgrand, &r, convuint, &n)) @@ -971,9 +991,10 @@ static PyObject *meth_strongprime(PyObject *me, PyObject *arg, PyObject *kw) unsigned n = 0; pgev evt = { 0 }; PyObject *rc = 0; - char *kwlist[] = { "nbits", "name", "event", "rng", "nsteps", 0 }; + static const char *const kwlist[] = + { "nbits", "name", "event", "rng", "nsteps", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|sO&O&O&", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|sO&O&O&", KWLIST, convuint, &nbits, &name, convpgev, &evt, convgrand, &r, convuint, &n)) @@ -998,11 +1019,11 @@ static PyObject *meth_limlee(PyObject *me, PyObject *arg, PyObject *kw) unsigned on = 0; size_t i, nf = 0; PyObject *rc = 0, *vec; - char *kwlist[] = { "pbits", "qbits", "name", "event", "ievent", - "rng", "nsteps", 0 }; + static const char *const kwlist[] = + { "pbits", "qbits", "name", "event", "ievent", "rng", "nsteps", 0 }; mp *x = 0, **v = 0; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|sO&O&O&O&:limlee", kwlist, + 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))