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
3 changes: 3 additions & 0 deletions python-stdlib/uuid/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
metadata(version="0.1.0")

module("uuid.py")
30 changes: 30 additions & 0 deletions python-stdlib/uuid/test_uuid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest
import uuid


class TestUUID(unittest.TestCase):
def test_unique(self):
n = 10
us = set(uuid.uuid4().bytes for _ in range(n))
self.assertEqual(len(us), n)

def test_len(self):
u = uuid.uuid4()
self.assertEqual(len(u.bytes), 16)
self.assertEqual(len(u.hex), 32)
self.assertEqual(len(str(u)), 36)

def test_repr(self):
u = repr(uuid.uuid4())
self.assertEqual(len(u), 44)
self.assertTrue(u.startswith("UUID('"))
self.assertTrue(u.endswith("')"))

def test_constructor(self):
u1 = uuid.uuid4()
u2 = uuid.UUID(bytes.fromhex(u1.hex))
self.assertEqual(u1.hex, u2.hex)


if __name__ == "__main__":
unittest.main()
27 changes: 27 additions & 0 deletions python-stdlib/uuid/uuid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os


class UUID:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure if other methods/properties would make porting CPython code easier, it's hard to know which are in common use. version and variant seem like they would be high on the list - particularly if we decide to add support for UUIDv7 in the future - but it's probably best to hold off for now and keep it as minimal as possible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I also don't really know what users are going to need...

def __init__(self, bytes):
if len(bytes) != 16:
raise ValueError("bytes arg must be 16 bytes long")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I tried to test if cpython gave the save error here but it gave a much messier exception. Possibly not worth worrying about, this should be user friendly enough.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, indeed CPython does give a confusing error.

self.bytes = bytes

@property
def hex(self):
return self.bytes.hex()

def __str__(self):
h = self.hex
return "-".join((h[0:8], h[8:12], h[12:16], h[16:20], h[20:32]))

def __repr__(self):
return "UUID('{}')".format(self)


def uuid4():
"""Generates a random UUID compliant to RFC 4122 pg.14"""
random = bytearray(os.urandom(16))
random[6] = (random[6] & 0x0F) | 0x40
random[8] = (random[8] & 0x3F) | 0x80
return UUID(bytes(random))
1 change: 1 addition & 0 deletions tools/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function ci_package_tests_run {
python-stdlib/time \
python-stdlib/unittest/tests \
python-stdlib/unittest-discover/tests \
python-stdlib/uuid \
; do
(cd $path && "${MICROPYTHON}" -m unittest)
if [ $? -ne 0 ]; then false; return; fi
Expand Down
Loading