diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-04-25 05:45:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-25 05:45:48 (GMT) |
commit | 882a7f44da08c6fb210bd9a17f80903cbca84034 (patch) | |
tree | 59b2a833a29703483951d266384658ba062f1a3e | |
parent | c7b55e929b35ccab552f9efd49eed4a168e97e2f (diff) | |
download | cpython-882a7f44da08c6fb210bd9a17f80903cbca84034.zip cpython-882a7f44da08c6fb210bd9a17f80903cbca84034.tar.gz cpython-882a7f44da08c6fb210bd9a17f80903cbca84034.tar.bz2 |
bpo-40279: Add some error-handling to the module initialisation docs example (GH-19705) (GH-19710)
(cherry picked from commit d4f3923d5901ef1ccdbe6ad6c5a753af90832a0f)
Co-authored-by: Cajetan Rodrigues <caje731@gmail.com>
-rw-r--r-- | Doc/extending/extending.rst | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/Doc/extending/extending.rst b/Doc/extending/extending.rst index 5b32a2c..25dc293 100644 --- a/Doc/extending/extending.rst +++ b/Doc/extending/extending.rst @@ -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"); + } ... |