Skip to content

Commit

Permalink
Make sure the WSGI Response object respects lock semantics (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
dataflake committed Feb 15, 2019
1 parent c9c23b5 commit 9c8dd76
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Expand Up @@ -23,6 +23,11 @@ Fixes
- Fix error when not selecting a file for upload in Files and Images
(`#492 <https://github.com/zopefoundation/Zope/issues/492>`_)

Other changes
+++++++++++++
- Make sure the WSGI Response object respects lock semantics
(`#216 <https://github.com/zopefoundation/Zope/issues/216>`_)


4.0b9 (2019-02-09)
------------------
Expand Down
9 changes: 9 additions & 0 deletions src/ZPublisher/HTTPResponse.py
Expand Up @@ -1065,6 +1065,10 @@ def write(self, data):
self.stdout.write(data)

def setBody(self, body, title='', is_error=False, lock=None):
# allow locking of the body in the same way as the status
if self._locked_body:
return

if isinstance(body, IOBase):
body.seek(0, 2)
length = body.tell()
Expand All @@ -1081,6 +1085,11 @@ def setBody(self, body, title='', is_error=False, lock=None):
else:
super(WSGIResponse, self).setBody(body, title, is_error)

# Have to apply the lock at the end in case the super class setBody
# is called, which will observe the lock and do nothing
if lock:
self._locked_body = 1

def __bytes__(self):
raise NotImplementedError

Expand Down
7 changes: 7 additions & 0 deletions src/ZPublisher/tests/test_WSGIPublisher.py
Expand Up @@ -152,6 +152,13 @@ def __len__(self):
self.assertEqual(response.getHeader('Content-Length'),
'%d' % len(TestStreamIterator.data))

def test_setBody_w_locking(self):
response = self._makeOne()
response.setBody(b'BEFORE', lock=True)
result = response.setBody(b'AFTER')
self.assertFalse(result)
self.assertEqual(response.body, b'BEFORE')

def test___str___raises(self):
response = self._makeOne()
response.setBody('TESTING')
Expand Down

0 comments on commit 9c8dd76

Please sign in to comment.