Repository navigation
gh-160106: Fix csv.Dialect.__replace__() losing slot-backed attributes - #160641
Open
saiyam-jain-dev wants to merge 1 commit into
Open
saiyam-jain-dev wants to merge 1 commit into
saiyam-jain-dev wants to merge 1 commit into
Conversation
…ributes 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__.
|
The following commit authors need to sign the Contributor License Agreement: |
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #160106
copy.replace() on a csv.Dialect subclass that stores dialect options in slots silently drops those slot-backed attributes. Dialect.replace() only copied self.dict, so any attribute stored in a slot (rather than dict) was missing from the new object, causing csv.reader/csv.writer to fall back to the default comma delimiter.
Reproducer (before fix)
`python
import copy, csv
class SemicolonDialect(csv.excel):
slots = ('delimiter',)
def init(self):
self.delimiter = ';'
super().init()
dialect = SemicolonDialect()
replacement = copy.replace(dialect, quoting=csv.QUOTE_ALL)
print(list(csv.reader(['alpha;beta'], dialect))) # [['alpha', 'beta']]
print(list(csv.reader(['alpha;beta'], replacement))) # [['alpha;beta']] ← BUG
`
Fix
In Dialect.replace(), after copying self.dict, iterate the MRO for slots entries and copy any dialect attribute not already set via dict before applying the caller's changes.
Testing
The existing csv test suite continues to pass. The reproducer from the issue now produces [['alpha', 'beta']] for both readers.