1212import io
1313import _pyio as pyio
1414import sys
15+ import pickle
1516
1617class MemorySeekTestMixin :
1718
@@ -352,6 +353,42 @@ def test_instance_dict_leak(self):
352353 memio = self .ioclass ()
353354 memio .foo = 1
354355
356+ def test_pickling (self ):
357+ buf = self .buftype ("1234567890" )
358+ memio = self .ioclass (buf )
359+ memio .foo = 42
360+ memio .seek (2 )
361+
362+ class PickleTestMemIO (self .ioclass ):
363+ def __init__ (me , initvalue , foo ):
364+ self .ioclass .__init__ (me , initvalue )
365+ me .foo = foo
366+ # __getnewargs__ is undefined on purpose. This checks that PEP 307
367+ # is used to provide pickling support.
368+
369+ # Pickle expects the class to be on the module level. Here we use a
370+ # little hack to allow the PickleTestMemIO class to derive from
371+ # self.ioclass without having to define all combinations explictly on
372+ # the module-level.
373+ import __main__
374+ PickleTestMemIO .__module__ = '__main__'
375+ __main__ .PickleTestMemIO = PickleTestMemIO
376+ submemio = PickleTestMemIO (buf , 80 )
377+ submemio .seek (2 )
378+
379+ # We only support pickle protocol 2 and onward since we use extended
380+ # __reduce__ API of PEP 307 to provide pickling support.
381+ for proto in range (2 , pickle .HIGHEST_PROTOCOL ):
382+ for obj in (memio , submemio ):
383+ obj2 = pickle .loads (pickle .dumps (obj , protocol = proto ))
384+ self .assertEqual (obj .getvalue (), obj2 .getvalue ())
385+ self .assertEqual (obj .__class__ , obj2 .__class__ )
386+ self .assertEqual (obj .foo , obj2 .foo )
387+ self .assertEqual (obj .tell (), obj2 .tell ())
388+ obj .close ()
389+ self .assertRaises (ValueError , pickle .dumps , obj , proto )
390+ del __main__ .PickleTestMemIO
391+
355392
356393class PyBytesIOTest (MemoryTestMixin , MemorySeekTestMixin , unittest .TestCase ):
357394
@@ -431,13 +468,26 @@ def test_bytes_array(self):
431468 self .assertEqual (memio .getvalue (), buf )
432469
433470
434- class PyStringIOTest (MemoryTestMixin , MemorySeekTestMixin , unittest .TestCase ):
435- buftype = unicode
436- ioclass = pyio .StringIO
437- UnsupportedOperation = pyio .UnsupportedOperation
438- EOF = ""
471+ class TextIOTestMixin :
439472
440- # TextIO-specific behaviour.
473+ def test_relative_seek (self ):
474+ memio = self .ioclass ()
475+
476+ self .assertRaises (IOError , memio .seek , - 1 , 1 )
477+ self .assertRaises (IOError , memio .seek , 3 , 1 )
478+ self .assertRaises (IOError , memio .seek , - 3 , 1 )
479+ self .assertRaises (IOError , memio .seek , - 1 , 2 )
480+ self .assertRaises (IOError , memio .seek , 1 , 1 )
481+ self .assertRaises (IOError , memio .seek , 1 , 2 )
482+
483+ def test_textio_properties (self ):
484+ memio = self .ioclass ()
485+
486+ # These are just dummy values but we nevertheless check them for fear
487+ # of unexpected breakage.
488+ self .assertTrue (memio .encoding is None )
489+ self .assertEqual (memio .errors , "strict" )
490+ self .assertEqual (memio .line_buffering , False )
441491
442492 def test_newlines_property (self ):
443493 memio = self .ioclass (newline = None )
@@ -519,15 +569,13 @@ def test_newline_lf(self):
519569 def test_newline_cr (self ):
520570 # newline="\r"
521571 memio = self .ioclass ("a\n b\r \n c\r d" , newline = "\r " )
522- memio .seek (0 )
523572 self .assertEqual (memio .read (), "a\r b\r \r c\r d" )
524573 memio .seek (0 )
525574 self .assertEqual (list (memio ), ["a\r " , "b\r " , "\r " , "c\r " , "d" ])
526575
527576 def test_newline_crlf (self ):
528577 # newline="\r\n"
529578 memio = self .ioclass ("a\n b\r \n c\r d" , newline = "\r \n " )
530- memio .seek (0 )
531579 self .assertEqual (memio .read (), "a\r \n b\r \r \n c\r d" )
532580 memio .seek (0 )
533581 self .assertEqual (list (memio ), ["a\r \n " , "b\r \r \n " , "c\r d" ])
@@ -538,6 +586,28 @@ def test_issue5265(self):
538586 self .assertEqual (memio .read (5 ), "a\n b\n " )
539587
540588
589+ class PyStringIOTest (MemoryTestMixin , MemorySeekTestMixin ,
590+ TextIOTestMixin , unittest .TestCase ):
591+ buftype = unicode
592+ ioclass = pyio .StringIO
593+ UnsupportedOperation = pyio .UnsupportedOperation
594+ EOF = ""
595+
596+
597+ class PyStringIOPickleTest (TextIOTestMixin , unittest .TestCase ):
598+ """Test if pickle restores properly the internal state of StringIO.
599+ """
600+ buftype = unicode
601+ UnsupportedOperation = pyio .UnsupportedOperation
602+ EOF = ""
603+
604+ class ioclass (pyio .StringIO ):
605+ def __new__ (cls , * args , ** kwargs ):
606+ return pickle .loads (pickle .dumps (pyio .StringIO (* args , ** kwargs )))
607+ def __init__ (self , * args , ** kwargs ):
608+ pass
609+
610+
541611class CBytesIOTest (PyBytesIOTest ):
542612 ioclass = io .BytesIO
543613 UnsupportedOperation = io .UnsupportedOperation
@@ -547,6 +617,33 @@ class CBytesIOTest(PyBytesIOTest):
547617 )(PyBytesIOTest .test_bytes_array )
548618
549619
620+ def test_getstate (self ):
621+ memio = self .ioclass ()
622+ state = memio .__getstate__ ()
623+ self .assertEqual (len (state ), 3 )
624+ bytearray (state [0 ]) # Check if state[0] supports the buffer interface.
625+ self .assert_ (isinstance (state [1 ], int ))
626+ self .assert_ (isinstance (state [2 ], dict ) or state [2 ] is None )
627+ memio .close ()
628+ self .assertRaises (ValueError , memio .__getstate__ )
629+
630+ def test_setstate (self ):
631+ # This checks whether __setstate__ does proper input validation.
632+ memio = self .ioclass ()
633+ memio .__setstate__ ((b"no error" , 0 , None ))
634+ memio .__setstate__ ((bytearray (b"no error" ), 0 , None ))
635+ memio .__setstate__ ((b"no error" , 0 , {'spam' : 3 }))
636+ self .assertRaises (ValueError , memio .__setstate__ , (b"" , - 1 , None ))
637+ self .assertRaises (TypeError , memio .__setstate__ , ("unicode" , 0 , None ))
638+ self .assertRaises (TypeError , memio .__setstate__ , (b"" , 0.0 , None ))
639+ self .assertRaises (TypeError , memio .__setstate__ , (b"" , 0 , 0 ))
640+ self .assertRaises (TypeError , memio .__setstate__ , (b"len-test" , 0 ))
641+ self .assertRaises (TypeError , memio .__setstate__ )
642+ self .assertRaises (TypeError , memio .__setstate__ , 0 )
643+ memio .close ()
644+ self .assertRaises (ValueError , memio .__setstate__ , (b"closed" , 0 , None ))
645+
646+
550647class CStringIOTest (PyStringIOTest ):
551648 ioclass = io .StringIO
552649 UnsupportedOperation = io .UnsupportedOperation
@@ -565,9 +662,50 @@ def test_widechar(self):
565662 self .assertEqual (memio .tell (), len (buf ) * 2 )
566663 self .assertEqual (memio .getvalue (), buf + buf )
567664
665+ def test_getstate (self ):
666+ memio = self .ioclass ()
667+ state = memio .__getstate__ ()
668+ self .assertEqual (len (state ), 4 )
669+ self .assert_ (isinstance (state [0 ], unicode ))
670+ self .assert_ (isinstance (state [1 ], str ))
671+ self .assert_ (isinstance (state [2 ], int ))
672+ self .assert_ (isinstance (state [3 ], dict ) or state [3 ] is None )
673+ memio .close ()
674+ self .assertRaises (ValueError , memio .__getstate__ )
675+
676+ def test_setstate (self ):
677+ # This checks whether __setstate__ does proper input validation.
678+ memio = self .ioclass ()
679+ memio .__setstate__ (("no error" , "\n " , 0 , None ))
680+ memio .__setstate__ (("no error" , "" , 0 , {'spam' : 3 }))
681+ self .assertRaises (ValueError , memio .__setstate__ , ("" , "f" , 0 , None ))
682+ self .assertRaises (ValueError , memio .__setstate__ , ("" , "" , - 1 , None ))
683+ self .assertRaises (TypeError , memio .__setstate__ , (b"" , "" , 0 , None ))
684+ # trunk is more tolerant than py3k on the type of the newline param
685+ #self.assertRaises(TypeError, memio.__setstate__, ("", b"", 0, None))
686+ self .assertRaises (TypeError , memio .__setstate__ , ("" , "" , 0.0 , None ))
687+ self .assertRaises (TypeError , memio .__setstate__ , ("" , "" , 0 , 0 ))
688+ self .assertRaises (TypeError , memio .__setstate__ , ("len-test" , 0 ))
689+ self .assertRaises (TypeError , memio .__setstate__ )
690+ self .assertRaises (TypeError , memio .__setstate__ , 0 )
691+ memio .close ()
692+ self .assertRaises (ValueError , memio .__setstate__ , ("closed" , "" , 0 , None ))
693+
694+
695+ class CStringIOPickleTest (PyStringIOPickleTest ):
696+ UnsupportedOperation = io .UnsupportedOperation
697+
698+ class ioclass (io .StringIO ):
699+ def __new__ (cls , * args , ** kwargs ):
700+ return pickle .loads (pickle .dumps (io .StringIO (* args , ** kwargs ),
701+ protocol = 2 ))
702+ def __init__ (self , * args , ** kwargs ):
703+ pass
704+
568705
569706def test_main ():
570- tests = [PyBytesIOTest , PyStringIOTest , CBytesIOTest , CStringIOTest ]
707+ tests = [PyBytesIOTest , PyStringIOTest , CBytesIOTest , CStringIOTest ,
708+ PyStringIOPickleTest , CStringIOPickleTest ]
571709 support .run_unittest (* tests )
572710
573711if __name__ == '__main__' :
0 commit comments