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
28 changes: 28 additions & 0 deletions Lib/test/test_uuid.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,34 @@ def __str__(self):

self.assertEqual(repr(CustomUUID(int=1)), "CustomUUID('custom uuid')")

def test_hash_subclass_and_reinit(self):
class CustomUUID(self.uuid.UUID):
pass

with support.swap_item(sys.modules, 'uuid', self.uuid):
for value in (0, 1, 0x12345678123456781234567812345678):
with self.subTest(value=value):
u = self.uuid.UUID(int=value)
expected = hash(u)

s = CustomUUID(int=value)
self.assertEqual(hash(s), expected)
self.assertIn(u, {s})

s = CustomUUID.__new__(CustomUUID)
s.__setstate__({'int': value})
self.assertEqual(hash(s), expected)

u = self.uuid.UUID(int=value + 1)
hash(u)
u.__init__(int=value)
self.assertEqual(hash(u), expected)

u = self.uuid.UUID(int=value + 1)
hash(u)
u.__setstate__({'int': value})
self.assertEqual(hash(u), expected)

def test_exceptions(self):
badvalue = lambda f: self.assertRaises(ValueError, f)
badtype = lambda f: self.assertRaises(TypeError, f)
Expand Down
2 changes: 2 additions & 0 deletions Modules/_uuidmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ _uuid_UUID___init___impl(uuidobject *self, PyObject *hex, PyObject *bytes,
self->bytes[6] |= (uint8_t)(version_num << 4);
}

self->cached_hash = -1;
Py_XSETREF(self->is_safe, Py_XNewRef(is_safe));

return 0;
Expand Down Expand Up @@ -1655,6 +1656,7 @@ _uuid_UUID___setstate___impl(uuidobject *self, PyObject *state)
if (result < 0) {
return NULL;
}
self->cached_hash = -1;

int has_is_safe = PySequence_Contains(state, &_Py_ID(is_safe));
if (has_is_safe < 0) {
Expand Down
Loading