annotate Modules/_bytesio.c @ 50784:65c1938fb9ff legacy-trunk

#1162154: inspect.getmembers() now skips attributes that raise AttributeError, e.g. a __slots__ attribute which has not been set.
author Amaury Forgeot d'Arc <amauryfa@gmail.com>
date Tue, 13 Jan 2009 23:39:22 +0000
parents 6a73134f9603
children 6c13bf35d166
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46805
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1 #include "Python.h"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3 typedef struct {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4 PyObject_HEAD
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5 char *buf;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6 Py_ssize_t pos;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7 Py_ssize_t string_size;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
8 size_t buf_size;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
9 } BytesIOObject;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
10
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
11 #define CHECK_CLOSED(self) \
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
12 if ((self)->buf == NULL) { \
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
13 PyErr_SetString(PyExc_ValueError, \
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
14 "I/O operation on closed file."); \
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
15 return NULL; \
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
16 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
17
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
18 /* Internal routine to get a line from the buffer of a BytesIO
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
19 object. Returns the length between the current position to the
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
20 next newline character. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
21 static Py_ssize_t
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
22 get_line(BytesIOObject *self, char **output)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
23 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
24 char *n;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
25 const char *str_end;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
26 Py_ssize_t len;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
27
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
28 assert(self->buf != NULL);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
29
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
30 /* Move to the end of the line, up to the end of the string, s. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
31 str_end = self->buf + self->string_size;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
32 for (n = self->buf + self->pos;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
33 n < str_end && *n != '\n';
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
34 n++);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
35
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
36 /* Skip the newline character */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
37 if (n < str_end)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
38 n++;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
39
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
40 /* Get the length from the current position to the end of the line. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
41 len = n - (self->buf + self->pos);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
42 *output = self->buf + self->pos;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
43
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
44 assert(len >= 0);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
45 assert(self->pos < PY_SSIZE_T_MAX - len);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
46 self->pos += len;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
47
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
48 return len;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
49 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
50
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
51 /* Internal routine for changing the size of the buffer of BytesIO objects.
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
52 The caller should ensure that the 'size' argument is non-negative. Returns
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
53 0 on success, -1 otherwise. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
54 static int
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
55 resize_buffer(BytesIOObject *self, size_t size)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
56 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
57 /* Here, unsigned types are used to avoid dealing with signed integer
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
58 overflow, which is undefined in C. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
59 size_t alloc = self->buf_size;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
60 char *new_buf = NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
61
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
62 assert(self->buf != NULL);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
63
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
64 /* For simplicity, stay in the range of the signed type. Anyway, Python
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
65 doesn't allow strings to be longer than this. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
66 if (size > PY_SSIZE_T_MAX)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
67 goto overflow;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
68
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
69 if (size < alloc / 2) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
70 /* Major downsize; resize down to exact size. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
71 alloc = size + 1;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
72 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
73 else if (size < alloc) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
74 /* Within allocated size; quick exit */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
75 return 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
76 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
77 else if (size <= alloc * 1.125) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
78 /* Moderate upsize; overallocate similar to list_resize() */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
79 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
80 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
81 else {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
82 /* Major upsize; resize up to exact size */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
83 alloc = size + 1;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
84 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
85
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
86 if (alloc > ((size_t)-1) / sizeof(char))
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
87 goto overflow;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
88 new_buf = (char *)PyMem_Realloc(self->buf, alloc * sizeof(char));
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
89 if (new_buf == NULL) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
90 PyErr_NoMemory();
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
91 return -1;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
92 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
93 self->buf_size = alloc;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
94 self->buf = new_buf;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
95
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
96 return 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
97
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
98 overflow:
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
99 PyErr_SetString(PyExc_OverflowError,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
100 "new buffer size too large");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
101 return -1;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
102 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
103
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
104 /* Internal routine for writing a string of bytes to the buffer of a BytesIO
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
105 object. Returns the number of bytes wrote, or -1 on error. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
106 static Py_ssize_t
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
107 write_bytes(BytesIOObject *self, const char *bytes, Py_ssize_t len)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
108 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
109 assert(self->buf != NULL);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
110 assert(self->pos >= 0);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
111 assert(len >= 0);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
112
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
113 if ((size_t)self->pos + len > self->buf_size) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
114 if (resize_buffer(self, (size_t)self->pos + len) < 0)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
115 return -1;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
116 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
117
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
118 if (self->pos > self->string_size) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
119 /* In case of overseek, pad with null bytes the buffer region between
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
120 the end of stream and the current position.
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
121
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
122 0 lo string_size hi
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
123 | |<---used--->|<----------available----------->|
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
124 | | <--to pad-->|<---to write---> |
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
125 0 buf position
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
126 */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
127 memset(self->buf + self->string_size, '\0',
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
128 (self->pos - self->string_size) * sizeof(char));
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
129 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
130
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
131 /* Copy the data to the internal buffer, overwriting some of the existing
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
132 data if self->pos < self->string_size. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
133 memcpy(self->buf + self->pos, bytes, len);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
134 self->pos += len;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
135
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
136 /* Set the new length of the internal string if it has changed. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
137 if (self->string_size < self->pos) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
138 self->string_size = self->pos;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
139 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
140
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
141 return len;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
142 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
143
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
144 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
145 bytesio_get_closed(BytesIOObject *self)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
146 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
147 if (self->buf == NULL)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
148 Py_RETURN_TRUE;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
149 else
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
150 Py_RETURN_FALSE;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
151 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
152
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
153 /* Generic getter for the writable, readable and seekable properties */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
154 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
155 return_true(BytesIOObject *self)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
156 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
157 Py_RETURN_TRUE;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
158 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
159
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
160 PyDoc_STRVAR(flush_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
161 "flush() -> None. Does nothing.");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
162
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
163 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
164 bytesio_flush(BytesIOObject *self)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
165 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
166 Py_RETURN_NONE;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
167 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
168
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
169 PyDoc_STRVAR(getval_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
170 "getvalue() -> bytes.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
171 "\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
172 "Retrieve the entire contents of the BytesIO object.");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
173
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
174 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
175 bytesio_getvalue(BytesIOObject *self)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
176 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
177 CHECK_CLOSED(self);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
178 return PyString_FromStringAndSize(self->buf, self->string_size);
46805
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
179 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
180
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
181 PyDoc_STRVAR(isatty_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
182 "isatty() -> False.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
183 "\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
184 "Always returns False since BytesIO objects are not connected\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
185 "to a tty-like device.");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
186
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
187 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
188 bytesio_isatty(BytesIOObject *self)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
189 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
190 CHECK_CLOSED(self);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
191 Py_RETURN_FALSE;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
192 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
193
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
194 PyDoc_STRVAR(tell_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
195 "tell() -> current file position, an integer\n");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
196
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
197 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
198 bytesio_tell(BytesIOObject *self)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
199 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
200 CHECK_CLOSED(self);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
201 return PyInt_FromSsize_t(self->pos);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
202 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
203
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
204 PyDoc_STRVAR(read_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
205 "read([size]) -> read at most size bytes, returned as a string.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
206 "\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
207 "If the size argument is negative, read until EOF is reached.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
208 "Return an empty string at EOF.");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
209
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
210 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
211 bytesio_read(BytesIOObject *self, PyObject *args)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
212 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
213 Py_ssize_t size, n;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
214 char *output;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
215 PyObject *arg = Py_None;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
216
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
217 CHECK_CLOSED(self);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
218
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
219 if (!PyArg_ParseTuple(args, "|O:read", &arg))
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
220 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
221
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
222 if (PyInt_Check(arg)) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
223 size = PyInt_AsSsize_t(arg);
49351
6a73134f9603 Victor Stinner's patches to check the return result of PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
224 if (size == -1 && PyErr_Occurred())
6a73134f9603 Victor Stinner's patches to check the return result of PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
225 return NULL;
46805
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
226 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
227 else if (arg == Py_None) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
228 /* Read until EOF is reached, by default. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
229 size = -1;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
230 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
231 else {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
232 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
233 Py_TYPE(arg)->tp_name);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
234 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
235 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
236
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
237 /* adjust invalid sizes */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
238 n = self->string_size - self->pos;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
239 if (size < 0 || size > n) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
240 size = n;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
241 if (size < 0)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
242 size = 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
243 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
244
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
245 assert(self->buf != NULL);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
246 output = self->buf + self->pos;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
247 self->pos += size;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
248
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
249 return PyString_FromStringAndSize(output, size);
46805
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
250 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
251
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
252
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
253 PyDoc_STRVAR(read1_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
254 "read1(size) -> read at most size bytes, returned as a string.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
255 "\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
256 "If the size argument is negative or omitted, read until EOF is reached.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
257 "Return an empty string at EOF.");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
258
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
259 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
260 bytesio_read1(BytesIOObject *self, PyObject *n)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
261 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
262 PyObject *arg, *res;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
263
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
264 arg = PyTuple_Pack(1, n);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
265 if (arg == NULL)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
266 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
267 res = bytesio_read(self, arg);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
268 Py_DECREF(arg);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
269 return res;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
270 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
271
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
272 PyDoc_STRVAR(readline_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
273 "readline([size]) -> next line from the file, as a string.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
274 "\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
275 "Retain newline. A non-negative size argument limits the maximum\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
276 "number of bytes to return (an incomplete line may be returned then).\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
277 "Return an empty string at EOF.\n");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
278
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
279 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
280 bytesio_readline(BytesIOObject *self, PyObject *args)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
281 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
282 Py_ssize_t size, n;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
283 char *output;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
284 PyObject *arg = Py_None;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
285
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
286 CHECK_CLOSED(self);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
287
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
288 if (!PyArg_ParseTuple(args, "|O:readline", &arg))
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
289 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
290
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
291 if (PyInt_Check(arg)) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
292 size = PyInt_AsSsize_t(arg);
49351
6a73134f9603 Victor Stinner's patches to check the return result of PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
293 if (size == -1 && PyErr_Occurred())
6a73134f9603 Victor Stinner's patches to check the return result of PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
294 return NULL;
46805
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
295 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
296 else if (arg == Py_None) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
297 /* No size limit, by default. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
298 size = -1;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
299 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
300 else {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
301 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
302 Py_TYPE(arg)->tp_name);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
303 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
304 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
305
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
306 n = get_line(self, &output);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
307
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
308 if (size >= 0 && size < n) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
309 size = n - size;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
310 n -= size;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
311 self->pos -= size;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
312 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
313
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
314 return PyString_FromStringAndSize(output, n);
46805
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
315 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
316
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
317 PyDoc_STRVAR(readlines_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
318 "readlines([size]) -> list of strings, each a line from the file.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
319 "\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
320 "Call readline() repeatedly and return a list of the lines so read.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
321 "The optional size argument, if given, is an approximate bound on the\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
322 "total number of bytes in the lines returned.\n");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
323
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
324 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
325 bytesio_readlines(BytesIOObject *self, PyObject *args)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
326 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
327 Py_ssize_t maxsize, size, n;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
328 PyObject *result, *line;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
329 char *output;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
330 PyObject *arg = Py_None;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
331
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
332 CHECK_CLOSED(self);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
333
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
334 if (!PyArg_ParseTuple(args, "|O:readlines", &arg))
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
335 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
336
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
337 if (PyInt_Check(arg)) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
338 maxsize = PyInt_AsSsize_t(arg);
49351
6a73134f9603 Victor Stinner's patches to check the return result of PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
339 if (maxsize == -1 && PyErr_Occurred())
6a73134f9603 Victor Stinner's patches to check the return result of PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
340 return NULL;
46805
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
341 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
342 else if (arg == Py_None) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
343 /* No size limit, by default. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
344 maxsize = -1;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
345 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
346 else {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
347 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
348 Py_TYPE(arg)->tp_name);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
349 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
350 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
351
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
352 size = 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
353 result = PyList_New(0);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
354 if (!result)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
355 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
356
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
357 while ((n = get_line(self, &output)) != 0) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
358 line = PyString_FromStringAndSize(output, n);
46805
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
359 if (!line)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
360 goto on_error;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
361 if (PyList_Append(result, line) == -1) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
362 Py_DECREF(line);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
363 goto on_error;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
364 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
365 Py_DECREF(line);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
366 size += n;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
367 if (maxsize > 0 && size >= maxsize)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
368 break;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
369 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
370 return result;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
371
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
372 on_error:
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
373 Py_DECREF(result);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
374 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
375 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
376
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
377 PyDoc_STRVAR(readinto_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
378 "readinto(bytearray) -> int. Read up to len(b) bytes into b.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
379 "\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
380 "Returns number of bytes read (0 for EOF), or None if the object\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
381 "is set not to block as has no data to read.");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
382
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
383 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
384 bytesio_readinto(BytesIOObject *self, PyObject *buffer)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
385 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
386 void *raw_buffer;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
387 Py_ssize_t len;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
388
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
389 CHECK_CLOSED(self);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
390
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
391 if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &len) == -1)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
392 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
393
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
394 if (self->pos + len > self->string_size)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
395 len = self->string_size - self->pos;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
396
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
397 memcpy(raw_buffer, self->buf + self->pos, len);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
398 assert(self->pos + len < PY_SSIZE_T_MAX);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
399 assert(len >= 0);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
400 self->pos += len;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
401
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
402 return PyInt_FromSsize_t(len);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
403 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
404
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
405 PyDoc_STRVAR(truncate_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
406 "truncate([size]) -> int. Truncate the file to at most size bytes.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
407 "\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
408 "Size defaults to the current file position, as returned by tell().\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
409 "Returns the new size. Imply an absolute seek to the position size.");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
410
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
411 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
412 bytesio_truncate(BytesIOObject *self, PyObject *args)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
413 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
414 Py_ssize_t size;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
415 PyObject *arg = Py_None;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
416
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
417 CHECK_CLOSED(self);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
418
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
419 if (!PyArg_ParseTuple(args, "|O:truncate", &arg))
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
420 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
421
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
422 if (PyInt_Check(arg)) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
423 size = PyInt_AsSsize_t(arg);
49351
6a73134f9603 Victor Stinner's patches to check the return result of PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
424 if (size == -1 && PyErr_Occurred())
6a73134f9603 Victor Stinner's patches to check the return result of PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
425 return NULL;
46805
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
426 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
427 else if (arg == Py_None) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
428 /* Truncate to current position if no argument is passed. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
429 size = self->pos;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
430 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
431 else {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
432 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
433 Py_TYPE(arg)->tp_name);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
434 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
435 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
436
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
437 if (size < 0) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
438 PyErr_Format(PyExc_ValueError,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
439 "negative size value %zd", size);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
440 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
441 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
442
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
443 if (size < self->string_size) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
444 self->string_size = size;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
445 if (resize_buffer(self, size) < 0)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
446 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
447 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
448 self->pos = size;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
449
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
450 return PyInt_FromSsize_t(size);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
451 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
452
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
453 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
454 bytesio_iternext(BytesIOObject *self)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
455 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
456 char *next;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
457 Py_ssize_t n;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
458
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
459 CHECK_CLOSED(self);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
460
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
461 n = get_line(self, &next);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
462
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
463 if (!next || n == 0)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
464 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
465
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
466 return PyString_FromStringAndSize(next, n);
46805
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
467 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
468
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
469 PyDoc_STRVAR(seek_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
470 "seek(pos, whence=0) -> int. Change stream position.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
471 "\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
472 "Seek to byte offset pos relative to position indicated by whence:\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
473 " 0 Start of stream (the default). pos should be >= 0;\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
474 " 1 Current position - pos may be negative;\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
475 " 2 End of stream - pos usually negative.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
476 "Returns the new absolute position.");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
477
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
478 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
479 bytesio_seek(BytesIOObject *self, PyObject *args)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
480 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
481 PyObject *pos_obj, *mode_obj;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
482 Py_ssize_t pos;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
483 int mode = 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
484
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
485 CHECK_CLOSED(self);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
486
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
487 /* Special-case for 2.x to prevent floats from passing through.
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
488 This only needed to make a test in test_io succeed. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
489 if (!PyArg_UnpackTuple(args, "seek", 1, 2, &pos_obj, &mode_obj))
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
490 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
491 if (PyFloat_Check(pos_obj)) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
492 PyErr_SetString(PyExc_TypeError,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
493 "position argument must be an integer");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
494 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
495 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
496
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
497 if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &mode))
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
498 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
499
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
500 if (pos < 0 && mode == 0) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
501 PyErr_Format(PyExc_ValueError,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
502 "negative seek value %zd", pos);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
503 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
504 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
505
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
506 /* mode 0: offset relative to beginning of the string.
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
507 mode 1: offset relative to current position.
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
508 mode 2: offset relative the end of the string. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
509 if (mode == 1) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
510 if (pos > PY_SSIZE_T_MAX - self->pos) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
511 PyErr_SetString(PyExc_OverflowError,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
512 "new position too large");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
513 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
514 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
515 pos += self->pos;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
516 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
517 else if (mode == 2) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
518 if (pos > PY_SSIZE_T_MAX - self->string_size) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
519 PyErr_SetString(PyExc_OverflowError,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
520 "new position too large");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
521 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
522 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
523 pos += self->string_size;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
524 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
525 else if (mode != 0) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
526 PyErr_Format(PyExc_ValueError,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
527 "invalid whence (%i, should be 0, 1 or 2)", mode);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
528 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
529 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
530
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
531 if (pos < 0)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
532 pos = 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
533 self->pos = pos;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
534
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
535 return PyInt_FromSsize_t(self->pos);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
536 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
537
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
538 PyDoc_STRVAR(write_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
539 "write(bytes) -> int. Write bytes to file.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
540 "\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
541 "Return the number of bytes written.");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
542
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
543 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
544 bytesio_write(BytesIOObject *self, PyObject *obj)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
545 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
546 const char *bytes;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
547 Py_ssize_t size;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
548 Py_ssize_t n = 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
549
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
550 CHECK_CLOSED(self);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
551
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
552 /* Special-case in 2.x to prevent unicode objects to pass through. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
553 if (PyUnicode_Check(obj)) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
554 PyErr_SetString(PyExc_TypeError,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
555 "expecting a bytes object, got unicode");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
556 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
557 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
558
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
559 if (PyObject_AsReadBuffer(obj, (void *)&bytes, &size) < 0)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
560 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
561
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
562 if (size != 0) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
563 n = write_bytes(self, bytes, size);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
564 if (n < 0)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
565 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
566 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
567
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
568 return PyInt_FromSsize_t(n);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
569 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
570
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
571 PyDoc_STRVAR(writelines_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
572 "writelines(sequence_of_strings) -> None. Write strings to the file.\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
573 "\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
574 "Note that newlines are not added. The sequence can be any iterable\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
575 "object producing strings. This is equivalent to calling write() for\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
576 "each string.");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
577
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
578 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
579 bytesio_writelines(BytesIOObject *self, PyObject *v)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
580 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
581 PyObject *it, *item;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
582 PyObject *ret;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
583
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
584 CHECK_CLOSED(self);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
585
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
586 it = PyObject_GetIter(v);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
587 if (it == NULL)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
588 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
589
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
590 while ((item = PyIter_Next(it)) != NULL) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
591 ret = bytesio_write(self, item);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
592 Py_DECREF(item);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
593 if (ret == NULL) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
594 Py_DECREF(it);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
595 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
596 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
597 Py_DECREF(ret);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
598 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
599 Py_DECREF(it);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
600
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
601 /* See if PyIter_Next failed */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
602 if (PyErr_Occurred())
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
603 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
604
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
605 Py_RETURN_NONE;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
606 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
607
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
608 PyDoc_STRVAR(close_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
609 "close() -> None. Disable all I/O operations.");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
610
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
611 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
612 bytesio_close(BytesIOObject *self)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
613 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
614 if (self->buf != NULL) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
615 PyMem_Free(self->buf);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
616 self->buf = NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
617 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
618 Py_RETURN_NONE;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
619 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
620
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
621 static void
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
622 bytesio_dealloc(BytesIOObject *self)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
623 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
624 if (self->buf != NULL) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
625 PyMem_Free(self->buf);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
626 self->buf = NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
627 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
628 Py_TYPE(self)->tp_free(self);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
629 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
630
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
631 static PyObject *
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
632 bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
633 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
634 BytesIOObject *self;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
635
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
636 assert(type != NULL && type->tp_alloc != NULL);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
637 self = (BytesIOObject *)type->tp_alloc(type, 0);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
638 if (self == NULL)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
639 return NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
640
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
641 self->string_size = 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
642 self->pos = 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
643 self->buf_size = 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
644 self->buf = (char *)PyMem_Malloc(0);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
645 if (self->buf == NULL) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
646 Py_DECREF(self);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
647 return PyErr_NoMemory();
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
648 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
649
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
650 return (PyObject *)self;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
651 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
652
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
653 static int
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
654 bytesio_init(BytesIOObject *self, PyObject *args, PyObject *kwds)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
655 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
656 PyObject *initvalue = NULL;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
657
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
658 if (!PyArg_ParseTuple(args, "|O:BytesIO", &initvalue))
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
659 return -1;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
660
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
661 /* In case, __init__ is called multiple times. */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
662 self->string_size = 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
663 self->pos = 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
664
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
665 if (initvalue && initvalue != Py_None) {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
666 PyObject *res;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
667 res = bytesio_write(self, initvalue);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
668 if (res == NULL)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
669 return -1;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
670 Py_DECREF(res);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
671 self->pos = 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
672 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
673
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
674 return 0;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
675 }
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
676
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
677 static PyGetSetDef bytesio_getsetlist[] = {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
678 {"closed", (getter)bytesio_get_closed, NULL,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
679 "True if the file is closed."},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
680 {0}, /* sentinel */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
681 };
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
682
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
683 static struct PyMethodDef bytesio_methods[] = {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
684 {"readable", (PyCFunction)return_true, METH_NOARGS, NULL},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
685 {"seekable", (PyCFunction)return_true, METH_NOARGS, NULL},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
686 {"writable", (PyCFunction)return_true, METH_NOARGS, NULL},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
687 {"close", (PyCFunction)bytesio_close, METH_NOARGS, close_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
688 {"flush", (PyCFunction)bytesio_flush, METH_NOARGS, flush_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
689 {"isatty", (PyCFunction)bytesio_isatty, METH_NOARGS, isatty_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
690 {"tell", (PyCFunction)bytesio_tell, METH_NOARGS, tell_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
691 {"write", (PyCFunction)bytesio_write, METH_O, write_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
692 {"writelines", (PyCFunction)bytesio_writelines, METH_O, writelines_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
693 {"read1", (PyCFunction)bytesio_read1, METH_O, read1_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
694 {"readinto", (PyCFunction)bytesio_readinto, METH_O, readinto_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
695 {"readline", (PyCFunction)bytesio_readline, METH_VARARGS, readline_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
696 {"readlines", (PyCFunction)bytesio_readlines, METH_VARARGS, readlines_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
697 {"read", (PyCFunction)bytesio_read, METH_VARARGS, read_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
698 {"getvalue", (PyCFunction)bytesio_getvalue, METH_VARARGS, getval_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
699 {"seek", (PyCFunction)bytesio_seek, METH_VARARGS, seek_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
700 {"truncate", (PyCFunction)bytesio_truncate, METH_VARARGS, truncate_doc},
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
701 {NULL, NULL} /* sentinel */
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
702 };
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
703
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
704 PyDoc_STRVAR(bytesio_doc,
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
705 "BytesIO([buffer]) -> object\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
706 "\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
707 "Create a buffered I/O implementation using an in-memory bytes\n"
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
708 "buffer, ready for reading and writing.");
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
709
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
710 static PyTypeObject BytesIO_Type = {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
711 PyVarObject_HEAD_INIT(NULL, 0)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
712 "_bytesio._BytesIO", /*tp_name*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
713 sizeof(BytesIOObject), /*tp_basicsize*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
714 0, /*tp_itemsize*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
715 (destructor)bytesio_dealloc, /*tp_dealloc*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
716 0, /*tp_print*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
717 0, /*tp_getattr*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
718 0, /*tp_setattr*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
719 0, /*tp_compare*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
720 0, /*tp_repr*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
721 0, /*tp_as_number*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
722 0, /*tp_as_sequence*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
723 0, /*tp_as_mapping*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
724 0, /*tp_hash*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
725 0, /*tp_call*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
726 0, /*tp_str*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
727 0, /*tp_getattro*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
728 0, /*tp_setattro*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
729 0, /*tp_as_buffer*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
730 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
731 bytesio_doc, /*tp_doc*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
732 0, /*tp_traverse*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
733 0, /*tp_clear*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
734 0, /*tp_richcompare*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
735 0, /*tp_weaklistoffset*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
736 PyObject_SelfIter, /*tp_iter*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
737 (iternextfunc)bytesio_iternext, /*tp_iternext*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
738 bytesio_methods, /*tp_methods*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
739 0, /*tp_members*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
740 bytesio_getsetlist, /*tp_getset*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
741 0, /*tp_base*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
742 0, /*tp_dict*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
743 0, /*tp_descr_get*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
744 0, /*tp_descr_set*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
745 0, /*tp_dictoffset*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
746 (initproc)bytesio_init, /*tp_init*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
747 0, /*tp_alloc*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
748 bytesio_new, /*tp_new*/
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
749 };
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
750
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
751 PyMODINIT_FUNC
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
752 init_bytesio(void)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
753 {
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
754 PyObject *m;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
755
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
756 if (PyType_Ready(&BytesIO_Type) < 0)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
757 return;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
758 m = Py_InitModule("_bytesio", NULL);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
759 if (m == NULL)
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
760 return;
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
761 Py_INCREF(&BytesIO_Type);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
762 PyModule_AddObject(m, "_BytesIO", (PyObject *)&BytesIO_Type);
4c5ee1b38045 Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
763 }