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!
This commit is contained in:
Guido van Rossum 1997-10-01 04:29:29 +00:00
parent ccf0a44d2d
commit 0cb96de269
27 changed files with 79 additions and 167 deletions

View File

@ -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");
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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");
}

View File

@ -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

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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__)
#define INITFUNC initnt
#define MODNAME "nt"
#else
#define INITFUNC initposix
#define MODNAME "posix"
#endif
void
initnt()
INITFUNC()
{
PyObject *m, *d, *v;
m = Py_InitModule4("nt",
m = Py_InitModule4(MODNAME,
posix_methods,
posix__doc__,
(PyObject *)NULL,
PYTHON_API_VERSION);
(PyObject *)NULL,
PYTHON_API_VERSION);
d = PyModule_GetDict(m);
/* Initialize nt.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 nt.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 NT posixmodule");
/* Initialize exception */
PosixError = PyErr_NewException("os.error", NULL, NULL);
if (PosixError != NULL)
PyDict_SetItemString(d, "error", PosixError);
}
#else /* not a PC port */
void
initposix()
{
PyObject *m, *d, *v;
m = Py_InitModule4("posix",
posix_methods,
posix__doc__,
(PyObject *)NULL,
PYTHON_API_VERSION);
d = PyModule_GetDict(m);
/* Initialize posix.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 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");
}
#endif /* !_MSC_VER */

View File

@ -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 */ ;
}

View File

@ -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 */;
}

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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);

View File

@ -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
}

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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;
}

View File

@ -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");
}

View File

@ -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();
}

View File

@ -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");
}

View File

@ -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");
}