@@ -290,21 +290,15 @@ def _select_formatspecs_neo(formatspecs=None, fmttype=None, outfile=None):
290290 # fall back to environment/defaults
291291 return _ensure_formatspecs (formatspecs )
292292
293- def make_empty_file_pointer_neo (fp , fmttype = None , checksumtype = 'crc32' , formatspecs = None , encoding = 'UTF-8' ):
293+ def make_empty_file_pointer_neo (fp , fmttype = None , checksumtype = 'crc32' ,
294+ formatspecs = None , encoding = 'UTF-8' ):
294295 """
295- Write an empty archive (header + end marker) to an open, writable file-like object.
296- Returns the same fp (positioned at end).
296+ Write an empty archive (header + end marker) to an open, writable
297+ file-like object. Returns the same fp (positioned at end).
297298 """
298299 fs = _select_formatspecs_neo (formatspecs , fmttype , None )
299- d = fs ['format_delimiter' ]
300-
301- class _Dst (object ):
302- def __init__ (self , fp ): self .fp = fp
303- def write (self , data ): self .fp .write (data if isinstance (data , (bytes , bytearray )) else b (data ))
304-
305- dst = _Dst (fp )
306300 _write_global_header (fp , 0 , encoding , checksumtype , extradata = [], formatspecs = fs )
307- fp .write (_append_nulls (['0' , '0' ], d ))
301+ fp .write (_append_nulls (['0' , '0' ], fs [ 'format_delimiter' ] ))
308302 try :
309303 fp .flush ()
310304 if hasattr (os , 'fsync' ):
@@ -313,10 +307,13 @@ def write(self, data): self.fp.write(data if isinstance(data, (bytes, bytearray)
313307 pass
314308 return fp
315309
316- def make_empty_archive_file_pointer_neo (fp , fmttype = None , checksumtype = 'crc32' , formatspecs = None , encoding = 'UTF-8' ):
310+
311+ def make_empty_archive_file_pointer_neo (fp , fmttype = None , checksumtype = 'crc32' ,
312+ formatspecs = None , encoding = 'UTF-8' ):
317313 """Alias for symmetry with other API names."""
318314 return make_empty_file_pointer_neo (fp , fmttype , checksumtype , formatspecs , encoding )
319315
316+
320317def make_empty_file_neo (outfile = None , fmttype = None , checksumtype = 'crc32' , formatspecs = None ,
321318 encoding = 'UTF-8' , returnfp = False ):
322319 """
@@ -327,33 +324,41 @@ def make_empty_file_neo(outfile=None, fmttype=None, checksumtype='crc32', format
327324 - returnfp=True returns the open file object when writing to a file-like or path.
328325 """
329326 fs = _select_formatspecs_neo (formatspecs , fmttype , outfile )
330- d = fs ['format_delimiter' ]
331327
332- bufmode , fp , buf = _wrap_outfile (outfile )
333- class _Dst (object ):
334- def write (self , data ): _write (bufmode , fp , buf , data )
335- dst = _Dst ()
336- _write_global_header (fp , 0 , encoding , checksumtype , extradata = [], formatspecs = fs )
337- fp .write (_append_nulls (['0' , '0' ], d ))
328+ fp , close_me , to_bytes = _wrap_outfile (outfile )
329+ try :
330+ _write_global_header (fp , 0 , encoding , checksumtype , extradata = [], formatspecs = fs )
331+ fp .write (_append_nulls (['0' , '0' ], fs ['format_delimiter' ]))
332+
333+ # Return policy
334+ if to_bytes :
335+ # in-memory build (outfile is '-' or None)
336+ return fp .getvalue ()
337+
338+ if returnfp :
339+ try :
340+ if hasattr (fp , 'seek' ):
341+ fp .seek (0 , os .SEEK_SET )
342+ except Exception :
343+ pass
344+ # Caller manages lifetime if we hand back the fp
345+ return fp
338346
339- # return policy
340- if bufmode :
341- # in-memory build (outfile is '-' or None)
342- return bytes (buf )
343- if returnfp :
344347 try :
345- if hasattr (fp , 'seek' ):
346- fp .seek (0 , os .SEEK_SET )
348+ fp .flush ()
349+ if hasattr (os , 'fsync' ):
350+ os .fsync (fp .fileno ())
347351 except Exception :
348352 pass
349- return fp
350- try :
351- fp .flush ()
352- if hasattr (os , 'fsync' ):
353- os .fsync (fp .fileno ())
354- except Exception :
355- pass
356- return True
353+ return True
354+ finally :
355+ # Only close if we opened it AND we're not returning the fp
356+ if close_me and not returnfp :
357+ try :
358+ fp .close ()
359+ except Exception :
360+ pass
361+
357362
358363def make_empty_archive_file_neo (outfile = None , fmttype = None , checksumtype = 'crc32' , formatspecs = None ,
359364 encoding = 'UTF-8' , returnfp = False ):
0 commit comments