Ejemplo n.º 1
0
    def test_pass_through_ascii_context(self):
        data = '12\u00d6\n'

        with NamedTemporaryFile('xt+', encoding='ascii') as fobj:
            assert fobj.closed is False
            with pytest.raises(UnicodeEncodeError):
                fobj.write(data)
            assert fobj.seek(0) == 0
            assert fobj.read() == ''
        assert fobj.closed is True
        assert not os.path.exists(fobj.name)

        with NamedTemporaryFile('xt+', encoding='ascii', errors=None) as fobj:
            assert fobj.closed is False
            with pytest.raises(UnicodeEncodeError):
                fobj.write(data)
            assert fobj.seek(0) == 0
            assert fobj.read() == ''
        assert fobj.closed is True
        assert not os.path.exists(fobj.name)

        with NamedTemporaryFile('xt+', encoding='ascii',
                                errors='strict') as fobj:
            assert fobj.closed is False
            with pytest.raises(UnicodeEncodeError):
                fobj.write(data)
            assert fobj.seek(0) == 0
            assert fobj.read() == ''
        assert fobj.closed is True
        assert not os.path.exists(fobj.name)
Ejemplo n.º 2
0
    def test_pass_through_utf8_context(self):
        data = '12\u00d6\n'

        with NamedTemporaryFile('xt+', encoding='utf-8') as fobj:
            assert fobj.closed is False
            assert fobj.write(data) == len(data)
            assert fobj.seek(0) == 0
            assert fobj.read() == data
        assert fobj.closed is True
        assert not os.path.exists(fobj.name)

        with NamedTemporaryFile('xt+', encoding='utf-8', errors=None) as fobj:
            assert fobj.closed is False
            assert fobj.write(data) == len(data)
            assert fobj.seek(0) == 0
            assert fobj.read() == data
        assert fobj.closed is True
        assert not os.path.exists(fobj.name)

        with NamedTemporaryFile('xt+', encoding='utf-8',
                                errors='strict') as fobj:
            assert fobj.closed is False
            assert fobj.write(data) == len(data)
            assert fobj.seek(0) == 0
            assert fobj.read() == data
        assert fobj.closed is True
        assert not os.path.exists(fobj.name)
Ejemplo n.º 3
0
 def test_invalid_iowrap_fail(self):
     # A bit of cheating here as we know we can induce an error in the
     # text wrapper to ensure the final section of the __init__ gets tested
     encoding = 'xxxasciixxx'
     with pytest.raises(LookupError):
         codecs.lookup(encoding)
     with pytest.raises(LookupError):
         NamedTemporaryFile('xt',
                            encoding=encoding,
                            errors='ignore',
                            delete=False)
Ejemplo n.º 4
0
 def test_replace_context(self):
     data = '12\u00d6\n'
     ref = reference(data, encoding='ascii', errors='replace')
     with NamedTemporaryFile('xt+', encoding='ascii',
                             errors='replace') as fobj:
         assert fobj.closed is False
         assert fobj.line_buffering is False
         assert fobj.write(data) == len(data)
         assert fobj.seek(0) == 0
         assert fobj.read() == ref
     assert fobj.closed is True
     assert not os.path.exists(fobj.name)
Ejemplo n.º 5
0
 def test_ignore(self):
     data = '12\u00d6\n'
     ref = reference(data, encoding='ascii', errors='ignore')
     fobj = NamedTemporaryFile('xt+', encoding='ascii', errors='ignore')
     assert fobj.closed is False
     assert fobj.line_buffering is False
     assert fobj.write(data) == len(data)
     assert fobj.seek(0) == 0
     assert fobj.read() == ref
     fobj.close()
     assert fobj.closed is True
     assert not os.path.exists(fobj.name)
Ejemplo n.º 6
0
 def test_invalid_buffering(self):
     # <buffering> is passed to the underlying constructor without checking
     # unless <errors> is specified
     with pytest.raises(ValueError):
         NamedTemporaryFile('xt', errors='ignore', buffering=0)
Ejemplo n.º 7
0
 def test_binary_with_errors(self):
     with pytest.raises(ValueError):
         NamedTemporaryFile('xb+', errors='ignore')
Ejemplo n.º 8
0
    def test_pass_through_utf8_no_delete(self):
        data = '12\u00d6\n'

        fobj = NamedTemporaryFile('xt+', encoding='utf-8', delete=False)
        assert fobj.closed is False
        assert fobj.write(data) == len(data)
        assert fobj.seek(0) == 0
        assert fobj.read() == data
        fobj.close()
        assert fobj.closed is True
        assert os.path.exists(fobj.name)
        os.unlink(fobj.name)
        assert not os.path.exists(fobj.name)

        fobj = NamedTemporaryFile('xt+',
                                  encoding='utf-8',
                                  errors=None,
                                  delete=False)
        assert fobj.closed is False
        assert fobj.write(data) == len(data)
        assert fobj.seek(0) == 0
        assert fobj.read() == data
        fobj.close()
        assert fobj.closed is True
        assert os.path.exists(fobj.name)
        os.unlink(fobj.name)
        assert not os.path.exists(fobj.name)

        fobj = NamedTemporaryFile('xt+',
                                  encoding='utf-8',
                                  errors='strict',
                                  delete=False)
        assert fobj.closed is False
        assert fobj.write(data) == len(data)
        assert fobj.seek(0) == 0
        assert fobj.read() == data
        fobj.close()
        assert fobj.closed is True
        assert os.path.exists(fobj.name)
        os.unlink(fobj.name)
        assert not os.path.exists(fobj.name)