3/extending/extending.html: C issues
In docs.python.org/3/extending/extending.html, one of the examples says static int PySpam_System(...) { ... } ... { static void *PySpamAPI[...]; ... PySpamAPI[...] = (void *)PySpam_System; ... PyCapsule_New((void *)PySpamAPI, ...) ... This is nonportable from a C perspective; C does not promise that it is possible to usefully convert between pointers to functions and pointers to objects. (They can be fundamentally different beasts, especially on Harvard architectures.) It's not clear to me whether Python cares about portability to systems where pointer-to-function and pointer-to-object are different enough for this to matter. But I think it would be good to either fix the example or specifically document that CPython isn't portable to systems where the casting doesn't work. As for fixing the example, if that's the direction chosen - I'd be inclined to do this particular case as static int (*)(const char *)PySpam_API[] = { [PySpam_System_NUM] = &PySpam_System }; ... ... PyCapsule_New(&PySpam_API[0], ...) ... (using &array[0] instead of just array is a personal quirk of mine) and then, in the non-SPAM_MODULE case in spammodule.h, either static void *PySpam_API; #define PySpam_System (((int (*)(const char *))PySpam_API)[PySpam_System_NUM]) or static int (**PySpam_API)(const char *); #define PySpam_System (PySpam_API[PySpam_System_NUM]) The latter is simpler; the former is easier to extend when including functions with other call signatures. (In either case, the cast in import_spam() needs to change or disappear - it's not necessary in any case, because void * is special.) There also is a language mistake: All that a client module must do ... call the function (or rather macro) import_spam() ... Assuming use of the spammodule.h given earlier, import_spam() is a static function (more strictly, a function whose name has internal linkage), not a macro. For it to be a macro it would need to be created with #define, not with a function definition. /~\ The ASCII Mouse \ / Ribbon Campaign X Against HTML mouse@rodents-montreal.org / \ Email! 7D C8 61 52 5D E7 2D 39 4E F1 31 3E E8 B3 27 4B
participants (1)
-
Mouse