diff options
author | Valentin Bartenev <vbart@nginx.com> | 2019-11-14 17:48:48 +0300 |
---|---|---|
committer | Valentin Bartenev <vbart@nginx.com> | 2019-11-14 17:48:48 +0300 |
commit | c667bb446d866f1a1a786fa9a3b3ecd06d163cfb (patch) | |
tree | cb8317fba035b069fc6368656444d2b95f676e95 | |
parent | cd6cb02be08906e28738d62485cdc3d9d75592bb (diff) | |
download | unit-c667bb446d866f1a1a786fa9a3b3ecd06d163cfb.tar.gz unit-c667bb446d866f1a1a786fa9a3b3ecd06d163cfb.tar.bz2 |
Python: refactored nxt_python_request_handler().
Diffstat (limited to '')
-rw-r--r-- | src/nxt_python_wsgi.c | 87 |
1 files changed, 31 insertions, 56 deletions
diff --git a/src/nxt_python_wsgi.c b/src/nxt_python_wsgi.c index a4b5a1f5..16298c05 100644 --- a/src/nxt_python_wsgi.c +++ b/src/nxt_python_wsgi.c @@ -410,59 +410,56 @@ 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(); + if (nxt_fast_path(iterator != NULL)) { + rc = NXT_UNIT_OK; - goto fail; - } + while (run_ctx.bytes_sent < run_ctx.content_length) { + item = PyIter_Next(iterator); - while (run_ctx.bytes_sent < run_ctx.content_length) { - item = PyIter_Next(iterator); + if (item == NULL) { + if (nxt_slow_path(PyErr_Occurred() != NULL)) { + nxt_unit_req_error(req, "Python failed to iterate over " + "the application response object"); + PyErr_Print(); - if (item == NULL) { - if (nxt_slow_path(PyErr_Occurred() != NULL)) { - nxt_unit_req_error(req, "Python failed to iterate over " - "the application response object"); - PyErr_Print(); + rc = NXT_UNIT_ERROR; + } - goto fail; + break; } - break; - } + if (nxt_fast_path(PyBytes_Check(item))) { + rc = nxt_python_write(&run_ctx, item); - if (nxt_slow_path(!PyBytes_Check(item))) { - nxt_unit_req_error(req, "the application returned " - "not a bytestring object"); + } else { + nxt_unit_req_error(req, "the application returned " + "not a bytestring object"); + rc = NXT_UNIT_ERROR; + } - goto fail; - } + Py_DECREF(item); - rc = nxt_python_write(&run_ctx, item); - if (nxt_slow_path(rc != NXT_UNIT_OK)) { - goto fail; + if (nxt_slow_path(rc != NXT_UNIT_OK)) { + break; + } } - Py_DECREF(item); - } + Py_DECREF(iterator); + + } else { + nxt_unit_req_error(req, + "the application returned not an iterable object"); + PyErr_Print(); - Py_DECREF(iterator); + rc = NXT_UNIT_ERROR; + } if (PyObject_HasAttrString(result, "close")) { PyObject_CallMethod(result, (char *) "close", NULL); @@ -471,28 +468,6 @@ nxt_python_request_handler(nxt_unit_request_info_t *req) 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); - - rc = NXT_UNIT_ERROR; - done: nxt_python_thread_state = PyEval_SaveThread(); |