util.c: Augment `convu64' to collect its argument via `kludge64'.
[pyke] / util.c
diff --git a/util.c b/util.c
index 4e82ed6..cfd4115 100644 (file)
--- a/util.c
+++ b/util.c
@@ -83,6 +83,7 @@ int convulong(PyObject *o, void *pp)
   unsigned long *p = pp;
   PyObject *t;
 
+  if (!o) VALERR("can't delete");
   if (PyInt_Check(o)) {
     i = PyInt_AS_LONG(o);
     if (i < 0) VALERR("must be nonnegative");
@@ -98,15 +99,32 @@ end:
   return (0);
 }
 
+#ifdef HAVE_UINT64
+#  define CONVu64(n) do {                                              \
+     kludge64 k;                                                       \
+     uint64 t;                                                         \
+     if (!convk64(o, &k)) goto end;                                    \
+     t = GET64(uint64, k);                                             \
+     if (t > MASK##n) VALERR("out of range");                          \
+     *p = t;                                                           \
+   } while (0)
+#else
+#  define CONVu64(n) assert(!"shouldn't be possible")
+#endif
+
 #define CONVU_(n)                                                      \
   int convu##n(PyObject *o, void *pp)                                  \
   {                                                                    \
     unsigned long u;                                                   \
     uint##n *p = pp;                                                   \
                                                                        \
-    if (!convulong(o, &u)) goto end;                                   \
-    if (u > MASK##n) VALERR("out of range");                           \
-    *p = u;                                                            \
+    if (MASK##n > ULONG_MAX)                                           \
+      CONVu64(n);                                                      \
+    else {                                                             \
+      if (!convulong(o, &u)) goto end;                                 \
+      if (u > MASK##n) VALERR("out of range");                         \
+      *p = u;                                                          \
+    }                                                                  \
     return (1);                                                                \
   end:                                                                 \
     return (0);                                                                \
@@ -132,6 +150,7 @@ int convk64(PyObject *o, void *pp)
   int rc = 0;
   uint32 lo, hi;
 
+  if (!o) VALERR("can't delete");
   if (init_i32()) goto end;
   if ((i = PyNumber_Int(o)) == 0) goto end;
   lo = PyInt_AsUnsignedLongMask(i);
@@ -176,8 +195,11 @@ end:
 
 int convbool(PyObject *o, void *pp)
 {
+  if (!o) VALERR("can't delete");
   *(int *)pp = PyObject_IsTrue(o);
   return (1);
+end:
+  return (0);
 }
 
 /*----- Type messing ------------------------------------------------------*/
@@ -223,9 +245,9 @@ void typeready(PyTypeObject *ty)
   PyDict_SetItemString(ty->tp_dict, "__module__", modname);
 }
 
-PyTypeObject *inittype(PyTypeObject *tyskel)
+PyTypeObject *inittype(PyTypeObject *tyskel, PyTypeObject *meta)
 {
-  PyTypeObject *ty = newtype(&PyType_Type, tyskel, 0);
+  PyTypeObject *ty = newtype(meta, tyskel, 0);
   ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
   typeready(ty);
   return (ty);