Skip to content

gh-139122: Restore generic attribute initialization of C UUIDs - #160203

Merged
vstinner merged 2 commits into
python:mainfrom
1st1:writeable_int
Dec 3, 2026
Merged

vstinner merged 2 commits into
python:mainfrom
1st1:writeable_int

Conversation

@1st1

@1st1 1st1 commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Pydantic constructs UUIDs by calling the type's tp_new and setting int and is_safe through PyObject_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__ and PyObject_GenericSetAttr. Ordinary assignment still raises TypeError, 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_uuid suite passed (165 tests, 22 skipped), including -R 3:3 reference leak checks. A direct PyObject_GenericSetAttr smoke test reproduced Pydantic's construction sequence successfully. Ruff and git diff --check passed.

Refs #139122.

@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 22, 2026
@1st1
1st1 requested a review from picnixz October 22, 2026 05:23
@picnixz

picnixz commented Oct 22, 2026

Copy link
Copy Markdown
Member

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!)

@emmatyping emmatyping closed this Oct 30, 2026
@emmatyping emmatyping reopened this Oct 30, 2026
@emmatyping

Copy link
Copy Markdown
Member

Close and re-open seems to have fixed the PR status being borked.

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

@vstinner
vstinner merged commit 828c153 into python:main Dec 3, 2026
66 of 76 checks passed
@vstinner vstinner added the needs backport to 3.15 bugs and security fixes label Dec 3, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @1st1 for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @1st1 and @vstinner, I could not cleanly backport this to 3.15 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker 828c15392fdc0a80574cd6948759cb404ab9e636 3.15

@vstinner vstinner removed the needs backport to 3.15 bugs and security fixes label Dec 3, 2026
@vstinner

vstinner commented Dec 3, 2026

Copy link
Copy Markdown
Member

Ooopsie, the 3.15 branch is not affected (even if it has a Modules/_uuidmodule.c file :-)).

@vstinner

vstinner commented Dec 3, 2026

Copy link
Copy Markdown
Member

I merged the change to get the fix for Pydantic in next Python 3.16 release (in two weeks).

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

Labels

skip news type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants