various: attempt to be more consistent about isStr py33
authorrobin
Mon, 25 Nov 2013 14:59:22 +0000
branchpy33
changeset 3835 8e35c471e0ee
parent 3834 3c5d76a16369
child 3836 2f2bd02eae27
various: attempt to be more consistent about isStr
src/reportlab/lib/rl_accel.py
src/reportlab/lib/validators.py
src/reportlab/pdfbase/pdfdoc.py
src/reportlab/pdfbase/pdfutils.py
src/reportlab/pdfgen/textobject.py
src/reportlab/platypus/paragraph.py
--- a/src/reportlab/lib/rl_accel.py	Mon Nov 25 14:57:51 2013 +0000
+++ b/src/reportlab/lib/rl_accel.py	Mon Nov 25 14:59:22 2013 +0000
@@ -28,7 +28,7 @@
         _py_funcs[fn] = None
 
 if _py_funcs:
-    from reportlab.lib.utils import isUnicode, isSeq
+    from reportlab.lib.utils import isBytes, isUnicode, isSeq, isPy3
     from math import log
 
 if 'fp_str' in _py_funcs:
@@ -153,12 +153,11 @@
         This is a compact encoding used for binary data within
         a PDF file.  Four bytes of binary data become five bytes of
         ASCII.  This is the default method used for encoding images."""
+        doOrd = isPy3 and isUnicode(input)
         # special rules apply if not a multiple of four bytes.
         whole_word_count, remainder_size = divmod(len(input), 4)
         cut = 4 * whole_word_count
         body, lastbit = input[0:cut], input[cut:]
-        if isPy3 and isStr(lastbit):
-            lastbit = lastbit.encode('utf-8')
 
         out = [].append
         for i in range(whole_word_count):
@@ -167,10 +166,11 @@
             b2 = body[offset+1]
             b3 = body[offset+2]
             b4 = body[offset+3]
-            if isStr(b1): b1 = ord(b1)
-            if isStr(b2): b2 = ord(b2)
-            if isStr(b3): b3 = ord(b3)
-            if isStr(b4): b4 = ord(b4)
+            if doOrd:
+                b1 = ord(b1)
+                b2 = ord(b2)
+                b3 = ord(b3)
+                b4 = ord(b4)
 
             if b1<128:
                 num = (((((b1<<8)|b2)<<8)|b3)<<8)|b4
@@ -205,10 +205,11 @@
             b2 = lastbit[1]
             b3 = lastbit[2]
             b4 = lastbit[3]
-            if isStr(b1): b1 = ord(b1)
-            if isStr(b2): b2 = ord(b2)
-            if isStr(b3): b3 = ord(b3)
-            if isStr(b4): b4 = ord(b4)
+            if doOrd:
+                b1 = ord(b1)
+                b2 = ord(b2)
+                b3 = ord(b3)
+                b4 = ord(b4)
 
             num = 16777216 * b1 + 65536 * b2 + 256 * b3 + b4
 
--- a/src/reportlab/lib/validators.py	Mon Nov 25 14:57:51 2013 +0000
+++ b/src/reportlab/lib/validators.py	Mon Nov 25 14:59:22 2013 +0000
@@ -5,7 +5,7 @@
 __doc__="""Standard verifying functions used by attrmap."""
 
 import sys, codecs
-from reportlab.lib.utils import isSeq, isStr, isUnicode
+from reportlab.lib.utils import isSeq, isBytes, isStr, isPy3
 from reportlab.lib import colors
 
 class Percentage(float):
@@ -82,8 +82,12 @@
         if not isinstance(x,int) and not isStr(x): return False
         return self.normalizeTest(x)
 
-    def normalize(self,x):
-        return int(x)
+    if not isPy3:
+        def normalize(self,x):
+            return int(x)
+    else:
+        def normalize(self,x):
+            return int(x.decode('utf8') if isBytes(x) else x)
 
 class _isNumberOrNone(_isNumber):
     def test(self,x):
--- a/src/reportlab/pdfbase/pdfdoc.py	Mon Nov 25 14:57:51 2013 +0000
+++ b/src/reportlab/pdfbase/pdfdoc.py	Mon Nov 25 14:59:22 2013 +0000
@@ -1449,6 +1449,7 @@
         destinationstotitles = self.destinationstotitles
         closedict = self.closedict
         if isStr(object):
+            if not isUnicode(object): object = object.decode('utf8')
             destination = canvas._bookmarkReference(object)
             title = object
             if object in destinationnamestotitles:
--- a/src/reportlab/pdfbase/pdfutils.py	Mon Nov 25 14:57:51 2013 +0000
+++ b/src/reportlab/pdfbase/pdfutils.py	Mon Nov 25 14:59:22 2013 +0000
@@ -10,7 +10,7 @@
 import os
 import binascii
 from reportlab import rl_config
-from reportlab.lib.utils import getBytesIO, ImageReader, isStr, isUnicode, isPy3
+from reportlab.lib.utils import getBytesIO, ImageReader, isUnicode, isPy3
 from reportlab.lib._rl_accel import asciiBase85Encode, asciiBase85Decode
 
 LINEEND = '\015\012'
@@ -180,7 +180,7 @@
     Not used except to provide a test of the inverse function."""
 
     #strip out all whitespace
