Moving several tests to github.com/rwgk/rwgk_tbx/tree/main/pybind11_tests

a2c2f88174

These tests are from experimenting, and for demonstrating UB in pybind11 multiple inheritance handling ("first_base"), to be fixed later.
This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-01-24 10:11:32 -08:00
parent 2d463aa3b6
commit 23dc9b173c
12 changed files with 0 additions and 700 deletions

View File

@ -1,57 +0,0 @@
// pybind11 equivalent of Boost.Python test:
// https://github.com/rwgk/rwgk_tbx/blob/6c9a6d6bc72d5c1b8609724433259c5b47178680/cpp_base_py_derived_ext.cpp
// See also: https://github.com/pybind/pybind11/issues/1333 (this was the starting point)
#include "pybind11_tests.h"
namespace pybind11_tests {
namespace cpp_base_py_derived {
struct base {
base() : base_num(100) {}
virtual int get_num() const { return base_num; }
virtual std::shared_ptr<base> clone() const {
return std::shared_ptr<base>(new base(150));
}
virtual ~base() = default;
private:
explicit base(int num) : base_num(num) {}
int base_num;
};
inline int get_num(std::shared_ptr<base> b) { return b->get_num(); }
inline int clone_get_num(std::shared_ptr<base> b) {
std::shared_ptr<base> c = b->clone();
return (b->get_num() + 3) * 1000 + (c->get_num() + 7);
}
struct base_trampoline : public base {
using base::base;
int get_num() const override {
PYBIND11_OVERRIDE(int, base, get_num);
}
std::shared_ptr<base> clone() const override {
PYBIND11_OVERRIDE(std::shared_ptr<base>, base, clone);
}
};
TEST_SUBMODULE(cpp_base_py_derived, m) {
py::class_<base, base_trampoline, std::shared_ptr<base>>(m, "base")
.def(py::init<>())
.def("get_num", &base::get_num)
.def("clone", &base::clone)
;
m.def("get_num", get_num);
m.def("clone_get_num", clone_get_num);
}
} // namespace cpp_base_py_derived
} // namespace pybind11_tests

View File

@ -1,36 +0,0 @@
# -*- coding: utf-8 -*-
# pybind11 equivalent of Boost.Python test:
# https://github.com/rwgk/rwgk_tbx/blob/6c9a6d6bc72d5c1b8609724433259c5b47178680/tst_cpp_base_py_derived.py
# See also: https://github.com/pybind/pybind11/issues/1333 (this was the starting point)
from pybind11_tests import cpp_base_py_derived as m
class drvd(m.base): # noqa: N801
def __init__(self, _num=200):
super().__init__()
self._drvd_num = _num
def get_num(self):
return self._drvd_num
def clone(self):
return drvd(250)
def test_base():
b = m.base()
assert b.get_num() == 100
assert m.get_num(b) == 100
bc = b.clone()
assert bc.get_num() == 150
assert m.clone_get_num(b) == 103157
def test_drvd():
d = drvd()
assert d.get_num() == 200
assert m.get_num(d) == 200
dc = d.clone()
assert dc.get_num() == 250
assert m.clone_get_num(d) == 203257

View File

@ -1,69 +0,0 @@
// KEEP IN SYNC WITH test_holder_unique_ptr.cpp
#include "pybind11_tests.h"
#include <iostream>
#include <memory>
namespace pybind11_tests {
namespace holder_shared_ptr {
inline void to_cout(std::string text) { std::cout << text << std::endl; }
class pointee { // NOT copyable.
public:
pointee() { to_cout("pointee::pointee()"); }
int get_int() const {
to_cout("pointee::get_int()");
return 213;
}
~pointee() { to_cout("~pointee()"); }
private:
pointee(const pointee &) = delete;
pointee(pointee &&) = delete;
pointee &operator=(const pointee &) = delete;
pointee &operator=(pointee &&) = delete;
};
inline std::unique_ptr<pointee> make_unique_pointee() {
return std::unique_ptr<pointee>(new pointee);
}
inline std::shared_ptr<pointee> make_shared_pointee() {
return std::unique_ptr<pointee>(new pointee);
}
inline int pass_unique_pointee(std::unique_ptr<pointee> ptr) {
return 4000 + ptr->get_int();
}
inline int pass_shared_pointee(std::shared_ptr<pointee> ptr) {
return 5000 + ptr->get_int();
}
inline pointee* get_static_pointee() {
static pointee cpp_instance;
return &cpp_instance;
}
TEST_SUBMODULE(holder_shared_ptr, m) {
m.def("to_cout", to_cout);
py::class_<pointee, std::shared_ptr<pointee>>(m, "pointee")
.def(py::init<>())
.def("get_int", &pointee::get_int);
m.def("make_unique_pointee", make_unique_pointee);
m.def("make_shared_pointee", make_shared_pointee);
// m.def("pass_unique_pointee", pass_unique_pointee);
m.def("pass_shared_pointee", pass_shared_pointee);
m.def("get_static_pointee",
get_static_pointee, py::return_value_policy::reference);
}
} // namespace holder_shared_ptr
} // namespace pybind11_tests

