From 0cb96de2699fa032ef1379ebb62a4dfb66ee5fde Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 1 Oct 1997 04:29:29 +0000 Subject: 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! --- Modules/_cursesmodule.c | 6 +---- Modules/audioop.c | 7 +++-- Modules/bsddbmodule.c | 6 ++--- Modules/clmodule.c | 7 +---- Modules/dbmmodule.c | 6 ++--- Modules/dlmodule.c | 12 ++++----- Modules/fpectlmodule.c | 12 +++------ Modules/fpetestmodule.c | 8 +++--- Modules/gdbmmodule.c | 6 ++--- Modules/imageop.c | 7 +++-- Modules/imgfile.c | 7 +++-- Modules/nismodule.c | 7 +++-- Modules/posixmodule.c | 68 ++++++++++++------------------------------------- Modules/regexmodule.c | 4 +-- Modules/reopmodule.c | 4 +-- Modules/resource.c | 6 +---- Modules/rgbimgmodule.c | 6 ++--- Modules/selectmodule.c | 4 +-- Modules/socketmodule.c | 17 ++++++------- Modules/stdwinmodule.c | 6 ++--- Modules/structmodule.c | 8 +++--- Modules/sunaudiodev.c | 4 +-- Modules/svmodule.c | 4 +-- Modules/termios.c | 5 +--- Modules/threadmodule.c | 6 +---- Modules/xxmodule.c | 6 +---- Modules/zlibmodule.c | 5 +--- 27 files changed, 78 insertions(+), 166 deletions(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 8e85256..a176225 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -1535,7 +1535,7 @@ initcurses() ModDict = d; /* For PyCurses_InitScr */ /* For exception curses.error */ - PyCursesError = PyString_FromString("curses.error"); + PyCursesError = PyErr_NewException("curses.error", NULL, NULL); PyDict_SetItemString(d, "error", PyCursesError); /* Make the version available */ @@ -1585,8 +1585,4 @@ initcurses() SetDictInt("KEY_MIN", KEY_MIN); SetDictInt("KEY_MAX", KEY_MAX); } - - /* Check for errors */ - if (PyErr_Occurred()) - Py_FatalError("can't initialize module curses"); } diff --git a/Modules/audioop.c b/Modules/audioop.c index fc33bcd..c6d9df5 100644 --- a/Modules/audioop.c +++ b/Modules/audioop.c @@ -1396,8 +1396,7 @@ initaudioop() PyObject *m, *d; m = Py_InitModule("audioop", audioop_methods); d = PyModule_GetDict(m); - AudioopError = PyString_FromString("audioop.error"); - if ( AudioopError == NULL - || PyDict_SetItemString(d,"error",AudioopError) ) - Py_FatalError("can't define audioop.error"); + AudioopError = PyErr_NewException("audioop.error", NULL, NULL); + if (AudioopError != NULL) + PyDict_SetItemString(d,"error",AudioopError); } diff --git a/Modules/bsddbmodule.c b/Modules/bsddbmodule.c index 0c9915a..ee979b8 100644 --- a/Modules/bsddbmodule.c +++ b/Modules/bsddbmodule.c @@ -746,7 +746,7 @@ initbsddb() { Bsddbtype.ob_type = &PyType_Type; m = Py_InitModule("bsddb", bsddbmodule_methods); d = PyModule_GetDict(m); - BsddbError = PyString_FromString("bsddb.error"); - if (BsddbError == NULL || PyDict_SetItemString(d, "error", BsddbError)) - Py_FatalError("can't define bsddb.error"); + BsddbError = PyErr_NewException("bsddb.error", NULL, NULL); + if (BsddbError != NULL) + PyDict_SetItemString(d, "error", BsddbError); } diff --git a/Modules/clmodule.c b/Modules/clmodule.c index 1550e58..b8bac31 100644 --- a/Modules/clmodule.c +++ b/Modules/clmodule.c @@ -1006,7 +1006,7 @@ initcl() m = Py_InitModule("cl", cl_methods); d = PyModule_GetDict(m); - ClError = PyString_FromString("cl.error"); + ClError = PyErr_NewException("cl.error", NULL, NULL); (void) PyDict_SetItemString(d, "error", ClError); #ifdef CL_ADDED_ALGORITHM_ERROR @@ -2594,10 +2594,5 @@ initcl() Py_DECREF(x); #endif - if (PyErr_Occurred()) { - error: - Py_FatalError("can't initialize module cl"); - } - (void) clSetErrorHandler(cl_ErrorHandler); } diff --git a/Modules/dbmmodule.c b/Modules/dbmmodule.c index 0b0cb70..ea628f1 100644 --- a/Modules/dbmmodule.c +++ b/Modules/dbmmodule.c @@ -317,7 +317,7 @@ initdbm() { m = Py_InitModule("dbm", dbmmodule_methods); d = PyModule_GetDict(m); - DbmError = PyString_FromString("dbm.error"); - if ( DbmError == NULL || PyDict_SetItemString(d, "error", DbmError) ) - Py_FatalError("can't define dbm.error"); + DbmError = PyErr_NewException("dbm.error", NULL, NULL); + if (DbmError != NULL) + PyDict_SetItemString(d, "error", DbmError); } diff --git a/Modules/dlmodule.c b/Modules/dlmodule.c index d946658..9d7ce71 100644 --- a/Modules/dlmodule.c +++ b/Modules/dlmodule.c @@ -229,16 +229,18 @@ initdl() PyObject *m, *d, *x; if (sizeof(int) != sizeof(long) || - sizeof(long) != sizeof(char *)) - Py_FatalError( + sizeof(long) != sizeof(char *)) { + Py_Err_SetStr( "module dl requires sizeof(int) == sizeof(long) == sizeof(char*)"); + return; + } /* Create the module and add the functions */ m = Py_InitModule("dl", dl_methods); /* Add some symbolic constants to the module */ d = PyModule_GetDict(m); - Dlerror = x = PyString_FromString("dl.error"); + Dlerror = x = PyErr_NewException("dl.error", NULL, NULL); PyDict_SetItemString(d, "error", x); x = PyInt_FromLong((long)RTLD_LAZY); PyDict_SetItemString(d, "RTLD_LAZY", x); @@ -246,8 +248,4 @@ initdl() x = PyInt_FromLong((long)RTLD_NOW); PyDict_SetItemString(d, "RTLD_NOW", x); #endif - - /* Check for errors */ - if (PyErr_Occurred()) - Py_FatalError("can't initialize module dl"); } diff --git a/Modules/fpectlmodule.c b/Modules/fpectlmodule.c index 0b7e8d7..b0ba9db 100644 --- a/Modules/fpectlmodule.c +++ b/Modules/fpectlmodule.c @@ -227,17 +227,11 @@ static void sigfpe_handler(int signo) void initfpectl(void) { PyObject *m, *d; - static int already_initialized = 0; - - if (already_initialized) return; m = Py_InitModule("fpectl", fpectl_methods); d = PyModule_GetDict(m); - fpe_error = PyString_FromString("fpectl.error"); - PyDict_SetItemString(d, "error", fpe_error); - - if (PyErr_Occurred()) - Py_FatalError("Cannot initialize module fpectl"); - already_initialized = 1; + fpe_error = PyErr_NewException("fpectl.error", NULL, NULL); + if (fpe_error != NULL) + PyDict_SetItemString(d, "error", fpe_error); } #ifdef __cplusplus diff --git a/Modules/fpetestmodule.c b/Modules/fpetestmodule.c index b568b60..61dd3db 100644 --- a/Modules/fpetestmodule.c +++ b/Modules/fpetestmodule.c @@ -178,9 +178,7 @@ void initfpetest(void) m = Py_InitModule("fpetest", fpetest_methods); d = PyModule_GetDict(m); - fpe_error = PyString_FromString("fpetest.error"); - PyDict_SetItemString(d, "error", fpe_error); - - if (PyErr_Occurred()) - Py_FatalError("Cannot initialize module fpetest"); + fpe_error = PyErr_NewException("fpetest.error", NULL, NULL); + if (fpe_error != NULL) + PyDict_SetItemString(d, "error", fpe_error); } diff --git a/Modules/gdbmmodule.c b/Modules/gdbmmodule.c index 23ec831..8c870c0 100644 --- a/Modules/gdbmmodule.c +++ b/Modules/gdbmmodule.c @@ -432,7 +432,7 @@ initgdbm() { m = Py_InitModule("gdbm", dbmmodule_methods); d = PyModule_GetDict(m); - DbmError = PyString_FromString("gdbm.error"); - if ( DbmError == NULL || PyDict_SetItemString(d, "error", DbmError) ) - Py_FatalError("can't define gdbm.error"); + DbmError = PyErr_NewException("gdbm.error", NULL, NULL); + if (DbmError != NULL) + PyDict_SetItemString(d, "error", DbmError); } diff --git a/Modules/imageop.c b/Modules/imageop.c index 007c83e..049908d 100644 --- a/Modules/imageop.c +++ b/Modules/imageop.c @@ -752,8 +752,7 @@ initimageop() PyObject *m, *d; m = Py_InitModule("imageop", imageop_methods); d = PyModule_GetDict(m); - ImageopError = PyString_FromString("imageop.error"); - if ( ImageopError == NULL || - PyDict_SetItemString(d,"error",ImageopError) ) - Py_FatalError("can't define imageop.error"); + ImageopError = PyErr_NewException("imageop.error", NULL, NULL); + if (ImageopError != NULL) + PyDict_SetItemString(d, "error", ImageopError); } diff --git a/Modules/imgfile.c b/Modules/imgfile.c index 865ecab..65976c1 100644 --- a/Modules/imgfile.c +++ b/Modules/imgfile.c @@ -560,10 +560,9 @@ initimgfile() PyObject *m, *d; m = Py_InitModule("imgfile", imgfile_methods); d = PyModule_GetDict(m); - ImgfileError = PyString_FromString("imgfile.error"); - if ( ImgfileError == NULL - || PyDict_SetItemString(d, "error", ImgfileError) ) - Py_FatalError("can't define imgfile.error"); + ImgfileError = PyErr_NewException("imgfile.error", NULL, NULL); + if (ImgfileError != NULL) + PyDict_SetItemString(d, "error", ImgfileError); } diff --git a/Modules/nismodule.c b/Modules/nismodule.c index 5866026..cadbad0 100644 --- a/Modules/nismodule.c +++ b/Modules/nismodule.c @@ -370,8 +370,7 @@ initnis () PyObject *m, *d; m = Py_InitModule("nis", nis_methods); d = PyModule_GetDict(m); - NisError = PyString_FromString("nis.error"); - if (NisError == NULL || - PyDict_SetItemString(d, "error", NisError) != 0) - Py_FatalError("Cannot define nis.error"); + NisError = PyErr_NewException("nis.error", NULL, NULL); + if (NisError != NULL) + PyDict_SetItemString(d, "error", NisError); } diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 3069d34..e06827a 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -2254,73 +2254,37 @@ all_ins(d) } -/* XXX The following should be more unified -- only difference left is - function name and module name. */ - #if defined(_MSC_VER) || defined(__WATCOMC__) -void -initnt() -{ - PyObject *m, *d, *v; - - m = Py_InitModule4("nt", - posix_methods, - posix__doc__, - (PyObject *)NULL, - PYTHON_API_VERSION); - d = PyModule_GetDict(m); - - /* Initialize nt.environ dictionary */ - v = convertenviron(); - if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0) - goto finally; - Py_DECREF(v); - - if (all_ins(d)) - goto finally; - - /* Initialize nt.error exception */ - PosixError = PyString_FromString("os.error"); - PyDict_SetItemString(d, "error", PosixError); - - if (!PyErr_Occurred()) - return; +#define INITFUNC initnt +#define MODNAME "nt" +#else +#define INITFUNC initposix +#define MODNAME "posix" +#endif - finally: - /* XXX Shouldn't */ - Py_FatalError("can't initialize NT posixmodule"); -} -#else /* not a PC port */ void -initposix() +INITFUNC() { PyObject *m, *d, *v; - m = Py_InitModule4("posix", + m = Py_InitModule4(MODNAME, posix_methods, posix__doc__, - (PyObject *)NULL, - PYTHON_API_VERSION); + (PyObject *)NULL, + PYTHON_API_VERSION); d = PyModule_GetDict(m); - /* Initialize posix.environ dictionary */ + /* Initialize environ dictionary */ v = convertenviron(); if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0) - goto finally; + return; Py_DECREF(v); if (all_ins(d)) - goto finally; - - /* Initialize posix.error exception */ - PosixError = PyString_FromString("os.error"); - PyDict_SetItemString(d, "error", PosixError); - - if (!PyErr_Occurred()) return; - finally: - /* XXX Shouldn't */ - Py_FatalError("can't initialize posix module"); + /* Initialize exception */ + PosixError = PyErr_NewException("os.error", NULL, NULL); + if (PosixError != NULL) + PyDict_SetItemString(d, "error", PosixError); } -#endif /* !_MSC_VER */ diff --git a/Modules/regexmodule.c b/Modules/regexmodule.c index 981dc8c..7a289d5 100644 --- a/Modules/regexmodule.c +++ b/Modules/regexmodule.c @@ -718,7 +718,7 @@ initregex() d = PyModule_GetDict(m); /* Initialize regex.error exception */ - v = RegexError = PyString_FromString("regex.error"); + v = RegexError = PyErr_NewException("regex.error", NULL, NULL); if (v == NULL || PyDict_SetItemString(d, "error", v) != 0) goto finally; @@ -742,5 +742,5 @@ initregex() if (!PyErr_Occurred()) return; finally: - Py_FatalError("can't initialize regex module"); + /* Nothing */ ; } diff --git a/Modules/reopmodule.c b/Modules/reopmodule.c index de5c63d..6c671c8 100644 --- a/Modules/reopmodule.c +++ b/Modules/reopmodule.c @@ -958,7 +958,7 @@ initreop() d = PyModule_GetDict(m); /* Initialize reop.error exception */ - v = ReopError = PyString_FromString("reop.error"); + v = ReopError = PyErr_NewException("reop.error", NULL, NULL); if (v == NULL || PyDict_SetItemString(d, "error", v) != 0) goto finally; @@ -1048,6 +1048,6 @@ initreop() return; finally: - Py_FatalError("can't initialize reop module"); + /* Nothing */; } diff --git a/Modules/resource.c b/Modules/resource.c index 428c32c..9518ed6 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -201,7 +201,7 @@ void initresource() /* Add some symbolic constants to the module */ d = PyModule_GetDict(m); - ResourceError = PyString_FromString("resource.error"); + ResourceError = PyErr_NewException("resource.error", NULL, NULL); PyDict_SetItemString(d, "error", ResourceError); /* insert constants */ @@ -264,8 +264,4 @@ void initresource() #ifdef RUSAGE_BOTH ins(d, "RUSAGE_BOTH", RUSAGE_BOTH); #endif - - /* Check for errors */ - if (PyErr_Occurred()) - Py_FatalError("can't initialize module resource"); } diff --git a/Modules/rgbimgmodule.c b/Modules/rgbimgmodule.c index 18d2776..36f6419 100644 --- a/Modules/rgbimgmodule.c +++ b/Modules/rgbimgmodule.c @@ -782,9 +782,7 @@ initrgbimg() PyObject *m, *d; m = Py_InitModule("rgbimg", rgbimg_methods); d = PyModule_GetDict(m); - ImgfileError = PyString_FromString("rgbimg.error"); - if (ImgfileError) + ImgfileError = PyErr_NewException("rgbimg.error", NULL, NULL); + if (ImgfileError != NULL) PyDict_SetItemString(d, "error", ImgfileError); - if (PyErr_Occurred()) - Py_FatalError("can't initialize rgbimg module"); } diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index 047c0d4..7655c3c 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -322,8 +322,6 @@ initselect() PyObject *m, *d; m = Py_InitModule("select", select_methods); d = PyModule_GetDict(m); - SelectError = PyString_FromString("select.error"); + SelectError = PyErr_NewException("select.error", NULL, NULL); PyDict_SetItemString(d, "error", SelectError); - if (PyErr_Occurred()) - Py_FatalError("Cannot initialize select module"); } 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 @@ static PyMethodDef PySocket_methods[] = { /* 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 @@ initsocket() 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); diff --git a/Modules/stdwinmodule.c b/Modules/stdwinmodule.c index a67bc33..0403138 100644 --- a/Modules/stdwinmodule.c +++ b/Modules/stdwinmodule.c @@ -2651,13 +2651,11 @@ initstdwin() d = PyModule_GetDict(m); /* Initialize stdwin.error exception */ - StdwinError = PyString_FromString("stdwin.error"); + StdwinError = PyErr_NewException("stdwin.error", NULL, NULL); if (StdwinError == NULL || PyDict_SetItemString(d, "error", StdwinError) != 0) - Py_FatalError("can't define stdwin.error"); + return; #ifdef WITH_THREAD StdwinLock = allocate_lock(); - if (StdwinLock == NULL) - Py_FatalError("can't allocate stdwin lock"); #endif } diff --git a/Modules/structmodule.c b/Modules/structmodule.c index ef35bd7..4b01f4b 100644 --- a/Modules/structmodule.c +++ b/Modules/structmodule.c @@ -1298,10 +1298,8 @@ initstruct() /* Add some symbolic constants to the module */ d = PyModule_GetDict(m); - StructError = PyString_FromString("struct.error"); + StructError = PyErr_NewException("struct.error", NULL, NULL); + if (StructError == NULL) + return; PyDict_SetItemString(d, "error", StructError); - - /* Check for errors */ - if (PyErr_Occurred()) - Py_FatalError("can't initialize module struct"); } diff --git a/Modules/sunaudiodev.c b/Modules/sunaudiodev.c index 6155445..d0edea6 100644 --- a/Modules/sunaudiodev.c +++ b/Modules/sunaudiodev.c @@ -508,9 +508,7 @@ initsunaudiodev() m = Py_InitModule("sunaudiodev", sunaudiodev_methods); d = PyModule_GetDict(m); - SunAudioError = PyString_FromString("sunaudiodev.error"); + SunAudioError = PyErr_NewException("sunaudiodev.error", NULL, NULL); if (SunAudioError) PyDict_SetItemString(d, "error", SunAudioError); - if (PyErr_Occurred()) - Py_FatalError("can't initialize sunaudiodev module"); } diff --git a/Modules/svmodule.c b/Modules/svmodule.c index bc6d13f..75f2023 100644 --- a/Modules/svmodule.c +++ b/Modules/svmodule.c @@ -1070,7 +1070,7 @@ initsv() m = Py_InitModule("sv", sv_methods); d = PyModule_GetDict(m); - SvError = PyString_FromString("sv.error"); + SvError = PyErr_NewException("sv.error", NULL, NULL); if (SvError == NULL || PyDict_SetItemString(d, "error", SvError) != 0) - Py_FatalError("can't define sv.error"); + return; } diff --git a/Modules/termios.c b/Modules/termios.c index 12418b5..5e46630 100644 --- a/Modules/termios.c +++ b/Modules/termios.c @@ -241,9 +241,6 @@ PyInit_termios() m = Py_InitModule("termios", termios_methods); d = PyModule_GetDict(m); - TermiosError = Py_BuildValue("s", "termios.error"); + TermiosError = PyErr_NewException("termios.error", NULL, NULL); PyDict_SetItemString(d, "error", TermiosError); - - if (PyErr_Occurred()) - Py_FatalError("can't initialize module termios"); } diff --git a/Modules/threadmodule.c b/Modules/threadmodule.c index 49cefcc..214263f 100644 --- a/Modules/threadmodule.c +++ b/Modules/threadmodule.c @@ -356,13 +356,9 @@ initthread() /* Add a symbolic constant */ d = PyModule_GetDict(m); - ThreadError = PyString_FromString("thread.error"); + ThreadError = PyErr_NewException("thread.error", NULL, NULL); PyDict_SetItemString(d, "error", ThreadError); - /* Check for errors */ - if (PyErr_Occurred()) - Py_FatalError("can't initialize module thread"); - /* Initialize the C thread library */ init_thread(); } diff --git a/Modules/xxmodule.c b/Modules/xxmodule.c index 465da68..792ef3f 100644 --- a/Modules/xxmodule.c +++ b/Modules/xxmodule.c @@ -231,10 +231,6 @@ initxx() /* Add some symbolic constants to the module */ d = PyModule_GetDict(m); - ErrorObject = PyString_FromString("xx.error"); + ErrorObject = PyErr_NewException("xx.error", NULL, NULL); PyDict_SetItemString(d, "error", ErrorObject); - - /* Check for errors */ - if (PyErr_Occurred()) - Py_FatalError("can't initialize module xx"); } diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index bc1b6f8..98dbfa5 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -801,7 +801,7 @@ PyInit_zlib() zlib_module_documentation, (PyObject*)NULL,PYTHON_API_VERSION); d = PyModule_GetDict(m); - ZlibError = Py_BuildValue("s", "zlib.error"); + ZlibError = PyErr_NewException("zlib.error", NULL, NULL); PyDict_SetItemString(d, "error", ZlibError); insint(d, "MAX_WBITS", MAX_WBITS); @@ -815,7 +815,4 @@ PyInit_zlib() insint(d, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY); ver = PyString_FromString(ZLIB_VERSION); PyDict_SetItemString(d, "ZLIB_VERSION", ver); - - if (PyErr_Occurred()) - Py_FatalError("can't initialize module zlib"); } -- cgit v0.12