
Hi Nick, 2016-01-09 8:43 GMT+01:00 Nick Coghlan <ncoghlan@gmail.com>:
For clarification, this code has defined behavior in C (we should avoid introducing new undefined behaviors). May be you mean that the bahavior is not specified from Python side (since it is platform and implementation defined).
At least in recent versions of the standard*, overflow is defined on unsigned types as wrapping modulo-N. It only remains formally undefined for signed types.
*(I'm not sure about C89, but with MSVC getting their standards compliance act together, we could potentially update our minimum C version expectation in PEP 7 to C99 or even C11).
Great.
Usage of dict.__version__ =========================
This also can be used for better detecting dict mutating during iterating: https://bugs.python.org/issue19332.
I initially thought the same thing, but the cache token will be updated even if the keys all stay the same, and one of the values is modified, while the mutation-during-iteration check is aimed at detecting changes to the keys, rather than the values.
Serhiy's unit test ensure that creating a new key and deleting a key during an iteration is detected as a dict mutation, even if the dict size doesn't change. This use case works well with dict.__version__. Any __setitem__() changes the version (except if the key already exists and the value is exactly the same, id(old_value) == id(new_value)). Example:
d={1 :1} len(d) 1 d.__version__, len(d) (1, 1) d[2]=2 del d[1] d.__version__, len(d) (3, 1)
Changing the value can be detected as well during iteration using dict.__version__:
d={1:1} d.__version__, len(d) (1, 1) d[1]=2 d.__version__, len(d) (2, 1)
It would be nice to detect keys mutation while iteration on dict.keys(), but it would also be be nice to detect values mutation while iterating on dict.values() and dict.items(). No? Victor