272 |
272 |
273 If ``forcibly``, this tries to remove READ-ONLY files, too. |
273 If ``forcibly``, this tries to remove READ-ONLY files, too. |
274 """ |
274 """ |
275 if forcibly: |
275 if forcibly: |
276 |
276 |
277 def onerror(function, path, excinfo): |
277 def onexc(function, path, excinfo): |
278 if function is not os.remove: |
278 if function is not os.remove: |
279 raise |
279 raise |
280 # read-only files cannot be unlinked under Windows |
280 # read-only files cannot be unlinked under Windows |
281 s = os.stat(path) |
281 s = os.stat(path) |
282 if (s.st_mode & stat.S_IWRITE) != 0: |
282 if (s.st_mode & stat.S_IWRITE) != 0: |
283 raise |
283 raise |
284 os.chmod(path, stat.S_IMODE(s.st_mode) | stat.S_IWRITE) |
284 os.chmod(path, stat.S_IMODE(s.st_mode) | stat.S_IWRITE) |
285 os.remove(path) |
285 os.remove(path) |
286 |
286 |
287 else: |
287 else: |
288 onerror = None |
288 onexc = None |
289 return shutil.rmtree( |
289 try: |
290 self.join(path), ignore_errors=ignore_errors, onerror=onerror |
290 # pytype: disable=wrong-keyword-args |
291 ) |
291 return shutil.rmtree( |
|
292 self.join(path), ignore_errors=ignore_errors, onexc=onexc |
|
293 ) |
|
294 # pytype: enable=wrong-keyword-args |
|
295 except TypeError: # onexc was introduced in Python 3.12 |
|
296 return shutil.rmtree( |
|
297 self.join(path), ignore_errors=ignore_errors, onerror=onexc |
|
298 ) |
292 |
299 |
293 def setflags(self, path: bytes, l: bool, x: bool): |
300 def setflags(self, path: bytes, l: bool, x: bool): |
294 return util.setflags(self.join(path), l, x) |
301 return util.setflags(self.join(path), l, x) |
295 |
302 |
296 def stat(self, path: Optional[bytes] = None): |
303 def stat(self, path: Optional[bytes] = None): |