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

Created on 2017-12-07 11:21 by serhiy.storchaka, last changed 2017-12-07 12:24 by serhiy.storchaka.

Pull Requests
URL Status Linked Edit
PR 4746 open serhiy.storchaka, 2017-12-07 11:23
Messages (4)
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.
Date User Action Args
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