[New-bugs-announce] [issue46450] namedtuple leaks data between instances when field's default value is empty list
Curtis M
report at bugs.python.org
Thu Jan 20 23:19:36 EST 2022
New submission from Curtis M <3ed7qja59y at liamekaens.com>:
Example code to replicate issue on python 3.10.2 is attached.
How to replicate issue:
1. Define a namedtuple where all fields have default values. At least one field's default value will be an empty list: []
2. Instantiate 2 instances of the namedtuple.
3. In the first instance, use namedtuple._replace() to add a replacement list into a field that defaults to [].
4. Now look at the contents of both namedtuples. *Both* of them are modified, even though the code only modified the first object.
***
Output from attached example code:
g.P5 1: myNamedTuple(P1='hello', P2='world', P3=None, P4='', P5=[], P6=[])
h.P5 1: myNamedTuple(P1='good', P2='morning', P3=None, P4='', P5=[], P6=[])
Expected: g.P5: []
Actual: g.P5: []
Expected: h.P5: []
Actual: h.P5: []
g.P5 2: myNamedTuple(P1='hello', P2='world', P3=None, P4='', P5=['a', 'b'], P6=[])
h.P5 2: myNamedTuple(P1='good', P2='morning', P3=None, P4='', P5=['a', 'b'], P6=[])
Expected: g.P5: ['a', 'b']
Actual: g.P5: ['a', 'b']
Expected: h.P5: []
Actual: h.P5: ['a', 'b']
g.P5 3: myNamedTuple(P1='hello', P2='world', P3=None, P4='', P5=['a', 'b', 'c', 'd'], P6=[])
h.P5 3: myNamedTuple(P1='good', P2='morning', P3=None, P4='', P5=['a', 'b', 'c', 'd'], P6=[])
Expected: g.P5: ['a', 'b', 'c', 'd']
Actual: g.P5: ['a', 'b', 'c', 'd']
Expected: h.P5: []
Actual: h.P5: ['a', 'b', 'c', 'd']
----------
components: Library (Lib)
files: namedtuple_example.py
messages: 411079
nosy: user027.7
priority: normal
severity: normal
status: open
title: namedtuple leaks data between instances when field's default value is empty list
type: behavior
versions: Python 3.10
Added file: https://bugs.python.org/file50568/namedtuple_example.py
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46450>
_______________________________________
More information about the New-bugs-announce
mailing list