diff roundup/cgi/client.py @ 5524:674ad58667b4

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.
author Joseph Myers <jsm@polyomino.org.uk>
date Sun, 02 Sep 2018 18:59:41 +0000
parents db3a95f28b3c
children 901d7ba146ad
line wrap: on
line diff
--- a/roundup/cgi/client.py	Sun Sep 02 14:18:12 2018 +0000
+++ b/roundup/cgi/client.py	Sun Sep 02 18:59:41 2018 +0000
@@ -1806,7 +1806,10 @@
             return
 
         if sys.version_info[0] > 2:
-            content = content.encode(self.charset, 'xmlcharrefreplace')
+            # An action setting appropriate headers for a non-HTML
+            # response may return a bytes object directly.
+            if not isinstance(content, bytes):
+                content = content.encode(self.charset, 'xmlcharrefreplace')
         elif self.charset != self.STORAGE_CHARSET:
             # recode output
             content = content.decode(self.STORAGE_CHARSET, 'replace')

Roundup Issue Tracker: http://roundup-tracker.org/