annotate Modules/_io/stringio.c @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents fcf097cb5f6b
children
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"
75876
5fe7d19ec49a Issue #14387: Do not include accu.h from Python.h.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73655
diff changeset
4 #include "accu.h"
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
5 #include "_iomodule.h"
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
7 /* 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
8 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
9 */
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
10
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
11 #define STATE_REALIZED 1
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
12 #define STATE_ACCUMULATING 2
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
13
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
14 typedef struct {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
15 PyObject_HEAD
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
16 Py_UCS4 *buf;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
17 Py_ssize_t pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
18 Py_ssize_t string_size;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
19 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
20
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
21 /* The stringio object can be in two states: accumulating or realized.
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
22 In accumulating state, the internal buffer contains nothing and
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
23 the contents are given by the embedded _PyAccu structure.
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
24 In realized state, the internal buffer is meaningful and the
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
25 _PyAccu is destroyed.
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
26 */
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
27 int state;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
28 _PyAccu accu;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
29
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
30 char ok; /* initialized? */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
31 char closed;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
32 char readuniversal;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
33 char readtranslate;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
34 PyObject *decoder;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
35 PyObject *readnl;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
36 PyObject *writenl;
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
37
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
38 PyObject *dict;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
39 PyObject *weakreflist;
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
40 } stringio;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
41
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
42 #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
43 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
44 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
45 "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
46 return NULL; \
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
47 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
48
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
49 #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
50 if (self->closed) { \
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
51 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
52 "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
53 return NULL; \
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
54 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
55
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
56 #define ENSURE_REALIZED(self) \
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
57 if (realize(self) < 0) { \
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
58 return NULL; \
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
59 }
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
60
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
61 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
62 "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
63 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
64 "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
65 "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
66
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
67
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
68 /* 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
69 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
70 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
71 static int
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
72 resize_buffer(stringio *self, size_t size)
47625
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 /* 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
75 overflow, which is undefined in C. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
76 size_t alloc = self->buf_size;
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
77 Py_UCS4 *new_buf = NULL;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
78
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
79 assert(self->buf != NULL);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
80
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
81 /* 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
82 size = size + 1;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
83 /* 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
84 doesn't allow strings to be longer than this. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
85 if (size > PY_SSIZE_T_MAX)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
86 goto overflow;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
87
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
88 if (size < alloc / 2) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
89 /* Major downsize; resize down to exact size. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
90 alloc = size + 1;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
91 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
92 else if (size < alloc) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
93 /* Within allocated size; quick exit */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
94 return 0;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
95 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
96 else if (size <= alloc * 1.125) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
97 /* Moderate upsize; overallocate similar to list_resize() */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
98 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
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 else {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
101 /* Major upsize; resize up to exact size */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
102 alloc = size + 1;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
103 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
104
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
105 if (alloc > PY_SIZE_MAX / sizeof(Py_UCS4))
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
106 goto overflow;
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
107 new_buf = (Py_UCS4 *)PyMem_Realloc(self->buf, alloc * sizeof(Py_UCS4));
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
108 if (new_buf == NULL) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
109 PyErr_NoMemory();
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
110 return -1;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
111 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
112 self->buf_size = alloc;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
113 self->buf = new_buf;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
114
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
115 return 0;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
116
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
117 overflow:
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
118 PyErr_SetString(PyExc_OverflowError,
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
119 "new buffer size too large");
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
120 return -1;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
121 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
122
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
123 static PyObject *
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
124 make_intermediate(stringio *self)
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
125 {
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
126 PyObject *intermediate = _PyAccu_Finish(&self->accu);
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
127 self->state = STATE_REALIZED;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
128 if (intermediate == NULL)
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
129 return NULL;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
130 if (_PyAccu_Init(&self->accu) ||
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
131 _PyAccu_Accumulate(&self->accu, intermediate)) {
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
132 Py_DECREF(intermediate);
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
133 return NULL;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
134 }
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
135 self->state = STATE_ACCUMULATING;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
136 return intermediate;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
137 }
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
138
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
139 static int
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
140 realize(stringio *self)
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
141 {
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
142 Py_ssize_t len;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
143 PyObject *intermediate;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
144
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
145 if (self->state == STATE_REALIZED)
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
146 return 0;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
147 assert(self->state == STATE_ACCUMULATING);
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
148 self->state = STATE_REALIZED;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
149
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
150 intermediate = _PyAccu_Finish(&self->accu);
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
151 if (intermediate == NULL)
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
152 return -1;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
153
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
154 /* Append the intermediate string to the internal buffer.
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
155 The length should be equal to the current cursor position.
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
156 */
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
157 len = PyUnicode_GET_LENGTH(intermediate);
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
158 if (resize_buffer(self, len) < 0) {
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
159 Py_DECREF(intermediate);
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
160 return -1;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
161 }
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
162 if (!PyUnicode_AsUCS4(intermediate, self->buf, len, 0)) {
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
163 Py_DECREF(intermediate);
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
164 return -1;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
165 }
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
166
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
167 Py_DECREF(intermediate);
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
168 return 0;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
169 }
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
170
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
171 /* 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
172 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
173 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
174 write_str(stringio *self, PyObject *obj)
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
175 {
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
176 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
177 PyObject *decoded = NULL;
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
178
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
179 assert(self->buf != NULL);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
180 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
181
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
182 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
183 decoded = _PyIncrementalNewlineDecoder_decode(
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
184 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
185 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
186 else {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
187 decoded = obj;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
188 Py_INCREF(decoded);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
189 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
190 if (self->writenl) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
191 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
192 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
193 Py_DECREF(decoded);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
194 decoded = translated;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
195 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
196 if (decoded == NULL)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
197 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
198
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
199 assert(PyUnicode_Check(decoded));
72679
b66033a0f140 Fix usage og PyUnicode_READY()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
200 if (PyUnicode_READY(decoded)) {
b66033a0f140 Fix usage og PyUnicode_READY()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
201 Py_DECREF(decoded);
b66033a0f140 Fix usage og PyUnicode_READY()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
202 return -1;
b66033a0f140 Fix usage og PyUnicode_READY()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
203 }
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
204 len = PyUnicode_GET_LENGTH(decoded);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
205 assert(len >= 0);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
206
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
207 /* 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
208 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
209 integer. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
210 if (self->pos > PY_SSIZE_T_MAX - len) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
211 PyErr_SetString(PyExc_OverflowError,
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
212 "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
213 goto fail;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
214 }
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
215
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
216 if (self->state == STATE_ACCUMULATING) {
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
217 if (self->string_size == self->pos) {
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
218 if (_PyAccu_Accumulate(&self->accu, decoded))
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
219 goto fail;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
220 goto success;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
221 }
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
222 if (realize(self))
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
223 goto fail;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
224 }
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
225
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
226 if (self->pos + len > self->string_size) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
227 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
228 goto fail;
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
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
231 if (self->pos > self->string_size) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
232 /* 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
233 the end of stream and the current position.
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
234
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
235 0 lo string_size hi
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
236 | |<---used--->|<----------available----------->|
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
237 | | <--to pad-->|<---to write---> |
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64383
diff changeset
238 0 buf position
47625
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 */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
241 memset(self->buf + self->string_size, '\0',
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
242 (self->pos - self->string_size) * sizeof(Py_UCS4));
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
243 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
244
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
245 /* 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
246 existing data if self->pos < self->string_size. */
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
247 if (!PyUnicode_AsUCS4(decoded,
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
248 self->buf + self->pos,
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
249 self->buf_size - self->pos,
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
250 0))
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
251 goto fail;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
252
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
253 success:
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
254 /* Set the new length of the internal string if it has changed. */
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
255 self->pos += len;
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
256 if (self->string_size < self->pos)
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
257 self->string_size = self->pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
258
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
259 Py_DECREF(decoded);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
260 return 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
261
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
262 fail:
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
263 Py_XDECREF(decoded);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
264 return -1;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
265 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
266
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
267 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
268 "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
269
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
270 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
271 stringio_getvalue(stringio *self)
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
272 {
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
273 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
274 CHECK_CLOSED(self);
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
275 if (self->state == STATE_ACCUMULATING)
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
276 return make_intermediate(self);
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
277 return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, self->buf,
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
278 self->string_size);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
279 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
280
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
281 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
282 "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
283
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
284 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
285 stringio_tell(stringio *self)
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
286 {
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
287 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
288 CHECK_CLOSED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
289 return PyLong_FromSsize_t(self->pos);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
290 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
291
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
292 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
293 "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
294 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
295 "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
296 "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
297
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
298 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
299 stringio_read(stringio *self, PyObject *args)
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
300 {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
301 Py_ssize_t size, n;
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
302 Py_UCS4 *output;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
303 PyObject *arg = Py_None;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
304
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
305 CHECK_INITIALIZED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
306 if (!PyArg_ParseTuple(args, "|O:read", &arg))
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
307 return NULL;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
308 CHECK_CLOSED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
309
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
310 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
311 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
312 if (size == -1 && PyErr_Occurred())
1f4ea0229ec6 Fix a probable merge glitch in r66695:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49353
diff changeset
313 return NULL;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
314 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
315 else if (arg == Py_None) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
316 /* Read until EOF is reached, by default. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
317 size = -1;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
318 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
319 else {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
320 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
321 Py_TYPE(arg)->tp_name);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
322 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
323 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
324
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
325 /* adjust invalid sizes */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
326 n = self->string_size - self->pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
327 if (size < 0 || size > n) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
328 size = n;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
329 if (size < 0)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
330 size = 0;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
331 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
332
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
333 /* Optimization for seek(0); read() */
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
334 if (self->state == STATE_ACCUMULATING && self->pos == 0 && size == n) {
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
335 PyObject *result = make_intermediate(self);
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
336 self->pos = self->string_size;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
337 return result;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
338 }
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
339
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
340 ENSURE_REALIZED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
341 output = self->buf + self->pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
342 self->pos += size;
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
343 return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, size);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
344 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
345
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
346 /* 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
347 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
348 _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
349 {
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
350 Py_UCS4 *start, *end, old_char;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
351 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
352
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
353 /* 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
354 if (self->pos >= self->string_size)
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
355 return PyUnicode_New(0, 0);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
356
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
357 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
358 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
359 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
360
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
361 end = start + limit;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
362 old_char = *end;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
363 *end = '\0';
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
364 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
365 self->readtranslate, self->readuniversal, self->readnl,
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
366 PyUnicode_4BYTE_KIND, (char*)start, (char*)end, &consumed);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
367 *end = old_char;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
368 /* 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
369 (`consumed` is ignored). */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
370 if (len < 0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
371 len = limit;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
372 self->pos += len;
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
373 return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, start, len);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
374 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
375
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
376 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
377 "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
378 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
379 "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
380
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
381 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
382 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
383 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
384 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
385 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
386
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
387 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
388 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
389 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
390 CHECK_CLOSED(self);
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
391 ENSURE_REALIZED(self);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
392
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
393 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
394 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
395 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
396 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
397 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
398 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
399 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
400 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
401 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
402 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
403 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
404 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
405
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
406 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
407 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
408 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
409 PyObject *line;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
410
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
411 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
412 CHECK_CLOSED(self);
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
413 ENSURE_REALIZED(self);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
414
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
415 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
416 /* 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
417 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
418 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
419 else {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
420 /* 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
421 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
422 _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
423 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
424 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
425 "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
426 "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
427 Py_DECREF(line);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
428 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
429 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
430 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
431
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
432 if (line == NULL)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
433 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
434
72863
afa42c04f0a3 Fix misuse of PyUnicode_GET_SIZE, use PyUnicode_GET_LENGTH instead
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72679
diff changeset
435 if (PyUnicode_GET_LENGTH(line) == 0) {
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
436 /* Reached EOF */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
437 Py_DECREF(line);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
438 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
439 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
440
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
441 return line;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
442 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
443
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
444 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
445 "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
446 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
447 "The pos argument defaults to the current file position, as\n"
58631
d887db501a22 Merged revisions 77895-77896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55609
diff changeset
448 "returned by tell(). The current file position is unchanged.\n"
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
449 "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
450
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
451 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
452 stringio_truncate(stringio *self, PyObject *args)
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
453 {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
454 Py_ssize_t size;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
455 PyObject *arg = Py_None;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
456
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
457 CHECK_INITIALIZED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
458 if (!PyArg_ParseTuple(args, "|O:truncate", &arg))
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
459 return NULL;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
460 CHECK_CLOSED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
461
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
462 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
463 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
464 if (size == -1 && PyErr_Occurred())
598d19936186 check for errors after PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
465 return NULL;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
466 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
467 else if (arg == Py_None) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
468 /* Truncate to current position if no argument is passed. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
469 size = self->pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
470 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
471 else {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
472 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
473 Py_TYPE(arg)->tp_name);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
474 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
475 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
476
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
477 if (size < 0) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
478 PyErr_Format(PyExc_ValueError,
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
479 "Negative size value %zd", size);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
480 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
481 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
482
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
483 if (size < self->string_size) {
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
484 ENSURE_REALIZED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
485 if (resize_buffer(self, size) < 0)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
486 return NULL;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
487 self->string_size = size;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
488 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
489
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
490 return PyLong_FromSsize_t(size);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
491 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
492
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
493 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
494 "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
495 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
496 "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
497 " 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
498 " 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
499 " 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
500 "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
501
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
502 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
503 stringio_seek(stringio *self, PyObject *args)
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
504 {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
505 Py_ssize_t pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
506 int mode = 0;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
507
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
508 CHECK_INITIALIZED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
509 if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &mode))
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
510 return NULL;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
511 CHECK_CLOSED(self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
512
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
513 if (mode != 0 && mode != 1 && mode != 2) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
514 PyErr_Format(PyExc_ValueError,
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
515 "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
516 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
517 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
518 else if (pos < 0 && mode == 0) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
519 PyErr_Format(PyExc_ValueError,
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
520 "Negative seek position %zd", pos);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
521 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
522 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
523 else if (mode != 0 && pos != 0) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
524 PyErr_SetString(PyExc_IOError,
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
525 "Can't do nonzero cur-relative seeks");
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
526 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
527 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
528
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
529 /* mode 0: offset relative to beginning of the string.
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
530 mode 1: no change to current position.
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
531 mode 2: change position to end of file. */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
532 if (mode == 1) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
533 pos = self->pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
534 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
535 else if (mode == 2) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
536 pos = self->string_size;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
537 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
538
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
539 self->pos = pos;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
540
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
541 return PyLong_FromSsize_t(self->pos);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
542 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
543
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
544 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
545 "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
546 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
547 "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
548 "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
549
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
550 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
551 stringio_write(stringio *self, PyObject *obj)
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
552 {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
553 Py_ssize_t size;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
554
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
555 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
556 if (!PyUnicode_Check(obj)) {
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
557 PyErr_Format(PyExc_TypeError, "string argument expected, got '%s'",
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
558 Py_TYPE(obj)->tp_name);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
559 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
560 }
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
561 if (PyUnicode_READY(obj))
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
562 return NULL;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
563 CHECK_CLOSED(self);
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
564 size = PyUnicode_GET_LENGTH(obj);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
565
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
566 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
567 return NULL;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
568
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
569 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
570 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
571
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
572 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
573 "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
574 "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
575 "\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
576 "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
577
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
578 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
579 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
580 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
581 self->closed = 1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
582 /* 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
583 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
584 return NULL;
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
585 _PyAccu_Destroy(&self->accu);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
586 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
587 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
588 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
589 Py_RETURN_NONE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
590 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
591
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
592 static int
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
593 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
594 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
595 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
596 return 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
597 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
598
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
599 static int
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
600 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
601 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
602 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
603 return 0;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
604 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
605
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
606 static void
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
607 stringio_dealloc(stringio *self)
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
608 {
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
609 _PyObject_GC_UNTRACK(self);
55609
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
610 self->ok = 0;
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
611 if (self->buf) {
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
612 PyMem_Free(self->buf);
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
613 self->buf = NULL;
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
614 }
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
615 _PyAccu_Destroy(&self->accu);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
616 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
617 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
618 Py_CLEAR(self->decoder);
55609
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
619 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
620 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
621 PyObject_ClearWeakRefs((PyObject *) self);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
622 Py_TYPE(self)->tp_free(self);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
623 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
624
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
625 static PyObject *
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
626 stringio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
627 {
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
628 stringio *self;
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
629
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
630 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
631 self = (stringio *)type->tp_alloc(type, 0);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
632 if (self == NULL)
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
633 return NULL;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
634
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
635 /* 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
636 initialize them here. */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
637
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
638 self->buf = (Py_UCS4 *)PyMem_Malloc(0);
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
639 if (self->buf == NULL) {
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
640 Py_DECREF(self);
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
641 return PyErr_NoMemory();
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
642 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
643
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
644 return (PyObject *)self;
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
645 }
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
646
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
647 static int
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
648 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
649 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
650 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
651 PyObject *value = NULL;
55609
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
652 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
653 char *newline = "\n";
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
654 Py_ssize_t value_len;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
655
55609
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
656 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:__init__", kwlist,
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
657 &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
658 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
659
55609
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
660 /* Parse the newline argument. This used to be done with the 'z'
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
661 specifier, however this allowed any object with the buffer interface to
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
662 be converted. Thus we have to parse it manually since we only want to
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
663 allow unicode objects or None. */
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
664 if (newline_obj == Py_None) {
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
665 newline = NULL;
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
666 }
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
667 else if (newline_obj) {
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
668 if (!PyUnicode_Check(newline_obj)) {
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
669 PyErr_Format(PyExc_TypeError,
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
670 "newline must be str or None, not %.200s",
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
671 Py_TYPE(newline_obj)->tp_name);
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
672 return -1;
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
673 }
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
674 newline = _PyUnicode_AsString(newline_obj);
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
675 if (newline == NULL)
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
676 return -1;
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
677 }
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
678
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
679 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
680 && !(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
681 && !(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
682 && !(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
683 PyErr_Format(PyExc_ValueError,
55609
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
684 "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
685 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
686 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
687 if (value && value != Py_None && !PyUnicode_Check(value)) {
55609
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
688 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
689 "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
690 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
691 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
692 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
693
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
694 self->ok = 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
695
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
696 _PyAccu_Destroy(&self->accu);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
697 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
698 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
699 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
700
55609
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
701 assert((newline != NULL && newline_obj != Py_None) ||
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
702 (newline == NULL && newline_obj == Py_None));
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54752
diff changeset
703
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
704 if (newline) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
705 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
706 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
707 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
708 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
709 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
710 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
711 /* 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
712 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
713 a no-op.
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
714 (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
715 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
716 */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
717 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
718 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
719 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
720 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
721
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
722 if (self->readuniversal) {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
723 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
724 (PyObject *)&PyIncrementalNewlineDecoder_Type,
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
725 "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
726 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
727 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
728 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
729
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
730 /* 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
731 and copy it */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
732 self->string_size = 0;
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
733 if (value && value != Py_None)
73655
2e37fc70e2dd Fix misuse of PyUnicode_GET_SIZE() => PyUnicode_GET_LENGTH()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73487
diff changeset
734 value_len = PyUnicode_GetLength(value);
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
735 else
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
736 value_len = 0;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
737 if (value_len > 0) {
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
738 /* 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
739 the string length. */
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
740 if (resize_buffer(self, 0) < 0)
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
741 return -1;
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
742 self->state = STATE_REALIZED;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
743 self->pos = 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
744 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
745 return -1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
746 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
747 else {
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
748 /* Empty stringio object, we can start by accumulating */
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
749 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
750 return -1;
73487
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
751 if (_PyAccu_Init(&self->accu))
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
752 return -1;
8d9a869db675 Issue #13149: Speed up append-only StringIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72863
diff changeset
753 self->state = STATE_ACCUMULATING;
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
754 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
755 self->pos = 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
756
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
757 self->closed = 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
758 self->ok = 1;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
759 return 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
760 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
761
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
762 /* Properties and pseudo-properties */
78852
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
763
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
764 PyDoc_STRVAR(stringio_readable_doc,
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
765 "readable() -> bool. Returns True if the IO object can be read.");
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
766
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
767 PyDoc_STRVAR(stringio_writable_doc,
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
768 "writable() -> bool. Returns True if the IO object can be written.");
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
769
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
770 PyDoc_STRVAR(stringio_seekable_doc,
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
771 "seekable() -> bool. Returns True if the IO object can be seeked.");
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
772
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
773 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
774 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
775 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
776 CHECK_INITIALIZED(self);
78852
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
777 CHECK_CLOSED(self);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
778 Py_RETURN_TRUE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
779 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
780
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
781 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
782 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
783 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
784 CHECK_INITIALIZED(self);
78852
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
785 CHECK_CLOSED(self);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
786 Py_RETURN_TRUE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
787 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
788
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
789 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
790 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
791 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
792 CHECK_INITIALIZED(self);
78852
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
793 CHECK_CLOSED(self);
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
794 Py_RETURN_TRUE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
795 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
796
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
797 /* Pickling support.
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
798
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
799 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
800 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
801 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
802 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
803 representation compact.
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 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
806 supported.
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
807 */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
808
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
809 static PyObject *
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
810 stringio_getstate(stringio *self)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
811 {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
812 PyObject *initvalue = stringio_getvalue(self);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
813 PyObject *dict;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
814 PyObject *state;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
815
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
816 if (initvalue == NULL)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
817 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
818 if (self->dict == NULL) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
819 Py_INCREF(Py_None);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
820 dict = Py_None;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
821 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
822 else {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
823 dict = PyDict_Copy(self->dict);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
824 if (dict == NULL)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
825 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
826 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
827
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
828 state = Py_BuildValue("(OOnN)", initvalue,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
829 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
830 self->pos, dict);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
831 Py_DECREF(initvalue);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
832 return state;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
833 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
834
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
835 static PyObject *
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
836 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
837 {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
838 PyObject *initarg;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
839 PyObject *position_obj;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
840 PyObject *dict;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
841 Py_ssize_t pos;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
842
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
843 assert(state != NULL);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
844 CHECK_CLOSED(self);
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 /* 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
847 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
848 backward-compatibility. */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
849 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
850 PyErr_Format(PyExc_TypeError,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
851 "%.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
852 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
853 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
854 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
855
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
856 /* Initialize the object's state. */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
857 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
858 if (initarg == NULL)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
859 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
860 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
861 Py_DECREF(initarg);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
862 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
863 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
864 Py_DECREF(initarg);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
865
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
866 /* 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
867 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
868 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
869 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
870 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
871 buffer completely. */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
872 {
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
873 PyObject *item;
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
874 Py_UCS4 *buf;
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
875 Py_ssize_t bufsize;
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
876
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
877 item = PyTuple_GET_ITEM(state, 0);
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
878 buf = PyUnicode_AsUCS4Copy(item);
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
879 if (buf == NULL)
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
880 return NULL;
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
881 bufsize = PyUnicode_GET_LENGTH(item);
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
882
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
883 if (resize_buffer(self, bufsize) < 0) {
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
884 PyMem_Free(buf);
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
885 return NULL;
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
886 }
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
887 memcpy(self->buf, buf, bufsize * sizeof(Py_UCS4));
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 68586
diff changeset
888 PyMem_Free(buf);
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
889 self->string_size = bufsize;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
890 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
891
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
892 /* 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
893 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
894 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
895 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
896 if (!PyLong_Check(position_obj)) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
897 PyErr_Format(PyExc_TypeError,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
898 "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
899 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
900 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
901 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
902 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
903 if (pos == -1 && PyErr_Occurred())
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
904 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
905 if (pos < 0) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
906 PyErr_SetString(PyExc_ValueError,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
907 "position value cannot be negative");
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
908 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
909 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
910 self->pos = pos;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
911
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
912 /* 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
913 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
914 if (dict != Py_None) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
915 if (!PyDict_Check(dict)) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
916 PyErr_Format(PyExc_TypeError,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
917 "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
918 Py_TYPE(dict)->tp_name);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
919 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
920 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
921 if (self->dict) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
922 /* 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
923 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
924 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
925 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
926 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
927 else {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
928 Py_INCREF(dict);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
929 self->dict = dict;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
930 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
931 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
932
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
933 Py_RETURN_NONE;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
934 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
935
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
936
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
937 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
938 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
939 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
940 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
941 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
942 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
943
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
944 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
945 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
946 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
947 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
948 CHECK_CLOSED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
949 Py_RETURN_FALSE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
950 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
951
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
952 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
953 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
954 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
955 CHECK_INITIALIZED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
956 CHECK_CLOSED(self);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
957 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
958 Py_RETURN_NONE;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
959 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
960 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
961
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
962 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
963 {"close", (PyCFunction)stringio_close, METH_NOARGS, stringio_close_doc},
64383
8b94f15cb650 Merged revisions 84438 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58631
diff changeset
964 {"getvalue", (PyCFunction)stringio_getvalue, METH_NOARGS, stringio_getvalue_doc},
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
965 {"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
966 {"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
967 {"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
968 {"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
969 {"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
970 {"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
971
78852
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
972 {"seekable", (PyCFunction)stringio_seekable, METH_NOARGS, stringio_seekable_doc},
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
973 {"readable", (PyCFunction)stringio_readable, METH_NOARGS, stringio_readable_doc},
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
974 {"writable", (PyCFunction)stringio_writable, METH_NOARGS, stringio_writable_doc},
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
975
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
976 {"__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
977 {"__setstate__", (PyCFunction)stringio_setstate, METH_O},
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
978 {NULL, NULL} /* sentinel */
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
979 };
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
980
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
981 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
982 {"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
983 {"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
984 /* (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
985 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
986 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
987 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
988 will be found.
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
989 */
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
990 {"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
991 {NULL}
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
992 };
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
993
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
994 PyTypeObject PyStringIO_Type = {
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
995 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
996 "_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
997 sizeof(stringio), /*tp_basicsize*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
998 0, /*tp_itemsize*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
999 (destructor)stringio_dealloc, /*tp_dealloc*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1000 0, /*tp_print*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1001 0, /*tp_getattr*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1002 0, /*tp_setattr*/
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49360
diff changeset
1003 0, /*tp_reserved*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1004 0, /*tp_repr*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1005 0, /*tp_as_number*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1006 0, /*tp_as_sequence*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1007 0, /*tp_as_mapping*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1008 0, /*tp_hash*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1009 0, /*tp_call*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1010 0, /*tp_str*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1011 0, /*tp_getattro*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1012 0, /*tp_setattro*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1013 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
1014 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
1015 | 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
1016 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
1017 (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
1018 (inquiry)stringio_clear, /*tp_clear*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1019 0, /*tp_richcompare*/
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 54599
diff changeset
1020 offsetof(stringio, weakreflist), /*tp_weaklistoffset*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1021 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
1022 (iternextfunc)stringio_iternext, /*tp_iternext*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1023 stringio_methods, /*tp_methods*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1024 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
1025 stringio_getset, /*tp_getset*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1026 0, /*tp_base*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1027 0, /*tp_dict*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1028 0, /*tp_descr_get*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1029 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
1030 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
1031 (initproc)stringio_init, /*tp_init*/
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1032 0, /*tp_alloc*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1033 stringio_new, /*tp_new*/
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1034 };