[Python-Dev] Const-correctness in C-API Object Protocol

Antoine Pitrou solipsis at pitrou.net
Tue Feb 22 20:25:44 CET 2011


On Tue, 22 Feb 2011 11:06:41 -0800
Guido van Rossum <guido at python.org> wrote:
> >
> > Probably because (a) the person who first wrote them used char* instead of
> > const char*, and (b) it gives us API flexibility by not promising to not
> > alter the char array at some point in the future.
> 
> I'm sorry, but (b) does not make sense. These APIs typically get
> passed string literals and modifying the argument would be a bad idea.

+1. If we started mutating strings passed to such APIs, it would
certainly break a lot of third-party code.

> FWIW, in Python 3.2, PySys_GetObject() is actually using const char *.
> I don't see a problem with changing PyObject_CallMethod() and its
> variations as well.
> 
> Though I do not get that warning -- which compiler and version issues
> it? Is it a C or a C++ compiler?

Well, which warning are you talking about?

I don't think changing a "char *" to a "const char *" specification
would be harmful, since it makes the API more tolerant: with the
latter, you can pass it both const and non-const strings.

Regards

Antoine.




More information about the Python-Dev mailing list