8000 Apply two changes, systematically: · python/cpython@0cb96de · GitHub
[go: up one dir, main page]

Skip to content

Commit 0cb96de

Browse files
committed
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!
1 parent ccf0a44 commit 0cb96de

27 files changed

+78
-166
lines changed

Modules/_cursesmodule.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,7 @@ initcurses()
15351535
ModDict = d; /* For PyCurses_InitScr */
15361536

15371537
/* For exception curses.error */
1538-
PyCursesError = PyString_FromString("curses.error");
1538+
PyCursesError = PyErr_NewException("curses.error", NULL, NULL);
15391539
PyDict_SetItemString(d, "error", PyCursesError);
15401540

15411541
/* Make the version available */
@@ -1585,8 +1585,4 @@ initcurses()
15851585
SetDictInt("KEY_MIN", KEY_MIN);
15861586
SetDictInt("KEY_MAX", KEY_MAX);
15871587
}
1588-
1589-
/* Check for errors */
1590-
if (PyErr_Occurred())
1591-
Py_FatalError("can't initi BD91 alize module curses");
15921588
}

Modules/audioop.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,8 +1396,7 @@ initaudioop()
13961396
PyObject *m, *d;
13971397
m = Py_InitModule("audioop", audioop_methods);
13981398
d = PyModule_GetDict(m);
1399-
AudioopError = PyString_FromString("audioop.error");
1400-
if ( AudioopError == NULL
1401-
|| PyDict_SetItemString(d,"error",AudioopError) )
1402-
Py_FatalError("can't define audioop.error");
1399+
AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
1400+
if (AudioopError != NULL)
1401+
PyDict_SetItemString(d,"error",AudioopError);
14031402
}

Modules/bsddbmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ initbsddb() {
746746
Bsddbtype.ob_type = &PyType_Type;
747747
m = Py_InitModule("bsddb", bsddbmodule_methods);
748748
d = PyModule_GetDict(m);
749-
BsddbError = PyString_FromString("bsddb.error");
750-
if (BsddbError == NULL || PyDict_SetItemString(d, "error", BsddbError))
751-
Py_FatalError("can't define bsddb.error");
749+
BsddbError = PyErr_NewException("bsddb.error", NULL, NULL);
750+
if (BsddbError != NULL)
751+
PyDict_SetItemString(d, "error", BsddbError);
752752
}

Modules/clmodule.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ initcl()
10061006
m = Py_InitModule("cl", cl_methods);
10071007
d = PyModule_GetDict(m);
10081008

1009-
ClError = PyString_FromString("cl.error");
1009+
ClError = PyErr_NewException("cl.error", NULL, NULL);
10101010
(void) PyDict_SetItemString(d, "error", ClError);
10111011

10121012
#ifdef CL_ADDED_ALGORITHM_ERROR
@@ -2594,10 +2594,5 @@ initcl()
25942594
Py_DECREF(x);
25952595
#endif
25962596

2597-
if (PyErr_Occurred()) {
2598-
error:
2599-
Py_FatalError("can't initialize module cl");
2600-
}
2601-
26022597
(void) clSetErrorHandler(cl_ErrorHandler);
26032598
}

Modules/dbmmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ initdbm() {
317317

318318
m = Py_InitModule("dbm", dbmmodule_methods);
319319
d = PyModule_GetDict(m);
320-
DbmError = PyString_FromString("dbm.error");
321-
if ( DbmError == NULL || PyDict_SetItemString(d, "error", DbmError) )
322-
Py_FatalError("can't define dbm.error");
320+
DbmError = PyErr_NewException("dbm.error", NULL, NULL);
321+
if (DbmError != NULL)
322+
PyDict_SetItemString(d, "error", DbmError);
323323
}

Modules/dlmodule.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,25 +229,23 @@ initdl()
229229
PyObject *m, *d, *x;
230230

