miscellaneous fixes

This commit is contained in:
benoitc 2012-10-24 14:24:19 +02:00
parent 039bf47c3d
commit 60644b12af
12 changed files with 44 additions and 37 deletions

View File

@ -3,17 +3,15 @@
# 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. # See the NOTICE for more information.
import errno
import os import os
import sys import sys
import traceback import traceback
from gunicorn.glogging import Logger
from gunicorn import util from gunicorn import util
from gunicorn.arbiter import Arbiter from gunicorn.arbiter import Arbiter
from gunicorn.config import Config from gunicorn.config import Config
from gunicorn import debug from gunicorn import debug
from gunicorn.six import execfile_
class Application(object): class Application(object):
"""\ """\
@ -62,7 +60,7 @@ class Application(object):
"__package__": None "__package__": None
} }
try: try:
execfile(opts.config, cfg, cfg) execfile_(opts.config, cfg, cfg)
except Exception: except Exception:
print("Failed to read config file: %s" % opts.config) print("Failed to read config file: %s" % opts.config)
traceback.print_exc() traceback.print_exc()
@ -104,15 +102,12 @@ class Application(object):
def run(self): def run(self):
if self.cfg.check_config: if self.cfg.check_config:
try: try:
self.load() self.load()
except: except:
sys.stderr.write("\nError while loading the application:\n\n") sys.stderr.write("\nError while loading the application:\n\n")
traceback.print_exc() traceback.print_exc()
sys.stderr.flush() sys.stderr.flush()
sys.exit(1) sys.exit(1)
sys.exit(0) sys.exit(0)
if self.cfg.spew: if self.cfg.spew:

View File

@ -10,10 +10,12 @@ import re
import sys import sys
import time import time
try: try:
from cStringIO import StringIO from io import StringIO
from imp import reload
except ImportError: except ImportError:
from StringIO import StringIO from StringIO import StringIO
from django.conf import settings from django.conf import settings
from django.core.management.validation import get_validation_errors from django.core.management.validation import get_validation_errors
from django.utils import translation from django.utils import translation

View File

@ -49,7 +49,7 @@ def make_default_env(cfg):
sys.path.insert(0, pythonpath) sys.path.insert(0, pythonpath)
try: try:
_ = os.environ['DJANGO_SETTINGS_MODULE'] os.environ['DJANGO_SETTINGS_MODULE']
except KeyError: except KeyError:
# not settings env set, try to build one. # not settings env set, try to build one.
project_path, settings_name = find_settings_module(os.getcwd()) project_path, settings_name = find_settings_module(os.getcwd())

View File

@ -6,7 +6,11 @@
import os import os
import pkg_resources import pkg_resources
import sys import sys
import ConfigParser
try:
import configparser as ConfigParser
except ImportError:
import ConfigParser
from paste.deploy import loadapp, loadwsgi from paste.deploy import loadapp, loadwsgi
SERVER = loadwsgi.SERVER SERVER = loadwsgi.SERVER

View File

@ -15,6 +15,7 @@ import types
from gunicorn import __version__ from gunicorn import __version__
from gunicorn.errors import ConfigError from gunicorn.errors import ConfigError
from gunicorn import util from gunicorn import util
from gunicorn.six import string_types
KNOWN_SETTINGS = [] KNOWN_SETTINGS = []
@ -181,7 +182,7 @@ class Setting(object):
def validate_bool(val): def validate_bool(val):
if isinstance(val, types.BooleanType): if isinstance(val, types.BooleanType):
return val return val
if not isinstance(val, basestring): if not isinstance(val, string_types):
raise TypeError("Invalid type for casting: %s" % val) raise TypeError("Invalid type for casting: %s" % val)
if val.lower().strip() == "true": if val.lower().strip() == "true":
return True return True
@ -208,7 +209,7 @@ def validate_pos_int(val):
def validate_string(val): def validate_string(val):
if val is None: if val is None:
return None return None
if not isinstance(val, basestring): if not isinstance(val, string_types):
raise TypeError("Not a string: %s" % val) raise TypeError("Not a string: %s" % val)
return val.strip() return val.strip()
@ -229,7 +230,7 @@ def validate_class(val):
def validate_callable(arity): def validate_callable(arity):
def _validate_callable(val): def _validate_callable(val):
if isinstance(val, basestring): if isinstance(val, string_types):
try: try:
mod_name, obj_name = val.rsplit(".", 1) mod_name, obj_name = val.rsplit(".", 1)
except ValueError: except ValueError:

