mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 21:25:13 +00:00
Add (const char *, size_t) ctors for str/bytes
This commit is contained in:
parent
1e1217817b
commit
89ec7f3e79
@ -144,7 +144,7 @@ public:
|
||||
}
|
||||
|
||||
py::bytes get_bytes_from_str() {
|
||||
return (py::bytes) py::str(std::string("bar"));
|
||||
return (py::bytes) py::str("bar", 3);
|
||||
}
|
||||
|
||||
py::str get_str_from_string() {
|
||||
@ -152,7 +152,7 @@ public:
|
||||
}
|
||||
|
||||
py::str get_str_from_bytes() {
|
||||
return (py::str) py::bytes(std::string("boo"));
|
||||
return (py::str) py::bytes("boo", 3);
|
||||
}
|
||||
|
||||
static int value;
|
||||
|
@ -345,8 +345,8 @@ class str : public object {
|
||||
public:
|
||||
PYBIND11_OBJECT_DEFAULT(str, object, detail::PyUnicode_Check_Permissive)
|
||||
|
||||
str(const std::string &s)
|
||||
: object(PyUnicode_FromStringAndSize(s.c_str(), (ssize_t) s.length()), false) {
|
||||
str(const char *c, size_t n)
|
||||
: object(PyUnicode_FromStringAndSize(c, (ssize_t) n), false) {
|
||||
if (!m_ptr) pybind11_fail("Could not allocate string object!");
|
||||
}
|
||||
|
||||
@ -355,6 +355,8 @@ public:
|
||||
if (!m_ptr) pybind11_fail("Could not allocate string object!");
|
||||
}
|
||||
|
||||
str(const std::string &s) : str(s.data(), s.size()) { }
|
||||
|
||||
operator std::string() const {
|
||||
object temp = *this;
|
||||
if (PyUnicode_Check(m_ptr)) {
|
||||
@ -385,11 +387,13 @@ class bytes : public object {
|
||||
public:
|
||||
PYBIND11_OBJECT_DEFAULT(bytes, object, PYBIND11_BYTES_CHECK)
|
||||
|
||||
bytes(const std::string &s)
|
||||
: object(PYBIND11_BYTES_FROM_STRING_AND_SIZE(s.data(), (ssize_t) s.size()), false) {
|
||||
bytes(const char *c, size_t n)
|
||||
: object(PYBIND11_BYTES_FROM_STRING_AND_SIZE(c, (ssize_t) n), false) {
|
||||
if (!m_ptr) pybind11_fail("Could not allocate bytes object!");
|
||||
}
|
||||
|
||||
bytes(const std::string &s) : bytes(s.data(), s.size()) { }
|
||||
|
||||
operator std::string() const {
|
||||
char *buffer;
|
||||
ssize_t length;
|
||||
|
Loading…
Reference in New Issue
Block a user