Skip to content

Commit 8ea75ab

Browse files
EthanSteinbergpre-commit-ci[bot]Skylion007
authored
Fix casts to void* (#4275)
* Fix casts to void* * Improve tests * style: pre-commit fixes * remove c style cast Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Aaron Gokaslan <[email protected]>
1 parent 17c1e27 commit 8ea75ab

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

include/pybind11/cast.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -1179,11 +1179,9 @@ enable_if_t<cast_is_temporary_value_reference<T>::value, T> cast_safe(object &&)
11791179
pybind11_fail("Internal error: cast_safe fallback invoked");
11801180
}
11811181
template <typename T>
1182-
enable_if_t<std::is_same<void, intrinsic_t<T>>::value, void> cast_safe(object &&) {}
1182+
enable_if_t<std::is_void<T>::value, void> cast_safe(object &&) {}
11831183
template <typename T>
1184-
enable_if_t<detail::none_of<cast_is_temporary_value_reference<T>,
1185-
std::is_same<void, intrinsic_t<T>>>::value,
1186-
T>
1184+
enable_if_t<detail::none_of<cast_is_temporary_value_reference<T>, std::is_void<T>>::value, T>
11871185
cast_safe(object &&o) {
11881186
return pybind11::cast<T>(std::move(o));
11891187
}

tests/test_class.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ TEST_SUBMODULE(class_, m) {
384384

385385
protected:
386386
virtual int foo() const { return value; }
387+
virtual void *void_foo() { return static_cast<void *>(&value); }
388+
virtual void *get_self() { return static_cast<void *>(this); }
387389

388390
private:
389391
int value = 42;
@@ -392,6 +394,8 @@ TEST_SUBMODULE(class_, m) {
392394
class TrampolineB : public ProtectedB {
393395
public:
394396
int foo() const override { PYBIND11_OVERRIDE(int, ProtectedB, foo, ); }
397+
void *void_foo() override { PYBIND11_OVERRIDE(void *, ProtectedB, void_foo, ); }
398+
void *get_self() override { PYBIND11_OVERRIDE(void *, ProtectedB, get_self, ); }
395399
};
396400

397401
class PublicistB : public ProtectedB {
@@ -401,11 +405,23 @@ TEST_SUBMODULE(class_, m) {
401405
// (in Debug builds only, tested with icpc (ICC) 2021.1 Beta 20200827)
402406
~PublicistB() override{}; // NOLINT(modernize-use-equals-default)
403407
using ProtectedB::foo;
408+
using ProtectedB::get_self;
409+
using ProtectedB::void_foo;
404410
};
405411

412+
m.def("read_foo", [](const void *original) {
413+
const int *ptr = reinterpret_cast<const int *>(original);
414+
return *ptr;
415+
});
416+
417+
m.def("pointers_equal",
418+
[](const void *original, const void *comparison) { return original == comparison; });
419+
406420
py::class_<ProtectedB, TrampolineB>(m, "ProtectedB")
407421
.def(py::init<>())
408-
.def("foo", &PublicistB::foo);
422+
.def("foo", &PublicistB::foo)
423+
.def("void_foo", &PublicistB::void_foo)
424+
.def("get_self", &PublicistB::get_self);
409425

410426
// test_brace_initialization
411427
struct BraceInitialization {

tests/test_class.py

+2
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ def test_bind_protected_functions():
313313

314314
b = m.ProtectedB()
315315
assert b.foo() == 42
316+
assert m.read_foo(b.void_foo()) == 42
317+
assert m.pointers_equal(b.get_self(), b)
316318

317319
class C(m.ProtectedB):
318320
def __init__(self):

0 commit comments

Comments
 (0)