bool PythonScript::exec()
{
    if (isFunction) compiled = notCompiled;
    if (compiled != Script::isCompiled && !compile(false))
        return false;
    PyObject *pyret;
    beginStdoutRedirect();
    if (PyCallable_Check(PyCode)) {
        PyObject *empty_tuple = PyTuple_New(0);
        if (!empty_tuple) {
            emit_error(env()->errorMsg(), 0);
            return false;
        }
        pyret = PyObject_Call(PyCode,empty_tuple,localDict);
        Py_DECREF(empty_tuple);
    } else {
        pyret = PyEval_EvalCode((PyCodeObject*)PyCode, env()->globalDict(), localDict);
        //pyret = PyRun_String(Code, 0, env()->globalDict(), localDict);
    }

    endStdoutRedirect();
    if (pyret) {
        Py_DECREF(pyret);
        return true;
    }
    emit_error(env()->errorMsg(), 0);
    return false;
}
Example #2
0
/**
 * Executes the compiled code object. If NULL nothing happens
 * @param compiledCode An object that has been compiled from a code fragment
 * @return The result python object
 */
PyObject* PythonScript::executeCompiledCode(PyObject *compiledCode)
{
    PyObject *result(NULL);
    if(!compiledCode) return result;

    InstallTrace traceInstall(*this);
    beginStdoutRedirect();
    result = PyEval_EvalCode((PyCodeObject*)compiledCode, localDict, localDict);
    endStdoutRedirect();
    return result;
}
Example #3
0
/**
 * Evaluate the code and return the value
 * @return
 */
QVariant PythonScript::evaluateImpl() {
  ScopedPythonGIL lock;
  PyObject *compiledCode = this->compileToByteCode(true);
  if (!compiledCode) {
    return QVariant("");
  }
  PyObject *pyret;
  beginStdoutRedirect();
  if (PyCallable_Check(compiledCode)) {
    PyObject *empty_tuple = PyTuple_New(0);
    pyret = PyObject_Call(compiledCode, empty_tuple, localDict);
    Py_DECREF(empty_tuple);
  } else {
    pyret = PyEval_EvalCode(CODE_OBJECT(compiledCode), localDict, localDict);
  }
  endStdoutRedirect();
  if (!pyret) {
    if (PyErr_ExceptionMatches(PyExc_ValueError) ||
        PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
      PyErr_Clear(); // silently ignore errors
      return QVariant("");
    } else {
      emit_error();
      return QVariant();
    }
  }

  QVariant qret = QVariant();
  /* None */
  if (pyret == Py_None) {
    qret = QVariant("");
  }
  /* numeric types */
  else if (PyFloat_Check(pyret)) {
    qret = QVariant(PyFloat_AS_DOUBLE(pyret));
  } else if (INT_CHECK(pyret)) {
    qret = QVariant((qlonglong)TO_LONG(pyret));
  }
#if !defined(IS_PY3K)
  else if (PyLong_Check(pyret)) {
    qret = QVariant((qlonglong)PyLong_AsLongLong(pyret));
  }
#endif
  else if (PyNumber_Check(pyret)) {
    PyObject *number = PyNumber_Float(pyret);
    if (number) {
      qret = QVariant(PyFloat_AS_DOUBLE(number));
      Py_DECREF(number);
    }
  }
  /* bool */
  else if (PyBool_Check(pyret)) {
    qret = QVariant(pyret == Py_True);
  }
  // could handle advanced types (such as PyList->QValueList) here if needed
  /* fallback: try to convert to (unicode) string */
  if (!qret.isValid()) {
#if defined(IS_PY3K)
    // In 3 everything is unicode
    PyObject *pystring = PyObject_Str(pyret);
    if (pystring) {
      qret = QVariant(QString::fromUtf8(_PyUnicode_AsString(pystring)));
    }
#else
    PyObject *pystring = PyObject_Unicode(pyret);
    if (pystring) {
      PyObject *asUTF8 =
          PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(pystring),
                               (int)PyUnicode_GET_DATA_SIZE(pystring), nullptr);
      Py_DECREF(pystring);
      if (asUTF8) {
        qret = QVariant(QString::fromUtf8(PyString_AS_STRING(asUTF8)));
        Py_DECREF(asUTF8);
      } else if ((pystring = PyObject_Str(pyret))) {
        qret = QVariant(QString(PyString_AS_STRING(pystring)));
        Py_DECREF(pystring);
      }
    }
#endif
  }
  Py_DECREF(pyret);
  if (PyErr_Occurred()) {
    if (PyErr_ExceptionMatches(PyExc_ValueError) ||
        PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
      PyErr_Clear(); // silently ignore errors
      return QVariant("");
    } else {
      emit_error();
    }
    return QVariant();
  }
  return qret;
}
QVariant PythonScript::eval()
{
    if (!isFunction) compiled = notCompiled;
    if (compiled != isCompiled && !compile(true))
        return QVariant();

    PyObject *pyret;
    beginStdoutRedirect();
    if (PyCallable_Check(PyCode)) {
        PyObject *empty_tuple = PyTuple_New(0);
        pyret = PyObject_Call(PyCode, empty_tuple, localDict);
        Py_DECREF(empty_tuple);
    } else
        pyret = PyEval_EvalCode((PyCodeObject*)PyCode, env()->globalDict(), localDict);
    endStdoutRedirect();
    if (!pyret) {
        if (PyErr_ExceptionMatches(PyExc_ValueError) ||
                PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
            PyErr_Clear(); // silently ignore errors
            return  QVariant("");
        } else {
            emit_error(env()->errorMsg(), 0);
            return QVariant();
        }
    }

    QVariant qret = QVariant();
    /* None */
    if (pyret == Py_None)
        qret = QVariant("");
    /* numeric types */
    else if (PyFloat_Check(pyret))
        qret = QVariant(PyFloat_AS_DOUBLE(pyret));
    else if (PyInt_Check(pyret))
        qret = QVariant((qlonglong)PyInt_AS_LONG(pyret));
    else if (PyLong_Check(pyret))
        qret = QVariant((qlonglong)PyLong_AsLongLong(pyret));
    else if (PyNumber_Check(pyret)) {
        PyObject *number = PyNumber_Float(pyret);
        if (number) {
            qret = QVariant(PyFloat_AS_DOUBLE(number));
            Py_DECREF(number);
        }
        /* bool */
    } else if (PyBool_Check(pyret))
        qret = QVariant(pyret==Py_True, 0);
    // could handle advanced types (such as PyList->QValueList) here if needed
    /* fallback: try to convert to (unicode) string */
    if(!qret.isValid()) {
        PyObject *pystring = PyObject_Unicode(pyret);
        if (pystring) {
            PyObject *asUTF8 = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(pystring), PyUnicode_GET_DATA_SIZE(pystring), 0);
            Py_DECREF(pystring);
            if (asUTF8) {
                qret = QVariant(QString::fromUtf8(PyString_AS_STRING(asUTF8)));
                Py_DECREF(asUTF8);
            } else if (pystring = PyObject_Str(pyret)) {
                qret = QVariant(QString(PyString_AS_STRING(pystring)));
                Py_DECREF(pystring);
            }
        }
    }

    Py_DECREF(pyret);
    if (PyErr_Occurred()) {
        if (PyErr_ExceptionMatches(PyExc_ValueError) ||
                PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
            PyErr_Clear(); // silently ignore errors
            return  QVariant("");
        } else {
            emit_error(env()->errorMsg(), 0);
            return QVariant();
        }
    } else
        return qret;
}