View File

@ -6,13 +6,15 @@
import datetime import datetime
import logging import logging
logging.Logger.manager.emittedNoHandlerWarning = 1 logging.Logger.manager.emittedNoHandlerWarning = 1
from logging.config import fileConfig
import os import os
import sys import sys
import traceback import traceback
import threading import threading
from logging.config import fileConfig
from gunicorn import util from gunicorn import util
from gunicorn.six import string_types
CONFIG_DEFAULTS = dict( CONFIG_DEFAULTS = dict(
version = 1, version = 1,
@ -175,7 +177,7 @@ class Logger(object):
self.error_log.exception(msg, *args) self.error_log.exception(msg, *args)
def log(self, lvl, msg, *args, **kwargs): 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) lvl = self.LOG_LEVELS.get(lvl.lower(), logging.INFO)
self.error_log.log(lvl, msg, *args, **kwargs) self.error_log.log(lvl, msg, *args, **kwargs)

View File

@ -7,7 +7,7 @@ import sys
from gunicorn.http.errors import (NoMoreData, ChunkMissingTerminator, from gunicorn.http.errors import (NoMoreData, ChunkMissingTerminator,
InvalidChunkSize) InvalidChunkSize)
from gunicorn.six import StringIO, bytes_to_str from gunicorn.six import StringIO, bytes_to_str, integer_types
class ChunkedReader(object): class ChunkedReader(object):
def __init__(self, req, unreader): def __init__(self, req, unreader):
@ -16,7 +16,7 @@ class ChunkedReader(object):
self.buf = StringIO() self.buf = StringIO()
def read(self, size): def read(self, size):
if not isinstance(size, (int, long)): if not isinstance(size, integer_types):
raise TypeError("size must be an integral type") raise TypeError("size must be an integral type")
if size <= 0: if size <= 0:
raise ValueError("Size must be positive.") raise ValueError("Size must be positive.")
@ -111,7 +111,7 @@ class LengthReader(object):
self.length = length self.length = length
def read(self, size): def read(self, size):
if not isinstance(size, (int, long)): if not isinstance(size, integer_types):
raise TypeError("size must be an integral type") raise TypeError("size must be an integral type")
size = min(self.length, size) size = min(self.length, size)
@ -142,7 +142,7 @@ class EOFReader(object):
self.finished = False self.finished = False
def read(self, size): def read(self, size):
if not isinstance(size, (int, long)): if not isinstance(size, integer_types):
raise TypeError("size must be an integral type") raise TypeError("size must be an integral type")
if size < 0: if size < 0:
raise ValueError("Size must be positive.") raise ValueError("Size must be positive.")
@ -189,7 +189,7 @@ class Body(object):
def getsize(self, size): def getsize(self, size):
if size is None: if size is None:
return sys.maxint return sys.maxint
elif not isinstance(size, (int, long)): elif not isinstance(size, integer_types):
raise TypeError("size must be an integral type") raise TypeError("size must be an integral type")
elif size < 0: elif size < 0:
return sys.maxint return sys.maxint

View File

@ -5,10 +5,7 @@
import os import os
try: from gunicorn.six import integer_types, StringIO
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
# Classes that can undo reading data from # Classes that can undo reading data from
# a given type of data source. # a given type of data source.
@ -21,7 +18,7 @@ class Unreader(object):
raise NotImplementedError() raise NotImplementedError()
def read(self, size=None): 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.") raise TypeError("size parameter must be an int or long.")
if size == 0: if size == 0:
return "" return ""

View File

@ -8,7 +8,8 @@ import os
import re import re
import sys 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 from gunicorn import SERVER_SOFTWARE
import gunicorn.util as util import gunicorn.util as util
@ -117,7 +118,7 @@ def create(req, sock, client, server, cfg):
environ['wsgi.url_scheme'] = url_scheme environ['wsgi.url_scheme'] = url_scheme
if isinstance(forward, basestring): if isinstance(forward, string_types):
# we only took the last one # we only took the last one
# http://en.wikipedia.org/wiki/X-Forwarded-For # http://en.wikipedia.org/wiki/X-Forwarded-For
if forward.find(",") >= 0: if forward.find(",") >= 0:
@ -144,7 +145,7 @@ def create(req, sock, client, server, cfg):
environ['REMOTE_ADDR'] = remote[0] environ['REMOTE_ADDR'] = remote[0]
environ['REMOTE_PORT'] = str(remote[1]) environ['REMOTE_PORT'] = str(remote[1])
if isinstance(server, basestring): if isinstance(server, string_types):
server = server.split(":") server = server.split(":")
if len(server) == 1: if len(server) == 1:
if url_scheme == "http": if url_scheme == "http":
@ -208,7 +209,7 @@ class Response(object):
def process_headers(self, headers): def process_headers(self, headers):
for name, value in 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() lname = name.lower().strip()
if lname == "content-length": if lname == "content-length":
self.response_length = int(value) self.response_length = int(value)

View File

@ -294,6 +294,10 @@ if PY3:
print_ = getattr(builtins, "print") print_ = getattr(builtins, "print")
del builtins del builtins
def execfile_(file, globals=globals(), locals=locals()):
with open(file, "r") as fh:
exec_(fh.read()+"\n", globals, locals)
else: else:
def exec_(code, globs=None, locs=None): def exec_(code, globs=None, locs=None):
"""Execute code in a namespace.""" """Execute code in a namespace."""
@ -312,6 +316,7 @@ else:
raise tp, value, tb raise tp, value, tb
""") """)
execfile_ = execfile
def print_(*args, **kwargs): def print_(*args, **kwargs):
"""The new-style print function.""" """The new-style print function."""

