8000 bpo-40279: Add some error-handling to the module initialisation docs … · python/cpython@882a7f4 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 882a7f4

Browse files
bpo-40279: Add some error-handling to the module initialisation docs example (GH-19705) (GH-19710)
(cherry picked from commit d4f3923) Co-authored-by: Cajetan Rodrigues <caje731@gmail.com>
1 parent c7b55e9 commit 882a7f4

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Doc/extending/extending.rst

Lines changed: 11 additions & E883 amp; 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,18 +395,26 @@ optionally followed by an import of the module::
395395
}
396396

397397
/* 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+
}
399402

400403
/* Pass argv[0] to the Python interpreter */
401404
Py_SetProgramName(program);
402405

403-
/* 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();
405409

406410
/* 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+
}
410418

411419
...
412420

0 commit comments

Comments
 (0)
0