bpo-40279: Add some error-handling to the module initialisation docs … · python/cpython@d4f3923

GitHub

@@ -395,18 +395,26 @@ optionally followed by an import of the module::

395395 }

396396397397 /* Add a built-in module, before Py_Initialize */

398- PyImport_AppendInittab("spam", PyInit_spam);

398+ if (PyImport_AppendInittab("spam", PyInit_spam) == -1) {

399+ fprintf(stderr, "Error: could not extend in-built modules table\n");

400+ exit(1);

401+ }

399402400403 /* Pass argv[0] to the Python interpreter */

401404 Py_SetProgramName(program);

402405403- /* Initialize the Python interpreter. Required. */

406+ /* Initialize the Python interpreter. Required.

407+ If this step fails, it will be a fatal error. */

404408 Py_Initialize();

405409406410 /* Optionally import the module; alternatively,

407411 import can be deferred until the embedded script

408412 imports it. */

409- PyImport_ImportModule("spam");

413+ pmodule = PyImport_ImportModule("spam");

414+ if (!pmodule) {

415+ PyErr_Print();

416+ fprintf(stderr, "Error: could not import module 'spam'\n");

417+ }

410418411419 ...

412420