View File

@ -1,56 +0,0 @@
# -*- coding: utf-8 -*-
# KEEP IN SYNC WITH test_holder_unique_ptr.py
import pytest
from pybind11_tests import holder_shared_ptr as m
def test_make_unique_pointee():
m.to_cout("")
m.to_cout("")
m.to_cout("make_unique_pointee")
obj = m.make_unique_pointee()
assert obj.get_int() == 213
m.to_cout("")
def test_make_shared_pointee():
m.to_cout("")
m.to_cout("")
m.to_cout("make_shared_pointee")
obj = m.make_shared_pointee()
assert obj.get_int() == 213
m.to_cout("")
def test_pass_unique_pointee():
m.to_cout("")
m.to_cout("")
m.to_cout("pass_unique_pointee")
obj = m.make_shared_pointee()
assert obj.get_int() == 213
i = m.pass_unique_pointee(obj)
assert i == 4213
m.to_cout("")
def test_pass_shared_pointee():
m.to_cout("")
m.to_cout("")
m.to_cout("pass_shared_pointee")
obj = m.make_shared_pointee()
assert obj.get_int() == 213
i = m.pass_shared_pointee(obj)
assert i == 5213
m.to_cout("")
def test_get_static_pointee():
m.to_cout("")
m.to_cout("")
m.to_cout("get_static_pointee")
obj = m.get_static_pointee()
assert obj.get_int() == 213
with pytest.raises(RuntimeError) as excinfo:
m.pass_shared_pointee(obj)
assert "Unable to cast from non-held to held instance" in str(excinfo.value)

View File

@ -1,69 +0,0 @@
// KEEP IN SYNC WITH test_holder_shared_ptr.cpp
#include "pybind11_tests.h"
#include <iostream>
#include <memory>
namespace pybind11_tests {
namespace holder_unique_ptr {
inline void to_cout(std::string text) { std::cout << text << std::endl; }
class pointee { // NOT copyable.
public:
pointee() { to_cout("pointee::pointee()"); }
int get_int() const {
to_cout("pointee::get_int()");
return 213;
}
~pointee() { to_cout("~pointee()"); }
private:
pointee(const pointee &) = delete;
pointee(pointee &&) = delete;
pointee &operator=(const pointee &) = delete;
pointee &operator=(pointee &&) = delete;
};
inline std::unique_ptr<pointee> make_unique_pointee() {
return std::unique_ptr<pointee>(new pointee);
}
inline std::shared_ptr<pointee> make_shared_pointee() {
return std::unique_ptr<pointee>(new pointee);
}
inline int pass_unique_pointee(std::unique_ptr<pointee> ptr) {
return 4000 + ptr->get_int();
}
inline int pass_shared_pointee(std::shared_ptr<pointee> ptr) {
return 5000 + ptr->get_int();
}
inline pointee* get_static_pointee() {
static pointee cpp_instance;
return &cpp_instance;
}
TEST_SUBMODULE(holder_unique_ptr, m) {
m.def("to_cout", to_cout);
py::class_<pointee>(m, "pointee")
.def(py::init<>())
.def("get_int", &pointee::get_int);
m.def("make_unique_pointee", make_unique_pointee);
m.def("make_shared_pointee", make_shared_pointee);
// m.def("pass_unique_pointee", pass_unique_pointee);
m.def("pass_shared_pointee", pass_shared_pointee);
m.def("get_static_pointee",
get_static_pointee, py::return_value_policy::reference);
}
} // namespace holder_unique_ptr
} // namespace pybind11_tests

View File

