changeset 8081:2837b66f68bb

py3: trivially replace safe_str with safe_bytes in some places where that is what we need
author Mads Kiilerich <mads@kiilerich.com>
date Thu, 26 Dec 2019 15:20:08 +0100
parents e7dbe089e10d
children 9d1d00c72e61
files kallithea/lib/auth.py kallithea/lib/celerylib/__init__.py kallithea/lib/helpers.py kallithea/lib/middleware/permanent_repo_url.py kallithea/lib/utils.py kallithea/lib/vcs/backends/git/inmemory.py kallithea/lib/vcs/backends/hg/repository.py kallithea/lib/vcs/backends/hg/ssh.py kallithea/lib/vcs/conf/settings.py kallithea/model/db.py
diffstat 10 files changed, 26 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/auth.py	Thu Jan 02 03:15:36 2020 +0100
+++ b/kallithea/lib/auth.py	Thu Dec 26 15:20:08 2019 +0100
@@ -41,7 +41,7 @@
 from kallithea.config.routing import url
 from kallithea.lib.caching_query import FromCache
 from kallithea.lib.utils import conditional_cache, get_repo_group_slug, get_repo_slug, get_user_group_slug
-from kallithea.lib.utils2 import ascii_bytes, ascii_str, safe_str, safe_unicode
+from kallithea.lib.utils2 import ascii_bytes, ascii_str, safe_bytes, safe_unicode
 from kallithea.lib.vcs.utils.lazy import LazyProperty
 from kallithea.model.db import (
     Permission, RepoGroup, Repository, User, UserApiKeys, UserGroup, UserGroupMember, UserGroupRepoGroupToPerm, UserGroupRepoToPerm, UserGroupToPerm, UserGroupUserGroupToPerm, UserIpMap, UserToPerm)
@@ -95,7 +95,7 @@
         return hashlib.sha256(password).hexdigest()
     elif is_unix:
         import bcrypt
-        return ascii_str(bcrypt.hashpw(safe_str(password), bcrypt.gensalt(10)))
+        return ascii_str(bcrypt.hashpw(safe_bytes(password), bcrypt.gensalt(10)))
     else:
         raise Exception('Unknown or unsupported platform %s'
                         % __platform__)
@@ -115,7 +115,7 @@
     elif is_unix:
         import bcrypt
         try:
-            return bcrypt.checkpw(safe_str(password), ascii_bytes(hashed))
+            return bcrypt.checkpw(safe_bytes(password), ascii_bytes(hashed))
         except ValueError as e:
             # bcrypt will throw ValueError 'Invalid hashed_password salt' on all password errors
             log.error('error from bcrypt checking password: %s', e)
--- a/kallithea/lib/celerylib/__init__.py	Thu Jan 02 03:15:36 2020 +0100
+++ b/kallithea/lib/celerylib/__init__.py	Thu Dec 26 15:20:08 2019 +0100
@@ -35,7 +35,7 @@
 
 from kallithea import CELERY_EAGER, CELERY_ON
 from kallithea.lib.pidlock import DaemonLock, LockHeld
-from kallithea.lib.utils2 import safe_str
+from kallithea.lib.utils2 import safe_bytes
 from kallithea.model import meta
 
 
@@ -95,7 +95,7 @@
     func_name = str(func.__name__) if hasattr(func, '__name__') else str(func)
 
     lockkey = 'task_%s.lock' % \
-        md5(safe_str(func_name + '-' + '-'.join(unicode(x) for x in params))).hexdigest()
+        md5(safe_bytes(func_name + '-' + '-'.join(unicode(x) for x in params))).hexdigest()
     return lockkey
 
 
--- a/kallithea/lib/helpers.py	Thu Jan 02 03:15:36 2020 +0100
+++ b/kallithea/lib/helpers.py	Thu Dec 26 15:20:08 2019 +0100
@@ -48,7 +48,7 @@
 from kallithea.lib.pygmentsutils import get_custom_lexer
 from kallithea.lib.utils2 import MENTIONS_REGEX, AttributeDict
 from kallithea.lib.utils2 import age as _age
-from kallithea.lib.utils2 import credentials_filter, safe_int, safe_str, safe_unicode, str2bool, time_to_datetime
+from kallithea.lib.utils2 import credentials_filter, safe_bytes, safe_int, safe_unicode, str2bool, time_to_datetime
 from kallithea.lib.vcs.backends.base import BaseChangeset, EmptyChangeset
 from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError
 #==============================================================================
