[Python-Dev] Baffled by PyArg_ParseTupleAndKeywords modification

M.-A. Lemburg mal at egenix.com
Mon Feb 13 23:18:23 CET 2006

Tim Peters wrote:
> [Jeremy]
>>>> I added some const to several API functions that take char* but
>>>> typically called by passing string literals.
> [Tim]
>>> If he had _stuck_ to that, we wouldn't be having this discussion :-)
>>> (that is, nobody passes string literals to
>>> PyArg_ParseTupleAndKeywords's kws argument).
> [Jeremy]
>> They are passing arrays of string literals.  In my mind, that was a
>> nearly equivalent use case.  I believe the C++ compiler complains
>> about passing an array of string literals to char**.
> It's the consequences:  nobody complains about tacking "const" on to a
> former honest-to-God "char *" argument that was in fact not modified,
> because that's not only helpful for C++ programmers, it's _harmless_
> for all programmers.  For example, nobody could sanely object (and
> nobody did :-)) to adding const to the attribute-name argument in
> PyObject_SetAttrString().  Sticking to that creates no new problems
> for anyone, so that's as far as I ever went.

Well, it broke my C extensions... I now have this in my code:

/* The keyword array changed to const char* in Python 2.5 */
#if PY_VERSION_HEX >= 0x02050000
# define Py_KEYWORDS_STRING_TYPE const char
static Py_KEYWORDS_STRING_TYPE *kwslist[] = {"yada", NULL};
if (!PyArg_ParseTupleAndKeywords(args,kws,format,kwslist,&a1))
    goto onError;

The crux is that code which should be portable across Python
versions won't work otherwise: you either get Python 2.5 xor
Python 2.x (for x < 5) compatibility.

Not too happy about it, but then compared to the ssize_t
changes and the relative imports PEP, this one is an easy
one to handle.

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Feb 13 2006)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::

More information about the Python-Dev mailing list