You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Documentation example of module init function lacks error handling #84460
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2020-04-25.05:46:25.518>created_at=<Date2020-04-14.06:08:29.273>labels= ['easy', 'type-feature', '3.8', '3.9', 'docs']
title='Documentation example of module init function lacks error handling'updated_at=<Date2020-04-25.05:46:25.517>user='https://github.com/scoder'
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.
intmain(intargc, 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");
At the risk of sounding like a jerk, I'd very much want to say that copy-pasters should get what they deserve :) But then I remember I was once a copy-paster too!
Hello again, Stefan :)
Thanks for watching out for the community - I'll be happy to fix this!
New changeset d4f3923 by Cajetan Rodrigues in branch 'master': bpo-40279: Add some error-handling to the module initialisation docs example (GH-19705) d4f3923
New changeset 882a7f4 by Miss Islington (bot) in branch '3.8': bpo-40279: Add some error-handling to the module initialisation docs example (GH-19705) (GH-19710) 882a7f4
Activity
scoder commentedon Apr 14, 2020
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
caje731 commentedon Apr 24, 2020
At the risk of sounding like a jerk, I'd very much want to say that copy-pasters should get what they deserve :) But then I remember I was once a copy-paster too!
Hello again, Stefan :)
Thanks for watching out for the community - I'll be happy to fix this!
scoder commentedon Apr 25, 2020
New changeset d4f3923 by Cajetan Rodrigues in branch 'master':
bpo-40279: Add some error-handling to the module initialisation docs example (GH-19705)
d4f3923
scoder commentedon Apr 25, 2020
New changeset 882a7f4 by Miss Islington (bot) in branch '3.8':
bpo-40279: Add some error-handling to the module initialisation docs example (GH-19705) (GH-19710)
882a7f4
scoder commentedon Apr 25, 2020
Hi Cajetan, thank you for your contribution! That's a nice documentation improvement.