pyke/pyke-mLib.c: Raise `OverflowError' on out-of-range inputs.
[pyke] / pyke.c
diff --git a/pyke.c b/pyke.c
index cef4f82..330f15c 100644 (file)
--- a/pyke.c
+++ b/pyke.c
@@ -46,16 +46,18 @@ PyObject *getbool(int b)
 
 int convulong(PyObject *o, void *pp)
 {
-  long i;
   unsigned long *p = pp;
   PyObject *t;
 
   if (!o) VALERR("can't delete");
+#ifdef PY2
   if (PyInt_Check(o)) {
-    i = PyInt_AS_LONG(o);
+    long i = PyInt_AS_LONG(o);
     if (i < 0) VALERR("must be nonnegative");
     *p = i;
-  } else {
+  } else
+#endif
+  {
     if ((t = PyNumber_Long(o)) == 0) goto end;
     *p = PyLong_AsUnsignedLong(t);
     Py_DECREF(t);
@@ -110,6 +112,7 @@ int convbin(PyObject *o, void *pp)
     r->sz = BIN_LEN(o);
     return (1);
   }
+#ifdef PY2
   if (PyUnicode_Check(o)) {
     o = _PyUnicode_AsDefaultEncodedString(o, 0);
     if (!o) return (0);
@@ -117,6 +120,7 @@ int convbin(PyObject *o, void *pp)
     r->sz = PyString_GET_SIZE(o);
     return (1);
   }
+#endif
   return (PyObject_AsReadBuffer(o, &r->p, &r->sz) ? 0 : 1);
 }
 
@@ -128,6 +132,22 @@ PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
   return (0);
 }
 
+PyObject *enrich_compare(int op, int cmp)
+{
+  int r = -1;
+
+  switch (op) {
+    case Py_LT: r = cmp <  0; break;
+    case Py_LE: r = cmp <= 0; break;
+    case Py_EQ: r = cmp == 0; break;
+    case Py_NE: r = cmp != 0; break;
+    case Py_GE: r = cmp >= 0; break;
+    case Py_GT: r = cmp >  0; break;
+    default: assert(0);
+  }
+  return (getbool(r));
+}
+
 /*----- Saving and restoring exceptions ----------------------------------*/
 
 void report_lost_exception_v(struct excinfo *exc,
@@ -258,6 +278,9 @@ void *newtype(PyTypeObject *metaty,
   if (ty->ht_name)
     ty->ht_type.tp_name = TEXT_STR(ty->ht_name);
   ty->ht_slots = 0;
+#ifdef PY3
+  ty->ht_qualname = 0;
+#endif
   (void)PyObject_INIT(&ty->ht_type, metaty);
   Py_INCREF(metaty);
   return (ty);
@@ -265,6 +288,10 @@ void *newtype(PyTypeObject *metaty,
 
 void typeready(PyTypeObject *ty)
 {
+#ifdef PY3
+  PyHeapTypeObject *hty = (PyHeapTypeObject *)ty;
+  hty->ht_qualname = hty->ht_name;
+#endif
   PyType_Ready(ty);
   PyDict_SetItemString(ty->tp_dict, "__module__", modname);
 }
@@ -296,7 +323,8 @@ PyObject *mkexc(PyObject *mod, PyObject *base,
     while (mm->ml_name) {
       if ((func = PyCFunction_NewEx((/*unconst*/ PyMethodDef *)mm,
                                    0, mod)) == 0 ||
-         (meth = PyMethod_New(func, 0, exc)) == 0 ||
+         (meth = PY23(PyMethod_New(func, 0, exc),
+                      PyInstanceMethod_New(func))) == 0 ||
          PyDict_SetItemString(dict, mm->ml_name, meth))
        goto fail;
       Py_DECREF(func); func = 0;