Merge ~nansari/django-piston:fix-for-SN-1445 into django-piston:master

Proposed by Najam Ahmed Ansari
Status: Rejected
Rejected by: Najam Ahmed Ansari
Proposed branch: ~nansari/django-piston:fix-for-SN-1445
Merge into: django-piston:master
Diff against target: 95 lines (+8/-41)
3 files modified
piston/__init__.py (+1/-1)
piston/tests.py (+6/-3)
piston/utils.py (+1/-37)
Reviewer Review Type Date Requested Status
Ubuntu One hackers Pending
Review via email: mp+446033@code.launchpad.net

Commit message

Fix for SN-1445

Description of the change

This fixes the bug in SCA wherein the SCA resource error handler the clobbers original exception message.

To post a comment you must log in.
Revision history for this message
Przemysław Suliga (suligap) wrote :

Hi, skimming the django-piston and Django code, I suspect a potential cleaner fix might be the removal of django-piston's HttpResponseWrapper (or not using the wrapper when on new enough Django). Have you tried that?

Because there are other places in django piston that set response.content as a string it looks like. Django's HttpResponse handles conversion and the wrapper doesn't. It's possible the wrapper is not needed for the django version we use?

Revision history for this message
Przemysław Suliga (suligap) wrote :

But that might be going against the "just apply a minimal fix to piston" approach. So if this works for SCA, then it should be fine.

Revision history for this message
Maximiliano Bertacchini (maxiberta) wrote :

I think I agree with Przemek here: piston's HttpResponseWrapper is apparently just trying to workaround an issue in django's HttpResponse, according to a comment in HttpResponseWrapper's code. But the issue http://code.djangoproject.com/ticket/9403 was fixed 11 years ago!!! :D:D:D

Maybe we can just drop HttpResponseWrapper and update piston's rc_factory.__getattr__() to return a plain HttpResponse? Afaict, the fix shipped in Django 1.4 (see https://github.com/django/django/commit/50255e3305 which is linked from the main issue). Based on piston's tox tests, we support Django >=1.4 which sounds like a really great coincidence and in which case we can just drop it and forget about django versions.

09f52da... by Najam Ahmed Ansari <email address hidden>

Removing piston's custom HttpResponseWrapper

The wrapper class was attempting to work around a bug in Django which has since
been fixed, rendering the wrapper unnecessary. Removing the wrapper also fixes
SN-1445.

Unmerged commits

09f52da... by Najam Ahmed Ansari <email address hidden>

Removing piston's custom HttpResponseWrapper

The wrapper class was attempting to work around a bug in Django which has since
been fixed, rendering the wrapper unnecessary. Removing the wrapper also fixes
SN-1445.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/piston/__init__.py b/piston/__init__.py
2index ffdf661..7be60e2 100644
3--- a/piston/__init__.py
4+++ b/piston/__init__.py
5@@ -1,4 +1,4 @@
6-__version__ = '2.2.6'
7+__version__ = '2.2.7'
8
9
10 def get_version():
11diff --git a/piston/tests.py b/piston/tests.py
12index aacce0b..f82460d 100644
13--- a/piston/tests.py
14+++ b/piston/tests.py
15@@ -99,7 +99,7 @@ class CustomResponseWithStatusCodeTest(TestCase):
16
17 def create(self, request):
18 resp = rc.CREATED
19- resp.content = response_data
20+ resp.content = json.dumps(response_data)
21 return resp
22
23 resource = Resource(MyHandler)
24@@ -146,11 +146,11 @@ class ErrorHandlerTest(TestCase):
25 # formatted as json
26 if isinstance(error, GoAwayError):
27 response = rc.FORBIDDEN
28- response.content = dict(error=dict(
29+ response.content = json.dumps(dict(error=dict(
30 name=error.name,
31 message="Get out of here and dont come back",
32 reason=error.reason
33- ))
34+ )))
35
36 return response
37
38@@ -187,6 +187,9 @@ class ErrorHandlerTest(TestCase):
39
40 self.assertTrue(isinstance(response, HttpResponse), "Expected a response, not: %s"
41 % response)
42+ error_str = response.content.decode('utf-8')
43+ self.assertTrue(error_str.find('Method signature does not match'),
44+ 'Expected underlying exception to be handled')
45
46
47 def test_other_error(self):
48diff --git a/piston/utils.py b/piston/utils.py
49index 8e76718..a48c201 100644
50--- a/piston/utils.py
51+++ b/piston/utils.py
52@@ -51,43 +51,7 @@ class rc_factory(object):
53 except TypeError:
54 raise AttributeError(attr)
55
56- class HttpResponseWrapper(HttpResponse):
57- """
58- Wrap HttpResponse and make sure that the internal
59- _is_string/_base_content_is_iter flag is updated when the
60- _set_content method (via the content property) is called
61- """
62- def _set_content(self, content):
63- """
64- Set the _container and _base_content_is_iter properties based
65- on the type of the value parameter. This logic is in the
66- construtor for HttpResponse, but doesn't get repeated when
67- setting HttpResponse.content although this bug report
68- (feature request) suggests that it should:
69- http://code.djangoproject.com/ticket/9403
70- """
71- if not isinstance(content, basestring) and hasattr(content, '__iter__'):
72- self._container = content
73- self._use_emitter = True
74- else:
75- self._container = [content]
76- self._use_emitter = False
77- # Django 1.7 and above does not have any custom internals to
78- # define the type of the content
79- if django.VERSION < (1, 7):
80- self._base_content_is_iter = self._use_emitter
81-
82- if django.VERSION >= (1, 5):
83- # HttpResponse._get_content does not exists in Django >= 1.5
84-
85- @HttpResponse.content.setter
86- def content(self, content):
87- self._set_content(content)
88-
89- else:
90- content = property(HttpResponse._get_content, _set_content)
91-
92- return HttpResponseWrapper(r, content_type='text/plain', status=c)
93+ return HttpResponse(r, content_type='text/plain', status=c)
94
95 rc = rc_factory()
96

Subscribers

People subscribed via source and target branches