utils.py, codecharts.py: add and use int2byte function py33
authorrobin
Tue, 19 Nov 2013 15:28:22 +0000
branchpy33
changeset 3796 c9a86549dee7
parent 3795 2a6c4b74219f
child 3797 360edd3e834d
utils.py, codecharts.py: add and use int2byte function
src/reportlab/lib/codecharts.py
src/reportlab/lib/utils.py
--- a/src/reportlab/lib/codecharts.py	Tue Nov 19 13:51:41 2013 +0000
+++ b/src/reportlab/lib/codecharts.py	Tue Nov 19 15:28:22 2013 +0000
@@ -17,6 +17,7 @@
 from reportlab.graphics.shapes import Drawing, Group, String, Circle, Rect
 from reportlab.graphics.widgetbase import Widget
 from reportlab.lib import colors
+from reportlab.lib.utils import int2byte
 
 adobe2codec = {
     'WinAnsiEncoding':'winansi',
@@ -149,7 +150,7 @@
 
     def draw(self):
         self.drawLabels()
-        charList = [None] * 32 + list(map(chr, list(range(32, 256))))
+        charList = [None] * 32 + list(map(int2byte, list(range(32, 256))))
 
         #we need to convert these to Unicode, since ReportLab
         #2.0 can only draw in Unicode.
@@ -209,12 +210,12 @@
         if self.encodingName.find('EUC') > -1:
             # it is an EUC family encoding.
             for col in range(1, 95):
-                ch = chr(row + 160) + chr(col+160)
+                ch = int2byte(row + 160) + int2byte(col+160)
                 cells.append(ch)
 ##        elif self.encodingName.find('GB') > -1:
 ##            # it is an EUC family encoding.
 ##            for col in range(1, 95):
-##                ch = chr(row + 160) + chr(col+160)
+##                ch = int2byte(row + 160) + int2byte(col+160)
         else:
             cells.append([None] * 94)
         return cells
@@ -266,7 +267,7 @@
             for y in [4,5,6,7,10,11,12,13,14,15]:
                 for x in range(16):
                     col = y*16+x
-                    ch = chr(row) + chr(col)
+                    ch = int2byte(row) + int2byte(col)
                     cells.append(ch)
 
         else:
@@ -320,7 +321,7 @@
                 charValue = y * 16 + x
                 if charValue > 32:
                     s = String(self.x + x * dx,
-                               self.y + (self.height - y*dy), chr(charValue))
+                               self.y + (self.height - y*dy), int2byte(charValue))
                     g.add(s)
         return g
 
@@ -360,5 +361,3 @@
 
 if __name__=='__main__':
     test()
-
-
--- a/src/reportlab/lib/utils.py	Tue Nov 19 13:51:41 2013 +0000
+++ b/src/reportlab/lib/utils.py	Tue Nov 19 15:28:22 2013 +0000
@@ -8,17 +8,13 @@
 import base64
 import pickle
 from io import BytesIO
-import hashlib
 from reportlab.lib.logger import warnOnce
 from reportlab.lib.rltempfile import get_rl_tempfile, get_rl_tempdir, _rl_getuid
 
-if sys.hexversion >= 0x02000000:
-    def _digester(s):
-        return md5(s).hexdigest()
-else:
-    # hexdigest not available in 1.5
-    def _digester(s):
-        return join(["%02x" % ord(x) for x in md5(s).digest()], '')
+try:
+    from hashlib import md5
+except ImportError:
+    import md5
 
 isPy3 = sys.version_info[0]==3
 
@@ -39,6 +35,9 @@
     return isinstance(v,_st)
 
 if isPy3:
+    def _digester(s):
+        return md5(s if isBytes(s) else s.encode('utf8')).hexdigest()
+
     def UniChr(v):
         return chr(v)
 
@@ -54,7 +53,15 @@
     def isClass(v):
         return isinstance(v, type)
     from string import ascii_letters
+    int2byte = lambda x: bytes([x])
 else:
+    if sys.hexversion >= 0x02000000:
+        def _digester(s):
+            return md5(s).hexdigest()
+    else:
+        # hexdigest not available in 1.5
+        def _digester(s):
+            return join(["%02x" % ord(x) for x in md5(s).digest()], '')
     def UniChr(v):
         return unichr(v)
 
@@ -74,6 +81,7 @@
     from future_builtins import ascii
     import __builtin__
     __builtin__.ascii = ascii
+    int2byte = chr
 
 
 def _findFiles(dirList,ext='.ttf'):
@@ -562,7 +570,7 @@
                         if not self._cache:
                             from rl_config import register_reset
                             register_reset(self._cache.clear)
-                        data=self._cache.setdefault(md5(data).digest(),data)
+                        data=self._cache.setdefault(_digester(data),data)
                     self.fp=getBytesIO(data)
                 elif imageReaderFlags==-1 and isinstance(fileName,str):
                     #try Ralf Schmitt's re-opening technique of avoiding too many open files