Skip to content
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

bpo-36876: Make some static string literal arrays constant. #15760

Closed

Conversation

ericsnowcurrently
Copy link
Member

@ericsnowcurrently ericsnowcurrently commented Sep 9, 2019

This gives us guarantees about immutability.

https://bugs.python.org/issue36876

Copy link
Member

@pganssle pganssle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, a few formatting nits around whitespace I found.

Modules/_datetimemodule.c Outdated Show resolved Hide resolved
Modules/_decimal/_decimal.c Outdated Show resolved Hide resolved
Modules/_decimal/_decimal.c Outdated Show resolved Hide resolved
@@ -3735,7 +3735,7 @@ dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
static PyObject * \
dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
{ \
static char *kwlist[] = {"other", "context", NULL}; \
static const char *kwlist[] = {"other", "context", NULL}; \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few other alignment issues like the ones above - probably best to align the newline escapes as above.

@bedevere-bot
Copy link

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

Copy link
Member

@pganssle pganssle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are all the alignment issues I found. Hopefully the "suggestions" are the right number of spaces.

@@ -1278,7 +1278,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
HANDLE fh = 0;
int access = (access_mode)ACCESS_DEFAULT;
DWORD flProtect, dwDesiredAccess;
static char *keywords[] = { "fileno", "length",
static const char *keywords[] = { "fileno", "length",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next two lines are also misaligned.

@@ -8976,7 +8976,7 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
struct sf_hdtr sf;
int flags = 0;
/* Beware that "in" clashes with Python's own "in" operator keyword */
static char *keywords[] = {"out", "in",
static const char *keywords[] = {"out", "in",
"offset", "count",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alignment here again.

@@ -9092,7 +9092,7 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
#else
Py_ssize_t count;
PyObject *offobj;
static char *keywords[] = {"out", "in",
static const char *keywords[] = {"out", "in",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alignment again.

Modules/selectmodule.c Outdated Show resolved Hide resolved
@@ -1024,7 +1024,7 @@ get_source_line(PyObject *module_globals, int lineno)
static PyObject *
warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *kwd_list[] = {"message", "category", "filename", "lineno",
static const char *kwd_list[] = {"message", "category", "filename", "lineno",
"module", "registry", "module_globals",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alignment here again.

@serhiy-storchaka
Copy link
Member

Changing char *kwlist[] to const char *kwlist[] is incompatible change, because PyArg_ParseTupleAndKeywords() takes char ** which is not compatible with const char **. The compiler should raise warnings or even errors about this. If not this this change would be made years ago.

It is possible to change the API, but it is long a complex process. It is a separate issue.

Adding consts in Python/Python-ast.c may be good, but this file is generated. You should modify the generating script.

@serhiy-storchaka
Copy link
Member

Note also that you can add two consts in static char *something[]: static const char * const something[]. One for C-strings, and other for an array.

See #15824 for changes in Python/Python-ast.c.

Co-Authored-By: Paul Ganssle <p.ganssle@gmail.com>
Co-Authored-By: Paul Ganssle <p.ganssle@gmail.com>
Co-Authored-By: Paul Ganssle <p.ganssle@gmail.com>
Co-Authored-By: Paul Ganssle <p.ganssle@gmail.com>
Co-Authored-By: Paul Ganssle <p.ganssle@gmail.com>
@ericsnowcurrently
Copy link
Member Author

Hmm, this won't work out.

@georgthegreat
Copy link
Contributor

At the time there is a bunch of forgotten const / const_cast / (char**) c-style casts everywhere in PyArg_ParseTupleAndKeywords invocations.

The list includes the following pip packages (but definitely not limited to):

Is there any way to continue working on the issue?

@georgthegreat
Copy link
Contributor

@serhiy-storchaka, could you, please, elaborate on

It is possible to change the API, but it is long a complex process. It is a separate issue.)

At the time the lack of const causes problems i. e. when compiling in MSVC standard conformance mode (which is default for /std:c++latest / /std:c++20).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants