Skip to content

Commit

Permalink
Merge pull request #1543 from dgelessus/python_cheat_arg_syntax
Browse files Browse the repository at this point in the history
Make `Python.Cheat` command more usable by not parsing its argument
  • Loading branch information
Hoikas authored Dec 16, 2023
2 parents 150c7d0 + 886fa15 commit 41e605f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 70 deletions.
12 changes: 2 additions & 10 deletions Sources/Plasma/FeatureLib/pfConsole/pfConsoleCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6124,16 +6124,8 @@ PF_CONSOLE_CMD( Python,
"Run a cheat command" )
{
const ST::string& function = params[0];
ST::string args;
if (numParams > 1)
{
args = ST::format("({},)", static_cast<const ST::string&>(params[1]));
}
else
args = "()";

PythonInterface::RunFunctionSafe("xCheat", function.c_str(), args.c_str());

const ST::string& args = numParams > 1 ? params[1] : ST::string();
PythonInterface::RunFunctionStringArg("xCheat", function.c_str(), args);
// get the messages
PrintString(PythonInterface::getOutputAndReset());
}
Expand Down
64 changes: 8 additions & 56 deletions Sources/Plasma/FeatureLib/pfPython/cyPythonInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1724,69 +1724,21 @@ 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;
}

PyObject* PythonInterface::ParseArgs(const char* args)
{
PyObject* result = nullptr;
PyObject* scope = PyDict_New();
if (scope)
{
//- Py_eval_input makes this function accept only single expresion (not statement)
//- When using empty scope, functions and classes like 'file' or '__import__' are not visible
result = PyRun_String(args, Py_eval_input, scope, nullptr);
Py_DECREF(scope);
}

return result;
}

bool PythonInterface::RunFunctionSafe(const char* module, const char* function, const char* args)
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* argsObj = ParseArgs(args);
if (argsObj)
{
PyObject* callResult = RunFunction(moduleObj, function, argsObj);
if (callResult)
{
if (moduleObj) {
pyObjectRef functionObj = PyObject_GetAttrString(moduleObj.Get(), name);
if (functionObj) {
pyObjectRef callResult = plPython::CallObject(functionObj, arg);
if (callResult) {
result = true;
Py_DECREF(callResult);
}

Py_DECREF(argsObj);
}
Py_DECREF(moduleObj);
}

if (!result)
{
if (!result) {
PyErr_Print();
}

Expand Down
5 changes: 1 addition & 4 deletions Sources/Plasma/FeatureLib/pfPython/cyPythonInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,8 @@ 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);

static PyObject* ParseArgs(const char* args);

static bool RunFunctionSafe(const char* module, const char* function, const char* args);
/////////////////////////////////////////////////////////////////////////////
//
// Function : GetpyKeyFromPython
Expand Down

0 comments on commit 41e605f

Please sign in to comment.