From 826077499525c36318448664c7caf4189cf25220 Mon Sep 17 00:00:00 2001 From: Rob McMullen Date: Thu, 18 May 2017 22:48:01 -0700 Subject: [PATCH] Fixed https://github.com/robmcmullen/omnivore/issues/209 * force unicode on uuid string conversion in py2 --- atrcopy/segments.py | 7 +++---- atrcopy/utils.py | 13 +++++++++++++ test/test_serialize.py | 12 ++++++++++-- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/atrcopy/segments.py b/atrcopy/segments.py index d3aed5e..69db8ea 100644 --- a/atrcopy/segments.py +++ b/atrcopy/segments.py @@ -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) diff --git a/atrcopy/utils.py b/atrcopy/utils.py index 21942eb..1c5bab1 100644 --- a/atrcopy/utils.py +++ b/atrcopy/utils.py @@ -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 diff --git a/test/test_serialize.py b/test/test_serialize.py index facbeaf..ff30bcd 100644 --- a/test/test_serialize.py +++ b/test/test_serialize.py @@ -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()