diff Python/exceptions.c @ 27559:faabd9867fb8 legacy-trunk

Provide __module__ attributes for functions defined in C and Python. __module__ is the string name of the module the function was defined in, just like __module__ of classes. In some cases, particularly for C functions, the __module__ may be None. Change PyCFunction_New() from a function to a macro, but keep an unused copy of the function around so that we don't change the binary API. Change pickle's save_global() to use whichmodule() if __module__ is None, but add the __module__ logic to whichmodule() since it might be used outside of pickle.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Fri, 31 Jan 2003 18:33:18 +0000
parents b9c912e2c3c0
children b38cebc76d82
line wrap: on
line diff
--- a/Python/exceptions.c	Fri Jan 31 18:15:58 2003 +0000
+++ b/Python/exceptions.c	Fri Jan 31 18:33:18 2003 +0000
@@ -126,22 +126,27 @@
 static int
 populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods)
 {
+    PyObject *module;
+    int status = -1;
+
     if (!methods)
 	return 0;
 
+    module = PyString_FromString("exceptions");
+    if (!module)
+	return 0;
     while (methods->ml_name) {
 	/* get a wrapper for the built-in function */
-	PyObject *func = PyCFunction_New(methods, NULL);
+	PyObject *func = PyCFunction_NewEx(methods, NULL, module);
 	PyObject *meth;
-	int status;
 
 	if (!func)
-	    return -1;
+	    goto status;
 
 	/* turn the function into an unbound method */
 	if (!(meth = PyMethod_New(func, NULL, klass))) {
 	    Py_DECREF(func);
-	    return -1;
+	    goto status;
 	}
 
 	/* add method to dictionary */
@@ -151,11 +156,14 @@
 
 	/* stop now if an error occurred, otherwise do the next method */
 	if (status)
-	    return status;
+	    goto status;
 
 	methods++;
     }
-    return 0;
+    status = 0;
+ status:
+    Py_DECREF(module);
+    return status;
 }