t/t-bytes.py: Check that indexing, slicing, etc. return `C.ByteString'.
[catacomb-python] / pgen.c
diff --git a/pgen.c b/pgen.c
index 8b5f6b2..9f9e1a2 100644 (file)
--- 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));
 }
@@ -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);
@@ -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,13 +430,20 @@ 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)
 {
   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);
@@ -524,20 +544,19 @@ 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;
   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(pg->obj, (/*unconst*/ char *)meth[rq],
+                               "(O)", pyev)) == 0)
     goto end;
   if (rc == Py_None)
     st = PGEN_TRY;
@@ -548,12 +567,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);
 }
 
@@ -568,24 +588,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)
@@ -616,12 +634,12 @@ static PyObject *pgev_stdev(pgen_proc *proc)
 
 static 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) -> PGRC -- prime generation aborted")
+  METH (pg_done,       "E.pg_done(EV) -> PGRC -- prime generation finished")
+  METH (pg_begin,     "E.pg_begin(EV) -> PGRC -- commence stepping/testing")
+  METH (pg_try,        "E.pg_try(EV) -> PGRC -- found new candidate")
+  METH (pg_pass,       "E.pg_pass(EV) -> PGRC -- passed primality test")
+  METH (pg_fail,       "E.pg_fail(EV) -> PGRC -- failed primality test")
 #undef METHNAME
   { 0 }
 };
@@ -678,9 +696,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;
@@ -752,9 +770,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);
@@ -833,9 +851,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;
@@ -893,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)
@@ -907,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;
-  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,
+  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);
@@ -942,18 +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;
-  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,
+  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:
@@ -969,18 +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;
-  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,
+  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:
@@ -992,24 +1016,27 @@ 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;
   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,
+  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]));
@@ -1027,18 +1054,18 @@ static PyMethodDef methods[] = {
   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")
+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)")
+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")
+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, ...])")
+limlee(PBITS, QBITS, [name = 'p'], [event = pgen_nullev],\n\
+       [ievent = pgen_nullev], [rng = rand], [nsteps = 0]) -> (P, [Q, ...])")
 #undef METHNAME
   { 0 }
 };