From 15cbb894f29c25b7c929dc368e11a211a7968adb Mon Sep 17 00:00:00 2001 From: Sirushti Murugesan Date: Mon, 29 Jun 2015 09:06:25 +0530 Subject: [PATCH] Port short_id to return unicode This will now use unichr instead of chr to convert an integer to byte-string. Then these bytes are encoded and decoded with 'latin-1' to comply with the existing tests that assert against absolute values. partial blueprint heat-python34-support Change-Id: I5b9de1cea54889fa388993b9afa4b45f01fbec4a --- heat/common/short_id.py | 7 ++++--- heat/tests/test_short_id.py | 14 +++++++------- py3-testlist | 1 + 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/heat/common/short_id.py b/heat/common/short_id.py index 12f7a44076..c6854a8c8b 100644 --- a/heat/common/short_id.py +++ b/heat/common/short_id.py @@ -32,7 +32,7 @@ def _to_byte_string(value, num_bits): """ shifts = six.moves.xrange(num_bits - 8, -8, -8) byte_at = lambda off: (value >> off if off >= 0 else value << -off) & 0xff - return ''.join(chr(byte_at(offset)) for offset in shifts) + return ''.join(six.unichr(byte_at(offset)) for offset in shifts) def get_id(source_uuid): @@ -49,9 +49,10 @@ def get_id(source_uuid): # (see RFC4122, Section 4.4) random_bytes = _to_byte_string(source_uuid.time, 60) # The first 12 bytes (= 60 bits) of base32-encoded output is our data - encoded = base64.b32encode(random_bytes)[:12] - return encoded.lower() + encoded = base64.b32encode(random_bytes.encode('latin-1'))[:12] + + return encoded.lower().decode('latin-1') def generate_id(): diff --git a/heat/tests/test_short_id.py b/heat/tests/test_short_id.py index d49f19b9d9..aa153dfb24 100644 --- a/heat/tests/test_short_id.py +++ b/heat/tests/test_short_id.py @@ -20,16 +20,16 @@ from heat.tests import common class ShortIdTest(common.HeatTestCase): def test_byte_string_8(self): - self.assertEqual('\xab', short_id._to_byte_string(0xab, 8)) - self.assertEqual('\x05', short_id._to_byte_string(0x05, 8)) + self.assertEqual(u'\xab', short_id._to_byte_string(0xab, 8)) + self.assertEqual(u'\x05', short_id._to_byte_string(0x05, 8)) def test_byte_string_16(self): - self.assertEqual('\xab\xcd', short_id._to_byte_string(0xabcd, 16)) - self.assertEqual('\x0a\xbc', short_id._to_byte_string(0xabc, 16)) + self.assertEqual(u'\xab\xcd', short_id._to_byte_string(0xabcd, 16)) + self.assertEqual(u'\x0a\xbc', short_id._to_byte_string(0xabc, 16)) def test_byte_string_12(self): - self.assertEqual('\xab\xc0', short_id._to_byte_string(0xabc, 12)) - self.assertEqual('\x0a\xb0', short_id._to_byte_string(0x0ab, 12)) + self.assertEqual(u'\xab\xc0', short_id._to_byte_string(0xabc, 12)) + self.assertEqual(u'\x0a\xb0', short_id._to_byte_string(0x0ab, 12)) def test_byte_string_60(self): val = 0x111111111111111 @@ -67,5 +67,5 @@ class ShortIdTest(common.HeatTestCase): for id in ids: self.assertEqual(12, len(id)) - self.assertFalse(id.translate(None, allowed_chars)) + self.assertEqual(id, id.translate(allowed_chars)) self.assertEqual(1, ids.count(id)) diff --git a/py3-testlist b/py3-testlist index 5d7bdcfcde..45564a722f 100644 --- a/py3-testlist +++ b/py3-testlist @@ -1 +1,2 @@ heat.tests.test_version +heat.tests.test_short_id