44 ZSTD_DStreamOutSize )
55from compression ._common import _streams
66
7- __all__ = (" ZstdFile" , " open" )
7+ __all__ = (' ZstdFile' , ' open' )
88
99_MODE_CLOSED = 0
1010_MODE_READ = 1
@@ -31,15 +31,15 @@ class ZstdFile(_streams.BaseStream):
3131 FLUSH_BLOCK = ZstdCompressor .FLUSH_BLOCK
3232 FLUSH_FRAME = ZstdCompressor .FLUSH_FRAME
3333
34- def __init__ (self , file , / , mode = "r" , * ,
34+ def __init__ (self , file , / , mode = 'r' , * ,
3535 level = None , options = None , zstd_dict = None ):
3636 """Open a Zstandard compressed file in binary mode.
3737
3838 *file* can be either an file-like object, or a file name to open.
3939
40- *mode* can be "r" for reading (default), "w" for (over)writing, "x" for
41- creating exclusively, or "a" for appending. These can equivalently be
42- given as "rb", "wb", "xb" and "ab" respectively.
40+ *mode* can be 'r' for reading (default), 'w' for (over)writing, 'x' for
41+ creating exclusively, or 'a' for appending. These can equivalently be
42+ given as 'rb', 'wb', 'xb' and 'ab' respectively.
4343
4444 *level* is an optional int specifying the compression level to use,
4545 or COMPRESSION_LEVEL_DEFAULT if not given.
@@ -57,33 +57,33 @@ def __init__(self, file, /, mode="r", *,
5757 self ._buffer = None
5858
5959 if not isinstance (mode , str ):
60- raise ValueError (" mode must be a str" )
60+ raise ValueError (' mode must be a str' )
6161 if options is not None and not isinstance (options , dict ):
62- raise TypeError (" options must be a dict or None" )
63- mode = mode .removesuffix ("b" ) # handle rb, wb, xb, ab
64- if mode == "r" :
62+ raise TypeError (' options must be a dict or None' )
63+ mode = mode .removesuffix ('b' ) # handle rb, wb, xb, ab
64+ if mode == 'r' :
6565 if level is not None :
66- raise TypeError (" level is illegal in read mode" )
66+ raise TypeError (' level is illegal in read mode' )
6767 self ._mode = _MODE_READ
68- elif mode in {"w" , "a" , "x" }:
68+ elif mode in {'w' , 'a' , 'x' }:
6969 if level is not None and not isinstance (level , int ):
70- raise TypeError (" level must be int or None" )
70+ raise TypeError (' level must be int or None' )
7171 self ._mode = _MODE_WRITE
7272 self ._compressor = ZstdCompressor (level = level , options = options ,
7373 zstd_dict = zstd_dict )
7474 self ._pos = 0
7575 else :
76- raise ValueError (f" Invalid mode: { mode !r} " )
76+ raise ValueError (f' Invalid mode: { mode !r} ' )
7777
7878 if isinstance (file , (str , bytes , PathLike )):
7979 self ._fp = io .open (file , f'{ mode } b' )
8080 self ._close_fp = True
81- elif ((mode == 'r' and hasattr (file , " read" ))
82- or (mode != 'r' and hasattr (file , " write" ))):
81+ elif ((mode == 'r' and hasattr (file , ' read' ))
82+ or (mode != 'r' and hasattr (file , ' write' ))):
8383 self ._fp = file
8484 else :
85- raise TypeError (" file must be a file-like object "
86- " or a str, bytes, or PathLike object" )
85+ raise TypeError (' file must be a file-like object '
86+ ' or a str, bytes, or PathLike object' )
8787
8888 if self ._mode == _MODE_READ :
8989 raw = _streams .DecompressReader (
@@ -151,22 +151,22 @@ def flush(self, mode=FLUSH_BLOCK):
151151 return
152152 self ._check_not_closed ()
153153 if mode not in {self .FLUSH_BLOCK , self .FLUSH_FRAME }:
154- raise ValueError (" Invalid mode argument, expected either "
155- " ZstdFile.FLUSH_FRAME or "
156- " ZstdFile.FLUSH_BLOCK" )
154+ raise ValueError (' Invalid mode argument, expected either '
155+ ' ZstdFile.FLUSH_FRAME or '
156+ ' ZstdFile.FLUSH_BLOCK' )
157157 if self ._compressor .last_mode == mode :
158158 return
159159 # Flush zstd block/frame, and write.
160160 data = self ._compressor .flush (mode )
161161 self ._fp .write (data )
162- if hasattr (self ._fp , " flush" ):
162+ if hasattr (self ._fp , ' flush' ):
163163 self ._fp .flush ()
164164
165165 def read (self , size = - 1 ):
166166 """Read up to size uncompressed bytes from the file.
167167
168168 If size is negative or omitted, read until EOF is reached.
169- Returns b"" if the file is already at EOF.
169+ Returns b'' if the file is already at EOF.
170170 """
171171 if size is None :
172172 size = - 1
@@ -178,7 +178,7 @@ def read1(self, size=-1):
178178 making multiple reads from the underlying stream. Reads up to a
179179 buffer's worth of data if size is negative.
180180
181- Returns b"" if the file is at EOF.
181+ Returns b'' if the file is at EOF.
182182 """
183183 self ._check_can_read ()
184184 if size < 0 :
@@ -293,16 +293,16 @@ def writable(self):
293293 return self ._mode == _MODE_WRITE
294294
295295
296- def open (file , / , mode = "rb" , * , level = None , options = None , zstd_dict = None ,
296+ def open (file , / , mode = 'rb' , * , level = None , options = None , zstd_dict = None ,
297297 encoding = None , errors = None , newline = None ):
298298 """Open a Zstandard compressed file in binary or text mode.
299299
300300 file can be either a file name (given as a str, bytes, or PathLike object),
301301 in which case the named file is opened, or it can be an existing file object
302302 to read from or write to.
303303
304- The mode parameter can be "r", "rb" (default), "w", "wb", "x", "xb", "a" ,
305- "ab" for binary mode, or "rt", "wt", "xt", "at" for text mode.
304+ The mode parameter can be 'r', 'rb' (default), 'w', 'wb', 'x', 'xb', 'a' ,
305+ 'ab' for binary mode, or 'rt', 'wt', 'xt', 'at' for text mode.
306306
307307 The level, options, and zstd_dict parameters specify the settings the same
308308 as ZstdFile.
@@ -323,19 +323,19 @@ def open(file, /, mode="rb", *, level=None, options=None, zstd_dict=None,
323323 behavior, and line ending(s).
324324 """
325325
326- text_mode = "t" in mode
327- mode = mode .replace ("t" , "" )
326+ text_mode = 't' in mode
327+ mode = mode .replace ('t' , '' )
328328
329329 if text_mode :
330- if "b" in mode :
331- raise ValueError (f" Invalid mode: { mode !r} " )
330+ if 'b' in mode :
331+ raise ValueError (f' Invalid mode: { mode !r} ' )
332332 else :
333333 if encoding is not None :
334- raise ValueError (" Argument ' encoding' not supported in binary mode" )
334+ raise ValueError (' Argument " encoding" not supported in binary mode' )
335335 if errors is not None :
336- raise ValueError (" Argument ' errors' not supported in binary mode" )
336+ raise ValueError (' Argument " errors" not supported in binary mode' )
337337 if newline is not None :
338- raise ValueError (" Argument ' newline' not supported in binary mode" )
338+ raise ValueError (' Argument " newline" not supported in binary mode' )
339339
340340 binary_file = ZstdFile (file , mode , level = level , options = options ,
341341 zstd_dict = zstd_dict )
0 commit comments