Opened 11 years ago

Closed 11 years ago

#21420 closed Bug (fixed)

Runserver autoreload defect on OSX

Reported by: Bouke Haarsma Owned by: nobody
Component: Core (Management commands) Version: dev
Severity: Release blocker Keywords:
Cc: Baptiste Mispelon Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

  File "/Users/bouke/Sites/django/django/utils/autoreload.py", line 359, in main
    reloader(wrapped_main_func, args, kwargs)
  File "/Users/bouke/Sites/django/django/utils/autoreload.py", line 325, in python_reloader
    reloader_thread()
  File "/Users/bouke/Sites/django/django/utils/autoreload.py", line 304, in reloader_thread
    if fn():
  File "/Users/bouke/Sites/django/django/utils/autoreload.py", line 173, in kqueue_code_changed
    watcher = tempfile.TemporaryFile(bufsize=0)
  TypeError: TemporaryFile() got an unexpected keyword argument 'bufsize'

It appears that PY3 doesn't have the bufsize argument on TemporaryFile (docs).

Change History (7)

comment:1 by Baptiste Mispelon, 11 years ago

Cc: Baptiste Mispelon added
Has patch: set
Triage Stage: UnreviewedAccepted

Indeed, it seems that the bufsize argument has been renamed to buffering in Python 3.