@ -1,56 +0,0 @@
# -*- coding: utf-8 -*-
# KEEP IN SYNC WITH test_holder_shared_ptr.py
import pytest
from pybind11_tests import holder_unique_ptr as m
def test_make_unique_pointee():
m.to_cout("")
m.to_cout("")
m.to_cout("make_unique_pointee")
obj = m.make_unique_pointee()
assert obj.get_int() == 213
m.to_cout("")
def test_make_shared_pointee():
m.to_cout("")
m.to_cout("")
m.to_cout("make_shared_pointee")
obj = m.make_shared_pointee()
assert obj.get_int() == 213
m.to_cout("")
def test_pass_unique_pointee():
m.to_cout("")
m.to_cout("")
m.to_cout("pass_unique_pointee")
obj = m.make_unique_pointee()
assert obj.get_int() == 213
i = m.pass_unique_pointee(obj)
assert i == 4213
m.to_cout("")
def test_pass_shared_pointee():
m.to_cout("")
m.to_cout("")
m.to_cout("pass_shared_pointee")
obj = m.make_unique_pointee()
assert obj.get_int() == 213
i = m.pass_shared_pointee(obj)
assert i == 5213
m.to_cout("")
def test_get_static_pointee():
m.to_cout("")
m.to_cout("")
m.to_cout("get_static_pointee")
obj = m.get_static_pointee()
assert obj.get_int() == 213
with pytest.raises(RuntimeError) as excinfo:
m.pass_unique_pointee(obj)
assert "Unable to cast from non-held to held instance" in str(excinfo.value)

View File

@ -1,63 +0,0 @@
// Demonstration of UB (Undefined Behavior) in handling of polymorphic pointers,
// specifically:
// https://github.com/pybind/pybind11/blob/30eb39ed79d1e2eeff15219ac00773034300a5e6/include/pybind11/cast.h#L229
// `return reinterpret_cast<V *&>(vh[0]);`
// casts a `void` pointer to a `base`. The `void` pointer is obtained through
// a `dynamic_cast` here:
// https://github.com/pybind/pybind11/blob/30eb39ed79d1e2eeff15219ac00773034300a5e6/include/pybind11/cast.h#L852
// `return dynamic_cast<const void*>(src);`
// The `dynamic_cast` is well-defined:
// https://en.cppreference.com/w/cpp/language/dynamic_cast
// 4) If expression is a pointer to a polymorphic type, and new-type
// is a pointer to void, the result is a pointer to the most derived
// object pointed or referenced by expression.
// But the `reinterpret_cast` above is UB: `test_make_drvd_pass_base` in
// `test_private_first_base.py` fails with a Segmentation Fault (Linux,
// clang++ -std=c++17).
// The only well-defined cast is back to a `drvd` pointer (`static_cast` can be
// used), which can then safely be cast up to a `base` pointer. Note that
// `test_make_drvd_up_cast_pass_drvd` passes because the `void` pointer is cast
// to `drvd` pointer in this situation.
#include "pybind11_tests.h"
namespace pybind11_tests {
namespace private_first_base {
struct base {
base() : base_id(100) {}
virtual ~base() = default;
virtual int id() const { return base_id; }
base(const base&) = delete;
int base_id;
};
struct private_first_base { // Any class with a virtual function will do.
virtual void some_other_virtual_function() const {}
virtual ~private_first_base() = default;
};
struct drvd : private private_first_base, public base {
int id() const override { return 2 * base_id; }
};
inline drvd* make_drvd() { return new drvd; }
inline base* make_drvd_up_cast() { return new drvd; }
inline int pass_base(const base* b) { return b->id(); }
inline int pass_drvd(const drvd* d) { return d->id(); }
TEST_SUBMODULE(private_first_base, m) {
py::class_<base>(m, "base");
py::class_<drvd, base>(m, "drvd");
m.def("make_drvd", make_drvd,
py::return_value_policy::take_ownership);
m.def("make_drvd_up_cast", make_drvd_up_cast,
py::return_value_policy::take_ownership);
m.def("pass_base", pass_base);
m.def("pass_drvd", pass_drvd);
}
} // namespace private_first_base
} // namespace pybind11_tests

View File

@ -1,17 +0,0 @@
# -*- coding: utf-8 -*-
from pybind11_tests import private_first_base as m
def test_make_drvd_pass_base():
d = m.make_drvd()
i = m.pass_base(d)
assert i == 200
def test_make_drvd_up_cast_pass_drvd():
b = m.make_drvd_up_cast()
# the base return is down-cast immediately.
assert b.__class__.__name__ == "drvd"
i = m.pass_drvd(b)
assert i == 200

View File

