diff roundup/backends/rdbms_common.py @ 5725:6923225fd781

Handle UnicodeDecodeError in file class when file contents are not text (e.g. jpg). This shows up in python3 due to conversion from bytes to string. This allows binary file retrieval via the rest interface to complete.
author John Rouillard <rouilj@ieee.org>
date Wed, 22 May 2019 20:56:59 -0400
parents 1a0498c1ed90
children 936275dfe1fa
line wrap: on
line diff
--- a/roundup/backends/rdbms_common.py	Wed May 22 17:35:47 2019 -0400
+++ b/roundup/backends/rdbms_common.py	Wed May 22 20:56:59 2019 -0400
@@ -71,6 +71,7 @@
 from roundup.backends.back_anydbm import compile_expression
 from roundup.anypy.strings import b2s, bs2b, us2s, repr_export, eval_import
 
+from hashlib import md5
 
 # dummy value meaning "argument not passed"
 _marker = []
@@ -3079,6 +3080,15 @@
                 # BUG: by catching this we donot see an error in the log.
                 return 'ERROR reading file: %s%s\n%s\n%s'%(
                         self.classname, nodeid, poss_msg, strerror)
+            except UnicodeDecodeError as e:
+                # if content is not text (e.g. jpeg file) we get
+                # unicode error trying to convert to string in python 3.
+                # trap it and supply an error message. Include md5sum
+                # of content as this string is included in the etag
+                # calculation of the object.
+                return ('%s%s is not text, retrieve using '
+                        'binary_content property. mdsum: %s')%(self.classname,
+                   nodeid, md5(self.db.getfile(self.classname, nodeid, None)).hexdigest())
         elif propname == 'binary_content':
             return self.db.getfile(self.classname, nodeid, None)
 

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