Edgewall Software

Changes between Initial Version and Version 1 of Ticket #13042, comment 21


Ignore:
Timestamp:
Jul 26, 2018, 12:04:27 PM (6 years ago)
Author:
Jun Omae

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #13042, comment 21

    initial v1  
    1818{{{#!diff
    1919diff --git a/trac/util/text.py b/trac/util/text.py
    20 index 839f7a8d1..20c0126ed 100644
     20index 839f7a8d1..0d383225e 100644
    2121--- a/trac/util/text.py
    2222+++ 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):
    2424         except UnicodeDecodeError:
    2525             return unicode(text, 'latin1')
     
    2727-        if os.name == 'nt' and isinstance(text, (OSError, IOError)):
    2828+        if os.name == 'nt' and isinstance(text, EnvironmentError):
     29+            strerror = text.strerror
    2930+            filename = text.filename
    30 +            if filename:
     31+            if strerror and filename:
    3132+                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')
    3535+                except UnicodeError:
    3636+                    pass
    3737+                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)
    4044             # the exception might have a localized error string encoded with
    4145             # ANSI codepage if OSError and IOError on Windows
     
    4347}}}
    4448
    45 
    4649{{{
    4750Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
    4851Type "help", "copyright", "credits" or "license" for more information.
     52>>> import os
    4953>>> from trac.util.text import to_unicode
    5054>>> try:
     
    5357...   print(to_unicode(e))
    5458...
    55 [Errno 2] No such file or directory 'C:\notfound.txt'
     59[Errno 2] No such file or directory: 'C:\notfound.txt'
    5660>>> try:
    5761...   open(u'C:\あ.txt')
     
    5963...   print(to_unicode(e))
    6064...
    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'
    6274}}}