__init__.py
changeset 10616 f454404733c1
parent 10602 4845012cfc8e
child 10665 79ff784cd8af
--- a/__init__.py	Fri Sep 18 18:26:07 2015 +0200
+++ b/__init__.py	Tue Sep 15 12:51:27 2015 +0200
@@ -29,6 +29,7 @@
 
 
 import __builtin__
+from six import PY2, binary_type
 # '_' is available in builtins to mark internationalized string but should
 # not be used to do the actual translation
 if not hasattr(__builtin__, '_'):
@@ -37,7 +38,7 @@
 CW_SOFTWARE_ROOT = __path__[0]
 
 import sys, os, logging
-from StringIO import StringIO
+from io import BytesIO
 
 from six.moves import cPickle as pickle
 
@@ -67,17 +68,19 @@
 #import threading
 #threading.settrace(log_thread)
 
-class Binary(StringIO):
-    """customize StringIO to make sure we don't use unicode"""
-    def __init__(self, buf=''):
-        assert isinstance(buf, (str, buffer, bytearray)), \
-               "Binary objects must use raw strings, not %s" % buf.__class__
-        StringIO.__init__(self, buf)
+class Binary(BytesIO):
+    """class to hold binary data. Use BytesIO to prevent use of unicode data"""
+    _allowed_types = (binary_type, bytearray, buffer if PY2 else memoryview)
+
+    def __init__(self, buf=b''):
+        assert isinstance(buf, self._allowed_types), \
+               "Binary objects must use bytes/buffer objects, not %s" % buf.__class__
+        super(Binary, self).__init__(buf)
 
     def write(self, data):
-        assert isinstance(data, (str, buffer, bytearray)), \
-               "Binary objects must use raw strings, not %s" % data.__class__
-        StringIO.write(self, data)
+        assert isinstance(data, self._allowed_types), \
+               "Binary objects must use bytes/buffer objects, not %s" % data.__class__
+        super(Binary, self).write(data)
 
     def to_file(self, fobj):
         """write a binary to disk