util.c (mkexc): Populate dictionary before constructing exception class.
[pyke] / util.c
diff --git a/util.c b/util.c
index d613a7a..29f7d12 100644 (file)
--- a/util.c
+++ b/util.c
@@ -280,14 +280,14 @@ PyTypeObject *inittype(PyTypeObject *tyskel, PyTypeObject *meta)
 void setconstants(PyObject *mod, const struct nameval *c)
 {
   PyObject *x;
+  unsigned long u;
 
   while (c->name) {
-    if (c->value > LONG_MAX)
-      x = PyLong_FromUnsignedLong(c->value);
-    else
-      x = PyInt_FromLong(c->value);
-    PyModule_AddObject(mod, (/*unconst*/ char *)c->name, x);
-    c++;
+    u = c->value;
+    if (u <= LONG_MAX) x = PyInt_FromLong(u);
+    else if (c->f&CF_SIGNED) x = PyInt_FromLong(-1 - (long)(ULONG_MAX - u));
+    else x = PyLong_FromUnsignedLong(u);
+    PyModule_AddObject(mod, (/*unconst*/ char *)c->name, x); c++;
   }
 }
 
@@ -324,13 +324,7 @@ PyObject *mkexc(PyObject *mod, PyObject *base,
   PyObject *func = 0;
   PyObject *meth = 0;
 
-  if ((nameobj = PyString_FromFormat("%s.%s",
-                                    PyModule_GetName(mod),
-                                    name)) == 0 ||
-      (dict = PyDict_New()) == 0 ||
-      (exc = PyErr_NewException(PyString_AS_STRING(nameobj),
-                               base, dict)) == 0)
-    goto fail;
+  if ((dict = PyDict_New()) == 0) goto fail;
 
   if (mm) {
     while (mm->ml_name) {
@@ -344,6 +338,13 @@ PyObject *mkexc(PyObject *mod, PyObject *base,
     }
   }
 
+  if ((nameobj = PyString_FromFormat("%s.%s",
+                                    PyModule_GetName(mod),
+                                    name)) == 0 ||
+      (exc = PyErr_NewException(PyString_AS_STRING(nameobj),
+                               base, dict)) == 0)
+    goto fail;
+
 done:
   Py_XDECREF(nameobj);
   Py_XDECREF(dict);