Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[PyROOT] Add cppyy synchronization script and update patches
The added script will be used to synchronize `cppyy` and `CPyCppyy`,
applying some patches that are necessary for ROOT.
  • Loading branch information
guitargeek committed Mar 18, 2024
commit c6b238a8b1e850549dad99233f89a5fb4e3bf8dd
23 changes: 0 additions & 23 deletions bindings/pyroot/cppyy/patches/32bit-int-numpy.patch

This file was deleted.

129 changes: 129 additions & 0 deletions bindings/pyroot/cppyy/patches/CPyCppyy-Adapt-to-no-std-in-ROOT.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
From 24b94cde0a5fa6b46be05359b7218af9bb295d87 Mon Sep 17 00:00:00 2001
From: Jonas Rembser <[email protected]>
Date: Tue, 12 Mar 2024 01:59:37 +0100
Subject: [PATCH] [CPyCppyy] Adapt to no `std::` in ROOT

---
.../pyroot/cppyy/CPyCppyy/src/Converters.cxx | 20 +++++++++++--------
.../pyroot/cppyy/CPyCppyy/src/Executors.cxx | 2 ++
.../pyroot/cppyy/CPyCppyy/src/Pythonize.cxx | 8 ++++----
3 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/bindings/pyroot/cppyy/CPyCppyy/src/Converters.cxx b/bindings/pyroot/cppyy/CPyCppyy/src/Converters.cxx
index efd404aebe..4ac294745c 100644
--- a/bindings/pyroot/cppyy/CPyCppyy/src/Converters.cxx
+++ b/bindings/pyroot/cppyy/CPyCppyy/src/Converters.cxx
@@ -3164,7 +3164,7 @@ CPyCppyy::Converter* CPyCppyy::CreateConverter(const std::string& fullType, cdim
}

//-- special case: initializer list
- if (realType.compare(0, 21, "std::initializer_list") == 0) {
+ if (realType.compare(0, 16, "initializer_list") == 0) {
// get the type of the list and create a converter (TODO: get hold of value_type?)
auto pos = realType.find('<');
std::string value_type = realType.substr(pos+1, realType.size()-pos-2);
@@ -3175,8 +3175,9 @@ CPyCppyy::Converter* CPyCppyy::CreateConverter(const std::string& fullType, cdim
bool control = cpd == "&" || isConst;

//-- special case: std::function
- auto pos = resolvedType.find("std::function<");
- if (pos == 0 /* std:: */ || pos == 6 /* const std:: */ ) {
+ auto pos = resolvedType.find("function<");
+ if (pos == 0 /* no std:: */ || pos == 5 /* with std:: */ ||
+ pos == 6 /* const no std:: */ || pos == 11 /* const with std:: */ ) {

// get actual converter for normal passing
Converter* cnv = selectInstanceCnv(
@@ -3184,14 +3185,14 @@ CPyCppyy::Converter* CPyCppyy::CreateConverter(const std::string& fullType, cdim

if (cnv) {
// get the type of the underlying (TODO: use target_type?)
- auto pos1 = resolvedType.find("(", pos+14);
+ auto pos1 = resolvedType.find("(", pos+9);
auto pos2 = resolvedType.rfind(")");
if (pos1 != std::string::npos && pos2 != std::string::npos) {
- auto sz1 = pos1-pos-14;
- if (resolvedType[pos+14+sz1-1] == ' ') sz1 -= 1;
+ auto sz1 = pos1-pos-9;
+ if (resolvedType[pos+9+sz1-1] == ' ') sz1 -= 1;

return new StdFunctionConverter(cnv,
- resolvedType.substr(pos+14, sz1), resolvedType.substr(pos1, pos2-pos1+1));
+ resolvedType.substr(pos+9, sz1), resolvedType.substr(pos1, pos2-pos1+1));
} else if (cnv->HasState())
delete cnv;
}
@@ -3301,7 +3302,7 @@ std::string::size_type dims2stringsz(cdims_t d) {
return (d && d.ndim() != UNKNOWN_SIZE) ? d[0] : std::string::npos;
}

-#define STRINGVIEW "std::basic_string_view<char>"
+#define STRINGVIEW "basic_string_view<char,char_traits<char> >"
#define WSTRING1 "std::basic_string<wchar_t>"
#define WSTRING2 "std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>"

@@ -3484,7 +3485,10 @@ public:
gf["char ptr"] = gf["char*[]"];
gf["std::string"] = (cf_t)+[](cdims_t) { return new STLStringConverter{}; };
gf["const std::string&"] = gf["std::string"];
+ gf["string"] = gf["std::string"];
+ gf["const string&"] = gf["std::string"];
gf["std::string&&"] = (cf_t)+[](cdims_t) { return new STLStringMoveConverter{}; };
+ gf["string&&"] = gf["std::string&&"];
#if __cplusplus > 201402L
gf["std::string_view"] = (cf_t)+[](cdims_t) { return new STLStringViewConverter{}; };
gf[STRINGVIEW] = gf["std::string_view"];
diff --git a/bindings/pyroot/cppyy/CPyCppyy/src/Executors.cxx b/bindings/pyroot/cppyy/CPyCppyy/src/Executors.cxx
index 9692fc8535..70d8f72596 100644
--- a/bindings/pyroot/cppyy/CPyCppyy/src/Executors.cxx
+++ b/bindings/pyroot/cppyy/CPyCppyy/src/Executors.cxx
@@ -1088,7 +1088,9 @@ public:
gf["char16_t*"] = (ef_t)+[](cdims_t) { static CString16Executor e{}; return &e;};
gf["char32_t*"] = (ef_t)+[](cdims_t) { static CString32Executor e{}; return &e;};
gf["std::string"] = (ef_t)+[](cdims_t) { static STLStringExecutor e{}; return &e; };
+ gf["string"] = gf["std::string"];
gf["std::string&"] = (ef_t)+[](cdims_t) { return new STLStringRefExecutor{}; };
+ gf["string&"] = gf["std::string&"];
gf["std::wstring"] = (ef_t)+[](cdims_t) { static STLWStringExecutor e{}; return &e; };
gf[WSTRING1] = gf["std::wstring"];
gf[WSTRING2] = gf["std::wstring"];
diff --git a/bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx b/bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx
index c1720cf3f2..ae0e31cac8 100644
--- a/bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx
+++ b/bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx
@@ -67,7 +67,7 @@ PyObject* GetAttrDirect(PyObject* pyclass, PyObject* pyname) {
inline bool IsTemplatedSTLClass(const std::string& name, const std::string& klass) {
// Scan the name of the class and determine whether it is a template instantiation.
auto pos = name.find(klass);
- return pos == 5 && name.rfind("std::", 0, 5) == 0 && name.find("::", name.rfind(">")) == std::string::npos;
+ return (pos == 0 || pos == 5) && name.find("::", name.rfind(">")) == std::string::npos;
}

// to prevent compiler warnings about const char* -> char*
@@ -1873,7 +1873,7 @@ bool CPyCppyy::Pythonize(PyObject* pyclass, const std::string& name)
Utility::AddToClass(pyclass, "__iter__", (PyCFunction)PyObject_SelfIter, METH_NOARGS);
}

- else if (name == "std::string") { // TODO: ask backend as well
+ else if (name == "string" || name == "std::string") { // 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);
@@ -1894,12 +1894,12 @@ bool CPyCppyy::Pythonize(PyObject* pyclass, const std::string& name)
((PyTypeObject*)pyclass)->tp_hash = (hashfunc)STLStringHash;
}

- else if (name == "std::basic_string_view<char>") {
+ else if (name == "basic_string_view<char>" || name == "std::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 == "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> >") {
Utility::AddToClass(pyclass, "__repr__", (PyCFunction)STLWStringRepr, METH_NOARGS);
Utility::AddToClass(pyclass, "__str__", (PyCFunction)STLWStringStr, METH_NOARGS);
Utility::AddToClass(pyclass, "__bytes__", (PyCFunction)STLWStringBytes, METH_NOARGS);
--
2.44.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
From ca28a3cd6cafa3318f17014aaf1a7346ed860228 Mon Sep 17 00:00:00 2001
From: Jonas Rembser <[email protected]>
Date: Thu, 1 Feb 2024 15:56:05 +0100
Subject: [PATCH] [CPyCppyy] Add converters and executors for ROOT type aliases

---
bindings/pyroot/cppyy/CPyCppyy/src/Converters.cxx | 13 +++++++++++++
bindings/pyroot/cppyy/CPyCppyy/src/Executors.cxx | 10 ++++++++++
2 files changed, 23 insertions(+)

diff --git a/bindings/pyroot/cppyy/CPyCppyy/src/Converters.cxx b/bindings/pyroot/cppyy/CPyCppyy/src/Converters.cxx
index 7f580e858a..b31d27cbb0 100644
--- a/bindings/pyroot/cppyy/CPyCppyy/src/Converters.cxx
+++ b/bindings/pyroot/cppyy/CPyCppyy/src/Converters.cxx
@@ -3418,6 +3418,19 @@ public:
gf["const " CCOMPLEX_D "&"] = gf["const std::complex<double>&"];
gf[CCOMPLEX_F " ptr"] = gf["std::complex<float> ptr"];
gf[CCOMPLEX_D " ptr"] = gf["std::complex<double> ptr"];
+ gf["Long64_t"] = gf["long long"];
+ gf["Long64_t ptr"] = gf["long long ptr"];
+ gf["Long64_t&"] = gf["long long&"];
+ gf["const Long64_t&"] = gf["const long long&"];
+ gf["ULong64_t"] = gf["unsigned long long"];
+ gf["ULong64_t ptr"] = gf["unsigned long long ptr"];
+ gf["ULong64_t&"] = gf["unsigned long long&"];
+ gf["const ULong64_t&"] = gf["const unsigned long long&"];
+ gf["Float16_t"] = gf["float"];
+ gf["const Float16_t&"] = gf["const float&"];
+ gf["Double32_t"] = gf["double"];
+ gf["Double32_t&"] = gf["double&"];
+ gf["const Double32_t&"] = gf["const double&"];

// factories for special cases
gf["nullptr_t"] = (cf_t)+[](cdims_t) { static NullptrConverter c{}; return &c;};
diff --git a/bindings/pyroot/cppyy/CPyCppyy/src/Executors.cxx b/bindings/pyroot/cppyy/CPyCppyy/src/Executors.cxx
index 7354146a60..0185ee8f00 100644
--- a/bindings/pyroot/cppyy/CPyCppyy/src/Executors.cxx
+++ b/bindings/pyroot/cppyy/CPyCppyy/src/Executors.cxx
@@ -1072,6 +1072,16 @@ public:
gf[CCOMPLEX_D "&"] = gf["std::complex<double>&"];
gf[CCOMPLEX_F " ptr"] = gf["std::complex<float> ptr"];
gf[CCOMPLEX_D " ptr"] = gf["std::complex<double> ptr"];
+ gf["Long64_t"] = gf["long long"];
+ gf["Long64_t&"] = gf["long long&"];
+ gf["Long64_t ptr"] = gf["long long ptr"];
+ gf["ULong64_t"] = gf["unsigned long long"];
+ gf["ULong64_t&"] = gf["unsigned long long&"];
+ gf["ULong64_t ptr"] = gf["unsigned long long ptr"];
+ gf["Float16_t"] = gf["float"];
+ gf["Float16_t&"] = gf["float&"];
+ gf["Double32_t"] = gf["double"];
+ gf["Double32_t&"] = gf["double&"];

// factories for special cases
gf["const char*"] = (ef_t)+[](cdims_t) { static CStringExecutor e{}; return &e; };
--
2.44.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
From 70ee90389088b4c62962769d2cafbe185c467972 Mon Sep 17 00:00:00 2001
From: Jonas Rembser <[email protected]>
Date: Fri, 15 Mar 2024 15:35:26 +0100
Subject: [PATCH] [CPyCppyy] Always convert returned `std::string` to Python
string

---
.../pyroot/cppyy/CPyCppyy/src/Executors.cxx | 24 +++++++------------
.../pyroot/cppyy/CPyCppyy/src/Pythonize.cxx | 4 ++++
2 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/bindings/pyroot/cppyy/CPyCppyy/src/Executors.cxx b/bindings/pyroot/cppyy/CPyCppyy/src/Executors.cxx
index 1f3e43152e..70d8f72596 100644
--- a/bindings/pyroot/cppyy/CPyCppyy/src/Executors.cxx
+++ b/bindings/pyroot/cppyy/CPyCppyy/src/Executors.cxx
@@ -389,17 +389,9 @@ PyObject* CPyCppyy::STLStringRefExecutor::Execute(
Cppyy::TCppMethod_t method, Cppyy::TCppObject_t self, CallContext* ctxt)
{
// execute <method> with argument <self, ctxt>, return python string return value
- static Cppyy::TCppScope_t sSTLStringScope = Cppyy::GetScope("std::string");
-
std::string* result = (std::string*)GILCallR(method, self, ctxt);
if (!fAssignable) {
- std::string* rescp = new std::string{*result};
- return BindCppObjectNoCast((void*)rescp, sSTLStringScope, CPPInstance::kIsOwner);
- }
-
- if (!CPyCppyy_PyText_Check(fAssignable)) {
- PyErr_Format(PyExc_TypeError, "wrong type in assignment (string expected)");
- return nullptr;
+ return CPyCppyy_PyText_FromStringAndSize(result->c_str(), result->size());
}

*result = std::string(
@@ -567,14 +559,16 @@ PyObject* CPyCppyy::STLStringExecutor::Execute(
// TODO: make use of GILLCallS (?!)
static Cppyy::TCppScope_t sSTLStringScope = Cppyy::GetScope("std::string");
std::string* result = (std::string*)GILCallO(method, self, ctxt, sSTLStringScope);
- if (!result)
- result = new std::string{};
- else if (PyErr_Occurred()) {
- delete result;
- return nullptr;
+ if (!result) {
+ Py_INCREF(PyStrings::gEmptyString);
+ return PyStrings::gEmptyString;
}

- return BindCppObjectNoCast((void*)result, sSTLStringScope, CPPInstance::kIsOwner);
+ PyObject* pyresult =
+ CPyCppyy_PyText_FromStringAndSize(result->c_str(), result->size());
+ ::operator delete(result); // calls Cppyy::CallO which calls ::operator new
+
+ return pyresult;
}

//----------------------------------------------------------------------------
diff --git a/bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx b/bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx
index 3ab4c8b3a1..ae0e31cac8 100644
--- a/bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx
+++ b/bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx
@@ -1327,6 +1327,7 @@ PyObject* STLStringGetAttr(CPPInstance* self, PyObject* attr_name)
}


+#if 0
PyObject* UTF8Repr(PyObject* self)
{
// force C++ string types conversion to Python str per Python __repr__ requirements
@@ -1348,6 +1349,7 @@ PyObject* UTF8Str(PyObject* self)
Py_DECREF(res);
return str_res;
}
+#endif

Py_hash_t STLStringHash(PyObject* self)
{
@@ -1695,6 +1697,7 @@ bool CPyCppyy::Pythonize(PyObject* pyclass, const std::string& name)
PyObject_SetAttr(pyclass, PyStrings::gNe, top_ne);
}

+#if 0
if (HasAttrDirect(pyclass, PyStrings::gRepr, true)) {
// guarantee that the result of __repr__ is a Python string
Utility::AddToClass(pyclass, "__cpp_repr", "__repr__");
@@ -1706,6 +1709,7 @@ bool CPyCppyy::Pythonize(PyObject* pyclass, const std::string& name)
Utility::AddToClass(pyclass, "__cpp_str", "__str__");
Utility::AddToClass(pyclass, "__str__", (PyCFunction)UTF8Str, METH_NOARGS);
}
+#endif

// This pythonization is disabled for ROOT because it is a bit buggy
#if 0
--
2.44.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
From dda0f91d5f221f6400f587b5d80e1eb4d6e6c48d Mon Sep 17 00:00:00 2001
From: Jonas Rembser <[email protected]>
Date: Mon, 11 Mar 2024 20:03:05 +0100
Subject: [PATCH] [CPyCppyy] Disable initializer-style construction pythonization

---
bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx | 3 +++
1 file changed, 3 insertions(+)

diff --git a/bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx b/bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx
index 3d36c9d09e..deaf5b10d1 100644
--- a/bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx
+++ b/bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx
@@ -1707,6 +1707,8 @@ bool CPyCppyy::Pythonize(PyObject* pyclass, const std::string& name)
Utility::AddToClass(pyclass, "__str__", (PyCFunction)UTF8Str, METH_NOARGS);
}

+ // This pythonization is disabled for ROOT because it is a bit buggy
+#if 0
if (Cppyy::IsAggregate(((CPPClass*)pyclass)->fCppType) && name.compare(0, 5, "std::", 5) != 0) {
// create a pseudo-constructor to allow initializer-style object creation
Cppyy::TCppType_t kls = ((CPPClass*)pyclass)->fCppType;
@@ -1774,6 +1776,7 @@ bool CPyCppyy::Pythonize(PyObject* pyclass, const std::string& name)
}
}
}
+#endif


//- class name based pythonization -------------------------------------------
--
2.44.0

Loading