Example #1
0
    def check_write(value, expected, stream_encoding, preferred_encoding):
        old_getpreferredencoding = locale.getpreferredencoding
        try:
            locale.getpreferredencoding = lambda: preferred_encoding

            # Check writing to io.StringIO
            stream = io.StringIO()
            _write_with_fallback(value, stream)
            assert stream.getvalue() == value

            # Check writing to a text stream
            buf = io.BytesIO()
            stream = io.TextIOWrapper(buf, encoding=stream_encoding)
            _write_with_fallback(value, stream)
            stream.flush()
            got = buf.getvalue()
            assert got == expected

            # Writing to io.BytesIO
            stream = io.BytesIO()
            with pytest.raises(TypeError):
                _write_with_fallback(value, stream)

            # Check writing to a file
            fn = os.path.join(tmpdir, 'tmp.txt')
            with io.open(fn, 'w', encoding=stream_encoding) as stream:
                _write_with_fallback(value, stream)
            with open(fn, 'rb') as stream:
                got = stream.read()
                assert got == expected
        finally:
            locale.getpreferredencoding = old_getpreferredencoding
Example #2
0
    def check_write(value, expected, stream_encoding, preferred_encoding):
        old_getpreferredencoding = locale.getpreferredencoding
        try:
            locale.getpreferredencoding = lambda: preferred_encoding

            # Check writing to io.StringIO
            stream = io.StringIO()
            _write_with_fallback(value, stream.write, stream)
            assert stream.getvalue() == value

            # Check writing to a text stream
            buf = io.BytesIO()
            stream = io.TextIOWrapper(buf, encoding=stream_encoding)
            _write_with_fallback(value, stream.write, stream)
            stream.flush()
            got = buf.getvalue()
            assert got == expected

            # Check writing to a byte stream (no stream encoding, so
            # it should write in locale encoding)
            if stream_encoding == preferred_encoding:
                buf = io.BytesIO()
                _write_with_fallback(value, buf.write, buf)
                got = buf.getvalue()
                assert got == expected

            # Check writing to a file
            fn = os.path.join(tmpdir, 'tmp.txt')
            with io.open(fn, 'w', encoding=stream_encoding) as stream:
                _write_with_fallback(value, stream.write, stream)
            with open(fn, 'rb') as stream:
                got = stream.read()
                assert got == expected

            # Check writing to Py2 files
            if not six.PY3:
                if stream_encoding == preferred_encoding:
                    # No stream encoding: write in locale encoding
                    for mode in ['w', 'wb']:
                        with open(fn, mode) as stream:
                            _write_with_fallback(value, stream.write, stream)
                        with open(fn, 'rb') as stream:
                            got = stream.read()
                            assert got == expected
        finally:
            locale.getpreferredencoding = old_getpreferredencoding
Example #3
0
    def check_write(value, expected, stream_encoding, preferred_encoding):
        old_getpreferredencoding = locale.getpreferredencoding
        try:
            locale.getpreferredencoding = lambda: preferred_encoding

            # Check writing to io.StringIO
            stream = io.StringIO()
            _write_with_fallback(value, stream.write, stream)
            assert stream.getvalue() == value

            # Check writing to a text stream
            buf = io.BytesIO()
            stream = io.TextIOWrapper(buf, encoding=stream_encoding)
            _write_with_fallback(value, stream.write, stream)
            stream.flush()
            got = buf.getvalue()
            assert got == expected

            # Check writing to a byte stream (no stream encoding, so
            # it should write in locale encoding)
            if stream_encoding == preferred_encoding:
                buf = io.BytesIO()
                _write_with_fallback(value, buf.write, buf)
                got = buf.getvalue()
                assert got == expected

            # Check writing to a file
            fn = os.path.join(tmpdir, 'tmp.txt')
            with io.open(fn, 'w', encoding=stream_encoding) as stream:
                _write_with_fallback(value, stream.write, stream)
            with open(fn, 'rb') as stream:
                got = stream.read()
                assert got == expected

            # Check writing to Py2 files
            if not six.PY3:
                if stream_encoding == preferred_encoding:
                    # No stream encoding: write in locale encoding
                    for mode in ['w', 'wb']:
                        with open(fn, mode) as stream:
                            _write_with_fallback(value, stream.write, stream)
                        with open(fn, 'rb') as stream:
                            got = stream.read()
                            assert got == expected
        finally:
            locale.getpreferredencoding = old_getpreferredencoding
