Title: csv writer doesn't escape escapechar
Type: behavior Stage: patch review
Components: Extension Modules Versions: Python 3.6, Python 3.5, Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Apathymannequin, Dmitriy Shashkin, berker.peksag, catalin.iacob, ebreck, eric.araujo, larry, xflr6
Priority: normal Keywords: needs review, patch

Created on 2011-05-25 18:27 by ebreck, last changed 2018-01-11 16:27 by xflr6.

File name Uploaded Description Edit ebreck, 2011-05-25 18:27
0eb420ce6567.diff catalin.iacob, 2011-07-06 21:21 review
Repositories containing patches
Messages (7)
msg136881 - (view) Author: Eric Breck (ebreck) Date: 2011-05-25 18:27
Consider the attached two files.  A reader and writer with the same dialect parameters (escapechar \ quotechar " doublequote False) read, then write a CSV cell that looks like "C\\".  It's written "C\".  The problem is, when doublequote=False, the escapechar isn't used to escape itself, and the writer writes something that in the same dialect would be understood differently (\" isn't \ then end of string, it's an escaped quotechar within the string).

Execute python first.csv to see.
msg136973 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-05-26 14:59
Thanks for the report.  It would be best if you could attach files as plain text instead of archives.
msg139953 - (view) Author: Catalin Iacob (catalin.iacob) * Date: 2011-07-06 21:20
I looked at this and tried to provide a patch + tests. Please review.

The bug is that a writer can use writerow on some input data but if a reader with the same dialect reads them back they are different from the input ones. This happens when the input data contains escapechar. Contrary to msg136881, this happens regardless whether doublequote is True or False.

The docs say "On reading, the escapechar removes any special meaning from the following character". Therefore, I understand that on writing, escapechar must always be escaped by itself. If that doesn't happen, when reading it back, escapechar alters the thing that follows it instead of counting as escapechar which is precisely what this bug is about.
msg140002 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-07-07 23:31
Thanks for the patch.  The tests look good at first glance.  I can’t comment on the C code, I don’t know C.  Hopefully someone will do it, otherwise if you don’t get feedback in say four weeks you can ask for a review on python-dev.
msg273746 - (view) Author: Wayne Harris (Apathymannequin) Date: 2016-08-27 02:11

I can confirm that this behavior still exists in my current python versions (3.5.2 & 2.7.11). I'm happy to take a look at the code, but considering I made this account specifically to comment on this issue I assume someone else will want to, as well.

If you want to make sure this is still broken, just use any of the docs' reader and writer examples, adding a trailing escape char to the end.
msg273784 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2016-08-27 16:18
The patch looked okay to me, and when applied to the 2.7 source, the new tests pass muster.

I'm not going to pretend I know where this patch should be applied. That's for someone else to pronounce.
msg309815 - (view) Author: Sebastian Bank (xflr6) Date: 2018-01-11 16:27
Hi, is there something we can do to get this going? As the issue breaks round-trip, it currently requires work-arounds like this:
Date User Action Args
2018-01-11 16:27:51xflr6setnosy: + xflr6
messages: + msg309815
2017-12-27 12:12:40Dmitriy Shashkinsetnosy: + Dmitriy Shashkin
2016-08-27 16:45:23berker.peksagsetnosy: + berker.peksag

versions: + Python 3.5, Python 3.6, - Python 3.2, Python 3.3
2016-08-27 16:18:51skip.montanarosetnosy: - skip.montanaro
2016-08-27 16:18:33skip.montanarosetmessages: + msg273784
2016-08-27 02:11:08Apathymannequinsetnosy: + Apathymannequin
messages: + msg273746
2012-07-07 08:40:10catalin.iacobsetnosy: + larry
2011-07-07 23:31:42eric.araujosetkeywords: + needs review

stage: patch review
messages: + msg140002
versions: - Python 3.1
2011-07-06 21:21:25catalin.iacobsetfiles: + 0eb420ce6567.diff
keywords: + patch
2011-07-06 21:20:31catalin.iacobsetnosy: + catalin.iacob

messages: + msg139953
hgrepos: + hgrepo38
2011-05-26 14:59:20eric.araujosetversions: + Python 3.1, Python 3.2, Python 3.3, - Python 2.6
nosy: + eric.araujo, skip.montanaro

messages: + msg136973

components: + Extension Modules, - None
2011-05-25 18:27:10ebreckcreate