bpo-40279: Add some error-handling to the module initialisation docs example (GH-19705) (GH-19710)

(cherry picked from commit d4f3923d59)

Co-authored-by: Cajetan Rodrigues <caje731@gmail.com>
This commit is contained in:
Miss Islington (bot) 2020-04-24 22:45:48 -07:00 committed by GitHub
parent c7b55e929b
commit 882a7f44da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 3 deletions

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