From c66957d6852599f1e554ff34100bb76bc99e216d Mon Sep 17 00:00:00 2001 From: "roberto@mrspurr" Date: Mon, 26 Mar 2012 10:46:12 +0200 Subject: [PATCH] first round of pypy patches --- plugins/python/pyloader.c | 2 + plugins/python/python_plugin.c | 66 +++++++++++++++++++++----------- plugins/python/pyutils.c | 8 ++++ plugins/python/wsgi_subhandler.c | 4 ++ 4 files changed, 58 insertions(+), 22 deletions(-) diff --git a/plugins/python/pyloader.c b/plugins/python/pyloader.c index ee3ffe7c..399a72e9 100644 --- a/plugins/python/pyloader.c +++ b/plugins/python/pyloader.c @@ -277,6 +277,7 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre wi->response_subhandler = uwsgi_response_subhandler_wsgi; wi->argc = 2; } +#ifndef UWSGI_PYPY else if (app_type == PYTHON_APP_TYPE_WEB3) { #ifdef UWSGI_DEBUG uwsgi_log("-- Web3 callable selected --\n"); @@ -291,6 +292,7 @@ int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThre wi->request_subhandler = uwsgi_request_subhandler_pump; wi->response_subhandler = uwsgi_response_subhandler_pump; } +#endif #ifdef UWSGI_ASYNC wi->args = malloc(sizeof(PyObject*)*uwsgi.cores); diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index 006039e5..3795cef4 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -5,6 +5,21 @@ struct uwsgi_python up; extern struct http_status_codes hsc[]; +#ifdef UWSGI_PYPY +char *RPython_StartupCode(void); +#define pypy_asm_stack_bottom() asm volatile ("/* GC_STACK_BOTTOM */" : : : \ + "memory") +void Py_Initialize(void) { + + pypy_asm_stack_bottom(); + char *errmsg = RPython_StartupCode(); + if (errmsg) { + uwsgi_log("unable to initialize PyPy: %s\n", errmsg); + exit(1); + } +} +#endif + #include extern PyTypeObject uwsgi_InputType; @@ -175,14 +190,14 @@ int uwsgi_python_init() { Py_SetProgramName("uWSGI"); #endif +#endif +#ifndef UWSGI_PYPY Py_OptimizeFlag = up.optimize; +#endif Py_Initialize(); - -#endif - up.wsgi_spitout = PyCFunction_New(uwsgi_spit_method, NULL); up.wsgi_writeout = PyCFunction_New(uwsgi_write_method, NULL); @@ -204,7 +219,6 @@ int uwsgi_python_init() { void uwsgi_python_reset_random_seed() { -#ifndef UWSGI_PYPY PyObject *random_module, *random_dict, *random_seed; // reinitialize the random seed (thanks Jonas Borgström) @@ -224,7 +238,6 @@ void uwsgi_python_reset_random_seed() { } } } -#endif } @@ -273,7 +286,9 @@ void uwsgi_python_atexit() { PyErr_Clear(); } +#ifndef UWSGI_PYPY Py_Finalize(); +#endif } void uwsgi_python_post_fork() { @@ -286,7 +301,6 @@ void uwsgi_python_post_fork() { uwsgi_python_reset_random_seed(); -#ifndef UWSGI_PYPY #ifdef UWSGI_EMBEDDED // call the post_fork_hook PyObject *uwsgi_dict = get_uwsgi_pydict("uwsgi"); @@ -298,7 +312,6 @@ void uwsgi_python_post_fork() { } PyErr_Clear(); #endif -#endif UWSGI_RELEASE_GIL @@ -306,7 +319,10 @@ UWSGI_RELEASE_GIL PyObject *uwsgi_pyimport_by_filename(char *name, char *filename) { -#ifndef UWSGI_PYPY +#ifdef UWSGI_PYPY + return NULL; +#else + FILE *pyfile; struct _node *py_file_node = NULL; PyObject *py_compiled_node, *py_file_module; @@ -391,8 +407,6 @@ PyObject *uwsgi_pyimport_by_filename(char *name, char *filename) { } return py_file_module; -#else - return NULL; #endif } @@ -698,7 +712,11 @@ void init_uwsgi_embedded_module() { #ifdef UNBIT if (PyDict_SetItemString(up.embedded_dict, "unbit", Py_True)) { #else - if (PyDict_SetItemString(up.embedded_dict, "unbit", Py_None)) { + uwsgi_log("UNO\n"); + Py_INCREF(Py_None); + uwsgi_log("UNO.0\n"); + if (PyDict_SetItemString(up.embedded_dict, "unbit", &_Py_NoneStruct)) { + uwsgi_log("DUE\n"); #endif PyErr_Print(); exit(1); @@ -910,18 +928,14 @@ void uwsgi_python_preinit_apps() { init_pyargv(); -#ifndef UWSGI_PYPY #ifdef UWSGI_EMBEDDED init_uwsgi_embedded_module(); #endif -#endif #ifdef __linux__ -#ifndef UWSGI_PYPY #ifdef UWSGI_EMBEDDED uwsgi_init_symbol_import(); #endif -#endif #endif if (up.test_module != NULL) { @@ -953,13 +967,13 @@ void uwsgi_python_init_apps() { struct http_status_codes *http_sc; +#ifndef UWSGI_PYPY // prepare for stack suspend/resume if (uwsgi.async > 1) { up.current_recursion_depth = uwsgi_malloc(sizeof(int)*uwsgi.async); -#ifndef UWSGI_PYPY up.current_frame = uwsgi_malloc(sizeof(struct _frame)*uwsgi.async); -#endif } +#endif // setup app loaders #ifdef UWSGI_MINTERPRETERS @@ -1051,13 +1065,13 @@ void uwsgi_python_init_apps() { init_uwsgi_app(LOADER_UWSGI, up.wsgi_lite, uwsgi.wsgi_req, up.main_thread, PYTHON_APP_TYPE_WSGI_LITE); } - if (uwsgi.profiler) { #ifndef UWSGI_PYPY + if (uwsgi.profiler) { if (!strcmp(uwsgi.profiler, "pycall")) { PyEval_SetProfile(uwsgi_python_profiler_call, NULL); } -#endif } +#endif PyObject *uwsgi_dict = get_uwsgi_pydict("uwsgi"); if (uwsgi_dict) { @@ -1404,6 +1418,7 @@ void uwsgi_python_hijack(void) { // the pyshell will be execute only in the first worker +#ifndef UWSGI_PYPY FILE *pyfile; if (up.pyrun) { uwsgi.workers[uwsgi.mywid].hijacked = 1; @@ -1417,8 +1432,8 @@ void uwsgi_python_hijack(void) { // could be never executed exit(0); } +#endif -#ifndef UWSGI_PYPY if (up.pyshell_oneshot && uwsgi.workers[uwsgi.mywid].hijacked_count > 0) { uwsgi.workers[uwsgi.mywid].hijacked = 0; return; @@ -1437,6 +1452,8 @@ void uwsgi_python_hijack(void) { } UWSGI_GET_GIL; PyImport_ImportModule("readline"); + +#ifndef UWSGI_PYPY int ret = PyRun_InteractiveLoop(stdin, "uwsgi"); if (up.pyshell_oneshot) { @@ -1446,9 +1463,9 @@ void uwsgi_python_hijack(void) { if (ret == 0) { exit(UWSGI_QUIET_CODE); } +#endif exit(0); } -#endif } int uwsgi_python_mule(char *opt) { @@ -1493,9 +1510,14 @@ int uwsgi_python_mule_msg(char *message, size_t len) { return 1; } -struct uwsgi_plugin python_plugin = { +#ifndef UWSGI_PYPY +struct uwsgi_plugin python_plugin = { .name = "python", +#else +struct uwsgi_plugin pypy_plugin = { + .name = "pypy", +#endif .alias = "python", .modifier1 = 0, .init = uwsgi_python_init, diff --git a/plugins/python/pyutils.c b/plugins/python/pyutils.c index 552420ac..9e4616bb 100644 --- a/plugins/python/pyutils.c +++ b/plugins/python/pyutils.c @@ -8,6 +8,7 @@ int manage_python_response(struct wsgi_request *wsgi_req) { return uwsgi_response_subhandler_wsgi(wsgi_req); } +#ifndef UWSGI_PYPY char *uwsgi_python_get_exception_type(PyObject *exc) { char *class_name = NULL; #ifndef PYTHREE @@ -90,6 +91,7 @@ int uwsgi_python_manage_exceptions(void) { return ret; } +#endif PyObject *python_call(PyObject *callable, PyObject *args, int catch, struct wsgi_request *wsgi_req) { @@ -103,7 +105,11 @@ PyObject *python_call(PyObject *callable, PyObject *args, int catch, struct wsgi if (PyErr_Occurred()) { +#ifndef UWSGI_PYPY int do_exit = uwsgi_python_manage_exceptions(); +#else + int do_exit = 0; +#endif if (PyErr_ExceptionMatches(PyExc_MemoryError)) { uwsgi_log("Memory Error detected !!!\n"); @@ -222,7 +228,9 @@ void init_pyargv() { } +#ifndef UWSGI_PYPY PySys_SetArgv(up.argc, up.py_argv); +#endif PyObject *sys_dict = get_uwsgi_pydict("sys"); if (!sys_dict) { diff --git a/plugins/python/wsgi_subhandler.c b/plugins/python/wsgi_subhandler.c index ffb74a6b..aa3f820d 100644 --- a/plugins/python/wsgi_subhandler.c +++ b/plugins/python/wsgi_subhandler.c @@ -222,7 +222,11 @@ int uwsgi_response_subhandler_wsgi(struct wsgi_request *wsgi_req) { if (!pychunk) { if (PyErr_Occurred()) { +#ifndef UWSGI_PYPY int do_exit = uwsgi_python_manage_exceptions(); +#else + int do_exit = 0; +#endif if (PyErr_ExceptionMatches(PyExc_MemoryError)) { uwsgi_log("Memory Error detected !!!\n"); }