@@ -275,7 +275,39 @@ def _mock_candidate_names(*names):
275275 lambda : iter (names ))
276276
277277
278- class TestMkstempInner (BaseTestCase ):
278+ class TestBadTempdir :
279+
280+ def test_read_only_directory (self ):
281+ with _inside_empty_temp_dir ():
282+ oldmode = mode = os .stat (tempfile .tempdir ).st_mode
283+ mode &= ~ (stat .S_IWUSR | stat .S_IWGRP | stat .S_IWOTH )
284+ os .chmod (tempfile .tempdir , mode )
285+ try :
286+ if os .access (tempfile .tempdir , os .W_OK ):
287+ self .skipTest ("can't set the directory read-only" )
288+ with self .assertRaises (PermissionError ):
289+ self .make_temp ()
290+ self .assertEqual (os .listdir (tempfile .tempdir ), [])
291+ finally :
292+ os .chmod (tempfile .tempdir , oldmode )
293+
294+ def test_nonexisting_directory (self ):
295+ with _inside_empty_temp_dir ():
296+ tempdir = os .path .join (tempfile .tempdir , 'nonexistent' )
297+ with support .swap_attr (tempfile , 'tempdir' , tempdir ):
298+ with self .assertRaises (FileNotFoundError ):
299+ self .make_temp ()
300+
301+ def test_non_directory (self ):
302+ with _inside_empty_temp_dir ():
303+ tempdir = os .path .join (tempfile .tempdir , 'file' )
304+ open (tempdir , 'wb' ).close ()
305+ with support .swap_attr (tempfile , 'tempdir' , tempdir ):
306+ with self .assertRaises ((NotADirectoryError , FileNotFoundError )):
307+ self .make_temp ()
308+
309+
310+ class TestMkstempInner (TestBadTempdir , BaseTestCase ):
279311 """Test the internal function _mkstemp_inner."""
280312
281313 class mkstemped :
@@ -390,7 +422,7 @@ def test_textmode(self):
390422 os .lseek (f .fd , 0 , os .SEEK_SET )
391423 self .assertEqual (os .read (f .fd , 20 ), b"blat" )
392424
393- def default_mkstemp_inner (self ):
425+ def make_temp (self ):
394426 return tempfile ._mkstemp_inner (tempfile .gettempdir (),
395427 tempfile .template ,
396428 '' ,
@@ -401,11 +433,11 @@ def test_collision_with_existing_file(self):
401433 # the chosen name already exists
402434 with _inside_empty_temp_dir (), \
403435 _mock_candidate_names ('aaa' , 'aaa' , 'bbb' ):
404- (fd1 , name1 ) = self .default_mkstemp_inner ()
436+ (fd1 , name1 ) = self .make_temp ()
405437 os .close (fd1 )
406438 self .assertTrue (name1 .endswith ('aaa' ))
407439
408- (fd2 , name2 ) = self .default_mkstemp_inner ()
440+ (fd2 , name2 ) = self .make_temp ()
409441 os .close (fd2 )
410442 self .assertTrue (name2 .endswith ('bbb' ))
411443
@@ -417,7 +449,7 @@ def test_collision_with_existing_directory(self):
417449 dir = tempfile .mkdtemp ()
418450 self .assertTrue (dir .endswith ('aaa' ))
419451
420- (fd , name ) = self .default_mkstemp_inner ()
452+ (fd , name ) = self .make_temp ()
421453 os .close (fd )
422454 self .assertTrue (name .endswith ('bbb' ))
423455
@@ -529,9 +561,12 @@ def test_choose_directory(self):
529561 os .rmdir (dir )
530562
531563
532- class TestMkdtemp (BaseTestCase ):
564+ class TestMkdtemp (TestBadTempdir , BaseTestCase ):
533565 """Test mkdtemp()."""
534566
567+ def make_temp (self ):
568+ return tempfile .mkdtemp ()
569+
535570 def do_create (self , dir = None , pre = "" , suf = "" ):
536571 if dir is None :
537572 dir = tempfile .gettempdir ()
0 commit comments