Example #4
0
def test_write_with_fallback(tmpdir, capfd):
    tmpdir = six.text_type(tmpdir)

    def check_write(value, expected, stream_encoding, preferred_encoding):
        old_getpreferredencoding = locale.getpreferredencoding
        try:
            locale.getpreferredencoding = lambda: preferred_encoding

            # Check writing to io.StringIO
            stream = io.StringIO()
            _write_with_fallback(value, stream.write, stream)
            assert stream.getvalue() == value

            # Check writing to a text stream
            buf = io.BytesIO()
            stream = io.TextIOWrapper(buf, encoding=stream_encoding)
            _write_with_fallback(value, stream.write, stream)
            stream.flush()
            got = buf.getvalue()
            assert got == expected

            # Check writing to a byte stream (no stream encoding, so
            # it should write in locale encoding)
            if stream_encoding == preferred_encoding:
                buf = io.BytesIO()
                _write_with_fallback(value, buf.write, buf)
                got = buf.getvalue()
                assert got == expected

            # Check writing to a file
            fn = os.path.join(tmpdir, 'tmp.txt')
            with io.open(fn, 'w', encoding=stream_encoding) as stream:
                _write_with_fallback(value, stream.write, stream)
            with open(fn, 'rb') as stream:
                got = stream.read()
                assert got == expected

            # Check writing to Py2 files
            if not six.PY3:
                if stream_encoding == preferred_encoding:
                    # No stream encoding: write in locale encoding
                    for mode in ['w', 'wb']:
                        with open(fn, mode) as stream:
                            _write_with_fallback(value, stream.write, stream)
                        with open(fn, 'rb') as stream:
                            got = stream.read()
                            assert got == expected
        finally:
            locale.getpreferredencoding = old_getpreferredencoding

    # What is printed should follow the following rules:
    #
    # - Try printing in stream encoding.
    # - Try printing in locale preferred encoding.
    # - Otherwise, map characters produced by asv to ascii equivalents, and
    #   - Try to print in latin1
    #   - Try to print in ascii, replacing all non-ascii characters
    encodings = ['utf-8', 'latin1', 'ascii', 'euc-jp']
    strings = ["helloμ", "hello·", "hello難", "helloä", "hello±"]
    repmap = {"helloμ": "hellou", "hello·": "hello-", "hello±": "hello~"}

    for pref_enc, stream_enc, s in itertools.product(encodings, encodings,
                                                     strings):
        expected = None
        for enc in [stream_enc, pref_enc]:
            try:
                expected = s.encode(enc)
                break
            except UnicodeError:
                pass
        else:
            s2 = repmap.get(s, s)
            try:
                expected = s2.encode('latin1')
            except UnicodeError:
                expected = s2.encode('ascii', 'replace')

        check_write(s, expected, stream_enc, pref_enc)

    # Should not bail out on bytes input
    _write_with_fallback("a".encode('ascii'), sys.stdout.write, sys.stdout)
    out, err = capfd.readouterr()
    assert out == "a"
