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