@@ -271,12 +271,15 @@ def __repr__(self):
271
271
)
272
272
273
273
def _feed_pipe (self ):
274
- while self ._feeding :
275
- chunk = self .fileobj .read (BUFFER_SIZE )
276
- if chunk == b"" :
277
- self .in_pipe .close ()
278
- return
279
- self .in_pipe .write (chunk )
274
+ try :
275
+ while self ._feeding :
276
+ chunk = self .fileobj .read (BUFFER_SIZE )
277
+ if chunk == b"" :
278
+ self .in_pipe .close ()
279
+ return
280
+ self .in_pipe .write (chunk )
281
+ finally :
282
+ self .in_pipe .close ()
280
283
281
284
def write (self , arg : bytes ) -> int :
282
285
return self ._file .write (arg )
@@ -312,18 +315,21 @@ def close(self) -> None:
312
315
self ._stderr .close ()
313
316
return
314
317
check_allowed_code_and_message = False
315
- if self .fileobj :
316
- self ._file .close ()
317
- self .process .wait ()
318
- self .fileobj .close ()
319
- else :
318
+ if "r" in self ._mode :
319
+ self ._feeding = False
320
+ self ._file .read ()
320
321
retcode = self .process .poll ()
321
322
if retcode is None :
322
323
# still running
323
324
self .process .terminate ()
324
325
check_allowed_code_and_message = True
325
326
self .process .wait ()
327
+ if self .in_thread :
328
+ self .in_thread .join ()
326
329
self ._file .close ()
330
+ else :
331
+ self ._file .close ()
332
+ self .process .wait ()
327
333
stderr_message = self ._read_error_message ()
328
334
self ._stderr .close ()
329
335
if not self ._error_raised :
@@ -591,6 +597,8 @@ def _detect_format_from_content(fileobj: BinaryIO) -> Optional[str]:
591
597
Attempts to detect file format from the content by reading the first
592
598
6 bytes. Returns None if no format could be detected.
593
599
"""
600
+ if not fileobj .readable ():
601
+ return None
594
602
if hasattr (fileobj , "peek" ):
595
603
bs = fileobj .peek (6 )
596
604
elif hasattr (fileobj , "seekable" ) and fileobj .seekable ():
@@ -651,7 +659,7 @@ def _file_or_path_to_name_and_binary_stream(
651
659
return "" , file_or_path
652
660
else :
653
661
raise TypeError (
654
- f"Unsupported type for { file_or_path :r } , "
662
+ f"Unsupported type for { file_or_path } , "
655
663
f"{ file_or_path .__class__ .__name__ } ."
656
664
)
657
665
0 commit comments