annotate Modules/_io/stringio.c @ 73798:6cde416ef03b

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