Skip to content

gh-142518: Annotate PyDict_* C APIs for thread safety#145875

Open
lysnikolaou wants to merge 4 commits intopython:mainfrom
lysnikolaou:annotate-pydict
Open

gh-142518: Annotate PyDict_* C APIs for thread safety#145875
lysnikolaou wants to merge 4 commits intopython:mainfrom
lysnikolaou:annotate-pydict

Conversation

@lysnikolaou
Copy link
Copy Markdown
Member

@lysnikolaou lysnikolaou commented Mar 12, 2026

@kumaraditya303 kumaraditya303 requested a review from encukou April 2, 2026 15:40

The operation is atomic in the :term:`free-threaded build`, if *key*
is a builtin type (e.g. :class:`str`, :class:`int`, :class:`float`) or any
other object which does not define :meth:`~object.__hash__` and :meth:`~object.__eq__` methods.
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.

Don't most objects have __hash__ and __eq__?

What's the real requirement here? If I define a built-in type, what property do its tp_hash/tp_richcompare need to have to keep this atomic?

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.

Don't most objects have hash and eq?

They have but it is inherited from object, here the docs mean user-defined methods which override it.

What's the real requirement here? If I define a built-in type, what property do its tp_hash/tp_richcompare need to have to keep this atomic?

The real requirements are the function should not call back into Python or the C-API and not release the thread state. All of this is implementation defined and subject to change hence it is not documented.

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.

The way this is written guarantees that all built-in types have this property.
But, tuple or frozendict don't, and for some others I'd consider it an implementation detail.

How do I tell whether we guarantee that a given type doesn't release thread state in __eq__/__hash__? It's easy for str/int/float, but there's a lot of other built-in types.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants