Repository navigation
Conversation
| } | ||
| dictkeys_incref(keys); | ||
| clear_common(old); | ||
| for (Py_ssize_t i = 0; i < keys->dk_nentries; i++) { |
There was a problem hiding this comment.
dk_nentries can be modified by another thread. How about using v->capacity instead?
There was a problem hiding this comment.
good catch, switched to v->capacity.
| class A: ... | ||
| a = A() | ||
| a.x = 1 | ||
| f = as_frozendict(a.__dict__) |
There was a problem hiding this comment.
I would prefer to check also a just after as_frozendict() call:
| f = as_frozendict(a.__dict__) | |
| f = as_frozendict(a.__dict__) | |
| self.assertEqual(vars(a), {}) |
| ASSERT_CONSISTENT(old); | ||
| } |
There was a problem hiding this comment.
IMO it's also worth it to check dict consistency for the if (values && values->embedded) case, so:
| ASSERT_CONSISTENT(old); | |
| } | |
| } | |
| ASSERT_CONSISTENT(old); |
| } | ||
|
|
||
| // Transfer keys and values from dict to frozendict: | ||
| new->ma_used = used; |
There was a problem hiding this comment.
| new->ma_used = used; | |
| // Only transfer keys and values to the new dict | |
| // once the old dict is cleared | |
| new->ma_used = used; |
|
done, applied all three. |
|
cc @sobolevn |
sobolevn
left a comment
There was a problem hiding this comment.
Maybe we should disallow values with ->embedded set?
The whole point of this API is to use mutable dict objects to create new frozendict objects efficently.
Using embedded dicts do not really make sense in my opinion.
I would just raise SystemError for this case.
| a = A() | ||
| a.x = 1 | ||
| f = as_frozendict(a.__dict__) | ||
| self.assertEqual(vars(a), {}) |
There was a problem hiding this comment.
It feels weird that this mutates instance's __dict__ :(
| } | ||
| dictkeys_incref(keys); | ||
| clear_common(old); | ||
| for (Py_ssize_t i = 0; i < v->capacity; i++) { |
There was a problem hiding this comment.
Maybe invalidate_and_clear_inline_values(v); instead?
| PyErr_NoMemory(); | ||
| return -1; | ||
| } | ||
| dictkeys_incref(keys); |
There was a problem hiding this comment.
Question: why do we need the incref here?
When you call PyDict_AsFrozenDictAndClear(), you may not know in advance if the dict uses inline values or not. |
|
I think that all dicts created by |
|
I never understood exactly when inline values are used or not. It seems like it's mostly used for object managed dictionary? Maybe you're right and raising an exception is just fine in this case. Can it be a ValueError or a RuntimeError? For me, SystemError if you obviously misused the API. Here you pass a dictionary, but the fact that values are inlined is a suble implementation detail. |
|
As far as I know, inline values are only used for I doubled checked with DeepWiki: https://deepwiki.com/search/7660ca12-56c2-4868-b54c-a6306dca7990?mode=deep
One more thing that I am not quite sure about: will this feature get more usage in other use-cases? Or will it stay as an implementation detail just for this small case?
|
|
I created an alternative PR with my proposal: #162139 :) |
Oh, I didn't know this website. Interesting! |

Copy the inline values into the new frozendict and clear them on the object, instead of leaving them shared.