Changes between Initial Version and Version 1 of Ticket #13042, comment 21
- Timestamp:
- Jul 26, 2018, 12:04:27 PM (6 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #13042, comment 21
initial v1 18 18 {{{#!diff 19 19 diff --git a/trac/util/text.py b/trac/util/text.py 20 index 839f7a8d1.. 20c0126ed10064420 index 839f7a8d1..0d383225e 100644 21 21 --- a/trac/util/text.py 22 22 +++ b/trac/util/text.py 23 @@ -106,7 +106, 18@@ def to_unicode(text, charset=None):23 @@ -106,7 +106,22 @@ def to_unicode(text, charset=None): 24 24 except UnicodeDecodeError: 25 25 return unicode(text, 'latin1') … … 27 27 - if os.name == 'nt' and isinstance(text, (OSError, IOError)): 28 28 + if os.name == 'nt' and isinstance(text, EnvironmentError): 29 + strerror = text.strerror 29 30 + filename = text.filename 30 + if filename:31 + if strerror and filename: 31 32 + try: 32 + strerror = unicode(text.strerror, 'mbcs') 33 + filename = filename if isinstance(filename, unicode) else \ 34 + unicode(filename, 'mbcs') 33 + strerror = to_unicode(strerror, 'mbcs') 34 + filename = to_unicode(filename, 'mbcs') 35 35 + except UnicodeError: 36 36 + pass 37 37 + else: 38 + return "[Errno %s] %s '%s'" % (text.errno, strerror, 39 + filename) 38 + if isinstance(text, WindowsError): 39 + return u"[Error %s] %s: '%s'" % (text.winerror, 40 + strerror, filename) 41 + else: 42 + return u"[Errno %s] %s: '%s'" % (text.errno, strerror, 43 + filename) 40 44 # the exception might have a localized error string encoded with 41 45 # ANSI codepage if OSError and IOError on Windows … … 43 47 }}} 44 48 45 46 49 {{{ 47 50 Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32 48 51 Type "help", "copyright", "credits" or "license" for more information. 52 >>> import os 49 53 >>> from trac.util.text import to_unicode 50 54 >>> try: … … 53 57 ... print(to_unicode(e)) 54 58 ... 55 [Errno 2] No such file or directory 'C:\notfound.txt'59 [Errno 2] No such file or directory: 'C:\notfound.txt' 56 60 >>> try: 57 61 ... open(u'C:\あ.txt') … … 59 63 ... print(to_unicode(e)) 60 64 ... 61 [Errno 2] No such file or directory 'C:\あ.txt' 65 [Errno 2] No such file or directory: 'C:\あ.txt' 66 >>> try: 67 ... os.mkdir('C:\\') 68 ... except Exception as e: 69 ... print(to_unicode(e)) 70 ... 71 [Error 5] アクセスが拒否されました。: 'C:\' 72 >>> e.strerror # "Access denied" in Japanese (cp932) 73 '\x83A\x83N\x83Z\x83X\x82\xaa\x8b\x91\x94\xdb\x82\xb3\x82\xea\x82\xdc\x82\xb5\x82\xbd\x81B' 62 74 }}}