@@ -199,7 +199,7 @@
     :param path:
     """
 
-    return 'C-%s-%s' % (short_id(raw_id), hashlib.md5(safe_str(path)).hexdigest()[:12])
+    return 'C-%s-%s' % (short_id(raw_id), hashlib.md5(safe_bytes(path)).hexdigest()[:12])
 
 
 class _FilesBreadCrumbs(object):
@@ -938,7 +938,7 @@
     parsed_url = urlparse.urlparse(url.current(qualified=True))
     url = (c.visual.gravatar_url or User.DEFAULT_GRAVATAR_URL) \
                .replace('{email}', email_address) \
-               .replace('{md5email}', hashlib.md5(safe_str(email_address).lower()).hexdigest()) \
+               .replace('{md5email}', hashlib.md5(safe_bytes(email_address).lower()).hexdigest()) \
                .replace('{netloc}', parsed_url.netloc) \
                .replace('{scheme}', parsed_url.scheme) \
                .replace('{size}', str(size))
--- a/kallithea/lib/middleware/permanent_repo_url.py	Thu Jan 02 03:15:36 2020 +0100
+++ b/kallithea/lib/middleware/permanent_repo_url.py	Thu Dec 26 15:20:08 2019 +0100
@@ -21,7 +21,7 @@
 
 
 from kallithea.lib.utils import fix_repo_id_name
-from kallithea.lib.utils2 import safe_str, safe_unicode
+from kallithea.lib.utils2 import safe_bytes, safe_unicode
 
 
 class PermanentRepoUrl(object):
@@ -35,7 +35,7 @@
         # we also have to do the reverse operation when patching it back in
         path_info = safe_unicode(environ['PATH_INFO'])
         if path_info.startswith('/'): # it must
-            path_info = '/' + safe_str(fix_repo_id_name(path_info[1:]))
-            environ['PATH_INFO'] = path_info
+            path_info = '/' + fix_repo_id_name(path_info[1:])
+            environ['PATH_INFO'] = safe_bytes(path_info)
 
         return self.application(environ, start_response)
--- a/kallithea/lib/utils.py	Thu Jan 02 03:15:36 2020 +0100
+++ b/kallithea/lib/utils.py	Thu Dec 26 15:20:08 2019 +0100
@@ -337,7 +337,7 @@
     sa = meta.Session()
     for ui_ in sa.query(Ui).all():
         if ui_.ui_active:
-            ui_val = b'' if ui_.ui_value is None else safe_str(ui_.ui_value)
+            ui_val = b'' if ui_.ui_value is None else safe_bytes(ui_.ui_value)
             log.debug('config from db: [%s] %s=%r', ui_.ui_section,
                       ui_.ui_key, ui_val)
             baseui.setconfig(ascii_bytes(ui_.ui_section), ascii_bytes(ui_.ui_key),
--- a/kallithea/lib/vcs/backends/git/inmemory.py	Thu Jan 02 03:15:36 2020 +0100
+++ b/kallithea/lib/vcs/backends/git/inmemory.py	Thu Dec 26 15:20:08 2019 +0100
@@ -7,7 +7,7 @@
 
 from kallithea.lib.vcs.backends.base import BaseInMemoryChangeset
 from kallithea.lib.vcs.exceptions import RepositoryError
-from kallithea.lib.vcs.utils import ascii_str, safe_bytes, safe_str
+from kallithea.lib.vcs.utils import ascii_str, safe_bytes
 
 
 class GitInMemoryChangeset(BaseInMemoryChangeset):
@@ -47,7 +47,7 @@
         for node in self.added + self.changed:
             # Compute subdirs if needed
             dirpath, nodename = posixpath.split(node.path)
-            dirnames = safe_str(dirpath).split(b'/') if dirpath else []
+            dirnames = safe_bytes(dirpath).split(b'/') if dirpath else []
             parent = commit_tree
             ancestors = [('', parent)]
 
@@ -126,9 +126,9 @@
         commit = objects.Commit()
         commit.tree = commit_tree.id
         commit.parents = [p._commit.id for p in self.parents if p]
-        commit.author = commit.committer = safe_str(author)
+        commit.author = commit.committer = safe_bytes(author)
         commit.encoding = ENCODING
-        commit.message = safe_str(message)
+        commit.message = safe_bytes(message)
 
         # Compute date
         if date is None:
--- a/kallithea/lib/vcs/backends/hg/repository.py	Thu Jan 02 03:15:36 2020 +0100
+++ b/kallithea/lib/vcs/backends/hg/repository.py	Thu Dec 26 15:20:08 2019 +0100
@@ -347,7 +347,7 @@
 
         try:
             if src_url:
-                url = safe_str(self._get_url(src_url))
+                url = safe_bytes(self._get_url(src_url))
                 opts = {}
                 if not update_after_clone:
                     opts.update({'noupdate': True})
@@ -521,7 +521,7 @@
         # filter branches
         filter_ = []
         if branch_name:
-            filter_.append(b'branch("%s")' % safe_str(branch_name))
+            filter_.append(b'branch("%s")' % safe_bytes(branch_name))
         if start_date:
             filter_.append(b'date(">%s")' % start_date)
         if end_date:
--- a/kallithea/lib/vcs/backends/hg/ssh.py	Thu Jan 02 03:15:36 2020 +0100
+++ b/kallithea/lib/vcs/backends/hg/ssh.py	Thu Dec 26 15:20:08 2019 +0100
@@ -19,7 +19,7 @@
 
 from kallithea.lib.utils import make_ui
 from kallithea.lib.vcs.backends.ssh import BaseSshHandler
-from kallithea.lib.vcs.utils import safe_str, safe_unicode
+from kallithea.lib.vcs.utils import safe_bytes, safe_unicode
 
 
 log = logging.getLogger(__name__)
@@ -61,6 +61,6 @@
             baseui.setconfig(b'hooks', b'pretxnopen._ssh_reject', b'python:kallithea.lib.hooks.rejectpush')
             baseui.setconfig(b'hooks', b'prepushkey._ssh_reject', b'python:kallithea.lib.hooks.rejectpush')
 
-        repo = hg.repository(baseui, safe_str(self.db_repo.repo_full_path))
+        repo = hg.repository(baseui, safe_bytes(self.db_repo.repo_full_path))
         log.debug("Starting Mercurial sshserver for %s", self.db_repo.repo_full_path)
         sshserver(baseui, repo).serve_forever()
--- a/kallithea/lib/vcs/conf/settings.py	Thu Jan 02 03:15:36 2020 +0100
+++ b/kallithea/lib/vcs/conf/settings.py	Thu Dec 26 15:20:08 2019 +0100
@@ -18,7 +18,7 @@
 if os.path.isdir(VCSRC_PATH):
     VCSRC_PATH = os.path.join(VCSRC_PATH, '__init__.py')
 
-# list of default encoding used in safe_unicode/safe_str methods
+# list of default encoding used in safe_unicode/safe_bytes methods
 DEFAULT_ENCODINGS = aslist('utf-8')
 
 # path to git executable run by run_git_command function
--- a/kallithea/model/db.py	Thu Jan 02 03:15:36 2020 +0100
+++ b/kallithea/model/db.py	Thu Dec 26 15:20:08 2019 +0100
@@ -48,7 +48,8 @@
 from kallithea.lib.caching_query import FromCache
 from kallithea.lib.compat import json
 from kallithea.lib.exceptions import DefaultUserException
-from kallithea.lib.utils2 import Optional, aslist, get_changeset_safe, get_clone_url, remove_prefix, safe_int, safe_str, safe_unicode, str2bool, urlreadable
+from kallithea.lib.utils2 import (
+    Optional, aslist, get_changeset_safe, get_clone_url, remove_prefix, safe_bytes, safe_int, safe_str, safe_unicode, str2bool, urlreadable)
 from kallithea.lib.vcs import get_backend
 from kallithea.lib.vcs.backends.base import EmptyChangeset
 from kallithea.lib.vcs.utils.helpers import get_scm
@@ -63,7 +64,8 @@
 # BASE CLASSES
 #==============================================================================
 
-_hash_key = lambda k: hashlib.md5(safe_str(k)).hexdigest()
+def _hash_key(k):
+    return hashlib.md5(safe_bytes(k)).hexdigest()
 
 
 class BaseDbModel(object):
@@ -183,7 +185,7 @@
     )
 
     SETTINGS_TYPES = {
-        'str': safe_str,
+        'str': safe_bytes,
         'int': safe_int,
         'unicode': safe_unicode,
         'bool': str2bool,