@@ -319,6 +319,38 @@ class LzmaListTest(LzmaTest, ListTest):
319319
320320class CommonReadTest (ReadTest ):
321321
322+ def test_is_tarfile_erroneous (self ):
323+ with open (tmpname , "wb" ):
324+ pass
325+
326+ # is_tarfile works on filenames
327+ self .assertFalse (tarfile .is_tarfile (tmpname ))
328+
329+ # is_tarfile works on path-like objects
330+ self .assertFalse (tarfile .is_tarfile (pathlib .Path (tmpname )))
331+
332+ # is_tarfile works on file objects
333+ with open (tmpname , "rb" ) as fobj :
334+ self .assertFalse (tarfile .is_tarfile (fobj ))
335+
336+ # is_tarfile works on file-like objects
337+ self .assertFalse (tarfile .is_tarfile (io .BytesIO (b"invalid" )))
338+
339+ def test_is_tarfile_valid (self ):
340+ # is_tarfile works on filenames
341+ self .assertTrue (tarfile .is_tarfile (self .tarname ))
342+
343+ # is_tarfile works on path-like objects
344+ self .assertTrue (tarfile .is_tarfile (pathlib .Path (self .tarname )))
345+
346+ # is_tarfile works on file objects
347+ with open (self .tarname , "rb" ) as fobj :
348+ self .assertTrue (tarfile .is_tarfile (fobj ))
349+
350+ # is_tarfile works on file-like objects
351+ with open (self .tarname , "rb" ) as fobj :
352+ self .assertTrue (tarfile .is_tarfile (io .BytesIO (fobj .read ())))
353+
322354 def test_empty_tarfile (self ):
323355 # Test for issue6123: Allow opening empty archives.
324356 # This test checks if tarfile.open() is able to open an empty tar
0 commit comments