Skip to content

Commit

Permalink
Reimplement RunFunctionStringArg using modern plPython::CallObject API
Browse files Browse the repository at this point in the history
  • Loading branch information
dgelessus committed Dec 15, 2023
1 parent a6041ba commit 886fa15
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 40 deletions.
44 changes: 6 additions & 38 deletions Sources/Plasma/FeatureLib/pfPython/cyPythonInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1724,50 +1724,18 @@ bool PythonInterface::RunPYC(PyObject* code, PyObject* module)
return true;
}

/////////////////////////////////////////////////////////////////////////////
//
// Function : RunFunction
// PARAMETERS : module - module name to run 'name' in
// : name - name of function
// : args - tuple with arguments
//
//
PyObject* PythonInterface::RunFunction(PyObject* module, const char* name, PyObject* args)
{
if (module == nullptr)
return nullptr;

PyObject* function = PyObject_GetAttrString(module, name);

PyObject* result = nullptr;
if (function != nullptr)
{
result = PyObject_Call(function, args, nullptr);
Py_DECREF(function);
}

return result;
}

bool PythonInterface::RunFunctionStringArg(const char* module, const char* name, const ST::string& arg)
{
PyObject* moduleObj = ImportModule(module);
pyObjectRef moduleObj = ImportModule(module);
bool result = false;
if (moduleObj) {
PyObject* argObj = PyUnicode_FromSTString(arg);
if (argObj) {
PyObject* argsObj = PyTuple_Pack(1, argObj);
if (argsObj) {
PyObject* callResult = RunFunction(moduleObj, name, argsObj);
if (callResult) {
result = true;
Py_DECREF(callResult);
}
Py_DECREF(argsObj);
pyObjectRef functionObj = PyObject_GetAttrString(moduleObj.Get(), name);
if (functionObj) {
pyObjectRef callResult = plPython::CallObject(functionObj, arg);
if (callResult) {
result = true;
}
Py_DECREF(argObj);
}
Py_DECREF(moduleObj);
}

if (!result) {
Expand Down
2 changes: 0 additions & 2 deletions Sources/Plasma/FeatureLib/pfPython/cyPythonInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,6 @@ class PythonInterface
//
static bool RunPYC(PyObject* code, PyObject* module);

static PyObject* RunFunction(PyObject* module, const char* name, PyObject* args);

static bool RunFunctionStringArg(const char* module, const char* name, const ST::string& arg);

/////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 886fa15

Please sign in to comment.