]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40279: Add some error-handling to the module initialisation docs example (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 25 Apr 2020 05:45:48 +0000 (22:45 -0700)
committerGitHub <noreply@github.com>
Sat, 25 Apr 2020 05:45:48 +0000 (07:45 +0200)
(cherry picked from commit d4f3923d5901ef1ccdbe6ad6c5a753af90832a0f)

Co-authored-by: Cajetan Rodrigues <caje731@gmail.com>
Doc/extending/extending.rst

index 5b32a2cdc5506efa302d979a76c2e8dda136040c..25dc2934d29ef6fcaba24faa96e21d934dc0b308 100644 (file)
@@ -395,18 +395,26 @@ optionally followed by an import of the module::
        }
 
        /* Add a built-in module, before Py_Initialize */
-       PyImport_AppendInittab("spam", PyInit_spam);
+       if (PyImport_AppendInittab("spam", PyInit_spam) == -1) {
+           fprintf(stderr, "Error: could not extend in-built modules table\n");
+           exit(1);
+       }
 
        /* Pass argv[0] to the Python interpreter */
        Py_SetProgramName(program);
 
-       /* Initialize the Python interpreter.  Required. */
+       /* Initialize the Python interpreter.  Required.
+          If this step fails, it will be a fatal error. */
        Py_Initialize();
 
        /* Optionally import the module; alternatively,
           import can be deferred until the embedded script
           imports it. */
-       PyImport_ImportModule("spam");
+       pmodule = PyImport_ImportModule("spam");
+       if (!pmodule) {
+           PyErr_Print();
+           fprintf(stderr, "Error: could not import module 'spam'\n");
+       }
 
        ...