Add const to python API - issue 6952
http://bugs.python.org/issue6952 Martin v. Löwis suggested that solutions to this issue should be discussed here. My goal is to avoid compiler warning and the need to cast to remove const when calling the python API. For example I see compiler messages like this on Mac OS X Snow Leopard g++ reports: example.cxx:633: warning: deprecated conversion from string constant to ‘char*’ The patch I developed for comment only adds const to the input parameters and used casts to allow output parameters to stay without the const. This is because adding the const to the input params will not break existing code, but adding const to output parameters may well require code changes for users of the Python API. What is the best approach to this problem that will be acceptable? Barry
On 20 Oct 2009, at 04:35, Martin v. Löwis wrote:
The patch I developed for comment only adds const to the input parameters and used casts to allow output parameters to stay without the const.
What specific APIs are you talking about here?
Checking my patch I have two functions that need to have output params changed to const to avoid casting. PyOS_strtoul - ptr PyLong_FromString - pend Barry
On Tue, Oct 20, 2009 at 4:03 PM, Barry Scott
Checking my patch I have two functions that need to have output params changed to const to avoid casting.
PyOS_strtoul - ptr PyLong_FromString - pend
This is a no-win situation. If the string is const in the caller, they currently need to cast it. If you make the change, then if string is not const in the caller then they will need to cast it. I've provided a short example below and marked the lines that generate "an incompatible pointer type" warning with gcc. I suggest following POSIX's lead and omitted the const in these cases. void test_const(const char **foo); void test_noconst(char **foo); int main(void) { char *noconst_var; const char *const_var; test_const(&noconst_var); // generates a warning test_const(&const_var); test_noconst(&noconst_var); test_noconst(&const_var); // generates a warning return 0; } -- Daniel Stutzbach, Ph.D. President, Stutzbach Enterprises, LLC http://stutzbachenterprises.com
On 21 Oct 2009, at 06:15, Martin v. Löwis wrote:
I suggest following POSIX's lead and omitted the const in these cases.
Ah... Yes I see this in strstr for example.
Thanks, that sounds reasonable.
This is basically what I have done in the patch I hope. I have updated the patch to include a comment explaining why output param is char ** and the need for a cast. Barry
On 22 Oct 2009, at 20:31, Barry Scott wrote:
On 21 Oct 2009, at 06:15, Martin v. Löwis wrote:
I suggest following POSIX's lead and omitted the const in these cases.
Ah... Yes I see this in strstr for example.
Thanks, that sounds reasonable.
This is basically what I have done in the patch I hope. I have updated the patch to include a comment explaining why output param is char ** and the need for a cast.
Have I address the issues sufficiently for the patch to be accepted? Should I go ahead a create the python 3 version of the patch? Barry
participants (3)
-
"Martin v. Löwis"
-
Barry Scott
-
Daniel Stutzbach