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