From 08c9efc86a2d76f763d73a843a961f4b6e0be253 Mon Sep 17 00:00:00 2001 From: Robey Pointer Date: Mon, 13 Feb 2006 10:17:24 -0800 Subject: [project @ robey@lag.net-20060213181724-ba80fa329c5be7f4] not all sftp servers obey the 'all filenames are utf8' requirement, so if both ascii and utf8 codecs fail, just return the filename as a byte string --- paramiko/sftp_client.py | 13 ++++++++++--- 1 file 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): -- cgit v1.2.3