Skip to content

Commit e18c620

Browse files
committed
Support actions returning binary data with Python 3.
An action's handle() function may return content that should be sent back to the client instead of the default HTML page. This content is not restricted to being HTML text; it can be for any content type (the handle() function should use self.client.setHeader in that case to set the Content-Type header, and Content-Disposition if the browser is to handle it as an attachment). If the content type is binary data, of course the handle() function has to return a bytes object in Python 3, not a str object. This then runs into Roundup (write_html, called on whatever the action returns, whether HTML or not) attempting to re-encode to the client character set, which is only appropriate with a str object. This patch adds an appropriate check to avoid the attempted re-encoding when given a bytes object.
1 parent 2c0c04e commit e18c620

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

roundup/cgi/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1806,7 +1806,10 @@ def write_html(self, content):
18061806
return
18071807

18081808
if sys.version_info[0] > 2:
1809-
content = content.encode(self.charset, 'xmlcharrefreplace')
1809+
# An action setting appropriate headers for a non-HTML
1810+
# response may return a bytes object directly.
1811+
if not isinstance(content, bytes):
1812+
content = content.encode(self.charset, 'xmlcharrefreplace')
18101813
elif self.charset != self.STORAGE_CHARSET:
18111814
# recode output
18121815
content = content.decode(self.STORAGE_CHARSET, 'replace')

0 commit comments

Comments
 (0)