mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-26 15:12:01 +00:00
Add casting operators between py::str / py::bytes
This commit is contained in:
parent
3768b6abf9
commit
006d8b6621
@ -139,6 +139,22 @@ public:
|
||||
throw std::runtime_error("This exception was intentionally thrown.");
|
||||
}
|
||||
|
||||
py::bytes get_bytes_from_string() {
|
||||
return std::string("foo");
|
||||
}
|
||||
|
||||
py::bytes get_bytes_from_str() {
|
||||
return py::str(std::string("bar"));
|
||||
}
|
||||
|
||||
py::str get_str_from_string() {
|
||||
return std::string("baz");
|
||||
}
|
||||
|
||||
py::str get_str_from_bytes() {
|
||||
return py::bytes(std::string("boo"));
|
||||
}
|
||||
|
||||
static int value;
|
||||
static const int value2;
|
||||
};
|
||||
@ -167,6 +183,10 @@ void init_ex_python_types(py::module &m) {
|
||||
.def("pair_passthrough", &ExamplePythonTypes::pair_passthrough, "Return a pair in reversed order")
|
||||
.def("tuple_passthrough", &ExamplePythonTypes::tuple_passthrough, "Return a triple in reversed order")
|
||||
.def("throw_exception", &ExamplePythonTypes::throw_exception, "Throw an exception")
|
||||
.def("get_bytes_from_string", &ExamplePythonTypes::get_bytes_from_string, "py::bytes from std::string")
|
||||
.def("get_bytes_from_str", &ExamplePythonTypes::get_bytes_from_str, "py::bytes from py::str")
|
||||
.def("get_str_from_string", &ExamplePythonTypes::get_str_from_string, "py::str from std::string")
|
||||
.def("get_str_from_bytes", &ExamplePythonTypes::get_str_from_bytes, "py::str from py::bytes")
|
||||
.def_static("new_instance", &ExamplePythonTypes::new_instance, "Return an instance")
|
||||
.def_readwrite_static("value", &ExamplePythonTypes::value, "Static value member")
|
||||
.def_readonly_static("value2", &ExamplePythonTypes::value2, "Static value member (readonly)")
|
||||
|
@ -72,3 +72,8 @@ cstats = ConstructorStats.get(ExamplePythonTypes)
|
||||
print("Instances not destroyed:", cstats.alive())
|
||||
instance = None
|
||||
print("Instances not destroyed:", cstats.alive())
|
||||
|
||||
print(instance.get_bytes_from_string().decode())
|
||||
print(instance.get_bytes_from_str().decode())
|
||||
print(instance.get_str_from_string())
|
||||
print(instance.get_str_from_bytes())
|
||||
|
@ -138,3 +138,8 @@ __module__(example.ExamplePythonTypes.get_set) = example
|
||||
Instances not destroyed: 1
|
||||
### ExamplePythonTypes @ 0x1045b80 destroyed
|
||||
Instances not destroyed: 0
|
||||
Destructing ExamplePythonTypes
|
||||
foo
|
||||
bar
|
||||
baz
|
||||
boo
|
||||
|
@ -339,6 +339,8 @@ inline iterator handle::begin() const { return iterator(PyObject_GetIter(ptr()),
|
||||
inline iterator handle::end() const { return iterator(nullptr, false); }
|
||||
inline detail::args_proxy handle::operator*() const { return detail::args_proxy(*this); }
|
||||
|
||||
class bytes;
|
||||
|
||||
class str : public object {
|
||||
public:
|
||||
PYBIND11_OBJECT_DEFAULT(str, object, detail::PyUnicode_Check_Permissive)
|
||||
@ -367,6 +369,8 @@ public:
|
||||
pybind11_fail("Unable to extract string contents! (invalid type)");
|
||||
return std::string(buffer, (size_t) length);
|
||||
}
|
||||
|
||||
operator bytes() const;
|
||||
};
|
||||
|
||||
inline pybind11::str handle::str() const {
|
||||
@ -395,8 +399,18 @@ public:
|
||||
pybind11_fail("Unable to extract bytes contents!");
|
||||
return std::string(buffer, (size_t) length);
|
||||
}
|
||||
|
||||
operator pybind11::str() const;
|
||||
};
|
||||
|
||||
inline str::operator bytes() const {
|
||||
return bytes((std::string) *this);
|
||||
}
|
||||
|
||||
inline bytes::operator pybind11::str() const {
|
||||
return pybind11::str((std::string) *this);
|
||||
}
|
||||
|
||||
class none : public object {
|
||||
public:
|
||||
PYBIND11_OBJECT(none, object, detail::PyNone_Check)
|
||||
|
Loading…
Reference in New Issue
Block a user