Provide a C-API to efficently convert `dict` into `frozendict`

I agree. Changing an object type if its refcount is 1 sounds fragile and can lead to nasty bugs.

It sounds safer to create a new frozendict object, move the contents from the dict, and then clear the dict. It would work even if the refcount is 2 or higher.

During PEP 814 discussion, it was discussed to add a (public) dict method to create a frozendict with O(1) complexity. The idea was added to PEP 814 Deferred Ideas. If such method is added, it can have the same behavior: clear the dict.

We already have a similar internal C API to convert a list to a tuple: _PyList_AsTupleAndClear() creates a new tuple from a list and clears the list. It avoids an inefficient Py_INCREF() / Py_DECREF() dance on items thanks to the usage of _PyTuple_FromArraySteal() function.

4 Likes