Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#25964 closed Bug (invalid)

AttributeError: 'HttpResponse' object has no attribute '_reason_phrase'

Reported by: George Karakostas Owned by: nobody
Component: Core (Other) Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When using Django 1.9 with wsgi, the following error occurs:

[Tue Dec 22 12:39:21 2015] [error] [client 94.68.52.160] mod_wsgi (pid=31977): Exception occurred processing WSGI script '/var/www/vhosts/.../index.wsgi'. 
[Tue Dec 22 12:39:21 2015] [error] [client 94.68.52.160] Traceback (most recent call last): 
[Tue Dec 22 12:39:21 2015] [error] [client 94.68.52.160] File "/var/virtualenvs/ttours/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 181, in __call__ 
[Tue Dec 22 12:39:21 2015] [error] [client 94.68.52.160] status = '%s %s' % (response.status_code, response.reason_phrase) 
[Tue Dec 22 12:39:21 2015] [error] [client 94.68.52.160] File "/var/virtualenvs/ttours/lib/python3.4/site-packages/django/http/response.py", line 63, in reason_phrase 
[Tue Dec 22 12:39:21 2015] [error] [client 94.68.52.160] if self._reason_phrase is not None: 
[Tue Dec 22 12:39:21 2015] [error] [client 94.68.52.160] AttributeError: 'HttpResponse' object has no attribute '_reason_phrase' 

It appears that this has something to do with this recent change, as indicated in 1.9 release notes:

django.http.responses.REASON_PHRASES and django.core.handlers.wsgi.STATUS_CODE_TEXT have been removed. Use Python’s stdlib instead: http.client.responses for Python 3 and httplib.responses for Python 2.

But in django/core/handlers/wsgi.py https://github.com/django/django/blob/stable/1.9.x/django/core/handlers/wsgi.py#L181 it clearly uses it:

status = '%s %s' % (response.status_code, response.reason_phrase)

This prevents Django 1.9 to be deployed in wsgi.

Change History (8)

comment:1 by Aymeric Augustin, 8 years ago

Resolution: invalid
Status: newclosed

You must be using a third party application which uses undocumented, private APIs and that isn't compatible with Django 1.9.

If Django 1.9 couldn't be "deployed in wsgi", we'd have heard about it, as there's no other way to deploy Django and there are some 1.9 sites deployed on the Internet.

See TicketClosingReasons/UseSupportChannels for your options if you need further help. Thanks!

comment:2 by George Karakostas, 8 years ago

Ok, apologies if I have overreacted. I have been struggling with this for days. I wonder what this has to do with third party APIs since the stack trace is clearly about django core files.

Last edited 8 years ago by George Karakostas (previous) (diff)

comment:3 by George Karakostas, 8 years ago

Resolution: invalid
Status: closednew

I will insist on this one. I understand the inconvenience, but please leave aside my initial strong statement and take a look at this:

The error is very specific: "AttributeError: 'HttpResponse' object has no attribute '_reason_phrase' ".

If I change line 181 of wsgi.py from:

        status = '%s %s' % (response.status_code, response.reason_phrase)

to:

        status = '%s %s' % (response.status_code, 'a random text')

Everything works fine. Naturally, as in Django 1.9 response does not have a reason_phrase.

I do not understand the conditions under which I meet the particular line of code. This happens to me only for the index page.

Last edited 8 years ago by George Karakostas (previous) (diff)

comment:4 by Tim Graham, 8 years ago

Resolution: needsinfo
Status: newclosed

Django 1.9 does have a reason_phrase attribute. Maybe you are using some HttpResponseBase subclass that fails to call super() in __init__(). You'll need to do more such as providing a sample project that we can use to reproduce the error to prove Django is at fault.

comment:5 by Marten Kenbeek, 8 years ago

Did you upgrade this particular deployment to Django 1.9 recently? The private _reason_phrase attribute has been added in 1.9, and HttpResponse.reason_phrase has been changed to a property. If you have a cached HttpResponse from before 1.9, the unpickled response will not contain the _reason_phrase attribute, and result in this traceback. The solution is to clear your cache, as documented in the upgrade guide.

comment:6 by George Karakostas, 8 years ago

Resolution: needsinfofixed

Thank you for your reply, much appreciated. Yes, it is a very recent upgrade for this deployment. Cleaning the cache is important, and I hope this might help anyone that might experience this too.

Sometimes it is easy to miss the obvious. I was wondering though, if incrementing the memcached 'VERSION' shouldn't be enough anyway.

comment:7 by Tim Graham, 8 years ago

Resolution: fixedinvalid

I'm not sure -- did you try it?

comment:8 by George Karakostas, 8 years ago

I am fairly sure that I did. Anyway, the conclusion is to clear caches. Thanks again.

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