This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author scoder
Recipients docs@python, scoder
Date 2020-04-14.06:08:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1586844509.3.0.451475770029.issue40279@roundup.psfhosted.org>
In-reply-to
Content
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;
}
History
Date User Action Args
2020-04-14 06:08:29scodersetrecipients: + scoder, docs@python
2020-04-14 06:08:29scodersetmessageid: <1586844509.3.0.451475770029.issue40279@roundup.psfhosted.org>
2020-04-14 06:08:29scoderlinkissue40279 messages
2020-04-14 06:08:28scodercreate