Skip to content

Commit

Permalink
Fix hashColumns.encode() function so that it properly returns bytes o…
Browse files Browse the repository at this point in the history
…n Python 2 and 3.
  • Loading branch information
rodrigc committed Jan 17, 2017
1 parent d355ae3 commit 7a61c7a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
15 changes: 8 additions & 7 deletions master/buildbot/db/base.py
Expand Up @@ -15,6 +15,7 @@

from __future__ import absolute_import
from __future__ import print_function
from future.utils import text_type

import hashlib
import itertools
Expand Down Expand Up @@ -92,14 +93,14 @@ def thd(conn, no_recurse=False):

def hashColumns(self, *args):
def encode(x):
try:
return x.encode('utf8')
except AttributeError:
if x is None:
return '\xf5'
return str(x)
if x is None:
return b'\xf5'
elif isinstance(x, text_type):
return x.encode('utf-8')
else:
return str(x).encode('utf-8')

return hashlib.sha1('\0'.join(map(encode, args))).hexdigest()
return hashlib.sha1(b'\0'.join(map(encode, args))).hexdigest()

def doBatch(self, batch, batch_n=500):
iterator = iter(batch)
Expand Down
15 changes: 8 additions & 7 deletions master/buildbot/test/fake/fakedb.py
Expand Up @@ -131,13 +131,14 @@ def nextId(self):
def hashColumns(self, *args):
# copied from master/buildbot/db/base.py
def encode(x):
try:
return x.encode('utf8')
except AttributeError:
if x is None:
return '\xf5'
return str(x)
return hashlib.sha1('\0'.join(map(encode, args))).hexdigest()
if x is None:
return b'\xf5'
elif isinstance(x, text_type):
return x.encode('utf-8')
else:
return str(x).encode('utf-8')

return hashlib.sha1(b'\0'.join(map(encode, args))).hexdigest()

@defer.inlineCallbacks
def checkForeignKeys(self, db, t):
Expand Down

0 comments on commit 7a61c7a

Please sign in to comment.