Skip to content

Commit 7f0d13a

Browse files
committed
Added additional error handling to python runner.
1 parent b111c7a commit 7f0d13a

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

PyLib/PythonRunner.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,17 @@ void CPythonRunner::runFile(QString fileName)
124124

125125
PyObject* obj = Py_BuildValue("s", szfile);
126126
FILE* file = _Py_fopen_obj(obj, "r+");
127-
if (file == nullptr) emit runFileFinished(false);
128-
129-
PyRun_SimpleFile(file, szfile);
130-
127+
if (file == nullptr)
128+
{
129+
emit runFileFinished(false);
130+
return;
131+
}
132+
133+
int result = PyRun_SimpleFile(file, szfile);
134+
emit runFileFinished(result == 0);
135+
#else
136+
emit runFileFinished(false);
131137
#endif
132-
emit runFileFinished(true);
133138
}
134139

135140
#ifdef HAS_PYTHON
@@ -260,14 +265,17 @@ void CPythonRunner::runScript(QString script)
260265
}
261266

262267
std::string s = script.toStdString();
263-
PyRun_SimpleString(s.c_str());
268+
int result = PyRun_SimpleString(s.c_str());
264269

270+
emit runScriptFinished(result == 0);
271+
#else
272+
emit runScriptFinished(false);
265273
#endif
266-
emit runScriptFinished(true);
267274
}
268275

269276
void CPythonRunner::setPythonCWD(const std::string& cwd)
270277
{
271-
std::string changeCWD = "import os\nos.chdir('" + cwd + "')\n";
272-
py::exec(changeCWD.c_str());
278+
#ifdef HAS_PYTHON
279+
py::module_::import("os").attr("chdir")(cwd);
280+
#endif
273281
}

0 commit comments

Comments
 (0)