New submission from Stefan Behnel <stefan...@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@python
components: Documentation
keywords: easy, newcomer friendly
messages: 366368
nosy: docs@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue40279>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to