Skip to content

[636][CPyCppyy] Add pythonizations for string_view #19628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1134,15 +1134,15 @@ static int PyObject_Compare(PyObject* one, PyObject* other) {
}
#endif
static inline
PyObject* CPyCppyy_PyString_FromCppString(std::string* s, bool native=true) {
PyObject* CPyCppyy_PyString_FromCppString(std::string_view s, bool native=true) {
if (native)
return PyBytes_FromStringAndSize(s->data(), s->size());
return CPyCppyy_PyText_FromStringAndSize(s->data(), s->size());
return PyBytes_FromStringAndSize(s.data(), s.size());
return CPyCppyy_PyText_FromStringAndSize(s.data(), s.size());
}

static inline
PyObject* CPyCppyy_PyString_FromCppString(std::wstring* s, bool native=true) {
PyObject* pyobj = PyUnicode_FromWideChar(s->data(), s->size());
PyObject* CPyCppyy_PyString_FromCppString(std::wstring_view s, bool native=true) {
PyObject* pyobj = PyUnicode_FromWideChar(s.data(), s.size());
if (pyobj && native) {
PyObject* pybytes = PyUnicode_AsEncodedString(pyobj, "UTF-8", "strict");
Py_DECREF(pyobj);
Expand All @@ -1157,7 +1157,7 @@ PyObject* name##StringGetData(PyObject* self, bool native=true) \
{ \
if (CPyCppyy::CPPInstance_Check(self)) { \
type* obj = ((type*)((CPPInstance*)self)->GetObject()); \
if (obj) return CPyCppyy_PyString_FromCppString(obj, native); \
if (obj) return CPyCppyy_PyString_FromCppString(*obj, native); \
} \
PyErr_Format(PyExc_TypeError, "object mismatch (%s expected)", #type); \
return nullptr; \
Expand Down Expand Up @@ -1234,6 +1234,7 @@ PyObject* name##StringCompare(PyObject* self, PyObject* obj) \

CPPYY_IMPL_STRING_PYTHONIZATION_CMP(std::string, STL)
CPPYY_IMPL_STRING_PYTHONIZATION_CMP(std::wstring, STLW)
CPPYY_IMPL_STRING_PYTHONIZATION_CMP(std::string_view, STLView)

static inline std::string* GetSTLString(CPPInstance* self) {
if (!CPPInstance_Check(self)) {
Expand Down Expand Up @@ -1932,9 +1933,15 @@ bool CPyCppyy::Pythonize(PyObject* pyclass, const std::string& name)
((PyTypeObject*)pyclass)->tp_hash = (hashfunc)STLStringHash;
}

else if (name == "basic_string_view<char>" || name == "std::basic_string_view<char>") {
else if (name == "basic_string_view<char,char_traits<char> >" || name == "std::basic_string_view<char>") {
Utility::AddToClass(pyclass, "__real_init", "__init__");
Utility::AddToClass(pyclass, "__init__", (PyCFunction)StringViewInit, METH_VARARGS | METH_KEYWORDS);
Utility::AddToClass(pyclass, "__init__", (PyCFunction)StringViewInit, METH_VARARGS | METH_KEYWORDS);
Utility::AddToClass(pyclass, "__bytes__", (PyCFunction)STLViewStringBytes, METH_NOARGS);
Utility::AddToClass(pyclass, "__cmp__", (PyCFunction)STLViewStringCompare, METH_O);
Utility::AddToClass(pyclass, "__eq__", (PyCFunction)STLViewStringIsEqual, METH_O);
Utility::AddToClass(pyclass, "__ne__", (PyCFunction)STLViewStringIsNotEqual, METH_O);
Utility::AddToClass(pyclass, "__repr__", (PyCFunction)STLViewStringRepr, METH_NOARGS);
Utility::AddToClass(pyclass, "__str__", (PyCFunction)STLViewStringStr, METH_NOARGS);
}

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> >") {
Expand Down
Loading