pgen.c, rand.c: Check correct variable when rejecting delete operations.
[catacomb-python] / pgen.c
diff --git a/pgen.c b/pgen.c
index ae4dcc5..afeeb33 100644 (file)
--- a/pgen.c
+++ b/pgen.c
@@ -1,13 +1,11 @@
 /* -*-c-*-
  *
- * $Id$
- *
  * Prime number generation
  *
  * (c) 2005 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of the Python interface to Catacomb.
  *
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * Catacomb/Python is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with Catacomb/Python; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
@@ -40,7 +38,7 @@ static PyObject *pfilt_pywrap(pfilt *f)
   o = PyObject_New(pfilt_pyobj, pfilt_pytype);
   o->f = *f;
   o->st = pfilt_step(f, 0);
-  return ((PyObject *)o);  
+  return ((PyObject *)o);
 }
 
 static PyObject *pfilt_pymake(PyTypeObject *ty, PyObject *xobj)
@@ -122,9 +120,8 @@ static PyObject *pfilt_pyint(PyObject *me)
   long l;
   PyObject *rc = 0;
 
-  if (mp_tolong_checked(PFILT_F(me)->m, &l)) goto end;
-  rc = PyInt_FromLong(l);
-end:
+  if (!mp_tolong_checked(PFILT_F(me)->m, &l, 0)) rc = PyInt_FromLong(l);
+  else rc = mp_topylong(PFILT_F(me)->m);
   return (rc);
 }
 
@@ -199,7 +196,7 @@ static PyNumberMethods pfilt_pynumber = {
 
 static PyTypeObject pfilt_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.PrimeFilter",              /* @tp_name@ */
+  "PrimeFilter",                       /* @tp_name@ */
   sizeof(pfilt_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -332,7 +329,7 @@ static PyMethodDef rabin_pymethods[] = {
 
 static PyTypeObject rabin_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.RabinMiller",              /* @tp_name@ */
+  "RabinMiller",                       /* @tp_name@ */
   sizeof(rabin_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -427,6 +424,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);
@@ -450,7 +448,7 @@ static PyGetSetDef pgevent_pygetset[] = {
 
 static PyTypeObject pgevent_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.PrimeGenEvent",            /* @tp_name@ */
+  "PrimeGenEvent",                     /* @tp_name@ */
   sizeof(pgevent_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -548,7 +546,7 @@ static int pgev_python(int rq, pgen_event *ev, void *p)
     goto end;
   else if (l < PGEN_ABORT || l > PGEN_PASS)
     VALERR("return code out of range");
-  else 
+  else
     st = l;
 end:
   if (pyev) {
@@ -619,19 +617,19 @@ 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 }
 };
 
 static PyTypeObject pgev_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.PrimeGenBuiltinHandler",   /* @tp_name@ */
+  "PrimeGenBuiltinHandler",            /* @tp_name@ */
   sizeof(pgev_pyobj),                  /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -698,14 +696,14 @@ static PyObject *psget_step(PyObject *me, void *hunoz)
 
 static PyGetSetDef pgstep_pygetset[] = {
 #define GETSETNAME(op, name) ps##op##_##name
-  GET  (step,          "S.step -> step size for the stepper")
+  GET  (step,          "S.step -> step size for the stepper")
 #undef GETSETNAME
   { 0 }
 };
 
 static PyTypeObject pgstep_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.PrimeGenStepper",          /* @tp_name@ */
+  "PrimeGenStepper",                   /* @tp_name@ */
   sizeof(pgstep_pyobj),                        /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -780,14 +778,14 @@ static PyObject *pjget_jump(PyObject *me, void *hunoz)
 
 static PyGetSetDef pgjump_pygetset[] = {
 #define GETSETNAME(op, name) pj##op##_##name
-  GET  (jump,          "S.jump -> jump size for the stepper")
+  GET  (jump,          "S.jump -> jump size for the stepper")
 #undef GETSETNAME
   { 0 }
 };
 
 static PyTypeObject pgjump_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.PrimeGenJumper",           /* @tp_name@ */
+  "PrimeGenJumper",                    /* @tp_name@ */
   sizeof(pgjump_pyobj),                        /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -848,7 +846,7 @@ end:
 
 static PyTypeObject pgtest_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.PrimeGenTester",           /* @tp_name@ */
+  "PrimeGenTester",                    /* @tp_name@ */
   sizeof(pgtest_pyobj),                        /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -933,7 +931,7 @@ static PyObject *meth_pgen(PyObject *me, PyObject *arg, PyObject *kw)
 end:
   mp_drop(r); mp_drop(x);
   droppgev(&step); droppgev(&test); droppgev(&evt);
-  return (rc);    
+  return (rc);
 }
 
 static PyObject *meth_strongprime_setup(PyObject *me,
@@ -1030,18 +1028,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_setup(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 }
 };