Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Doc/c-api/dict.rst
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ Dictionary objects
as an argument, prefer :c:func:`PyFrozenDict_New` to leave the source
intact.

Does not work for dicts with embedded values.

.. impl-detail::

Works with O(1) complexity.
Expand Down
36 changes: 36 additions & 0 deletions Lib/test/test_capi/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,47 @@ class DictSubtype(dict): ...
self.assertIs(type(d), DictSubtype)
self.assertEqual(d, DictSubtype({}))

# non str keys:
d = {1: 'a', 2: 'b', 3: 'c', None: 'f'}
f = as_frozendict(d)
self.assertIs(type(f), frozendict)
self.assertEqual(f, frozendict({1: 'a', 2: 'b', 3: 'c', None: 'f'}))
self.assertIs(type(d), dict)
self.assertEqual(d, {})

d = DictSubtype({1: 'a', 2: 'b', 3: 'c', None: 'f'})

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.

If you add this test, is it still useful to keep d = DictSubtype(x=123) test above?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think that yes, because we need to test both str and non-str keys.

f = as_frozendict(d)
self.assertIs(type(f), frozendict)
self.assertEqual(f, frozendict({1: 'a', 2: 'b', 3: 'c', None: 'f'}))
self.assertIs(type(d), DictSubtype)
self.assertEqual(d, DictSubtype({}))

# wrong inputs:
for wrong_input in (frozendict(), [], None):
with self.subTest(wrong_input=wrong_input):
with self.assertRaises(SystemError):
as_frozendict(wrong_input)

# gh-161767: check type with inline values and dict with embedded values
class Regular: ...

for klass in (Regular, _testcapi.ManagedDictType):
with self.subTest(klass=klass):
instance = klass() # empty `.__dict__`
with self.assertRaisesRegex(ValueError, 'dict with embedded values'):
as_frozendict(instance.__dict__)
self.assertEqual(instance.__dict__, {})

instance = klass() # non-empty `.__dict__`
instance.key = 'value'
with self.assertRaisesRegex(ValueError, 'dict with embedded values'):
as_frozendict(instance.__dict__)

self.assertEqual(instance.__dict__, {'key': 'value'})
# Both of these actions were needed to cause a crash:
instance.other = 'new'
del instance # must not crash

def test_frozendict_check(self):
# Test PyFrozenDict_Check()
check = _testcapi.frozendict_check
Expand Down
24 changes: 21 additions & 3 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -8663,16 +8663,22 @@ frozendict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return d;
}

static void
static int
transfer_keys_and_values_lock_held(PyObject *res, PyObject *dict)
{
PyDictObject *new = (PyDictObject *)res;
PyDictObject *old = (PyDictObject *)dict;
assert(can_modify_dict(old));

if (old->ma_values && old->ma_values->embedded) {
PyErr_SetString(PyExc_ValueError,
"can't convert dict with embedded values");
return -1;
}

// Fast path: do nothing on an empty dict:
if (old->ma_keys == Py_EMPTY_KEYS) {
return;
return 0;
}

Py_ssize_t used = old->ma_used;
Expand All @@ -8690,6 +8696,7 @@ transfer_keys_and_values_lock_held(PyObject *res, PyObject *dict)
new->ma_keys = keys;
new->ma_values = values;
ASSERT_CONSISTENT(new);
return 0;
}

PyObject *
Expand All @@ -8700,14 +8707,25 @@ PyDict_AsFrozenDictAndClear(PyObject *dict)
return NULL;
}

PyDictObject *ma = (PyDictObject*)dict;
if (ma->ma_values && ma->ma_values->embedded) {
Comment thread
sobolevn marked this conversation as resolved.
Resolved
PyErr_SetString(PyExc_ValueError,
"can't convert dict with embedded values");
return NULL;
}

PyObject *res = frozendict_new_untracked(&PyFrozenDict_Type);
if (res == NULL) {
return NULL;
}

int trasfer;
Py_BEGIN_CRITICAL_SECTION(dict);
transfer_keys_and_values_lock_held(res, dict);
trasfer = transfer_keys_and_values_lock_held(res, dict);
Py_END_CRITICAL_SECTION();
if (trasfer < 0) {
return NULL;
}

_PyObject_GC_TRACK(res);
assert(_PyFrozenDictObject_CAST(res)->ma_hash == -1);
Expand Down
Loading