Skip to content

Commit e607ee7

Browse files
committed
Handle removal of WSGIServerException in Django 1.6
1 parent a008428 commit e607ee7

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

devserver/management/commands/runserver.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
from django.conf import settings
22
from django.core.management.commands.runserver import Command as BaseCommand
33
from django.core.management.base import CommandError, handle_default_options
4-
from django.core.servers.basehttp import WSGIServerException, WSGIServer
4+
from django.core.servers.basehttp import WSGIServer
55
from django.core.handlers.wsgi import WSGIHandler
66

77
import os
88
import sys
99
import imp
10+
import errno
11+
import socket
1012
import SocketServer
1113
from optparse import make_option
1214

1315
from devserver.handlers import DevServerHandler
1416
from devserver.utils.http import SlimWSGIRequestHandler
1517

18+
try:
19+
from django.core.servers.basehttp import (WSGIServerException as
20+
wsgi_server_exc_cls)
21+
except ImportError: # Django 1.6
22+
wsgi_server_exc_cls = socket.error
23+
1624

1725
STATICFILES_APPS = ('django.contrib.staticfiles', 'staticfiles')
1826

@@ -189,15 +197,23 @@ def inner_run(self, *args, **options):
189197
else:
190198
run(self.addr, int(self.port), app, mixin, ipv6=options['use_ipv6'])
191199

192-
except WSGIServerException, e:
200+
except wsgi_server_exc_cls, e:
193201
# Use helpful error messages instead of ugly tracebacks.
194202
ERRORS = {
195-
13: "You don't have permission to access that port.",
196-
98: "That port is already in use.",
197-
99: "That IP address can't be assigned-to.",
203+
errno.EACCES: "You don't have permission to access that port.",
204+
errno.EADDRINUSE: "That port is already in use.",
205+
errno.EADDRNOTAVAIL: "That IP address can't be assigned-to.",
198206
}
207+
if not isinstance(e, socket.error): # Django < 1.6
208+
ERRORS[13] = ERRORS.pop(errno.EACCES)
209+
ERRORS[98] = ERRORS.pop(errno.EADDRINUSE)
210+
ERRORS[99] = ERRORS.pop(errno.EADDRNOTAVAIL)
211+
199212
try:
200-
error_text = ERRORS[e.args[0].args[0]]
213+
if not isinstance(e, socket.error): # Django < 1.6
214+
error_text = ERRORS[e.args[0].args[0]]
215+
else:
216+
error_text = ERRORS[e.errno]
201217
except (AttributeError, KeyError):
202218
error_text = str(e)
203219
sys.stderr.write(self.style.ERROR("Error: %s" % error_text) + '\n')

devserver/testcases.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
from django.conf import settings
66
from django.core.handlers.wsgi import WSGIHandler
77
from django.core.management import call_command
8-
from django.core.servers.basehttp import WSGIServer, AdminMediaHandler, WSGIServerException
8+
from django.core.servers.basehttp import WSGIServer, AdminMediaHandler
99

1010
from devserver.utils.http import SlimWSGIRequestHandler
1111

12+
try:
13+
from django.core.servers.basehttp import (WSGIServerException as
14+
wsgi_server_exc_cls)
15+
except ImportError: # Django 1.6
16+
wsgi_server_exc_cls = socket.error
17+
1218

1319
class StoppableWSGIServer(WSGIServer):
1420
"""WSGIServer with short timeout, so that server thread can stop this server."""
@@ -52,7 +58,7 @@ def __init__(self, *args, **kwargs):
5258
httpd = new(server_address, SlimWSGIRequestHandler)
5359
httpd.set_app(handler)
5460
self.started.set()
55-
except WSGIServerException, e:
61+
except wsgi_server_exc_cls, e:
5662
self.error = e
5763
self.started.set()
5864
return

0 commit comments

Comments
 (0)