annotate Modules/_stringio.c @ 52039:bebc4eef9e89

Blocked revisions 70166-70167,70169,70176,70178,70197 via svnmerge ........ r70166 | georg.brandl | 2009-03-04 12:24:41 -0600 (Wed, 04 Mar 2009) | 2 lines Remove obsolete stuff from string module docs. ........ r70167 | ronald.oussoren | 2009-03-04 15:07:19 -0600 (Wed, 04 Mar 2009) | 2 lines Fix issue 5224. ........ r70169 | ronald.oussoren | 2009-03-04 15:12:17 -0600 (Wed, 04 Mar 2009) | 2 lines Fix for issue 5226. ........ r70176 | ronald.oussoren | 2009-03-04 15:35:05 -0600 (Wed, 04 Mar 2009) | 2 lines Fixes issues 3883 and 5194 ........ r70178 | ronald.oussoren | 2009-03-04 16:49:36 -0600 (Wed, 04 Mar 2009) | 2 lines Fix for issue #1113328. ........ r70197 | jesus.cea | 2009-03-05 13:37:37 -0600 (Thu, 05 Mar 2009) | 1 line Minor bsddb documentation glitch ........
author Benjamin Peterson <benjamin@python.org>
date Thu, 05 Mar 2009 22:53:54 +0000
parents 3049ac17e256
children 2a3af211f0e1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
1 #define PY_SSIZE_T_CLEAN
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2 #include "Python.h"
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
3 #include "structmember.h"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
4 #include "_iomodule.h"
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
6 /* Implementation note: the buffer is always at least one character longer
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
7 than the enclosed string, for proper functioning of _PyIO_find_line_ending.
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
8 */
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
9
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
10 typedef struct {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
11 PyObject_HEAD
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
12 Py_UNICODE *buf;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
13 Py_ssize_t pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
14 Py_ssize_t string_size;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
15 size_t buf_size;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
16
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
17 char ok; /* initialized? */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
18 char closed;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
19 char readuniversal;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
20 char readtranslate;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
21 PyObject *decoder;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
22 PyObject *readnl;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
23 PyObject *writenl;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
24
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
25 PyObject *dict;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
26 PyObject *weakreflist;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
27 } StringIOObject;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
28
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
29 #define CHECK_INITIALIZED(self) \
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
30 if (self->ok <= 0) { \
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
31 PyErr_SetString(PyExc_ValueError, \
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
32 "I/O operation on uninitialized object"); \
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
33 return NULL; \
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
34 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
35
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
36 #define CHECK_CLOSED(self) \
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
37 if (self->closed) { \
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
38 PyErr_SetString(PyExc_ValueError, \
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
39 "I/O operation on closed file"); \
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
40 return NULL; \
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
41 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
42
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
43 PyDoc_STRVAR(stringio_doc,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
44 "Text I/O implementation using an in-memory buffer.\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
45 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
46 "The initial_value argument sets the value of object. The newline\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
47 "argument is like the one of TextIOWrapper's constructor.");
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
48
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
49
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
50 /* Internal routine for changing the size, in terms of characters, of the
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
51 buffer of StringIO objects. The caller should ensure that the 'size'
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
52 argument is non-negative. Returns 0 on success, -1 otherwise. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
53 static int
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
54 resize_buffer(StringIOObject *self, size_t size)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
55 {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
56 /* Here, unsigned types are used to avoid dealing with signed integer
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
57 overflow, which is undefined in C. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
58 size_t alloc = self->buf_size;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
59 Py_UNICODE *new_buf = NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
60
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
61 assert(self->buf != NULL);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
62
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
63 /* Reserve one more char for line ending detection. */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
64 size = size + 1;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
65 /* For simplicity, stay in the range of the signed type. Anyway, Python
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
66 doesn't allow strings to be longer than this. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
67 if (size > PY_SSIZE_T_MAX)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
68 goto overflow;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
69
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
70 if (size < alloc / 2) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
71 /* Major downsize; resize down to exact size. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
72 alloc = size + 1;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
73 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
74 else if (size < alloc) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
75 /* Within allocated size; quick exit */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
76 return 0;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
77 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
78 else if (size <= alloc * 1.125) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
79 /* Moderate upsize; overallocate similar to list_resize() */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
80 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
81 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
82 else {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
83 /* Major upsize; resize up to exact size */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
84 alloc = size + 1;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
85 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
86
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
87 if (alloc > ((size_t)-1) / sizeof(Py_UNICODE))
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
88 goto overflow;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
89 new_buf = (Py_UNICODE *)PyMem_Realloc(self->buf,
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
90 alloc * sizeof(Py_UNICODE));
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
91 if (new_buf == NULL) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
92 PyErr_NoMemory();
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
93 return -1;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
94 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
95 self->buf_size = alloc;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
96 self->buf = new_buf;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
97
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
98 return 0;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
99
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
100 overflow:
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
101 PyErr_SetString(PyExc_OverflowError,
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
102 "new buffer size too large");
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
103 return -1;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
104 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
105
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
106 /* Internal routine for writing a whole PyUnicode object to the buffer of a
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
107 StringIO object. Returns 0 on success, or -1 on error. */
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
108 static Py_ssize_t
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
109 write_str(StringIOObject *self, PyObject *obj)
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
110 {
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
111 Py_UNICODE *str;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
112 Py_ssize_t len;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
113 PyObject *decoded = NULL;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
114 assert(self->buf != NULL);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
115 assert(self->pos >= 0);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
116
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
117 if (self->decoder != NULL) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
118 decoded = _PyIncrementalNewlineDecoder_decode(
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
119 self->decoder, obj, 1 /* always final */);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
120 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
121 else {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
122 decoded = obj;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
123 Py_INCREF(decoded);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
124 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
125 if (self->writenl) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
126 PyObject *translated = PyUnicode_Replace(
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
127 decoded, _PyIO_str_nl, self->writenl, -1);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
128 Py_DECREF(decoded);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
129 decoded = translated;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
130 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
131 if (decoded == NULL)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
132 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
133
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
134 assert(PyUnicode_Check(decoded));
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
135 str = PyUnicode_AS_UNICODE(decoded);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
136 len = PyUnicode_GET_SIZE(decoded);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
137
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
138 assert(len >= 0);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
139
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
140 /* This overflow check is not strictly necessary. However, it avoids us to
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
141 deal with funky things like comparing an unsigned and a signed
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
142 integer. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
143 if (self->pos > PY_SSIZE_T_MAX - len) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
144 PyErr_SetString(PyExc_OverflowError,
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
145 "new position too large");
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
146 goto fail;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
147 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
148 if (self->pos + len > self->string_size) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
149 if (resize_buffer(self, self->pos + len) < 0)
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
150 goto fail;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
151 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
152
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
153 if (self->pos > self->string_size) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
154 /* In case of overseek, pad with null bytes the buffer region between
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
155 the end of stream and the current position.
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
156
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
157 0 lo string_size hi
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
158 | |<---used--->|<----------available----------->|
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
159 | | <--to pad-->|<---to write---> |
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
160 0 buf positon
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
161
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
162 */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
163 memset(self->buf + self->string_size, '\0',
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
164 (self->pos - self->string_size) * sizeof(Py_UNICODE));
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
165 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
166
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
167 /* Copy the data to the internal buffer, overwriting some of the
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
168 existing data if self->pos < self->string_size. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
169 memcpy(self->buf + self->pos, str, len * sizeof(Py_UNICODE));
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
170 self->pos += len;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
171
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
172 /* Set the new length of the internal string if it has changed. */
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
173 if (self->string_size < self->pos) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
174 self->string_size = self->pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
175 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
176
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
177 Py_DECREF(decoded);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
178 return 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
179
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
180 fail:
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
181 Py_XDECREF(decoded);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
182 return -1;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
183 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
184
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
185 PyDoc_STRVAR(stringio_getvalue_doc,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
186 "Retrieve the entire contents of the object.");
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
187
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
188 static PyObject *
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
189 stringio_getvalue(StringIOObject *self)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
190 {
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
191 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
192 CHECK_CLOSED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
193 return PyUnicode_FromUnicode(self->buf, self->string_size);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
194 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
195
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
196 PyDoc_STRVAR(stringio_tell_doc,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
197 "Tell the current file position.");
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
198
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
199 static PyObject *
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
200 stringio_tell(StringIOObject *self)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
201 {
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
202 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
203 CHECK_CLOSED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
204 return PyLong_FromSsize_t(self->pos);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
205 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
206
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
207 PyDoc_STRVAR(stringio_read_doc,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
208 "Read at most n characters, returned as a string.\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
209 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
210 "If the argument is negative or omitted, read until EOF\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
211 "is reached. Return an empty string at EOF.\n");
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
212
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
213 static PyObject *
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
214 stringio_read(StringIOObject *self, PyObject *args)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
215 {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
216 Py_ssize_t size, n;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
217 Py_UNICODE *output;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
218 PyObject *arg = Py_None;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
219
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
220 CHECK_INITIALIZED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
221 if (!PyArg_ParseTuple(args, "|O:read", &arg))
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
222 return NULL;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
223 CHECK_CLOSED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
224
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
225 if (PyNumber_Check(arg)) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
226 size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
49360
1f4ea0229ec6 Fix a probable merge glitch in r66695:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49353
diff changeset
227 if (size == -1 && PyErr_Occurred())
1f4ea0229ec6 Fix a probable merge glitch in r66695:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49353
diff changeset
228 return NULL;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
229 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
230 else if (arg == Py_None) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
231 /* Read until EOF is reached, by default. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
232 size = -1;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
233 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
234 else {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
235 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
236 Py_TYPE(arg)->tp_name);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
237 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
238 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
239
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
240 /* adjust invalid sizes */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
241 n = self->string_size - self->pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
242 if (size < 0 || size > n) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
243 size = n;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
244 if (size < 0)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
245 size = 0;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
246 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
247
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
248 output = self->buf + self->pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
249 self->pos += size;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
250 return PyUnicode_FromUnicode(output, size);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
251 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
252
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
253 /* Internal helper, used by stringio_readline and stringio_iternext */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
254 static PyObject *
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
255 _stringio_readline(StringIOObject *self, Py_ssize_t limit)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
256 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
257 Py_UNICODE *start, *end, old_char;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
258 Py_ssize_t len, consumed;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
259
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
260 /* In case of overseek, return the empty string */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
261 if (self->pos >= self->string_size)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
262 return PyUnicode_FromString("");
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
263
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
264 start = self->buf + self->pos;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
265 if (limit < 0 || limit > self->string_size - self->pos)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
266 limit = self->string_size - self->pos;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
267
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
268 end = start + limit;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
269 old_char = *end;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
270 *end = '\0';
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
271 len = _PyIO_find_line_ending(
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
272 self->readtranslate, self->readuniversal, self->readnl,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
273 start, end, &consumed);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
274 *end = old_char;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
275 /* If we haven't found any line ending, we just return everything
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
276 (`consumed` is ignored). */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
277 if (len < 0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
278 len = limit;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
279 self->pos += len;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
280 return PyUnicode_FromUnicode(start, len);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
281 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
282
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
283 PyDoc_STRVAR(stringio_readline_doc,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
284 "Read until newline or EOF.\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
285 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
286 "Returns an empty string if EOF is hit immediately.\n");
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
287
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
288 static PyObject *
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
289 stringio_readline(StringIOObject *self, PyObject *args)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
290 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
291 PyObject *arg = Py_None;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
292 Py_ssize_t limit = -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
293
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
294 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
295 if (!PyArg_ParseTuple(args, "|O:readline", &arg))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
296 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
297 CHECK_CLOSED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
298
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
299 if (PyNumber_Check(arg)) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
300 limit = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
301 if (limit == -1 && PyErr_Occurred())
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
302 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
303 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
304 else if (arg != Py_None) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
305 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
306 Py_TYPE(arg)->tp_name);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
307 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
308 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
309 return _stringio_readline(self, limit);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
310 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
311
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
312 static PyObject *
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
313 stringio_iternext(StringIOObject *self)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
314 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
315 PyObject *line;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
316
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
317 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
318 CHECK_CLOSED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
319
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
320 if (Py_TYPE(self) == &PyStringIO_Type) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
321 /* Skip method call overhead for speed */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
322 line = _stringio_readline(self, -1);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
323 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
324 else {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
325 /* XXX is subclassing StringIO really supported? */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
326 line = PyObject_CallMethodObjArgs((PyObject *)self,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
327 _PyIO_str_readline, NULL);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
328 if (line && !PyUnicode_Check(line)) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
329 PyErr_Format(PyExc_IOError,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
330 "readline() should have returned an str object, "
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
331 "not '%.200s'", Py_TYPE(line)->tp_name);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
332 Py_DECREF(line);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
333 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
334 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
335 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
336
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
337 if (line == NULL)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
338 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
339
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
340 if (PyUnicode_GET_SIZE(line) == 0) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
341 /* Reached EOF */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
342 Py_DECREF(line);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
343 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
344 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
345
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
346 return line;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
347 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
348
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
349 PyDoc_STRVAR(stringio_truncate_doc,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
350 "Truncate size to pos.\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
351 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
352 "The pos argument defaults to the current file position, as\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
353 "returned by tell(). Imply an absolute seek to pos.\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
354 "Returns the new absolute position.\n");
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
355
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
356 static PyObject *
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
357 stringio_truncate(StringIOObject *self, PyObject *args)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
358 {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
359 Py_ssize_t size;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
360 PyObject *arg = Py_None;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
361
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
362 CHECK_INITIALIZED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
363 if (!PyArg_ParseTuple(args, "|O:truncate", &arg))
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
364 return NULL;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
365 CHECK_CLOSED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
366
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
367 if (PyNumber_Check(arg)) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
368 size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
49353
598d19936186 check for errors after PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
369 if (size == -1 && PyErr_Occurred())
598d19936186 check for errors after PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
370 return NULL;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
371 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
372 else if (arg == Py_None) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
373 /* Truncate to current position if no argument is passed. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
374 size = self->pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
375 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
376 else {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
377 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
378 Py_TYPE(arg)->tp_name);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
379 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
380 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
381
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
382 if (size < 0) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
383 PyErr_Format(PyExc_ValueError,
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
384 "Negative size value %zd", size);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
385 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
386 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
387
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
388 if (size < self->string_size) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
389 if (resize_buffer(self, size) < 0)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
390 return NULL;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
391 self->string_size = size;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
392 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
393 self->pos = size;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
394
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
395 return PyLong_FromSsize_t(size);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
396 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
397
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
398 PyDoc_STRVAR(stringio_seek_doc,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
399 "Change stream position.\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
400 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
401 "Seek to character offset pos relative to position indicated by whence:\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
402 " 0 Start of stream (the default). pos should be >= 0;\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
403 " 1 Current position - pos must be 0;\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
404 " 2 End of stream - pos must be 0.\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
405 "Returns the new absolute position.\n");
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
406
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
407 static PyObject *
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
408 stringio_seek(StringIOObject *self, PyObject *args)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
409 {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
410 Py_ssize_t pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
411 int mode = 0;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
412
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
413 CHECK_INITIALIZED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
414 if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &mode))
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
415 return NULL;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
416 CHECK_CLOSED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
417
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
418 if (mode != 0 && mode != 1 && mode != 2) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
419 PyErr_Format(PyExc_ValueError,
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
420 "Invalid whence (%i, should be 0, 1 or 2)", mode);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
421 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
422 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
423 else if (pos < 0 && mode == 0) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
424 PyErr_Format(PyExc_ValueError,
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
425 "Negative seek position %zd", pos);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
426 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
427 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
428 else if (mode != 0 && pos != 0) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
429 PyErr_SetString(PyExc_IOError,
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
430 "Can't do nonzero cur-relative seeks");
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
431 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
432 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
433
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
434 /* mode 0: offset relative to beginning of the string.
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
435 mode 1: no change to current position.
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
436 mode 2: change position to end of file. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
437 if (mode == 1) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
438 pos = self->pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
439 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
440 else if (mode == 2) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
441 pos = self->string_size;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
442 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
443
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
444 self->pos = pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
445
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
446 return PyLong_FromSsize_t(self->pos);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
447 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
448
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
449 PyDoc_STRVAR(stringio_write_doc,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
450 "Write string to file.\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
451 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
452 "Returns the number of characters written, which is always equal to\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
453 "the length of the string.\n");
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
454
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
455 static PyObject *
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
456 stringio_write(StringIOObject *self, PyObject *obj)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
457 {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
458 Py_ssize_t size;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
459
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
460 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
461 if (!PyUnicode_Check(obj)) {
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
462 PyErr_Format(PyExc_TypeError, "string argument expected, got '%s'",
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
463 Py_TYPE(obj)->tp_name);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
464 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
465 }
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
466 CHECK_CLOSED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
467 size = PyUnicode_GET_SIZE(obj);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
468
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
469 if (size > 0 && write_str(self, obj) < 0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
470 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
471
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
472 return PyLong_FromSsize_t(size);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
473 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
474
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
475 PyDoc_STRVAR(stringio_close_doc,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
476 "Close the IO object. Attempting any further operation after the\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
477 "object is closed will raise a ValueError.\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
478 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
479 "This method has no effect if the file is already closed.\n");
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
480
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
481 static PyObject *
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
482 stringio_close(StringIOObject *self)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
483 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
484 self->closed = 1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
485 /* Free up some memory */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
486 if (resize_buffer(self, 0) < 0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
487 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
488 Py_CLEAR(self->readnl);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
489 Py_CLEAR(self->writenl);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
490 Py_CLEAR(self->decoder);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
491 Py_RETURN_NONE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
492 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
493
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
494 static int
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
495 stringio_traverse(StringIOObject *self, visitproc visit, void *arg)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
496 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
497 Py_VISIT(self->dict);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
498 return 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
499 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
500
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
501 static int
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
502 stringio_clear(StringIOObject *self)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
503 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
504 Py_CLEAR(self->dict);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
505 return 0;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
506 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
507
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
508 static void
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
509 stringio_dealloc(StringIOObject *self)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
510 {
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
511 _PyObject_GC_UNTRACK(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
512 Py_CLEAR(self->readnl);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
513 Py_CLEAR(self->writenl);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
514 Py_CLEAR(self->decoder);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
515 if (self->buf)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
516 PyMem_Free(self->buf);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
517 if (self->weakreflist != NULL)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
518 PyObject_ClearWeakRefs((PyObject *) self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
519 Py_TYPE(self)->tp_free(self);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
520 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
521
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
522 static PyObject *
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
523 stringio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
524 {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
525 StringIOObject *self;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
526
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
527 assert(type != NULL && type->tp_alloc != NULL);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
528 self = (StringIOObject *)type->tp_alloc(type, 0);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
529 if (self == NULL)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
530 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
531
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
532 self->string_size = 0;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
533 self->pos = 0;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
534 self->buf_size = 0;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
535 self->buf = (Py_UNICODE *)PyMem_Malloc(0);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
536 if (self->buf == NULL) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
537 Py_DECREF(self);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
538 return PyErr_NoMemory();
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
539 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
540
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
541 return (PyObject *)self;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
542 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
543
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
544 static int
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
545 stringio_init(StringIOObject *self, PyObject *args, PyObject *kwds)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
546 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
547 char *kwlist[] = {"initial_value", "newline", NULL};
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
548 PyObject *value = NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
549 char *newline = "\n";
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
550
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
551 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oz:__init__", kwlist,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
552 &value, &newline))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
553 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
554
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
555 if (newline && newline[0] != '\0'
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
556 && !(newline[0] == '\n' && newline[1] == '\0')
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
557 && !(newline[0] == '\r' && newline[1] == '\0')
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
558 && !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
559 PyErr_Format(PyExc_ValueError,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
560 "illegal newline value: %s", newline);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
561 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
562 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
563 if (value && value != Py_None && !PyUnicode_Check(value)) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
564 PyErr_Format(PyExc_ValueError,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
565 "initial_value must be str or None, not %.200s",
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
566 Py_TYPE(value)->tp_name);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
567 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
568 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
569
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
570 self->ok = 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
571
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
572 Py_CLEAR(self->readnl);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
573 Py_CLEAR(self->writenl);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
574 Py_CLEAR(self->decoder);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
575
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
576 if (newline) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
577 self->readnl = PyUnicode_FromString(newline);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
578 if (self->readnl == NULL)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
579 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
580 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
581 self->readuniversal = (newline == NULL || newline[0] == '\0');
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
582 self->readtranslate = (newline == NULL);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
583 /* If newline == "", we don't translate anything.
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
584 If newline == "\n" or newline == None, we translate to "\n", which is
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
585 a no-op.
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
586 (for newline == None, TextIOWrapper translates to os.sepline, but it
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
587 is pointless for StringIO)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
588 */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
589 if (newline != NULL && newline[0] == '\r') {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
590 self->writenl = self->readnl;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
591 Py_INCREF(self->writenl);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
592 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
593
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
594 if (self->readuniversal) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
595 self->decoder = PyObject_CallFunction(
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
596 (PyObject *)&PyIncrementalNewlineDecoder_Type,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
597 "Oi", Py_None, (int) self->readtranslate);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
598 if (self->decoder == NULL)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
599 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
600 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
601
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
602 /* Now everything is set up, resize buffer to size of initial value,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
603 and copy it */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
604 self->string_size = 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
605 if (value && value != Py_None) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
606 Py_ssize_t len = PyUnicode_GetSize(value);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
607 /* This is a heuristic, for newline translation might change
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
608 the string length. */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
609 if (resize_buffer(self, len) < 0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
610 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
611 self->pos = 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
612 if (write_str(self, value) < 0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
613 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
614 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
615 else {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
616 if (resize_buffer(self, 0) < 0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
617 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
618 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
619 self->pos = 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
620
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
621 self->closed = 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
622 self->ok = 1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
623 return 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
624 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
625
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
626 /* Properties and pseudo-properties */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
627 static PyObject *
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
628 stringio_seekable(StringIOObject *self, PyObject *args)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
629 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
630 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
631 Py_RETURN_TRUE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
632 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
633
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
634 static PyObject *
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
635 stringio_readable(StringIOObject *self, PyObject *args)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
636 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
637 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
638 Py_RETURN_TRUE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
639 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
640
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
641 static PyObject *
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
642 stringio_writable(StringIOObject *self, PyObject *args)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
643 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
644 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
645 Py_RETURN_TRUE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
646 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
647
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
648 static PyObject *
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
649 stringio_buffer(StringIOObject *self, void *context)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
650 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
651 PyErr_SetString(IO_STATE->unsupported_operation,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
652 "buffer attribute is unsupported on type StringIO");
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
653 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
654 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
655
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
656 static PyObject *
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
657 stringio_closed(StringIOObject *self, void *context)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
658 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
659 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
660 return PyBool_FromLong(self->closed);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
661 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
662
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
663 static PyObject *
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
664 stringio_encoding(StringIOObject *self, void *context)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
665 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
666 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
667 CHECK_CLOSED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
668 return PyUnicode_FromString("utf-8");
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
669 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
670
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
671 static PyObject *
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
672 stringio_errors(StringIOObject *self, void *context)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
673 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
674 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
675 CHECK_CLOSED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
676 return PyUnicode_FromString("strict");
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
677 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
678
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
679 static PyObject *
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
680 stringio_line_buffering(StringIOObject *self, void *context)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
681 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
682 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
683 CHECK_CLOSED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
684 Py_RETURN_FALSE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
685 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
686
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
687 static PyObject *
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
688 stringio_newlines(StringIOObject *self, void *context)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
689 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
690 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
691 CHECK_CLOSED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
692 if (self->decoder == NULL)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
693 Py_RETURN_NONE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
694 return PyObject_GetAttr(self->decoder, _PyIO_str_newlines);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
695 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
696
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
697 static struct PyMethodDef stringio_methods[] = {
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
698 {"close", (PyCFunction)stringio_close, METH_NOARGS, stringio_close_doc},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
699 {"getvalue", (PyCFunction)stringio_getvalue, METH_VARARGS, stringio_getvalue_doc},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
700 {"read", (PyCFunction)stringio_read, METH_VARARGS, stringio_read_doc},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
701 {"readline", (PyCFunction)stringio_readline, METH_VARARGS, stringio_readline_doc},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
702 {"tell", (PyCFunction)stringio_tell, METH_NOARGS, stringio_tell_doc},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
703 {"truncate", (PyCFunction)stringio_truncate, METH_VARARGS, stringio_truncate_doc},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
704 {"seek", (PyCFunction)stringio_seek, METH_VARARGS, stringio_seek_doc},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
705 {"write", (PyCFunction)stringio_write, METH_O, stringio_write_doc},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
706
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
707 {"seekable", (PyCFunction)stringio_seekable, METH_NOARGS},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
708 {"readable", (PyCFunction)stringio_readable, METH_NOARGS},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
709 {"writable", (PyCFunction)stringio_writable, METH_NOARGS},
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
710 {NULL, NULL} /* sentinel */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
711 };
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
712
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
713 static PyGetSetDef stringio_getset[] = {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
714 {"closed", (getter)stringio_closed, NULL, NULL},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
715 {"newlines", (getter)stringio_newlines, NULL, NULL},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
716 /* (following comments straight off of the original Python wrapper:)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
717 XXX Cruft to support the TextIOWrapper API. This would only
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
718 be meaningful if StringIO supported the buffer attribute.
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
719 Hopefully, a better solution, than adding these pseudo-attributes,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
720 will be found.
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
721 */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
722 {"buffer", (getter)stringio_buffer, NULL, NULL},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
723 {"encoding", (getter)stringio_encoding, NULL, NULL},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
724 {"errors", (getter)stringio_errors, NULL, NULL},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
725 {"line_buffering", (getter)stringio_line_buffering, NULL, NULL},
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
726 {0}
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
727 };
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
728
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
729 PyTypeObject PyStringIO_Type = {
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
730 PyVarObject_HEAD_INIT(NULL, 0)
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
731 "_io.StringIO", /*tp_name*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
732 sizeof(StringIOObject), /*tp_basicsize*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
733 0, /*tp_itemsize*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
734 (destructor)stringio_dealloc, /*tp_dealloc*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
735 0, /*tp_print*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
736 0, /*tp_getattr*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
737 0, /*tp_setattr*/
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49360
diff changeset
738 0, /*tp_reserved*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
739 0, /*tp_repr*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
740 0, /*tp_as_number*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
741 0, /*tp_as_sequence*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
742 0, /*tp_as_mapping*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
743 0, /*tp_hash*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
744 0, /*tp_call*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
745 0, /*tp_str*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
746 0, /*tp_getattro*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
747 0, /*tp_setattro*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
748 0, /*tp_as_buffer*/
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
749 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
750 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
751 stringio_doc, /*tp_doc*/
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
752 (traverseproc)stringio_traverse, /*tp_traverse*/
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
753 (inquiry)stringio_clear, /*tp_clear*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
754 0, /*tp_richcompare*/
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
755 offsetof(StringIOObject, weakreflist), /*tp_weaklistoffset*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
756 0, /*tp_iter*/
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
757 (iternextfunc)stringio_iternext, /*tp_iternext*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
758 stringio_methods, /*tp_methods*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
759 0, /*tp_members*/
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
760 stringio_getset, /*tp_getset*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
761 0, /*tp_base*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
762 0, /*tp_dict*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
763 0, /*tp_descr_get*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
764 0, /*tp_descr_set*/
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
765 offsetof(StringIOObject, dict), /*tp_dictoffset*/
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
766 (initproc)stringio_init, /*tp_init*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
767 0, /*tp_alloc*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
768 stringio_new, /*tp_new*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
769 };