summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--paramiko/sftp_client.py13
1 files changed, 10 insertions, 3 deletions
diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py
index 19155629..ede7e58a 100644
--- a/paramiko/sftp_client.py
+++ b/paramiko/sftp_client.py
@@ -31,11 +31,18 @@ from paramiko.sftp_file import SFTPFile
def _to_unicode(s):
- "if a str is not ascii, decode its utf8 into unicode"
+ """
+ decode a string as ascii or utf8 if possible (as required by the sftp
+ protocol). if neither works, just return a byte string because the server
+ probably doesn't know the filename's encoding.
+ """
try:
return s.encode('ascii')
- except:
- return s.decode('utf-8')
+ except UnicodeError:
+ try:
+ return s.decode('utf-8')
+ except UnicodeError:
+ return s
class SFTPClient (BaseSFTP):