@@ -98,6 +98,7 @@ class TempfilePatcher:
9898
9999 def __init__ (self ):
100100 self .tempfile_cleanup = None
101+ self .tempfile_rmtree = None
101102
102103 def start_patching (self ):
103104 if self .tempfile_cleanup is not None :
@@ -109,6 +110,16 @@ def cleanup(self_, windows=(os.name == "nt"), unlink=None):
109110
110111 self .tempfile_cleanup = tempfile ._TemporaryFileCloser .cleanup # type: ignore[module-attr]
111112 tempfile ._TemporaryFileCloser .cleanup = cleanup # type: ignore[module-attr]
113+
114+ if sys .version_info >= (3 , 13 ) and hasattr (tempfile , "_rmtree" ):
115+ # Debian patches tempfile by importing or copying shutil.rmtree as _rmtree
116+ # we patch this to use the original (patched) version
117+ def _rmtree (* args , ** kwargs ):
118+ return tempfile ._shutil .rmtree (* args , ** kwargs )
119+
120+ self .tempfile_rmtree = tempfile ._rmtree # type: ignore[module-attr]
121+ tempfile ._rmtree = _rmtree # type: ignore[module-attr]
122+
112123 elif sys .platform != "win32" :
113124
114125 def close (self_ , unlink = None ):
@@ -125,6 +136,9 @@ def stop_patching(self):
125136 else :
126137 tempfile ._TemporaryFileCloser .cleanup = self .tempfile_cleanup # type: ignore[module-attr]
127138 self .tempfile_cleanup = None
139+ if self .tempfile_rmtree is not None :
140+ tempfile ._rmtree = self .tempfile_rmtree # type: ignore[module-attr]
141+ self .tempfile_rmtree = None
128142 # reset the cached tempdir in tempfile
129143 tempfile .tempdir = None
130144
@@ -1063,12 +1077,13 @@ def start_patching(self) -> None:
10631077 self ._paused = False
10641078
10651079 self .linecache_patcher .start_patching ()
1066- self .tempfile_patcher .start_patching ()
10671080
10681081 self .patch_modules ()
10691082 self .patch_functions ()
10701083 self .patch_defaults ()
1084+
10711085 self ._set_glob_os_functions ()
1086+ self .tempfile_patcher .start_patching ()
10721087
10731088 self ._dyn_patcher = DynamicPatcher (self )
10741089 sys .meta_path .insert (0 , self ._dyn_patcher )
0 commit comments