def test_print_encoding(monkeypatch): # Fool the type checking in print_. monkeypatch.setattr(six, "file", six.BytesIO, raising=False) out = six.BytesIO() out.encoding = "utf-8" out.errors = None six.print_(six.u("\u053c"), end="", file=out) assert out.getvalue() == six.b("\xd4\xbc") out = six.BytesIO() out.encoding = "ascii" out.errors = "strict" py.test.raises(UnicodeEncodeError, six.print_, six.u("\u053c"), file=out) out.errors = "backslashreplace" six.print_(six.u("\u053c"), end="", file=out) assert out.getvalue() == six.b("\\u053c")
def test_python_2_unicode_compatible(): @six.python_2_unicode_compatible class MyTest(object): def __str__(self): return six.u('hello') def __bytes__(self): return six.b('hello') my_test = MyTest() if six.PY2: assert str(my_test) == six.b("hello") assert unicode(my_test) == six.u("hello") elif six.PY3: assert bytes(my_test) == six.b("hello") assert str(my_test) == six.u("hello") assert getattr(six.moves.builtins, 'bytes', str)(my_test) == six.b("hello")
def test_bytesiter(): it = six.iterbytes(six.b("hi")) assert six.next(it) == ord("h") assert six.next(it) == ord("i") py.test.raises(StopIteration, six.next, it)
def test_bytesindex(): assert six.indexbytes(six.b("hello"), 3) == ord("l")
def test_binary_type(): assert type(six.b("hi")) is six.binary_type
def test_byte2int(): assert six.byte2int(six.b("\x03")) == 3 assert six.byte2int(six.b("\x03\x04")) == 3 py.test.raises(IndexError, six.byte2int, six.b(""))
def test_int2byte(): assert six.int2byte(3) == six.b("\x03") py.test.raises(Exception, six.int2byte, 256)
def test_b(): data = six.b("\xff") assert isinstance(data, str) assert len(data) == 1 assert data == "\xff"
def test_BytesIO(): fp = six.BytesIO() fp.write(six.b("hello")) assert fp.getvalue() == six.b("hello")
def __bytes__(self): return six.b('hello')
def test_b(): data = six.b("\xff") assert isinstance(data, bytes) assert len(data) == 1 assert data == bytes([255])