Skip to content

Commit

Permalink
Reduced further after finding #4549
Browse files Browse the repository at this point in the history
  • Loading branch information
davisp committed Dec 1, 2023
1 parent e5e2b78 commit 98699f8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 67 deletions.
67 changes: 20 additions & 47 deletions main.cc
Original file line number Diff line number Diff line change
@@ -1,70 +1,43 @@
#include <iostream>
#include <memory>

#include <pybind11/pybind11.h>

namespace py = pybind11;

class MyContext {
public:
MyContext() : msg_("Can I be a mongoose dog?") {
std::cerr << "Context Constructor: " << (void*) this << std::endl;
}

~MyContext() {
std::cerr << "Context Destructor: " << (void*) this << std::endl;
}

std::string get() const {
return msg_;
}

private:
std::string msg_;
struct Test {
Test(const std::string& val) : val_(val) {}
std::string val_;
};

class MyGroup {
class ErrorTest {
public:
MyGroup(const MyContext& ctx, const std::string& uri) : ctx_(ctx), uri_(uri) {
std::cerr << "Group Constructor: " << (void*) this << std::endl;
std::cerr << "Group Context: " << (void*) &ctx_ << std::endl;
throw std::runtime_error("Look! A squirrel!");
ErrorTest(const std::string& mesg)
: test_(std::make_shared<Test>(mesg)) {
std::cerr << "Constructor: " << (void*) this << std::endl;
throw std::runtime_error("");
}

~MyGroup() {
std::cerr << "Group Destructor: " << (void*) this << std::endl;
~ErrorTest() {
std::cerr << "Destructor: " << (void*) this << std::endl;
}

std::string dump(bool) {
return ctx_.get();
std::string repr() {
return test_->val_;
}

private:
const MyContext& ctx_;
std::string uri_;
std::shared_ptr<Test> test_;
};

void init_context(py::module &m) {
py::class_<MyContext>(m, "Context")
.def(py::init());
}

std::string do_dump(MyGroup& group, bool recursive) {
std::cerr << "DUMPING GROUP: " << (void*) &group << std::endl;
auto ret = group.dump(recursive);
std::cerr << "FINISHED DUMPING GROUP: " << (void*) &group << std::endl;
return ret;
}

void init_group(py::module &m) {
py::class_<MyGroup>(m, "Group")
.def(
py::init<const MyContext &, const std::string &>(),
py::keep_alive<1, 2>())
.def("_dump", do_dump);

}

PYBIND11_MODULE(errortest, m) {
m.doc() = "Test exceptions from contructors";
init_context(m);
init_group(m);
m.doc() = "Test exceptions from contructors";

py::class_<ErrorTest>(m, "ErrorTest")
.def(py::init<const std::string &>())
.def("__repr__", &ErrorTest::repr);
}
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pybind11
pytest
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python setup.py develop
pytest --capture=no tests.py
python tests.py
25 changes: 7 additions & 18 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import pytest

import errortest

class Group(errortest.Group):
def __init__(self, uri):
try:
ctx = errortest.Context()
super().__init__(ctx, uri)
except Exception as exc:
print(exc)
raise

def __repr__(self):
return self._dump(True)
class Derived(errortest.ErrorTest):
pass

def test_invalid_object_type():
path = "not_a_group"
with pytest.raises(ValueError):
# Should return error that uri is not a Group
group = Group(uri=path)
def test_new():
g = Derived.__new__(Derived)
print(repr(g))

if __name__ == "__main__":
test_new()

0 comments on commit 98699f8

Please sign in to comment.