Exemple #1
0
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")
Exemple #2
0
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")
Exemple #3
0
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")
Exemple #4
0
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")
Exemple #5
0
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)
Exemple #6
0
def test_bytesindex():
    assert six.indexbytes(six.b("hello"), 3) == ord("l")
Exemple #7
0
def test_binary_type():
    assert type(six.b("hi")) is six.binary_type
Exemple #8
0
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(""))
Exemple #9
0
def test_int2byte():
    assert six.int2byte(3) == six.b("\x03")
    py.test.raises(Exception, six.int2byte, 256)
Exemple #10
0
 def test_b():
     data = six.b("\xff")
     assert isinstance(data, str)
     assert len(data) == 1
     assert data == "\xff"
Exemple #11
0
 def test_b():
     data = six.b("\xff")
     assert isinstance(data, str)
     assert len(data) == 1
     assert data == "\xff"
Exemple #12
0
def test_binary_type():
    assert type(six.b("hi")) is six.binary_type
Exemple #13
0
def test_BytesIO():
    fp = six.BytesIO()
    fp.write(six.b("hello"))
    assert fp.getvalue() == six.b("hello")
Exemple #14
0
def test_BytesIO():
    fp = six.BytesIO()
    fp.write(six.b("hello"))
    assert fp.getvalue() == six.b("hello")
Exemple #15
0
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)
Exemple #16
0
def test_bytesindex():
    assert six.indexbytes(six.b("hello"), 3) == ord("l")
Exemple #17
0
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(""))
Exemple #18
0
def test_int2byte():
    assert six.int2byte(3) == six.b("\x03")
    py.test.raises(Exception, six.int2byte, 256)
Exemple #19
0
 def __bytes__(self):
     return six.b('hello')
Exemple #20
0
 def test_b():
     data = six.b("\xff")
     assert isinstance(data, bytes)
     assert len(data) == 1
     assert data == bytes([255])
Exemple #21
0
 def __bytes__(self):
     return six.b('hello')
Exemple #22
0
 def test_b():
     data = six.b("\xff")
     assert isinstance(data, bytes)
     assert len(data) == 1
     assert data == bytes([255])