Message366368
The example in the docs that shows how to initialise an embedded module gives a wrong impression about error handling. Most of the functions that it calls return error codes, but those do not get looked at. Innocent users who copy and paste the example may miss some of them when adapting the code, thus ending up with an unsafe implementation.
The example should at least make it clear where error handling is needed.
https://docs.python.org/3/extending/extending.html#the-module-s-method-table-and-initialization-function
int
main(int argc, char *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
/* Add a built-in module, before Py_Initialize */
PyImport_AppendInittab("spam", PyInit_spam);
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(program);
/* Initialize the Python interpreter. Required. */
Py_Initialize();
/* Optionally import the module; alternatively,
import can be deferred until the embedded script
imports it. */
PyImport_ImportModule("spam");
...
PyMem_RawFree(program);
return 0;
} |
|
Date |
User |
Action |
Args |
2020-04-14 06:08:29 | scoder | set | recipients:
+ scoder, docs@python |
2020-04-14 06:08:29 | scoder | set | messageid: <1586844509.3.0.451475770029.issue40279@roundup.psfhosted.org> |
2020-04-14 06:08:29 | scoder | link | issue40279 messages |
2020-04-14 06:08:28 | scoder | create | |
|