From cebec46353b3a185f96520e012cbf52e40238cae Mon Sep 17 00:00:00 2001 From: Valentin Bartenev Date: Wed, 19 Dec 2018 20:06:53 +0300 Subject: Python: replaced PyErr_PrintEx(1) with PyErr_Print(). These function calls are equivalent. No functional changes. --- src/nxt_python_wsgi.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nxt_python_wsgi.c') diff --git a/src/nxt_python_wsgi.c b/src/nxt_python_wsgi.c index 3a5f1913..739ee0b9 100644 --- a/src/nxt_python_wsgi.c +++ b/src/nxt_python_wsgi.c @@ -354,7 +354,7 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf) if (nxt_slow_path(module == NULL)) { nxt_alert(task, "Python failed to import module \"%s\"", nxt_py_module); - PyErr_PrintEx(1); + PyErr_Print(); return NXT_ERROR; } @@ -369,7 +369,7 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf) if (nxt_slow_path(PyCallable_Check(obj) == 0)) { nxt_alert(task, "\"application\" in module \"%s\" " "is not a callable object", nxt_py_module); - PyErr_PrintEx(1); + PyErr_Print(); goto fail; } @@ -804,7 +804,7 @@ nxt_python_add_sptr(nxt_python_run_ctx_t *ctx, const char *name, nxt_unit_req_error(ctx->req, "Python failed to create value string \"%.*s\"", (int) size, src); - PyErr_PrintEx(1); + PyErr_Print(); return NXT_UNIT_ERROR; } @@ -839,7 +839,7 @@ nxt_python_add_str(nxt_python_run_ctx_t *ctx, const char *name, nxt_unit_req_error(ctx->req, "Python failed to create value string \"%.*s\"", (int) size, str); - PyErr_PrintEx(1); + PyErr_Print(); return NXT_UNIT_ERROR; } -- cgit From 9c03079e3a4a7aead2f761a3a425bee065cc89d1 Mon Sep 17 00:00:00 2001 From: Artem Konev Date: Thu, 20 Dec 2018 14:17:58 +0300 Subject: Python: fixed a typo in path error message. --- src/nxt_python_wsgi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nxt_python_wsgi.c') diff --git a/src/nxt_python_wsgi.c b/src/nxt_python_wsgi.c index 739ee0b9..df1c74ad 100644 --- a/src/nxt_python_wsgi.c +++ b/src/nxt_python_wsgi.c @@ -284,7 +284,7 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf) c->path.length); if (nxt_slow_path(obj == NULL)) { - nxt_alert(task, "Python failed create string object \"%V\"", + nxt_alert(task, "Python failed to create string object \"%V\"", &c->path); goto fail; } -- cgit From 1ce7e860b2913bc5c89258dca6c8fa9cf86ade35 Mon Sep 17 00:00:00 2001 From: Valentin Bartenev Date: Thu, 20 Dec 2018 15:47:10 +0300 Subject: Python: cleanup of nxt_python_init(). - Removed surplus NULL assignments; - Added missing nxt_slow_path(); - Style cleanup. --- src/nxt_python_wsgi.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'src/nxt_python_wsgi.c') diff --git a/src/nxt_python_wsgi.c b/src/nxt_python_wsgi.c index df1c74ad..b6566788 100644 --- a/src/nxt_python_wsgi.c +++ b/src/nxt_python_wsgi.c @@ -276,7 +276,6 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf) Py_InitializeEx(0); - obj = NULL; module = NULL; if (c->path.length > 0) { @@ -303,11 +302,9 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf) } Py_DECREF(obj); - obj = NULL; } obj = PyCFunction_New(nxt_py_start_resp_method, NULL); - if (nxt_slow_path(obj == NULL)) { nxt_alert(task, "Python failed to initialize the \"start_response\" function"); @@ -317,7 +314,6 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf) nxt_py_start_resp_obj = obj; obj = PyCFunction_New(nxt_py_write_method, NULL); - if (nxt_slow_path(obj == NULL)) { nxt_alert(task, "Python failed to initialize the \"write\" function"); goto fail; @@ -326,20 +322,19 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf) nxt_py_write_obj = obj; obj = nxt_python_create_environ(task); - - if (obj == NULL) { + if (nxt_slow_path(obj == NULL)) { goto fail; } nxt_py_environ_ptyp = obj; obj = Py_BuildValue("[s]", "unit"); - if (obj == NULL) { + if (nxt_slow_path(obj == NULL)) { nxt_alert(task, "Python failed to create the \"sys.argv\" list"); goto fail; } - if (PySys_SetObject((char *) "argv", obj) != 0) { + if (nxt_slow_path(PySys_SetObject((char *) "argv", obj) != 0)) { nxt_alert(task, "Python failed to set the \"sys.argv\" list"); goto fail; } @@ -351,7 +346,6 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf) nxt_py_module[c->module.length] = '\0'; module = PyImport_ImportModule(nxt_py_module); - if (nxt_slow_path(module == NULL)) { nxt_alert(task, "Python failed to import module \"%s\"", nxt_py_module); PyErr_Print(); @@ -359,7 +353,6 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf) } obj = PyDict_GetItemString(PyModule_GetDict(module), "application"); - if (nxt_slow_path(obj == NULL)) { nxt_alert(task, "Python failed to get \"application\" " "from module \"%s\"", nxt_py_module); -- cgit From 27394118b32ab395be50de15ecf514d5529090c5 Mon Sep 17 00:00:00 2001 From: Valentin Bartenev Date: Thu, 20 Dec 2018 15:47:10 +0300 Subject: Python: fixed error reporting on initialization of applications. PyErr_Print() writes traceback to "sys.stderr", which is a file object that can buffer the output. If the process exits immediately, the buffer can be destroyed before flushing to the log. As a result, the user doesn't see the traceback. Now Py_Finalize() is also called in case of any errors during initialization. It finalizes the interpreter and flushes all data. --- src/nxt_python_wsgi.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'src/nxt_python_wsgi.c') diff --git a/src/nxt_python_wsgi.c b/src/nxt_python_wsgi.c index b6566788..bd3a2cb2 100644 --- a/src/nxt_python_wsgi.c +++ b/src/nxt_python_wsgi.c @@ -339,7 +339,7 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf) goto fail; } - Py_DECREF(obj); + Py_CLEAR(obj); nxt_py_module = nxt_alloca(c->module.length + 1); nxt_memcpy(nxt_py_module, c->module.start, c->module.length); @@ -349,7 +349,7 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf) if (nxt_slow_path(module == NULL)) { nxt_alert(task, "Python failed to import module \"%s\"", nxt_py_module); PyErr_Print(); - return NXT_ERROR; + goto fail; } obj = PyDict_GetItemString(PyModule_GetDict(module), "application"); @@ -367,9 +367,10 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf) } Py_INCREF(obj); - Py_DECREF(module); + Py_CLEAR(module); nxt_py_application = obj; + obj = NULL; nxt_unit_default_init(task, &python_init); @@ -377,7 +378,7 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf) unit_ctx = nxt_unit_init(&python_init); if (nxt_slow_path(unit_ctx == NULL)) { - return NXT_ERROR; + goto fail; } rc = nxt_unit_run(unit_ctx); @@ -395,9 +396,7 @@ fail: Py_XDECREF(obj); Py_XDECREF(module); - if (nxt_py_home != NULL) { - nxt_free(nxt_py_home); - } + nxt_python_atexit(); return NXT_ERROR; } @@ -529,10 +528,10 @@ fail: static void nxt_python_atexit(void) { - Py_DECREF(nxt_py_application); - Py_DECREF(nxt_py_start_resp_obj); - Py_DECREF(nxt_py_write_obj); - Py_DECREF(nxt_py_environ_ptyp); + Py_XDECREF(nxt_py_application); + Py_XDECREF(nxt_py_start_resp_obj); + Py_XDECREF(nxt_py_write_obj); + Py_XDECREF(nxt_py_environ_ptyp); Py_Finalize(); -- cgit