From 60644b12aff0cf494a4a6115ed3e4ad1b21bc98c Mon Sep 17 00:00:00 2001 From: benoitc Date: Wed, 24 Oct 2012 14:24:19 +0200 Subject: [PATCH] miscellaneous fixes --- gunicorn/app/base.py | 9 ++------- gunicorn/app/django_wsgi.py | 4 +++- gunicorn/app/djangoapp.py | 2 +- gunicorn/app/pasterapp.py | 6 +++++- gunicorn/config.py | 7 ++++--- gunicorn/glogging.py | 6 ++++-- gunicorn/http/body.py | 10 +++++----- gunicorn/http/unreader.py | 7 ++----- gunicorn/http/wsgi.py | 9 +++++---- gunicorn/six.py | 5 +++++ gunicorn/sock.py | 4 ++-- gunicorn/util.py | 12 ++++++------ 12 files changed, 44 insertions(+), 37 deletions(-) diff --git a/gunicorn/app/base.py b/gunicorn/app/base.py index 443bf894..e5d4c996 100644 --- a/gunicorn/app/base.py +++ b/gunicorn/app/base.py @@ -3,17 +3,15 @@ # This file is part of gunicorn released under the MIT license. # See the NOTICE for more information. -import errno import os import sys import traceback - -from gunicorn.glogging import Logger from gunicorn import util from gunicorn.arbiter import Arbiter from gunicorn.config import Config from gunicorn import debug +from gunicorn.six import execfile_ class Application(object): """\ @@ -62,7 +60,7 @@ class Application(object): "__package__": None } try: - execfile(opts.config, cfg, cfg) + execfile_(opts.config, cfg, cfg) except Exception: print("Failed to read config file: %s" % opts.config) traceback.print_exc() @@ -104,15 +102,12 @@ class Application(object): def run(self): if self.cfg.check_config: try: - self.load() except: sys.stderr.write("\nError while loading the application:\n\n") traceback.print_exc() - sys.stderr.flush() sys.exit(1) - sys.exit(0) if self.cfg.spew: diff --git a/gunicorn/app/django_wsgi.py b/gunicorn/app/django_wsgi.py index 910dd265..7ec41bf3 100644 --- a/gunicorn/app/django_wsgi.py +++ b/gunicorn/app/django_wsgi.py @@ -10,10 +10,12 @@ import re import sys import time try: - from cStringIO import StringIO + from io import StringIO + from imp import reload except ImportError: from StringIO import StringIO + from django.conf import settings from django.core.management.validation import get_validation_errors from django.utils import translation diff --git a/gunicorn/app/djangoapp.py b/gunicorn/app/djangoapp.py index 0d20da4e..6eb5d574 100644 --- a/gunicorn/app/djangoapp.py +++ b/gunicorn/app/djangoapp.py @@ -49,7 +49,7 @@ def make_default_env(cfg): sys.path.insert(0, pythonpath) try: - _ = os.environ['DJANGO_SETTINGS_MODULE'] + os.environ['DJANGO_SETTINGS_MODULE'] except KeyError: # not settings env set, try to build one. project_path, settings_name = find_settings_module(os.getcwd()) diff --git a/gunicorn/app/pasterapp.py b/gunicorn/app/pasterapp.py index 2c7a2cdb..9a2e132d 100644 --- a/gunicorn/app/pasterapp.py +++ b/gunicorn/app/pasterapp.py @@ -6,7 +6,11 @@ import os import pkg_resources import sys -import ConfigParser + +try: + import configparser as ConfigParser +except ImportError: + import ConfigParser from paste.deploy import loadapp, loadwsgi SERVER = loadwsgi.SERVER diff --git a/gunicorn/config.py b/gunicorn/config.py index c9045cb0..fa3d15bb 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -15,6 +15,7 @@ import types from gunicorn import __version__ from gunicorn.errors import ConfigError from gunicorn import util +from gunicorn.six import string_types KNOWN_SETTINGS = [] @@ -181,7 +182,7 @@ class Setting(object): def validate_bool(val): if isinstance(val, types.BooleanType): return val - if not isinstance(val, basestring): + if not isinstance(val, string_types): raise TypeError("Invalid type for casting: %s" % val) if val.lower().strip() == "true": return True @@ -208,7 +209,7 @@ def validate_pos_int(val): def validate_string(val): if val is None: return None - if not isinstance(val, basestring): + if not isinstance(val, string_types): raise TypeError("Not a string: %s" % val) return val.strip() @@ -229,7 +230,7 @@ def validate_class(val): def validate_callable(arity): def _validate_callable(val): - if isinstance(val, basestring): + if isinstance(val, string_types): try: mod_name, obj_name = val.rsplit(".", 1) except ValueError: diff --git a/gunicorn/glogging.py b/gunicorn/glogging.py index d554a207..eaa82708 100644 --- a/gunicorn/glogging.py +++ b/gunicorn/glogging.py @@ -6,13 +6,15 @@ import datetime import logging logging.Logger.manager.emittedNoHandlerWarning = 1 +from logging.config import fileConfig import os import sys import traceback import threading -from logging.config import fileConfig + from gunicorn import util +from gunicorn.six import string_types CONFIG_DEFAULTS = dict( version = 1, @@ -175,7 +177,7 @@ class Logger(object): self.error_log.exception(msg, *args) def log(self, lvl, msg, *args, **kwargs): - if isinstance(lvl, basestring): + if isinstance(lvl, string_types): lvl = self.LOG_LEVELS.get(lvl.lower(), logging.INFO) self.error_log.log(lvl, msg, *args, **kwargs) diff --git a/gunicorn/http/body.py b/gunicorn/http/body.py index 82797e4c..e81e655a 100644 --- a/gunicorn/http/body.py +++ b/gunicorn/http/body.py @@ -7,7 +7,7 @@ import sys from gunicorn.http.errors import (NoMoreData, ChunkMissingTerminator, InvalidChunkSize) -from gunicorn.six import StringIO, bytes_to_str +from gunicorn.six import StringIO, bytes_to_str, integer_types class ChunkedReader(object): def __init__(self, req, unreader): @@ -16,7 +16,7 @@ class ChunkedReader(object): self.buf = StringIO() def read(self, size): - if not isinstance(size, (int, long)): + if not isinstance(size, integer_types): raise TypeError("size must be an integral type") if size <= 0: raise ValueError("Size must be positive.") @@ -111,7 +111,7 @@ class LengthReader(object): self.length = length def read(self, size): - if not isinstance(size, (int, long)): + if not isinstance(size, integer_types): raise TypeError("size must be an integral type") size = min(self.length, size) @@ -142,7 +142,7 @@ class EOFReader(object): self.finished = False def read(self, size): - if not isinstance(size, (int, long)): + if not isinstance(size, integer_types): raise TypeError("size must be an integral type") if size < 0: raise ValueError("Size must be positive.") @@ -189,7 +189,7 @@ class Body(object): def getsize(self, size): if size is None: return sys.maxint - elif not isinstance(size, (int, long)): + elif not isinstance(size, integer_types): raise TypeError("size must be an integral type") elif size < 0: return sys.maxint diff --git a/gunicorn/http/unreader.py b/gunicorn/http/unreader.py index be1ca3dc..1631a24a 100644 --- a/gunicorn/http/unreader.py +++ b/gunicorn/http/unreader.py @@ -5,10 +5,7 @@ import os -try: - from cStringIO import StringIO -except ImportError: - from StringIO import StringIO +from gunicorn.six import integer_types, StringIO # Classes that can undo reading data from # a given type of data source. @@ -21,7 +18,7 @@ class Unreader(object): raise NotImplementedError() def read(self, size=None): - if size is not None and not isinstance(size, (int, long)): + if size is not None and not isinstance(size, integer_types): raise TypeError("size parameter must be an int or long.") if size == 0: return "" diff --git a/gunicorn/http/wsgi.py b/gunicorn/http/wsgi.py index 9981389f..5ea54f28 100644 --- a/gunicorn/http/wsgi.py +++ b/gunicorn/http/wsgi.py @@ -8,7 +8,8 @@ import os import re import sys -from gunicorn.six import unquote, string_types, binary_type, reraise +from gunicorn.six import (unquote, string_types, binary_type, reraise, + text_type) from gunicorn import SERVER_SOFTWARE import gunicorn.util as util @@ -117,7 +118,7 @@ def create(req, sock, client, server, cfg): environ['wsgi.url_scheme'] = url_scheme - if isinstance(forward, basestring): + if isinstance(forward, string_types): # we only took the last one # http://en.wikipedia.org/wiki/X-Forwarded-For if forward.find(",") >= 0: @@ -144,7 +145,7 @@ def create(req, sock, client, server, cfg): environ['REMOTE_ADDR'] = remote[0] environ['REMOTE_PORT'] = str(remote[1]) - if isinstance(server, basestring): + if isinstance(server, string_types): server = server.split(":") if len(server) == 1: if url_scheme == "http": @@ -208,7 +209,7 @@ class Response(object): def process_headers(self, headers): for name, value in headers: - assert isinstance(name, basestring), "%r is not a string" % name + assert isinstance(name, string_types), "%r is not a string" % name lname = name.lower().strip() if lname == "content-length": self.response_length = int(value) diff --git a/gunicorn/six.py b/gunicorn/six.py index e82ddce2..76c15530 100644 --- a/gunicorn/six.py +++ b/gunicorn/six.py @@ -294,6 +294,10 @@ if PY3: print_ = getattr(builtins, "print") del builtins + def execfile_(file, globals=globals(), locals=locals()): + with open(file, "r") as fh: + exec_(fh.read()+"\n", globals, locals) + else: def exec_(code, globs=None, locs=None): """Execute code in a namespace.""" @@ -312,6 +316,7 @@ else: raise tp, value, tb """) + execfile_ = execfile def print_(*args, **kwargs): """The new-style print function.""" diff --git a/gunicorn/sock.py b/gunicorn/sock.py index e172ceb2..11dcd75e 100644 --- a/gunicorn/sock.py +++ b/gunicorn/sock.py @@ -10,7 +10,7 @@ import sys import time from gunicorn import util - +from gunicorn.six import string_types class BaseSocket(object): @@ -108,7 +108,7 @@ def create_socket(conf, log): sock_type = TCP6Socket else: sock_type = TCPSocket - elif isinstance(addr, basestring): + elif isinstance(addr, string_types): sock_type = UnixSocket else: raise TypeError("Unable to create socket from: %r" % addr) diff --git a/gunicorn/util.py b/gunicorn/util.py index 11731327..0f6895a7 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -25,7 +25,7 @@ import textwrap import time import inspect -from gunicorn.six import text_type +from gunicorn.six import text_type, string_types MAXFD = 1024 if (hasattr(os, "devnull")): @@ -75,7 +75,7 @@ except ImportError: if not hasattr(package, 'rindex'): raise ValueError("'package' not set to a string") dot = len(package) - for x in xrange(level, 1, -1): + for x in range(level, 1, -1): try: dot = package.rindex('.', 0, dot) except ValueError: @@ -217,14 +217,14 @@ try: except ImportError: def closerange(fd_low, fd_high): # Iterate through and close all file descriptors. - for fd in xrange(fd_low, fd_high): + for fd in range(fd_low, fd_high): try: os.close(fd) except OSError: # ERROR, fd wasn't open to begin with (ignored) pass def write_chunk(sock, data): - if instance(data, text_type): + if isinstance(data, text_type): data = data.decode('utf-8') chunk_size = "%X\r\n" % len(data) chunk = b"".join([chunk_size.decode('utf-8'), data, b"\r\n"]) @@ -263,7 +263,7 @@ def write_error(sock, status_int, reason, mesg): """) % {"reason": reason, "mesg": mesg} - headers = textwrap.dedent("""\ + http = textwrap.dedent("""\ HTTP/1.1 %s %s\r Connection: close\r Content-Type: text/html\r @@ -313,7 +313,7 @@ def http_date(timestamp=None): def to_bytestring(s): """ convert to bytestring an unicode """ - if not isinstance(s, basestring): + if not isinstance(s, string_types): return s if isinstance(s, unicode): return s.encode('utf-8')