Repository navigation
gh-139122: Restore generic attribute initialization of C UUIDs - #160203
Conversation
|
Ok that's weird: I deleted the file from your branch (and it shows as deleted) but GH is still showing it here. I will wait a bit (or maybe you should force push again). Sorry for the mess though (otherwise LGTM!) |
|
Close and re-open seems to have fixed the PR status being borked. |
vstinner
left a comment
There was a problem hiding this comment.
LGTM.
The tests cover all tests that I expected to be tested. So setting u.int or u.safe directly raises TypeError, but using object.__setattr__(), they can be modified. The cached hash value is reset, and setting .int attribute is atomic (leave the object unchanged on error).
I just have some doubts about critical section used to set the attributes (setter functions), but not in getter functions (Uuid_get_int, Uuid_get_is_safe). But that can be adjusted/fixed later if there is a real bug. I'm not an expert, so I'm not sure.
Using a critical section in getter functions can hurt performance, and I expect object.__setattr__() to be only used when creating a new UUID object, before it can be seen/used by other threads. Also, uuid documentation says nothing about thread safety currently. My doubts
|
Sorry, @1st1 and @vstinner, I could not cleanly backport this to Please backport manually with cherry_picker, see the devguide for more information. |
|
Ooopsie, the 3.15 branch is not affected (even if it has a |
|
I merged the change to get the fix for Pydantic in next Python 3.16 release (in two weeks). |
Pydantic constructs UUIDs by calling the type's
tp_newand settingintandis_safethroughPyObject_GenericSetAttr. The C UUID implementation made these descriptors read-only, so that construction path fails after #139123.Add setters for both descriptors, restoring construction through
object.__setattr__andPyObject_GenericSetAttr. Ordinary assignment still raisesTypeError, matching the Python UUID implementation. The integer setter validates the value and converts it into a temporary 16-byte, big-endian buffer before updating the UUID, so failed conversions leave the object unchanged. Successful replacement invalidates the cached hash.Tests cover construction without
__init__, subclasses, 128-bit boundary values, integer subclasses, hash invalidation, and rejected assignments preserving the original value and hash.Validation: the full
test_uuidsuite passed (165 tests, 22 skipped), including-R 3:3reference leak checks. A directPyObject_GenericSetAttrsmoke test reproduced Pydantic's construction sequence successfully. Ruff andgit diff --checkpassed.Refs #139122.