From 79caa04feabcadb656c870b3c50cde0527efb1fc Mon Sep 17 00:00:00 2001 From: Dobatymo Date: Wed, 13 Mar 2024 20:35:19 +0800 Subject: [PATCH] fix assertion for missing files --- send2trash/win/modern.py | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/send2trash/win/modern.py b/send2trash/win/modern.py index b0fa65c..5fc601e 100644 --- a/send2trash/win/modern.py +++ b/send2trash/win/modern.py @@ -63,22 +63,27 @@ def send2trash(paths): # error as OSError so wrapping with try to convert pysink, sink = create_sink() try: - for path in paths: - item = shell.SHCreateItemFromParsingName(path, None, shell.IID_IShellItem) - fileop.DeleteItem(item, sink) - result = fileop.PerformOperations() - aborted = fileop.GetAnyOperationsAborted() - # if non-zero result or aborted throw an exception - assert not pysink.errors, pysink.errors - if result or aborted: - raise OSError(None, None, paths, result) - except pywintypes.com_error: - assert len(pysink.errors) == 1, pysink.errors - # convert to standard OS error, allows other code to get a - # normal errno - path, hr = pysink.errors[0] - hr = winerrormap.get(hr + 2**32, hr) - raise win_exception(hr, path) + try: + for path in paths: + item = shell.SHCreateItemFromParsingName(path, None, shell.IID_IShellItem) + fileop.DeleteItem(item, sink) + except pywintypes.com_error as error: + # convert to standard OS error, allows other code to get a + # normal errno + raise OSError(None, error.strerror, path, error.hresult) + + try: + result = fileop.PerformOperations() + aborted = fileop.GetAnyOperationsAborted() + # if non-zero result or aborted throw an exception + assert not pysink.errors, pysink.errors + if result or aborted: + raise OSError(None, None, paths, result) + except pywintypes.com_error: + assert len(pysink.errors) == 1, pysink.errors + path, hr = pysink.errors[0] + hr = winerrormap.get(hr + 2**32, hr) + raise win_exception(hr, path) finally: # Need to make sure we call this once fore every init pythoncom.CoUninitialize()