static int s_s3_request_on_body()

in source/s3_meta_request.c [152:215]


static int s_s3_request_on_body(
    struct aws_s3_meta_request *meta_request,
    const struct aws_byte_cursor *body,
    uint64_t range_start,
    void *user_data) {
    (void)meta_request;
    struct s3_meta_request_binding *request_binding = user_data;

    bool report_progress;
    if (s_record_progress(request_binding, (uint64_t)body->len, &report_progress)) {
        return AWS_OP_ERR;
    }
    if (request_binding->recv_file) {
        /* The callback will be invoked with the right order, so we don't need to seek first. */
        if (fwrite((void *)body->ptr, body->len, 1, request_binding->recv_file) < 1) {
            return aws_translate_and_raise_io_error(errno);
        }
        if (!report_progress) {
            return AWS_OP_SUCCESS;
        }
    }
    bool error = true;
    /*************** GIL ACQUIRE ***************/
    PyGILState_STATE state;
    PyObject *result = NULL;
    if (aws_py_gilstate_ensure(&state)) {
        return AWS_OP_ERR; /* Python has shut down. Nothing matters anymore, but don't crash */
    }
    if (!request_binding->recv_file) {
        result = PyObject_CallMethod(
            request_binding->py_core,
            "_on_body",
            "(y#K)",
            (const char *)(body->ptr),
            (Py_ssize_t)body->len,
            range_start);

        if (!result) {
            PyErr_WriteUnraisable(request_binding->py_core);
            goto done;
        }
        Py_DECREF(result);
    }
    if (report_progress) {
        /* Hold the GIL before enterring here */
        result =
            PyObject_CallMethod(request_binding->py_core, "_on_progress", "(K)", request_binding->size_transferred);
        if (!result) {
            PyErr_WriteUnraisable(request_binding->py_core);
        } else {
            Py_DECREF(result);
        }
        request_binding->size_transferred = 0;
    }
    error = false;
done:
    PyGILState_Release(state);
    /*************** GIL RELEASE ***************/
    if (error) {
        return aws_raise_error(AWS_ERROR_CRT_CALLBACK_EXCEPTION);
    } else {
        return AWS_OP_SUCCESS;
    }
}