@@ -352,26 +352,50 @@ def test_ignore_zeros(self):
352352
353353
354354class MiscReadTestBase (CommonReadTest ):
355+ def requires_name_attribute (self ):
356+ pass
357+
355358 def test_no_name_argument (self ):
359+ self .requires_name_attribute ()
356360 with open (self .tarname , "rb" ) as fobj :
357- tar = tarfile .open (fileobj = fobj , mode = self .mode )
358- self .assertEqual (tar .name , os .path .abspath (fobj .name ))
361+ self .assertIsInstance (fobj .name , str )
362+ with tarfile .open (fileobj = fobj , mode = self .mode ) as tar :
363+ self .assertIsInstance (tar .name , str )
364+ self .assertEqual (tar .name , os .path .abspath (fobj .name ))
359365
360366 def test_no_name_attribute (self ):
361367 with open (self .tarname , "rb" ) as fobj :
362368 data = fobj .read ()
363369 fobj = io .BytesIO (data )
364370 self .assertRaises (AttributeError , getattr , fobj , "name" )
365371 tar = tarfile .open (fileobj = fobj , mode = self .mode )
366- self .assertEqual (tar .name , None )
372+ self .assertIsNone (tar .name )
367373
368374 def test_empty_name_attribute (self ):
369375 with open (self .tarname , "rb" ) as fobj :
370376 data = fobj .read ()
371377 fobj = io .BytesIO (data )
372378 fobj .name = ""
373379 with tarfile .open (fileobj = fobj , mode = self .mode ) as tar :
374- self .assertEqual (tar .name , None )
380+ self .assertIsNone (tar .name )
381+
382+ def test_int_name_attribute (self ):
383+ # Issue 21044: tarfile.open() should handle fileobj with an integer
384+ # 'name' attribute.
385+ fd = os .open (self .tarname , os .O_RDONLY )
386+ with open (fd , 'rb' ) as fobj :
387+ self .assertIsInstance (fobj .name , int )
388+ with tarfile .open (fileobj = fobj , mode = self .mode ) as tar :
389+ self .assertIsNone (tar .name )
390+
391+ def test_bytes_name_attribute (self ):
392+ self .requires_name_attribute ()
393+ tarname = os .fsencode (self .tarname )
394+ with open (tarname , 'rb' ) as fobj :
395+ self .assertIsInstance (fobj .name , bytes )
396+ with tarfile .open (fileobj = fobj , mode = self .mode ) as tar :
397+ self .assertIsInstance (tar .name , bytes )
398+ self .assertEqual (tar .name , os .path .abspath (fobj .name ))
375399
376400 def test_illegal_mode_arg (self ):
377401 with open (tmpname , 'wb' ):
@@ -549,11 +573,11 @@ class GzipMiscReadTest(GzipTest, MiscReadTestBase, unittest.TestCase):
549573 pass
550574
551575class Bz2MiscReadTest (Bz2Test , MiscReadTestBase , unittest .TestCase ):
552- def test_no_name_argument (self ):
576+ def requires_name_attribute (self ):
553577 self .skipTest ("BZ2File have no name attribute" )
554578
555579class LzmaMiscReadTest (LzmaTest , MiscReadTestBase , unittest .TestCase ):
556- def test_no_name_argument (self ):
580+ def requires_name_attribute (self ):
557581 self .skipTest ("LZMAFile have no name attribute" )
558582
559583
0 commit comments