Commits

Anonymous committed 6706e63

0.12.3dev: Convert paths to unicode before integrating them into error messages on the console.

Closes #9571.

Comments (0)

Files changed (4)

trac/attachment.py

 from trac.util import get_reporter_id, create_unique_file
 from trac.util.datefmt import format_datetime, from_utimestamp, \
                               to_datetime, to_utimestamp, utc
-from trac.util.text import exception_to_unicode, pretty_size, print_table, \
-                           unicode_quote, unicode_unquote
+from trac.util.text import exception_to_unicode, path_to_unicode, \
+                           pretty_size, print_table, unicode_quote, \
+                           unicode_unquote
 from trac.util.translation import _, tag_
 from trac.web import HTTPBadRequest, IRequestHandler
 from trac.web.chrome import add_link, add_stylesheet, add_ctxtnav, \
                 destination = os.path.join(destination, name)
             if os.path.isfile(destination):
                 raise AdminCommandError(_("File '%(name)s' exists",
-                                          name=destination))
+                                          name=path_to_unicode(destination)))
         input = attachment.open()
         try:
             output = (destination is None) and sys.stdout \
 from trac.util import copytree, create_file, get_pkginfo, makedirs
 from trac.util.compat import any
 from trac.util.concurrency import threading
-from trac.util.text import exception_to_unicode, printerr, printout
+from trac.util.text import exception_to_unicode, path_to_unicode, printerr, \
+                           printout
 from trac.util.translation import _, N_
 from trac.versioncontrol import RepositoryManager
 from trac.web.href import Href
     def _do_hotcopy(self, dest):
         if os.path.exists(dest):
             raise TracError(_("hotcopy can't overwrite existing '%(dest)s'",
-                              dest=dest))
+                              dest=path_to_unicode(dest)))
         import shutil
 
         # Bogus statement to lock the database while copying files
 
         try:
             printout(_('Hotcopying %(src)s to %(dst)s ...', 
-                       src=self.env.path, dst=dest))
+                       src=path_to_unicode(self.env.path),
+                       dst=path_to_unicode(dest)))
             db_str = self.env.config.get('trac', 'database')
             prefix, db_path = db_str.split(':', 1)
             if prefix == 'sqlite':
                     if src in err:
                         printerr('  %s' % err)
                     else:
-                        printerr("  %s: '%s'" % (err, src))
+                        printerr("  %s: '%s'" % (err, path_to_unicode(src)))
         finally:
             # Unlock database
             cnx.rollback()
         printout(_("Upgrade done.\n\n"
                    "You may want to upgrade the Trac documentation now by "
                    "running:\n\n  trac-admin %(path)s wiki upgrade",
-                   path=self.env.path))
+                   path=path_to_unicode(self.env.path)))

trac/util/text.py

     return message
 
 
+def path_to_unicode(path):
+    """Convert a filesystem path to unicode, using the filesystem encoding."""
+    if isinstance(path, str):
+        try:
+            return unicode(path, sys.getfilesystemencoding())
+        except UnicodeDecodeError:
+            return unicode(path, 'latin1')
+    return unicode(path)
+
+
 _js_quote = {'\\': '\\\\', '"': '\\"', '\b': '\\b', '\f': '\\f',
              '\n': '\\n', '\r': '\\r', '\t': '\\t', "'": "\\'"}
 for i in range(0x20) + [ord(c) for c in '&<>']:

trac/wiki/admin.py

 from trac.util.compat import any
 from trac.util.datefmt import format_datetime, from_utimestamp, \
                               to_utimestamp, utc
-from trac.util.text import to_unicode, unicode_quote, unicode_unquote, \
-                           print_table, printout
+from trac.util.text import path_to_unicode, print_table, printout, \
+                           to_unicode, unicode_quote, unicode_unquote
 from trac.util.translation import _
 
 
         else:
             if os.path.isfile(filename):
                 raise AdminCommandError(_("File '%(name)s' exists",
-                                          name=filename))
+                                          name=path_to_unicode(filename)))
             f = open(filename, 'w')
             try:
                 f.write(text.encode('utf-8'))
         if filename:
             if not os.path.isfile(filename):
                 raise AdminCommandError(_("'%(name)s' is not a file",
-                                          name=filename))
+                                          name=path_to_unicode(filename)))
             data = read_file(filename)
         else:
             data = sys.stdin.read()
                 if os.path.isfile(filename):
                     if self.import_page(filename, page, create_only, replace):
                         printout(_("  %(page)s imported from %(filename)s",
-                                   filename=filename, page=page))
+                                   filename=path_to_unicode(filename),
+                                   page=page))
     
     def _complete_page(self, args):
         if len(args) == 1:
                 os.mkdir(directory)
             else:
                 raise AdminCommandError(_("'%(name)s' is not a directory",
-                                          name=directory))
+                                          name=path_to_unicode(directory)))
         db = self.env.get_db_cnx()
         cursor = db.cursor()
         for p in pages:
                     page = unicode_unquote(page.encode('utf-8'))
                     if self.import_page(path, page, replace=replace):
                         printout(_("  %(page)s imported from %(filename)s",
-                                   filename=path, page=page))
+                                   filename=path_to_unicode(path), page=page))
     
     def _do_load(self, *paths):
         self._load_or_replace(paths, replace=False)