[Python-Dev] List mutation in list_repr? (original) (raw)
Patrick Westerhoff patrickwesterhoff at gmail.com
Tue Dec 6 05:27:35 EST 2016
- Previous message (by thread): [Python-Dev] LAST CHANCE: 3.6.0 code freeze (3.6.0rc1) in 12 hours at 2016-12-07 12:00 UTC
- Next message (by thread): [Python-Dev] List mutation in list_repr?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hey all,
I just stumbled on the following comment in the C source of the repr implementation for the list object:
/* Do repr() on each element. Note that this may mutate the list,
so must refetch the list size on each iteration. */
(as seen in list_repr implementation [1])
I’m honestly very surprised about this remark since I can neither
understand why this would be the case (repr shouldn’t mutate the
list), and I also don’t see any clue in the source as to when this
would actually happen. Since inside the loop, the list object v
is
never accessed other than passing v->ob_item[i]
to the recursive
repr call, there shouldn’t be any mutation on the list object itself.
I understand that a custom repr implementation could theoretically
mutate an object, but this could only affect the object itself (or its
children), but not the container list. So the list object v
here
should be safe from mutations.
I tried looking at the original change when this was introduced. Unfortunately, this was in 2001 [2], so I don’t really expect Tim to still know why the comment was added back then.
Do you have any insights on why the comment is there, and whether I am missing something that could actually mutate the list, making the size refetch necessary and the comment justified?
Thanks a lot! Patrick
[1] https://github.com/python/cpython/blob/b8519e4d08a82da9aa438d531058100c0e3d04b4/Objects/listobject.c#L361 [2] https://github.com/python/cpython/commit/bce15a39b30b0f5866e7b48ba3c29c3aa430a766
- Previous message (by thread): [Python-Dev] LAST CHANCE: 3.6.0 code freeze (3.6.0rc1) in 12 hours at 2016-12-07 12:00 UTC
- Next message (by thread): [Python-Dev] List mutation in list_repr?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]