Example #1
0
bool PythonEngine::runExpressionConsole(const QString &expression)
{
    bool successfulRun = runExpression(expression);
    emit executedScript();

    return successfulRun;
}
Example #2
0
PythonBrowserView::PythonBrowserView(PythonEngine *pythonEngine, PythonScriptingConsole *console, QWidget *parent)
    : QDockWidget(tr("Browser"), parent), pythonEngine(pythonEngine), console(console)
{
    setMinimumWidth(280);
    setObjectName("BrowserView");

    connect(pythonEngine, SIGNAL(executedExpression()), this, SLOT(executed()));
    connect(pythonEngine, SIGNAL(executedScript()), this, SLOT(executed()));

    trvBrowser = new QTreeWidget(this);
    trvBrowser->setContextMenuPolicy(Qt::CustomContextMenu);
    trvBrowser->setColumnCount(3);
    trvBrowser->setIndentation(18);
    trvBrowser->setColumnWidth(0, 200);
    trvBrowser->setColumnWidth(0, 200);

    QStringList headers;
    headers << tr("Name") << tr("Type") << tr("Value");
    trvBrowser->setHeaderLabels(headers);

    trvVariables = NULL;
    trvFunctions = NULL;
    trvClasses = NULL;
    trvOther = NULL;

    variableExpanded = true;
    functionExpanded = true;
    classExpanded = true;
    otherExpanded = false;

    executed();
    trvBrowser->sortItems(0, Qt::AscendingOrder);

    connect(trvBrowser, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(executeCommand(QTreeWidgetItem *, int)));
    connect(trvBrowser, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(doContextMenu(const QPoint &)));

    actCopyName = new QAction(icon(""), tr("Copy name"), this);
    connect(actCopyName, SIGNAL(triggered()), this, SLOT(copyName()));

    actCopyValue = new QAction(icon(""), tr("Copy value"), this);
    connect(actCopyValue, SIGNAL(triggered()), this, SLOT(copyValue()));

    actDelete = new QAction(icon(""), tr("&Delete"), this);
    connect(actDelete, SIGNAL(triggered()), this, SLOT(deleteVariable()));

    mnuContext = new QMenu(trvBrowser);
    mnuContext->addAction(actCopyName);
    mnuContext->addAction(actCopyValue);
    mnuContext->addSeparator();
    mnuContext->addAction(actDelete);

    setWidget(trvBrowser);
}
Example #3
0
ScriptResult PythonEngine::runPythonScript(const QString &script, const QString &fileName)
{
    m_isRunning = true;
    m_stdOut = "";

    QSettings settings;
    // enable user module deleter
    if (settings.value("PythonEngine/UserModuleDeleter", true).toBool())
        deleteUserModules();

    runPythonHeader();

    PyObject *output = NULL;
    if (QFile::exists(fileName))
    {
        QString str = QString("from os import chdir; chdir(u'" + QFileInfo(fileName).absolutePath() + "')");
        PyRun_String(str.toStdString().c_str(), Py_single_input, m_dict, m_dict);
    }
    // compile
    PyObject *code = Py_CompileString(script.toStdString().c_str(), fileName.toStdString().c_str(), Py_file_input);
    // run
    if (code) output = PyEval_EvalCode((PyCodeObject *) code, m_dict, m_dict);

    ScriptResult scriptResult;
    if (output)
    {
        scriptResult.isError = false;
        scriptResult.text = m_stdOut.trimmed();
    }
    else
    {
        scriptResult = parseError();
    }
    Py_XDECREF(output);

    m_isRunning = false;

    emit executedScript();

    return scriptResult;
}
Example #4
0
bool PythonEngine::runScript(const QString &script, const QString &fileName, bool useProfiler)
{
    m_isScriptRunning = true;

    PyGILState_STATE gstate = PyGILState_Ensure();

    emit startedScript();

    bool successfulRun = false;

    QSettings settings;
    // enable user module deleter
    if (settings.value("PythonEngine/UserModuleDeleter", true).toBool())
        deleteUserModules();

    runPythonHeader();

    PyObject *output = NULL;
    if (QFile::exists(fileName))
    {
        QString str = QString("from os import chdir; chdir(u'" + QFileInfo(fileName).absolutePath() + "')");
#pragma omp critical(import)
        {
            PyObject *import = PyRun_String(str.toLatin1().data(), Py_single_input, m_dict, m_dict);
            Py_XDECREF(import);
        }
    }

    // compile
    PyObject *code = Py_CompileString(script.toLatin1().data(), fileName.toLatin1().data(), Py_file_input);
    // run
    if (useProfiler)
    {
        setProfilerFileName(fileName);
        startProfiler();
    }
    if (code) output = PyEval_EvalCode((PyCodeObject *) code, m_dict, m_dict);
    if (useProfiler)
        finishProfiler();

    if (output)
    {
        successfulRun = true;
        Py_XDECREF(output);
    }
    else
    {
        // error traceback
        Py_XDECREF(errorType);
        Py_XDECREF(errorValue);
        Py_XDECREF(errorTraceback);
        PyErr_Fetch(&errorType, &errorValue, &errorTraceback);
        if (errorTraceback)
            successfulRun = false;
    }

    Py_XDECREF(code);

    m_isScriptRunning = false;

    // release the thread, no Python API allowed beyond this point
    PyGILState_Release(gstate);

    emit executedScript();

    return successfulRun;
}