View File

@ -10,7 +10,7 @@ import sys
import time import time
from gunicorn import util from gunicorn import util
from gunicorn.six import string_types
class BaseSocket(object): class BaseSocket(object):
@ -108,7 +108,7 @@ def create_socket(conf, log):
sock_type = TCP6Socket sock_type = TCP6Socket
else: else:
sock_type = TCPSocket sock_type = TCPSocket
elif isinstance(addr, basestring): elif isinstance(addr, string_types):
sock_type = UnixSocket sock_type = UnixSocket
else: else:
raise TypeError("Unable to create socket from: %r" % addr) raise TypeError("Unable to create socket from: %r" % addr)

View File

@ -25,7 +25,7 @@ import textwrap
import time import time
import inspect import inspect
from gunicorn.six import text_type from gunicorn.six import text_type, string_types
MAXFD = 1024 MAXFD = 1024
if (hasattr(os, "devnull")): if (hasattr(os, "devnull")):
@ -75,7 +75,7 @@ except ImportError:
if not hasattr(package, 'rindex'): if not hasattr(package, 'rindex'):
raise ValueError("'package' not set to a string") raise ValueError("'package' not set to a string")
dot = len(package) dot = len(package)
for x in xrange(level, 1, -1): for x in range(level, 1, -1):
try: try:
dot = package.rindex('.', 0, dot) dot = package.rindex('.', 0, dot)
except ValueError: except ValueError:
@ -217,14 +217,14 @@ try:
except ImportError: except ImportError:
def closerange(fd_low, fd_high): def closerange(fd_low, fd_high):
# Iterate through and close all file descriptors. # Iterate through and close all file descriptors.
for fd in xrange(fd_low, fd_high): for fd in range(fd_low, fd_high):
try: try:
os.close(fd) os.close(fd)
except OSError: # ERROR, fd wasn't open to begin with (ignored) except OSError: # ERROR, fd wasn't open to begin with (ignored)
pass pass
def write_chunk(sock, data): def write_chunk(sock, data):
if instance(data, text_type): if isinstance(data, text_type):
data = data.decode('utf-8') data = data.decode('utf-8')
chunk_size = "%X\r\n" % len(data) chunk_size = "%X\r\n" % len(data)
chunk = b"".join([chunk_size.decode('utf-8'), data, b"\r\n"]) chunk = b"".join([chunk_size.decode('utf-8'), data, b"\r\n"])
@ -263,7 +263,7 @@ def write_error(sock, status_int, reason, mesg):
</html> </html>
""") % {"reason": reason, "mesg": mesg} """) % {"reason": reason, "mesg": mesg}
headers = textwrap.dedent("""\ http = textwrap.dedent("""\
HTTP/1.1 %s %s\r HTTP/1.1 %s %s\r
Connection: close\r Connection: close\r
Content-Type: text/html\r Content-Type: text/html\r
@ -313,7 +313,7 @@ def http_date(timestamp=None):
def to_bytestring(s): def to_bytestring(s):
""" convert to bytestring an unicode """ """ convert to bytestring an unicode """
if not isinstance(s, basestring): if not isinstance(s, string_types):
return s return s
if isinstance(s, unicode): if isinstance(s, unicode):
return s.encode('utf-8') return s.encode('utf-8')