API : constness ?

Benoît Dejean bnet at ifrance.com
Sun May 30 21:26:53 EDT 2004


Le Sun, 30 May 2004 08:31:36 -0400, Tim Peters a écrit :

> [Benoît Dejean]
>> i don't understand why the ANSI const keyword is never used in the API.
> 
> It is used in the API, and more than once, but it isn't used as often as it
> could be used.  It's more likely to appear in newer API functions.  When
> Python was first written, most compilers were K&R, and Python avoided ANSI
> features until about a decade later (a C89 compiler is required now).  A lot
> of the C API survived unchaged across all that time.  There's no objection
> to slamming in consts now, but nobody has cared enough to bother doing so.

it's also for a simple reason, eg using PyArg_ParseTupleAndKeywords

using const helps gcc to move these data to the text segment

static const char * const kwlist[] = { "uri", "open_mode", "exclusive",
"perm", NULL };

or a better (but tricky)  storage could be used to avoid relocation

bust casting again again like this

PyArg_ParseTupleAndKeywords(args, kwargs, "should be const char *",
                                     (char**)kwlist, ...

is quite ugly ...



More information about the Python-list mailing list