PyObject *
py_download_url(G_GNUC_UNUSED PyObject *self, PyObject *args)
{
    gboolean ret;
    PyObject *py_handle;
    LrHandle *handle = NULL;
    char *url;
    int fd;
    GError *tmp_err = NULL;
    PyThreadState *state = NULL;

    if (!PyArg_ParseTuple(args, "Osi:download_url",
                          &py_handle, &url, &fd))
        return NULL;

    if (HandleObject_Check(py_handle)) {
        handle = Handle_FromPyObject(py_handle);
        Handle_SetThreadState(py_handle, &state);
    } else if (py_handle != Py_None) {
        PyErr_SetString(PyExc_TypeError, "Only Handle or None is supported");
        return NULL;
    }

    // XXX: GIL Hack
    int hack_rc = gil_logger_hack_begin(&state);
    if (hack_rc == GIL_HACK_ERROR)
        return NULL;

    BeginAllowThreads(&state);
    ret = lr_download_url(handle, url, fd, &tmp_err);
    EndAllowThreads(&state);

    // XXX: GIL Hack
    if (!gil_logger_hack_end(hack_rc))
        return NULL;

    assert((ret && !tmp_err) || (!ret && tmp_err));

    if (ret)
        Py_RETURN_NONE; // All fine - Return None

    // Error occurred
    if (PyErr_Occurred()) {
        // Python exception occurred (in a python callback probably)
        return NULL;
    } else if(tmp_err->code == LRE_INTERRUPTED) {
        // Interrupted by Ctr+C
        g_error_free(tmp_err);
        PyErr_SetInterrupt();
        PyErr_CheckSignals();
        return NULL;
    } else {
        // Return exception created from GError
        RETURN_ERROR(&tmp_err, -1, NULL);
    }
}
Example #2
0
void
PackageTarget_SetThreadState(PyObject *o, PyThreadState **state)
{
    _PackageTargetObject *self = (_PackageTargetObject *) o;
    if (!self) return;
    self->state = state;

    // XXX: Little tricky (but not so much)
    // Set the state to the depending handle as well
    // (Needed when fastestmirrorcb is used)
    if (self->handle) {
        Handle_SetThreadState(self->handle, state);
    }
}