util.c (mkexc): Populate dictionary before constructing exception class.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 24 Nov 2019 15:07:08 +0000 (15:07 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 27 Nov 2019 15:10:43 +0000 (15:10 +0000)
It turns out that the dictionary contents are copied into the new class
rather than being used by reference, so populating it later doesn't
actually do anything.

util.c

diff --git a/util.c b/util.c
index 9332c8b..29f7d12 100644 (file)
--- a/util.c
+++ b/util.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);