Skip to content

Commit c232b7f

Browse files
authored
Update csv.py from 3.13.5 (RustPython#6035)
* Use `raise_if_stop!` macro * Update `csv.py` from 3.13.5 * Mark failing tests
1 parent ae03bac commit c232b7f

File tree

3 files changed

+305
-78
lines changed

3 files changed

+305
-78
lines changed

Lib/csv.py

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,90 @@
11

2-
"""
3-
csv.py - read/write/investigate CSV files
2+
r"""
3+
CSV parsing and writing.
4+
5+
This module provides classes that assist in the reading and writing
6+
of Comma Separated Value (CSV) files, and implements the interface
7+
described by PEP 305. Although many CSV files are simple to parse,
8+
the format is not formally defined by a stable specification and
9+
is subtle enough that parsing lines of a CSV file with something
10+
like line.split(",") is bound to fail. The module supports three
11+
basic APIs: reading, writing, and registration of dialects.
12+
13+
14+
DIALECT REGISTRATION:
15+
16+
Readers and writers support a dialect argument, which is a convenient
17+
handle on a group of settings. When the dialect argument is a string,
18+
it identifies one of the dialects previously registered with the module.
19+
If it is a class or instance, the attributes of the argument are used as
20+
the settings for the reader or writer:
21+
22+
class excel:
23+
delimiter = ','
24+
quotechar = '"'
25+
escapechar = None
26+
doublequote = True
27+
skipinitialspace = False
28+
lineterminator = '\r\n'
29+
quoting = QUOTE_MINIMAL
30+
31+
SETTINGS:
32+
33+
* quotechar - specifies a one-character string to use as the
34+
quoting character. It defaults to '"'.
35+
* delimiter - specifies a one-character string to use as the
36+
field separator. It defaults to ','.
37+
* skipinitialspace - specifies how to interpret spaces which
38+
immediately follow a delimiter. It defaults to False, which
39+
means that spaces immediately following a delimiter is part
40+
of the following field.
41+
* lineterminator - specifies the character sequence which should
42+
terminate rows.
43+
* quoting - controls when quotes should be generated by the writer.
44+
It can take on any of the following module constants:
45+
46+
csv.QUOTE_MINIMAL means only when required, for example, when a
47+
field contains either the quotechar or the delimiter
48+
csv.QUOTE_ALL means that quotes are always placed around fields.
49+
csv.QUOTE_NONNUMERIC means that quotes are always placed around
50+
fields which do not parse as integers or floating-point
51+
numbers.
52+
csv.QUOTE_STRINGS means that quotes are always placed around
53+
fields which are strings. Note that the Python value None
54+
is not a string.
55+
csv.QUOTE_NOTNULL means that quotes are only placed around fields
56+
that are not the Python value None.
57+
csv.QUOTE_NONE means that quotes are never placed around fields.
58+
* escapechar - specifies a one-character string used to escape
59+
the delimiter when quoting is set to QUOTE_NONE.
60+
* doublequote - controls the handling of quotes inside fields. When
61+
True, two consecutive quotes are interpreted as one during read,
62+
and when writing, each quote character embedded in the data is
63+
written as two quotes
464
"""
565

666
import re
767
import types
8-
from _csv import Error, __version__, writer, reader, register_dialect, \
68+
from _csv import Error, writer, reader, register_dialect, \
969
unregister_dialect, get_dialect, list_dialects, \
1070
field_size_limit, \
1171
QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE, \
12-
QUOTE_STRINGS, QUOTE_NOTNULL, \
13-
__doc__
72+
QUOTE_STRINGS, QUOTE_NOTNULL
1473
from _csv import Dialect as _Dialect
1574

1675
from io import StringIO
1776

1877
__all__ = ["QUOTE_MINIMAL", "QUOTE_ALL", "QUOTE_NONNUMERIC", "QUOTE_NONE",
1978
"QUOTE_STRINGS", "QUOTE_NOTNULL",
20-
"Error", "Dialect", "__doc__", "excel", "excel_tab",
79+
"Error", "Dialect", "excel", "excel_tab",
2180
"field_size_limit", "reader", "writer",
2281
"register_dialect", "get_dialect", "list_dialects", "Sniffer",
23-
"unregister_dialect", "__version__", "DictReader", "DictWriter",
82+
"unregister_dialect", "DictReader", "DictWriter",
2483
"unix_dialect"]
2584

85+
__version__ = "1.0"
86+
87+
2688
class Dialect:
2789
"""Describe a CSV dialect.
2890
@@ -51,8 +113,8 @@ def _validate(self):
51113
try:
52114
_Dialect(self)
53115
except TypeError as e:
54-
# We do this for compatibility with py2.3
55-
raise Error(str(e))
116+
# Re-raise to get a traceback showing more user code.
117+
raise Error(str(e)) from None
56118

57119
class excel(Dialect):
58120
"""Describe the usual properties of Excel-generated CSV files."""

0 commit comments

Comments
 (0)