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