From 53ce50bc7b089e5eb2d90e78bf9fa2df2cef52c9 Mon Sep 17 00:00:00 2001 From: benoitc Date: Wed, 24 Oct 2012 12:11:15 +0200 Subject: [PATCH] obvious syntax fixes preparing python3 support --- .../django/djangotest/testing/views.py | 24 +++++++++---------- .../testing/apps/someapp/middleware.py | 2 +- .../testing/testing/apps/someapp/views.py | 6 ++--- examples/longpoll.py | 8 ++++--- examples/multiapp.py | 6 ++--- examples/slowclient.py | 9 +++---- examples/test.py | 3 +-- gunicorn/app/base.py | 6 ++--- gunicorn/app/pasterapp.py | 2 +- gunicorn/arbiter.py | 14 +++++------ gunicorn/debug.py | 4 ++-- gunicorn/http/message.py | 2 +- gunicorn/logging_config.py | 2 +- gunicorn/pidfile.py | 4 ++-- gunicorn/sock.py | 6 ++--- gunicorn/util.py | 4 ++-- gunicorn/workers/async.py | 14 +++++------ 17 files changed, 59 insertions(+), 57 deletions(-) diff --git a/examples/frameworks/django/djangotest/testing/views.py b/examples/frameworks/django/djangotest/testing/views.py index 780cfdc2..482571cf 100755 --- a/examples/frameworks/django/djangotest/testing/views.py +++ b/examples/frameworks/django/djangotest/testing/views.py @@ -12,18 +12,18 @@ class MsgForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() f = forms.FileField() - + def home(request): from django.conf import settings - print settings.SOME_VALUE + print(settings.SOME_VALUE) subject = None message = None size = 0 - print request.META + print(request.META) if request.POST: form = MsgForm(request.POST, request.FILES) - print request.FILES + print(request.FILES) if form.is_valid(): subject = form.cleaned_data['subject'] message = form.cleaned_data['message'] @@ -31,29 +31,29 @@ def home(request): size = int(os.fstat(f.fileno())[6]) else: form = MsgForm() - - + + return render_to_response('home.html', { 'form': form, 'subject': subject, 'message': message, 'size': size }, RequestContext(request)) - - + + def acsv(request): rows = [ {'a': 1, 'b': 2}, {'a': 3, 'b': 3} ] - + response = HttpResponse(mimetype='text/csv') response['Content-Disposition'] = 'attachment; filename=report.csv' - + writer = csv.writer(response) writer.writerow(['a', 'b']) - + for r in rows: writer.writerow([r['a'], r['b']]) - + return response diff --git a/examples/frameworks/django/testing/testing/apps/someapp/middleware.py b/examples/frameworks/django/testing/testing/apps/someapp/middleware.py index 3fe5a825..c346e54f 100644 --- a/examples/frameworks/django/testing/testing/apps/someapp/middleware.py +++ b/examples/frameworks/django/testing/testing/apps/someapp/middleware.py @@ -4,7 +4,7 @@ import gevent def child_process(queue): while True: - print queue.get() + print(queue.get()) requests.get('http://requestb.in/15s95oz1') class GunicornSubProcessTestMiddleware(object): diff --git a/examples/frameworks/django/testing/testing/apps/someapp/views.py b/examples/frameworks/django/testing/testing/apps/someapp/views.py index 9ad0046d..d685b5bc 100755 --- a/examples/frameworks/django/testing/testing/apps/someapp/views.py +++ b/examples/frameworks/django/testing/testing/apps/someapp/views.py @@ -16,14 +16,14 @@ class MsgForm(forms.Form): def home(request): from django.conf import settings - print settings.SOME_VALUE + print(settings.SOME_VALUE) subject = None message = None size = 0 - print request.META + print(request.META) if request.POST: form = MsgForm(request.POST, request.FILES) - print request.FILES + print(request.FILES) if form.is_valid(): subject = form.cleaned_data['subject'] message = form.cleaned_data['message'] diff --git a/examples/longpoll.py b/examples/longpoll.py index 0bbbb283..19559d1b 100644 --- a/examples/longpoll.py +++ b/examples/longpoll.py @@ -1,13 +1,14 @@ # -*- coding: utf-8 - # -# This file is part of gunicorn released under the MIT license. +# This file is part of gunicorn released under the MIT license. # See the NOTICE for more information. +import sys import time class TestIter(object): - + def __iter__(self): lines = ['line 1\n', 'line 2\n'] for line in lines: @@ -22,6 +23,7 @@ def app(environ, start_response): ('Content-type','text/plain'), ('Transfer-Encoding', "chunked"), ] - print 'request received' + sys.stdout.write('request received') + sys.stdout.flush() start_response(status, response_headers) return TestIter() diff --git a/examples/multiapp.py b/examples/multiapp.py index 340fa7cc..e48a253d 100644 --- a/examples/multiapp.py +++ b/examples/multiapp.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 - # -# This file is part of gunicorn released under the MIT license. +# This file is part of gunicorn released under the MIT license. # See the NOTICE for more information. # # Run this application with: @@ -17,7 +17,7 @@ try: from routes import Mapper except: - print "This example requires Routes to be installed" + print("This example requires Routes to be installed") # Obviously you'd import your app callables # from different places... @@ -55,4 +55,4 @@ class Application(object): start_response('404 Not Found', headers) return [html] -app = Application() \ No newline at end of file +app = Application() diff --git a/examples/slowclient.py b/examples/slowclient.py index b4bc2019..6f612bea 100644 --- a/examples/slowclient.py +++ b/examples/slowclient.py @@ -1,9 +1,9 @@ # -*- coding: utf-8 - # -# This file is part of gunicorn released under the MIT license. +# This file is part of gunicorn released under the MIT license. # See the NOTICE for more information. - +import sys import time @@ -14,7 +14,8 @@ def app(environ, start_response): response_headers = [ ('Content-type','text/plain'), ('Content-Length', str(len(data))) ] - print 'request received, pausing 10 seconds' + sys.stdout.write('request received, pausing 10 seconds') + sys.stdout.flush() time.sleep(10) start_response(status, response_headers) - return iter([data]) \ No newline at end of file + return iter([data]) diff --git a/examples/test.py b/examples/test.py index cf55bf0d..8972f68d 100644 --- a/examples/test.py +++ b/examples/test.py @@ -14,8 +14,7 @@ def app(environ, start_response): """Simplest possible application object""" data = 'Hello, World!\n' status = '200 OK' -# print("print to stdout in test app") -# sys.stderr.write("stderr, print to stderr in test app\n") + response_headers = [ ('Content-type','text/plain'), ('Content-Length', str(len(data))), diff --git a/gunicorn/app/base.py b/gunicorn/app/base.py index d6917e72..443bf894 100644 --- a/gunicorn/app/base.py +++ b/gunicorn/app/base.py @@ -31,7 +31,7 @@ class Application(object): def do_load_config(self): try: self.load_config() - except Exception, e: + except Exception as e: sys.stderr.write("\nError: %s\n" % str(e)) sys.stderr.flush() sys.exit(1) @@ -64,7 +64,7 @@ class Application(object): try: execfile(opts.config, cfg, cfg) except Exception: - print "Failed to read config file: %s" % opts.config + print("Failed to read config file: %s" % opts.config) traceback.print_exc() sys.exit(1) @@ -122,7 +122,7 @@ class Application(object): try: Arbiter(self).run() - except RuntimeError, e: + except RuntimeError as e: sys.stderr.write("\nError: %s\n\n" % e) sys.stderr.flush() sys.exit(1) diff --git a/gunicorn/app/pasterapp.py b/gunicorn/app/pasterapp.py index df014723..2c7a2cdb 100644 --- a/gunicorn/app/pasterapp.py +++ b/gunicorn/app/pasterapp.py @@ -118,7 +118,7 @@ class PasterServerApplication(PasterBaseApplication): for k, v in cfg.items(): if k.lower() in self.cfg.settings and v is not None: self.cfg.set(k.lower(), v) - except Exception, e: + except Exception as e: sys.stderr.write("\nConfig error: %s\n" % str(e)) sys.stderr.flush() sys.exit(1) diff --git a/gunicorn/arbiter.py b/gunicorn/arbiter.py index 1e7d829a..4e0d665e 100644 --- a/gunicorn/arbiter.py +++ b/gunicorn/arbiter.py @@ -181,7 +181,7 @@ class Arbiter(object): self.halt() except KeyboardInterrupt: self.halt() - except HaltServer, inst: + except HaltServer as inst: self.halt(reason=inst.reason, exit_status=inst.exit_status) except SystemExit: raise @@ -271,7 +271,7 @@ class Arbiter(object): """ try: os.write(self.PIPE[1], '.') - except IOError, e: + except IOError as e: if e.errno not in [errno.EAGAIN, errno.EINTR]: raise @@ -296,10 +296,10 @@ class Arbiter(object): return while os.read(self.PIPE[0], 1): pass - except select.error, e: - if e[0] not in [errno.EAGAIN, errno.EINTR]: + except select.error as e: + if e.args[0] not in [errno.EAGAIN, errno.EINTR]: raise - except OSError, e: + except OSError as e: if e.errno not in [errno.EAGAIN, errno.EINTR]: raise except KeyboardInterrupt: @@ -423,7 +423,7 @@ class Arbiter(object): if not worker: continue worker.tmp.close() - except OSError, e: + except OSError as e: if e.errno == errno.ECHILD: pass @@ -504,7 +504,7 @@ class Arbiter(object): """ try: os.kill(pid, sig) - except OSError, e: + except OSError as e: if e.errno == errno.ESRCH: try: worker = self.WORKERS.pop(pid) diff --git a/gunicorn/debug.py b/gunicorn/debug.py index 3f342448..52cda798 100644 --- a/gunicorn/debug.py +++ b/gunicorn/debug.py @@ -43,7 +43,7 @@ class Spew(object): line = 'Unknown code named [%s]. VM instruction #%d' % ( frame.f_code.co_name, frame.f_lasti) if self.trace_names is None or name in self.trace_names: - print '%s:%s: %s' % (name, lineno, line.rstrip()) + print('%s:%s: %s' % (name, lineno, line.rstrip())) if not self.show_values: return self details = [] @@ -54,7 +54,7 @@ class Spew(object): if tok in frame.f_locals: details.append('%s=%r' % (tok, frame.f_locals[tok])) if details: - print "\t%s" % ' '.join(details) + print("\t%s" % ' '.join(details)) return self diff --git a/gunicorn/http/message.py b/gunicorn/http/message.py index ab3575fc..d24ea5d0 100644 --- a/gunicorn/http/message.py +++ b/gunicorn/http/message.py @@ -256,7 +256,7 @@ class Request(Message): try: remote_host = self.unreader.sock.getpeername()[0] except socket.error as e: - if e[0] == ENOTCONN: + if e.args[0] == ENOTCONN: raise ForbiddenProxyRequest("UNKNOW") raise if remote_host not in self.cfg.proxy_allow_ips: diff --git a/gunicorn/logging_config.py b/gunicorn/logging_config.py index f1dd6dd8..14e7fe63 100644 --- a/gunicorn/logging_config.py +++ b/gunicorn/logging_config.py @@ -286,7 +286,7 @@ def listen(port=DEFAULT_LOGGING_CONFIG_PORT): except: traceback.print_exc() os.remove(file) - except socket.error, e: + except socket.error as e: if type(e.args) != types.TupleType: raise else: diff --git a/gunicorn/pidfile.py b/gunicorn/pidfile.py index d04f941e..5fa38995 100644 --- a/gunicorn/pidfile.py +++ b/gunicorn/pidfile.py @@ -76,11 +76,11 @@ class Pidfile(object): try: os.kill(wpid, 0) return wpid - except OSError, e: + except OSError as e: if e[0] == errno.ESRCH: return raise - except IOError, e: + except IOError as e: if e[0] == errno.ENOENT: return raise diff --git a/gunicorn/sock.py b/gunicorn/sock.py index 9f0cc480..e172ceb2 100644 --- a/gunicorn/sock.py +++ b/gunicorn/sock.py @@ -44,7 +44,7 @@ class BaseSocket(object): def close(self): try: self.sock.close() - except socket.error, e: + except socket.error as e: self.log.info("Error while closing socket %s", str(e)) time.sleep(0.3) del self.sock @@ -117,7 +117,7 @@ def create_socket(conf, log): fd = int(os.environ.pop('GUNICORN_FD')) try: return sock_type(conf, log, fd=fd) - except socket.error, e: + except socket.error as e: if e[0] == errno.ENOTCONN: log.error("GUNICORN_FD should refer to an open socket.") else: @@ -130,7 +130,7 @@ def create_socket(conf, log): for i in range(5): try: return sock_type(conf, log) - except socket.error, e: + except socket.error as e: if e[0] == errno.EADDRINUSE: log.error("Connection in use: %s", str(addr)) if e[0] == errno.EADDRNOTAVAIL: diff --git a/gunicorn/util.py b/gunicorn/util.py index e919d53c..e6fdb5ed 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -125,7 +125,7 @@ def load_class(uri, default="sync", section="gunicorn.workers"): return pkg_resources.load_entry_point("gunicorn", section, uri) - except ImportError, e: + except ImportError as e: raise RuntimeError("class uri invalid or not found: " + "[%s]" % str(e)) klass = components.pop(-1) @@ -350,6 +350,6 @@ def seed(): def check_is_writeable(path): try: f = open(path, 'a') - except IOError, e: + except IOError as e: raise RuntimeError("Error: '%s' isn't writable [%r]" % (path, e)) f.close() diff --git a/gunicorn/workers/async.py b/gunicorn/workers/async.py index 65b16981..08fed0ee 100644 --- a/gunicorn/workers/async.py +++ b/gunicorn/workers/async.py @@ -43,25 +43,25 @@ class AsyncWorker(base.Worker): if not req: break self.handle_request(req, client, addr) - except http.errors.NoMoreData, e: + except http.errors.NoMoreData as e: self.log.debug("Ignored premature client disconnection. %s", e) - except StopIteration, e: + except StopIteration as e: self.log.debug("Closing connection. %s", e) except socket.error: raise # pass to next try-except level - except Exception, e: + except Exception as e: self.handle_error(req, client, addr, e) except socket.timeout as e: self.handle_error(req, client, addr, e) - except socket.error, e: - if e[0] not in (errno.EPIPE, errno.ECONNRESET): + except socket.error as e: + if e.args[0] not in (errno.EPIPE, errno.ECONNRESET): self.log.exception("Socket error processing request.") else: - if e[0] == errno.ECONNRESET: + if e.args[0] == errno.ECONNRESET: self.log.debug("Ignoring connection reset") else: self.log.debug("Ignoring EPIPE") - except Exception, e: + except Exception as e: self.handle_error(req, client, addr, e) finally: util.close(client)