Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

char* arguments in C API

1 view
Skip to first unread message

Julian Rockey

unread,
Mar 23, 2003, 11:41:11 AM3/23/03
to
I looked around for a list dedicated to the Python/C API, and couldn't find
one, so I hope this is an appropriate place to ask this question.

The Python API declares most of its functions that take a C string using
char* (as opposed to const char*). This results in a warning in stricter
compilers if you do something like

PyObject *spam_module = PyImport_ImportModule("spam");

To stop the warning I have to cast to char*:
PyObject *spam_module = PyImport_ImportModule((char*)"spam");

Is there an alternative to this slightly ugly hack? Shouldn't parameters
like these be const char*? Or is it just an historical thing?
cheers
Julian

"Martin v. Löwis"

unread,
Mar 23, 2003, 12:56:10 PM3/23/03
to
> The Python API declares most of its functions that take a C string using
> char* (as opposed to const char*). This results in a warning in stricter
> compilers if you do something like

Is there any chance that the "slightly stricter compiler" is a C++
compiler, as opposed to being a C compiler? If not, which compiler
is it, and what precisely is its message?

> Is there an alternative to this slightly ugly hack?

I suggest using a C compiler if you need an alternative now.

> Shouldn't parameters like these be const char*?
> Or is it just an historical thing?

It's mostly historical, and also irrelevant for C. For C++ users
of the Python API, a number of these functions have been changed
to const char*. Please understand that changing a single function
in that way will cause dozens of changes if done properly, as now
the parameter will be const char*, so the function will fail to
compile if it passes the parameter to functions expecting char*.

With that in mind, feel free to submit patches changing the API.
It is fine to change as much as needed, as long as existing code
won't be broken. You should start, in any case, with the Python
CVS, since it already has const char* in a few more places.

Regards,
Martin


Julian Rockey

unread,
Mar 23, 2003, 1:45:06 PM3/23/03
to
"Martin v. Löwis" wrote:

>> The Python API declares most of its functions that take a C string using
>> char* (as opposed to const char*). This results in a warning in stricter
>> compilers if you do something like
>
> Is there any chance that the "slightly stricter compiler" is a C++
> compiler, as opposed to being a C compiler? If not, which compiler
> is it, and what precisely is its message?

Ah yes. You caught me out. I actually started off with C++, and I'm afraid
the lines between C and C++ are a little blurry for me (I unfortunately -
and, I know, very wrongly - tend to consider C as being "C++ without
classes".. :-) )

>
>> Is there an alternative to this slightly ugly hack?
>
> I suggest using a C compiler if you need an alternative now.

The module is C++ - so I guess I'll cast to char* explicitly for now.


>
>> Shouldn't parameters like these be const char*?
> > Or is it just an historical thing?
>
> It's mostly historical, and also irrelevant for C. For C++ users
> of the Python API, a number of these functions have been changed
> to const char*. Please understand that changing a single function
> in that way will cause dozens of changes if done properly, as now
> the parameter will be const char*, so the function will fail to
> compile if it passes the parameter to functions expecting char*.

I understand that completely - not a trivial job, and I appreciate it's
really just for tidyness for C++ - no tangible benefit.

>
> With that in mind, feel free to submit patches changing the API.
> It is fine to change as much as needed, as long as existing code
> won't be broken. You should start, in any case, with the Python
> CVS, since it already has const char* in a few more places.

I read from this that the direction is to change to const char* as and when
possible. In the meantime I'll cast. And if I get really sick of it I'll
try creating some patches :-)

Thanks very much for the response. I didn't intend my query to be a
complaint - more of a "I am missing something here?" Which I was.

>
> Regards,
> Martin
cheers
Julian

Erik Max Francis

unread,
Mar 23, 2003, 6:12:59 PM3/23/03
to
"Martin v. Löwis" wrote:

> Is there any chance that the "slightly stricter compiler" is a C++
> compiler, as opposed to being a C compiler? If not, which compiler
> is it, and what precisely is its message?

Even in C++, a special case for silent conversions of string literals to
char * is provided. The code should compile fine on both C and C++
compilers.

--
Erik Max Francis / m...@alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ Wretches hang that jurymen may dine.
\__/ Alexander Pope
Official Omega page / http://www.alcyone.com/max/projects/omega/
The official distribution page for the popular Roguelike, Omega.

Martin v. Löwis

unread,
Mar 24, 2003, 4:24:56 AM3/24/03
to
> Even in C++, a special case for silent conversions of string literals to
> char * is provided. The code should compile fine on both C and C++
> compilers.

While this conversion is well-formed, it is also deprecated. Atleast the
SunPro compiler warns if the conversion is used (and "all" warnings are
activated).

Regards,
Martin


Julian Rockey

unread,
Mar 26, 2003, 12:37:14 PM3/26/03
to
Martin v. Löwis wrote:

So does gcc3.2

>
> Regards,
> Martin

0 new messages