From 07fd84f89fdfd235c976407255808e0ac37eb091 Mon Sep 17 00:00:00 2001 From: "roberto@debian32" Date: Mon, 24 Oct 2011 16:35:59 +0200 Subject: [PATCH] ported symimporter/symzipimporter to python3 --- bootstrap.py | 1 + plugins/python/python_plugin.c | 4 ++- plugins/python/symimporter.c | 46 +++++++++++++++++++++++++++++++--- plugins/python/uwsgi_python.h | 2 -- welcome.py | 12 ++++----- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/bootstrap.py b/bootstrap.py index c711fc05..aa77c4c2 100644 --- a/bootstrap.py +++ b/bootstrap.py @@ -1,4 +1,5 @@ import sys import uwsgi +print("i am the bootstrap for uwsgi.SymbolsImporter") sys.meta_path.insert(0, uwsgi.SymbolsImporter()) diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index 6d92c627..8b2b2f44 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -915,9 +915,11 @@ void uwsgi_python_init_apps() { #ifdef __linux__ -#if !defined(PYTHREE) && !defined(UWSGI_PYPY) +#ifndef UWSGI_PYPY +#ifdef UWSGI_EMBEDDED uwsgi_init_symbol_import(); #endif +#endif #endif if (up.test_module != NULL) { diff --git a/plugins/python/symimporter.c b/plugins/python/symimporter.c index 7bc1b0e9..a91d6fcd 100644 --- a/plugins/python/symimporter.c +++ b/plugins/python/symimporter.c @@ -1,6 +1,6 @@ #include "uwsgi_python.h" -#if !defined(PYTHREE) && !defined(UWSGI_PYPY) +#ifndef UWSGI_PYPY extern struct uwsgi_server uwsgi; extern struct uwsgi_python up; @@ -201,9 +201,14 @@ static PyObject* symzipimporter_load_module(PyObject *self, PyObject *args) { PyObject *source = PyObject_CallMethod(this->zip, "read", "(s)", filename); free(filename); PyObject *code = Py_CompileString(PyString_AsString(source), modname, Py_file_input); + if (!code) { + PyErr_Print(); + goto shit; + } mod = PyImport_ExecCodeModuleEx(fullname, code, modname); Py_DECREF(code); +shit: Py_DECREF(source); free(modname); return mod; @@ -231,9 +236,14 @@ static PyObject* symzipimporter_load_module(PyObject *self, PyObject *args) { PyObject *source = PyObject_CallMethod(this->zip, "read", "(s)", filename); free(filename); PyObject *code = Py_CompileString(PyString_AsString(source), modname, Py_file_input); + if (!code) { + PyErr_Print(); + goto shit2; + } mod = PyImport_ExecCodeModuleEx(fullname, code, modname); Py_DECREF(code); +shit2: Py_DECREF(source); free(modname); return mod; @@ -306,9 +316,14 @@ static PyObject* symimporter_load_module(PyObject *self, PyObject *args) { modname = uwsgi_concat3("sym://", fullname2, "_py"); code = Py_CompileString(source, modname, Py_file_input); + if (!code) { + PyErr_Print(); + goto shit; + } mod = PyImport_ExecCodeModuleEx(fullname, code, modname); Py_DECREF(code); +shit: free(source); free(modname); free(fullname2); @@ -334,9 +349,14 @@ static PyObject* symimporter_load_module(PyObject *self, PyObject *args) { PyDict_SetItemString(dict, "__loader__", self); code = Py_CompileString(source, modname, Py_file_input); + if (!code) { + PyErr_Print(); + goto shit2; + } mod = PyImport_ExecCodeModuleEx(fullname, code, modname); Py_DECREF(code); +shit2: free(source); free(modname); free(fullname2); @@ -450,7 +470,10 @@ zipimporter_init(struct _symzipimporter *self, PyObject *args, PyObject *kwds) } - PyObject *stringio_dict = PyModule_GetDict(stringio); +#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; } @@ -466,7 +489,9 @@ zipimporter_init(struct _symzipimporter *self, PyObject *args, PyObject *kwds) 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; } @@ -480,7 +505,10 @@ zipimporter_init(struct _symzipimporter *self, PyObject *args, PyObject *kwds) } - PyObject *zipfile_dict = PyModule_GetDict(zipfile); +#ifdef PYTHREE + self->zip = PyObject_CallMethodObjArgs(zipfile, PyString_FromString("ZipFile"), source_code); +#else + PyObject *zipfile_dict = PyModule_GetDict(zipfile); if (!zipfile_dict) { return -1; } @@ -494,7 +522,9 @@ zipimporter_init(struct _symzipimporter *self, PyObject *args, PyObject *kwds) 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; } @@ -556,6 +586,9 @@ symzipimporter_init(struct _symzipimporter *self, PyObject *args, PyObject *kwds return -1; } +#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) { return -1; @@ -569,7 +602,9 @@ symzipimporter_init(struct _symzipimporter *self, PyObject *args, PyObject *kwds 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) { return -1; } @@ -579,6 +614,9 @@ symzipimporter_init(struct _symzipimporter *self, PyObject *args, PyObject *kwds return -1; } +#ifdef PYTHREE + self->zip = PyObject_CallMethodObjArgs(zipfile, PyString_FromString("ZipFile"), source_code); +#else PyObject *zipfile_dict = PyModule_GetDict(zipfile); if (!zipfile_dict) { return -1; @@ -592,7 +630,9 @@ symzipimporter_init(struct _symzipimporter *self, PyObject *args, PyObject *kwds 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; } diff --git a/plugins/python/uwsgi_python.h b/plugins/python/uwsgi_python.h index 8845d36a..975e3062 100644 --- a/plugins/python/uwsgi_python.h +++ b/plugins/python/uwsgi_python.h @@ -275,7 +275,5 @@ char *uwsgi_pythonize(char *); #define uwsgi_pyexit {PyErr_Print();exit(1);} #ifdef __linux__ -#ifndef PYTHREE int uwsgi_init_symbol_import(void); #endif -#endif diff --git a/welcome.py b/welcome.py index 882252bb..ce09c2cd 100644 --- a/welcome.py +++ b/welcome.py @@ -5,9 +5,9 @@ import sys from uwsgidecorators import * gc.set_debug(gc.DEBUG_SAVEALL) -print os.environ -print sys.modules -print sys.argv +print(os.environ) +print(sys.modules) +print(sys.argv) try: if sys.argv[1] == 'debug': @@ -58,7 +58,7 @@ def application(env, start_response): gc.collect(2) if DEBUG: - print env['wsgi.input'].fileno() + print(env['wsgi.input'].fileno()) if routes.has_key(env['PATH_INFO']): return routes[env['PATH_INFO']](env, start_response) @@ -66,12 +66,12 @@ def application(env, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) if DEBUG: - print env['wsgi.input'].fileno() + print(env['wsgi.input'].fileno()) gc.collect(2) if DEBUG: - print len(gc.get_objects()) + print(len(gc.get_objects())) workers = '' for w in uwsgi.workers():