10
10
import sys
11
11
import unittest
12
12
from subprocess import PIPE , Popen
13
- from test import support
14
- from test .support import _4G , bigmemtest , os_helper , import_helper
13
+ from test .support import import_helper
14
+ from test .support import os_helper
15
+ from test .support import _4G , bigmemtest
15
16
from test .support .script_helper import assert_python_ok , assert_python_failure
16
17
17
18
gzip = import_helper .import_module ('gzip' )
@@ -328,8 +329,15 @@ def test_metadata(self):
328
329
cmByte = fRead .read (1 )
329
330
self .assertEqual (cmByte , b'\x08 ' ) # deflate
330
331
332
+ try :
333
+ expectedname = self .filename .encode ('Latin-1' ) + b'\x00 '
334
+ expectedflags = b'\x08 ' # only the FNAME flag is set
335
+ except UnicodeEncodeError :
336
+ expectedname = b''
337
+ expectedflags = b'\x00 '
338
+
331
339
flagsByte = fRead .read (1 )
332
- self .assertEqual (flagsByte , b' \x08 ' ) # only the FNAME flag is set
340
+ self .assertEqual (flagsByte , expectedflags )
333
341
334
342
mtimeBytes = fRead .read (4 )
335
343
self .assertEqual (mtimeBytes , struct .pack ('<i' , mtime )) # little-endian
@@ -344,9 +352,8 @@ def test_metadata(self):
344
352
# RFC 1952 specifies that this is the name of the input file, if any.
345
353
# However, the gzip module defaults to storing the name of the output
346
354
# file in this field.
347
- expected = self .filename .encode ('Latin-1' ) + b'\x00 '
348
- nameBytes = fRead .read (len (expected ))
349
- self .assertEqual (nameBytes , expected )
355
+ nameBytes = fRead .read (len (expectedname ))
356
+ self .assertEqual (nameBytes , expectedname )
350
357
351
358
# Since no other flags were set, the header ends here.
352
359
# Rather than process the compressed data, let's seek to the trailer.
@@ -358,6 +365,10 @@ def test_metadata(self):
358
365
isizeBytes = fRead .read (4 )
359
366
self .assertEqual (isizeBytes , struct .pack ('<i' , len (data1 )))
360
367
368
+ def test_metadata_ascii_name (self ):
369
+ self .filename = os_helper .TESTFN_ASCII
370
+ self .test_metadata ()
371
+
361
372
def test_compresslevel_metadata (self ):
362
373
# see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
363
374
# specifically, discussion of XFL in section 2.3.1
@@ -489,7 +500,9 @@ def test_fileobj_mode(self):
489
500
if "x" in mode :
490
501
os_helper .unlink (self .filename )
491
502
with open (self .filename , mode ) as f :
492
- with gzip .GzipFile (fileobj = f ) as g :
503
+ with self .assertWarns (FutureWarning ):
504
+ g = gzip .GzipFile (fileobj = f )
505
+ with g :
493
506
self .assertEqual (g .mode , gzip .WRITE )
494
507
495
508
def test_bytes_filename (self ):
@@ -578,6 +591,15 @@ def test_prepend_error(self):
578
591
with gzip .open (self .filename , "rb" ) as f :
579
592
f ._buffer .raw ._fp .prepend ()
580
593
594
+ def test_issue44439 (self ):
595
+ q = array .array ('Q' , [1 , 2 , 3 , 4 , 5 ])
596
+ LENGTH = len (q ) * q .itemsize
597
+
598
+ with gzip .GzipFile (fileobj = io .BytesIO (), mode = 'w' ) as f :
599
+ self .assertEqual (f .write (q ), LENGTH )
600
+ self .assertEqual (f .tell (), LENGTH )
601
+
602
+
581
603
class TestOpen (BaseTest ):
582
604
def test_binary_modes (self ):
583
605
uncompressed = data1 * 50
@@ -647,14 +669,14 @@ def test_implicit_binary_modes(self):
647
669
def test_text_modes (self ):
648
670
uncompressed = data1 .decode ("ascii" ) * 50
649
671
uncompressed_raw = uncompressed .replace ("\n " , os .linesep )
650
- with gzip .open (self .filename , "wt" ) as f :
672
+ with gzip .open (self .filename , "wt" , encoding = "ascii" ) as f :
651
673
f .write (uncompressed )
652
674
with open (self .filename , "rb" ) as f :
653
675
file_data = gzip .decompress (f .read ()).decode ("ascii" )
654
676
self .assertEqual (file_data , uncompressed_raw )
655
- with gzip .open (self .filename , "rt" ) as f :
677
+ with gzip .open (self .filename , "rt" , encoding = "ascii" ) as f :
656
678
self .assertEqual (f .read (), uncompressed )
657
- with gzip .open (self .filename , "at" ) as f :
679
+ with gzip .open (self .filename , "at" , encoding = "ascii" ) as f :
658
680
f .write (uncompressed )
659
681
with open (self .filename , "rb" ) as f :
660
682
file_data = gzip .decompress (f .read ()).decode ("ascii" )
@@ -668,7 +690,7 @@ def test_fileobj(self):
668
690
self .assertEqual (f .read (), uncompressed_bytes )
669
691
with gzip .open (io .BytesIO (compressed ), "rb" ) as f :
670
692
self .assertEqual (f .read (), uncompressed_bytes )
671
- with gzip .open (io .BytesIO (compressed ), "rt" ) as f :
693
+ with gzip .open (io .BytesIO (compressed ), "rt" , encoding = "ascii" ) as f :
672
694
self .assertEqual (f .read (), uncompressed_str )
673
695
674
696
def test_bad_params (self ):
@@ -716,9 +738,9 @@ def test_encoding_error_handler(self):
716
738
def test_newline (self ):
717
739
# Test with explicit newline (universal newline mode disabled).
718
740
uncompressed = data1 .decode ("ascii" ) * 50
719
- with gzip .open (self .filename , "wt" , newline = "\n " ) as f :
741
+ with gzip .open (self .filename , "wt" , encoding = "ascii" , newline = "\n " ) as f :
720
742
f .write (uncompressed )
721
- with gzip .open (self .filename , "rt" , newline = "\r " ) as f :
743
+ with gzip .open (self .filename , "rt" , encoding = "ascii" , newline = "\r " ) as f :
722
744
self .assertEqual (f .readlines (), [uncompressed ])
723
745
724
746
@@ -768,10 +790,10 @@ def test_decompress_infile_outfile(self):
768
790
self .assertEqual (err , b'' )
769
791
770
792
def test_decompress_infile_outfile_error (self ):
771
- rc , out , err = assert_python_ok ('-m' , 'gzip' , '-d' , 'thisisatest.out' )
772
- self .assertIn (b"filename doesn't end in .gz:" , out )
773
- self .assertEqual (rc , 0 )
774
- self .assertEqual (err , b'' )
793
+ rc , out , err = assert_python_failure ('-m' , 'gzip' , '-d' , 'thisisatest.out' )
794
+ self .assertEqual (b"filename doesn't end in .gz: 'thisisatest.out' " , err . strip () )
795
+ self .assertEqual (rc , 1 )
796
+ self .assertEqual (out , b'' )
775
797
776
798
@create_and_remove_directory (TEMPDIR )
777
799
def test_compress_stdin_outfile (self ):
@@ -827,9 +849,5 @@ def test_decompress_cannot_have_flags_compression(self):
827
849
self .assertEqual (out , b'' )
828
850
829
851
830
- def test_main (verbose = None ):
831
- support .run_unittest (TestGzip , TestOpen , TestCommandLine )
832
-
833
-
834
852
if __name__ == "__main__" :
835
- test_main ( verbose = True )
853
+ unittest . main ( )
0 commit comments