static qreal ec_call(int ec, qreal v)
{
    PyObject *res_obj;
    qreal res = 0.0;

    SIP_BLOCK_THREADS

    res_obj = PyObject_CallFunction(ec_custom_types[ec].py_func, (char *)"(d)", (double)v);

    if (res_obj)
    {
        PyErr_Clear();

        res = PyFloat_AsDouble(res_obj);
        Py_DECREF(res_obj);

        if (PyErr_Occurred())
            res_obj = 0;
    }

    if (!res_obj)
        pyqt5_err_print();

    SIP_UNBLOCK_THREADS

    return res;
}
// Serialise operator.
QDataStream &operator<<(QDataStream &out, const PyQt_PyObject &obj)
{
    PyObject *ser_obj = 0;
    const char *ser = 0;
    uint len = 0;

    if (obj.pyobject)
    {
        static PyObject *dumps = 0;

        SIP_BLOCK_THREADS

        if (!dumps)
        {
            PyObject *pickle = PyImport_ImportModule("pickle");

            if (pickle)
            {
                dumps = PyObject_GetAttrString(pickle, "dumps");
                Py_DECREF(pickle);
            }
        }

        if (dumps)
        {
            if (!qpycore_pickle_protocol)
            {
                Py_INCREF(Py_None);
                qpycore_pickle_protocol = Py_None;
            }

            ser_obj = PyObject_CallFunctionObjArgs(dumps, obj.pyobject,
                    qpycore_pickle_protocol, 0);

            if (ser_obj)
            {
                if (SIPBytes_Check(ser_obj))
                {
                    ser = SIPBytes_AS_STRING(ser_obj);
                    len = SIPBytes_GET_SIZE(ser_obj);
                }
                else
                {
                    Py_DECREF(ser_obj);
                    ser_obj = 0;
                }
            }
            else
            {
                pyqt5_err_print();
            }
        }

        SIP_UNBLOCK_THREADS
    }

    out.writeBytes(ser, len);

    if (ser_obj)
    {
        SIP_BLOCK_THREADS
        Py_DECREF(ser_obj);
        SIP_UNBLOCK_THREADS
    }

    return out;
}
Example #3
0
// Return the current Python context.  This should be called with the GIL.
int qpycore_current_context(const char **file, const char **function)
{
    static PyObject *currentframe = 0;
    static PyObject *getframeinfo = 0;
    static PyObject *saved_file = 0;
    static PyObject *saved_function = 0;

    PyObject *frame, *info, *file_obj, *linenr_obj, *function_obj;
    int linenr;

    frame = info = NULL;

    // Make sure we have what we need from the inspect module.
    if (!currentframe || !getframeinfo)
    {
        PyObject *inspect = PyImport_ImportModule("inspect");

        if (inspect)
        {
            if (!currentframe)
                currentframe = PyObject_GetAttrString(inspect, "currentframe");

            if (!getframeinfo)
                getframeinfo = PyObject_GetAttrString(inspect, "getframeinfo");

            Py_DECREF(inspect);
        }

        if (!currentframe || !getframeinfo)
            goto py_error;
    }

    // Get the current frame.
    if ((frame = PyObject_CallFunctionObjArgs(currentframe, NULL)) == NULL)
        goto py_error;

    // Get the frame details.
    if ((info = PyObject_CallFunctionObjArgs(getframeinfo, frame, NULL)) == NULL)
        goto py_error;;

    if ((file_obj = PyTuple_GetItem(info, 0)) == NULL)
        goto py_error;

    if ((linenr_obj = PyTuple_GetItem(info, 1)) == NULL)
        goto py_error;

    if ((function_obj = PyTuple_GetItem(info, 2)) == NULL)
        goto py_error;

    Py_XDECREF(saved_file);
#if PY_MAJOR_VERSION >= 3
    saved_file = PyUnicode_AsEncodedString(file_obj, "latin_1", "ignore");
#else
    saved_file = file_obj;
    Py_INCREF(saved_file);
#endif
    *file = SIPBytes_AS_STRING(saved_file);

    linenr = SIPLong_AsLong(linenr_obj);

    Py_XDECREF(saved_function);
#if PY_MAJOR_VERSION >= 3
    saved_function = PyUnicode_AsEncodedString(function_obj, "latin_1", "ignore");
#else
    saved_function = function_obj;
    Py_INCREF(saved_function);
#endif
    *function = SIPBytes_AS_STRING(saved_function);

    Py_DECREF(info);
    Py_DECREF(frame);

    return linenr;

py_error:
    Py_XDECREF(info);
    Py_XDECREF(frame);

    pyqt5_err_print();

    *file = *function = "";
    return 0;
}