@ -1,160 +0,0 @@
#include "pybind11_tests.h"
#include <iostream>
#include <memory>
namespace pybind11_tests {
namespace smart_ptr_base_derived {
inline void to_cout(std::string text) { std::cout << text << std::endl; }
class cbase {
public:
int get_int() const { return 90146438; }
};
class cderived : public cbase {
public:
// Printing from constructor & destructor for simple external validation.
cderived() {
std::cout << std::endl << "cderived+" << std::endl;
}
~cderived() {
std::cout << std::endl << "cderived-" << std::endl;
}
int get_int() const { return 31607978; }
int base_get_int(const cbase& base) { return get_int() + base.get_int(); }
};
class vbase {
public:
virtual ~vbase() {}
virtual int get_int() const = 0;
};
class vderived : public vbase {
public:
// Printing from constructor & destructor for simple external validation.
vderived() {
std::cout << std::endl << "vderived+" << std::endl;
}
~vderived() {
std::cout << std::endl << "vderived-" << std::endl;
}
int get_int() const override { return 29852452; }
int base_get_int(const vbase& base) { return get_int() + base.get_int(); }
};
class vrederived : public vderived {};
inline std::unique_ptr<cbase>
make_unique_cderived_up_cast() {
// Undefined Behavior (pure C++ problem, NOT a pybind11 problem):
// cderived destructor does not run.
return std::unique_ptr<cderived>(new cderived);
}
inline std::shared_ptr<cderived>
make_shared_cderived(bool use_custom_deleter = false) {
if (use_custom_deleter) {
return std::shared_ptr<cderived>(
new cderived, [](cderived *p) { delete p; });
}
return std::shared_ptr<cderived>(new cderived);
}
inline std::shared_ptr<cbase>
make_shared_cderived_up_cast(bool use_custom_deleter = false) {
return make_shared_cderived(use_custom_deleter);
}
inline int pass_unique_cbase(std::unique_ptr<cbase> cb) {
return cb->get_int();
}
inline int pass_shared_cbase(std::shared_ptr<cbase> cb) {
return cb->get_int();
}
inline int pass_shared_cderived(std::shared_ptr<cderived> cd) {
return cd->get_int();
}
inline std::unique_ptr<vbase>
make_unique_vderived_up_cast() {
// Well-defined behavior because vderived has a virtual destructor.
return std::unique_ptr<vderived>(new vderived);
}
inline std::shared_ptr<vderived>
make_shared_vderived(bool use_custom_deleter = false) {
if (use_custom_deleter) {
return std::shared_ptr<vderived>(
new vderived, [](vderived *p) { delete p; });
}
return std::shared_ptr<vderived>(new vderived);
}
inline std::shared_ptr<vbase>
make_shared_vderived_up_cast(bool use_custom_deleter = false) {
return make_shared_vderived(use_custom_deleter);
}
inline int pass_unique_vbase(std::unique_ptr<vbase> vb) {
return vb->get_int();
}
inline int pass_shared_vbase(std::shared_ptr<vbase> vb) {
return vb->get_int();
}
inline int pass_shared_vderived(std::shared_ptr<vderived> vd) {
return vd->get_int();
}
inline int pass_shared_vrederived(std::shared_ptr<vrederived> vr) {
return vr->get_int();
}
TEST_SUBMODULE(smart_ptr_base_derived, m) {
m.def("to_cout", to_cout);
py::class_<cbase, std::shared_ptr<cbase>>(m, "cbase")
.def(py::init<>())
.def("get_int", &cbase::get_int);
py::class_<cderived, cbase, std::shared_ptr<cderived>>(m, "cderived")
.def(py::init<>())
.def("get_int", &cderived::get_int);
py::class_<vbase, std::shared_ptr<vbase>>(m, "vbase")
.def("get_int", &vbase::get_int);
py::class_<vderived, vbase, std::shared_ptr<vderived>>(m, "vderived")
.def(py::init<>());
py::class_<vrederived, vderived, std::shared_ptr<vrederived>>(m, "vrederived")
.def(py::init<>());
m.def("make_shared_cderived",
make_shared_cderived,
py::arg("use_custom_deleter") = false);
m.def("make_shared_cderived_up_cast",
make_shared_cderived_up_cast,
py::arg("use_custom_deleter") = false);
m.def("pass_shared_cbase", pass_shared_cbase);
m.def("pass_shared_cderived", pass_shared_cderived);
m.def("make_shared_vderived",
make_shared_vderived,
py::arg("use_custom_deleter") = false);
m.def("make_shared_vderived_up_cast",
make_shared_vderived_up_cast,
py::arg("use_custom_deleter") = false);
m.def("pass_shared_vbase", pass_shared_vbase);
m.def("pass_shared_vderived", pass_shared_vderived);
m.def("pass_shared_vrederived", pass_shared_vrederived);
}
} // namespace smart_ptr_base_derived
} // namespace pybind11_tests

