[New-bugs-announce] [issue40279] Documentation example of module init function lacks error handling

Stefan Behnel report at bugs.python.org
Tue Apr 14 02:08:29 EDT 2020


New submission from Stefan Behnel <stefan_ml at behnel.de>:

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;
}

----------
assignee: docs at python
components: Documentation
keywords: easy, newcomer friendly
messages: 366368
nosy: docs at python, scoder
priority: normal
severity: normal
stage: needs patch
status: open
title: Documentation example of module init function lacks error handling
type: enhancement
versions: Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40279>
_______________________________________


More information about the New-bugs-announce mailing list