From 67ac4682935d91833a8030804f4d0176b7f8c611 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 20 Mar 2024 05:09:03 +0000 Subject: [PATCH 1/2] Drop Python ~3.2 --- plugins/asyncio/asyncio.c | 12 ++--- plugins/python/gil.c | 11 ----- plugins/python/profiler.c | 13 ------ plugins/python/pump_subhandler.c | 14 ------ plugins/python/python_plugin.c | 49 -------------------- plugins/python/pyutils.c | 64 +-------------------------- plugins/python/symimporter.c | 76 -------------------------------- plugins/python/tracebacker.c | 33 -------------- plugins/python/uwsgi_pymodule.c | 24 ---------- plugins/python/uwsgi_python.h | 42 ++---------------- plugins/python/web3_subhandler.c | 5 --- plugins/python/wsgi_headers.c | 36 --------------- plugins/python/wsgi_subhandler.c | 30 ------------- 13 files changed, 8 insertions(+), 401 deletions(-) diff --git a/plugins/asyncio/asyncio.c b/plugins/asyncio/asyncio.c index 7c7b330dd5..eae562f2f9 100644 --- a/plugins/asyncio/asyncio.c +++ b/plugins/asyncio/asyncio.c @@ -162,7 +162,7 @@ static PyObject *py_uwsgi_asyncio_request(PyObject *self, PyObject *args) { end: uwsgi.async_proto_fd_table[wsgi_req->fd] = NULL; - uwsgi_close_request(uwsgi.wsgi_req); + uwsgi_close_request(uwsgi.wsgi_req); free_req_queue; again: Py_INCREF(Py_None); @@ -282,11 +282,11 @@ static void uwsgi_asyncio_schedule_fix(struct wsgi_request *wsgi_req) { PyObject *cb = PyObject_CallMethod(uasyncio.loop, "call_soon", "Ol", uasyncio.hook_fix, (long) wsgi_req); if (!cb) goto error; Py_DECREF(cb); - return; + return; error: PyErr_Print(); -} +} static void asyncio_loop() { @@ -328,11 +328,7 @@ static void asyncio_loop() { uwsgi.schedule_fix = uwsgi_asyncio_schedule_fix; } -#ifndef PYTHREE - PyObject *asyncio = PyImport_ImportModule("trollius"); -#else PyObject *asyncio = PyImport_ImportModule("asyncio"); -#endif if (!asyncio) uwsgi_pyexit; uasyncio.mod = asyncio; @@ -366,7 +362,7 @@ static void asyncio_loop() { uwsgi_pyexit; } uwsgi_sock = uwsgi_sock->next; - } + } if (PyObject_CallMethod(uasyncio.loop, "run_forever", NULL) == NULL) { uwsgi_pyexit; diff --git a/plugins/python/gil.c b/plugins/python/gil.c index 46f7023910..2460b211e6 100644 --- a/plugins/python/gil.c +++ b/plugins/python/gil.c @@ -5,25 +5,14 @@ extern struct uwsgi_python up; void gil_real_get() { //uwsgi_log("LOCK %d\n", uwsgi.mywid); -#if !defined(PYTHREE) - PyEval_AcquireLock(); - PyThreadState_Swap((PyThreadState *) pthread_getspecific(up.upt_gil_key)); -#else PyEval_RestoreThread((PyThreadState *) pthread_getspecific(up.upt_gil_key)); -#endif - //uwsgi_log("LOCKED !!! %d\n", uwsgi.mywid); } void gil_real_release() { //uwsgi_log("UNLOCK %d\n", uwsgi.mywid); -#if !defined(PYTHREE) - pthread_setspecific(up.upt_gil_key, (void *) PyThreadState_Swap(NULL)); - PyEval_ReleaseLock(); -#else pthread_setspecific(up.upt_gil_key, (void *) PyThreadState_Get()); PyEval_SaveThread(); -#endif } diff --git a/plugins/python/profiler.c b/plugins/python/profiler.c index 2a7e1cc231..3ddd5fad3b 100644 --- a/plugins/python/profiler.c +++ b/plugins/python/profiler.c @@ -2,17 +2,6 @@ extern struct uwsgi_server uwsgi; -#ifdef HAS_NOT_PyFrame_GetLineNumber -int PyFrame_GetLineNumber(PyFrameObject *frame) { - if (frame->f_trace) { - return frame->f_lineno; - } - else { - return PyCode_Addr2Line(frame->f_code, frame->f_lasti); - } -} -#endif - #if PY_VERSION_HEX < 0x030900B1 PyCodeObject* PyFrame_GetCode(PyFrameObject *frame) { @@ -21,14 +10,12 @@ PyCodeObject* PyFrame_GetCode(PyFrameObject *frame) } #endif -#ifdef PYTHREE #undef PyString_AsString static char *PyString_AsString(PyObject *o) { PyObject *zero = PyUnicode_AsLatin1String(o); if (!zero) return ""; return PyBytes_AsString(zero); } -#endif int uwsgi_python_profiler_call(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg) { diff --git a/plugins/python/pump_subhandler.c b/plugins/python/pump_subhandler.c index 0e6809d789..8f3065eb83 100644 --- a/plugins/python/pump_subhandler.c +++ b/plugins/python/pump_subhandler.c @@ -72,13 +72,8 @@ void *uwsgi_request_subhandler_pump(struct wsgi_request *wsgi_req, struct uwsgi_ if (wsgi_req->hvec[i].iov_len < 6) continue; if (!uwsgi_startswith(wsgi_req->hvec[i].iov_base, "HTTP_", 5)) { (void) uwsgi_lower(wsgi_req->hvec[i].iov_base+5, wsgi_req->hvec[i].iov_len-5); -#ifdef PYTHREE pydictkey = PyUnicode_DecodeLatin1(wsgi_req->hvec[i].iov_base+5, wsgi_req->hvec[i].iov_len-5, NULL); pydictvalue = PyUnicode_DecodeLatin1(wsgi_req->hvec[i + 1].iov_base, wsgi_req->hvec[i + 1].iov_len, NULL); -#else - pydictkey = PyString_FromStringAndSize(wsgi_req->hvec[i].iov_base+5, wsgi_req->hvec[i].iov_len-5); - pydictvalue = PyString_FromStringAndSize(wsgi_req->hvec[i + 1].iov_base, wsgi_req->hvec[i + 1].iov_len); -#endif PyObject *old_value = PyDict_GetItem(headers, pydictkey); if (old_value) { if (PyString_Check(old_value)) { @@ -199,11 +194,7 @@ int uwsgi_response_subhandler_pump(struct wsgi_request *wsgi_req) { } PyObject *hhkey, *hhvalue; -#ifdef UWSGI_PYTHON_OLD - int hhpos = 0; -#else Py_ssize_t hhpos = 0; -#endif while (PyDict_Next(headers, &hhpos, &hhkey, &hhvalue)) { if (!PyString_Check(hhkey)) continue; @@ -235,12 +226,7 @@ int uwsgi_response_subhandler_pump(struct wsgi_request *wsgi_req) { } goto clear; } -#ifdef PYTHREE else if ((wsgi_req->sendfile_fd = PyObject_AsFileDescriptor((PyObject *)wsgi_req->async_placeholder)) > -1) { -#else - else if (PyFile_Check((PyObject *)wsgi_req->async_placeholder)) { - wsgi_req->sendfile_fd = fileno(PyFile_AsFile((PyObject *)wsgi_req->async_placeholder)); -#endif UWSGI_RELEASE_GIL uwsgi_response_sendfile_do(wsgi_req, wsgi_req->sendfile_fd, 0, 0); UWSGI_GET_GIL diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index de92f34c2f..8fee7f0c9b 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -190,9 +190,7 @@ struct uwsgi_option uwsgi_python_options[] = { {"python-raw", required_argument, 0, "load a python file for managing raw requests", uwsgi_opt_set_str, &up.raw, 0}, -#if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER) {"py-sharedarea", required_argument, 0, "create a sharedarea from a python bytearray object of the specified size", uwsgi_opt_add_string_list, &up.sharedarea, 0}, -#endif {"py-call-osafterfork", no_argument, 0, "enable child processes running cpython to trap OS signals", uwsgi_opt_true, &up.call_osafterfork, 0}, {"py-call-uwsgi-fork-hooks", no_argument, 0, "call pre and post hooks when uswgi forks to update the internal interpreter state of CPython", uwsgi_opt_true, &up.call_uwsgi_fork_hooks, 0}, @@ -235,7 +233,6 @@ PyMethodDef uwsgi_write_method[] = { {"uwsgi_write", py_uwsgi_write, METH_VARARG PyDoc_STRVAR(uwsgi_py_doc, "uWSGI api module."); -#ifdef PYTHREE static PyModuleDef uwsgi_module3 = { PyModuleDef_HEAD_INIT, "uwsgi", @@ -246,7 +243,6 @@ static PyModuleDef uwsgi_module3 = { PyObject *init_uwsgi3(void) { return PyModule_Create(&uwsgi_module3); } -#endif int uwsgi_python_init() { @@ -269,7 +265,6 @@ int uwsgi_python_init() { uwsgi_log("Python Home is not a directory: %s\n", up.home); exit(1); } -#ifdef PYTHREE // check for PEP 405 virtualenv (starting from python 3.3) char *pep405_env = uwsgi_concat2(up.home, "/pyvenv.cfg"); if (uwsgi_file_exists(pep405_env)) { @@ -292,9 +287,6 @@ int uwsgi_python_init() { // do not free this memory !!! //free(wpyhome); pep405: -#else - Py_SetPythonHome(up.home); -#endif uwsgi_log("Set PythonHome to %s\n", up.home); } @@ -303,7 +295,6 @@ int uwsgi_python_init() { program_name = uwsgi.binary_path; } -#ifdef PYTHREE if (!up.programname) { if (up.home) { program_name = uwsgi_concat2(up.home, "/bin/python"); @@ -316,10 +307,6 @@ int uwsgi_python_init() { #ifdef UWSGI_PY312 PyImport_AppendInittab("uwsgi", init_uwsgi3); #endif -#else - Py_SetProgramName(program_name); -#endif - Py_OptimizeFlag = up.optimize; @@ -343,7 +330,6 @@ int uwsgi_python_init() { up.swap_ts = simple_swap_ts; up.reset_ts = simple_reset_ts; -#if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER) struct uwsgi_string_list *usl = NULL; uwsgi_foreach(usl, up.sharedarea) { uint64_t len = uwsgi_n64(usl->value); @@ -353,7 +339,6 @@ int uwsgi_python_init() { struct uwsgi_sharedarea *sa = uwsgi_sharedarea_init_ptr(storage, len); sa->obj = obj; } -#endif uwsgi_log_initial("Python main interpreter initialized at %p\n", up.main_thread); @@ -597,7 +582,6 @@ void init_uwsgi_vars() { } pysys_dict = PyModule_GetDict(pysys); -#ifdef PYTHREE // In python3, stdout / stderr was changed to be buffered (a bug according // to many): // - https://bugs.python.org/issue13597 @@ -606,17 +590,12 @@ void init_uwsgi_vars() { // In the case of a tty, this fix breaks readline support in interactive // debuggers so we'll only do this in the non-tty case. if (!Py_FdIsInteractive(stdin, NULL)) { -#ifdef HAS_NO_ERRORS_IN_PyFile_FromFd - PyObject *new_stdprint = PyFile_FromFd(2, NULL, "w", _IOLBF, NULL, NULL, 0); -#else PyObject *new_stdprint = PyFile_FromFd(2, NULL, "w", _IOLBF, NULL, "backslashreplace", NULL, 0); -#endif PyDict_SetItemString(pysys_dict, "stdout", new_stdprint); PyDict_SetItemString(pysys_dict, "__stdout__", new_stdprint); PyDict_SetItemString(pysys_dict, "stderr", new_stdprint); PyDict_SetItemString(pysys_dict, "__stderr__", new_stdprint); } -#endif pypath = PyDict_GetItemString(pysys_dict, "path"); if (!pypath) { PyErr_Print(); @@ -697,14 +676,10 @@ void init_uwsgi_embedded_module() { } -#ifdef PYTHREE #ifndef UWSGI_PY312 PyImport_AppendInittab("uwsgi", init_uwsgi3); #endif new_uwsgi_module = PyImport_AddModule("uwsgi"); -#else - new_uwsgi_module = Py_InitModule3("uwsgi", NULL, uwsgi_py_doc); -#endif if (new_uwsgi_module == NULL) { uwsgi_log("could not initialize the uwsgi python module\n"); exit(1); @@ -827,11 +802,7 @@ void init_uwsgi_embedded_module() { PyObject *py_opt_dict = PyDict_New(); for (i = 0; i < uwsgi.exported_opts_cnt; i++) { -#ifdef PYTHREE PyObject *key = PyUnicode_FromString(uwsgi.exported_opts[i]->key); -#else - PyObject *key = PyString_FromString(uwsgi.exported_opts[i]->key); -#endif if (PyDict_Contains(py_opt_dict, key)) { PyObject *py_opt_item = PyDict_GetItem(py_opt_dict, key); if (PyList_Check(py_opt_item)) { @@ -1458,11 +1429,7 @@ void uwsgi_python_set_thread_name(int core_id) { PyErr_Clear(); } else { -#ifdef PYTHREE PyObject_SetAttrString(current_thread, "name", PyUnicode_FromFormat("uWSGIWorker%dCore%d", uwsgi.mywid, core_id)); -#else - PyObject_SetAttrString(current_thread, "name", PyString_FromFormat("uWSGIWorker%dCore%d", uwsgi.mywid, core_id)); -#endif Py_INCREF(current_thread); } } @@ -1577,11 +1544,7 @@ void *uwsgi_python_autoreloader_thread(void *interpreter) { // do not start monitoring til the first app is loaded (required for lazy mode) if (uwsgi_apps_cnt == 0) continue; } -#ifdef UWSGI_PYTHON_OLD - int pos = 0; -#else Py_ssize_t pos = 0; -#endif char *app_dir = NULL; int i; for (i = 0; i < uwsgi_apps_cnt; i++) { @@ -1597,15 +1560,11 @@ void *uwsgi_python_autoreloader_thread(void *interpreter) { int found = 0; struct uwsgi_string_list *usl = up.auto_reload_ignore; while(usl) { -#ifdef PYTHREE PyObject *zero = PyUnicode_AsUTF8String(mod_name); char *str_mod_name = PyString_AsString(zero); int ret_cmp = strcmp(usl->value, str_mod_name); Py_DECREF(zero); if (!ret_cmp) { -#else - if (!strcmp(usl->value, PyString_AsString(mod_name))) { -#endif found = 1; break; } @@ -1616,16 +1575,10 @@ void *uwsgi_python_autoreloader_thread(void *interpreter) { PyObject *mod_file = PyObject_GetAttrString(mod, "__file__"); if (!mod_file) continue; if (mod_file == Py_None) continue; -#ifdef PYTHREE PyObject *zero = PyUnicode_AsUTF8String(mod_file); char *mod_filename = PyString_AsString(zero); -#else - char *mod_filename = PyString_AsString(mod_file); -#endif if (!mod_filename) { -#ifdef PYTHREE Py_DECREF(zero); -#endif continue; } char *ext = strrchr(mod_filename, '.'); @@ -1645,9 +1598,7 @@ void *uwsgi_python_autoreloader_thread(void *interpreter) { return NULL; } free(filename); -#ifdef PYTHREE Py_DECREF(zero); -#endif } } diff --git a/plugins/python/pyutils.c b/plugins/python/pyutils.c index 9cb111d0bc..b1a350bfdd 100644 --- a/plugins/python/pyutils.c +++ b/plugins/python/pyutils.c @@ -10,16 +10,7 @@ int manage_python_response(struct wsgi_request *wsgi_req) { char *uwsgi_python_get_exception_type(PyObject *exc) { char *class_name = NULL; -#if !defined(PYTHREE) - if (PyClass_Check(exc)) { - class_name = PyString_AsString( ((PyClassObject*)(exc))->cl_name ); - } - else { -#endif - class_name = (char *) ((PyTypeObject*)exc)->tp_name; -#if !defined(PYTHREE) - } -#endif + class_name = (char *) ((PyTypeObject*)exc)->tp_name; if (class_name) { char *dot = strrchr(class_name, '.'); @@ -27,20 +18,14 @@ char *uwsgi_python_get_exception_type(PyObject *exc) { PyObject *module_name = PyObject_GetAttrString(exc, "__module__"); if (module_name) { -#ifdef PYTHREE char *mod_name = NULL; PyObject *zero = PyUnicode_AsUTF8String(module_name); if (zero) { mod_name = PyString_AsString(zero); } -#else - char *mod_name = PyString_AsString(module_name); -#endif if (mod_name && strcmp(mod_name, "exceptions") ) { char *ret = uwsgi_concat3(mod_name, ".", class_name); -#ifdef PYTHREE Py_DECREF(zero); -#endif Py_DECREF(module_name); return ret; } @@ -91,7 +76,6 @@ struct uwsgi_buffer *uwsgi_python_backtrace(struct wsgi_request *wsgi_req) { PyObject *tb_text = PyTuple_GetItem(t, 3); int64_t line_no = PyInt_AsLong(tb_lineno); -#ifdef PYTHREE PyObject *zero = NULL; if (tb_filename) { zero = PyUnicode_AsUTF8String(tb_filename); @@ -138,23 +122,6 @@ struct uwsgi_buffer *uwsgi_python_backtrace(struct wsgi_request *wsgi_req) { if (uwsgi_buffer_u16le(ub, 0)) { goto end0; } } -#else - // filename - if (uwsgi_buffer_u16le(ub, PyString_Size(tb_filename))) goto end0; - if (uwsgi_buffer_append(ub, PyString_AsString(tb_filename), PyString_Size(tb_filename))) goto end0; - - // lineno - if (uwsgi_buffer_append_valnum(ub, line_no)) goto end0; - - // function - if (uwsgi_buffer_u16le(ub, PyString_Size(tb_function))) goto end0; - if (uwsgi_buffer_append(ub, PyString_AsString(tb_function), PyString_Size(tb_function))) goto end0; - - // text - if (uwsgi_buffer_u16le(ub, PyString_Size(tb_text))) goto end0; - if (uwsgi_buffer_append(ub, PyString_AsString(tb_text), PyString_Size(tb_text))) goto end0; -#endif - // custom (unused) if (uwsgi_buffer_u16le(ub, 0)) goto end0; if (uwsgi_buffer_append(ub, "", 0)) goto end0; @@ -211,29 +178,21 @@ struct uwsgi_buffer *uwsgi_python_exception_msg(struct wsgi_request *wsgi_req) { // value could be NULL ? if (!value) goto end; -#ifdef PYTHREE char *msg = NULL; PyObject *zero = PyUnicode_AsUTF8String( PyObject_Str(value) ); if (zero) { msg = PyString_AsString( zero ); } -#else - char *msg = PyString_AsString( PyObject_Str(value) ); -#endif if (msg) { size_t msg_len = strlen(msg); ub = uwsgi_buffer_new(msg_len); if (uwsgi_buffer_append(ub, msg, msg_len)) { -#ifdef PYTHREE Py_DECREF(zero); -#endif uwsgi_buffer_destroy(ub); ub = NULL; goto end; } -#ifdef PYTHREE Py_DECREF(zero); -#endif } end: PyErr_Restore(type, value, traceback); @@ -323,12 +282,8 @@ void init_pyargv() { argv0 = up.pyrun; } -#ifdef PYTHREE wchar_t *pname = uwsgi_calloc(sizeof(wchar_t) * (strlen(argv0)+1)); mbstowcs(pname, argv0, strlen(argv0)+1); -#else - char *pname = argv0; -#endif up.argc = 1; if (up.argv) { @@ -349,12 +304,7 @@ void init_pyargv() { free(tmp_ptr); } -#ifdef PYTHREE up.py_argv = uwsgi_calloc(sizeof(wchar_t *) * up.argc+1); -#else - up.py_argv = uwsgi_calloc(sizeof(char *) * up.argc+1); -#endif - up.py_argv[0] = pname; @@ -362,9 +312,7 @@ void init_pyargv() { char *py_argv_copy = uwsgi_str(up.argv); up.argc = 1; -#ifdef PYTHREE wchar_t *wcargv = uwsgi_calloc( sizeof( wchar_t ) * (strlen(py_argv_copy)+1)); -#endif #ifdef __sun__ // FIX THIS !!! @@ -375,13 +323,9 @@ void init_pyargv() { while ((ap = strsep(&py_argv_copy, " \t")) != NULL) { #endif if (*ap != '\0') { -#ifdef PYTHREE mbstowcs( wcargv, ap, strlen(ap)); up.py_argv[up.argc] = wcargv; wcargv += strlen(ap) + 1; -#else - up.py_argv[up.argc] = ap; -#endif up.argc++; } } @@ -397,11 +341,5 @@ void init_pyargv() { } if (!up.executable) up.executable = uwsgi.binary_path; -#ifdef PYTHREE PyDict_SetItemString(sys_dict, "executable", PyUnicode_FromString(up.executable)); -#else - PyDict_SetItemString(sys_dict, "executable", PyString_FromString(up.executable)); -#endif - - } diff --git a/plugins/python/symimporter.c b/plugins/python/symimporter.c index 228e73a5af..90bb16ecdd 100644 --- a/plugins/python/symimporter.c +++ b/plugins/python/symimporter.c @@ -472,28 +472,7 @@ zipimporter_init(struct _symzipimporter *self, PyObject *args, PyObject *kwds) } -#ifdef PYTHREE PyObject *source_code = PyObject_CallMethodObjArgs(stringio, PyString_FromString("StringIO"), PyString_FromStringAndSize(body, len)); -#else - PyObject *stringio_dict = PyModule_GetDict(stringio); - if (!stringio_dict) { - return -1; - } - - - PyObject *stringio_stringio = PyDict_GetItemString(stringio_dict, "StringIO"); - if (!stringio_stringio) { - return -1; - } - - - PyObject *stringio_args = PyTuple_New(1); - PyTuple_SetItem(stringio_args, 0, PyString_FromStringAndSize(body, len)); - - - - PyObject *source_code = PyInstance_New(stringio_stringio, stringio_args, NULL); -#endif if (!source_code) { return -1; } @@ -507,26 +486,7 @@ zipimporter_init(struct _symzipimporter *self, PyObject *args, PyObject *kwds) } -#ifdef PYTHREE self->zip = PyObject_CallMethodObjArgs(zipfile, PyString_FromString("ZipFile"), source_code); -#else - PyObject *zipfile_dict = PyModule_GetDict(zipfile); - if (!zipfile_dict) { - return -1; - } - - PyObject *zipfile_zipfile = PyDict_GetItemString(zipfile_dict, "ZipFile"); - if (!zipfile_zipfile) { - return -1; - } - - - PyObject *zipfile_args = PyTuple_New(1); - PyTuple_SetItem(zipfile_args, 0, source_code); - - - self->zip = PyInstance_New(zipfile_zipfile, zipfile_args, NULL); -#endif if (!self->zip) { return -1; } @@ -588,25 +548,7 @@ symzipimporter_init(struct _symzipimporter *self, PyObject *args, PyObject *kwds goto error; } -#ifdef PYTHREE PyObject *source_code = PyObject_CallMethodObjArgs(stringio, PyString_FromString("StringIO"), PyString_FromStringAndSize(code_start, code_end-code_start)); -#else - PyObject *stringio_dict = PyModule_GetDict(stringio); - if (!stringio_dict) { - goto error; - } - - PyObject *stringio_stringio = PyDict_GetItemString(stringio_dict, "StringIO"); - if (!stringio_stringio) { - goto error; - } - - PyObject *stringio_args = PyTuple_New(1); - PyTuple_SetItem(stringio_args, 0, PyString_FromStringAndSize(code_start, code_end-code_start)); - - - PyObject *source_code = PyInstance_New(stringio_stringio, stringio_args, NULL); -#endif if (!source_code) { goto error; } @@ -616,25 +558,7 @@ symzipimporter_init(struct _symzipimporter *self, PyObject *args, PyObject *kwds goto error; } -#ifdef PYTHREE self->zip = PyObject_CallMethodObjArgs(zipfile, PyString_FromString("ZipFile"), source_code); -#else - PyObject *zipfile_dict = PyModule_GetDict(zipfile); - if (!zipfile_dict) { - goto error; - } - - PyObject *zipfile_zipfile = PyDict_GetItemString(zipfile_dict, "ZipFile"); - if (!zipfile_zipfile) { - goto error; - } - - PyObject *zipfile_args = PyTuple_New(1); - PyTuple_SetItem(zipfile_args, 0, source_code); - - - self->zip = PyInstance_New(zipfile_zipfile, zipfile_args, NULL); -#endif if (!self->zip) { goto error; } diff --git a/plugins/python/tracebacker.c b/plugins/python/tracebacker.c index a91692f059..b1eeaf0903 100644 --- a/plugins/python/tracebacker.c +++ b/plugins/python/tracebacker.c @@ -27,7 +27,6 @@ char *uwsgi_python_get_thread_name(PyObject *thread_id) { if (PyInt_AsLong(thread_ident) == PyInt_AsLong(thread_id)) { PyObject *thread_name = PyObject_GetAttrString(threads_list_next, "name"); if (!thread_name) goto clear2; -#ifdef PYTHREE PyObject *thread_name_utf8 = PyUnicode_AsEncodedString(thread_name, "ASCII", "strict"); if (!thread_name_utf8) goto clear2; char *name = NULL; @@ -36,9 +35,6 @@ char *uwsgi_python_get_thread_name(PyObject *thread_id) { name = uwsgi_str(tmp_name); Py_DECREF(thread_name_utf8); } -#else - char *name = PyString_AsString(thread_name); -#endif Py_DECREF(threads_list_next); Py_DECREF(threads_list_iter); Py_DECREF(threads_list); @@ -144,7 +140,6 @@ void *uwsgi_python_tracebacker_thread(void *foobar) { PyObject *st_items = PyIter_Next(stacktrace_iter); // we have the first traceback item while(st_items) { -#ifdef PYTHREE int thread_name_need_free = 0; @@ -156,16 +151,6 @@ void *uwsgi_python_tracebacker_thread(void *foobar) { if (!st_name) {Py_DECREF(st_items); goto next;} PyObject *st_line = PyObject_GetAttrString(st_items, "line"); -#else - PyObject *st_filename = PyTuple_GetItem(st_items, 0); - if (!st_filename) { Py_DECREF(st_items); goto next; } - PyObject *st_lineno = PyTuple_GetItem(st_items, 1); - if (!st_lineno) {Py_DECREF(st_items); goto next;} - PyObject *st_name = PyTuple_GetItem(st_items, 2); - if (!st_name) {Py_DECREF(st_items); goto next;} - - PyObject *st_line = PyTuple_GetItem(st_items, 3); -#endif iov[0].iov_base = "thread_id = "; iov[0].iov_len = 12; @@ -173,26 +158,20 @@ void *uwsgi_python_tracebacker_thread(void *foobar) { if (!iov[1].iov_base) { iov[1].iov_base = ""; } -#ifdef PYTHREE else { thread_name_need_free = 1; } -#endif iov[1].iov_len = strlen(iov[1].iov_base); iov[2].iov_base = " filename = "; iov[2].iov_len = 12; -#ifdef PYTHREE PyObject *st_filename_utf8 = PyUnicode_AsEncodedString(st_filename, "ASCII", "strict"); if (!st_filename_utf8) { if (thread_name_need_free) free(iov[1].iov_base); goto next; } iov[3].iov_base = PyBytes_AS_STRING(st_filename_utf8); -#else - iov[3].iov_base = PyString_AsString(st_filename); -#endif iov[3].iov_len = strlen(iov[3].iov_base); iov[4].iov_base = " lineno = "; @@ -204,7 +183,6 @@ void *uwsgi_python_tracebacker_thread(void *foobar) { iov[6].iov_base = " function = "; iov[6].iov_len = 12 ; -#ifdef PYTHREE PyObject *st_name_utf8 = PyUnicode_AsEncodedString(st_name, "ASCII", "strict"); if (!st_name_utf8) { if (thread_name_need_free) free(iov[1].iov_base); @@ -212,9 +190,6 @@ void *uwsgi_python_tracebacker_thread(void *foobar) { goto next; } iov[7].iov_base = PyBytes_AS_STRING(st_name_utf8); -#else - iov[7].iov_base = PyString_AsString(st_name); -#endif iov[7].iov_len = strlen(iov[7].iov_base); iov[8].iov_base = ""; @@ -226,13 +201,10 @@ void *uwsgi_python_tracebacker_thread(void *foobar) { iov[10].iov_base = "\n"; iov[10].iov_len = 1; -#ifdef PYTHREE PyObject *st_line_utf8 = NULL; -#endif if (st_line) { iov[8].iov_base = " line = "; iov[8].iov_len = 8; -#ifdef PYTHREE PyObject *st_line_utf8 = PyUnicode_AsEncodedString(st_line, "ASCII", "strict"); if (!st_line_utf8) { if (thread_name_need_free) free(iov[1].iov_base); @@ -241,9 +213,6 @@ void *uwsgi_python_tracebacker_thread(void *foobar) { goto next; } iov[9].iov_base = PyBytes_AS_STRING(st_line_utf8); -#else - iov[9].iov_base = PyString_AsString(st_line); -#endif iov[9].iov_len = strlen(iov[9].iov_base); } @@ -254,7 +223,6 @@ void *uwsgi_python_tracebacker_thread(void *foobar) { // free the line_no free(iov[5].iov_base); Py_DECREF(st_items); -#ifdef PYTHREE Py_DECREF(st_filename_utf8); Py_DECREF(st_name_utf8); if (st_line_utf8) { @@ -262,7 +230,6 @@ void *uwsgi_python_tracebacker_thread(void *foobar) { } if (thread_name_need_free) free(iov[1].iov_base); -#endif st_items = PyIter_Next(stacktrace_iter); } if (write(client_fd, "\n", 1) < 0) { diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index cc6948746a..671cc70bbf 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -447,11 +447,9 @@ PyObject *py_uwsgi_rpc(PyObject * self, PyObject * args) { if (PyString_Check(py_node)) { node = PyString_AsString(py_node); } -#ifdef PYTHREE else if (PyUnicode_Check(py_node)) { node = PyBytes_AsString(PyUnicode_AsLatin1String(py_node)); } -#endif py_func = PyTuple_GetItem(args, 1); @@ -861,7 +859,6 @@ PyObject *py_uwsgi_advanced_sendfile(PyObject * self, PyObject * args) { } } -#ifdef PYTHREE else if (PyUnicode_Check(what)) { filename = PyBytes_AsString(PyUnicode_AsLatin1String(what)); @@ -871,7 +868,6 @@ PyObject *py_uwsgi_advanced_sendfile(PyObject * self, PyObject * args) { goto clear; } } -#endif else { fd = PyObject_AsFileDescriptor(what); if (fd < 0) @@ -2023,11 +2019,7 @@ PyObject *py_uwsgi_sharedarea_read(PyObject * self, PyObject * args) { } PyObject *ret = PyString_FromStringAndSize(NULL, len); -#ifdef PYTHREE char *storage = PyBytes_AsString(ret); -#else - char *storage = PyString_AS_STRING(ret); -#endif UWSGI_RELEASE_GIL int64_t rlen = uwsgi_sharedarea_read(id, pos, storage, len); @@ -2044,8 +2036,6 @@ PyObject *py_uwsgi_sharedarea_read(PyObject * self, PyObject * args) { return ret; } -#if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER) -#ifndef HAS_NOT_PyMemoryView_FromBuffer PyObject *py_uwsgi_sharedarea_memoryview(PyObject * self, PyObject * args) { int id; if (!PyArg_ParseTuple(args, "i:sharedarea_memoryview", &id)) { @@ -2060,7 +2050,6 @@ PyObject *py_uwsgi_sharedarea_memoryview(PyObject * self, PyObject * args) { return PyErr_Format(PyExc_ValueError, "cannot get a memoryview object from sharedarea %d", id); return PyMemoryView_FromBuffer(&info); } -#endif PyObject *py_uwsgi_sharedarea_object(PyObject * self, PyObject * args) { int id; @@ -2073,7 +2062,6 @@ PyObject *py_uwsgi_sharedarea_object(PyObject * self, PyObject * args) { } return (PyObject *) sa->obj; } -#endif PyObject *py_uwsgi_spooler_freq(PyObject * self, PyObject * args) { @@ -2203,11 +2191,7 @@ PyObject *py_uwsgi_send_spool(PyObject * self, PyObject * args, PyObject *kw) { } } else { -#ifdef PYTHREE PyObject *str = PyObject_Bytes(val); -#else - PyObject *str = PyObject_Str(val); -#endif if (!str) { uwsgi_buffer_destroy(ub); goto error; @@ -2261,11 +2245,7 @@ PyObject *py_uwsgi_send_spool(PyObject * self, PyObject * args, PyObject *kw) { } return PyErr_Format(PyExc_ValueError, "unable to spool job"); error: -#ifdef PYTHREE return PyErr_Format(PyExc_ValueError, "spooler callable dictionary must contains only bytes"); -#else - return PyErr_Format(PyExc_ValueError, "spooler callable dictionary must contains only strings"); -#endif } PyObject *py_uwsgi_spooler_pid(PyObject * self, PyObject * args) { @@ -2924,12 +2904,8 @@ static PyMethodDef uwsgi_sa_methods[] = { {"sharedarea_rlock", py_uwsgi_sharedarea_rlock, METH_VARARGS, ""}, {"sharedarea_wlock", py_uwsgi_sharedarea_wlock, METH_VARARGS, ""}, {"sharedarea_unlock", py_uwsgi_sharedarea_unlock, METH_VARARGS, ""}, -#if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER) -#ifndef HAS_NOT_PyMemoryView_FromBuffer {"sharedarea_memoryview", py_uwsgi_sharedarea_memoryview, METH_VARARGS, ""}, -#endif {"sharedarea_object", py_uwsgi_sharedarea_object, METH_VARARGS, ""}, -#endif {NULL, NULL}, }; diff --git a/plugins/python/uwsgi_python.h b/plugins/python/uwsgi_python.h index 961a46c49f..277ffab16c 100644 --- a/plugins/python/uwsgi_python.h +++ b/plugins/python/uwsgi_python.h @@ -12,11 +12,6 @@ #define PYTHON_APP_TYPE_PUMP 3 #define PYTHON_APP_TYPE_WSGI_LITE 4 -#if PY_MINOR_VERSION == 4 && PY_MAJOR_VERSION == 2 -#define Py_ssize_t ssize_t -#define UWSGI_PYTHON_OLD -#endif - #if (PY_VERSION_HEX >= 0x030b0000) # define UWSGI_PY311 #endif @@ -25,35 +20,16 @@ # define UWSGI_PY312 #endif -#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7 -#define HAS_NOT_PyMemoryView_FromBuffer -#endif - -#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7 -#define HAS_NOT_PyFrame_GetLineNumber +#if PY_MAJOR_VERSION < 3 +#error "Python 2 is not supported" #endif #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 2 -#define HAS_NOT_PyFrame_GetLineNumber -#endif - -#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 0 -#define HAS_NO_ERRORS_IN_PyFile_FromFd +#error "Python <3.3 is not supported" #endif #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 7 #define HAS_NOT_PyOS_AfterFork_Child -#endif - -#if PY_MAJOR_VERSION < 3 -#define HAS_NOT_PyOS_AfterFork_Child -#endif - -#if PY_MAJOR_VERSION > 2 -#define PYTHREE -#endif - -#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 7) || PY_MAJOR_VERSION < 3 #define UWSGI_SHOULD_CALL_PYEVAL_INITTHREADS #endif @@ -82,7 +58,6 @@ PyAPI_FUNC(PyObject *) PyMarshal_WriteObjectToString(PyObject *, int); PyAPI_FUNC(PyObject *) PyMarshal_ReadObjectFromString(char *, Py_ssize_t); -#ifdef PYTHREE #define UWSGI_PYFROMSTRING(x) PyUnicode_FromString(x) #define UWSGI_PYFROMSTRINGSIZE(x, y) PyUnicode_FromStringAndSize(x, y) #define PyInt_FromLong PyLong_FromLong @@ -99,13 +74,6 @@ PyAPI_FUNC(PyObject *) PyMarshal_ReadObjectFromString(char *, Py_ssize_t); #define uwsgi_py_dict_get(a, b) PyDict_GetItem(a, PyBytes_FromString(b)); #define uwsgi_py_dict_del(a, b) PyDict_DelItem(a, PyBytes_FromString(b)); -#else -#define UWSGI_PYFROMSTRING(x) PyString_FromString(x) -#define UWSGI_PYFROMSTRINGSIZE(x, y) PyString_FromStringAndSize(x, y) -#define uwsgi_py_dict_get(a, b) PyDict_GetItemString(a, b) -#define uwsgi_py_dict_del(a, b) PyDict_DelItemString(a, b) -#endif - #define LOADER_DYN 0 #define LOADER_UWSGI 1 #define LOADER_FILE 2 @@ -144,11 +112,7 @@ struct uwsgi_python { char *argv; int argc; -#ifdef PYTHREE wchar_t **py_argv; -#else - char **py_argv; -#endif PyObject *wsgi_spitout; PyObject *wsgi_writeout; diff --git a/plugins/python/web3_subhandler.c b/plugins/python/web3_subhandler.c index f9961ba581..de057c50d0 100644 --- a/plugins/python/web3_subhandler.c +++ b/plugins/python/web3_subhandler.c @@ -17,13 +17,8 @@ void *uwsgi_request_subhandler_web3(struct wsgi_request *wsgi_req, struct uwsgi_ #ifdef UWSGI_DEBUG uwsgi_debug("%.*s: %.*s\n", wsgi_req->hvec[i].iov_len, wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i+1].iov_len, wsgi_req->hvec[i+1].iov_base); #endif -#ifdef PYTHREE pydictkey = PyUnicode_DecodeLatin1(wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i].iov_len, NULL); pydictvalue = PyUnicode_DecodeLatin1(wsgi_req->hvec[i + 1].iov_base, wsgi_req->hvec[i + 1].iov_len, NULL); -#else - pydictkey = PyString_FromStringAndSize(wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i].iov_len); - pydictvalue = PyString_FromStringAndSize(wsgi_req->hvec[i + 1].iov_base, wsgi_req->hvec[i + 1].iov_len); -#endif PyDict_SetItem(wsgi_req->async_environ, pydictkey, pydictvalue); Py_DECREF(pydictkey); Py_DECREF(pydictvalue); diff --git a/plugins/python/wsgi_headers.c b/plugins/python/wsgi_headers.c index 0fd5a60d6d..e438e8a92a 100644 --- a/plugins/python/wsgi_headers.c +++ b/plugins/python/wsgi_headers.c @@ -46,18 +46,13 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { return PyErr_Format(PyExc_TypeError, "start_response() takes at least 2 arguments"); } -#ifdef PYTHREE // check for web3 if ((self != Py_None && !PyUnicode_Check(head)) || (self == Py_None && !PyBytes_Check(head))) { -#else - if (!PyString_Check(head)) { -#endif return PyErr_Format(PyExc_TypeError, "http status must be a string"); } char *status_line = NULL; size_t status_line_len = 0; -#ifdef PYTHREE PyObject *zero = NULL; PyObject *zero2 = NULL; if (self != Py_None) { @@ -72,20 +67,12 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { status_line = PyBytes_AsString(head); status_line_len = PyBytes_Size(head); } -#else - status_line = PyString_AsString(head); - status_line_len = PyString_Size(head); -#endif if (uwsgi_response_prepare_headers(wsgi_req, status_line, status_line_len)) { -#ifdef PYTHREE Py_DECREF(zero); -#endif goto end; } -#ifdef PYTHREE Py_DECREF(zero); -#endif headers = PyTuple_GetItem(args, 1); if (!headers) { @@ -110,22 +97,14 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { if (!h_key) { return PyErr_Format(PyExc_TypeError, "http header must be a 2-item tuple"); } -#ifdef PYTHREE if ((self != Py_None && !PyUnicode_Check(h_key)) || (self == Py_None && !PyBytes_Check(h_key))) { -#else - if (!PyString_Check(h_key)) { -#endif return PyErr_Format(PyExc_TypeError, "http header key must be a string"); } h_value = PyTuple_GetItem(head, 1); if (!h_value) { return PyErr_Format(PyExc_TypeError, "http header must be a 2-item tuple"); } -#ifdef PYTHREE if ((self != Py_None && !PyUnicode_Check(h_value)) || (self == Py_None && !PyBytes_Check(h_value))) { -#else - if (!PyString_Check(h_value)) { -#endif return PyErr_Format(PyExc_TypeError, "http header value must be a string"); } @@ -133,7 +112,6 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { char *k = NULL; size_t kl = 0; char *v = NULL; size_t vl = 0; -#ifdef PYTHREE if (self != Py_None) { zero = PyUnicode_AsLatin1String(h_key); if (!zero) { @@ -146,12 +124,7 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { k = PyBytes_AsString(h_key); kl = PyBytes_Size(h_key); } -#else - k = PyString_AsString(h_key); - kl = PyString_Size(h_key); -#endif -#ifdef PYTHREE if (self != Py_None) { zero2 = PyUnicode_AsLatin1String(h_value); if (!zero2) { @@ -164,24 +137,15 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { v = PyBytes_AsString(h_value); vl = PyBytes_Size(h_value); } -#else - v = PyString_AsString(h_value); - vl = PyString_Size(h_value); -#endif if (uwsgi_response_add_header(wsgi_req, k, kl, v, vl)) { -#ifdef PYTHREE Py_DECREF(zero); Py_DECREF(zero2); -#endif return PyErr_Format(PyExc_TypeError, "unable to add header to the response"); } -#ifdef PYTHREE Py_DECREF(zero); Py_DECREF(zero2); -#endif - } if (up.start_response_nodelay) { diff --git a/plugins/python/wsgi_subhandler.c b/plugins/python/wsgi_subhandler.c index faa3fd8521..a29d897370 100644 --- a/plugins/python/wsgi_subhandler.c +++ b/plugins/python/wsgi_subhandler.c @@ -21,14 +21,11 @@ int uwsgi_python_send_body(struct wsgi_request *wsgi_req, PyObject *chunk) { char *content = NULL; size_t content_len = 0; -#if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER) Py_buffer pbuf; int has_buffer = 0; -#endif if (!up.wsgi_accept_buffer && !wsgi_req->is_raw) goto strict; -#if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER) if (PyObject_CheckBuffer(chunk)) { if (!PyObject_GetBuffer(chunk, &pbuf, PyBUF_SIMPLE)) { content = (char *) pbuf.buf; @@ -37,22 +34,6 @@ int uwsgi_python_send_body(struct wsgi_request *wsgi_req, PyObject *chunk) { goto found; } } -#else - if (PyObject_CheckReadBuffer(chunk)) { -#ifdef UWSGI_PYTHON_OLD - int buffer_len = 0; - if (!PyObject_AsCharBuffer(chunk, (const char **) &content, &buffer_len)) { -#else - if (!PyObject_AsCharBuffer(chunk, (const char **) &content, (Py_ssize_t *) &content_len)) { -#endif - PyErr_Clear(); - goto found; - } -#ifdef UWSGI_PYTHON_OLD - content_len = buffer_len; -#endif - } -#endif strict: // fallback @@ -66,9 +47,7 @@ int uwsgi_python_send_body(struct wsgi_request *wsgi_req, PyObject *chunk) { UWSGI_RELEASE_GIL uwsgi_response_write_body_do(wsgi_req, content, content_len); UWSGI_GET_GIL -#if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER) if (has_buffer) PyBuffer_Release(&pbuf); -#endif uwsgi_py_check_write_errors { uwsgi_py_write_exception(wsgi_req); return -1; @@ -139,13 +118,8 @@ void *uwsgi_request_subhandler_wsgi(struct wsgi_request *wsgi_req, struct uwsgi_ #ifdef UWSGI_DEBUG uwsgi_debug("%.*s: %.*s\n", wsgi_req->hvec[i].iov_len, wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i+1].iov_len, wsgi_req->hvec[i+1].iov_base); #endif -#ifdef PYTHREE pydictkey = PyUnicode_DecodeLatin1(wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i].iov_len, NULL); pydictvalue = PyUnicode_DecodeLatin1(wsgi_req->hvec[i + 1].iov_base, wsgi_req->hvec[i + 1].iov_len, NULL); -#else - pydictkey = PyString_FromStringAndSize(wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i].iov_len); - pydictvalue = PyString_FromStringAndSize(wsgi_req->hvec[i + 1].iov_base, wsgi_req->hvec[i + 1].iov_len); -#endif #ifdef UWSGI_DEBUG uwsgi_log("%p %d %p %d\n", pydictkey, wsgi_req->hvec[i].iov_len, pydictvalue, wsgi_req->hvec[i + 1].iov_len); @@ -347,11 +321,7 @@ int uwsgi_response_subhandler_wsgi(struct wsgi_request *wsgi_req) { } else { // The iterator returned something that we were not able to handle. PyObject *pystr = PyObject_Repr(pychunk); -#ifdef PYTHREE const char *cstr = PyUnicode_AsUTF8(pystr); -#else - const char *cstr = PyString_AsString(pystr); -#endif uwsgi_log("[ERROR] Unhandled object from iterator: %s (%p)\n", cstr, pychunk); Py_DECREF(pystr); } From 693a2b53d62e3308bf837d80fde698045cf6243b Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 20 Mar 2024 05:12:01 +0000 Subject: [PATCH 2/2] remove python 2.7 test from ci --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ba0cbbb49f..f2ab04240e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-20.04 strategy: matrix: - python-version: ["2.7", "3.6", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12"] + python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12"] test-suite: [unittest, python, deadlocks] steps: - name: Add deadnakes ppa