def test_byte(self): # bytes are different in the two versions if sys.version_info[0] < 3: self.assertTrue(py2.byte(0x2A) == '\x2A') self.assertTrue(isinstance(py2.byte(0x2A), type('*'))) self.assertFalse(isinstance(py2.byte(0x2A), type(u'*'))) else: self.assertTrue(py2.byte(0x2A) == 0x2A) self.assertTrue(isinstance(py2.byte(0x2A), int)) if sys.version_info[0] < 3: self.assertTrue(py2.byte('*') == '\x2A') self.assertTrue(py2.byte('\xe9') == '\xe9') self.assertTrue(py2.byte(b'\xe9') == '\xe9') try: py2.byte(u'\xe9') self.fail("py2.byte(unicode)") except TypeError: pass self.assertTrue(py2.byte(bytearray(b'\xe9')) == '\xe9') self.assertTrue(isinstance(py2.byte('*'), type('*'))) self.assertFalse(isinstance(py2.byte('*'), type(u'*'))) else: self.assertTrue(py2.byte('*') == 0x2A) self.assertTrue(py2.byte('\xe9') == 0xE9) self.assertTrue(py2.byte(b'\xe9') == 0xE9) self.assertTrue(py2.byte(u'\xe9') == 0xE9) self.assertTrue(py2.byte(bytearray(b'\xe9')) == 0xE9) try: py2.byte('\u82f1') self.fail("py2.byte(wide char)") except ValueError: pass self.assertTrue(isinstance(py2.byte('*'), int)) data = b"hello" self.assertTrue(py2.join_bytes(list(data)) == data) try: py2.byte(256) self.fail("py2.byte(large)") except ValueError: pass try: py2.byte(-1) self.fail("py2.byte(negative)") except ValueError: pass # now test byte value... self.assertTrue(py2.byte_value(py2.byte(0)) == 0) self.assertTrue(py2.byte_value(py2.byte(0x30)) == 0x30) self.assertTrue(py2.byte_value(py2.byte(0xFF)) == 0xFF)
def test_byte(self): # bytes are different in the two versions if sys.version_info[0] < 3: self.assertTrue(py2.byte(0x2A) == '\x2A') self.assertTrue(isinstance(py2.byte(0x2A), type('*'))) self.assertFalse(isinstance(py2.byte(0x2A), type(u'*'))) self.assertFalse(py2.is_byte(0x82F1)) self.assertFalse(py2.is_byte(256)) self.assertTrue(py2.is_byte('\x2A')) self.assertTrue(py2.is_byte(b'\x2A')) self.assertFalse(py2.is_byte(u'\x2A')) self.assertFalse(py2.is_byte(u'**')) self.assertTrue(py2.is_byte('**'[0])) self.assertFalse(py2.is_byte(u'**'[0])) else: self.assertTrue(py2.byte(0x2A) == 0x2A) self.assertTrue(isinstance(py2.byte(0x2A), int)) self.assertTrue(py2.is_byte(0x2A)) self.assertFalse(py2.is_byte(0x82F1)) self.assertFalse(py2.is_byte('\x2A')) self.assertFalse(py2.is_byte(b'\x2A')) self.assertFalse(py2.is_byte('**'[0])) self.assertFalse(py2.is_byte('**')) self.assertFalse(py2.is_byte(b'**')) self.assertTrue(py2.is_byte(b'**'[0])) if sys.version_info[0] < 3: self.assertTrue(py2.byte('*') == '\x2A') self.assertTrue(py2.byte('\xe9') == '\xe9') self.assertTrue(py2.byte(b'\xe9') == '\xe9') self.assertTrue(py2.byte(u'\xe9') == '\xe9') self.assertTrue(py2.byte(bytearray(b'\xe9')) == '\xe9') try: py2.byte(u'\u82f1') self.fail("py2.byte(wide char)") except ValueError: pass try: py2.byte('\u82f1') self.fail("py2.byte(wide char, missing u)") except ValueError: pass self.assertTrue(isinstance(py2.byte('*'), type('*'))) self.assertFalse(isinstance(py2.byte('*'), type(u'*'))) else: self.assertTrue(py2.byte('*') == 0x2A) self.assertTrue(py2.byte('\xe9') == 0xE9) self.assertTrue(py2.byte(b'\xe9') == 0xE9) self.assertTrue(py2.byte(u'\xe9') == 0xE9) self.assertTrue(py2.byte(bytearray(b'\xe9')) == 0xE9) try: py2.byte('\u82f1') self.fail("py2.byte(wide char)") except ValueError: pass self.assertTrue(isinstance(py2.byte('*'), int)) # test joining iterables of byte data = b"hello" self.assertTrue(py2.join_bytes(list(data)) == data) # test byte_to_bstr data = py2.byte(0x40) self.assertTrue(py2.byte_to_bstr(data) == b'@') self.assertTrue(isinstance(py2.byte_to_bstr(data), bytes)) for i in py2.range3(256): b = py2.byte(i) self.assertTrue(py2.byte_to_bstr(b)[0] == b) # Now move on to exception handling try: py2.byte(256) self.fail("py2.byte(large)") except ValueError: pass try: py2.byte(-1) self.fail("py2.byte(negative)") except ValueError: pass # now test byte value... self.assertTrue(py2.byte_value(py2.byte(0)) == 0) self.assertTrue(py2.byte_value(py2.byte(0x30)) == 0x30) self.assertTrue(py2.byte_value(py2.byte(0xFF)) == 0xFF) # force bytes self.assertTrue(py2.force_bytes("Hello") == b"Hello") self.assertTrue(isinstance(py2.force_bytes("Hello"), bytes)) self.assertTrue(py2.force_bytes(b"Hello") == b"Hello") self.assertTrue(isinstance(py2.force_bytes(b"Hello"), bytes)) self.assertTrue(py2.force_bytes(u"Hello") == b"Hello") self.assertTrue(isinstance(py2.force_bytes(u"Hello"), bytes)) try: py2.force_bytes(py2.ul('Caf\xe9')) self.fail("force_bytes with high-bit character") except UnicodeEncodeError: pass