Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix silently failing on Windows #33

Merged
3 commits merged into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions send2trash/plat_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from __future__ import unicode_literals

from ctypes import (windll, Structure, byref, c_uint,
create_unicode_buffer, addressof)
create_unicode_buffer, addressof,
GetLastError, FormatError)
from ctypes.wintypes import HWND, UINT, LPCWSTR, BOOL
import os.path as op

Expand Down Expand Up @@ -49,6 +50,11 @@ def get_short_path_name(long_name):
if not long_name.startswith('\\\\?\\'):
long_name = '\\\\?\\' + long_name
buf_size = GetShortPathNameW(long_name, None, 0)
# FIX: https://github.com/hsoft/send2trash/issues/31
# If buffer size is zero, an error has occurred.
if not buf_size:
err_no = GetLastError()
raise WindowsError(err_no, FormatError(err_no), long_name[4:])
output = create_unicode_buffer(buf_size)
GetShortPathNameW(long_name, output, buf_size)
return output.value[4:] # Remove '\\?\' for SHFileOperationW
Expand Down Expand Up @@ -83,4 +89,4 @@ def send2trash(path):
fileop.lpszProgressTitle = None
result = SHFileOperationW(byref(fileop))
if result:
raise WindowsError(None, None, path, result)
raise WindowsError(result, FormatError(result), path)
36 changes: 32 additions & 4 deletions tests/test_plat_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,40 @@
from send2trash import send2trash as s2t


@unittest.skipIf(sys.platform != 'win32', 'Windows only')
class TestNormal(unittest.TestCase):
def setUp(self):
self.dirname = '\\\\?\\' + op.join(gettempdir(), 'python.send2trash')
self.file = op.join(self.dirname, 'testfile.txt')
self._create_tree(self.file)

def tearDown(self):
try:
os.remove(self.dirname)
except OSError:
pass

def _create_tree(self, path):
dirname = op.dirname(path)
if not op.isdir(dirname):
os.makedirs(dirname)
with open(path, 'w') as writer:
writer.write('send2trash test')

def test_trash_file(self):
s2t(self.file)
self.assertFalse(op.exists(self.file))

def test_file_not_found(self):
file = op.join(self.dirname, 'otherfile.txt')
self.assertRaises(WindowsError, s2t, file)

@unittest.skipIf(sys.platform != 'win32', 'Windows only')
class TestLongPath(unittest.TestCase):
def setUp(self):
filename = 'A' * 100
self.dirname = '\\\\?\\' + os.path.join(gettempdir(), filename)
self.file = os.path.join(
self.dirname = '\\\\?\\' + op.join(gettempdir(), filename)
self.file = op.join(
self.dirname,
filename,
filename, # From there, the path is not trashable from Explorer
Expand All @@ -28,8 +56,8 @@ def tearDown(self):
pass

def _create_tree(self, path):
dirname = os.path.dirname(path)
if not os.path.isdir(dirname):
dirname = op.dirname(path)
if not op.isdir(dirname):
os.makedirs(dirname)
with open(path, 'w') as writer:
writer.write('Looong filename!')
Expand Down