annotate Modules/_io/stringio.c @ 57114:d3a81051663b

importlib.test.source.util referenced variables in the 'finally' part of a try/finally which may not have been set.
author Brett Cannon <bcannon@gmail.com>
date Thu, 05 Nov 2009 01:17:22 +0000
parents 538850b38931
children b354c1b6b476
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;
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
27 } stringio;
47625
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
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
54 resize_buffer(stringio *self, size_t size)
47625
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
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
109 write_str(stringio *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 *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
189 stringio_getvalue(stringio *self)
47625
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 *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
200 stringio_tell(stringio *self)
47625
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 *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
214 stringio_read(stringio *self, PyObject *args)
47625
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 *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
255 _stringio_readline(stringio *self, Py_ssize_t limit)
51991
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 *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
289 stringio_readline(stringio *self, PyObject *args)
51991
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 *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
313 stringio_iternext(stringio *self)
51991
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 *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
357 stringio_truncate(stringio *self, PyObject *args)
47625
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 *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
408 stringio_seek(stringio *self, PyObject *args)
47625
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 *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
456 stringio_write(stringio *self, PyObject *obj)
47625
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 *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
482 stringio_close(stringio *self)
51991
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
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
495 stringio_traverse(stringio *self, visitproc visit, void *arg)
51991
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
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
502 stringio_clear(stringio *self)
51991
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
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
509 stringio_dealloc(stringio *self)
47625
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);
55395
d747d52f9a66 Issue #6242: Fix deallocator of io.StringIO and io.BytesIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54752
diff changeset
512 self->ok = 0;
d747d52f9a66 Issue #6242: Fix deallocator of io.StringIO and io.BytesIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54752
diff changeset
513 if (self->buf) {
d747d52f9a66 Issue #6242: Fix deallocator of io.StringIO and io.BytesIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54752
diff changeset
514 PyMem_Free(self->buf);
d747d52f9a66 Issue #6242: Fix deallocator of io.StringIO and io.BytesIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54752
diff changeset
515 self->buf = NULL;
d747d52f9a66 Issue #6242: Fix deallocator of io.StringIO and io.BytesIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54752
diff changeset
516 }
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
517 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
518 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
519 Py_CLEAR(self->decoder);
55395
d747d52f9a66 Issue #6242: Fix deallocator of io.StringIO and io.BytesIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54752
diff changeset
520 Py_CLEAR(self->dict);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
521 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
522 PyObject_ClearWeakRefs((PyObject *) self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
523 Py_TYPE(self)->tp_free(self);
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
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
526 static PyObject *
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
527 stringio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
528 {
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
529 stringio *self;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
530
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
531 assert(type != NULL && type->tp_alloc != NULL);
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
532 self = (stringio *)type->tp_alloc(type, 0);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
533 if (self == NULL)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
534 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
535
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
536 /* tp_alloc initializes all the fields to zero. So we don't have to
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
537 initialize them here. */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
538
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
539 self->buf = (Py_UNICODE *)PyMem_Malloc(0);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
540 if (self->buf == NULL) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
541 Py_DECREF(self);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
542 return PyErr_NoMemory();
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
543 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
544
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
545 return (PyObject *)self;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
546 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
547
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
548 static int
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
549 stringio_init(stringio *self, PyObject *args, PyObject *kwds)
51991
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 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
552 PyObject *value = NULL;
55397
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
553 PyObject *newline_obj = NULL;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
554 char *newline = "\n";
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
555
55397
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
556 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:__init__", kwlist,
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
557 &value, &newline_obj))
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
558 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
559
55397
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
560 /* Parse the newline argument. This used to be done with the 'z'
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
561 specifier, however this allowed any object with the buffer interface to
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
562 be converted. Thus we have to parse it manually since we only want to
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
563 allow unicode objects or None. */
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
564 if (newline_obj == Py_None) {
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
565 newline = NULL;
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
566 }
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
567 else if (newline_obj) {
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
568 if (!PyUnicode_Check(newline_obj)) {
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
569 PyErr_Format(PyExc_TypeError,
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
570 "newline must be str or None, not %.200s",
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
571 Py_TYPE(newline_obj)->tp_name);
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
572 return -1;
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
573 }
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
574 newline = _PyUnicode_AsString(newline_obj);
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
575 if (newline == NULL)
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
576 return -1;
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
577 }
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
578
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
579 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
580 && !(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
581 && !(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
582 && !(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
583 PyErr_Format(PyExc_ValueError,
55397
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
584 "illegal newline value: %R", newline_obj);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
585 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
586 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
587 if (value && value != Py_None && !PyUnicode_Check(value)) {
55397
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
588 PyErr_Format(PyExc_TypeError,
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
589 "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
590 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
591 return -1;
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 self->ok = 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
595
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
596 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
597 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
598 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
599
55397
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
600 assert((newline != NULL && newline_obj != Py_None) ||
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
601 (newline == NULL && newline_obj == Py_None));
d682ca888e27 Issue #6241: Better type checking for the arguments of io.StringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
602
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
603 assert((newline != NULL && newline_obj != Py_None) ||
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
604 (newline == NULL && newline_obj == Py_None));
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
605
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
606 if (newline) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
607 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
608 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
609 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
610 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
611 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
612 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
613 /* 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
614 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
615 a no-op.
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
616 (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
617 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
618 */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
619 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
620 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
621 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
622 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
623
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
624 if (self->readuniversal) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
625 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
626 (PyObject *)&PyIncrementalNewlineDecoder_Type,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
627 "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
628 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
629 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
630 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
631
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
632 /* 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
633 and copy it */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
634 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
635 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
636 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
637 /* 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
638 the string length. */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
639 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
640 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
641 self->pos = 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
642 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
643 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
644 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
645 else {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
646 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
647 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
648 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
649 self->pos = 0;
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 self->closed = 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
652 self->ok = 1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
653 return 0;
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 /* 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
657 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
658 stringio_seekable(stringio *self, PyObject *args)
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
659 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
660 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
661 Py_RETURN_TRUE;
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
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
664 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
665 stringio_readable(stringio *self, PyObject *args)
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
666 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
667 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
668 Py_RETURN_TRUE;
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 *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
672 stringio_writable(stringio *self, PyObject *args)
51991
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 Py_RETURN_TRUE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
676 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
677
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
678 /* Pickling support.
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
679
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
680 The implementation of __getstate__ is similar to the one for BytesIO,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
681 except that we also save the newline parameter. For __setstate__ and unlike
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
682 BytesIO, we call __init__ to restore the object's state. Doing so allows us
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
683 to avoid decoding the complex newline state while keeping the object
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
684 representation compact.
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
685
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
686 See comment in bytesio.c regarding why only pickle protocols and onward are
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
687 supported.
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
688 */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
689
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
690 static PyObject *
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
691 stringio_getstate(stringio *self)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
692 {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
693 PyObject *initvalue = stringio_getvalue(self);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
694 PyObject *dict;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
695 PyObject *state;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
696
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
697 if (initvalue == NULL)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
698 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
699 if (self->dict == NULL) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
700 Py_INCREF(Py_None);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
701 dict = Py_None;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
702 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
703 else {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
704 dict = PyDict_Copy(self->dict);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
705 if (dict == NULL)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
706 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
707 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
708
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
709 state = Py_BuildValue("(OOnN)", initvalue,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
710 self->readnl ? self->readnl : Py_None,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
711 self->pos, dict);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
712 Py_DECREF(initvalue);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
713 return state;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
714 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
715
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
716 static PyObject *
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
717 stringio_setstate(stringio *self, PyObject *state)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
718 {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
719 PyObject *initarg;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
720 PyObject *position_obj;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
721 PyObject *dict;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
722 Py_ssize_t pos;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
723
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
724 assert(state != NULL);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
725 CHECK_CLOSED(self);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
726
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
727 /* We allow the state tuple to be longer than 4, because we may need
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
728 someday to extend the object's state without breaking
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
729 backward-compatibility. */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
730 if (!PyTuple_Check(state) || Py_SIZE(state) < 4) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
731 PyErr_Format(PyExc_TypeError,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
732 "%.200s.__setstate__ argument should be 4-tuple, got %.200s",
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
733 Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
734 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
735 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
736
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
737 /* Initialize the object's state. */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
738 initarg = PyTuple_GetSlice(state, 0, 2);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
739 if (initarg == NULL)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
740 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
741 if (stringio_init(self, initarg, NULL) < 0) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
742 Py_DECREF(initarg);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
743 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
744 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
745 Py_DECREF(initarg);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
746
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
747 /* Restore the buffer state. Even if __init__ did initialize the buffer,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
748 we have to initialize it again since __init__ may translates the
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
749 newlines in the inital_value string. We clearly do not want that
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
750 because the string value in the state tuple has already been translated
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
751 once by __init__. So we do not take any chance and replace object's
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
752 buffer completely. */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
753 {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
754 Py_UNICODE *buf = PyUnicode_AS_UNICODE(PyTuple_GET_ITEM(state, 0));
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
755 Py_ssize_t bufsize = PyUnicode_GET_SIZE(PyTuple_GET_ITEM(state, 0));
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
756 if (resize_buffer(self, bufsize) < 0)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
757 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
758 memcpy(self->buf, buf, bufsize * sizeof(Py_UNICODE));
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
759 self->string_size = bufsize;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
760 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
761
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
762 /* Set carefully the position value. Alternatively, we could use the seek
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
763 method instead of modifying self->pos directly to better protect the
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
764 object internal state against errneous (or malicious) inputs. */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
765 position_obj = PyTuple_GET_ITEM(state, 2);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
766 if (!PyLong_Check(position_obj)) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
767 PyErr_Format(PyExc_TypeError,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
768 "third item of state must be an integer, got %.200s",
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
769 Py_TYPE(position_obj)->tp_name);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
770 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
771 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
772 pos = PyLong_AsSsize_t(position_obj);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
773 if (pos == -1 && PyErr_Occurred())
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
774 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
775 if (pos < 0) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
776 PyErr_SetString(PyExc_ValueError,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
777 "position value cannot be negative");
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
778 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
779 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
780 self->pos = pos;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
781
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
782 /* Set the dictionary of the instance variables. */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
783 dict = PyTuple_GET_ITEM(state, 3);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
784 if (dict != Py_None) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
785 if (!PyDict_Check(dict)) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
786 PyErr_Format(PyExc_TypeError,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
787 "fourth item of state should be a dict, got a %.200s",
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
788 Py_TYPE(dict)->tp_name);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
789 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
790 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
791 if (self->dict) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
792 /* Alternatively, we could replace the internal dictionary
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
793 completely. However, it seems more practical to just update it. */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
794 if (PyDict_Update(self->dict, dict) < 0)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
795 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
796 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
797 else {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
798 Py_INCREF(dict);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
799 self->dict = dict;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
800 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
801 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
802
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
803 Py_RETURN_NONE;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
804 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
805
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
806
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
807 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
808 stringio_closed(stringio *self, void *context)
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
809 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
810 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
811 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
812 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
813
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
814 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
815 stringio_line_buffering(stringio *self, void *context)
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
816 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
817 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
818 CHECK_CLOSED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
819 Py_RETURN_FALSE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
820 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
821
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
822 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
823 stringio_newlines(stringio *self, void *context)
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
824 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
825 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
826 CHECK_CLOSED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
827 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
828 Py_RETURN_NONE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
829 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
830 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
831
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
832 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
833 {"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
834 {"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
835 {"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
836 {"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
837 {"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
838 {"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
839 {"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
840 {"write", (PyCFunction)stringio_write, METH_O, stringio_write_doc},
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
841
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
842 {"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
843 {"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
844 {"writable", (PyCFunction)stringio_writable, METH_NOARGS},
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
845
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
846 {"__getstate__", (PyCFunction)stringio_getstate, METH_NOARGS},
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
847 {"__setstate__", (PyCFunction)stringio_setstate, METH_O},
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
848 {NULL, NULL} /* sentinel */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
849 };
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
850
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
851 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
852 {"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
853 {"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
854 /* (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
855 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
856 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
857 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
858 will be found.
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
859 */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
860 {"line_buffering", (getter)stringio_line_buffering, NULL, NULL},
53259
b7b8fbd010ca use NULL for the ends of tables
Benjamin Peterson <benjamin@python.org>
parents: 52843
diff changeset
861 {NULL}
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
862 };
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
863
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
864 PyTypeObject PyStringIO_Type = {
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
865 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
866 "_io.StringIO", /*tp_name*/
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
867 sizeof(stringio), /*tp_basicsize*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
868 0, /*tp_itemsize*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
869 (destructor)stringio_dealloc, /*tp_dealloc*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
870 0, /*tp_print*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
871 0, /*tp_getattr*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
872 0, /*tp_setattr*/
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49360
diff changeset
873 0, /*tp_reserved*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
874 0, /*tp_repr*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
875 0, /*tp_as_number*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
876 0, /*tp_as_sequence*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
877 0, /*tp_as_mapping*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
878 0, /*tp_hash*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
879 0, /*tp_call*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
880 0, /*tp_str*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
881 0, /*tp_getattro*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
882 0, /*tp_setattro*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
883 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
884 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
885 | 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
886 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
887 (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
888 (inquiry)stringio_clear, /*tp_clear*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
889 0, /*tp_richcompare*/
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
890 offsetof(stringio, weakreflist), /*tp_weaklistoffset*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
891 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
892 (iternextfunc)stringio_iternext, /*tp_iternext*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
893 stringio_methods, /*tp_methods*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
894 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
895 stringio_getset, /*tp_getset*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
896 0, /*tp_base*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
897 0, /*tp_dict*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
898 0, /*tp_descr_get*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
899 0, /*tp_descr_set*/
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
900 offsetof(stringio, dict), /*tp_dictoffset*/
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
901 (initproc)stringio_init, /*tp_init*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
902 0, /*tp_alloc*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
903 stringio_new, /*tp_new*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
904 };