-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replacing char* with const char* #45300
Comments
Many functions use char* where const char* should be used. I've changed this in a few function prototypes which I use while embedding python due to numerous warnings about deprecated casting from string constants to char*. |
I was about to submit the very same patch :-) Please look into this matter. GCC 4.2 has arrived and it has a new char* kwlist[] = { "target", "encoding", NULL }; now gives a warning like: "warning: deprecated conversion from string constant to ‘char*’" at least when compiling in C++ mode... This means that people have to declare it as "const char* kwlist", which PyAPI_FUNC(int) PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
const char *, char **,
...); The "char **" should be "const char **". ext-, any chance you could fix |
There was once a rather long discussion about "const" in If you don't read it to the end, here is the conclusion: "const char*" was accepted without opposition, though. |
Douglas Greiman submitted many similar changes with his bpo-2135 patch. I am adding dgreiman to the nosy list here to avoid duplication of I am -0 on the idea. |
For external APIs visible to user code, this can cause some compatibility problems and users may need to at the very least re-compile this code. So -1 here. For new external APIs, having const wherever appropriate should be considered on a case by case basis. For internal code this is a good idea, but I wouldn't go mass-slashing all the code base now replacing char* by const char*. Presenting some examples where this makes sense in particular would be interesting though. +0 |
Can you explain exactly which compatibility problems this would cause? Actually, the point is precisely to make life easier for third-party code, since "const char *" is more accepting than "char *". |
Antoine, I was referring to the discussion linked earlier (http://mail.python.org/pipermail/python-dev/2006-February/060689.html). Users of the C API needed to recompile their code and also add preprocessor hacks to make things compatible with C and C++, AFAIU. |
Ok, I've read it through. The problem is specifically with And indeed, PyArg_ParseTupleAndKeywords() got its "const char **" So it looks like the patch, in essence, is legit. |
In C++ we may overload functions for backward compatibility: PyAPI_FUNC(int) PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
const char *, const char * const *, ...);
PyAPI_FUNC(int) PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
const char *, const char * const *, va_list va);
...
#ifdef __cplusplus
inline int
PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *keywords,
const char *format, char * const *kwlist, ...)
{
int retval;
va_list va;
va_start(va, kwlist);
retval = PyArg_ParseTupleAndKeywords(args, keywords, format,
(const char * const *)kwlist, va_start);
va_end(va);
return retval;
}
#endif |
See also bpo-9369. |
The patch is outdated. Some function prototypes were changed in bpo-9369, some were changed before. Here is a new patch. It adds const qualifier to following public functions: |
The patch synchronized with tip. |
New changeset 3055e61f1e66 by Serhiy Storchaka in branch 'default': |
Compile errors on OS X 10.8 with clang: cc -Wno-unused-result -Werror=declaration-after-statement -g -O0 -Wall -Wstrict-prototypes -I. -IInclude -I../../source/Include -DPy_BUILD_CORE -c ../../source/Modules/posixmodule.c -o Modules/posixmodule.o
In file included from ../../source/Modules/posixmodule.c:5959:
/usr/include/util.h:94:5: error: conflicting types for 'openpty'
int openpty(int *, int *, char *, struct termios *,
^
../../source/Include/pyport.h:676:12: note: previous declaration is here
extern int openpty(int *, int *, char *,
^
In file included from ../../source/Modules/posixmodule.c:5959:
/usr/include/util.h:97:7: error: conflicting types for 'forkpty'
pid_t forkpty(int *, char *, struct termios *, struct winsize *);
^
../../source/Include/pyport.h:678:14: note: previous declaration is here
extern pid_t forkpty(int *, char *,
^
2 errors generated.
make: *** [Modules/posixmodule.o] Error 1 |
New changeset 8bd69bd6e129 by Serhiy Storchaka in branch 'default': |
New changeset 599a957038fa by Serhiy Storchaka in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: