Skip to content

Commit 3fa11eb

Browse files
committed
[CPyCppyy] Remove Eval() and classes redundant with TPython
The `TPython::Eval()` method was removed some time ago, and replaced with the `Exec()` overload that uses `std::any` for data transfer. So for ROOT, there is no reason to keep the fragile `Eval()` functionality in the future CppJIT. If CppJIT users would need such functionality, we should instead port the `std::any` mechanism to the CPyCppyy API. Remove PyResult, TPyArg and TPyClassGenerator in CPyCppyy, as they are copies of TPython classes. The corresponding TPython class for PyResult is the TPyReturn, and to implement it without forwarding to the former PyResult class, the CPyCppyy API needs to be extended with setters for ownership flags.
1 parent 488ff17 commit 3fa11eb

File tree

10 files changed

+37
-804
lines changed

10 files changed

+37
-804
lines changed

bindings/pyroot/cppyy/CPyCppyy/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
set(headers
88
include/CPyCppyy/API.h
99
include/CPyCppyy/Reflex.h
10-
include/CPyCppyy/PyResult.h
1110
include/CPyCppyy/CommonDefs.h
1211
include/CPyCppyy/PyException.h
1312
include/CPyCppyy/DispatchPtr.h
@@ -38,7 +37,6 @@ set(sources
3837
src/MemoryRegulator.cxx
3938
src/ProxyWrappers.cxx
4039
src/PyException.cxx
41-
src/PyResult.cxx
4240
src/PyStrings.cxx
4341
src/Pythonize.cxx
4442
src/TemplateProxy.cxx

bindings/pyroot/cppyy/CPyCppyy/include/CPyCppyy/API.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ namespace Cppyy {
4040
} // namespace Cppyy
4141

4242
// Bindings
43-
#include "CPyCppyy/PyResult.h"
4443
#include "CPyCppyy/CommonDefs.h"
4544

4645
// Standard
@@ -193,6 +192,10 @@ CPYCPPYY_EXTERN bool Scope_CheckExact(PyObject* pyobject);
193192
CPYCPPYY_EXTERN bool Instance_Check(PyObject* pyobject);
194193
CPYCPPYY_EXTERN bool Instance_CheckExact(PyObject* pyobject);
195194

195+
// memory management: ownership of the underlying C++ object
196+
CPYCPPYY_EXTERN void Instance_SetPythonOwns(PyObject* pyobject);
197+
CPYCPPYY_EXTERN void Instance_SetCppOwns(PyObject* pyobject);
198+
196199
// type verifier for sequences
197200
CPYCPPYY_EXTERN bool Sequence_Check(PyObject* pyobject);
198201

@@ -217,9 +220,6 @@ CPYCPPYY_EXTERN bool Import(const std::string& name);
217220
// execute a python statement (e.g. "import sys")
218221
CPYCPPYY_EXTERN bool Exec(const std::string& cmd);
219222

220-
// evaluate a python expression (e.g. "1+1")
221-
CPYCPPYY_EXTERN const PyResult Eval(const std::string& expr);
222-
223223
// execute a python stand-alone script, with argv CLI arguments
224224
CPYCPPYY_EXTERN void ExecScript(const std::string& name, const std::vector<std::string>& args);
225225

bindings/pyroot/cppyy/CPyCppyy/include/CPyCppyy/PyResult.h

Lines changed: 0 additions & 65 deletions
This file was deleted.

bindings/pyroot/cppyy/CPyCppyy/include/CPyCppyy/TPyArg.h

Lines changed: 0 additions & 52 deletions
This file was deleted.

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

Lines changed: 26 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,32 @@ bool CPyCppyy::Instance_CheckExact(PyObject* pyobject)
196196
return CPPInstance_CheckExact(pyobject);
197197
}
198198

199+
//-----------------------------------------------------------------------------
200+
void CPyCppyy::Instance_SetPythonOwns(PyObject* pyobject)
201+
{
202+
if (!Initialize())
203+
return;
204+
205+
// check validity of cast
206+
if (!CPPInstance_Check(pyobject))
207+
return;
208+
209+
((CPPInstance *)pyobject)->PythonOwns();
210+
}
211+
212+
//-----------------------------------------------------------------------------
213+
void CPyCppyy::Instance_SetCppOwns(PyObject* pyobject)
214+
{
215+
if (!Initialize())
216+
return;
217+
218+
// check validity of cast
219+
if (!CPPInstance_Check(pyobject))
220+
return;
221+
222+
((CPPInstance *)pyobject)->CppOwns();
223+
}
224+
199225
//-----------------------------------------------------------------------------
200226
bool CPyCppyy::Sequence_Check(PyObject* pyobject)
201227
{
@@ -313,10 +339,6 @@ bool CPyCppyy::Import(const std::string& mod_name)
313339
fullname += ".";
314340
fullname += CPyCppyy_PyText_AsString(pyClName);
315341

316-
// force class creation (this will eventually call TPyClassGenerator)
317-
// TODO: the following is broken (and should live in Cppyy.cxx) to
318-
// TClass::GetClass(fullname.c_str(), true);
319-
320342
Py_XDECREF(pyClName);
321343
}
322344

@@ -430,61 +452,6 @@ bool CPyCppyy::Exec(const std::string& cmd)
430452
return false;
431453
}
432454

433-
//-----------------------------------------------------------------------------
434-
const CPyCppyy::PyResult CPyCppyy::Eval(const std::string& expr)
435-
{
436-
// Evaluate a python expression.
437-
//
438-
// Caution: do not hold on to the return value: either store it in a builtin
439-
// type (implicit casting will work), or in a pointer to a cppyy object (explicit
440-
// casting to a void* is required).
441-
if (!Initialize())
442-
return PyResult();
443-
444-
// evaluate the expression
445-
PyObject* result =
446-
PyRun_String(const_cast<char*>(expr.c_str()), Py_eval_input, gMainDict, gMainDict);
447-
448-
// report errors as appropriate; return void
449-
if (!result) {
450-
PyErr_Print();
451-
return PyResult();
452-
}
453-
454-
// results that require no conversion
455-
if (result == Py_None || CPPInstance_Check(result) ||
456-
PyBytes_Check(result) ||
457-
PyFloat_Check(result) || PyLong_Check(result) || PyInt_Check(result))
458-
return PyResult(result);
459-
460-
// explicit conversion for python type required
461-
PyObject* pyclass = (PyObject*)Py_TYPE(result);
462-
463-
// retrieve class name and the module in which it resides
464-
PyObject* name = PyObject_GetAttr(pyclass, PyStrings::gName);
465-
PyObject* module = PyObject_GetAttr(pyclass, PyStrings::gModule);
466-
467-
// concat name
468-
std::string qname =
469-
std::string(CPyCppyy_PyText_AsString(module)) + \
470-
'.' + CPyCppyy_PyText_AsString(name);
471-
Py_DECREF(module);
472-
Py_DECREF(name);
473-
474-
// locate cppyy style class with this name
475-
// TODO: use Cppyy.cxx ...
476-
//TClass* klass = TClass::GetClass(qname.c_str());
477-
void* klass = nullptr;
478-
479-
// construct general cppyy python object that pretends to be of class 'klass'
480-
if (klass)
481-
return PyResult(result);
482-
483-
// no conversion, return null pointer object
484-
Py_DECREF(result);
485-
return PyResult();
486-
}
487-
488455
//-----------------------------------------------------------------------------
489456
void CPyCppyy::Prompt() {
490457
// Enter an interactive python session (exit with ^D). State is preserved

0 commit comments

Comments
 (0)