From 2362cc128e4640a5b1940b9c2b6474aea9594327 Mon Sep 17 00:00:00 2001 From: Valery Yundin Date: Wed, 30 Dec 2015 12:26:08 +0100 Subject: [PATCH] Change bytestring to use type wrapper py::bytes py::bytes will work with any T which has these methods const char* data() and int size() Other types can be accommodated by specializing bytes and type_caster> --- include/pybind11/cast.h | 10 +++++----- include/pybind11/pytypes.h | 11 ++++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index bcaed97e9..a6153b807 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -378,15 +378,15 @@ public: #endif }; -template <> class type_caster : public type_caster { +template class type_caster> : public type_caster { public: - static PyObject *cast(const bytestring &src, return_value_policy /* policy */, PyObject * /* parent */) { - return PYBIND11_FROM_STRING_AND_SIZE(src.c_str(), src.size()); + static PyObject *cast(const bytes &src, return_value_policy /* policy */, PyObject * /* parent */) { + return PYBIND11_FROM_STRING_AND_SIZE(src.data(), src.size()); } #if PY_MAJOR_VERSION >= 3 - PYBIND11_TYPE_CASTER(bytestring, "bytes"); + PYBIND11_TYPE_CASTER(bytes, "bytes"); #else - PYBIND11_TYPE_CASTER(bytestring, "str"); + PYBIND11_TYPE_CASTER(bytes, "str"); #endif }; diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 154842348..0b1ad5e1c 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -259,13 +259,14 @@ inline iterator handle::end() { return iterator(nullptr); } PYBIND11_OBJECT(Name, Parent, CheckFun) \ Name() : Parent() { } -class bytestring : public std::string { +template +class bytes : public T { public: - using std::string::string; + using T::T; - bytestring(const std::string& src) : std::string(src) { } - bytestring(std::string&& src) : std::string(std::move(src)) { } - bytestring() : std::string() { } + bytes(const T& src) : T(src) { } + bytes(T&& src) : T(std::move(src)) { } + bytes() : T() { } }; class str : public object {