View File

@ -1,42 +0,0 @@
# -*- coding: utf-8 -*-
import pytest
from pybind11_tests import smart_ptr_base_derived as m
CBASE_GET_INT_RESULT = 90146438
CDERIVED_GET_INT_RESULT = 31607978
VDERIVED_GET_INT_RESULT = 29852452
def test_concrete():
m.to_cout("")
m.to_cout("")
m.to_cout("make_shared_cderived")
cd = m.make_shared_cderived()
assert cd.get_int() == CDERIVED_GET_INT_RESULT
m.pass_shared_cderived(cd)
m.pass_shared_cbase(cd)
cb = m.make_shared_cderived_up_cast()
assert cb.get_int() == CBASE_GET_INT_RESULT
m.pass_shared_cbase(cb)
with pytest.raises(TypeError):
m.pass_shared_cderived(cb)
m.to_cout("")
def test_virtual():
m.to_cout("")
m.to_cout("")
m.to_cout("make_shared_vderived")
vd = m.make_shared_vderived()
assert vd.get_int() == VDERIVED_GET_INT_RESULT
m.pass_shared_vderived(vd)
m.pass_shared_vbase(vd)
vd_uc = m.make_shared_vderived_up_cast()
assert vd_uc.get_int() == VDERIVED_GET_INT_RESULT
assert isinstance(vd_uc, m.vderived) # pybind11 un-did upcast.
m.pass_shared_vbase(vd_uc)
m.pass_shared_vderived(vd_uc)
with pytest.raises(TypeError):
m.pass_shared_vrederived(vd_uc)
m.to_cout("")

View File

@ -1,58 +0,0 @@
// Demonstration of Undefined Behavior in handling of shared_ptr holder,
// specifically:
// https://github.com/pybind/pybind11/blob/30eb39ed79d1e2eeff15219ac00773034300a5e6/include/pybind11/cast.h#L235
// `return reinterpret_cast<H &>(vh[1]);`
// indirectly casts a `shared_ptr<drvd>` reference to a `shared_ptr<base>`.
// Similarly:
// https://github.com/pybind/pybind11/blob/30eb39ed79d1e2eeff15219ac00773034300a5e6/include/pybind11/pybind11.h#L1505
// `init_holder(inst, v_h, (const holder_type *) holder_ptr, v_h.value_ptr<type>());`
// explictly casts a `shared_ptr<base>` reference to a `shared_ptr<drvd>`.
// Both tests in `test_smart_ptr_private_first_base.py` fail with a
// Segmentation Fault (Linux, clang++ -std=c++17).
#include <memory>
#include "pybind11_tests.h"
namespace pybind11_tests {
namespace smart_ptr_private_first_base {
struct base {
base() : base_id(100) {}
virtual ~base() = default;
virtual int id() const { return base_id; }
int base_id;
};
struct private_first_base { // Any class with a virtual function will do.
virtual void some_other_virtual_function() const {}
virtual ~private_first_base() = default;
};
struct drvd : private private_first_base, public base {
int id() const override { return 2 * base_id; }
};
inline std::shared_ptr<drvd> make_shared_drvd() {
return std::shared_ptr<drvd>(new drvd);
}
inline std::shared_ptr<base> make_shared_drvd_up_cast() {
return std::shared_ptr<base>(new drvd);
}
inline int pass_shared_base(std::shared_ptr<base> b) { return b->id(); }
inline int pass_shared_drvd(std::shared_ptr<drvd> d) { return d->id(); }
TEST_SUBMODULE(smart_ptr_private_first_base, m) {
py::class_<base, std::shared_ptr<base>>(m, "base");
py::class_<drvd, base, std::shared_ptr<drvd>>(m, "drvd");
m.def("make_shared_drvd", make_shared_drvd);
m.def("make_shared_drvd_up_cast", make_shared_drvd_up_cast);
m.def("pass_shared_base", pass_shared_base);
m.def("pass_shared_drvd", pass_shared_drvd);
}
} // namespace smart_ptr_private_first_base
} // namespace pybind11_tests

View File

@ -1,17 +0,0 @@
# -*- coding: utf-8 -*-
from pybind11_tests import smart_ptr_private_first_base as m
def test_make_drvd_pass_base():
d = m.make_shared_drvd()
i = m.pass_shared_base(d)
assert i == 200
def test_make_drvd_up_cast_pass_drvd():
b = m.make_shared_drvd_up_cast()
# the base return is down-cast immediately.
assert b.__class__.__name__ == "drvd"
i = m.pass_shared_drvd(b)
assert i == 200