Edgewall Software

Ticket #9571: 9571-path-to-unicode-r10043.patch

File 9571-path-to-unicode-r10043.patch, 3.3 KB (added by rblank, 17 months ago)

Possible fix.

  • trac/util/text.py

    diff --git a/trac/util/text.py b/trac/util/text.py
    a b  
    7676        message = '\n%s\n%s' % (to_unicode('\n'.join(traceback_only)), message) 
    7777    return message 
    7878 
     79def path_to_unicode(path): 
     80    """Convert a filesystem path to unicode, using the filesystem encoding.""" 
     81    if isinstance(path, str): 
     82        try: 
     83            return unicode(text, sys.getfilesystemencoding()) 
     84        except UnicodeDecodeError: 
     85            return unicode(path, 'latin1') 
     86    return unicode(path) 
     87 
    7988_js_quote = {'\\': '\\\\', '"': '\\"', '\b': '\\b', '\f': '\\f', 
    8089             '\n': '\\n', '\r': '\\r', '\t': '\\t', "'": "\\'"} 
    8190for i in range(0x20) + [ord(c) for c in '&<>']: 
  • trac/wiki/admin.py

    diff --git a/trac/wiki/admin.py b/trac/wiki/admin.py
    a b  
    2424from trac.util.compat import any 
    2525from trac.util.datefmt import format_datetime, from_utimestamp, \ 
    2626                              to_utimestamp, utc 
    27 from trac.util.text import to_unicode, unicode_quote, unicode_unquote, \ 
    28                            print_table, printout 
     27from trac.util.text import path_to_unicode, print_table, printout, \ 
     28                           to_unicode, unicode_quote, unicode_unquote 
    2929from trac.util.translation import _ 
    3030 
    3131 
     
    102102        else: 
    103103            if os.path.isfile(filename): 
    104104                raise AdminCommandError(_("File '%(name)s' exists", 
    105                                           name=filename)) 
     105                                          name=path_to_unicode(filename))) 
    106106            f = open(filename, 'w') 
    107107            try: 
    108108                f.write(text.encode('utf-8')) 
     
    114114        if filename: 
    115115            if not os.path.isfile(filename): 
    116116                raise AdminCommandError(_("'%(name)s' is not a file", 
    117                                           name=filename)) 
     117                                          name=path_to_unicode(filename))) 
    118118            data = read_file(filename) 
    119119        else: 
    120120            data = sys.stdin.read() 
     
    166166                if os.path.isfile(filename): 
    167167                    if self.import_page(filename, page, create_only, replace): 
    168168                        printout(_("  %(page)s imported from %(filename)s", 
    169                                    filename=filename, page=page)) 
     169                                   filename=path_to_unicode(filename), 
     170                                   page=page)) 
    170171     
    171172    def _complete_page(self, args): 
    172173        if len(args) == 1: 
     
    241242                os.mkdir(directory) 
    242243            else: 
    243244                raise AdminCommandError(_("'%(name)s' is not a directory", 
    244                                           name=directory)) 
     245                                          name=path_to_unicode(directory))) 
    245246        db = self.env.get_db_cnx() 
    246247        cursor = db.cursor() 
    247248        for p in pages: 
     
    263264                    page = unicode_unquote(page.encode('utf-8')) 
    264265                    if self.import_page(path, page, replace=replace): 
    265266                        printout(_("  %(page)s imported from %(filename)s", 
    266                                    filename=path, page=page)) 
     267                                   filename=path_to_unicode(path), page=page)) 
    267268     
    268269    def _do_load(self, *paths): 
    269270        self._load_or_replace(paths, replace=False)