def test_roundtrip_translation(): sio1 = StringIO(u'a\nb', newline='\r\n') pos = sio1.seek(1) assert sio1.getvalue() == u'a\r\nb' state = sio1.__getstate__() sio2 = StringIO() sio2.__setstate__(state) assert sio2.getvalue() == u'a\r\nb' assert sio2.tell() == pos
def test_roundtrip_state(): s = u'12345678' sio1 = StringIO(s) sio1.foo = 42 sio1.seek(2) assert sio1.getvalue() == s state = sio1.__getstate__() sio2 = StringIO() sio2.__setstate__(state) assert sio2.getvalue() == s assert sio2.foo == 42 assert sio2.tell() == 2
def test_setstate(): sio = StringIO() sio.__setstate__((u"no error", u"\n", 0, None)) sio.__setstate__((u"no error", u"", 0, {"spam": 3})) raises(ValueError, sio.__setstate__, (u"", u"f", 0, None)) raises(ValueError, sio.__setstate__, (u"", u"", -1, None)) raises(TypeError, sio.__setstate__, (b"", u"", 0, None)) raises(TypeError, sio.__setstate__, (u"", u"", 0.0, None)) raises(TypeError, sio.__setstate__, (u"", u"", 0, 0)) raises(TypeError, sio.__setstate__, (u"len-test", 0)) raises(TypeError, sio.__setstate__) raises(TypeError, sio.__setstate__, 0) sio.close() raises(ValueError, sio.__setstate__, (u"closed", u"", 0, None))