Example #5
0
def test_write_with_fallback(tmpdir, capfd):
    tmpdir = six.text_type(tmpdir)

    def check_write(value, expected, stream_encoding, preferred_encoding):
        old_getpreferredencoding = locale.getpreferredencoding
        try:
            locale.getpreferredencoding = lambda: preferred_encoding

            # Check writing to io.StringIO
            stream = io.StringIO()
            _write_with_fallback(value, stream.write, stream)
            assert stream.getvalue() == value

            # Check writing to a text stream
            buf = io.BytesIO()
            stream = io.TextIOWrapper(buf, encoding=stream_encoding)
            _write_with_fallback(value, stream.write, stream)
            stream.flush()
            got = buf.getvalue()
            assert got == expected

            # Check writing to a byte stream (no stream encoding, so
            # it should write in locale encoding)
            if stream_encoding == preferred_encoding:
                buf = io.BytesIO()
                _write_with_fallback(value, buf.write, buf)
                got = buf.getvalue()
                assert got == expected

            # Check writing to a file
            fn = os.path.join(tmpdir, 'tmp.txt')
            with io.open(fn, 'w', encoding=stream_encoding) as stream:
                _write_with_fallback(value, stream.write, stream)
            with open(fn, 'rb') as stream:
                got = stream.read()
                assert got == expected

            # Check writing to Py2 files
            if not six.PY3:
                if stream_encoding == preferred_encoding:
                    # No stream encoding: write in locale encoding
                    for mode in ['w', 'wb']:
                        with open(fn, mode) as stream:
                            _write_with_fallback(value, stream.write, stream)
                        with open(fn, 'rb') as stream:
                            got = stream.read()
                            assert got == expected
        finally:
            locale.getpreferredencoding = old_getpreferredencoding

    # What is printed should follow the following rules:
    #
    # - Try printing in stream encoding.
    # - Try printing in locale preferred encoding.
    # - Otherwise, map characters produced by asv to ascii equivalents, and
    #   - Try to print in latin1
    #   - Try to print in ascii, replacing all non-ascii characters
    encodings = ['utf-8', 'latin1', 'ascii', 'euc-jp']
    strings = ["helloμ", "hello·", "hello難", "helloä", "hello±"]
    repmap = {"helloμ": "hellou", "hello·": "hello-", "hello±": "hello~"}

    for pref_enc, stream_enc, s in itertools.product(encodings, encodings, strings):
        expected = None
        for enc in [stream_enc, pref_enc]:
            try:
                expected = s.encode(enc)
                break
            except UnicodeError:
                pass
        else:
            s2 = repmap.get(s, s)
            try:
                expected = s2.encode('latin1')
            except UnicodeError:
                expected = s2.encode('ascii', 'replace')

        check_write(s, expected, stream_enc, pref_enc)

    # Should not fail with Unicode streams
    def write(s):
        if isinstance(s, bytes):
            raise TypeError("failure")

    _write_with_fallback("a", write, None)
    _write_with_fallback(b"a", write, None)

    # Should not bail out on bytes input
    _write_with_fallback(b"a", sys.stdout.write, sys.stdout)
    out, err = capfd.readouterr()
    assert out == "a"
Example #6
0
def test_write_with_fallback(tmpdir, capfd):
    tmpdir = str(tmpdir)

    def check_write(value, expected, stream_encoding, preferred_encoding):
        old_getpreferredencoding = locale.getpreferredencoding
        try:
            locale.getpreferredencoding = lambda: preferred_encoding

            # Check writing to io.StringIO
            stream = io.StringIO()
            _write_with_fallback(value, stream)
            assert stream.getvalue() == value

            # Check writing to a text stream
            buf = io.BytesIO()
            stream = io.TextIOWrapper(buf, encoding=stream_encoding)
            _write_with_fallback(value, stream)
            stream.flush()
            got = buf.getvalue()
            assert got == expected

            # Writing to io.BytesIO
            stream = io.BytesIO()
            with pytest.raises(TypeError):
                _write_with_fallback(value, stream)

            # Check writing to a file
            fn = os.path.join(tmpdir, 'tmp.txt')
            with io.open(fn, 'w', encoding=stream_encoding) as stream:
                _write_with_fallback(value, stream)
            with open(fn, 'rb') as stream:
                got = stream.read()
                assert got == expected
        finally:
            locale.getpreferredencoding = old_getpreferredencoding

    # What is printed should follow the following rules:
    #
    # - Try printing in stream encoding.
    # - Try printing in locale preferred encoding.
    # - Otherwise, map characters produced by asv to ascii equivalents, and
    #   - Try to print in latin1
    #   - Try to print in ascii, replacing all non-ascii characters
    encodings = ['utf-8', 'latin1', 'ascii', 'euc-jp']
    strings = ["helloμ", "hello·", "hello難", "helloä", "hello±"]
    repmap = {"helloμ": "hellou", "hello·": "hello-", "hello±": "hello~"}

    for pref_enc, stream_enc, s in itertools.product(encodings, encodings,
                                                     strings):
        expected = None
        encodings = [stream_enc, pref_enc]
        for enc in encodings:
            try:
                expected = s.encode(enc)
                break
            except UnicodeError:
                pass
        else:
            s2 = repmap.get(s, s)
            expected = s2.encode(pref_enc, errors='replace')

        check_write(s, expected, stream_enc, pref_enc)

    # Should bail out on bytes input
    with pytest.raises(ValueError):
        _write_with_fallback(b"a", sys.stdout)