* force unicode on uuid string conversion in py2
This commit is contained in:
Rob McMullen 2017-05-18 22:48:01 -07:00
parent 06e07bafe6
commit 8260774995
3 changed files with 26 additions and 6 deletions

View File

@ -8,12 +8,11 @@ from builtins import range
from builtins import object
import bisect
import io
import uuid
import numpy as np
from .errors import *
from .utils import to_numpy, to_numpy_list
from .utils import to_numpy, to_numpy_list, uuid
from functools import reduce
user_bit_mask = 0x07
@ -364,7 +363,7 @@ class DefaultSegment(object):
self.verbose_name = verbose_name
self.page_size = -1
self.map_width = 40
self.uuid = str(uuid.uuid4())
self.uuid = uuid()
if memory_map is None:
memory_map = {}
self.memory_map = memory_map
@ -453,7 +452,7 @@ class DefaultSegment(object):
added to a segment, a default value should be applied here.
"""
self.memory_map = dict(state.pop('memory_map', []))
self.uuid = state.pop('uuid', uuid.uuid4())
self.uuid = state.pop('uuid', uuid())
self.can_resize = state.pop('can_resize', self.__class__.can_resize_default)
self.restore_missing_serializable_defaults()
self.__dict__.update(state)

View File

@ -4,6 +4,7 @@ from builtins import str
from builtins import range
from builtins import object
import types
import uuid as stdlib_uuid
import numpy as np
@ -17,6 +18,18 @@ except NameError:
_xd = False
def uuid():
u = stdlib_uuid.uuid4()
# Force it to use unicode(py2) or str(py3) so it isn't serialized as
# future.types.newstr.newstr on py2
try:
u = unicode(u)
except:
u = str(u)
return u
def to_numpy(value):
if type(value) is np.ndarray:
return value

View File

@ -1,6 +1,7 @@
from __future__ import print_function
from builtins import range
from builtins import object
from builtins import str
import os
import numpy as np
@ -15,7 +16,14 @@ class TestSegment(object):
r = SegmentData(data)
self.segment = DefaultSegment(r, 0)
def test_s1(self):
def test_getstate(self):
state = self.segment.__getstate__()
for k, v in state.items():
print("k=%s v=%s type=%s" % (k, v, type(v)))
byte_type = type(str(u' ').encode('utf-8')) # py2 and py3
assert type(state['uuid']) == byte_type
def test_extra(self):
s = self.segment
s.set_comment([[4,5]], "test1")
s.set_comment([[40,50]], "test2")
@ -51,4 +59,4 @@ class TestSegment(object):
if __name__ == "__main__":
t = TestSegment()
t.setup()
t.test_s1()
t.test_getstate()