Skip to content

Commit 6cbb57f

Browse files
encukouda-woods
andauthored
gh-94731: Revert to C-style casts for _Py_CAST (GH-94782)
Co-authored-by: da-woods <dw-git@d-woods.co.uk>
1 parent 81dca70 commit 6cbb57f

File tree

5 files changed

+85
-59
lines changed

5 files changed

+85
-59
lines changed

Diff for: Include/pyport.h

+3-51
Original file line numberDiff line numberDiff line change
@@ -14,62 +14,14 @@
1414
#endif
1515

1616

17-
// Macro to use C++ static_cast<>, reinterpret_cast<> and const_cast<>
18-
// in the Python C API.
19-
//
20-
// In C++, _Py_CAST(type, expr) converts a constant expression to a
21-
// non constant type using const_cast<type>. For example,
22-
// _Py_CAST(PyObject*, op) can convert a "const PyObject*" to
23-
// "PyObject*".
24-
//
25-
// The type argument must not be a constant type.
17+
// Macro to use C++ static_cast<> in the Python C API.
2618
#ifdef __cplusplus
27-
#include <cstddef>
2819
# define _Py_STATIC_CAST(type, expr) static_cast<type>(expr)
29-
extern "C++" {
30-
namespace {
31-
template <typename type>
32-
inline type _Py_CAST_impl(long int ptr) {
33-
return reinterpret_cast<type>(ptr);
34-
}
35-
template <typename type>
36-
inline type _Py_CAST_impl(int ptr) {
37-
return reinterpret_cast<type>(ptr);
38-
}
39-
#if __cplusplus >= 201103
40-
template <typename type>
41-
inline type _Py_CAST_impl(std::nullptr_t) {
42-
return static_cast<type>(nullptr);
43-
}
44-
#endif
45-
46-
template <typename type, typename expr_type>
47-
inline type _Py_CAST_impl(expr_type *expr) {
48-
return reinterpret_cast<type>(expr);
49-
}
50-
51-
template <typename type, typename expr_type>
52-
inline type _Py_CAST_impl(expr_type const *expr) {
53-
return reinterpret_cast<type>(const_cast<expr_type *>(expr));
54-
}
55-
56-
template <typename type, typename expr_type>
57-
inline type _Py_CAST_impl(expr_type &expr) {
58-
return static_cast<type>(expr);
59-
}
60-
61-
template <typename type, typename expr_type>
62-
inline type _Py_CAST_impl(expr_type const &expr) {
63-
return static_cast<type>(const_cast<expr_type &>(expr));
64-
}
65-
}
66-
}
67-
# define _Py_CAST(type, expr) _Py_CAST_impl<type>(expr)
68-
6920
#else
7021
# define _Py_STATIC_CAST(type, expr) ((type)(expr))
71-
# define _Py_CAST(type, expr) ((type)(expr))
7222
#endif
23+
// Macro to use the more powerful/dangerous C-style cast even in C++.
24+
#define _Py_CAST(type, expr) ((type)(expr))
7325

7426
// Static inline functions should use _Py_NULL rather than using directly NULL
7527
// to prevent C++ compiler warnings. On C++11 and newer, _Py_NULL is defined as

Diff for: Lib/test/_testcppext.cpp

+73-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# define NAME _testcpp03ext
1313
#endif
1414

15+
#define _STR(NAME) #NAME
16+
#define STR(NAME) _STR(NAME)
17+
1518
PyDoc_STRVAR(_testcppext_add_doc,
1619
"add(x, y)\n"
1720
"\n"
@@ -123,11 +126,77 @@ test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
123126
Py_RETURN_NONE;
124127
}
125128

