annotate Modules/_csv.c @ 58765:2ba7677bfa95 legacy-trunk

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