gh-144175: Add PyArg_ParseVector() function#144283
gh-144175: Add PyArg_ParseVector() function#144283vstinner wants to merge 5 commits intopython:mainfrom
Conversation
|
The change doesn't include tests because my plan is to write a following PR to rename |
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
|
Ok, I completed the PR to add also |
|
I propose the following API: int PyArg_ParseVector(
PyObject *const *args,
Py_ssize_t nargs,
const char *format,
...);
int PyArg_ParseVectorAndKeywords(
PyObject *const *args,
Py_ssize_t nargs,
PyObject *kwnames,
const char *format,
char * const *kwlist,
...); |
| Py_ssize_t nargs, | ||
| PyObject *kwnames, | ||
| const char *format, | ||
| PY_CXX_CONST char * const *kwlist, |
There was a problem hiding this comment.
Any reason to have PY_CXX_CONST? why now always const char *const *kwlist? (are there occurrences where we don't have a const?)
There was a problem hiding this comment.
If kwlist is declared as const char * const *kwlist, I get a compiler error on the following code:
int arg, arg2 = 0;
char *kwlist[] = {"arg", "arg2", NULL};
if (!PyArg_ParseVectorAndKeywords(args, nargs, kwnames, "i|i", kwlist,
&arg, &arg2)) {
return NULL;
}Error:
./Modules/_testcapimodule.c: In function 'fastcall_kwnames':
./Modules/_testcapimodule.c:2603:68: error: passing argument 5 of 'PyArg_ParseVectorAndKeywords' from incompatible pointer type [-Wincompatible-pointer-types]
2603 | if (!PyArg_ParseVectorAndKeywords(args, nargs, kwnames, "i|i", kwlist,
| ^~~~~~
| |
| char **
In file included from ./Include/modsupport.h:153,
from ./Include/Python.h:132,
from ./Modules/_testcapi/parts.h:25,
from ./Modules/_testcapimodule.c:13:
./Include/cpython/modsupport.h:15:25: note: expected 'const char * const*' but argument is of type 'char **'
15 | const char * const *kwlist,
| ~~~~~~~~~~~~~~~~~~~~^~~~~~
PY_CXX_CONST is also used for kwlist by PyArg_ParseTupleAndKeywords() for example.
PY_CXX_CONST is only defined as const if building C++.
There was a problem hiding this comment.
This error can be avoided by writing (godbolt example)
const char *kwlist[] = {"arg", "arg2", NULL};
C++ even warns on using string constants without const using default compiler warning settings (godbolt example)
// warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
char *kwlist[] = {"arg1", NULL};
char *arg = "arg";
Since PyArg_ParseVectorAndKeywords() is new it is maybe ok to "drive" people into using the const pattern even in C, which is cleaner IMHO?
|
I created a C API Working Group decision issue: capi-workgroup/decisions#97. |
📚 Documentation preview 📚: https://cpython-previews--144283.org.readthedocs.build/