Skip to content

Commit 68b5d3d

Browse files
committed
gh-160106: Fix csv.Dialect.__replace__() losing slot-backed attributes
copy.replace() on a csv.Dialect subclass that stores dialect options in __slots__ would silently drop those attributes. __replace__() only copied self.__dict__, so any attribute stored in a slot was missing from the new object, causing csv.reader/writer to fall back to the default value. Fix by iterating the MRO for __slots__ entries and copying any dialect attribute that was not already set via __dict__.
1 parent ffe1315 commit 68b5d3d

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

‎Lib/csv.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ def __replace__(self, /, **changes):
125125
f'argument {min(unexpected)!r}')
126126
new = object.__new__(self.__class__)
127127
new.__dict__.update(self.__dict__)
128+
# Copy slot-backed dialect attributes from the instance's MRO,
129+
# since __dict__ only captures attributes stored in __dict__.
130+
for cls in type(self).__mro__:
131+
for attr in getattr(cls, '__slots__', ()):
132+
if attr in _dialect_attributes and attr not in new.__dict__:
133+
try:
134+
new.__dict__[attr] = getattr(self, attr)
135+
except AttributeError:
136+
pass
128137
new.__dict__.update(changes)
129138
new._validate()
130139
return new

0 commit comments

Comments
 (0)