MCPcopy Create free account
hub / github.com/commontk/CTK / executeFile

Method executeFile

Libs/Scripting/Python/Core/ctkAbstractPythonManager.cpp:325–355  ·  view source on GitHub ↗

-----------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

323
324//-----------------------------------------------------------------------------
325void ctkAbstractPythonManager::executeFile(const QString& filename)
326{
327 PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
328 if (main)
329 {
330 QString path = QFileInfo(filename).absolutePath();
331 // See http://nedbatchelder.com/blog/200711/rethrowing_exceptions_in_python.html
332 // Re-throwing is only needed in Python 2.7
333 QStringList code = QStringList()
334 << "import sys"
335 << QString("sys.path.insert(0, %1)").arg(ctkAbstractPythonManager::toPythonStringLiteral(path))
336 << "_updated_globals = globals()"
337 << QString("_updated_globals['__file__'] = %1").arg(ctkAbstractPythonManager::toPythonStringLiteral(filename))
338#if PY_MAJOR_VERSION >= 3
339 << QString("exec(open(%1).read(), _updated_globals)").arg(ctkAbstractPythonManager::toPythonStringLiteral(filename));
340#else
341 << "_ctk_executeFile_exc_info = None"
342 << "try:"
343 << QString(" execfile(%1, _updated_globals)").arg(ctkAbstractPythonManager::toPythonStringLiteral(filename))
344 << "except Exception as e:"
345 << " _ctk_executeFile_exc_info = sys.exc_info()"
346 << "finally:"
347 << " del _updated_globals"
348 << QString(" if sys.path[0] == %1: sys.path.pop(0)").arg(ctkAbstractPythonManager::toPythonStringLiteral(path))
349 << " if _ctk_executeFile_exc_info:"
350 << " raise _ctk_executeFile_exc_info[1], None, _ctk_executeFile_exc_info[2]";
351#endif
352 this->executeString(code.join("\n"));
353 //PythonQt::self()->handleError(); // Clear errorOccured flag
354 }
355}
356
357//-----------------------------------------------------------------------------
358void ctkAbstractPythonManager::setInitializationFunction(void (*initFunction)())

Callers 4

foreachFunction · 0.80
testExecuteFileMethod · 0.80
testPythonAttributesMethod · 0.80

Calls 4

executeStringMethod · 0.95
QStringListClass · 0.85
joinMethod · 0.80
QStringClass · 0.50

Tested by 3

testExecuteFileMethod · 0.64
testPythonAttributesMethod · 0.64