This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author petdance
Recipients corona10, nascheme, petdance, serhiy.storchaka, shihai1991, steve.dower, vstinner
Date 2020-02-18.02:21:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1581992479.99.0.293256342656.issue39573@roundup.psfhosted.org>
In-reply-to
Content
> Would you mind to explain how it's an issue to modify PyObject* temporarily during a function call?

It's not a problem to modify the PyObject* during a function call.  However, many functions don't need to modify the object, but are still taking non-const PyObject* arguments.

For example if I have this code:

    if (Py_TYPE(deque) == &deque_type) {

That doesn't modify deque to check the type, but because Py_TYPE casts away the constness, deque can't be a const object.

However, with the new Py_IS_TYPE function:

    if (Py_IS_TYPE(deque, &deque_type)) {

and these two changes:

-static inline int _Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
+static inline int _Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
     return ob->ob_type == type;
 }
-#define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST(ob), type)
+#define Py_IS_TYPE(ob, type) _Py_IS_TYPE(((const PyObject*)(ob)), type)

the deque variable can be const.

Another example of a common pattern that I believe could benefit from this is Py_TYPE(ob)->tp_name.  That could be turned into Py_TYPE_NAME(ob) and that would allow the ob to be a const pointer. 

If we can keep functions that don't modify the object to accept const PyObject* it will help make things safer in the long run.
History
Date User Action Args
2020-02-18 02:21:20petdancesetrecipients: + petdance, nascheme, vstinner, serhiy.storchaka, steve.dower, corona10, shihai1991
2020-02-18 02:21:19petdancesetmessageid: <1581992479.99.0.293256342656.issue39573@roundup.psfhosted.org>
2020-02-18 02:21:19petdancelinkissue39573 messages
2020-02-18 02:21:19petdancecreate