changeset 8032:287b5f1cb40a

py3: only use safe_str for string conversion - not for arbitrary __str__ invocation Usually, we actually just want unicode string, and it is better to just unicode().
author Mads Kiilerich <mads@kiilerich.com>
date Mon, 16 Dec 2019 00:02:34 +0100
parents 84847aa20d77
children 1e8b300b0540
files kallithea/controllers/api/__init__.py kallithea/controllers/changelog.py kallithea/controllers/files.py kallithea/controllers/summary.py kallithea/lib/base.py kallithea/lib/vcs/utils/__init__.py
diffstat 6 files changed, 12 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/api/__init__.py	Sun Nov 24 23:23:42 2019 +0100
+++ b/kallithea/controllers/api/__init__.py	Mon Dec 16 00:02:34 2019 +0100
@@ -226,12 +226,12 @@
             if isinstance(raw_response, HTTPError):
                 self._error = str(raw_response)
         except JSONRPCError as e:
-            self._error = safe_str(e)
+            self._error = unicode(e)
         except Exception as e:
             log.error('Encountered unhandled exception: %s',
                       traceback.format_exc(),)
             json_exc = JSONRPCError('Internal server error')
-            self._error = safe_str(json_exc)
+            self._error = unicode(json_exc)
 
         if self._error is not None:
             raw_response = None
--- a/kallithea/controllers/changelog.py	Sun Nov 24 23:23:42 2019 +0100
+++ b/kallithea/controllers/changelog.py	Mon Dec 16 00:02:34 2019 +0100
@@ -67,7 +67,7 @@
             h.flash(_('There are no changesets yet'), category='error')
         except RepositoryError as e:
             log.error(traceback.format_exc())
-            h.flash(safe_str(e), category='error')
+            h.flash(unicode(e), category='error')
         raise HTTPBadRequest()
 
     @LoginRequired(allow_default_user=True)
@@ -111,7 +111,7 @@
                         cs = self.__get_cs(revision, repo_name)
                         collection = cs.get_file_history(f_path)
                     except RepositoryError as e:
-                        h.flash(safe_str(e), category='warning')
+                        h.flash(unicode(e), category='warning')
                         raise HTTPFound(location=h.url('changelog_home', repo_name=repo_name))
             else:
                 collection = c.db_repo_scm_instance.get_changesets(start=0, end=revision,
@@ -125,11 +125,11 @@
             c.cs_comments = c.db_repo.get_comments(page_revisions)
             c.cs_statuses = c.db_repo.statuses(page_revisions)
         except EmptyRepositoryError as e:
-            h.flash(safe_str(e), category='warning')
+            h.flash(unicode(e), category='warning')
             raise HTTPFound(location=url('summary_home', repo_name=c.repo_name))
         except (RepositoryError, ChangesetDoesNotExistError, Exception) as e:
             log.error(traceback.format_exc())
-            h.flash(safe_str(e), category='error')
+            h.flash(unicode(e), category='error')
             raise HTTPFound(location=url('changelog_home', repo_name=c.repo_name))
 
         c.branch_name = branch_name
--- a/kallithea/controllers/files.py	Sun Nov 24 23:23:42 2019 +0100
+++ b/kallithea/controllers/files.py	Mon Dec 16 00:02:34 2019 +0100
@@ -90,7 +90,7 @@
             h.flash(msg, category='error')
             raise HTTPNotFound()
         except RepositoryError as e:
-            h.flash(safe_str(e), category='error')
+            h.flash(unicode(e), category='error')
             raise HTTPNotFound()
 
     def __get_filenode(self, cs, path):
@@ -110,7 +110,7 @@
             h.flash(msg, category='error')
             raise HTTPNotFound()
         except RepositoryError as e:
-            h.flash(safe_str(e), category='error')
+            h.flash(unicode(e), category='error')
             raise HTTPNotFound()
 
         return file_node
@@ -175,7 +175,7 @@
             else:
                 c.authors = c.file_history = []
         except RepositoryError as e:
-            h.flash(safe_str(e), category='error')
+            h.flash(unicode(e), category='error')
             raise HTTPNotFound()
 
         if request.environ.get('HTTP_X_PARTIAL_XHR'):
--- a/kallithea/controllers/summary.py	Sun Nov 24 23:23:42 2019 +0100
+++ b/kallithea/controllers/summary.py	Mon Dec 16 00:02:34 2019 +0100
@@ -108,7 +108,7 @@
         try:
             collection = c.db_repo_scm_instance.get_changesets(reverse=True)
         except EmptyRepositoryError as e:
-            h.flash(safe_str(e), category='warning')
+            h.flash(unicode(e), category='warning')
             collection = []
         c.cs_pagination = Page(collection, page=p, items_per_page=size)
         page_revisions = [x.raw_id for x in list(c.cs_pagination)]
--- a/kallithea/lib/base.py	Sun Nov 24 23:23:42 2019 +0100
+++ b/kallithea/lib/base.py	Mon Dec 16 00:02:34 2019 +0100
@@ -603,7 +603,7 @@
             raise webob.exc.HTTPNotFound()
         except RepositoryError as e:
             log.error(traceback.format_exc())
-            h.flash(safe_str(e), category='error')
+            h.flash(unicode(e), category='error')
             raise webob.exc.HTTPBadRequest()
 
 
--- a/kallithea/lib/vcs/utils/__init__.py	Sun Nov 24 23:23:42 2019 +0100
+++ b/kallithea/lib/vcs/utils/__init__.py	Mon Dec 16 00:02:34 2019 +0100
@@ -106,8 +106,7 @@
     if isinstance(s, str):
         return s
 
-    if not isinstance(s, unicode):
-        return str(s)
+    assert isinstance(s, unicode), s  # don't use safe_str to coerce non-strings
 
     from kallithea.lib.vcs.conf import settings
     for enc in settings.DEFAULT_ENCODINGS: