Skip to content

CSV Writer Regression in Python 3.11+: Escaping Required but Unnecessary #128064

@cspurk

Description

@cspurk

Bug report

Bug description:

The following csvtest.py script works for Python 3.10 and earlier but fails for 3.11 and later:

import csv
import io
b = io.StringIO()
csv.writer(b, quoting=csv.QUOTE_NONE, lineterminator='\n').writerow(["\r"])
print(repr(b.getvalue()))

For example, running the shell script …

for v in 9 10 11 12 13; do
    pyenv shell 3.$v
    python --version
    python csvtest.py
done

… yields:

Python 3.9.21
'\r\n'
Python 3.10.16
'\r\n'
Python 3.11.11
Traceback (most recent call last):
  File "/tmp/csvtest.py", line 4, in <module>
    csv.writer(b, quoting=csv.QUOTE_NONE, lineterminator='\n').writerow(["\r"])
_csv.Error: need to escape, but no escapechar set
Python 3.12.8
Traceback (most recent call last):
  File "/tmp/csvtest.py", line 4, in <module>
    csv.writer(b, quoting=csv.QUOTE_NONE, lineterminator='\n').writerow(["\r"])
_csv.Error: need to escape, but no escapechar set
Python 3.13.1
Traceback (most recent call last):
  File "/tmp/csvtest.py", line 4, in <module>
    csv.writer(b, quoting=csv.QUOTE_NONE, lineterminator='\n').writerow(["\r"])
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
_csv.Error: need to escape, but no escapechar set

As in Python 3.10 and earlier, escaping the carriage return character (\r) should not be necessary (due to the custom lineterminator), but Python 3.11+ still expects it.

CPython versions tested on:

3.9, 3.10, 3.11, 3.12, 3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesextension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions