@@ -565,6 +565,42 @@ def test_open_raises_with_trailing_separator_windows(self):
565
565
self .check_windows_only ()
566
566
self .check_open_raises_with_trailing_separator (errno .EINVAL )
567
567
568
+ @unittest .skipIf (not hasattr (os , "O_DIRECTORY" ), "opening directory not supported" )
569
+ def test_open_raises_if_not_dir (self ):
570
+ self .check_posix_only ()
571
+ file_path = self .make_path ("file.txt" )
572
+ self .create_file (file_path , contents = "foo" )
573
+ with self .assertRaises (NotADirectoryError ):
574
+ self .os .open (file_path , os .O_RDONLY | os .O_DIRECTORY )
575
+ dir_path = self .make_path ("dir" )
576
+ self .create_dir (dir_path )
577
+ with self .assertRaises (IsADirectoryError ):
578
+ self .os .open (dir_path , os .O_RDWR | os .O_DIRECTORY )
579
+
580
+ @unittest .skipIf (not hasattr (os , "O_NOFOLLOW" ), "NOFOLLOW attribute not supported" )
581
+ def test_open_nofollow_symlink_raises (self ):
582
+ self .skip_if_symlink_not_supported ()
583
+ file_path = self .make_path ("file.txt" )
584
+ self .create_file (file_path , contents = "foo" )
585
+ link_path = self .make_path ("link" )
586
+ self .create_symlink (link_path , file_path )
587
+ with self .assertRaises (OSError ) as cm :
588
+ self .os .open (link_path , os .O_RDONLY | os .O_NOFOLLOW )
589
+ assert cm .exception .errno == errno .ELOOP
590
+
591
+ @unittest .skipIf (not hasattr (os , "O_NOFOLLOW" ), "NOFOLLOW attribute not supported" )
592
+ def test_open_nofollow_symlink_as_parent_works (self ):
593
+ self .skip_if_symlink_not_supported ()
594
+ dir_path = self .make_path ("dir" )
595
+ self .create_dir (dir_path )
596
+ link_path = self .make_path ("link" )
597
+ self .create_symlink (link_path , dir_path )
598
+ file_path = self .os .path .join (link_path , "file.txt" )
599
+ self .create_file (file_path , contents = "foo" )
600
+ fd = self .os .open (file_path , os .O_RDONLY | os .O_NOFOLLOW )
601
+ self .assertGreater (fd , 0 )
602
+ self .os .close (fd )
603
+
568
604
def test_lexists_with_trailing_separator_linux_windows (self ):
569
605
self .check_linux_and_windows ()
570
606
self .skip_if_symlink_not_supported ()
@@ -5298,7 +5334,6 @@ def test_scandir_stat_nlink(self):
5298
5334
self .assertEqual (1 , self .os .stat (self .file_path ).st_nlink )
5299
5335
5300
5336
@unittest .skipIf (not hasattr (os , "O_DIRECTORY" ), "opening directory not supported" )
5301
- @unittest .skipIf (sys .version_info < (3 , 7 ), "fd not supported for scandir" )
5302
5337
def test_scandir_with_fd (self ):
5303
5338
# regression test for #723
5304
5339
temp_dir = self .make_path ("tmp" , "dir" )
0 commit comments