@@ -517,7 +517,21 @@ def _check_trash(os_path):
517
517
home_dev = os .stat (os .path .expanduser ('~' )).st_dev
518
518
return file_dev == home_dev
519
519
520
+ def is_non_empty_dir (os_path ):
521
+ if os .path .isdir (os_path ):
522
+ # A directory containing only leftover checkpoints is
523
+ # considered empty.
524
+ cp_dir = getattr (self .checkpoints , 'checkpoint_dir' , None )
525
+ if set (os .listdir (os_path )) - {cp_dir }:
526
+ return True
527
+
528
+ return False
529
+
520
530
if self .delete_to_trash :
531
+ if sys .platform == 'win32' and is_non_empty_dir (os_path ):
532
+ # send2trash can really delete files on Windows, so disallow
533
+ # deleting non-empty files. See Github issue 3631.
534
+ raise web .HTTPError (400 , u'Directory %s not empty' % os_path )
521
535
if _check_trash (os_path ):
522
536
self .log .debug ("Sending %s to trash" , os_path )
523
537
# Looking at the code in send2trash, I don't think the errors it
@@ -530,14 +544,9 @@ def _check_trash(os_path):
530
544
"to home directory" , os_path )
531
545
532
546
if os .path .isdir (os_path ):
533
- listing = os .listdir (os_path )
534
547
# Don't permanently delete non-empty directories.
535
- # A directory containing only leftover checkpoints is
536
- # considered empty.
537
- cp_dir = getattr (self .checkpoints , 'checkpoint_dir' , None )
538
- for entry in listing :
539
- if entry != cp_dir :
540
- raise web .HTTPError (400 , u'Directory %s not empty' % os_path )
548
+ if is_non_empty_dir (os_path ):
549
+ raise web .HTTPError (400 , u'Directory %s not empty' % os_path )
541
550
self .log .debug ("Removing directory %s" , os_path )
542
551
with self .perm_to_403 ():
543
552
shutil .rmtree (os_path )
0 commit comments