# HG changeset patch # User Mads Kiilerich # Date 2019-12-16 00:02:34 # Node ID 287b5f1cb40ae3641db5af8a03c911cb9ed6558a # Parent 84847aa20d777847c714d20d3d390bb7e97fa9d8 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(). diff --git a/kallithea/controllers/api/__init__.py b/kallithea/controllers/api/__init__.py --- a/kallithea/controllers/api/__init__.py +++ b/kallithea/controllers/api/__init__.py @@ -226,12 +226,12 @@ class JSONRPCController(TGController): 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 diff --git a/kallithea/controllers/changelog.py b/kallithea/controllers/changelog.py --- a/kallithea/controllers/changelog.py +++ b/kallithea/controllers/changelog.py @@ -67,7 +67,7 @@ class ChangelogController(BaseRepoContro 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 @@ class ChangelogController(BaseRepoContro 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 @@ class ChangelogController(BaseRepoContro 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 diff --git a/kallithea/controllers/files.py b/kallithea/controllers/files.py --- a/kallithea/controllers/files.py +++ b/kallithea/controllers/files.py @@ -90,7 +90,7 @@ class FilesController(BaseRepoController 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 @@ class FilesController(BaseRepoController 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 @@ class FilesController(BaseRepoController 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'): diff --git a/kallithea/controllers/summary.py b/kallithea/controllers/summary.py --- a/kallithea/controllers/summary.py +++ b/kallithea/controllers/summary.py @@ -108,7 +108,7 @@ class SummaryController(BaseRepoControll 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)] diff --git a/kallithea/lib/base.py b/kallithea/lib/base.py --- a/kallithea/lib/base.py +++ b/kallithea/lib/base.py @@ -603,7 +603,7 @@ class BaseRepoController(BaseController) 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() diff --git a/kallithea/lib/vcs/utils/__init__.py b/kallithea/lib/vcs/utils/__init__.py --- a/kallithea/lib/vcs/utils/__init__.py +++ b/kallithea/lib/vcs/utils/__init__.py @@ -106,8 +106,7 @@ def safe_str(s): 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: