24 ms·
> you should have a hard time to program Python in a way that makes it run into undefined behavior Iterating a data structure while modifying it can get some v
by mejutoco 4y ago
> you should have a hard time to program Python in a way that makes it run into undefined behavior
Iterating a data structure while modifying it can get some very weird behaviour in Python.
I think it qualifies as undefined behaviour. Python recommends not to do this, without any safeguards in place.
- ubercore 4y agoWouldn't that produce unexpected but not necessarily undefined behavior? To parent's point, I'm not sure there's going to be a language level _exploit_ there, but it will certainly not work the way you'd think.
- quietbritishjim 4y agoIterating a pure-Python list in one thread while modifying it in another will not give you true C-level undefined behaviour. It might throw an exception or return the same element twice, etc., but it won't crash or do any nasal daemon stuff (time travelling crashes). In part, that's due to the Python GIL. What this articles says is that the buffer protocol allows bugs that are qualitatively different.
- Etherlord87 4y agoUndefined behavior means it's not specified by the language how the code should be compiled. Just because something is weird and hard to predict doesn't mean it's UB.
- mejutoco 4y agoSo undefined behaviour only applies to compiled languages? that seems strange. I thought the behaviour was simply whatever cpython does but it seems it is documented (so not undefined, indeed as you say). Copy pasted below with source. Note There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, e.g. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g., https://docs.python.org/3.7/reference/compound_stmts.html#the-for-statement https://docs.python.org/3.7/reference/compound_stmts.html#th...