Skip to content

Commit

Permalink
Fix up exceptions that can have unicode chars
Browse files Browse the repository at this point in the history
  • Loading branch information
jborean93 committed Nov 20, 2019
1 parent 74411dc commit dd1b5de
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
15 changes: 9 additions & 6 deletions smbclient/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ def readlink(path, **kwargs):
reparse_buffer = _get_reparse_point(norm_path, **kwargs)
reparse_tag = reparse_buffer['reparse_tag']
if reparse_tag.get_value() != ReparseTags.IO_REPARSE_TAG_SYMLINK:
raise ValueError("Cannot read link of reparse point with tag %s at '%s'" % (str(reparse_tag), norm_path))
raise ValueError(to_native("Cannot read link of reparse point with tag %s at '%s'" % (str(reparse_tag),
norm_path)))

symlink_buffer = SymbolicLinkReparseDataBuffer()
symlink_buffer.unpack(reparse_buffer['data_buffer'].get_value())
Expand Down Expand Up @@ -556,7 +557,8 @@ def symlink(src, dst, target_is_directory=False, **kwargs):
src_drive = ntpath.splitdrive(norm_src)[0]
dst_drive = ntpath.splitdrive(norm_dst)[0]
if src_drive.lower() != dst_drive.lower():
raise ValueError("Resolved link src root '%s' must be the same as the dst root '%s'" % (src_drive, dst_drive))
raise ValueError(to_native("Resolved link src root '%s' must be the same as the dst root '%s'"
% (src_drive, dst_drive)))

try:
src_stat = stat(norm_src, **kwargs)
Expand Down Expand Up @@ -1033,9 +1035,10 @@ def copyfile(src, dst, **kwargs):
"""

if not _is_sameshare(src, dst):
raise ValueError(
"Server side copy can only occur on the same drive, '%s' must be the same as the dst root '%s'" % (
src, dst))
raise ValueError(to_native(
"Server side copy can only occur on the same drive, '%s' must be the same as the dst root '%s'"
% (src, dst))
)

norm_dst = ntpath.normpath(dst)
norm_src = ntpath.normpath(src)
Expand All @@ -1046,7 +1049,7 @@ def copyfile(src, dst, **kwargs):
if err.errno != errno.ENOENT:
raise
else:
raise ValueError("Target %s already exists" % norm_dst)
raise ValueError(to_native("Target %s already exists" % norm_dst))

with SMBRawIO(norm_src, mode='r', desired_access=FilePipePrinterAccessMask.GENERIC_READ,
create_options=(CreateOptions.FILE_NON_DIRECTORY_FILE), share_access='r',
Expand Down
11 changes: 10 additions & 1 deletion smbclient/shutil.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright: (c) 2019, Jordan Borean (@jborean93) <jborean93@gmail.com> and other contributors
# MIT License (see LICENSE or https://opensource.org/licenses/MIT)

from __future__ import unicode_literals

from ntpath import join, basename, splitdrive
Expand All @@ -8,6 +12,11 @@

from smbclient._os import remove, rmdir, listdir, stat, makedirs, readlink, symlink, scandir, copyfile
from smbclient.path import islink, isdir

from smbprotocol._text import (
to_native,
)

from smbprotocol.exceptions import SMBOSError


Expand Down Expand Up @@ -69,7 +78,7 @@ def copy(src, dst, **kwargs):
_check_src_dst(src, dst)

if _samefile(src, dst, **kwargs):
raise ValueError("`%s` and `%s` are the same file" % (src, dst))
raise ValueError(to_native("`%s` and `%s` are the same file" % (src, dst)))

if isdir(dst, **kwargs):
dst = join(dst, basename(src))
Expand Down
9 changes: 2 additions & 7 deletions tests/test_smbclient_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,8 @@ def test_copy_raises_when_source_and_target_identical(smb_share):
with open_file("%s\\file1" % smb_share, mode='w') as fd:
fd.write(u"content")

if (sys.version_info > (3, 0)):
expected = 'are the same file'
context = pytest.raises(ValueError, match=re.escape(expected))
else:
context = pytest.raises(ValueError)

with context:
expected = 'are the same file'
with pytest.raises(ValueError, match=re.escape(expected)):
copy("%s\\file1" % smb_share, "%s\\file1" % smb_share)


Expand Down

0 comments on commit dd1b5de

Please sign in to comment.