Skip to content

Commit

Permalink
Improve converters and basic_string support in Pythonisations
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronj0 committed Sep 10, 2024
1 parent 7541520 commit 8e2b27c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/Converters.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3349,11 +3349,11 @@ CPyCppyy::Converter* CPyCppyy::CreateConverter(Cppyy::TCppType_t type, cdims_t d

if (!result && cpd == "&&") {
// for builtin, can use const-ref for r-ref
h = gConvFactories.find("const " + realTypeStr + " &");
h = gConvFactories.find("const " + realTypeStr + "&");
if (h != gConvFactories.end())
return (h->second)(dims);
std::string temp ="const " + realUnresolvedTypeStr + " &";
h = gConvFactories.find("const " + realUnresolvedTypeStr + " &");
std::string temp ="const " + realUnresolvedTypeStr + "&";
h = gConvFactories.find("const " + realUnresolvedTypeStr + "&");
if (h != gConvFactories.end())
return (h->second)(dims);
// else, unhandled moves
Expand Down Expand Up @@ -3589,6 +3589,11 @@ static struct InitConvFactories_t {
gf["std::basic_string<char>&&"] = (cf_t)+[](cdims_t) { return new STLStringMoveConverter{}; };
gf["const std::basic_string<char> &"] = gf["std::basic_string<char>"];
gf["std::basic_string<char> &&"] = (cf_t)+[](cdims_t) { return new STLStringMoveConverter{}; };

gf["basic_string<char>"] = gf["std::basic_string<char>"];
gf["const basic_string<char>&"] = gf["const std::basic_string<char>&"];
gf["basic_string<char>&&"] = gf["std::basic_string<char>&&"];
gf["basic_string<char> &&"] = gf["std::basic_string<char> &&"];
#if __cplusplus > 201402L
gf["std::basic_string_view<char>"] = (cf_t)+[](cdims_t) { return new STLStringViewConverter{}; };
gf[STRINGVIEW] = gf["std::basic_string_view<char>"];
Expand Down
2 changes: 1 addition & 1 deletion src/ProxyWrappers.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ static int BuildScopeProxyDict(Cppyy::TCppScope_t scope, PyObject* pyclass, cons
// two options: this is a static variable, or it is the enum value, the latter
// already exists, so check for it and move on if set
PyObject* eset = PyObject_GetAttrString(pyclass,
const_cast<char*>(Cppyy::GetFinalName(datamember).c_str()));
const_cast<char*>(Cppyy::GetScopedFinalName(datamember).c_str()));
if (eset) {
Py_DECREF(eset);
continue;
Expand Down
6 changes: 3 additions & 3 deletions src/Pythonize.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1846,7 +1846,7 @@ bool CPyCppyy::Pythonize(PyObject* pyclass, Cppyy::TCppScope_t scope)
Utility::AddToClass(pyclass, "__iter__", (PyCFunction)PyObject_SelfIter, METH_NOARGS);
}

else if (name == "std::basic_string<char>") { // TODO: ask backend as well
else if (name == "std::basic_string<char>" || name == "basic_string<char>") { // TODO: ask backend as well
Utility::AddToClass(pyclass, "__repr__", (PyCFunction)STLStringRepr, METH_NOARGS);
Utility::AddToClass(pyclass, "__str__", (PyCFunction)STLStringStr, METH_NOARGS);
Utility::AddToClass(pyclass, "__bytes__", (PyCFunction)STLStringBytes, METH_NOARGS);
Expand All @@ -1867,12 +1867,12 @@ bool CPyCppyy::Pythonize(PyObject* pyclass, Cppyy::TCppScope_t scope)
((PyTypeObject*)pyclass)->tp_hash = (hashfunc)STLStringHash;
}

else if (name == "std::basic_string_view<char>") {
else if (name == "std::basic_string_view<char>" || name == "basic_string_view<char>") {
Utility::AddToClass(pyclass, "__real_init", "__init__");
Utility::AddToClass(pyclass, "__init__", (PyCFunction)StringViewInit, METH_VARARGS | METH_KEYWORDS);
}

else if (name == "std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >") {
else if (name == "std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >" || name == "basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >") {
Utility::AddToClass(pyclass, "__repr__", (PyCFunction)STLWStringRepr, METH_NOARGS);
Utility::AddToClass(pyclass, "__str__", (PyCFunction)STLWStringStr, METH_NOARGS);
Utility::AddToClass(pyclass, "__bytes__", (PyCFunction)STLWStringBytes, METH_NOARGS);
Expand Down

0 comments on commit 8e2b27c

Please sign in to comment.