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.

classification
Title: Functions in Python/traceback.c can take const pointer arguments
Type: enhancement Stage: resolved
Components: Interpreter Core Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: petdance, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2020-02-09 05:44 by petdance, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg361643 - (view) Author: Andy Lester (petdance) * Date: 2020-02-09 05:44
The functions tb_displayline and tb_printinternal can take const pointers on some of their arguments.

tb_displayline(PyObject *f, PyObject *filename, int lineno, const PyObject *name)

tb_printinternal(const PyTracebackObject *tb, PyObject *f, long limit)
msg361649 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-02-09 12:35
They cannot. PyObject cannot be const because the code that uses it can change its reference counter even if it does not change any other fields.
msg361655 - (view) Author: Andy Lester (petdance) * Date: 2020-02-09 15:54
I'm sorry, I think my comment was misleading.

The changes I had proposed were not making the object itself const, but some of the arguments in the static worker functions.  For example:

-tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
+tb_displayline(PyObject *f, PyObject *filename, int lineno, const PyObject *name)

and

-tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
+tb_printinternal(const PyTracebackObject *tb, PyObject *f, long limit)

I've got -Wincompatible-pointer-types-discards-qualifiers and -Wcast-qual turned on, and no errors occur.

Is there somewhere in the deep internals of the Python macros where constness can be changed but the compiler isn't reporting on it?

Thanks,
Andy
msg361658 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-02-09 17:19
Yes, Py_INCREF and Py_DECREF change the type, and therefore constness.
msg361699 - (view) Author: Andy Lester (petdance) * Date: 2020-02-10 15:18
> Yes, Py_INCREF and Py_DECREF change the type, and therefore constness.

Understood. The changes that I have proposed are not to objects that get sent through Py_INCREF/Py_DECREF.  If they did, -Wcast-qual would have caught it.  -Wcast-qual catches if you cast, say, a const char * to a char *.

Let's let this stay closed and I'll resubmit with a clearer ticket & PR.
msg361793 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-02-11 10:10
Even if the object is not modified currently in common case, it does not guarantee that it cannot be modified in uncommon cases, or that it cannot be modified in future after introducing changes in different files.

For example, if names was created by the legacy C API like PyUnicode_FromUnicode(), it will be modified by calling PyUnicode_READY().
History
Date User Action Args
2022-04-11 14:59:26adminsetgithub: 83772
2020-02-11 10:10:27serhiy.storchakasetmessages: + msg361793
2020-02-10 15:18:03petdancesetmessages: + msg361699
2020-02-09 17:19:32serhiy.storchakasetmessages: + msg361658
2020-02-09 15:54:03petdancesetmessages: + msg361655
2020-02-09 15:16:38petdancesetpull_requests: - pull_request17798
2020-02-09 12:35:22serhiy.storchakasetstatus: open -> closed
resolution: rejected
stage: patch review -> resolved
2020-02-09 12:35:04serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg361649
2020-02-09 05:51:15petdancesetpull_requests: - pull_request17794
2020-02-09 05:50:50petdancesetpull_requests: + pull_request17798
2020-02-09 05:47:05petdancesetkeywords: + patch
stage: patch review
pull_requests: + pull_request17794
2020-02-09 05:44:35petdancecreate