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
13 changes: 13 additions & 0 deletions unix-ffi/machine/machine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,17 @@


def unique_id():
for base in ("/etc", "/var/lib/dbus"):
try:
with open(base + "/machine-id", "rb") as source:
data = source.read(32)
if len(data) == 32:
for byte in data:
if byte not in b"0123456789abcdef":
break
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest keeping the len(data) == 32 check but removing the hex char validation loop.

(If the file exists and has at least 32 bytes, it's highly likely to be a well formed hex number.)

# unhexlify might not be available
return bytes([int(data[i : i + 2], 16) for i in range(0, len(data), 2)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest replacing len(data) with 32, since that 32 is validated above (and it'll reduce code size).

except OSError as e:
if "ENOENT" not in str(e):
raise
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest to just do pass here in the except clause, ie silently ignore any error. That'll allow it to try the DBus file if anything fails in the etc file, it'll reduce code size, and also work correctly on targets that cannot render exceptions (ie MICROPY_PY_ERRNO is disabled).

return b"upy-non-unique"
2 changes: 1 addition & 1 deletion unix-ffi/machine/manifest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
metadata(version="0.2.2")
metadata(version="0.2.3")

# Originally written by Paul Sokolovsky.

Expand Down
Loading