Title: Add the const qualifier for PyObject* array arguments
Type: enhancement Stage: resolved
Components: Versions: Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: scoder, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2017-12-07 11:21 by serhiy.storchaka, last changed 2017-12-15 11:15 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 4746 merged serhiy.storchaka, 2017-12-07 11:23
Messages (7)
msg307795 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-12-07 11:21
The proposed PR replaces argument declarations "PyObject **args" with "PyObject * const *args". In many cases this can be a pointer to the internal array of the tuple object. It is not safe to change it.

PyEval_EvalCodeEx() is the only public function affected by this change, but this change is backward compatible. All other affected code is a private C API.
msg307796 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-12-07 11:31
I hate this "obscure" C syntax "PyObject * const *args".

Technically, is it possible to define it as a type with a better name to give more context where the type would be defined?

For example, "PyConstObjectArray"?
msg307798 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-12-07 12:17
I don't like "PyConstObjectArray" or any other name. I will just obfuscate the C code. "PyObject * const *args" should be clear for every C programmer, but if I see "PyConstObjectArray" I need to search the definition of it in the header files. And it is easy to make a mistake by using "PyConstObjectArray *" instead of "PyConstObjectArray" since complex types usually are passed by pointer in C.

If you prefer, "PyObject * const *args" can be written as "PyObject * const args[]". This is an identical syntax, but the latter form is used more rarely.
msg307799 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-12-07 12:24
This change is inspired by reviewing one PR in which an input array of PyObject* was modified inplace. Even if it was correct in that particular  case, it looked unsafe (actually that code was wrong for other causes). Adding the const qualifier allows to distinguish input PyObject* array arguments from pointers to output PyObject* arguments.
msg308076 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-12-11 22:07
This can affect Cython which uses the "fast call" convention.
msg308372 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2017-12-15 10:07
Seems ok from Cython side. Could be a bit difficult to make an actual use of this, but at least it shouldn't break anything. And it's a reasonable constraint to enforce.
msg308381 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-12-15 11:11
New changeset a5552f023e1d8cbafee1e51d316cc581deb2295f by Serhiy Storchaka in branch 'master':
bpo-32240: Add the const qualifier to declarations of PyObject* array arguments. (#4746)
Date User Action Args
2017-12-15 11:15:02serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-12-15 11:11:14serhiy.storchakasetmessages: + msg308381
2017-12-15 10:07:08scodersetmessages: + msg308372
2017-12-11 22:07:06serhiy.storchakasetnosy: + scoder
messages: + msg308076
2017-12-07 12:24:50serhiy.storchakasetmessages: + msg307799
2017-12-07 12:17:15serhiy.storchakasetmessages: + msg307798
2017-12-07 11:31:29vstinnersetmessages: + msg307796
2017-12-07 11:23:50serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request4649
2017-12-07 11:21:41serhiy.storchakacreate