Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Feb 12, 2026

Add TYPE_FROZENDICT to the marshal module.

Add C API functions:

  • PyAnyDict_Check()
  • PyAnyDict_CheckExact()
  • PyFrozenDict_Check()
  • PyFrozenDict_CheckExact()
  • PyFrozenDict_New()

Add PyFrozenDict_Type C type.


📚 Documentation preview 📚: https://cpython-previews--144757.org.readthedocs.build/

Add TYPE_FROZENDICT to the marshal module.

Add C API functions:

* PyAnyDict_Check()
* PyAnyDict_CheckExact()
* PyFrozenDict_Check()
* PyFrozenDict_CheckExact()
* PyFrozenDict_New()

Add PyFrozenDict_Type C type.
@vstinner
Copy link
Member Author

cc @corona10

Fix also indentation.
@corona10 corona10 self-requested a review February 13, 2026 10:07
Comment on lines +89 to +100
.. _whatsnew315-pep814:

:pep:`814`: Add frozendict built-in type
----------------------------------------

A new public immutable type :class:`frozendict` is added to the :mod:`builtins`
module. It is not a ``dict`` subclass but inherits directly from ``object``.

A ``frozendict`` can be hashed with ``hash(frozendict)`` if all keys and values
can be hashed.

.. seealso:: :pep:`814` for the full specification and rationale.
Copy link
Member

Choose a reason for hiding this comment

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

I think let's list this directly after lazy imports.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean that I should move the PEP 814 section after the PEP 810 section? (I'm not sure if I understood your comment correctly.)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's right, PEP 810 first and then 814.

Comment on lines +132 to +135
self.assertTrue(d.get(list(self.other.keys())[0]) is None)
self.assertEqual(d.get(list(self.other.keys())[0], 3), 3)
d = self.reference
self.assertTrue(d.get(list(self.other.keys())[0]) is None)
Copy link
Member

@hugovk hugovk Feb 13, 2026

Choose a reason for hiding this comment

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

Suggested change
self.assertTrue(d.get(list(self.other.keys())[0]) is None)
self.assertEqual(d.get(list(self.other.keys())[0], 3), 3)
d = self.reference
self.assertTrue(d.get(list(self.other.keys())[0]) is None)
self.assertIsNone(d.get(list(self.other.keys())[0]))
self.assertEqual(d.get(list(self.other.keys())[0], 3), 3)
d = self.reference
self.assertIsNone(d.get(list(self.other.keys())[0]))

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
}

if (PyDict_CheckExact(d)) {
if (PyAnyDict_CheckExact(d)) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe same opinion with @kumaraditya303 we can avoid a lot of critical section if dict is frozen dict.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree, but I would prefer to make such "optimization" change in a separated PR.

dict_length(PyObject *self)
{
return FT_ATOMIC_LOAD_SSIZE_RELAXED(((PyDictObject *)self)->ma_used);
return GET_USED(_PyAnyDict_CAST(self));
Copy link
Member

Choose a reason for hiding this comment

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

Do we have to use atomic operation for frozendict?

};

static PyMappingMethods frozendict_as_mapping = {
.mp_length = dict_length,
Copy link
Member

Choose a reason for hiding this comment

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

I think that we can make frozendict_lenth


static PyMappingMethods frozendict_as_mapping = {
.mp_length = dict_length,
.mp_subscript = dict_subscript,
Copy link
Member

Choose a reason for hiding this comment

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

Still perfer to make frozendict_subscript, but let's do it at the separate PR.

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