diff --git a/main.cc b/main.cc index 81748e4..38514fa 100644 --- a/main.cc +++ b/main.cc @@ -1,70 +1,43 @@ #include +#include #include 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(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_; }; -void init_context(py::module &m) { - py::class_(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_(m, "Group") - .def( - py::init(), - 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_(m, "ErrorTest") + .def(py::init()) + .def("__repr__", &ErrorTest::repr); } diff --git a/requirements.txt b/requirements.txt index f66543c..e47c59f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1 @@ pybind11 -pytest diff --git a/run.sh b/run.sh index f8f2994..530efa9 100755 --- a/run.sh +++ b/run.sh @@ -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 diff --git a/tests.py b/tests.py index e1a64bd..6b8a524 100644 --- a/tests.py +++ b/tests.py @@ -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()