Python: refactored nxt_python_request_handler().
This commit is contained in:
@@ -410,27 +410,15 @@ nxt_python_request_handler(nxt_unit_request_info_t *req)
|
||||
goto done;
|
||||
}
|
||||
|
||||
item = NULL;
|
||||
iterator = NULL;
|
||||
|
||||
/* Shortcut: avoid iterate over result string symbols. */
|
||||
if (PyBytes_Check(result)) {
|
||||
|
||||
rc = nxt_python_write(&run_ctx, result);
|
||||
if (nxt_slow_path(rc != NXT_UNIT_OK)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
} else {
|
||||
iterator = PyObject_GetIter(result);
|
||||
|
||||
if (nxt_slow_path(iterator == NULL)) {
|
||||
nxt_unit_req_error(req, "the application returned "
|
||||
"not an iterable object");
|
||||
PyErr_Print();
|
||||
|
||||
goto fail;
|
||||
}
|
||||
if (nxt_fast_path(iterator != NULL)) {
|
||||
rc = NXT_UNIT_OK;
|
||||
|
||||
while (run_ctx.bytes_sent < run_ctx.content_length) {
|
||||
item = PyIter_Next(iterator);
|
||||
@@ -441,57 +429,44 @@ nxt_python_request_handler(nxt_unit_request_info_t *req)
|
||||
"the application response object");
|
||||
PyErr_Print();
|
||||
|
||||
goto fail;
|
||||
rc = NXT_UNIT_ERROR;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (nxt_slow_path(!PyBytes_Check(item))) {
|
||||
if (nxt_fast_path(PyBytes_Check(item))) {
|
||||
rc = nxt_python_write(&run_ctx, item);
|
||||
|
||||
} else {
|
||||
nxt_unit_req_error(req, "the application returned "
|
||||
"not a bytestring object");
|
||||
|
||||
goto fail;
|
||||
rc = NXT_UNIT_ERROR;
|
||||
}
|
||||
|
||||
rc = nxt_python_write(&run_ctx, item);
|
||||
Py_DECREF(item);
|
||||
|
||||
if (nxt_slow_path(rc != NXT_UNIT_OK)) {
|
||||
goto fail;
|
||||
break;
|
||||
}
|
||||
|
||||
Py_DECREF(item);
|
||||
}
|
||||
|
||||
Py_DECREF(iterator);
|
||||
|
||||
if (PyObject_HasAttrString(result, "close")) {
|
||||
PyObject_CallMethod(result, (char *) "close", NULL);
|
||||
}
|
||||
}
|
||||
|
||||
Py_DECREF(result);
|
||||
|
||||
rc = NXT_UNIT_OK;
|
||||
|
||||
goto done;
|
||||
|
||||
fail:
|
||||
|
||||
if (item != NULL) {
|
||||
Py_DECREF(item);
|
||||
}
|
||||
|
||||
if (iterator != NULL) {
|
||||
Py_DECREF(iterator);
|
||||
}
|
||||
|
||||
if (PyObject_HasAttrString(result, "close")) {
|
||||
PyObject_CallMethod(result, (char *) "close", NULL);
|
||||
}
|
||||
|
||||
Py_DECREF(result);
|
||||
} else {
|
||||
nxt_unit_req_error(req,
|
||||
"the application returned not an iterable object");
|
||||
PyErr_Print();
|
||||
|
||||
rc = NXT_UNIT_ERROR;
|
||||
}
|
||||
|
||||
if (PyObject_HasAttrString(result, "close")) {
|
||||
PyObject_CallMethod(result, (char *) "close", NULL);
|
||||
}
|
||||
}
|
||||
|
||||
Py_DECREF(result);
|
||||
|
||||
done:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user