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