Skip to content

Commit d36655b

Browse files
guitargeekdpiparo
authored andcommitted
[CPyCppyy] Add pythonizations for string_view
This also makes sure that the `string_view` converts to Python strings correctly. Closes #19476.
1 parent ccdb26a commit d36655b

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,15 +1136,15 @@ static int PyObject_Compare(PyObject* one, PyObject* other) {
11361136
}
11371137
#endif
11381138
static inline
1139-
PyObject* CPyCppyy_PyString_FromCppString(std::string* s, bool native=true) {
1139+
PyObject* CPyCppyy_PyString_FromCppString(std::string_view s, bool native=true) {
11401140
if (native)
1141-
return PyBytes_FromStringAndSize(s->data(), s->size());
1142-
return CPyCppyy_PyText_FromStringAndSize(s->data(), s->size());
1141+
return PyBytes_FromStringAndSize(s.data(), s.size());
1142+
return CPyCppyy_PyText_FromStringAndSize(s.data(), s.size());
11431143
}
11441144

11451145
static inline
1146-
PyObject* CPyCppyy_PyString_FromCppString(std::wstring* s, bool native=true) {
1147-
PyObject* pyobj = PyUnicode_FromWideChar(s->data(), s->size());
1146+
PyObject* CPyCppyy_PyString_FromCppString(std::wstring_view s, bool native=true) {
1147+
PyObject* pyobj = PyUnicode_FromWideChar(s.data(), s.size());
11481148
if (pyobj && native) {
11491149
PyObject* pybytes = PyUnicode_AsEncodedString(pyobj, "UTF-8", "strict");
11501150
Py_DECREF(pyobj);
@@ -1159,7 +1159,7 @@ PyObject* name##StringGetData(PyObject* self, bool native=true) \
11591159
{ \
11601160
if (CPyCppyy::CPPInstance_Check(self)) { \
11611161
type* obj = ((type*)((CPPInstance*)self)->GetObject()); \
1162-
if (obj) return CPyCppyy_PyString_FromCppString(obj, native); \
1162+
if (obj) return CPyCppyy_PyString_FromCppString(*obj, native); \
11631163
} \
11641164
PyErr_Format(PyExc_TypeError, "object mismatch (%s expected)", #type); \
11651165
return nullptr; \
@@ -1236,6 +1236,7 @@ PyObject* name##StringCompare(PyObject* self, PyObject* obj) \
12361236

12371237
CPPYY_IMPL_STRING_PYTHONIZATION_CMP(std::string, STL)
12381238
CPPYY_IMPL_STRING_PYTHONIZATION_CMP(std::wstring, STLW)
1239+
CPPYY_IMPL_STRING_PYTHONIZATION_CMP(std::string_view, STLView)
12391240

12401241
static inline std::string* GetSTLString(CPPInstance* self) {
12411242
if (!CPPInstance_Check(self)) {
@@ -1935,9 +1936,15 @@ bool CPyCppyy::Pythonize(PyObject* pyclass, const std::string& name)
19351936
((PyTypeObject*)pyclass)->tp_hash = (hashfunc)STLStringHash;
19361937
}
19371938

1938-
else if (name == "basic_string_view<char>" || name == "std::basic_string_view<char>") {
1939+
else if (name == "basic_string_view<char,char_traits<char> >" || name == "std::basic_string_view<char>") {
19391940
Utility::AddToClass(pyclass, "__real_init", "__init__");
1940-
Utility::AddToClass(pyclass, "__init__", (PyCFunction)StringViewInit, METH_VARARGS | METH_KEYWORDS);
1941+
Utility::AddToClass(pyclass, "__init__", (PyCFunction)StringViewInit, METH_VARARGS | METH_KEYWORDS);
1942+
Utility::AddToClass(pyclass, "__bytes__", (PyCFunction)STLViewStringBytes, METH_NOARGS);
1943+
Utility::AddToClass(pyclass, "__cmp__", (PyCFunction)STLViewStringCompare, METH_O);
1944+
Utility::AddToClass(pyclass, "__eq__", (PyCFunction)STLViewStringIsEqual, METH_O);
1945+
Utility::AddToClass(pyclass, "__ne__", (PyCFunction)STLViewStringIsNotEqual, METH_O);
1946+
Utility::AddToClass(pyclass, "__repr__", (PyCFunction)STLViewStringRepr, METH_NOARGS);
1947+
Utility::AddToClass(pyclass, "__str__", (PyCFunction)STLViewStringStr, METH_NOARGS);
19411948
}
19421949

19431950
else if (name == "basic_string<wchar_t,char_traits<wchar_t>,allocator<wchar_t> >" || name == "std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >") {

0 commit comments

Comments
 (0)