annotate Modules/_csv.c @ 49961:e793fc3c8abe

More 3.0 -> 3.1 transistion in the Windows and OS/2 build files
author Christian Heimes <christian@cheimes.de>
date Thu, 04 Dec 2008 14:27:25 +0000
parents ac854cde3135
children e9675d72e3e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28393
16aa72a8c281 add comment about 2.2 compatibility
Skip Montanaro <skip@pobox.com>
parents: 28349
diff changeset
1 /* csv module */
16aa72a8c281 add comment about 2.2 compatibility
Skip Montanaro <skip@pobox.com>
parents: 28349
diff changeset
2
16aa72a8c281 add comment about 2.2 compatibility
Skip Montanaro <skip@pobox.com>
parents: 28349
diff changeset
3 /*
16aa72a8c281 add comment about 2.2 compatibility
Skip Montanaro <skip@pobox.com>
parents: 28349
diff changeset
4
16aa72a8c281 add comment about 2.2 compatibility
Skip Montanaro <skip@pobox.com>
parents: 28349
diff changeset
5 This module provides the low-level underpinnings of a CSV reading/writing
16aa72a8c281 add comment about 2.2 compatibility
Skip Montanaro <skip@pobox.com>
parents: 28349
diff changeset
6 module. Users should not use this module directly, but import the csv.py
16aa72a8c281 add comment about 2.2 compatibility
Skip Montanaro <skip@pobox.com>
parents: 28349
diff changeset
7 module instead.
16aa72a8c281 add comment about 2.2 compatibility
Skip Montanaro <skip@pobox.com>
parents: 28349
diff changeset
8
16aa72a8c281 add comment about 2.2 compatibility
Skip Montanaro <skip@pobox.com>
parents: 28349
diff changeset
9 **** For people modifying this code, please note that as of this writing
28588
Skip Montanaro <skip@pobox.com>
parents: 28585
diff changeset
10 **** (2003-03-23), it is intended that this code should work with Python
28393
16aa72a8c281 add comment about 2.2 compatibility
Skip Montanaro <skip@pobox.com>
parents: 28349
diff changeset
11 **** 2.2.
16aa72a8c281 add comment about 2.2 compatibility
Skip Montanaro <skip@pobox.com>
parents: 28349
diff changeset
12
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
13 */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
14
28592
ef05f981bc4b use PyModule_Add{Int,String}Constant() where appropriate
Skip Montanaro <skip@pobox.com>
parents: 28591
diff changeset
15 #define MODULE_VERSION "1.0"
ef05f981bc4b use PyModule_Add{Int,String}Constant() where appropriate
Skip Montanaro <skip@pobox.com>
parents: 28591
diff changeset
16
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
17 #include "Python.h"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
18 #include "structmember.h"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
19
34385
3533e3cb7b97 Where a string is desired, test for PyBaseString_Type derived type,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34381
diff changeset
20 #define IS_BASESTRING(o) \
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 42486
diff changeset
21 PyUnicode_Check(o)
34385
3533e3cb7b97 Where a string is desired, test for PyBaseString_Type derived type,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34381
diff changeset
22
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
23 static PyObject *error_obj; /* CSV exception */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
24 static PyObject *dialects; /* Dialect registry */
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
25 static long field_limit = 128 * 1024; /* max parsed field size */
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
26
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
27 typedef enum {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
28 START_RECORD, START_FIELD, ESCAPED_CHAR, IN_FIELD,
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
29 IN_QUOTED_FIELD, ESCAPE_IN_QUOTED_FIELD, QUOTE_IN_QUOTED_FIELD,
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
30 EAT_CRNL
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
31 } ParserState;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
32
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
33 typedef enum {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
34 QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
35 } QuoteStyle;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
36
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
37 typedef struct {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
38 QuoteStyle style;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
39 char *name;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
40 } StyleDesc;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
41
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
42 static StyleDesc quote_styles[] = {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
43 { QUOTE_MINIMAL, "QUOTE_MINIMAL" },
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
44 { QUOTE_ALL, "QUOTE_ALL" },
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
45 { QUOTE_NONNUMERIC, "QUOTE_NONNUMERIC" },
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
46 { QUOTE_NONE, "QUOTE_NONE" },
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
47 { 0 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
48 };
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
49
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
50 typedef struct {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
51 PyObject_HEAD
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
52
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
53 int doublequote; /* is " represented by ""? */
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
54 Py_UNICODE delimiter; /* field separator */
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
55 Py_UNICODE quotechar; /* quote character */
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
56 Py_UNICODE escapechar; /* escape character */
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
57 int skipinitialspace; /* ignore spaces following delimiter? */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
58 PyObject *lineterminator; /* string to write between records */
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
59 int quoting; /* style of quoting to write */
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
60
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
61 int strict; /* raise exception on bad CSV */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
62 } DialectObj;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
63
37144
91dd4d44d3bc Finish getting rid of statichere/staticforward
Neal Norwitz <nnorwitz@gmail.com>
parents: 36684
diff changeset
64 static PyTypeObject Dialect_Type;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
65
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
66 typedef struct {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
67 PyObject_HEAD
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
68
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
69 PyObject *input_iter; /* iterate over this for input lines */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
70
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
71 DialectObj *dialect; /* parsing dialect */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
72
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
73 PyObject *fields; /* field list for current record */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
74 ParserState state; /* current CSV parse state */
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
75 Py_UNICODE *field; /* build current field in here */
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
76 int field_size; /* size of allocated buffer */
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
77 Py_ssize_t field_len; /* length of current field */
34409
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
78 int numeric_field; /* treat field as numeric */
34410
a70dd8767b07 Add counting of source iterator lines to the reader object - handy for
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34409
diff changeset
79 unsigned long line_num; /* Source-file line number */
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
80 } ReaderObj;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
81
37144
91dd4d44d3bc Finish getting rid of statichere/staticforward
Neal Norwitz <nnorwitz@gmail.com>
parents: 36684
diff changeset
82 static PyTypeObject Reader_Type;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
83
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44158
diff changeset
84 #define ReaderObject_Check(v) (Py_TYPE(v) == &Reader_Type)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
85
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
86 typedef struct {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
87 PyObject_HEAD
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
88
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
89 PyObject *writeline; /* write output lines to this file */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
90
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
91 DialectObj *dialect; /* parsing dialect */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
92
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
93 Py_UNICODE *rec; /* buffer for parser.join */
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
94 int rec_size; /* size of allocated record */
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
95 Py_ssize_t rec_len; /* length of record */
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
96 int num_fields; /* number of fields in record */
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
97 } WriterObj;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
98
37144
91dd4d44d3bc Finish getting rid of statichere/staticforward
Neal Norwitz <nnorwitz@gmail.com>
parents: 36684
diff changeset
99 static PyTypeObject Writer_Type;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
100
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
101 /*
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
102 * DIALECT class
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
103 */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
104
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
105 static PyObject *
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
106 get_dialect_from_registry(PyObject * name_obj)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
107 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
108 PyObject *dialect_obj;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
109
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
110 dialect_obj = PyDict_GetItem(dialects, name_obj);
34389
7391e18b0a7c Only set error string when dict lookup found no matching key (was setting
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34386
diff changeset
111 if (dialect_obj == NULL) {
7391e18b0a7c Only set error string when dict lookup found no matching key (was setting
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34386
diff changeset
112 if (!PyErr_Occurred())
7391e18b0a7c Only set error string when dict lookup found no matching key (was setting
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34386
diff changeset
113 PyErr_Format(error_obj, "unknown dialect");
7391e18b0a7c Only set error string when dict lookup found no matching key (was setting
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34386
diff changeset
114 }
7391e18b0a7c Only set error string when dict lookup found no matching key (was setting
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34386
diff changeset
115 else
7391e18b0a7c Only set error string when dict lookup found no matching key (was setting
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34386
diff changeset
116 Py_INCREF(dialect_obj);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
117 return dialect_obj;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
118 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
119
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
120 static PyObject *
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
121 get_string(PyObject *str)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
122 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
123 Py_XINCREF(str);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
124 return str;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
125 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
126
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
127 static PyObject *
42463
b10ba38e6b80 One char->Py_UNICODE change missed in r56777 - according to Adam Hupp this is the change to make...
Skip Montanaro <skip@pobox.com>
parents: 42462
diff changeset
128 get_nullchar_as_None(Py_UNICODE c)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
129 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
130 if (c == '\0') {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
131 Py_INCREF(Py_None);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
132 return Py_None;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
133 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
134 else
42463
b10ba38e6b80 One char->Py_UNICODE change missed in r56777 - according to Adam Hupp this is the change to make...
Skip Montanaro <skip@pobox.com>
parents: 42462
diff changeset
135 return PyUnicode_FromUnicode((Py_UNICODE *)&c, 1);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
136 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
137
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
138 static PyObject *
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
139 Dialect_get_lineterminator(DialectObj *self)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
140 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
141 return get_string(self->lineterminator);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
142 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
143
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
144 static PyObject *
42486
3f1fcd080ec4 Fix the last remaining test_csv failure.
Guido van Rossum <guido@python.org>
parents: 42463
diff changeset
145 Dialect_get_delimiter(DialectObj *self)
3f1fcd080ec4 Fix the last remaining test_csv failure.
Guido van Rossum <guido@python.org>
parents: 42463
diff changeset
146 {
3f1fcd080ec4 Fix the last remaining test_csv failure.
Guido van Rossum <guido@python.org>
parents: 42463
diff changeset
147 return get_nullchar_as_None(self->delimiter);
3f1fcd080ec4 Fix the last remaining test_csv failure.
Guido van Rossum <guido@python.org>
parents: 42463
diff changeset
148 }
3f1fcd080ec4 Fix the last remaining test_csv failure.
Guido van Rossum <guido@python.org>
parents: 42463
diff changeset
149
3f1fcd080ec4 Fix the last remaining test_csv failure.
Guido van Rossum <guido@python.org>
parents: 42463
diff changeset
150 static PyObject *
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
151 Dialect_get_escapechar(DialectObj *self)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
152 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
153 return get_nullchar_as_None(self->escapechar);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
154 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
155
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
156 static PyObject *
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
157 Dialect_get_quotechar(DialectObj *self)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
158 {
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
159 return get_nullchar_as_None(self->quotechar);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
160 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
161
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
162 static PyObject *
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
163 Dialect_get_quoting(DialectObj *self)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
164 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44046
diff changeset
165 return PyLong_FromLong(self->quoting);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
166 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
167
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
168 static int
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
169 _set_bool(const char *name, int *target, PyObject *src, int dflt)
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
170 {
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
171 if (src == NULL)
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
172 *target = dflt;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
173 else
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
174 *target = PyObject_IsTrue(src);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
175 return 0;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
176 }
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
177
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
178 static int
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
179 _set_int(const char *name, int *target, PyObject *src, int dflt)
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
180 {
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
181 if (src == NULL)
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
182 *target = dflt;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
183 else {
44158
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
184 long value;
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
185 if (!PyLong_CheckExact(src)) {
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
186 PyErr_Format(PyExc_TypeError,
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
187 "\"%s\" must be an integer", name);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
188 return -1;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
189 }
44158
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
190 value = PyLong_AsLong(src);
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
191 if (value == -1 && PyErr_Occurred())
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
192 return -1;
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
193 #if SIZEOF_LONG > SIZEOF_INT
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
194 if (value > INT_MAX || value < INT_MIN) {
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
195 PyErr_Format(PyExc_ValueError,
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
196 "integer out of range for \"%s\"", name);
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
197 return -1;
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
198 }
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
199 #endif
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
200 *target = (int)value;
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
201 }
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
202 return 0;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
203 }
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
204
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
205 static int
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
206 _set_char(const char *name, Py_UNICODE *target, PyObject *src, Py_UNICODE dflt)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
207 {
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
208 if (src == NULL)
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
209 *target = dflt;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
210 else {
41684
fb705f18bf3d Fix some miscellaneous places that incorrectly insisted on str8.
Guido van Rossum <guido@python.org>
parents: 40626
diff changeset
211 *target = '\0';
fb705f18bf3d Fix some miscellaneous places that incorrectly insisted on str8.
Guido van Rossum <guido@python.org>
parents: 40626
diff changeset
212 if (src != Py_None) {
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
213 Py_UNICODE *buf;
41684
fb705f18bf3d Fix some miscellaneous places that incorrectly insisted on str8.
Guido van Rossum <guido@python.org>
parents: 40626
diff changeset
214 Py_ssize_t len;
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
215 buf = PyUnicode_AsUnicode(src);
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
216 len = PyUnicode_GetSize(src);
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
217 if (buf == NULL || len > 1) {
41684
fb705f18bf3d Fix some miscellaneous places that incorrectly insisted on str8.
Guido van Rossum <guido@python.org>
parents: 40626
diff changeset
218 PyErr_Format(PyExc_TypeError,
fb705f18bf3d Fix some miscellaneous places that incorrectly insisted on str8.
Guido van Rossum <guido@python.org>
parents: 40626
diff changeset
219 "\"%s\" must be an 1-character string",
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
220 name);
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
221 return -1;
41684
fb705f18bf3d Fix some miscellaneous places that incorrectly insisted on str8.
Guido van Rossum <guido@python.org>
parents: 40626
diff changeset
222 }
fb705f18bf3d Fix some miscellaneous places that incorrectly insisted on str8.
Guido van Rossum <guido@python.org>
parents: 40626
diff changeset
223 if (len > 0)
fb705f18bf3d Fix some miscellaneous places that incorrectly insisted on str8.
Guido van Rossum <guido@python.org>
parents: 40626
diff changeset
224 *target = buf[0];
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
225 }
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
226 }
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
227 return 0;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
228 }
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
229
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
230 static int
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
231 _set_str(const char *name, PyObject **target, PyObject *src, const char *dflt)
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
232 {
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
233 if (src == NULL)
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
234 *target = PyUnicode_DecodeASCII(dflt, strlen(dflt), NULL);
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
235 else {
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
236 if (src == Py_None)
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
237 *target = NULL;
34385
3533e3cb7b97 Where a string is desired, test for PyBaseString_Type derived type,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34381
diff changeset
238 else if (!IS_BASESTRING(src)) {
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
239 PyErr_Format(PyExc_TypeError,
43918
146ba97ffe1a Correct test_cvs on Windows, as suggested by Raghuram Devarakonda
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43866
diff changeset
240 "\"%s\" must be a string", name);
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
241 return -1;
34341
f80bb8aff419 Fix to use PEP7 brace style.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34340
diff changeset
242 }
f80bb8aff419 Fix to use PEP7 brace style.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34340
diff changeset
243 else {
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
244 Py_XDECREF(*target);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
245 Py_INCREF(src);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
246 *target = src;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
247 }
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
248 }
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
249 return 0;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
250 }
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
251
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
252 static int
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
253 dialect_check_quoting(int quoting)
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
254 {
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
255 StyleDesc *qs = quote_styles;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
256
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
257 for (qs = quote_styles; qs->name; qs++) {
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
258 if (qs->style == quoting)
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
259 return 0;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
260 }
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
261 PyErr_Format(PyExc_TypeError, "bad \"quoting\" value");
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
262 return -1;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
263 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
264
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
265 #define D_OFF(x) offsetof(DialectObj, x)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
266
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
267 static struct PyMemberDef Dialect_memberlist[] = {
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
268 { "skipinitialspace", T_INT, D_OFF(skipinitialspace), READONLY },
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
269 { "doublequote", T_INT, D_OFF(doublequote), READONLY },
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
270 { "strict", T_INT, D_OFF(strict), READONLY },
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
271 { NULL }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
272 };
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
273
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
274 static PyGetSetDef Dialect_getsetlist[] = {
42486
3f1fcd080ec4 Fix the last remaining test_csv failure.
Guido van Rossum <guido@python.org>
parents: 42463
diff changeset
275 { "delimiter", (getter)Dialect_get_delimiter},
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
276 { "escapechar", (getter)Dialect_get_escapechar},
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
277 { "lineterminator", (getter)Dialect_get_lineterminator},
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
278 { "quotechar", (getter)Dialect_get_quotechar},
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
279 { "quoting", (getter)Dialect_get_quoting},
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
280 {NULL},
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
281 };
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
282
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
283 static void
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
284 Dialect_dealloc(DialectObj *self)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
285 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
286 Py_XDECREF(self->lineterminator);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44158
diff changeset
287 Py_TYPE(self)->tp_free((PyObject *)self);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
288 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
289
36684
0bebeb393045 More unconsting.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
290 static char *dialect_kws[] = {
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
291 "dialect",
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
292 "delimiter",
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
293 "doublequote",
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
294 "escapechar",
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
295 "lineterminator",
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
296 "quotechar",
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
297 "quoting",
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
298 "skipinitialspace",
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
299 "strict",
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
300 NULL
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
301 };
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
302
34395
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
303 static PyObject *
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
304 dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
305 {
34395
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
306 DialectObj *self;
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
307 PyObject *ret = NULL;
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
308 PyObject *dialect = NULL;
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
309 PyObject *delimiter = NULL;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
310 PyObject *doublequote = NULL;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
311 PyObject *escapechar = NULL;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
312 PyObject *lineterminator = NULL;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
313 PyObject *quotechar = NULL;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
314 PyObject *quoting = NULL;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
315 PyObject *skipinitialspace = NULL;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
316 PyObject *strict = NULL;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
317
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
318 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
319 "|OOOOOOOOO", dialect_kws,
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
320 &dialect,
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
321 &delimiter,
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
322 &doublequote,
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
323 &escapechar,
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
324 &lineterminator,
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
325 &quotechar,
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
326 &quoting,
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
327 &skipinitialspace,
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
328 &strict))
34395
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
329 return NULL;
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
330
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
331 if (dialect != NULL) {
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
332 if (IS_BASESTRING(dialect)) {
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
333 dialect = get_dialect_from_registry(dialect);
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
334 if (dialect == NULL)
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
335 return NULL;
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
336 }
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
337 else
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
338 Py_INCREF(dialect);
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
339 /* Can we reuse this instance? */
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
340 if (PyObject_TypeCheck(dialect, &Dialect_Type) &&
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
341 delimiter == 0 &&
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
342 doublequote == 0 &&
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
343 escapechar == 0 &&
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
344 lineterminator == 0 &&
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
345 quotechar == 0 &&
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
346 quoting == 0 &&
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
347 skipinitialspace == 0 &&
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
348 strict == 0)
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
349 return dialect;
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
350 }
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
351
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
352 self = (DialectObj *)type->tp_alloc(type, 0);
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
353 if (self == NULL) {
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
354 Py_XDECREF(dialect);
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
355 return NULL;
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
356 }
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
357 self->lineterminator = NULL;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
358
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
359 Py_XINCREF(delimiter);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
360 Py_XINCREF(doublequote);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
361 Py_XINCREF(escapechar);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
362 Py_XINCREF(lineterminator);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
363 Py_XINCREF(quotechar);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
364 Py_XINCREF(quoting);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
365 Py_XINCREF(skipinitialspace);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
366 Py_XINCREF(strict);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
367 if (dialect != NULL) {
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
368 #define DIALECT_GETATTR(v, n) \
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
369 if (v == NULL) \
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
370 v = PyObject_GetAttrString(dialect, n)
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
371 DIALECT_GETATTR(delimiter, "delimiter");
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
372 DIALECT_GETATTR(doublequote, "doublequote");
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
373 DIALECT_GETATTR(escapechar, "escapechar");
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
374 DIALECT_GETATTR(lineterminator, "lineterminator");
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
375 DIALECT_GETATTR(quotechar, "quotechar");
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
376 DIALECT_GETATTR(quoting, "quoting");
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
377 DIALECT_GETATTR(skipinitialspace, "skipinitialspace");
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
378 DIALECT_GETATTR(strict, "strict");
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
379 PyErr_Clear();
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
380 }
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
381
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
382 /* check types and convert to C values */
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
383 #define DIASET(meth, name, target, src, dflt) \
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
384 if (meth(name, target, src, dflt)) \
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
385 goto err
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
386 DIASET(_set_char, "delimiter", &self->delimiter, delimiter, ',');
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
387 DIASET(_set_bool, "doublequote", &self->doublequote, doublequote, 1);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
388 DIASET(_set_char, "escapechar", &self->escapechar, escapechar, 0);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
389 DIASET(_set_str, "lineterminator", &self->lineterminator, lineterminator, "\r\n");
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
390 DIASET(_set_char, "quotechar", &self->quotechar, quotechar, '"');
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
391 DIASET(_set_int, "quoting", &self->quoting, quoting, QUOTE_MINIMAL);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
392 DIASET(_set_bool, "skipinitialspace", &self->skipinitialspace, skipinitialspace, 0);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
393 DIASET(_set_bool, "strict", &self->strict, strict, 0);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
394
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
395 /* validate options */
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
396 if (dialect_check_quoting(self->quoting))
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
397 goto err;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
398 if (self->delimiter == 0) {
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
399 PyErr_SetString(PyExc_TypeError, "delimiter must be set");
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
400 goto err;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
401 }
34408
72fd373cd0f9 Fix logic problem in quoting=csv.QUOTE_ALL, quotechar=None check, add test.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34407
diff changeset
402 if (quotechar == Py_None && quoting == NULL)
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
403 self->quoting = QUOTE_NONE;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
404 if (self->quoting != QUOTE_NONE && self->quotechar == 0) {
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
405 PyErr_SetString(PyExc_TypeError,
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
406 "quotechar must be set if quoting enabled");
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
407 goto err;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
408 }
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
409 if (self->lineterminator == 0) {
34395
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
410 PyErr_SetString(PyExc_TypeError, "lineterminator must be set");
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
411 goto err;
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
412 }
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
413
34395
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
414 ret = (PyObject *)self;
35081
2b5186b595b1 Leak fix from Michael Hudson. Fix memory leak when dialect doesn't
Skip Montanaro <skip@pobox.com>
parents: 34418
diff changeset
415 Py_INCREF(self);
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
416 err:
35081
2b5186b595b1 Leak fix from Michael Hudson. Fix memory leak when dialect doesn't
Skip Montanaro <skip@pobox.com>
parents: 34418
diff changeset
417 Py_XDECREF(self);
34395
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
418 Py_XDECREF(dialect);
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
419 Py_XDECREF(delimiter);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
420 Py_XDECREF(doublequote);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
421 Py_XDECREF(escapechar);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
422 Py_XDECREF(lineterminator);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
423 Py_XDECREF(quotechar);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
424 Py_XDECREF(quoting);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
425 Py_XDECREF(skipinitialspace);
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
426 Py_XDECREF(strict);
34395
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
427 return ret;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
428 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
429
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
430
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
431 PyDoc_STRVAR(Dialect_Type_doc,
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
432 "CSV dialect\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
433 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
434 "The Dialect type records CSV parsing and generation options.\n");
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
435
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
436 static PyTypeObject Dialect_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41684
diff changeset
437 PyVarObject_HEAD_INIT(NULL, 0)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
438 "_csv.Dialect", /* tp_name */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
439 sizeof(DialectObj), /* tp_basicsize */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
440 0, /* tp_itemsize */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
441 /* methods */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
442 (destructor)Dialect_dealloc, /* tp_dealloc */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
443 (printfunc)0, /* tp_print */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
444 (getattrfunc)0, /* tp_getattr */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
445 (setattrfunc)0, /* tp_setattr */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
446 (cmpfunc)0, /* tp_compare */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
447 (reprfunc)0, /* tp_repr */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
448 0, /* tp_as_number */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
449 0, /* tp_as_sequence */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
450 0, /* tp_as_mapping */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
451 (hashfunc)0, /* tp_hash */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
452 (ternaryfunc)0, /* tp_call */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
453 (reprfunc)0, /* tp_str */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
454 0, /* tp_getattro */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
455 0, /* tp_setattro */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
456 0, /* tp_as_buffer */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
457 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
458 Dialect_Type_doc, /* tp_doc */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
459 0, /* tp_traverse */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
460 0, /* tp_clear */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
461 0, /* tp_richcompare */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
462 0, /* tp_weaklistoffset */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
463 0, /* tp_iter */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
464 0, /* tp_iternext */
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
465 0, /* tp_methods */
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
466 Dialect_memberlist, /* tp_members */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
467 Dialect_getsetlist, /* tp_getset */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
468 0, /* tp_base */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
469 0, /* tp_dict */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
470 0, /* tp_descr_get */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
471 0, /* tp_descr_set */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
472 0, /* tp_dictoffset */
34395
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
473 0, /* tp_init */
6c02bb3fcca6 Now that internal dialect type is immutable, and the dialect registry
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34392
diff changeset
474 0, /* tp_alloc */
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
475 dialect_new, /* tp_new */
28599
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
476 0, /* tp_free */
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
477 };
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
478
34390
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
479 /*
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
480 * Return an instance of the dialect type, given a Python instance or kwarg
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
481 * description of the dialect
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
482 */
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
483 static PyObject *
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
484 _call_dialect(PyObject *dialect_inst, PyObject *kwargs)
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
485 {
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
486 PyObject *ctor_args;
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
487 PyObject *dialect;
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
488
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
489 ctor_args = Py_BuildValue(dialect_inst ? "(O)" : "()", dialect_inst);
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
490 if (ctor_args == NULL)
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
491 return NULL;
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
492 dialect = PyObject_Call((PyObject *)&Dialect_Type, ctor_args, kwargs);
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
493 Py_DECREF(ctor_args);
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
494 return dialect;
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
495 }
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
496
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
497 /*
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
498 * READER
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
499 */
34409
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
500 static int
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
501 parse_save_field(ReaderObj *self)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
502 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
503 PyObject *field;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
504
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
505 field = PyUnicode_FromUnicode(self->field, self->field_len);
34409
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
506 if (field == NULL)
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
507 return -1;
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
508 self->field_len = 0;
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
509 if (self->numeric_field) {
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
510 PyObject *tmp;
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
511
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
512 self->numeric_field = 0;
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
513 tmp = PyNumber_Float(field);
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
514 if (tmp == NULL) {
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
515 Py_DECREF(field);
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
516 return -1;
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
517 }
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
518 Py_DECREF(field);
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
519 field = tmp;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
520 }
34409
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
521 PyList_Append(self->fields, field);
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
522 Py_DECREF(field);
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
523 return 0;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
524 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
525
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
526 static int
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
527 parse_grow_buff(ReaderObj *self)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
528 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
529 if (self->field_size == 0) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
530 self->field_size = 4096;
29389
857428cb418c Fix potential leaks identified by Neal Norwitz.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 28599
diff changeset
531 if (self->field != NULL)
857428cb418c Fix potential leaks identified by Neal Norwitz.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 28599
diff changeset
532 PyMem_Free(self->field);
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
533 self->field = PyMem_New(Py_UNICODE, self->field_size);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
534 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
535 else {
48330
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
536 if (self->field_size > INT_MAX / 2) {
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
537 PyErr_NoMemory();
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
538 return 0;
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
539 }
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
540 self->field_size *= 2;
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
541 self->field = PyMem_Resize(self->field, Py_UNICODE,
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
542 self->field_size);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
543 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
544 if (self->field == NULL) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
545 PyErr_NoMemory();
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
546 return 0;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
547 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
548 return 1;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
549 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
550
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
551 static int
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
552 parse_add_char(ReaderObj *self, Py_UNICODE c)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
553 {
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
554 if (self->field_len >= field_limit) {
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
555 PyErr_Format(error_obj, "field larger than field limit (%ld)",
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
556 field_limit);
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
557 return -1;
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
558 }
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
559 if (self->field_len == self->field_size && !parse_grow_buff(self))
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
560 return -1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
561 self->field[self->field_len++] = c;
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
562 return 0;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
563 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
564
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
565 static int
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
566 parse_process_char(ReaderObj *self, Py_UNICODE c)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
567 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
568 DialectObj *dialect = self->dialect;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
569
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
570 switch (self->state) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
571 case START_RECORD:
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
572 /* start of record */
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
573 if (c == '\0')
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
574 /* empty line - return [] */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
575 break;
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
576 else if (c == '\n' || c == '\r') {
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
577 self->state = EAT_CRNL;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
578 break;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
579 }
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
580 /* normal character - handle as START_FIELD */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
581 self->state = START_FIELD;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
582 /* fallthru */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
583 case START_FIELD:
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
584 /* expecting field */
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
585 if (c == '\n' || c == '\r' || c == '\0') {
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
586 /* save empty field - return [fields] */
34409
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
587 if (parse_save_field(self) < 0)
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
588 return -1;
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
589 self->state = (c == '\0' ? START_RECORD : EAT_CRNL);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
590 }
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
591 else if (c == dialect->quotechar &&
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
592 dialect->quoting != QUOTE_NONE) {
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
593 /* start quoted field */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
594 self->state = IN_QUOTED_FIELD;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
595 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
596 else if (c == dialect->escapechar) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
597 /* possible escaped character */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
598 self->state = ESCAPED_CHAR;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
599 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
600 else if (c == ' ' && dialect->skipinitialspace)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
601 /* ignore space at start of field */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
602 ;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
603 else if (c == dialect->delimiter) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
604 /* save empty field */
34409
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
605 if (parse_save_field(self) < 0)
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
606 return -1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
607 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
608 else {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
609 /* begin new unquoted field */
34409
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
610 if (dialect->quoting == QUOTE_NONNUMERIC)
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
611 self->numeric_field = 1;
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
612 if (parse_add_char(self, c) < 0)
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
613 return -1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
614 self->state = IN_FIELD;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
615 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
616 break;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
617
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
618 case ESCAPED_CHAR:
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
619 if (c == '\0')
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
620 c = '\n';
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
621 if (parse_add_char(self, c) < 0)
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
622 return -1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
623 self->state = IN_FIELD;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
624 break;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
625
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
626 case IN_FIELD:
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
627 /* in unquoted field */
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
628 if (c == '\n' || c == '\r' || c == '\0') {
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
629 /* end of line - return [fields] */
34409
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
630 if (parse_save_field(self) < 0)
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
631 return -1;
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
632 self->state = (c == '\0' ? START_RECORD : EAT_CRNL);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
633 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
634 else if (c == dialect->escapechar) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
635 /* possible escaped character */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
636 self->state = ESCAPED_CHAR;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
637 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
638 else if (c == dialect->delimiter) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
639 /* save field - wait for new field */
34409
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
640 if (parse_save_field(self) < 0)
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
641 return -1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
642 self->state = START_FIELD;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
643 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
644 else {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
645 /* normal character - save in field */
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
646 if (parse_add_char(self, c) < 0)
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
647 return -1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
648 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
649 break;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
650
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
651 case IN_QUOTED_FIELD:
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
652 /* in quoted field */
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
653 if (c == '\0')
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
654 ;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
655 else if (c == dialect->escapechar) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
656 /* Possible escape character */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
657 self->state = ESCAPE_IN_QUOTED_FIELD;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
658 }
34340
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
659 else if (c == dialect->quotechar &&
be4929e5179b Improved the implementation of the internal "dialect" type. The new
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34331
diff changeset
660 dialect->quoting != QUOTE_NONE) {
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
661 if (dialect->doublequote) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
662 /* doublequote; " represented by "" */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
663 self->state = QUOTE_IN_QUOTED_FIELD;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
664 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
665 else {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
666 /* end of quote part of field */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
667 self->state = IN_FIELD;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
668 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
669 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
670 else {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
671 /* normal character - save in field */
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
672 if (parse_add_char(self, c) < 0)
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
673 return -1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
674 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
675 break;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
676
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
677 case ESCAPE_IN_QUOTED_FIELD:
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
678 if (c == '\0')
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
679 c = '\n';
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
680 if (parse_add_char(self, c) < 0)
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
681 return -1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
682 self->state = IN_QUOTED_FIELD;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
683 break;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
684
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
685 case QUOTE_IN_QUOTED_FIELD:
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
686 /* doublequote - seen a quote in an quoted field */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
687 if (dialect->quoting != QUOTE_NONE &&
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
688 c == dialect->quotechar) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
689 /* save "" as " */
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
690 if (parse_add_char(self, c) < 0)
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
691 return -1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
692 self->state = IN_QUOTED_FIELD;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
693 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
694 else if (c == dialect->delimiter) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
695 /* save field - wait for new field */
34409
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
696 if (parse_save_field(self) < 0)
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
697 return -1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
698 self->state = START_FIELD;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
699 }
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
700 else if (c == '\n' || c == '\r' || c == '\0') {
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
701 /* end of line - return [fields] */
34409
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
702 if (parse_save_field(self) < 0)
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
703 return -1;
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
704 self->state = (c == '\0' ? START_RECORD : EAT_CRNL);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
705 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
706 else if (!dialect->strict) {
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
707 if (parse_add_char(self, c) < 0)
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
708 return -1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
709 self->state = IN_FIELD;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
710 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
711 else {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
712 /* illegal */
34411
02f398fbb0f0 Improve wording of parser error message.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34410
diff changeset
713 PyErr_Format(error_obj, "'%c' expected after '%c'",
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
714 dialect->delimiter,
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
715 dialect->quotechar);
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
716 return -1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
717 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
718 break;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
719
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
720 case EAT_CRNL:
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
721 if (c == '\n' || c == '\r')
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
722 ;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
723 else if (c == '\0')
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
724 self->state = START_RECORD;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
725 else {
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
726 PyErr_Format(error_obj, "new-line character seen in unquoted field - do you need to open the file in universal-newline mode?");
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
727 return -1;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
728 }
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
729 break;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
730
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
731 }
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
732 return 0;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
733 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
734
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
735 static int
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
736 parse_reset(ReaderObj *self)
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
737 {
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
738 Py_XDECREF(self->fields);
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
739 self->fields = PyList_New(0);
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
740 if (self->fields == NULL)
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
741 return -1;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
742 self->field_len = 0;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
743 self->state = START_RECORD;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
744 self->numeric_field = 0;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
745 return 0;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
746 }
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
747
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
748 static PyObject *
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
749 Reader_iternext(ReaderObj *self)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
750 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
751 PyObject *lineobj;
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
752 PyObject *fields = NULL;
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
753 Py_UNICODE *line, c;
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
754 Py_ssize_t linelen;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
755
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
756 if (parse_reset(self) < 0)
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
757 return NULL;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
758 do {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
759 lineobj = PyIter_Next(self->input_iter);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
760 if (lineobj == NULL) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
761 /* End of input OR exception */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
762 if (!PyErr_Occurred() && self->field_len != 0)
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
763 PyErr_Format(error_obj,
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
764 "newline inside string");
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
765 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
766 }
44019
7b30111247f6 #1479: Fix refleak in csv module (from rev 59062).
Georg Brandl <georg@python.org>
parents: 43918
diff changeset
767 if (!PyUnicode_Check(lineobj)) {
44032
8f92b4ee6fc4 Rewrap error message.
Georg Brandl <georg@python.org>
parents: 44019
diff changeset
768 PyErr_Format(error_obj,
8f92b4ee6fc4 Rewrap error message.
Georg Brandl <georg@python.org>
parents: 44019
diff changeset
769 "iterator should return strings, "
8f92b4ee6fc4 Rewrap error message.
Georg Brandl <georg@python.org>
parents: 44019
diff changeset
770 "not %.200s "
8f92b4ee6fc4 Rewrap error message.
Georg Brandl <georg@python.org>
parents: 44019
diff changeset
771 "(did you open the file in text mode?)",
43918
146ba97ffe1a Correct test_cvs on Windows, as suggested by Raghuram Devarakonda
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43866
diff changeset
772 lineobj->ob_type->tp_name
146ba97ffe1a Correct test_cvs on Windows, as suggested by Raghuram Devarakonda
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43866
diff changeset
773 );
44019
7b30111247f6 #1479: Fix refleak in csv module (from rev 59062).
Georg Brandl <georg@python.org>
parents: 43918
diff changeset
774 Py_DECREF(lineobj);
43918
146ba97ffe1a Correct test_cvs on Windows, as suggested by Raghuram Devarakonda
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43866
diff changeset
775 return NULL;
146ba97ffe1a Correct test_cvs on Windows, as suggested by Raghuram Devarakonda
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43866
diff changeset
776 }
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
777 ++self->line_num;
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
778 line = PyUnicode_AsUnicode(lineobj);
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
779 linelen = PyUnicode_GetSize(lineobj);
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
780 if (line == NULL || linelen < 0) {
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
781 Py_DECREF(lineobj);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
782 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
783 }
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
784 while (linelen--) {
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
785 c = *line++;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
786 if (c == '\0') {
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
787 Py_DECREF(lineobj);
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
788 PyErr_Format(error_obj,
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
789 "line contains NULL byte");
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
790 goto err;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
791 }
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
792 if (parse_process_char(self, c) < 0) {
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
793 Py_DECREF(lineobj);
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
794 goto err;
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
795 }
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
796 }
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
797 Py_DECREF(lineobj);
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
798 if (parse_process_char(self, 0) < 0)
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
799 goto err;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
800 } while (self->state != START_RECORD);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
801
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
802 fields = self->fields;
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
803 self->fields = NULL;
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
804 err:
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
805 return fields;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
806 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
807
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
808 static void
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
809 Reader_dealloc(ReaderObj *self)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
810 {
34381
4bd180d171cc Add missing PyObject_GC_Track and PyObject_GC_UnTrack calls to csv reader and
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34380
diff changeset
811 PyObject_GC_UnTrack(self);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
812 Py_XDECREF(self->dialect);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
813 Py_XDECREF(self->input_iter);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
814 Py_XDECREF(self->fields);
29389
857428cb418c Fix potential leaks identified by Neal Norwitz.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 28599
diff changeset
815 if (self->field != NULL)
857428cb418c Fix potential leaks identified by Neal Norwitz.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 28599
diff changeset
816 PyMem_Free(self->field);
28599
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
817 PyObject_GC_Del(self);
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
818 }
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
819
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
820 static int
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
821 Reader_traverse(ReaderObj *self, visitproc visit, void *arg)
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
822 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37144
diff changeset
823 Py_VISIT(self->dialect);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37144
diff changeset
824 Py_VISIT(self->input_iter);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37144
diff changeset
825 Py_VISIT(self->fields);
28599
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
826 return 0;
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
827 }
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
828
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
829 static int
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
830 Reader_clear(ReaderObj *self)
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
831 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37144
diff changeset
832 Py_CLEAR(self->dialect);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37144
diff changeset
833 Py_CLEAR(self->input_iter);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37144
diff changeset
834 Py_CLEAR(self->fields);
28599
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
835 return 0;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
836 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
837
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
838 PyDoc_STRVAR(Reader_Type_doc,
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
839 "CSV reader\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
840 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
841 "Reader objects are responsible for reading and parsing tabular data\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
842 "in CSV format.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
843 );
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
844
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
845 static struct PyMethodDef Reader_methods[] = {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
846 { NULL, NULL }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
847 };
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
848 #define R_OFF(x) offsetof(ReaderObj, x)
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
849
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
850 static struct PyMemberDef Reader_memberlist[] = {
42455
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
851 { "dialect", T_OBJECT, R_OFF(dialect), READONLY },
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
852 { "line_num", T_ULONG, R_OFF(line_num), READONLY },
34418
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
853 { NULL }
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
854 };
fa65e3425a4c Moved reader \r and \n processing from the iterator to the state machine -
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34411
diff changeset
855
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
856
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
857 static PyTypeObject Reader_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41684
diff changeset
858 PyVarObject_HEAD_INIT(NULL, 0)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
859 "_csv.reader", /*tp_name*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
860 sizeof(ReaderObj), /*tp_basicsize*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
861 0, /*tp_itemsize*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
862 /* methods */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
863 (destructor)Reader_dealloc, /*tp_dealloc*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
864 (printfunc)0, /*tp_print*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
865 (getattrfunc)0, /*tp_getattr*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
866 (setattrfunc)0, /*tp_setattr*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
867 (cmpfunc)0, /*tp_compare*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
868 (reprfunc)0, /*tp_repr*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
869 0, /*tp_as_number*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
870 0, /*tp_as_sequence*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
871 0, /*tp_as_mapping*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
872 (hashfunc)0, /*tp_hash*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
873 (ternaryfunc)0, /*tp_call*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
874 (reprfunc)0, /*tp_str*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
875 0, /*tp_getattro*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
876 0, /*tp_setattro*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
877 0, /*tp_as_buffer*/
28599
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
878 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
879 Py_TPFLAGS_HAVE_GC, /*tp_flags*/
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
880 Reader_Type_doc, /*tp_doc*/
28599
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
881 (traverseproc)Reader_traverse, /*tp_traverse*/
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
882 (inquiry)Reader_clear, /*tp_clear*/
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
883 0, /*tp_richcompare*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
884 0, /*tp_weaklistoffset*/
34331
3fc8ece9d2b8 Delete Reader_getiter and replace with PyObject_SelfIter.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 33134
diff changeset
885 PyObject_SelfIter, /*tp_iter*/
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
886 (getiterfunc)Reader_iternext, /*tp_iternext*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
887 Reader_methods, /*tp_methods*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
888 Reader_memberlist, /*tp_members*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
889 0, /*tp_getset*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
890
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
891 };
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
892
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
893 static PyObject *
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
894 csv_reader(PyObject *module, PyObject *args, PyObject *keyword_args)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
895 {
34390
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
896 PyObject * iterator, * dialect = NULL;
28599
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
897 ReaderObj * self = PyObject_GC_New(ReaderObj, &Reader_Type);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
898
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
899 if (!self)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
900 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
901
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
902 self->dialect = NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
903 self->fields = NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
904 self->input_iter = NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
905 self->field = NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
906 self->field_size = 0;
34410
a70dd8767b07 Add counting of source iterator lines to the reader object - handy for
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34409
diff changeset
907 self->line_num = 0;
34409
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
908
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
909 if (parse_reset(self) < 0) {
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
910 Py_DECREF(self);
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
911 return NULL;
1d866c02a8ca When quoting=QUOTE_NONNUMERIC, the reader now casts unquoted fields
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34408
diff changeset
912 }
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
913
32389
f9569526e8d4 Use PyArg_UnpackTuple() where possible.
Raymond Hettinger <python@rcn.com>
parents: 31074
diff changeset
914 if (!PyArg_UnpackTuple(args, "", 1, 2, &iterator, &dialect)) {
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
915 Py_DECREF(self);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
916 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
917 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
918 self->input_iter = PyObject_GetIter(iterator);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
919 if (self->input_iter == NULL) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
920 PyErr_SetString(PyExc_TypeError,
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
921 "argument 1 must be an iterator");
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
922 Py_DECREF(self);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
923 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
924 }
34390
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
925 self->dialect = (DialectObj *)_call_dialect(dialect, keyword_args);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
926 if (self->dialect == NULL) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
927 Py_DECREF(self);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
928 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
929 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
930
34381
4bd180d171cc Add missing PyObject_GC_Track and PyObject_GC_UnTrack calls to csv reader and
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34380
diff changeset
931 PyObject_GC_Track(self);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
932 return (PyObject *)self;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
933 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
934
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
935 /*
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
936 * WRITER
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
937 */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
938 /* ---------------------------------------------------------------- */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
939 static void
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
940 join_reset(WriterObj *self)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
941 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
942 self->rec_len = 0;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
943 self->num_fields = 0;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
944 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
945
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
946 #define MEM_INCR 32768
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
947
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
948 /* Calculate new record length or append field to record. Return new
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
949 * record length.
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
950 */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
951 static int
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
952 join_append_data(WriterObj *self, Py_UNICODE *field, int quote_empty,
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
953 int *quoted, int copy_phase)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
954 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
955 DialectObj *dialect = self->dialect;
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
956 int i;
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
957 int rec_len;
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
958 Py_UNICODE *lineterm;
34407
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
959
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
960 #define ADDCH(c) \
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
961 do {\
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
962 if (copy_phase) \
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
963 self->rec[rec_len] = c;\
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
964 rec_len++;\
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
965 } while(0)
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
966
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
967 lineterm = PyUnicode_AsUnicode(dialect->lineterminator);
34407
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
968 if (lineterm == NULL)
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
969 return -1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
970
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
971 rec_len = self->rec_len;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
972
34407
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
973 /* If this is not the first field we need a field separator */
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
974 if (self->num_fields > 0)
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
975 ADDCH(dialect->delimiter);
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
976
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
977 /* Handle preceding quote */
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
978 if (copy_phase && *quoted)
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
979 ADDCH(dialect->quotechar);
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
980
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
981 /* Copy/count field data */
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
982 /* If field is null just pass over */
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
983 for (i = 0; field; i++) {
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
984 Py_UNICODE c = field[i];
34407
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
985 int want_escape = 0;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
986
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
987 if (c == '\0')
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
988 break;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
989
34407
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
990 if (c == dialect->delimiter ||
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
991 c == dialect->escapechar ||
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
992 c == dialect->quotechar ||
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
993 Py_UNICODE_strchr(lineterm, c)) {
34407
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
994 if (dialect->quoting == QUOTE_NONE)
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
995 want_escape = 1;
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
996 else {
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
997 if (c == dialect->quotechar) {
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
998 if (dialect->doublequote)
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
999 ADDCH(dialect->quotechar);
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1000 else
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1001 want_escape = 1;
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1002 }
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1003 if (!want_escape)
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1004 *quoted = 1;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1005 }
34407
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1006 if (want_escape) {
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1007 if (!dialect->escapechar) {
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1008 PyErr_Format(error_obj,
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1009 "need to escape, but no escapechar set");
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1010 return -1;
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1011 }
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1012 ADDCH(dialect->escapechar);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1013 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1014 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1015 /* Copy field character into record buffer.
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1016 */
34407
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1017 ADDCH(c);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1018 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1019
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1020 /* If field is empty check if it needs to be quoted.
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1021 */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1022 if (i == 0 && quote_empty) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1023 if (dialect->quoting == QUOTE_NONE) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1024 PyErr_Format(error_obj,
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1025 "single empty field record must be quoted");
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1026 return -1;
34341
f80bb8aff419 Fix to use PEP7 brace style.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34340
diff changeset
1027 }
f80bb8aff419 Fix to use PEP7 brace style.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34340
diff changeset
1028 else
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1029 *quoted = 1;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1030 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1031
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1032 if (*quoted) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1033 if (copy_phase)
34407
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1034 ADDCH(dialect->quotechar);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1035 else
34407
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1036 rec_len += 2;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1037 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1038 return rec_len;
34407
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1039 #undef ADDCH
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1040 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1041
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1042 static int
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1043 join_check_rec_size(WriterObj *self, int rec_len)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1044 {
48330
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1045
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1046 if (rec_len < 0 || rec_len > INT_MAX - MEM_INCR) {
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1047 PyErr_NoMemory();
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1048 return 0;
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1049 }
ac854cde3135 Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
1050
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1051 if (rec_len > self->rec_size) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1052 if (self->rec_size == 0) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1053 self->rec_size = (rec_len / MEM_INCR + 1) * MEM_INCR;
29389
857428cb418c Fix potential leaks identified by Neal Norwitz.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 28599
diff changeset
1054 if (self->rec != NULL)
857428cb418c Fix potential leaks identified by Neal Norwitz.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 28599
diff changeset
1055 PyMem_Free(self->rec);
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1056 self->rec = PyMem_New(Py_UNICODE, self->rec_size);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1057 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1058 else {
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1059 Py_UNICODE* old_rec = self->rec;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1060
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1061 self->rec_size = (rec_len / MEM_INCR + 1) * MEM_INCR;
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1062 self->rec = PyMem_Resize(self->rec, Py_UNICODE,
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1063 self->rec_size);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1064 if (self->rec == NULL)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1065 PyMem_Free(old_rec);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1066 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1067 if (self->rec == NULL) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1068 PyErr_NoMemory();
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1069 return 0;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1070 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1071 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1072 return 1;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1073 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1074
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1075 static int
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1076 join_append(WriterObj *self, Py_UNICODE *field, int *quoted, int quote_empty)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1077 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1078 int rec_len;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1079
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1080 rec_len = join_append_data(self, field, quote_empty, quoted, 0);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1081 if (rec_len < 0)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1082 return 0;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1083
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1084 /* grow record buffer if necessary */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1085 if (!join_check_rec_size(self, rec_len))
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1086 return 0;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1087
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1088 self->rec_len = join_append_data(self, field, quote_empty, quoted, 1);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1089 self->num_fields++;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1090
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1091 return 1;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1092 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1093
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1094 static int
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1095 join_append_lineterminator(WriterObj *self)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1096 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1097 int terminator_len;
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1098 Py_UNICODE *terminator;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1099
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1100 terminator_len = PyUnicode_GetSize(self->dialect->lineterminator);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1101 if (terminator_len == -1)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1102 return 0;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1103
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1104 /* grow record buffer if necessary */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1105 if (!join_check_rec_size(self, self->rec_len + terminator_len))
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1106 return 0;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1107
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1108 terminator = PyUnicode_AsUnicode(self->dialect->lineterminator);
34403
13a85806ba48 Add belt and braces check of PyString_AsString return.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34396
diff changeset
1109 if (terminator == NULL)
13a85806ba48 Add belt and braces check of PyString_AsString return.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34396
diff changeset
1110 return 0;
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1111 memmove(self->rec + self->rec_len, terminator,
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1112 sizeof(Py_UNICODE)*terminator_len);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1113 self->rec_len += terminator_len;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1114
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1115 return 1;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1116 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1117
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1118 PyDoc_STRVAR(csv_writerow_doc,
28590
95b6dd04da8f add writerows docstring
Skip Montanaro <skip@pobox.com>
parents: 28589
diff changeset
1119 "writerow(sequence)\n"
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1120 "\n"
28590
95b6dd04da8f add writerows docstring
Skip Montanaro <skip@pobox.com>
parents: 28589
diff changeset
1121 "Construct and write a CSV record from a sequence of fields. Non-string\n"
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1122 "elements will be converted to string.");
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1123
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1124 static PyObject *
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1125 csv_writerow(WriterObj *self, PyObject *seq)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1126 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1127 DialectObj *dialect = self->dialect;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1128 int len, i;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1129
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1130 if (!PySequence_Check(seq))
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1131 return PyErr_Format(error_obj, "sequence expected");
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1132
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1133 len = PySequence_Length(seq);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1134 if (len < 0)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1135 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1136
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1137 /* Join all fields in internal buffer.
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1138 */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1139 join_reset(self);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1140 for (i = 0; i < len; i++) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1141 PyObject *field;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1142 int append_ok;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1143 int quoted;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1144
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1145 field = PySequence_GetItem(seq, i);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1146 if (field == NULL)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1147 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1148
34407
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1149 switch (dialect->quoting) {
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1150 case QUOTE_NONNUMERIC:
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1151 quoted = !PyNumber_Check(field);
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1152 break;
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1153 case QUOTE_ALL:
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1154 quoted = 1;
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1155 break;
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1156 default:
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1157 quoted = 0;
c88ab292bf05 When using QUOTE_NONNUMERIC, we now test for "numericness" with
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34406
diff changeset
1158 break;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1159 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1160
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1161 if (PyUnicode_Check(field)) {
28591
883a694795f5 tighten up string checks
Skip Montanaro <skip@pobox.com>
parents: 28590
diff changeset
1162 append_ok = join_append(self,
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1163 PyUnicode_AS_UNICODE(field),
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1164 &quoted, len == 1);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1165 Py_DECREF(field);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1166 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1167 else if (field == Py_None) {
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1168 append_ok = join_append(self, NULL,
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1169 &quoted, len == 1);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1170 Py_DECREF(field);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1171 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1172 else {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1173 PyObject *str;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1174
43866
8ed0280c2ab6 Replace PyObject_Unicode with PyObject_Str everywhere, and remove the
Thomas Heller <theller@ctypes.org>
parents: 43577
diff changeset
1175 str = PyObject_Str(field);
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1176 Py_DECREF(field);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1177 if (str == NULL)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1178 return NULL;
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1179 append_ok = join_append(self,
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1180 PyUnicode_AS_UNICODE(str),
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1181 &quoted, len == 1);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1182 Py_DECREF(str);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1183 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1184 if (!append_ok)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1185 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1186 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1187
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1188 /* Add line terminator.
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1189 */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1190 if (!join_append_lineterminator(self))
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1191 return 0;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1192
42462
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1193 return PyObject_CallFunction(self->writeline,
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1194 "(u#)", self->rec,
8b45f2297dbd SF patch# 1767398 by Adam Hupp.
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1195 self->rec_len);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1196 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1197
28590
95b6dd04da8f add writerows docstring
Skip Montanaro <skip@pobox.com>
parents: 28589
diff changeset
1198 PyDoc_STRVAR(csv_writerows_doc,
95b6dd04da8f add writerows docstring
Skip Montanaro <skip@pobox.com>
parents: 28589
diff changeset
1199 "writerows(sequence of sequences)\n"
95b6dd04da8f add writerows docstring
Skip Montanaro <skip@pobox.com>
parents: 28589
diff changeset
1200 "\n"
95b6dd04da8f add writerows docstring
Skip Montanaro <skip@pobox.com>
parents: 28589
diff changeset
1201 "Construct and write a series of sequences to a csv file. Non-string\n"
95b6dd04da8f add writerows docstring
Skip Montanaro <skip@pobox.com>
parents: 28589
diff changeset
1202 "elements will be converted to string.");
95b6dd04da8f add writerows docstring
Skip Montanaro <skip@pobox.com>
parents: 28589
diff changeset
1203
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1204 static PyObject *
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1205 csv_writerows(WriterObj *self, PyObject *seqseq)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1206 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1207 PyObject *row_iter, *row_obj, *result;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1208
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1209 row_iter = PyObject_GetIter(seqseq);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1210 if (row_iter == NULL) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1211 PyErr_SetString(PyExc_TypeError,
28589
Skip Montanaro <skip@pobox.com>
parents: 28588
diff changeset
1212 "writerows() argument must be iterable");
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1213 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1214 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1215 while ((row_obj = PyIter_Next(row_iter))) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1216 result = csv_writerow(self, row_obj);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1217 Py_DECREF(row_obj);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1218 if (!result) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1219 Py_DECREF(row_iter);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1220 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1221 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1222 else
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1223 Py_DECREF(result);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1224 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1225 Py_DECREF(row_iter);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1226 if (PyErr_Occurred())
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1227 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1228 Py_INCREF(Py_None);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1229 return Py_None;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1230 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1231
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1232 static struct PyMethodDef Writer_methods[] = {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1233 { "writerow", (PyCFunction)csv_writerow, METH_O, csv_writerow_doc},
28590
95b6dd04da8f add writerows docstring
Skip Montanaro <skip@pobox.com>
parents: 28589
diff changeset
1234 { "writerows", (PyCFunction)csv_writerows, METH_O, csv_writerows_doc},
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1235 { NULL, NULL }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1236 };
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1237
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1238 #define W_OFF(x) offsetof(WriterObj, x)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1239
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1240 static struct PyMemberDef Writer_memberlist[] = {
42455
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1241 { "dialect", T_OBJECT, W_OFF(dialect), READONLY },
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1242 { NULL }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1243 };
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1244
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1245 static void
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1246 Writer_dealloc(WriterObj *self)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1247 {
34381
4bd180d171cc Add missing PyObject_GC_Track and PyObject_GC_UnTrack calls to csv reader and
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34380
diff changeset
1248 PyObject_GC_UnTrack(self);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1249 Py_XDECREF(self->dialect);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1250 Py_XDECREF(self->writeline);
29389
857428cb418c Fix potential leaks identified by Neal Norwitz.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 28599
diff changeset
1251 if (self->rec != NULL)
857428cb418c Fix potential leaks identified by Neal Norwitz.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 28599
diff changeset
1252 PyMem_Free(self->rec);
28599
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1253 PyObject_GC_Del(self);
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1254 }
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1255
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1256 static int
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1257 Writer_traverse(WriterObj *self, visitproc visit, void *arg)
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1258 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37144
diff changeset
1259 Py_VISIT(self->dialect);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37144
diff changeset
1260 Py_VISIT(self->writeline);
28599
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1261 return 0;
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1262 }
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1263
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1264 static int
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1265 Writer_clear(WriterObj *self)
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1266 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37144
diff changeset
1267 Py_CLEAR(self->dialect);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37144
diff changeset
1268 Py_CLEAR(self->writeline);
28599
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1269 return 0;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1270 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1271
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1272 PyDoc_STRVAR(Writer_Type_doc,
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1273 "CSV writer\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1274 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1275 "Writer objects are responsible for generating tabular data\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1276 "in CSV format from sequence input.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1277 );
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1278
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1279 static PyTypeObject Writer_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41684
diff changeset
1280 PyVarObject_HEAD_INIT(NULL, 0)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1281 "_csv.writer", /*tp_name*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1282 sizeof(WriterObj), /*tp_basicsize*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1283 0, /*tp_itemsize*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1284 /* methods */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1285 (destructor)Writer_dealloc, /*tp_dealloc*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1286 (printfunc)0, /*tp_print*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1287 (getattrfunc)0, /*tp_getattr*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1288 (setattrfunc)0, /*tp_setattr*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1289 (cmpfunc)0, /*tp_compare*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1290 (reprfunc)0, /*tp_repr*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1291 0, /*tp_as_number*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1292 0, /*tp_as_sequence*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1293 0, /*tp_as_mapping*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1294 (hashfunc)0, /*tp_hash*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1295 (ternaryfunc)0, /*tp_call*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1296 (reprfunc)0, /*tp_str*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1297 0, /*tp_getattro*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1298 0, /*tp_setattro*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1299 0, /*tp_as_buffer*/
28599
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1300 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1301 Py_TPFLAGS_HAVE_GC, /*tp_flags*/
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1302 Writer_Type_doc,
28599
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1303 (traverseproc)Writer_traverse, /*tp_traverse*/
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1304 (inquiry)Writer_clear, /*tp_clear*/
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1305 0, /*tp_richcompare*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1306 0, /*tp_weaklistoffset*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1307 (getiterfunc)0, /*tp_iter*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1308 (getiterfunc)0, /*tp_iternext*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1309 Writer_methods, /*tp_methods*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1310 Writer_memberlist, /*tp_members*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1311 0, /*tp_getset*/
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1312 };
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1313
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1314 static PyObject *
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1315 csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1316 {
34390
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
1317 PyObject * output_file, * dialect = NULL;
28599
11660f4361f8 Make readers and writers participate in garbage collection.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28594
diff changeset
1318 WriterObj * self = PyObject_GC_New(WriterObj, &Writer_Type);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1319
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1320 if (!self)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1321 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1322
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1323 self->dialect = NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1324 self->writeline = NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1325
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1326 self->rec = NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1327 self->rec_size = 0;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1328 self->rec_len = 0;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1329 self->num_fields = 0;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1330
32389
f9569526e8d4 Use PyArg_UnpackTuple() where possible.
Raymond Hettinger <python@rcn.com>
parents: 31074
diff changeset
1331 if (!PyArg_UnpackTuple(args, "", 1, 2, &output_file, &dialect)) {
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1332 Py_DECREF(self);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1333 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1334 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1335 self->writeline = PyObject_GetAttrString(output_file, "write");
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1336 if (self->writeline == NULL || !PyCallable_Check(self->writeline)) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1337 PyErr_SetString(PyExc_TypeError,
34411
02f398fbb0f0 Improve wording of parser error message.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34410
diff changeset
1338 "argument 1 must have a \"write\" method");
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1339 Py_DECREF(self);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1340 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1341 }
34390
28f84938468d Factor out the code for making a dialect instance.
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34389
diff changeset
1342 self->dialect = (DialectObj *)_call_dialect(dialect, keyword_args);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1343 if (self->dialect == NULL) {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1344 Py_DECREF(self);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1345 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1346 }
34381
4bd180d171cc Add missing PyObject_GC_Track and PyObject_GC_UnTrack calls to csv reader and
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34380
diff changeset
1347 PyObject_GC_Track(self);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1348 return (PyObject *)self;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1349 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1350
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1351 /*
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1352 * DIALECT REGISTRY
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1353 */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1354 static PyObject *
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1355 csv_list_dialects(PyObject *module, PyObject *args)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1356 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1357 return PyDict_Keys(dialects);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1358 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1359
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1360 static PyObject *
34391
0a1608a5eb05 Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34390
diff changeset
1361 csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1362 {
34391
0a1608a5eb05 Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34390
diff changeset
1363 PyObject *name_obj, *dialect_obj = NULL;
0a1608a5eb05 Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34390
diff changeset
1364 PyObject *dialect;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1365
34391
0a1608a5eb05 Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34390
diff changeset
1366 if (!PyArg_UnpackTuple(args, "", 1, 2, &name_obj, &dialect_obj))
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1367 return NULL;
34385
3533e3cb7b97 Where a string is desired, test for PyBaseString_Type derived type,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34381
diff changeset
1368 if (!IS_BASESTRING(name_obj)) {
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1369 PyErr_SetString(PyExc_TypeError,
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1370 "dialect name must be a string or unicode");
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1371 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1372 }
34391
0a1608a5eb05 Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34390
diff changeset
1373 dialect = _call_dialect(dialect_obj, kwargs);
0a1608a5eb05 Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34390
diff changeset
1374 if (dialect == NULL)
0a1608a5eb05 Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34390
diff changeset
1375 return NULL;
0a1608a5eb05 Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34390
diff changeset
1376 if (PyDict_SetItem(dialects, name_obj, dialect) < 0) {
0a1608a5eb05 Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34390
diff changeset
1377 Py_DECREF(dialect);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1378 return NULL;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1379 }
34391
0a1608a5eb05 Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34390
diff changeset
1380 Py_DECREF(dialect);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1381 Py_INCREF(Py_None);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1382 return Py_None;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1383 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1384
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1385 static PyObject *
28591
883a694795f5 tighten up string checks
Skip Montanaro <skip@pobox.com>
parents: 28590
diff changeset
1386 csv_unregister_dialect(PyObject *module, PyObject *name_obj)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1387 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1388 if (PyDict_DelItem(dialects, name_obj) < 0)
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1389 return PyErr_Format(error_obj, "unknown dialect");
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1390 Py_INCREF(Py_None);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1391 return Py_None;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1392 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1393
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1394 static PyObject *
28591
883a694795f5 tighten up string checks
Skip Montanaro <skip@pobox.com>
parents: 28590
diff changeset
1395 csv_get_dialect(PyObject *module, PyObject *name_obj)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1396 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1397 return get_dialect_from_registry(name_obj);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1398 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1399
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1400 static PyObject *
34406
b93845ce4454 Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34403
diff changeset
1401 csv_field_size_limit(PyObject *module, PyObject *args)
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1402 {
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1403 PyObject *new_limit = NULL;
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1404 long old_limit = field_limit;
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1405
34406
b93845ce4454 Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34403
diff changeset
1406 if (!PyArg_UnpackTuple(args, "field_size_limit", 0, 1, &new_limit))
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1407 return NULL;
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1408 if (new_limit != NULL) {
44158
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1409 if (!PyLong_CheckExact(new_limit)) {
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1410 PyErr_Format(PyExc_TypeError,
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1411 "limit must be an integer");
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1412 return NULL;
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1413 }
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44046
diff changeset
1414 field_limit = PyLong_AsLong(new_limit);
44158
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1415 if (field_limit == -1 && PyErr_Occurred()) {
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1416 field_limit = old_limit;
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1417 return NULL;
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1418 }
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1419 }
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44046
diff changeset
1420 return PyLong_FromLong(old_limit);
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1421 }
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1422
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1423 /*
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1424 * MODULE
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1425 */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1426
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1427 PyDoc_STRVAR(csv_module_doc,
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1428 "CSV parsing and writing.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1429 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1430 "This module provides classes that assist in the reading and writing\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1431 "of Comma Separated Value (CSV) files, and implements the interface\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1432 "described by PEP 305. Although many CSV files are simple to parse,\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1433 "the format is not formally defined by a stable specification and\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1434 "is subtle enough that parsing lines of a CSV file with something\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1435 "like line.split(\",\") is bound to fail. The module supports three\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1436 "basic APIs: reading, writing, and registration of dialects.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1437 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1438 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1439 "DIALECT REGISTRATION:\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1440 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1441 "Readers and writers support a dialect argument, which is a convenient\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1442 "handle on a group of settings. When the dialect argument is a string,\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1443 "it identifies one of the dialects previously registered with the module.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1444 "If it is a class or instance, the attributes of the argument are used as\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1445 "the settings for the reader or writer:\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1446 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1447 " class excel:\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1448 " delimiter = ','\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1449 " quotechar = '\"'\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1450 " escapechar = None\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1451 " doublequote = True\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1452 " skipinitialspace = False\n"
33134
fc1637fc4588 Quote \r\n correctly, remove random indentation (patch #1009384). Thanks
Johannes Gijsbers <jlg@dds.nl>
parents: 32389
diff changeset
1453 " lineterminator = '\\r\\n'\n"
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1454 " quoting = QUOTE_MINIMAL\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1455 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1456 "SETTINGS:\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1457 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1458 " * quotechar - specifies a one-character string to use as the \n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1459 " quoting character. It defaults to '\"'.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1460 " * delimiter - specifies a one-character string to use as the \n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1461 " field separator. It defaults to ','.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1462 " * skipinitialspace - specifies how to interpret whitespace which\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1463 " immediately follows a delimiter. It defaults to False, which\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1464 " means that whitespace immediately following a delimiter is part\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1465 " of the following field.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1466 " * lineterminator - specifies the character sequence which should \n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1467 " terminate rows.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1468 " * quoting - controls when quotes should be generated by the writer.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1469 " It can take on any of the following module constants:\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1470 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1471 " csv.QUOTE_MINIMAL means only when required, for example, when a\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1472 " field contains either the quotechar or the delimiter\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1473 " csv.QUOTE_ALL means that quotes are always placed around fields.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1474 " csv.QUOTE_NONNUMERIC means that quotes are always placed around\n"
31074
48695c839d98 doc nit
Skip Montanaro <skip@pobox.com>
parents: 29389
diff changeset
1475 " fields which do not parse as integers or floating point\n"
48695c839d98 doc nit
Skip Montanaro <skip@pobox.com>
parents: 29389
diff changeset
1476 " numbers.\n"
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1477 " csv.QUOTE_NONE means that quotes are never placed around fields.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1478 " * escapechar - specifies a one-character string used to escape \n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1479 " the delimiter when quoting is set to QUOTE_NONE.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1480 " * doublequote - controls the handling of quotes inside fields. When\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1481 " True, two consecutive quotes are interpreted as one during read,\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1482 " and when writing, each quote character embedded in the data is\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1483 " written as two quotes\n");
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1484
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1485 PyDoc_STRVAR(csv_reader_doc,
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1486 " csv_reader = reader(iterable [, dialect='excel']\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1487 " [optional keyword args])\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1488 " for row in csv_reader:\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1489 " process(row)\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1490 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1491 "The \"iterable\" argument can be any object that returns a line\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1492 "of input for each iteration, such as a file object or a list. The\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1493 "optional \"dialect\" parameter is discussed below. The function\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1494 "also accepts optional keyword arguments which override settings\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1495 "provided by the dialect.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1496 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1497 "The returned object is an iterator. Each iteration returns a row\n"
33134
fc1637fc4588 Quote \r\n correctly, remove random indentation (patch #1009384). Thanks
Johannes Gijsbers <jlg@dds.nl>
parents: 32389
diff changeset
1498 "of the CSV file (which can span multiple input lines):\n");
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1499
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1500 PyDoc_STRVAR(csv_writer_doc,
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1501 " csv_writer = csv.writer(fileobj [, dialect='excel']\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1502 " [optional keyword args])\n"
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37144
diff changeset
1503 " for row in sequence:\n"
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1504 " csv_writer.writerow(row)\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1505 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1506 " [or]\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1507 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1508 " csv_writer = csv.writer(fileobj [, dialect='excel']\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1509 " [optional keyword args])\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1510 " csv_writer.writerows(rows)\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1511 "\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1512 "The \"fileobj\" argument can be any object that supports the file API.\n");
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1513
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1514 PyDoc_STRVAR(csv_list_dialects_doc,
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1515 "Return a list of all know dialect names.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1516 " names = csv.list_dialects()");
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1517
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1518 PyDoc_STRVAR(csv_get_dialect_doc,
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1519 "Return the dialect instance associated with name.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1520 " dialect = csv.get_dialect(name)");
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1521
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1522 PyDoc_STRVAR(csv_register_dialect_doc,
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1523 "Create a mapping from a string name to a dialect class.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1524 " dialect = csv.register_dialect(name, dialect)");
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1525
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1526 PyDoc_STRVAR(csv_unregister_dialect_doc,
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1527 "Delete the name/dialect mapping associated with a string name.\n"
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1528 " csv.unregister_dialect(name)");
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1529
34406
b93845ce4454 Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34403
diff changeset
1530 PyDoc_STRVAR(csv_field_size_limit_doc,
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1531 "Sets an upper limit on parsed fields.\n"
34406
b93845ce4454 Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34403
diff changeset
1532 " csv.field_size_limit([limit])\n"
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1533 "\n"
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1534 "Returns old limit. If limit is not given, no new limit is set and\n"
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1535 "the old limit is returned");
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1536
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1537 static struct PyMethodDef csv_methods[] = {
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1538 { "reader", (PyCFunction)csv_reader,
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1539 METH_VARARGS | METH_KEYWORDS, csv_reader_doc},
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1540 { "writer", (PyCFunction)csv_writer,
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1541 METH_VARARGS | METH_KEYWORDS, csv_writer_doc},
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1542 { "list_dialects", (PyCFunction)csv_list_dialects,
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1543 METH_NOARGS, csv_list_dialects_doc},
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1544 { "register_dialect", (PyCFunction)csv_register_dialect,
34391
0a1608a5eb05 Allow dialect-describing keywords to be supplied to register_dialect,
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34390
diff changeset
1545 METH_VARARGS | METH_KEYWORDS, csv_register_dialect_doc},
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1546 { "unregister_dialect", (PyCFunction)csv_unregister_dialect,
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1547 METH_O, csv_unregister_dialect_doc},
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1548 { "get_dialect", (PyCFunction)csv_get_dialect,
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1549 METH_O, csv_get_dialect_doc},
34406
b93845ce4454 Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34403
diff changeset
1550 { "field_size_limit", (PyCFunction)csv_field_size_limit,
b93845ce4454 Rename csv.set_field_limit to csv.field_size_limit (since it both sets and
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34403
diff changeset
1551 METH_VARARGS, csv_field_size_limit_doc},
34396
7059fb8b9eb3 Set an upper limit on the size of the field buffer, raise an exception
Andrew McNamara <andrewm@object-craft.com.au>
parents: 34395
diff changeset
1552 { NULL, NULL }
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1553 };
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1554
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1555
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1556 static struct PyModuleDef _csvmodule = {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1557 PyModuleDef_HEAD_INIT,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1558 "_csv",
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1559 csv_module_doc,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1560 -1,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1561 csv_methods,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1562 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1563 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1564 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1565 NULL
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1566 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1567
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1568 PyMODINIT_FUNC
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1569 PyInit__csv(void)
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1570 {
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1571 PyObject *module;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1572 StyleDesc *style;
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1573
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1574 if (PyType_Ready(&Dialect_Type) < 0)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1575 return NULL;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1576
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1577 if (PyType_Ready(&Reader_Type) < 0)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1578 return NULL;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1579
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1580 if (PyType_Ready(&Writer_Type) < 0)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1581 return NULL;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1582
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1583 /* Create the module and add the functions */
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1584 module = PyModule_Create(&_csvmodule);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1585 if (module == NULL)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1586 return NULL;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1587
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1588 /* Add version to the module. */
28592
ef05f981bc4b use PyModule_Add{Int,String}Constant() where appropriate
Skip Montanaro <skip@pobox.com>
parents: 28591
diff changeset
1589 if (PyModule_AddStringConstant(module, "__version__",
ef05f981bc4b use PyModule_Add{Int,String}Constant() where appropriate
Skip Montanaro <skip@pobox.com>
parents: 28591
diff changeset
1590 MODULE_VERSION) == -1)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1591 return NULL;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1592
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1593 /* Add _dialects dictionary */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1594 dialects = PyDict_New();
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1595 if (dialects == NULL)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1596 return NULL;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1597 if (PyModule_AddObject(module, "_dialects", dialects))
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1598 return NULL;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1599
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1600 /* Add quote styles into dictionary */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1601 for (style = quote_styles; style->name; style++) {
28592
ef05f981bc4b use PyModule_Add{Int,String}Constant() where appropriate
Skip Montanaro <skip@pobox.com>
parents: 28591
diff changeset
1602 if (PyModule_AddIntConstant(module, style->name,
ef05f981bc4b use PyModule_Add{Int,String}Constant() where appropriate
Skip Montanaro <skip@pobox.com>
parents: 28591
diff changeset
1603 style->style) == -1)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1604 return NULL;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1605 }
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1606
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1607 /* Add the Dialect type */
35087
17ccac695413 Michael Hudson pointed out that the Dialect_Type object isn't INCREF'd. Why
Skip Montanaro <skip@pobox.com>
parents: 35081
diff changeset
1608 Py_INCREF(&Dialect_Type);
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1609 if (PyModule_AddObject(module, "Dialect", (PyObject *)&Dialect_Type))
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1610 return NULL;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1611
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1612 /* Add the CSV exception object to the module. */
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1613 error_obj = PyErr_NewException("_csv.Error", NULL, NULL);
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1614 if (error_obj == NULL)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1615 return NULL;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1616 PyModule_AddObject(module, "Error", error_obj);
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
1617 return module;
28339
cc35ed2b26a8 new CSV file processing module - see PEP 305
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
1618 }