@@ -562,15 +562,16 @@ pub(super) mod _os {
562562
563563 #[ pymethod]
564564 fn is_dir ( & self , follow_symlinks : FollowSymlinks , vm : & VirtualMachine ) -> PyResult < bool > {
565+ // Use cached file_type first to avoid stat() calls that may fail
566+ if let Ok ( file_type) = & self . file_type {
567+ if !follow_symlinks. 0 || !file_type. is_symlink ( ) {
568+ return Ok ( file_type. is_dir ( ) ) ;
569+ }
570+ }
565571 match super :: fs_metadata ( & self . pathval , follow_symlinks. 0 ) {
566572 Ok ( meta) => Ok ( meta. is_dir ( ) ) ,
567573 Err ( e) => {
568574 if e. kind ( ) == io:: ErrorKind :: NotFound {
569- // On Windows, use cached file_type when file is removed
570- #[ cfg( windows) ]
571- if let Ok ( file_type) = & self . file_type {
572- return Ok ( file_type. is_dir ( ) ) ;
573- }
574575 Ok ( false )
575576 } else {
576577 Err ( e. into_pyexception ( vm) )
@@ -581,15 +582,16 @@ pub(super) mod _os {
581582
582583 #[ pymethod]
583584 fn is_file ( & self , follow_symlinks : FollowSymlinks , vm : & VirtualMachine ) -> PyResult < bool > {
585+ // Use cached file_type first to avoid stat() calls that may fail
586+ if let Ok ( file_type) = & self . file_type {
587+ if !follow_symlinks. 0 || !file_type. is_symlink ( ) {
588+ return Ok ( file_type. is_file ( ) ) ;
589+ }
590+ }
584591 match super :: fs_metadata ( & self . pathval , follow_symlinks. 0 ) {
585592 Ok ( meta) => Ok ( meta. is_file ( ) ) ,
586593 Err ( e) => {
587594 if e. kind ( ) == io:: ErrorKind :: NotFound {
588- // On Windows, use cached file_type when file is removed
589- #[ cfg( windows) ]
590- if let Ok ( file_type) = & self . file_type {
591- return Ok ( file_type. is_file ( ) ) ;
592- }
593595 Ok ( false )
594596 } else {
595597 Err ( e. into_pyexception ( vm) )
0 commit comments