129+
/* Test a `new`-allocated object with a virtual method.
130+
* (https://github.com/python/cpython/issues/94731) */
131+
132+
class VirtualPyObject : public PyObject {
133+
public:
134+
VirtualPyObject();
135+
virtual ~VirtualPyObject() {
136+
delete [] internal_data;
137+
--instance_count;
138+
}
139+
virtual void set_internal_data() {
140+
internal_data[0] = 1;
141+
}
142+
static void dealloc(PyObject* o) {
143+
delete static_cast<VirtualPyObject*>(o);
144+
}
145+
146+
// Number of "living" instances
147+
static int instance_count;
148+
private:
149+
// buffer that can get corrupted
150+
int* internal_data;
151+
};
152+
153+
int VirtualPyObject::instance_count = 0;
154+
155+
PyType_Slot VirtualPyObject_Slots[] = {
156+
{Py_tp_free, (void*)VirtualPyObject::dealloc},
157+
{0, _Py_NULL},
158+
};
159+
160+
PyType_Spec VirtualPyObject_Spec = {
161+
/* .name */ STR(NAME) ".VirtualPyObject",
162+
/* .basicsize */ sizeof(VirtualPyObject),
163+
/* .itemsize */ 0,
164+
/* .flags */ Py_TPFLAGS_DEFAULT,
165+
/* .slots */ VirtualPyObject_Slots,
166+
};
167+
168+
VirtualPyObject::VirtualPyObject() {
169+
// Create a temporary type (just so we don't need to store it)
170+
PyObject *type = PyType_FromSpec(&VirtualPyObject_Spec);
171+
// no good way to signal failure from a C++ constructor, so use assert
172+
// for error handling
173+
assert(type);
174+
assert(PyObject_Init(this, (PyTypeObject *)type));
175+
Py_DECREF(type);
176+
internal_data = new int[50];
177+
++instance_count;
178+
}
179+
180+
static PyObject *
181+
test_virtual_object(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
182+
{
183+
VirtualPyObject* obj = new VirtualPyObject();
184+
obj->set_internal_data();
185+
Py_DECREF(obj);
186+
if (VirtualPyObject::instance_count != 0) {
187+
return PyErr_Format(
188+
PyExc_AssertionError,
189+
"instance_count should be 0, got %d",
190+
VirtualPyObject::instance_count);
191+
}
192+
Py_RETURN_NONE;
193+
}
126194

127195
static PyMethodDef _testcppext_methods[] = {
128196
{"add", _testcppext_add, METH_VARARGS, _testcppext_add_doc},
129197
{"test_api_casts", test_api_casts, METH_NOARGS, _Py_NULL},
130198
{"test_unicode", test_unicode, METH_NOARGS, _Py_NULL},
199+
{"test_virtual_object", test_virtual_object, METH_NOARGS, _Py_NULL},
131200
// Note: _testcppext_exec currently runs all test functions directly.
132201
// When adding a new one, add a call there.
133202

@@ -152,6 +221,10 @@ _testcppext_exec(PyObject *module)
152221
if (!result) return -1;
153222
Py_DECREF(result);
154223

224+
result = PyObject_CallMethod(module, "test_virtual_object", "");
225+
if (!result) return -1;
226+
Py_DECREF(result);
227+
155228
return 0;
156229
}
157230

@@ -163,9 +236,6 @@ static PyModuleDef_Slot _testcppext_slots[] = {
163236

164237
PyDoc_STRVAR(_testcppext_doc, "C++ test extension.");
165238

166-
#define _STR(NAME) #NAME
167-
#define STR(NAME) _STR(NAME)
168-
169239
static struct PyModuleDef _testcppext_module = {
170240
PyModuleDef_HEAD_INIT, // m_base
171241
STR(NAME), // m_name

Diff for: Lib/test/setup_testcppext.py

-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
# a C++ extension using the Python C API does not emit C++ compiler
1818
# warnings
1919
'-Werror',
20-
# Warn on old-style cast (C cast) like: (PyObject*)op
21-
'-Wold-style-cast',
2220
]
2321
else:
2422
# Don't pass any compiler flag to MSVC
@@ -37,9 +35,6 @@ def main():
3735
name = '_testcpp11ext'
3836

3937
cppflags = [*CPPFLAGS, f'-std={std}']
40-
if std == 'c++11':
41-
# Warn when using NULL rather than _Py_NULL in static inline functions
42-
cppflags.append('-Wzero-as-null-pointer-constant')
4338

4439
cpp_ext = Extension(
4540
name,

Diff for: Lib/test/test_cppext.py

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import unittest
66
import subprocess
7+
import sysconfig
78
from test import support
89
from test.support import os_helper
910

@@ -25,6 +26,11 @@ def test_build_cpp03(self):
2526
# With MSVC, the linker fails with: cannot open file 'python311.lib'
2627
# https://github.com/python/cpython/pull/32175#issuecomment-1111175897
2728
@unittest.skipIf(MS_WINDOWS, 'test fails on Windows')
29+
# Building and running an extension in clang sanitizing mode is not
30+
# straightforward
31+
@unittest.skipIf(
32+
'-fsanitize' in (sysconfig.get_config_var('PY_CFLAGS') or ''),
33+
'test does not work with analyzing builds')
2834
# the test uses venv+pip: skip if it's not available
2935
@support.requires_venv_with_pip()
3036
def check_build(self, std_cpp03, extension_name):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Python again uses C-style casts for most casting operations when compiled
2+
with C++. This may trigger compiler warnings, if they are enabled with e.g.
3+
``-Wold-style-cast `` or ``-Wzero-as-null-pointer-constant`` options for ``g++``.

0 commit comments

Comments
 (0)