Skip to content

Handle bytes from socket.gethostname() in GUIDerator.reseed#413

Open
uttam12331 wants to merge 1 commit into
mahmoud:masterfrom
uttam12331:fix/295-guiderator-bytes-hostname
Open

Handle bytes from socket.gethostname() in GUIDerator.reseed#413
uttam12331 wants to merge 1 commit into
mahmoud:masterfrom
uttam12331:fix/295-guiderator-bytes-hostname

Conversation

@uttam12331

Copy link
Copy Markdown

Summary

Closes #295.

On some platforms/configurations socket.gethostname() returns bytes rather than str. GUIDerator.reseed() joins it into the salt with str.join():

self.salt = '-'.join([str(self.pid),
                      socket.gethostname() or '<nohostname>',
                      ...])

which raises TypeError: sequence item 1: expected str instance, bytes found. Because a module-level GUIDerator() is created at import time, this can break importing boltons.iterutils entirely on affected systems.

Fix

Decode the hostname to str when it comes back as bytes:

hostname = socket.gethostname()
if isinstance(hostname, bytes):
    hostname = hostname.decode('utf8', 'replace')

Tests

Added test_guiderator_bytes_hostname (monkeypatches socket.gethostname to return b'myhost') asserting a valid hex GUID is produced without raising.

Verified locally: pytest tests/test_iterutils.py45 passed.

On some platforms/configurations socket.gethostname() returns bytes rather
than str. GUIDerator.reseed() joins it into the salt with str.join(), which
then raises 'TypeError: sequence item 1: expected str instance, bytes found'
(and, because a module-level GUIDerator is created on import, this breaks
importing boltons.iterutils entirely).

Decode the hostname to str when it comes back as bytes.

Closes mahmoud#295
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Default value for socket.gethostname() should be str, not bytes

1 participant