Would the following patch fix the issue (I don't have an OSX box to test this):

diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py
index 97522b9..3e0db5a 100644
--- a/django/utils/autoreload.py
+++ b/django/utils/autoreload.py
@@ -37,6 +37,7 @@ import traceback
 
 from django.conf import settings
 from django.core.signals import request_finished
+from django.utils import six
 try:
     from django.utils.six.moves import _thread as thread
 except ImportError:
@@ -170,7 +171,10 @@ def kqueue_code_changed():
 
     # New modules may get imported when a request is processed. We add a file
     # descriptor to the kqueue to exit the kqueue.control after each request.
-    watcher = tempfile.TemporaryFile(bufsize=0)
+    if six.PY2:
+        watcher = tempfile.TemporaryFile(bufsize=0)
+    else:
+        watcher = tempfile.TemporaryFile(buffering=0)
     kqueue.control([make_kevent(watcher)], 0)
 
     def update_watch(sender=None, **kwargs):

comment:2 by Aymeric Augustin <aymeric.augustin@…>, 11 years ago

Resolution: fixed
Status: newclosed

In dbbd10e75f2f078909d231b2fd5ca1a351726faa:

Fixed #21420 -- Autoreloader on BSD with Python 3.

Thanks Bouke Haarsma for the report.

comment:3 by Bouke Haarsma, 11 years ago

Has patch: unset
Resolution: fixed
Status: closednew

Now it generates exceptions after processing a request:

Exception happened during processing of request from ('127.0.0.1', 53045)
Traceback (most recent call last):
  File "python3.3/wsgiref/handlers.py", line 138, in run
    self.finish_response()
  File "python3.3/wsgiref/handlers.py", line 183, in finish_response
    self.close()
  File "python3.3/wsgiref/simple_server.py", line 38, in close
    SimpleHandler.close(self)
  File "python3.3/wsgiref/handlers.py", line 321, in close
    self.result.close()
  File "django/http/response.py", line 303, in close
    signals.request_finished.send(sender=self._handler_class)
  File "django/dispatch/dispatcher.py", line 185, in send
    response = receiver(signal=self, sender=sender, **named)
  File "django/utils/autoreload.py", line 187, in update_watch
    watcher.write('.')
TypeError: 'str' does not support the buffer interface

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "python3.3/wsgiref/handlers.py", line 141, in run
    self.handle_error()
  File "python3.3/wsgiref/handlers.py", line 368, in handle_error
    self.finish_response()
  File "python3.3/wsgiref/handlers.py", line 180, in finish_response
    self.write(data)
  File "python3.3/wsgiref/handlers.py", line 274, in write
    self.send_headers()
  File "python3.3/wsgiref/handlers.py", line 331, in send_headers
    if not self.origin_server or self.client_is_modern():
  File "python3.3/wsgiref/handlers.py", line 344, in client_is_modern
    return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "python3.3/socketserver.py", line 610, in process_request_thread
    self.finish_request(request, client_address)
  File "python3.3/socketserver.py", line 345, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "django/core/servers/basehttp.py", line 119, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "python3.3/socketserver.py", line 666, in __init__
    self.handle()
  File "python3.3/wsgiref/simple_server.py", line 126, in handle
    handler.run(self.server.get_app())
  File "python3.3/wsgiref/handlers.py", line 144, in run
    self.close()
  File "python3.3/wsgiref/simple_server.py", line 35, in close
    self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'

comment:4 by Baptiste Mispelon, 11 years ago

This happens because line 187 is passing a unicode string to the write method of the temporary file.

The doc says that temporary files are opened in w+b mode by default, hence the error.

Does it work if you replace the line with watcher.write(b'.')?

comment:5 by Aymeric Augustin <aymeric.augustin@…>, 11 years ago

Resolution: fixed
Status: newclosed

comment:6 by geniofuturo@…, 11 years ago

Resolution: fixed
Status: closednew
Summary: Runserver autoreload defect on OSXRunserver autoreload defect on Windows 8

I'm using the django version 1.6.1

Python 2.7.5
OS: Windows 8

Added the 'grappelli' version 2.5.1 package and am having the same problem.

0 errors found
December 27, 2013 - 13:11:21
Django version 1.6.1, using settings 'Caefe.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[27/Dec/2013 13:11:26] "GET /admin/votacao/funcionario/1/ HTTP/1.1" 200 0
Traceback (most recent call last):
  File "C:\Python27\lib\wsgiref\handlers.py", line 86, in run
    self.finish_response()
  File "C:\Python27\lib\wsgiref\handlers.py", line 127, in finish_response
    for data in self.result:
  File "C:\Python27\lib\site-packages\django\utils\six.py", line 414, in next
    return type(self).__next__(self)
  File "C:\Python27\lib\site-packages\django\http\response.py", line 295, in __next__
    return self.make_bytes(next(self._iterator))
  File "C:\Python27\lib\site-packages\django\http\response.py", line 285, in make_bytes
    return bytes(value.encode(self._charset))
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 15, in _curried
    return _curried_func(*(args + moreargs), **dict(kwargs, **morekwargs))
  File "C:\Python27\lib\site-packages\django\utils\safestring.py", line 93, in _proxy_method
    data = method(self, *args, **kwargs)
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2013' in position 6414: ordinal not in range(256)
[27/Dec/2013 13:11:26] "GET /admin/votacao/funcionario/1/ HTTP/1.1" 500 59
Traceback (most recent call last):
  File "C:\Python27\lib\SocketServer.py", line 593, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Python27\lib\SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 126, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "C:\Python27\lib\SocketServer.py", line 649, in __init__
    self.handle()
  File "C:\Python27\lib\wsgiref\simple_server.py", line 124, in handle
    handler.run(self.server.get_app())
  File "C:\Python27\lib\wsgiref\handlers.py", line 92, in run
    self.close()
  File "C:\Python27\lib\wsgiref\simple_server.py", line 33, in close
    self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 57720)
----------------------------------------

Importantly, the problem only started to occur after I added the 'grappelli' package.

comment:8 by Tim Graham, 11 years ago

Resolution: fixed
Status: newclosed
Summary: Runserver autoreload defect on Windows 8Runserver autoreload defect on OSX

Hi geniofuturo,

Looking at the first traceback, your problem seems to be an issue with unicode handling in either your app or grappelli:

UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2013' in position 6414: ordinal not in range(256)

Please use our support channels rather than this ticket tracker to get help.

Thanks!

Note: See TracTickets for help on using tickets.
Back to Top