-    if not isStr(input):
+    if not isUnicode(input):
         input = input.decode('utf-8')
     stripped = ''.join(input.split())
     assert stripped[-1] == '>', 'Invalid terminator for Ascii Hex Stream'
--- a/src/reportlab/pdfgen/textobject.py	Mon Nov 25 14:57:51 2013 +0000
+++ b/src/reportlab/pdfgen/textobject.py	Mon Nov 25 14:59:22 2013 +0000
@@ -12,7 +12,7 @@
 import string
 from types import *
 from reportlab.lib.colors import Color, CMYKColor, CMYKColorSep, toColor, black, white, _CMYK_black, _CMYK_white
-from reportlab.lib.utils import isBytes, isStr
+from reportlab.lib.utils import isBytes, isStr, asUnicode
 from reportlab.lib.rl_accel import fp_str
 from reportlab.pdfbase import pdfmetrics
 
@@ -433,7 +433,7 @@
         off each line and from the beginning; set trim=0 to preserve
         whitespace."""
         if isStr(stuff):
-            lines = '\n'.split(stuff.strip())
+            lines = '\n'.split(asUnicode(stuff).strip())
             if trim==1:
                 lines = [s.strip() for s in lines]
         elif isinstance(stuff,(tuple,list)):
--- a/src/reportlab/platypus/paragraph.py	Mon Nov 25 14:57:51 2013 +0000
+++ b/src/reportlab/platypus/paragraph.py	Mon Nov 25 14:59:22 2013 +0000
@@ -18,7 +18,7 @@
 from reportlab.lib.abag import ABag
 from reportlab.rl_config import platypus_link_underline
 from reportlab import rl_config
-from reportlab.lib.utils import isUnicode, isStr
+from reportlab.lib.utils import isBytes
 from reportlab.lib.rl_accel import sameFrag
 import re
 
@@ -60,12 +60,12 @@
 _wsc_re_split=re.compile('[%s]+'% re.escape(_wsc)).split
 
 def split(text, delim=None):
-    if not isUnicode(text): text = text.decode('utf8')
-    if delim is not None and not isUnicode(delim): delim = delim.decode('utf8')
+    if isBytes(text): text = text.decode('utf8')
+    if delim is not None and isBytes(delim): delim = delim.decode('utf8')
     return [uword for uword in (_wsc_re_split(text) if delim is None and '\xa0' in text else text.split(delim))]
 
 def strip(text):
-    if not isUnicode(text): text = text.decode('utf8')
+    if isBytes(text): text = text.decode('utf8')
     return text.strip(_wsc)
 
 class ParaLines(ABag):
@@ -125,8 +125,8 @@
     return m
 
 def _nbspCount(w):
-    if isStr(str):
-        return w.count('\xc2\xa0')
+    if isBytes(str):
+        return w.count(b'\xc2\xa0')
     else:
         return w.count('\xa0')