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: Constness in _PyErr_BadInternalCall
Type: compile error Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: jdemeyer, nnorwitz, python-dev, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2016-03-03 10:25 by jdemeyer, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
PyErr_BadInternalCall_const.patch jdemeyer, 2016-03-03 10:25
Messages (7)
msg261156 - (view) Author: Jeroen Demeyer (jdemeyer) * (Python triager) Date: 2016-03-03 10:25
PyErr_BadInternalCall() calls _PyErr_BadInternalCall(__FILE__, __LINE__). Since __FILE__ is a string constant, the first argument of _PyErr_BadInternalCall should be a "const char*" instead of a "char*".

This is a follow-up to #4949. Most of the patch from #4949 was applied, but not the change to _PyErr_BadInternalCall on Python 2.
msg261159 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-03-03 11:21
Was already fixed in 84618b2064c1. It is questionable wherever it should be backported to 2.7. Most other "const" additions were applied only to the default branch (see issue25923, issue24436, issue1772673, issue9369, issue16369, issue12173, issue1419652 and many others).
msg261160 - (view) Author: Jeroen Demeyer (jdemeyer) * (Python triager) Date: 2016-03-03 11:25
> It is questionable wherever it should be backported to 2.7.

It violates the C++ standard (for extension modules written in C++), so it's clearly a bug.
msg261161 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-03-03 11:59
CPython is written on C and provides C API.

Even if change the signature of one function, this will not help much, because a lot of other functions require "char *" instead of "const char *".

There is small disadvantage of changing the signature in a bugfix release. This will lead to incompatibility of your extension with older bugfix releases.
msg261164 - (view) Author: Jeroen Demeyer (jdemeyer) * (Python triager) Date: 2016-03-03 12:48
> CPython is written on C and provides C API.

If you look at the title of https://docs.python.org/2/extending/extending.html clearly C++ extensions are also supported.

> Even if change the signature of one function, this will not help much, because a lot of other functions require "char *" instead of "const char *".

I don't know which functions you mean, I only encountered this problem for PyErr_BadInternalCall().

I should also add that the problem cannot be avoided or worked around for PyErr_BadInternalCall() because it's a CPython header itself which does an illegal conversion of a string constant to char*.

> This will lead to incompatibility of your extension with older bugfix releases.

I don't see why there would be ABI incompatibility. My patch is only adding "const" in the call of a C function.
msg261169 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-03-03 16:11
OK, now I understand. Since _PyErr_BadInternalCall() is private function, we will not break any code. Since it is used only by the PyErr_BadInternalCall() macro and always called with string literal as an argument, C++ user can't use PyErr_BadInternalCall() (without a trick described below). Thus the patch fixes a bug. Thank you Jeroen.

The workaround for unpatched Python 2.7 is to undefine the PyErr_BadInternalCall() macro.

    #undef PyErr_BadInternalCall

There is the PyErr_BadInternalCall() function hidden by the macro. It provides less detailed error message, without file name and line number.
msg261170 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-03-03 16:17
New changeset 0e3526ab6a9b by Serhiy Storchaka in branch '2.7':
Issue #26476: Fixed compilation error when use PyErr_BadInternalCall() in C++.
https://hg.python.org/cpython/rev/0e3526ab6a9b
History
Date User Action Args
2022-04-11 14:58:28adminsetgithub: 70663
2016-03-03 16:18:14serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: commit review -> resolved
2016-03-03 16:17:19python-devsetnosy: + python-dev
messages: + msg261170
2016-03-03 16:11:42serhiy.storchakasetassignee: serhiy.storchaka
type: compile error
messages: + msg261169
stage: commit review
2016-03-03 12:48:26jdemeyersetmessages: + msg261164
2016-03-03 11:59:02serhiy.storchakasetmessages: + msg261161
2016-03-03 11:25:37jdemeyersetmessages: + msg261160
2016-03-03 11:21:00serhiy.storchakasetnosy: + serhiy.storchaka, nnorwitz
messages: + msg261159
2016-03-03 10:25:31jdemeyercreate