231231
if (sizeof(int) != sizeof(long) ||
232-
sizeof(long) != sizeof(char *))
233-
Py_FatalError(
232+
sizeof(long) != sizeof(char *)) {
233+
Py_Err_SetStr(
234234
"module dl requires sizeof(int) == sizeof(long) == sizeof(char*)");
235+
return;
236+
}
235237

236238
/* Create the module and add the functions */
237239
m = Py_InitModule("dl", dl_methods);
238240

239241
/* Add some symbolic constants to the module */
240242
d = PyModule_GetDict(m);
241-
Dlerror = x = PyString_FromString("dl.error");
243+
Dlerror = x = PyErr_NewException("dl.error", NULL, NULL);
242244
PyDict_SetItemString(d, "error", x);
243245
x = PyInt_FromLong((long)RTLD_LAZY);
244246
PyDict_SetItemString(d, "RTLD_LAZY", x);
245247
#ifdef RTLD_NOW
246248
x = PyInt_FromLong((long)RTLD_NOW);
247249
PyDict_SetItemString(d, "RTLD_NOW", x);
248250
#endif
249-
250-
/* Check for errors */
251-
if (PyErr_Occurred())
252-
Py_FatalError("can't initialize module dl");
253251
}

Modules/fpectlmodule.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,11 @@ static void sigfpe_handler(int signo)
227227
void initfpectl(void)
228228
{
229229
PyObject *m, *d;
230-
static int already_initialized = 0;
231-
232-
if (already_initialized) return;
233230
m = Py_InitModule("fpectl", fpectl_methods);
234231
d = PyModule_GetDict(m);
235-
fpe_error = PyString_FromString("fpectl.error");
236-
PyDict_SetItemString(d, "error", fpe_error);
237-
238-
if (PyErr_Occurred())
239-
Py_FatalError("Cannot initialize module fpectl");
240-
already_initialized = 1;
232+
fpe_error = PyErr_NewException("fpectl.error", NULL, NULL);
233+
if (fpe_error != NULL)
234+
PyDict_SetItemString(d, "error", fpe_error);
241235
}
242236

243237
#ifdef __cplusplus

Modules/fpetestmodule.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,7 @@ void initfpetest(void)
178178

179179
m = Py_InitModule("fpetest", fpetest_methods);
180180
d = PyModule_GetDict(m);
181-
fpe_error = PyString_FromString("fpetest.error");
182-
PyDict_SetItemString(d, "error", fpe_error);
183-
184-
if (PyErr_Occurred())
185-
Py_FatalError("Cannot initialize module fpetest");
181+
fpe_error = PyErr_NewException("fpetest.error", NULL, NULL);
182+
if (fpe_error != NULL)
183+
PyDict_SetItemString(d, "error", fpe_error);
186184
}

Modules/gdbmmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ initgdbm() {
432432

433433
m = Py_InitModule("gdbm", dbmmodule_methods);
434434
d = PyModule_GetDict(m);
435-
DbmError = PyString_FromString("gdbm.error");
436-
if ( DbmError == NULL || PyDict_SetItemString(d, "error", DbmError) )
437-
Py_FatalError("can't define gdbm.error");
435+
DbmError = PyErr_NewException("gdbm.error", NULL, NULL);
436+
if (DbmError != NULL)
437+
PyDict_SetItemString(d, "error", DbmError);
438438
}

Modules/imageop.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,7 @@ initimageop()
752752
PyObject *m, *d;
753753
m = Py_InitModule("imageop", imageop_methods);
754754
d = PyModule_GetDict(m);
755-
ImageopError = PyString_FromString("imageop.error");
756-
if ( ImageopError == NULL ||
757-
PyDict_SetItemString(d,"error",ImageopError) )
758-
Py_FatalError("can't define imageop.error");
755+
ImageopError = PyErr_NewException("imageop.error", NULL, NULL);
756+
if (ImageopError != NULL)
757+
PyDict_SetItemString(d, "error", ImageopError);
759758
}

0 commit comments

Comments
 (0)
0