From 6834733212244b4ffde712f5caf475f28e940d7b Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Mon, 1 May 2017 16:44:59 -0400 Subject: [PATCH] Less unicode weirdness in short_id The function _to_byte_string() deals with binary data, so it should return... a byte string. Also Base32 uses strictly valid ASCII characters, so it can be decoded as ASCII. This effectively reverts commit 15cbb894f29c25b7c929dc368e11a211a7968adb. Change-Id: I24b625a41eb8f7fd47f4a3df7ac53c598b01abb6 --- heat/common/short_id.py | 7 +++---- heat/tests/test_short_id.py | 18 +++++++++--------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/heat/common/short_id.py b/heat/common/short_id.py index afd7a5cd05..ef4acd0bb1 100644 --- a/heat/common/short_id.py +++ b/heat/common/short_id.py @@ -35,7 +35,7 @@ def _to_byte_string(value, num_bits): def byte_at(off): return (value >> off if off >= 0 else value << -off) & 0xff - return ''.join(six.unichr(byte_at(offset)) for offset in shifts) + return b''.join(six.int2byte(byte_at(offset)) for offset in shifts) def get_id(source_uuid): @@ -53,9 +53,8 @@ def get_id(source_uuid): 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.encode('latin-1'))[:12] - - return encoded.lower().decode('latin-1') + encoded_bytes = base64.b32encode(random_bytes)[:12] + return encoded_bytes.decode('ascii').lower() def generate_id(): diff --git a/heat/tests/test_short_id.py b/heat/tests/test_short_id.py index aa153dfb24..91d775a50c 100644 --- a/heat/tests/test_short_id.py +++ b/heat/tests/test_short_id.py @@ -20,21 +20,21 @@ from heat.tests import common class ShortIdTest(common.HeatTestCase): def test_byte_string_8(self): - self.assertEqual(u'\xab', short_id._to_byte_string(0xab, 8)) - self.assertEqual(u'\x05', short_id._to_byte_string(0x05, 8)) + self.assertEqual(b'\xab', short_id._to_byte_string(0xab, 8)) + self.assertEqual(b'\x05', short_id._to_byte_string(0x05, 8)) def test_byte_string_16(self): - self.assertEqual(u'\xab\xcd', short_id._to_byte_string(0xabcd, 16)) - self.assertEqual(u'\x0a\xbc', short_id._to_byte_string(0xabc, 16)) + self.assertEqual(b'\xab\xcd', short_id._to_byte_string(0xabcd, 16)) + self.assertEqual(b'\x0a\xbc', short_id._to_byte_string(0xabc, 16)) def test_byte_string_12(self): - self.assertEqual(u'\xab\xc0', short_id._to_byte_string(0xabc, 12)) - self.assertEqual(u'\x0a\xb0', short_id._to_byte_string(0x0ab, 12)) + self.assertEqual(b'\xab\xc0', short_id._to_byte_string(0xabc, 12)) + self.assertEqual(b'\x0a\xb0', short_id._to_byte_string(0x0ab, 12)) def test_byte_string_60(self): val = 0x111111111111111 byte_string = short_id._to_byte_string(val, 60) - self.assertEqual('\x11\x11\x11\x11\x11\x11\x11\x10', byte_string) + self.assertEqual(b'\x11\x11\x11\x11\x11\x11\x11\x10', byte_string) def test_get_id_string(self): id = short_id.get_id('11111111-1111-4111-bfff-ffffffffffff') @@ -62,10 +62,10 @@ class ShortIdTest(common.HeatTestCase): self.assertRaises(ValueError, short_id.get_id, source) def test_generate_ids(self): - allowed_chars = 'abcdefghijklmnopqrstuvwxyz234567' + allowed_chars = [ord(c) for c in u'abcdefghijklmnopqrstuvwxyz234567'] ids = [short_id.generate_id() for i in range(25)] for id in ids: self.assertEqual(12, len(id)) - self.assertEqual(id, id.translate(allowed_chars)) + self.assertFalse(id.translate({c: None for c in allowed_chars})) self.assertEqual(1, ids.count(id))