@@ -797,117 +797,6 @@ def test_slice(self):
797797 with self .assertRaises (ValueError ):
798798 marshal .dumps (obj , version )
799799
800- @support .cpython_only
801- @unittest .skipUnless (_testcapi , 'requires _testcapi' )
802- class CAPI_TestCase (unittest .TestCase , HelperMixin ):
803-
804- def test_read_from_file_error (self ):
805- # A read error is reported as OSError, not EOFError.
806- # A directory cannot be read (on some platforms it cannot even
807- # be opened, which is reported as OSError as well).
808- os .mkdir (os_helper .TESTFN )
809- self .addCleanup (os_helper .rmdir , os_helper .TESTFN )
810- for func in (_testcapi .pymarshal_read_short_from_file ,
811- _testcapi .pymarshal_read_long_from_file ,
812- _testcapi .pymarshal_read_object_from_file ,
813- _testcapi .pymarshal_read_last_object_from_file ):
814- with self .subTest (func = func .__name__ ):
815- self .assertRaises (OSError , func , os_helper .TESTFN )
816-
817- @unittest .skipUnless (os .path .exists ('/dev/full' ), 'requires /dev/full' )
818- def test_write_to_file_error (self ):
819- # A write error is reported as OSError.
820- # The data is large enough to not fit in the stdio buffer, so that
821- # the error is detected before the file is closed.
822- obj = b'x' * 100000
823- with self .assertRaises (OSError ):
824- _testcapi .pymarshal_write_object_to_file (obj , '/dev/full' ,
825- marshal .version )
826-
827- def test_write_unmarshallable_to_file (self ):
828- self .addCleanup (os_helper .unlink , os_helper .TESTFN )
829- with self .assertRaisesRegex (ValueError , 'unmarshallable object' ):
830- _testcapi .pymarshal_write_object_to_file (object (), os_helper .TESTFN ,
831- marshal .version )
832-
833- def test_write_long_to_file (self ):
834- for v in range (marshal .version + 1 ):
835- _testcapi .pymarshal_write_long_to_file (0x12345678 , os_helper .TESTFN , v )
836- with open (os_helper .TESTFN , 'rb' ) as f :
837- data = f .read ()
838- os_helper .unlink (os_helper .TESTFN )
839- self .assertEqual (data , b'\x78 \x56 \x34 \x12 ' )
840-
841- def test_write_object_to_file (self ):
842- obj = ('\u20ac ' , b'abc' , 123 , 45.6 , 7 + 8j , 'long line ' * 1000 )
843- for v in range (marshal .version + 1 ):
844- _testcapi .pymarshal_write_object_to_file (obj , os_helper .TESTFN , v )
845- with open (os_helper .TESTFN , 'rb' ) as f :
846- data = f .read ()
847- os_helper .unlink (os_helper .TESTFN )
848- self .assertEqual (marshal .loads (data ), obj )
849-
850- def test_read_short_from_file (self ):
851- with open (os_helper .TESTFN , 'wb' ) as f :
852- f .write (b'\x34 \x12 xxxx' )
853- r , p = _testcapi .pymarshal_read_short_from_file (os_helper .TESTFN )
854- os_helper .unlink (os_helper .TESTFN )
855- self .assertEqual (r , 0x1234 )
856- self .assertEqual (p , 2 )
857-
858- with open (os_helper .TESTFN , 'wb' ) as f :
859- f .write (b'\x12 ' )
860- with self .assertRaises (EOFError ):
861- _testcapi .pymarshal_read_short_from_file (os_helper .TESTFN )
862- os_helper .unlink (os_helper .TESTFN )
863-
864- def test_read_long_from_file (self ):
865- with open (os_helper .TESTFN , 'wb' ) as f :
866- f .write (b'\x78 \x56 \x34 \x12 xxxx' )
867- r , p = _testcapi .pymarshal_read_long_from_file (os_helper .TESTFN )
868- os_helper .unlink (os_helper .TESTFN )
869- self .assertEqual (r , 0x12345678 )
870- self .assertEqual (p , 4 )
871-
872- with open (os_helper .TESTFN , 'wb' ) as f :
873- f .write (b'\x56 \x34 \x12 ' )
874- with self .assertRaises (EOFError ):
875- _testcapi .pymarshal_read_long_from_file (os_helper .TESTFN )
876- os_helper .unlink (os_helper .TESTFN )
877-
878- def test_read_last_object_from_file (self ):
879- obj = ('\u20ac ' , b'abc' , 123 , 45.6 , 7 + 8j )
880- for v in range (marshal .version + 1 ):
881- data = marshal .dumps (obj , v )
882- with open (os_helper .TESTFN , 'wb' ) as f :
883- f .write (data + b'xxxx' )
884- r , p = _testcapi .pymarshal_read_last_object_from_file (os_helper .TESTFN )
885- os_helper .unlink (os_helper .TESTFN )
886- self .assertEqual (r , obj )
887-
888- with open (os_helper .TESTFN , 'wb' ) as f :
889- f .write (omit_last_byte (data ))
890- with self .assertRaises (EOFError ):
891- _testcapi .pymarshal_read_last_object_from_file (os_helper .TESTFN )
892- os_helper .unlink (os_helper .TESTFN )
893-
894- def test_read_object_from_file (self ):
895- obj = ('\u20ac ' , b'abc' , 123 , 45.6 , 7 + 8j )
896- for v in range (marshal .version + 1 ):
897- data = marshal .dumps (obj , v )
898- with open (os_helper .TESTFN , 'wb' ) as f :
899- f .write (data + b'xxxx' )
900- r , p = _testcapi .pymarshal_read_object_from_file (os_helper .TESTFN )
901- os_helper .unlink (os_helper .TESTFN )
902- self .assertEqual (r , obj )
903- self .assertEqual (p , len (data ))
904-
905- with open (os_helper .TESTFN , 'wb' ) as f :
906- f .write (omit_last_byte (data ))
907- with self .assertRaises (EOFError ):
908- _testcapi .pymarshal_read_object_from_file (os_helper .TESTFN )
909- os_helper .unlink (os_helper .TESTFN )
910-
911800
912801if __name__ == "__main__" :
913802 unittest .main ()
0 commit comments