Mercurial > cpython-lru_cache-weakref
annotate Modules/_bytesio.c @ 47192:32bd18709dfd legacy-trunk
Improve diffing logic and output for test_pydoc.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Tue, 20 May 2008 08:07:36 +0000 |
| parents | 4c5ee1b38045 |
| children | 4fe192d48960 |
| 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); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
178 return PyString_FromStringAndSize(self->buf, self->string_size); |
|
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); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
224 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
225 else if (arg == Py_None) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
226 /* Read until EOF is reached, by default. */ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
227 size = -1; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
228 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
229 else { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
230 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
231 Py_TYPE(arg)->tp_name); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
232 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
233 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
234 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
235 /* adjust invalid sizes */ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
236 n = self->string_size - self->pos; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
237 if (size < 0 || size > n) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
238 size = n; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
239 if (size < 0) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
240 size = 0; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
241 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
242 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
243 assert(self->buf != NULL); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
244 output = self->buf + self->pos; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
245 self->pos += size; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
246 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
247 return PyString_FromStringAndSize(output, size); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
248 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
249 |
|
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 PyDoc_STRVAR(read1_doc, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
252 "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
|
253 "\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
254 "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
|
255 "Return an empty string at EOF."); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
256 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
257 static PyObject * |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
258 bytesio_read1(BytesIOObject *self, PyObject *n) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
259 { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
260 PyObject *arg, *res; |
|
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 arg = PyTuple_Pack(1, n); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
263 if (arg == NULL) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
264 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
265 res = bytesio_read(self, arg); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
266 Py_DECREF(arg); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
267 return res; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
268 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
269 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
270 PyDoc_STRVAR(readline_doc, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
271 "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
|
272 "\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
273 "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
|
274 "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
|
275 "Return an empty string at EOF.\n"); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
276 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
277 static PyObject * |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
278 bytesio_readline(BytesIOObject *self, PyObject *args) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
279 { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
280 Py_ssize_t size, n; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
281 char *output; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
282 PyObject *arg = Py_None; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
283 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
284 CHECK_CLOSED(self); |
|
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 if (!PyArg_ParseTuple(args, "|O:readline", &arg)) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
287 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
288 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
289 if (PyInt_Check(arg)) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
290 size = PyInt_AsSsize_t(arg); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
291 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
292 else if (arg == Py_None) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
293 /* No size limit, by default. */ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
294 size = -1; |
|
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 { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
297 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
298 Py_TYPE(arg)->tp_name); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
299 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
300 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
301 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
302 n = get_line(self, &output); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
303 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
304 if (size >= 0 && size < n) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
305 size = n - size; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
306 n -= size; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
307 self->pos -= size; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
308 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
309 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
310 return PyString_FromStringAndSize(output, n); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
311 } |
|
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 PyDoc_STRVAR(readlines_doc, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
314 "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
|
315 "\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
316 "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
|
317 "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
|
318 "total number of bytes in the lines returned.\n"); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
319 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
320 static PyObject * |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
321 bytesio_readlines(BytesIOObject *self, PyObject *args) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
322 { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
323 Py_ssize_t maxsize, size, n; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
324 PyObject *result, *line; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
325 char *output; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
326 PyObject *arg = Py_None; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
327 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
328 CHECK_CLOSED(self); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
329 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
330 if (!PyArg_ParseTuple(args, "|O:readlines", &arg)) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
331 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
332 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
333 if (PyInt_Check(arg)) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
334 maxsize = PyInt_AsSsize_t(arg); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
335 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
336 else if (arg == Py_None) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
337 /* No size limit, by default. */ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
338 maxsize = -1; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
339 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
340 else { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
341 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
342 Py_TYPE(arg)->tp_name); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
343 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
344 } |
|
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 size = 0; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
347 result = PyList_New(0); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
348 if (!result) |
|
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 while ((n = get_line(self, &output)) != 0) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
352 line = PyString_FromStringAndSize(output, n); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
353 if (!line) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
354 goto on_error; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
355 if (PyList_Append(result, line) == -1) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
356 Py_DECREF(line); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
357 goto on_error; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
358 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
359 Py_DECREF(line); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
360 size += n; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
361 if (maxsize > 0 && size >= maxsize) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
362 break; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
363 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
364 return result; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
365 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
366 on_error: |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
367 Py_DECREF(result); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
368 return NULL; |
|
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 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
371 PyDoc_STRVAR(readinto_doc, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
372 "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
|
373 "\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
374 "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
|
375 "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
|
376 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
377 static PyObject * |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
378 bytesio_readinto(BytesIOObject *self, PyObject *buffer) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
379 { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
380 void *raw_buffer; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
381 Py_ssize_t len; |
|
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 CHECK_CLOSED(self); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
384 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
385 if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &len) == -1) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
386 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
387 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
388 if (self->pos + len > self->string_size) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
389 len = self->string_size - self->pos; |
|
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 memcpy(raw_buffer, self->buf + self->pos, len); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
392 assert(self->pos + len < PY_SSIZE_T_MAX); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
393 assert(len >= 0); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
394 self->pos += len; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
395 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
396 return PyInt_FromSsize_t(len); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
397 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
398 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
399 PyDoc_STRVAR(truncate_doc, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
400 "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
|
401 "\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
402 "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
|
403 "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
|
404 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
405 static PyObject * |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
406 bytesio_truncate(BytesIOObject *self, PyObject *args) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
407 { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
408 Py_ssize_t size; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
409 PyObject *arg = Py_None; |
|
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 CHECK_CLOSED(self); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
412 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
413 if (!PyArg_ParseTuple(args, "|O:truncate", &arg)) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
414 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
415 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
416 if (PyInt_Check(arg)) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
417 size = PyInt_AsSsize_t(arg); |
|
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 else if (arg == Py_None) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
420 /* Truncate to current position if no argument is passed. */ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
421 size = self->pos; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
422 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
423 else { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
424 PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
425 Py_TYPE(arg)->tp_name); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
426 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
427 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
428 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
429 if (size < 0) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
430 PyErr_Format(PyExc_ValueError, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
431 "negative size value %zd", size); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
432 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
433 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
434 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
435 if (size < self->string_size) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
436 self->string_size = size; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
437 if (resize_buffer(self, size) < 0) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
438 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
439 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
440 self->pos = size; |
|
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 return PyInt_FromSsize_t(size); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
443 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
444 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
445 static PyObject * |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
446 bytesio_iternext(BytesIOObject *self) |
|
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 char *next; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
449 Py_ssize_t n; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
450 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
451 CHECK_CLOSED(self); |
|
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 n = get_line(self, &next); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
454 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
455 if (!next || n == 0) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
456 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
457 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
458 return PyString_FromStringAndSize(next, n); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
459 } |
|
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 PyDoc_STRVAR(seek_doc, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
462 "seek(pos, whence=0) -> int. Change stream position.\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
463 "\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
464 "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
|
465 " 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
|
466 " 1 Current position - pos may be negative;\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
467 " 2 End of stream - pos usually negative.\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
468 "Returns the new absolute position."); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
469 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
470 static PyObject * |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
471 bytesio_seek(BytesIOObject *self, PyObject *args) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
472 { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
473 PyObject *pos_obj, *mode_obj; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
474 Py_ssize_t pos; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
475 int mode = 0; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
476 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
477 CHECK_CLOSED(self); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
478 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
479 /* 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
|
480 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
|
481 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
|
482 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
483 if (PyFloat_Check(pos_obj)) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
484 PyErr_SetString(PyExc_TypeError, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
485 "position argument must be an integer"); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
486 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
487 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
488 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
489 if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &mode)) |
|
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 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
492 if (pos < 0 && mode == 0) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
493 PyErr_Format(PyExc_ValueError, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
494 "negative seek value %zd", pos); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
495 return NULL; |
|
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 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
498 /* mode 0: offset relative to beginning of the string. |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
499 mode 1: offset relative to current position. |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
500 mode 2: offset relative the end of the string. */ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
501 if (mode == 1) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
502 if (pos > PY_SSIZE_T_MAX - self->pos) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
503 PyErr_SetString(PyExc_OverflowError, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
504 "new position too large"); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
505 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
506 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
507 pos += self->pos; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
508 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
509 else if (mode == 2) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
510 if (pos > PY_SSIZE_T_MAX - self->string_size) { |
|
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->string_size; |
|
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 != 0) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
518 PyErr_Format(PyExc_ValueError, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
519 "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
|
520 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
521 } |
|
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 if (pos < 0) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
524 pos = 0; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
525 self->pos = pos; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
526 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
527 return PyInt_FromSsize_t(self->pos); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
528 } |
|
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 PyDoc_STRVAR(write_doc, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
531 "write(bytes) -> int. Write bytes to file.\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
532 "\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
533 "Return the number of bytes written."); |
|
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 static PyObject * |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
536 bytesio_write(BytesIOObject *self, PyObject *obj) |
|
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 const char *bytes; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
539 Py_ssize_t size; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
540 Py_ssize_t n = 0; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
541 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
542 CHECK_CLOSED(self); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
543 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
544 /* 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
|
545 if (PyUnicode_Check(obj)) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
546 PyErr_SetString(PyExc_TypeError, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
547 "expecting a bytes object, got unicode"); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
548 return NULL; |
|
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 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
551 if (PyObject_AsReadBuffer(obj, (void *)&bytes, &size) < 0) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
552 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
553 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
554 if (size != 0) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
555 n = write_bytes(self, bytes, size); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
556 if (n < 0) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
557 return NULL; |
|
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 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
560 return PyInt_FromSsize_t(n); |
|
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 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
563 PyDoc_STRVAR(writelines_doc, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
564 "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
|
565 "\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
566 "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
|
567 "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
|
568 "each string."); |
|
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 static PyObject * |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
571 bytesio_writelines(BytesIOObject *self, PyObject *v) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
572 { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
573 PyObject *it, *item; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
574 PyObject *ret; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
575 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
576 CHECK_CLOSED(self); |
|
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 it = PyObject_GetIter(v); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
579 if (it == NULL) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
580 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
581 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
582 while ((item = PyIter_Next(it)) != NULL) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
583 ret = bytesio_write(self, item); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
584 Py_DECREF(item); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
585 if (ret == NULL) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
586 Py_DECREF(it); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
587 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
588 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
589 Py_DECREF(ret); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
590 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
591 Py_DECREF(it); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
592 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
593 /* See if PyIter_Next failed */ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
594 if (PyErr_Occurred()) |
|
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_RETURN_NONE; |
|
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 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
600 PyDoc_STRVAR(close_doc, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
601 "close() -> None. Disable all I/O operations."); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
602 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
603 static PyObject * |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
604 bytesio_close(BytesIOObject *self) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
605 { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
606 if (self->buf != NULL) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
607 PyMem_Free(self->buf); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
608 self->buf = NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
609 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
610 Py_RETURN_NONE; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
611 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
612 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
613 static void |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
614 bytesio_dealloc(BytesIOObject *self) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
615 { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
616 if (self->buf != NULL) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
617 PyMem_Free(self->buf); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
618 self->buf = NULL; |
|
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 Py_TYPE(self)->tp_free(self); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
621 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
622 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
623 static PyObject * |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
624 bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
625 { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
626 BytesIOObject *self; |
|
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 assert(type != NULL && type->tp_alloc != NULL); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
629 self = (BytesIOObject *)type->tp_alloc(type, 0); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
630 if (self == NULL) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
631 return NULL; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
632 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
633 self->string_size = 0; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
634 self->pos = 0; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
635 self->buf_size = 0; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
636 self->buf = (char *)PyMem_Malloc(0); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
637 if (self->buf == NULL) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
638 Py_DECREF(self); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
639 return PyErr_NoMemory(); |
|
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 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
642 return (PyObject *)self; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
643 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
644 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
645 static int |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
646 bytesio_init(BytesIOObject *self, PyObject *args, PyObject *kwds) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
647 { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
648 PyObject *initvalue = NULL; |
|
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 if (!PyArg_ParseTuple(args, "|O:BytesIO", &initvalue)) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
651 return -1; |
|
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 /* In case, __init__ is called multiple times. */ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
654 self->string_size = 0; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
655 self->pos = 0; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
656 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
657 if (initvalue && initvalue != Py_None) { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
658 PyObject *res; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
659 res = bytesio_write(self, initvalue); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
660 if (res == NULL) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
661 return -1; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
662 Py_DECREF(res); |
|
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 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
666 return 0; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
667 } |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
668 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
669 static PyGetSetDef bytesio_getsetlist[] = { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
670 {"closed", (getter)bytesio_get_closed, NULL, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
671 "True if the file is closed."}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
672 {0}, /* sentinel */ |
|
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 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
675 static struct PyMethodDef bytesio_methods[] = { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
676 {"readable", (PyCFunction)return_true, METH_NOARGS, NULL}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
677 {"seekable", (PyCFunction)return_true, METH_NOARGS, NULL}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
678 {"writable", (PyCFunction)return_true, METH_NOARGS, NULL}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
679 {"close", (PyCFunction)bytesio_close, METH_NOARGS, close_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
680 {"flush", (PyCFunction)bytesio_flush, METH_NOARGS, flush_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
681 {"isatty", (PyCFunction)bytesio_isatty, METH_NOARGS, isatty_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
682 {"tell", (PyCFunction)bytesio_tell, METH_NOARGS, tell_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
683 {"write", (PyCFunction)bytesio_write, METH_O, write_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
684 {"writelines", (PyCFunction)bytesio_writelines, METH_O, writelines_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
685 {"read1", (PyCFunction)bytesio_read1, METH_O, read1_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
686 {"readinto", (PyCFunction)bytesio_readinto, METH_O, readinto_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
687 {"readline", (PyCFunction)bytesio_readline, METH_VARARGS, readline_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
688 {"readlines", (PyCFunction)bytesio_readlines, METH_VARARGS, readlines_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
689 {"read", (PyCFunction)bytesio_read, METH_VARARGS, read_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
690 {"getvalue", (PyCFunction)bytesio_getvalue, METH_VARARGS, getval_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
691 {"seek", (PyCFunction)bytesio_seek, METH_VARARGS, seek_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
692 {"truncate", (PyCFunction)bytesio_truncate, METH_VARARGS, truncate_doc}, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
693 {NULL, NULL} /* sentinel */ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
694 }; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
695 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
696 PyDoc_STRVAR(bytesio_doc, |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
697 "BytesIO([buffer]) -> object\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
698 "\n" |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
699 "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
|
700 "buffer, ready for reading and writing."); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
701 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
702 static PyTypeObject BytesIO_Type = { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
703 PyVarObject_HEAD_INIT(NULL, 0) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
704 "_bytesio._BytesIO", /*tp_name*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
705 sizeof(BytesIOObject), /*tp_basicsize*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
706 0, /*tp_itemsize*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
707 (destructor)bytesio_dealloc, /*tp_dealloc*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
708 0, /*tp_print*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
709 0, /*tp_getattr*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
710 0, /*tp_setattr*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
711 0, /*tp_compare*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
712 0, /*tp_repr*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
713 0, /*tp_as_number*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
714 0, /*tp_as_sequence*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
715 0, /*tp_as_mapping*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
716 0, /*tp_hash*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
717 0, /*tp_call*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
718 0, /*tp_str*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
719 0, /*tp_getattro*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
720 0, /*tp_setattro*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
721 0, /*tp_as_buffer*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
722 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
723 bytesio_doc, /*tp_doc*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
724 0, /*tp_traverse*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
725 0, /*tp_clear*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
726 0, /*tp_richcompare*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
727 0, /*tp_weaklistoffset*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
728 PyObject_SelfIter, /*tp_iter*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
729 (iternextfunc)bytesio_iternext, /*tp_iternext*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
730 bytesio_methods, /*tp_methods*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
731 0, /*tp_members*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
732 bytesio_getsetlist, /*tp_getset*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
733 0, /*tp_base*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
734 0, /*tp_dict*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
735 0, /*tp_descr_get*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
736 0, /*tp_descr_set*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
737 0, /*tp_dictoffset*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
738 (initproc)bytesio_init, /*tp_init*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
739 0, /*tp_alloc*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
740 bytesio_new, /*tp_new*/ |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
741 }; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
742 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
743 PyMODINIT_FUNC |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
744 init_bytesio(void) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
745 { |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
746 PyObject *m; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
747 |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
748 if (PyType_Ready(&BytesIO_Type) < 0) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
749 return; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
750 m = Py_InitModule("_bytesio", NULL); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
751 if (m == NULL) |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
752 return; |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
753 Py_INCREF(&BytesIO_Type); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
754 PyModule_AddObject(m, "_BytesIO", (PyObject *)&BytesIO_Type); |
|
4c5ee1b38045
Backport fast alternate io.BytesIO implementation.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
755 } |
