Skip to content

Make devserver run on Python 3 #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions devserver/management/commands/runserver.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import print_function

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError, handle_default_options
from django.core.servers.basehttp import WSGIServerException, WSGIServer
Expand All @@ -7,15 +9,18 @@
import sys
import django
import imp
import SocketServer
try:
import SocketServer
except ImportError:
import socketserver as SocketServer
from optparse import make_option

from devserver.handlers import DevServerHandler
from devserver.utils.http import SlimWSGIRequestHandler


def null_technical_500_response(request, exc_type, exc_value, tb):
raise exc_type, exc_value, tb
raise exc_type(exc_value, tb)


def run(addr, port, wsgi_handler, mixin=None):
Expand Down Expand Up @@ -99,8 +104,8 @@ def handle(self, addrport='', *args, **options):
if use_werkzeug:
try:
from werkzeug import run_simple, DebuggedApplication
except ImportError, e:
print >> sys.stderr, "WARNING: Unable to initialize werkzeug: %s" % e
except ImportError as e:
print("WARNING: Unable to initialize werkzeug: %s" % e, file=sys.stderr)
use_werkzeug = False
else:
use_werkzeug = True
Expand All @@ -117,16 +122,16 @@ def inner_run():
from django.conf import settings
from django.utils import translation

print "Validating models..."
print("Validating models...")
self.validate(display_num_errors=True)
print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)
print "Running django-devserver %s" % (devserver.get_version(),)
print("\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE))
print("Running django-devserver %s" % (devserver.get_version(),))
if use_werkzeug:
server_type = 'werkzeug'
else:
server_type = 'django'
print "%s %s server is running at http://%s:%s/" % (options['use_forked'] and 'Forked' or 'Threaded', server_type, addr, port)
print "Quit the server with %s." % quit_command
print("%s %s server is running at http://%s:%s/" % (options['use_forked'] and 'Forked' or 'Threaded', server_type, addr, port))
print("Quit the server with %s." % quit_command)

# django.core.management.base forces the locale to en-us. We should
# set it up correctly for the first request (particularly important
Expand All @@ -139,7 +144,7 @@ def inner_run():
app = DevServerHandler()

if wsgi_app:
print "Using WSGI application %r" % wsgi_app
print("Using WSGI application %r" % wsgi_app)
if os.path.exists(os.path.abspath(wsgi_app)):
# load from file
app = imp.load_source('wsgi_app', os.path.abspath(wsgi_app)).application
Expand Down Expand Up @@ -182,7 +187,7 @@ def inner_run():
use_reloader=False, use_debugger=True)
else:
run(addr, int(port), app, mixin)
except WSGIServerException, e:
except WSGIServerException as e:
# Use helpful error messages instead of ugly tracebacks.
ERRORS = {
13: "You don't have permission to access that port.",
Expand All @@ -198,7 +203,7 @@ def inner_run():
os._exit(1)
except KeyboardInterrupt:
if shutdown_message:
print shutdown_message
print(shutdown_message)
sys.exit(0)

# werkzeug does its own autoreload stuff
Expand Down
8 changes: 4 additions & 4 deletions devserver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ def load_modules():
try:
name, class_name = path.rsplit('.', 1)
except ValueError:
raise exceptions.ImproperlyConfigured, '%s isn\'t a devserver module' % path
raise exceptions.ImproperlyConfigured('%s isn\'t a devserver module' % path)

try:
module = __import__(name, {}, {}, [''])
except ImportError, e:
raise exceptions.ImproperlyConfigured, 'Error importing devserver module %s: "%s"' % (name, e)
except ImportError as e:
raise exceptions.ImproperlyConfigured('Error importing devserver module %s: "%s"' % (name, e))

try:
cls = getattr(module, class_name)
except AttributeError:
raise exceptions.ImproperlyConfigured, 'Error importing devserver module "%s" does not define a "%s" class' % (name, class_name)
raise exceptions.ImproperlyConfigured('Error importing devserver module "%s" does not define a "%s" class' % (name, class_name))

try:
instance = cls(GenericLogger(cls))
Expand Down
7 changes: 5 additions & 2 deletions devserver/testcases.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import socket
import SocketServer
try:
import SocketServer
except ImportError:
import socketserver as SocketServer
import threading

from django.conf import settings
Expand Down Expand Up @@ -52,7 +55,7 @@ def __init__(self, *args, **kwargs):
httpd = new(server_address, SlimWSGIRequestHandler)
httpd.set_app(handler)
self.started.set()
except WSGIServerException, e:
except WSGIServerException as e:
self.error = e
self.started.set()
return
Expand Down
5 changes: 4 additions & 1 deletion devserver/utils/stack.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import django
import SocketServer
try:
import SocketServer
except ImportError:
import socketserver as SocketServer
import os.path

from django.conf import settings
Expand Down