diff --git a/example/example4.cpp b/example/example4.cpp index 9865931aa..e673c4a81 100644 --- a/example/example4.cpp +++ b/example/example4.cpp @@ -21,8 +21,9 @@ public: ESecondMode }; - static void test_function(EMode mode) { + static EMode test_function(EMode mode) { std::cout << "Example4::test_function(enum=" << mode << ")" << std::endl; + return mode; } }; @@ -42,7 +43,7 @@ float test_function3(int i) { py::bytes return_bytes() { const char *data = "\x01\x00\x02\x00"; - return py::bytes(std::string(data, 4)); + return std::string(data, 4); } void print_bytes(py::bytes bytes) { diff --git a/example/example4.py b/example/example4.py index 74f860d0c..37d952f2b 100755 --- a/example/example4.py +++ b/example/example4.py @@ -20,10 +20,39 @@ print(test_function()) print(test_function(7)) print(test_function(EMyEnumeration.EFirstEntry)) print(test_function(EMyEnumeration.ESecondEntry)) +print("enum->integer = %i" % int(EMyEnumeration.ESecondEntry)) +print("integer->enum = %s" % str(EMyEnumeration(2))) + +print("A constant = " + str(some_constant)) print(Example4.EMode) print(Example4.EMode.EFirstMode) print(Example4.EFirstMode) Example4.test_function(Example4.EFirstMode) +print("Equality test 1: " + str( + Example4.test_function(Example4.EFirstMode) == + Example4.test_function(Example4.EFirstMode))) + +print("Inequality test 1: " + str( + Example4.test_function(Example4.EFirstMode) != + Example4.test_function(Example4.EFirstMode))) + +print("Equality test 2: " + str( + Example4.test_function(Example4.EFirstMode) == + Example4.test_function(Example4.ESecondMode))) + +print("Inequality test 2: " + str( + Example4.test_function(Example4.EFirstMode) != + Example4.test_function(Example4.ESecondMode))) + +x = { + Example4.test_function(Example4.EFirstMode): 1, + Example4.test_function(Example4.ESecondMode): 2 +} + +x[Example4.test_function(Example4.EFirstMode)] = 3 +x[Example4.test_function(Example4.ESecondMode)] = 4 +print("Hashing test = " + str(x)) + print_bytes(return_bytes()) diff --git a/example/example4.ref b/example/example4.ref index 040d55b85..a21f62c93 100644 --- a/example/example4.ref +++ b/example/example4.ref @@ -10,10 +10,30 @@ test_function(enum=1) None test_function(enum=2) None +enum->integer = 2 +integer->enum = EMyEnumeration.ESecondEntry +A constant = 14 EMode.EFirstMode EMode.EFirstMode Example4::test_function(enum=1) +Example4::test_function(enum=1) +Example4::test_function(enum=1) +Equality test 1: True +Example4::test_function(enum=1) +Example4::test_function(enum=1) +Inequality test 1: False +Example4::test_function(enum=1) +Example4::test_function(enum=2) +Equality test 2: False +Example4::test_function(enum=1) +Example4::test_function(enum=2) +Inequality test 2: True +Example4::test_function(enum=1) +Example4::test_function(enum=2) +Example4::test_function(enum=1) +Example4::test_function(enum=2) +Hashing test = {EMode.EFirstMode: 3, EMode.ESecondMode: 4} bytes[0]=1 bytes[1]=0 bytes[2]=2 diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index bbcda47f5..166966a1c 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -907,7 +907,11 @@ public: ((it == entries->end()) ? std::string("???") : std::string(it->second)); }); + this->def("__init__", [](Type& value, int i) { value = (Type) i; }); this->def("__int__", [](Type value) { return (int) value; }); + this->def("__eq__", [](const Type &value, Type value2) { return value == value2; }); + this->def("__ne__", [](const Type &value, Type value2) { return value != value2; }); + this->def("__hash__", [](const Type &value) { return (int) value; }); m_entries = entries; }