Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion boltons/iterutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,13 @@ def __init__(self, size=24):
def reseed(self):
import socket
self.pid = os.getpid()
hostname = socket.gethostname()
# gethostname() may return bytes on some platforms/configs; decode so
# the str.join() below does not raise a TypeError on a mixed sequence.
if isinstance(hostname, bytes):
hostname = hostname.decode('utf8', 'replace')
self.salt = '-'.join([str(self.pid),
socket.gethostname() or '<nohostname>',
hostname or '<nohostname>',
str(time.time()),
os.urandom(6).hex()])
return
Expand Down
15 changes: 15 additions & 0 deletions tests/test_iterutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,21 @@ def test_guiderator():
assert len(next(guid_iter)) == 26


def test_guiderator_bytes_hostname(monkeypatch):
# socket.gethostname() may return bytes on some platforms; reseed() must
# not raise a TypeError when building the salt (issue #295).
import socket
import string
from boltons.iterutils import GUIDerator

monkeypatch.setattr(socket, 'gethostname', lambda: b'myhost')

guid_iter = GUIDerator()
guid = next(guid_iter)
assert guid
assert all([c in string.hexdigits for c in guid])


def test_seqguiderator():
import string
from boltons.iterutils import SequentialGUIDerator as GUIDerator
Expand Down