Apply two changes, systematically:
(1) Use PyErr_NewException("module.class", NULL, NULL) to create the
exception object.
(2) Remove all calls to Py_FatalError(); instead, return or
ignore the errors -- the import code now checks PyErr_Occurred()
after calling a module's init function, so it's no longer a
fatal error for the initialization to fail.
Also did some small cleanups, e.g. removed unnecessary test for
"already initialized" from initfpectl(), and unified
initposix()/initnt().
I haven't checked this very thoroughly, so while the changes are
pretty trivial -- beware of untested code!
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 48ce55a..77cf5d1 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -1314,17 +1314,16 @@
/* Convenience routine to export an integer value.
*
- * Since this function is called only from initsocket/init_socket(), any
- * errors trigger a fatal exception.
+ * Errors are silently ignored, for better or for worse...
*/
static void
BUILD_FUNC_DEF_3(insint,PyObject *,d, char *,name, int,value)
{
PyObject *v = PyInt_FromLong((long) value);
if (!v || PyDict_SetItemString(d, name, v))
- Py_FatalError("can't initialize socket module");
+ PyErr_Clear();
- Py_DECREF(v);
+ Py_XDECREF(v);
}
@@ -1398,15 +1397,15 @@
m = Py_InitModule("socket", PySocket_methods);
#endif
d = PyModule_GetDict(m);
- PySocket_Error = PyString_FromString("socket.error");
- if (PySocket_Error == NULL ||
- PyDict_SetItemString(d, "error", PySocket_Error) != 0)
- Py_FatalError("can't define socket.error");
+ PySocket_Error = PyErr_NewException("socket.error", NULL, NULL);
+ if (PySocket_Error == NULL)
+ return;
+ PyDict_SetItemString(d, "error", PySocket_Error);
PySocketSock_Type.ob_type = &PyType_Type;
Py_INCREF(&PySocketSock_Type);
if (PyDict_SetItemString(d, "SocketType",
(PyObject *)&PySocketSock_Type) != 0)
- Py_FatalError("can't define socket.SocketType");
+ return;
insint(d, "AF_INET", AF_INET);
#ifdef AF_UNIX
insint(d, "AF_UNIX", AF_UNIX);