-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
28 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
pybind11 | ||
pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |