Mercurial > cpython-lru_cache-weakref
annotate Modules/_io/stringio.c @ 71285:8bc9dbc61ba6 3.2
Issue #12326: document the recommended idiom for checking sys.platform on Unix systems.
Also, point to the various alternatives.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Sat, 09 Jul 2011 15:54:23 +0200 |
| parents | dc245d681901 |
| children | 524931e5aebf 8beaa9a37387 |
| rev | line source |
|---|---|
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
1 #define PY_SSIZE_T_CLEAN |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
2 #include "Python.h" |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
3 #include "structmember.h" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
4 #include "_iomodule.h" |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
5 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
6 /* Implementation note: the buffer is always at least one character longer |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
7 than the enclosed string, for proper functioning of _PyIO_find_line_ending. |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
8 */ |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
9 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
10 typedef struct { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
11 PyObject_HEAD |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
12 Py_UNICODE *buf; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
13 Py_ssize_t pos; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
14 Py_ssize_t string_size; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
15 size_t buf_size; |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
16 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
17 char ok; /* initialized? */ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
18 char closed; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
19 char readuniversal; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
20 char readtranslate; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
21 PyObject *decoder; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
22 PyObject *readnl; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
23 PyObject *writenl; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
24 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
25 PyObject *dict; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
26 PyObject *weakreflist; |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
27 } stringio; |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
28 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
29 #define CHECK_INITIALIZED(self) \ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
30 if (self->ok <= 0) { \ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
31 PyErr_SetString(PyExc_ValueError, \ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
32 "I/O operation on uninitialized object"); \ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
33 return NULL; \ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
34 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
35 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
36 #define CHECK_CLOSED(self) \ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
37 if (self->closed) { \ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
38 PyErr_SetString(PyExc_ValueError, \ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
39 "I/O operation on closed file"); \ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
40 return NULL; \ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
41 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
42 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
43 PyDoc_STRVAR(stringio_doc, |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
44 "Text I/O implementation using an in-memory buffer.\n" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
45 "\n" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
46 "The initial_value argument sets the value of object. The newline\n" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
47 "argument is like the one of TextIOWrapper's constructor."); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
48 |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
49 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
50 /* Internal routine for changing the size, in terms of characters, of the |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
51 buffer of StringIO objects. The caller should ensure that the 'size' |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
52 argument is non-negative. Returns 0 on success, -1 otherwise. */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
53 static int |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
54 resize_buffer(stringio *self, size_t size) |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
55 { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
56 /* Here, unsigned types are used to avoid dealing with signed integer |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
57 overflow, which is undefined in C. */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
58 size_t alloc = self->buf_size; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
59 Py_UNICODE *new_buf = NULL; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
60 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
61 assert(self->buf != NULL); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
62 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
63 /* Reserve one more char for line ending detection. */ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
64 size = size + 1; |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
65 /* For simplicity, stay in the range of the signed type. Anyway, Python |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
66 doesn't allow strings to be longer than this. */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
67 if (size > PY_SSIZE_T_MAX) |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
68 goto overflow; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
69 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
70 if (size < alloc / 2) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
71 /* Major downsize; resize down to exact size. */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
72 alloc = size + 1; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
73 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
74 else if (size < alloc) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
75 /* Within allocated size; quick exit */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
76 return 0; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
77 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
78 else if (size <= alloc * 1.125) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
79 /* Moderate upsize; overallocate similar to list_resize() */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
80 alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
81 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
82 else { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
83 /* Major upsize; resize up to exact size */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
84 alloc = size + 1; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
85 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
86 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
87 if (alloc > ((size_t)-1) / sizeof(Py_UNICODE)) |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
88 goto overflow; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
89 new_buf = (Py_UNICODE *)PyMem_Realloc(self->buf, |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
90 alloc * sizeof(Py_UNICODE)); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
91 if (new_buf == NULL) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
92 PyErr_NoMemory(); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
93 return -1; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
94 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
95 self->buf_size = alloc; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
96 self->buf = new_buf; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
97 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
98 return 0; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
99 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
100 overflow: |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
101 PyErr_SetString(PyExc_OverflowError, |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
102 "new buffer size too large"); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
103 return -1; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
104 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
105 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
106 /* Internal routine for writing a whole PyUnicode object to the buffer of a |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
107 StringIO object. Returns 0 on success, or -1 on error. */ |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
108 static Py_ssize_t |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
109 write_str(stringio *self, PyObject *obj) |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
110 { |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
111 Py_UNICODE *str; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
112 Py_ssize_t len; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
113 PyObject *decoded = NULL; |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
114 assert(self->buf != NULL); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
115 assert(self->pos >= 0); |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
116 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
117 if (self->decoder != NULL) { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
118 decoded = _PyIncrementalNewlineDecoder_decode( |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
119 self->decoder, obj, 1 /* always final */); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
120 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
121 else { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
122 decoded = obj; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
123 Py_INCREF(decoded); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
124 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
125 if (self->writenl) { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
126 PyObject *translated = PyUnicode_Replace( |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
127 decoded, _PyIO_str_nl, self->writenl, -1); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
128 Py_DECREF(decoded); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
129 decoded = translated; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
130 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
131 if (decoded == NULL) |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
132 return -1; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
133 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
134 assert(PyUnicode_Check(decoded)); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
135 str = PyUnicode_AS_UNICODE(decoded); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
136 len = PyUnicode_GET_SIZE(decoded); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
137 |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
138 assert(len >= 0); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
139 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
140 /* This overflow check is not strictly necessary. However, it avoids us to |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
141 deal with funky things like comparing an unsigned and a signed |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
142 integer. */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
143 if (self->pos > PY_SSIZE_T_MAX - len) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
144 PyErr_SetString(PyExc_OverflowError, |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
145 "new position too large"); |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
146 goto fail; |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
147 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
148 if (self->pos + len > self->string_size) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
149 if (resize_buffer(self, self->pos + len) < 0) |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
150 goto fail; |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
151 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
152 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
153 if (self->pos > self->string_size) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
154 /* In case of overseek, pad with null bytes the buffer region between |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
155 the end of stream and the current position. |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
156 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
157 0 lo string_size hi |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
158 | |<---used--->|<----------available----------->| |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
159 | | <--to pad-->|<---to write---> | |
|
68585
bf94b6a73fc8
#11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64383
diff
changeset
|
160 0 buf position |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
161 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
162 */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
163 memset(self->buf + self->string_size, '\0', |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
164 (self->pos - self->string_size) * sizeof(Py_UNICODE)); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
165 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
166 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
167 /* Copy the data to the internal buffer, overwriting some of the |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
168 existing data if self->pos < self->string_size. */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
169 memcpy(self->buf + self->pos, str, len * sizeof(Py_UNICODE)); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
170 self->pos += len; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
171 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
172 /* Set the new length of the internal string if it has changed. */ |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
173 if (self->string_size < self->pos) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
174 self->string_size = self->pos; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
175 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
176 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
177 Py_DECREF(decoded); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
178 return 0; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
179 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
180 fail: |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
181 Py_XDECREF(decoded); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
182 return -1; |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
183 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
184 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
185 PyDoc_STRVAR(stringio_getvalue_doc, |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
186 "Retrieve the entire contents of the object."); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
187 |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
188 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
189 stringio_getvalue(stringio *self) |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
190 { |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
191 CHECK_INITIALIZED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
192 CHECK_CLOSED(self); |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
193 return PyUnicode_FromUnicode(self->buf, self->string_size); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
194 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
195 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
196 PyDoc_STRVAR(stringio_tell_doc, |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
197 "Tell the current file position."); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
198 |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
199 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
200 stringio_tell(stringio *self) |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
201 { |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
202 CHECK_INITIALIZED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
203 CHECK_CLOSED(self); |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
204 return PyLong_FromSsize_t(self->pos); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
205 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
206 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
207 PyDoc_STRVAR(stringio_read_doc, |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
208 "Read at most n characters, returned as a string.\n" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
209 "\n" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
210 "If the argument is negative or omitted, read until EOF\n" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
211 "is reached. Return an empty string at EOF.\n"); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
212 |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
213 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
214 stringio_read(stringio *self, PyObject *args) |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
215 { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
216 Py_ssize_t size, n; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
217 Py_UNICODE *output; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
218 PyObject *arg = Py_None; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
219 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
220 CHECK_INITIALIZED(self); |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
221 if (!PyArg_ParseTuple(args, "|O:read", &arg)) |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
222 return NULL; |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
223 CHECK_CLOSED(self); |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
224 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
225 if (PyNumber_Check(arg)) { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
226 size = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
|
49360
1f4ea0229ec6
Fix a probable merge glitch in r66695:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49353
diff
changeset
|
227 if (size == -1 && PyErr_Occurred()) |
|
1f4ea0229ec6
Fix a probable merge glitch in r66695:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
49353
diff
changeset
|
228 return NULL; |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
229 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
230 else if (arg == Py_None) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
231 /* Read until EOF is reached, by default. */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
232 size = -1; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
233 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
234 else { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
235 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
236 Py_TYPE(arg)->tp_name); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
237 return NULL; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
238 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
239 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
240 /* adjust invalid sizes */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
241 n = self->string_size - self->pos; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
242 if (size < 0 || size > n) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
243 size = n; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
244 if (size < 0) |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
245 size = 0; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
246 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
247 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
248 output = self->buf + self->pos; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
249 self->pos += size; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
250 return PyUnicode_FromUnicode(output, size); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
251 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
252 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
253 /* Internal helper, used by stringio_readline and stringio_iternext */ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
254 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
255 _stringio_readline(stringio *self, Py_ssize_t limit) |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
256 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
257 Py_UNICODE *start, *end, old_char; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
258 Py_ssize_t len, consumed; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
259 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
260 /* In case of overseek, return the empty string */ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
261 if (self->pos >= self->string_size) |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
262 return PyUnicode_FromString(""); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
263 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
264 start = self->buf + self->pos; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
265 if (limit < 0 || limit > self->string_size - self->pos) |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
266 limit = self->string_size - self->pos; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
267 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
268 end = start + limit; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
269 old_char = *end; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
270 *end = '\0'; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
271 len = _PyIO_find_line_ending( |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
272 self->readtranslate, self->readuniversal, self->readnl, |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
273 start, end, &consumed); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
274 *end = old_char; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
275 /* If we haven't found any line ending, we just return everything |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
276 (`consumed` is ignored). */ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
277 if (len < 0) |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
278 len = limit; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
279 self->pos += len; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
280 return PyUnicode_FromUnicode(start, len); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
281 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
282 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
283 PyDoc_STRVAR(stringio_readline_doc, |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
284 "Read until newline or EOF.\n" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
285 "\n" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
286 "Returns an empty string if EOF is hit immediately.\n"); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
287 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
288 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
289 stringio_readline(stringio *self, PyObject *args) |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
290 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
291 PyObject *arg = Py_None; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
292 Py_ssize_t limit = -1; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
293 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
294 CHECK_INITIALIZED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
295 if (!PyArg_ParseTuple(args, "|O:readline", &arg)) |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
296 return NULL; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
297 CHECK_CLOSED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
298 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
299 if (PyNumber_Check(arg)) { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
300 limit = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
301 if (limit == -1 && PyErr_Occurred()) |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
302 return NULL; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
303 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
304 else if (arg != Py_None) { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
305 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
306 Py_TYPE(arg)->tp_name); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
307 return NULL; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
308 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
309 return _stringio_readline(self, limit); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
310 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
311 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
312 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
313 stringio_iternext(stringio *self) |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
314 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
315 PyObject *line; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
316 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
317 CHECK_INITIALIZED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
318 CHECK_CLOSED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
319 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
320 if (Py_TYPE(self) == &PyStringIO_Type) { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
321 /* Skip method call overhead for speed */ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
322 line = _stringio_readline(self, -1); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
323 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
324 else { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
325 /* XXX is subclassing StringIO really supported? */ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
326 line = PyObject_CallMethodObjArgs((PyObject *)self, |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
327 _PyIO_str_readline, NULL); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
328 if (line && !PyUnicode_Check(line)) { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
329 PyErr_Format(PyExc_IOError, |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
330 "readline() should have returned an str object, " |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
331 "not '%.200s'", Py_TYPE(line)->tp_name); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
332 Py_DECREF(line); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
333 return NULL; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
334 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
335 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
336 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
337 if (line == NULL) |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
338 return NULL; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
339 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
340 if (PyUnicode_GET_SIZE(line) == 0) { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
341 /* Reached EOF */ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
342 Py_DECREF(line); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
343 return NULL; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
344 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
345 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
346 return line; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
347 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
348 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
349 PyDoc_STRVAR(stringio_truncate_doc, |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
350 "Truncate size to pos.\n" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
351 "\n" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
352 "The pos argument defaults to the current file position, as\n" |
|
58631
d887db501a22
Merged revisions 77895-77896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
55609
diff
changeset
|
353 "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
|
354 "Returns the new absolute position.\n"); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
355 |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
356 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
357 stringio_truncate(stringio *self, PyObject *args) |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
358 { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
359 Py_ssize_t size; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
360 PyObject *arg = Py_None; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
361 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
362 CHECK_INITIALIZED(self); |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
363 if (!PyArg_ParseTuple(args, "|O:truncate", &arg)) |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
364 return NULL; |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
365 CHECK_CLOSED(self); |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
366 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
367 if (PyNumber_Check(arg)) { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
368 size = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
|
49353
598d19936186
check for errors after PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents:
47625
diff
changeset
|
369 if (size == -1 && PyErr_Occurred()) |
|
598d19936186
check for errors after PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents:
47625
diff
changeset
|
370 return NULL; |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
371 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
372 else if (arg == Py_None) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
373 /* Truncate to current position if no argument is passed. */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
374 size = self->pos; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
375 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
376 else { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
377 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
378 Py_TYPE(arg)->tp_name); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
379 return NULL; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
380 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
381 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
382 if (size < 0) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
383 PyErr_Format(PyExc_ValueError, |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
384 "Negative size value %zd", size); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
385 return NULL; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
386 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
387 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
388 if (size < self->string_size) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
389 if (resize_buffer(self, size) < 0) |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
390 return NULL; |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
391 self->string_size = size; |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
392 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
393 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
394 return PyLong_FromSsize_t(size); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
395 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
396 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
397 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
|
398 "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
|
399 "\n" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
400 "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
|
401 " 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
|
402 " 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
|
403 " 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
|
404 "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
|
405 |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
406 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
407 stringio_seek(stringio *self, PyObject *args) |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
408 { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
409 Py_ssize_t pos; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
410 int mode = 0; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
411 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
412 CHECK_INITIALIZED(self); |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
413 if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &mode)) |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
414 return NULL; |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
415 CHECK_CLOSED(self); |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
416 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
417 if (mode != 0 && mode != 1 && mode != 2) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
418 PyErr_Format(PyExc_ValueError, |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
419 "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
|
420 return NULL; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
421 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
422 else if (pos < 0 && mode == 0) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
423 PyErr_Format(PyExc_ValueError, |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
424 "Negative seek position %zd", pos); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
425 return NULL; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
426 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
427 else if (mode != 0 && pos != 0) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
428 PyErr_SetString(PyExc_IOError, |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
429 "Can't do nonzero cur-relative seeks"); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
430 return NULL; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
431 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
432 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
433 /* mode 0: offset relative to beginning of the string. |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
434 mode 1: no change to current position. |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
435 mode 2: change position to end of file. */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
436 if (mode == 1) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
437 pos = self->pos; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
438 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
439 else if (mode == 2) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
440 pos = self->string_size; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
441 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
442 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
443 self->pos = pos; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
444 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
445 return PyLong_FromSsize_t(self->pos); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
446 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
447 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
448 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
|
449 "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
|
450 "\n" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
451 "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
|
452 "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
|
453 |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
454 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
455 stringio_write(stringio *self, PyObject *obj) |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
456 { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
457 Py_ssize_t size; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
458 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
459 CHECK_INITIALIZED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
460 if (!PyUnicode_Check(obj)) { |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
461 PyErr_Format(PyExc_TypeError, "string argument expected, got '%s'", |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
462 Py_TYPE(obj)->tp_name); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
463 return NULL; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
464 } |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
465 CHECK_CLOSED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
466 size = PyUnicode_GET_SIZE(obj); |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
467 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
468 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
|
469 return NULL; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
470 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
471 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
|
472 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
473 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
474 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
|
475 "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
|
476 "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
|
477 "\n" |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
478 "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
|
479 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
480 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
481 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
|
482 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
483 self->closed = 1; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
484 /* 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
|
485 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
|
486 return NULL; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
487 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
|
488 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
|
489 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
|
490 Py_RETURN_NONE; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
491 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
492 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
493 static int |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
494 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
|
495 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
496 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
|
497 return 0; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
498 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
499 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
500 static int |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
501 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
|
502 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
503 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
|
504 return 0; |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
505 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
506 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
507 static void |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
508 stringio_dealloc(stringio *self) |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
509 { |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
510 _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
|
511 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
|
512 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
|
513 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
|
514 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
|
515 } |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
516 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
|
517 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
|
518 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
|
519 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
|
520 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
|
521 PyObject_ClearWeakRefs((PyObject *) self); |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
522 Py_TYPE(self)->tp_free(self); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
523 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
524 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
525 static PyObject * |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
526 stringio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
527 { |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
528 stringio *self; |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
529 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
530 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
|
531 self = (stringio *)type->tp_alloc(type, 0); |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
532 if (self == NULL) |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
533 return NULL; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
534 |
|
55398
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
535 /* 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
|
536 initialize them here. */ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
537 |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
538 self->buf = (Py_UNICODE *)PyMem_Malloc(0); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
539 if (self->buf == NULL) { |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
540 Py_DECREF(self); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
541 return PyErr_NoMemory(); |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
542 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
543 |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
544 return (PyObject *)self; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
545 } |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
546 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
547 static int |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
548 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
|
549 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
550 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
|
551 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
|
552 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
|
553 char *newline = "\n"; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
554 |
|
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
|
555 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
|
556 &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
|
557 return -1; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
558 |
|
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
|
559 /* 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
|
560 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
|
561 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
|
562 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
|
563 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
|
564 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
|
565 } |
|
9d40586a6afc
Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54752
diff
changeset
|
566 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
|
567 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
|
568 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
|
569 "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
|
570 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
|
571 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
|
572 } |
|
9d40586a6afc
Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54752
diff
changeset
|
573 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
|
574 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
|
575 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
|
576 } |
|
9d40586a6afc
Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54752
diff
changeset
|
577 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
578 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
|
579 && !(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
|
580 && !(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
|
581 && !(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
|
582 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
|
583 "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
|
584 return -1; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
585 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
586 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
|
587 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
|
588 "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
|
589 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
|
590 return -1; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
591 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
592 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
593 self->ok = 0; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
594 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
595 Py_CLEAR(self->readnl); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
596 Py_CLEAR(self->writenl); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
597 Py_CLEAR(self->decoder); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
598 |
|
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
|
599 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
|
600 (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
|
601 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
602 if (newline) { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
603 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
|
604 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
|
605 return -1; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
606 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
607 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
|
608 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
|
609 /* 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
|
610 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
|
611 a no-op. |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
612 (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
|
613 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
|
614 */ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
615 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
|
616 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
|
617 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
|
618 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
619 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
620 if (self->readuniversal) { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
621 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
|
622 (PyObject *)&PyIncrementalNewlineDecoder_Type, |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
623 "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
|
624 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
|
625 return -1; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
626 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
627 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
628 /* 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
|
629 and copy it */ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
630 self->string_size = 0; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
631 if (value && value != Py_None) { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
632 Py_ssize_t len = PyUnicode_GetSize(value); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
633 /* 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
|
634 the string length. */ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
635 if (resize_buffer(self, len) < 0) |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
636 return -1; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
637 self->pos = 0; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
638 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
|
639 return -1; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
640 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
641 else { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
642 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
|
643 return -1; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
644 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
645 self->pos = 0; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
646 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
647 self->closed = 0; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
648 self->ok = 1; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
649 return 0; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
650 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
651 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
652 /* 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
|
653 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
654 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
|
655 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
656 CHECK_INITIALIZED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
657 Py_RETURN_TRUE; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
658 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
659 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
660 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
661 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
|
662 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
663 CHECK_INITIALIZED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
664 Py_RETURN_TRUE; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
665 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
666 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
667 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
668 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
|
669 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
670 CHECK_INITIALIZED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
671 Py_RETURN_TRUE; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
672 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
673 |
|
55398
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
674 /* Pickling support. |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
675 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
676 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
|
677 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
|
678 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
|
679 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
|
680 representation compact. |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
681 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
682 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
|
683 supported. |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
684 */ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
685 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
686 static PyObject * |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
687 stringio_getstate(stringio *self) |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
688 { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
689 PyObject *initvalue = stringio_getvalue(self); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
690 PyObject *dict; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
691 PyObject *state; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
692 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
693 if (initvalue == NULL) |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
694 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
695 if (self->dict == NULL) { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
696 Py_INCREF(Py_None); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
697 dict = Py_None; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
698 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
699 else { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
700 dict = PyDict_Copy(self->dict); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
701 if (dict == NULL) |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
702 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
703 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
704 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
705 state = Py_BuildValue("(OOnN)", initvalue, |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
706 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
|
707 self->pos, dict); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
708 Py_DECREF(initvalue); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
709 return state; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
710 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
711 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
712 static PyObject * |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
713 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
|
714 { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
715 PyObject *initarg; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
716 PyObject *position_obj; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
717 PyObject *dict; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
718 Py_ssize_t pos; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
719 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
720 assert(state != NULL); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
721 CHECK_CLOSED(self); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
722 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
723 /* 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
|
724 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
|
725 backward-compatibility. */ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
726 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
|
727 PyErr_Format(PyExc_TypeError, |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
728 "%.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
|
729 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
|
730 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
731 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
732 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
733 /* Initialize the object's state. */ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
734 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
|
735 if (initarg == NULL) |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
736 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
737 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
|
738 Py_DECREF(initarg); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
739 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
740 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
741 Py_DECREF(initarg); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
742 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
743 /* 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
|
744 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
|
745 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
|
746 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
|
747 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
|
748 buffer completely. */ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
749 { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
750 Py_UNICODE *buf = PyUnicode_AS_UNICODE(PyTuple_GET_ITEM(state, 0)); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
751 Py_ssize_t bufsize = PyUnicode_GET_SIZE(PyTuple_GET_ITEM(state, 0)); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
752 if (resize_buffer(self, bufsize) < 0) |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
753 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
754 memcpy(self->buf, buf, bufsize * sizeof(Py_UNICODE)); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
755 self->string_size = bufsize; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
756 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
757 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
758 /* 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
|
759 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
|
760 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
|
761 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
|
762 if (!PyLong_Check(position_obj)) { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
763 PyErr_Format(PyExc_TypeError, |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
764 "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
|
765 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
|
766 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
767 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
768 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
|
769 if (pos == -1 && PyErr_Occurred()) |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
770 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
771 if (pos < 0) { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
772 PyErr_SetString(PyExc_ValueError, |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
773 "position value cannot be negative"); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
774 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
775 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
776 self->pos = pos; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
777 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
778 /* 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
|
779 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
|
780 if (dict != Py_None) { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
781 if (!PyDict_Check(dict)) { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
782 PyErr_Format(PyExc_TypeError, |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
783 "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
|
784 Py_TYPE(dict)->tp_name); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
785 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
786 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
787 if (self->dict) { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
788 /* 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
|
789 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
|
790 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
|
791 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
792 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
793 else { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
794 Py_INCREF(dict); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
795 self->dict = dict; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
796 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
797 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
798 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
799 Py_RETURN_NONE; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
800 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
801 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
802 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
803 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
804 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
|
805 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
806 CHECK_INITIALIZED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
807 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
|
808 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
809 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
810 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
811 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
|
812 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
813 CHECK_INITIALIZED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
814 CHECK_CLOSED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
815 Py_RETURN_FALSE; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
816 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
817 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
818 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
819 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
|
820 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
821 CHECK_INITIALIZED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
822 CHECK_CLOSED(self); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
823 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
|
824 Py_RETURN_NONE; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
825 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
|
826 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
827 |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
828 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
|
829 {"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
|
830 {"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
|
831 {"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
|
832 {"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
|
833 {"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
|
834 {"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
|
835 {"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
|
836 {"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
|
837 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
838 {"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
|
839 {"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
|
840 {"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
|
841 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55397
diff
changeset
|
842 {"__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
|
843 {"__setstate__", (PyCFunction)stringio_setstate, METH_O}, |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
844 {NULL, NULL} /* sentinel */ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
845 }; |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
846 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
847 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
|
848 {"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
|
849 {"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
|
850 /* (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
|
851 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
|
852 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
|
853 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
|
854 will be found. |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
855 */ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
856 {"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
|
857 {NULL} |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
858 }; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
859 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
860 PyTypeObject PyStringIO_Type = { |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
861 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
|
862 "_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
|
863 sizeof(stringio), /*tp_basicsize*/ |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
864 0, /*tp_itemsize*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
865 (destructor)stringio_dealloc, /*tp_dealloc*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
866 0, /*tp_print*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
867 0, /*tp_getattr*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
868 0, /*tp_setattr*/ |
|
51271
87fbbf22adf3
Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
49360
diff
changeset
|
869 0, /*tp_reserved*/ |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
870 0, /*tp_repr*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
871 0, /*tp_as_number*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
872 0, /*tp_as_sequence*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
873 0, /*tp_as_mapping*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
874 0, /*tp_hash*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
875 0, /*tp_call*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
876 0, /*tp_str*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
877 0, /*tp_getattro*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
878 0, /*tp_setattro*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
879 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
|
880 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
|
881 | 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
|
882 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
|
883 (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
|
884 (inquiry)stringio_clear, /*tp_clear*/ |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
885 0, /*tp_richcompare*/ |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
54599
diff
changeset
|
886 offsetof(stringio, weakreflist), /*tp_weaklistoffset*/ |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
887 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
|
888 (iternextfunc)stringio_iternext, /*tp_iternext*/ |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
889 stringio_methods, /*tp_methods*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
890 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
|
891 stringio_getset, /*tp_getset*/ |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
892 0, /*tp_base*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
893 0, /*tp_dict*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
894 0, /*tp_descr_get*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
895 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
|
896 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
|
897 (initproc)stringio_init, /*tp_init*/ |
|
47625
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
898 0, /*tp_alloc*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
899 stringio_new, /*tp_new*/ |
|
1bfea956d0d7
Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
900 }; |
