Mercurial > cpython-lru_cache-weakref
annotate Modules/_bytesio.c @ 49961:e793fc3c8abe
More 3.0 -> 3.1 transistion in the Windows and OS/2 build files
| author | Christian Heimes <christian@cheimes.de> |
|---|---|
| date | Thu, 04 Dec 2008 14:27:25 +0000 |
| parents | b2801aca9d6b |
| children | 87fbbf22adf3 |
| 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" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
2 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
3 typedef struct { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
4 PyObject_HEAD |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
5 char *buf; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
6 Py_ssize_t pos; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
7 Py_ssize_t string_size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
8 size_t buf_size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
9 } BytesIOObject; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
10 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
11 #define CHECK_CLOSED(self) \ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
12 if ((self)->buf == NULL) { \ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
13 PyErr_SetString(PyExc_ValueError, \ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
14 "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
|
15 return NULL; \ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
16 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
17 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
18 /* 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
|
19 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
|
20 next newline character. */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
21 static Py_ssize_t |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
22 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
|
23 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
24 char *n; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
25 const char *str_end; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
26 Py_ssize_t len; |
|
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 assert(self->buf != NULL); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
29 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
30 /* 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
|
31 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
|
32 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
|
33 n < str_end && *n != '\n'; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
34 n++); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
35 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
36 /* Skip the newline character */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
37 if (n < str_end) |
|
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 /* 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
|
41 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
|
42 *output = self->buf + self->pos; |
|
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 assert(len >= 0); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
45 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
|
46 self->pos += len; |
|
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 return len; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
49 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
50 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
51 /* 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
|
52 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
|
53 0 on success, -1 otherwise. */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
54 static int |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
55 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
|
56 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
57 /* 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
|
58 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
|
59 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
|
60 char *new_buf = NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
61 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
62 assert(self->buf != NULL); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
63 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
64 /* 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
|
65 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
|
66 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
|
67 goto overflow; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
68 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
69 if (size < alloc / 2) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
70 /* 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
|
71 alloc = size + 1; |
|
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 else if (size < alloc) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
74 /* Within allocated size; quick exit */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
75 return 0; |
|
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 * 1.125) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
78 /* 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
|
79 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
|
80 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
81 else { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
82 /* 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
|
83 alloc = size + 1; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
86 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
|
87 goto overflow; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
88 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
|
89 if (new_buf == NULL) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
90 PyErr_NoMemory(); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
91 return -1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
92 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
93 self->buf_size = alloc; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
94 self->buf = new_buf; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
95 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
96 return 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
97 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
98 overflow: |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
99 PyErr_SetString(PyExc_OverflowError, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
100 "new buffer size too large"); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
101 return -1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
102 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
103 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
104 /* 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
|
105 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
|
106 static Py_ssize_t |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
107 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
|
108 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
109 assert(self->buf != NULL); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
110 assert(self->pos >= 0); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
111 assert(len >= 0); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
112 |
|
46714
5fbd97f1bc35
Changed _bytesio.c to avoid comparing a signed with an unsigned value.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46694
diff
changeset
|
113 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
|
114 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
|
115 return -1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
116 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
117 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
118 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
|
119 /* 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
|
120 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
|
121 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
122 0 lo string_size hi |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
123 | |<---used--->|<----------available----------->| |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
124 | | <--to pad-->|<---to write---> | |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
125 0 buf position |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
126 */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
127 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
|
128 (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
|
129 } |
|
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 /* 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
|
132 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
|
133 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
|
134 self->pos += len; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
135 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
136 /* 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
|
137 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
|
138 self->string_size = self->pos; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
141 return len; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
142 } |
|
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 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
145 bytesio_get_closed(BytesIOObject *self) |
|
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 if (self->buf == NULL) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
148 Py_RETURN_TRUE; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
149 else |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
150 Py_RETURN_FALSE; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
151 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
152 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
153 /* 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
|
154 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
155 return_true(BytesIOObject *self) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
156 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
157 Py_RETURN_TRUE; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
160 PyDoc_STRVAR(flush_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
161 "flush() -> None. Does nothing."); |
|
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 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
164 bytesio_flush(BytesIOObject *self) |
|
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 Py_RETURN_NONE; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
167 } |
|
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 PyDoc_STRVAR(getval_doc, |
|
46722
831a30cd23a2
Fixed bytes naming in the docstrings of _bytesio.c.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46715
diff
changeset
|
170 "getvalue() -> bytes.\n" |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
171 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
172 "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
|
173 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
174 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
175 bytesio_getvalue(BytesIOObject *self) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
176 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
177 CHECK_CLOSED(self); |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46722
diff
changeset
|
178 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
|
179 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
180 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
181 PyDoc_STRVAR(isatty_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
182 "isatty() -> False.\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
183 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
184 "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
|
185 "to a tty-like device."); |
|
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 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
188 bytesio_isatty(BytesIOObject *self) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
189 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
190 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
191 Py_RETURN_FALSE; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
194 PyDoc_STRVAR(tell_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
195 "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
|
196 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
197 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
198 bytesio_tell(BytesIOObject *self) |
|
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 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
201 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
|
202 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
203 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
204 PyDoc_STRVAR(read_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
205 "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
|
206 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
207 "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
|
208 "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
|
209 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
210 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
211 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
|
212 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
213 Py_ssize_t size, n; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
214 char *output; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
215 PyObject *arg = Py_None; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
216 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
217 CHECK_CLOSED(self); |
|
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 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
|
220 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
221 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
222 if (PyLong_Check(arg)) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
223 size = PyLong_AsSsize_t(arg); |
|
49352
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
224 if (size == -1 && PyErr_Occurred()) |
|
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
225 return NULL; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
226 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
227 else if (arg == Py_None) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
228 /* 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
|
229 size = -1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
230 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
231 else { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
232 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
|
233 Py_TYPE(arg)->tp_name); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
234 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
235 } |
|
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 /* adjust invalid sizes */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
238 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
|
239 if (size < 0 || size > n) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
240 size = n; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
241 if (size < 0) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
242 size = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
243 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
244 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
245 assert(self->buf != NULL); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
246 output = self->buf + self->pos; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
247 self->pos += size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
248 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46722
diff
changeset
|
249 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
|
250 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
251 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
252 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
253 PyDoc_STRVAR(read1_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
254 "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
|
255 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
256 "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
|
257 "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
|
258 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
259 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
260 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
|
261 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
262 PyObject *arg, *res; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
263 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
264 arg = PyTuple_Pack(1, n); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
265 if (arg == NULL) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
266 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
267 res = bytesio_read(self, arg); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
268 Py_DECREF(arg); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
269 return res; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
270 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
271 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
272 PyDoc_STRVAR(readline_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
273 "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
|
274 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
275 "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
|
276 "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
|
277 "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
|
278 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
279 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
280 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
|
281 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
282 Py_ssize_t size, n; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
283 char *output; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
284 PyObject *arg = Py_None; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
285 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
286 CHECK_CLOSED(self); |
|
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 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
|
289 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
290 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
291 if (PyLong_Check(arg)) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
292 size = PyLong_AsSsize_t(arg); |
|
49352
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
293 if (size == -1 && PyErr_Occurred()) |
|
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
294 return NULL; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
295 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
296 else if (arg == Py_None) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
297 /* No size limit, by default. */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
298 size = -1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
299 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
300 else { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
301 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
|
302 Py_TYPE(arg)->tp_name); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
303 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
304 } |
|
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 n = get_line(self, &output); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
307 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
308 if (size >= 0 && size < n) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
309 size = n - size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
310 n -= size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
311 self->pos -= size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
312 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
313 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46722
diff
changeset
|
314 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
|
315 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
316 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
317 PyDoc_STRVAR(readlines_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
318 "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
|
319 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
320 "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
|
321 "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
|
322 "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
|
323 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
324 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
325 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
|
326 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
327 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
|
328 PyObject *result, *line; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
329 char *output; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
330 PyObject *arg = Py_None; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
331 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
332 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
333 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
334 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
|
335 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
336 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
337 if (PyLong_Check(arg)) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
338 maxsize = PyLong_AsSsize_t(arg); |
|
49352
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
339 if (maxsize == -1 && PyErr_Occurred()) |
|
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
340 return NULL; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
341 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
342 else if (arg == Py_None) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
343 /* No size limit, by default. */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
344 maxsize = -1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
345 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
346 else { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
347 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
|
348 Py_TYPE(arg)->tp_name); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
349 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
350 } |
|
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 size = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
353 result = PyList_New(0); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
354 if (!result) |
|
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 while ((n = get_line(self, &output)) != 0) { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46722
diff
changeset
|
358 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
|
359 if (!line) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
360 goto on_error; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
361 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
|
362 Py_DECREF(line); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
363 goto on_error; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
364 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
365 Py_DECREF(line); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
366 size += n; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
367 if (maxsize > 0 && size >= maxsize) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
368 break; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
369 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
370 return result; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
371 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
372 on_error: |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
373 Py_DECREF(result); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
374 return NULL; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
377 PyDoc_STRVAR(readinto_doc, |
|
46722
831a30cd23a2
Fixed bytes naming in the docstrings of _bytesio.c.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46715
diff
changeset
|
378 "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
|
379 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
380 "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
|
381 "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
|
382 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
383 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
384 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
|
385 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
386 void *raw_buffer; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
387 Py_ssize_t len; |
|
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 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
390 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
391 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
|
392 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
393 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
394 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
|
395 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
|
396 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
397 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
|
398 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
|
399 assert(len >= 0); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
400 self->pos += len; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
401 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
402 return PyLong_FromSsize_t(len); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
403 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
404 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
405 PyDoc_STRVAR(truncate_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
406 "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
|
407 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
408 "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
|
409 "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
|
410 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
411 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
412 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
|
413 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
414 Py_ssize_t size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
415 PyObject *arg = Py_None; |
|
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 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
418 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
419 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
|
420 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
421 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
422 if (PyLong_Check(arg)) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
423 size = PyLong_AsSsize_t(arg); |
|
49352
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
424 if (size == -1 && PyErr_Occurred()) |
|
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
425 return NULL; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
426 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
427 else if (arg == Py_None) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
428 /* 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
|
429 size = self->pos; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
430 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
431 else { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
432 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
|
433 Py_TYPE(arg)->tp_name); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
434 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
435 } |
|
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 if (size < 0) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
438 PyErr_Format(PyExc_ValueError, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
439 "negative size value %zd", size); |
|
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 < self->string_size) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
444 self->string_size = size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
445 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
|
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 self->pos = size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
449 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
450 return PyLong_FromSsize_t(size); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
451 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
452 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
453 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
454 bytesio_iternext(BytesIOObject *self) |
|
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 char *next; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
457 Py_ssize_t n; |
|
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 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
460 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
461 n = get_line(self, &next); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
462 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
463 if (!next || n == 0) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
464 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
465 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46722
diff
changeset
|
466 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
|
467 } |
|
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 PyDoc_STRVAR(seek_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
470 "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
|
471 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
472 "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
|
473 " 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
|
474 " 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
|
475 " 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
|
476 "Returns the new absolute position."); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
477 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
478 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
479 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
|
480 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
481 Py_ssize_t pos; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
482 int mode = 0; |
|
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 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
485 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
486 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
|
487 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
488 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
489 if (pos < 0 && mode == 0) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
490 PyErr_Format(PyExc_ValueError, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
491 "negative seek value %zd", pos); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
492 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
493 } |
|
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 /* 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
|
496 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
|
497 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
|
498 if (mode == 1) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
499 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
|
500 PyErr_SetString(PyExc_OverflowError, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
501 "new position too large"); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
502 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
503 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
504 pos += self->pos; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
505 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
506 else if (mode == 2) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
507 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
|
508 PyErr_SetString(PyExc_OverflowError, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
509 "new position too large"); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
510 return NULL; |
|
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 pos += self->string_size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
513 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
514 else if (mode != 0) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
515 PyErr_Format(PyExc_ValueError, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
516 "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
|
517 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
518 } |
|
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 if (pos < 0) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
521 pos = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
522 self->pos = pos; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
523 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
524 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
|
525 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
526 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
527 PyDoc_STRVAR(write_doc, |
|
46722
831a30cd23a2
Fixed bytes naming in the docstrings of _bytesio.c.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46715
diff
changeset
|
528 "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
|
529 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
530 "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
|
531 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
532 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
533 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
|
534 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
535 const char *bytes; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
536 Py_ssize_t size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
537 Py_ssize_t n = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
538 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
539 CHECK_CLOSED(self); |
|
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 if (PyObject_AsReadBuffer(obj, (void *)&bytes, &size) < 0) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
542 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
543 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
544 if (size != 0) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
545 n = write_bytes(self, bytes, size); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
546 if (n < 0) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
547 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
548 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
549 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
550 return PyLong_FromSsize_t(n); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
551 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
552 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
553 PyDoc_STRVAR(writelines_doc, |
|
46715
38b94c097a55
Fixed docstring of _bytesio._BytesIO.writelines().
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46714
diff
changeset
|
554 "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
|
555 "\n" |
|
46715
38b94c097a55
Fixed docstring of _bytesio._BytesIO.writelines().
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46714
diff
changeset
|
556 "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
|
557 "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
|
558 "each string."); |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
559 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
560 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
561 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
|
562 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
563 PyObject *it, *item; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
564 PyObject *ret; |
|
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 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
567 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
568 it = PyObject_GetIter(v); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
569 if (it == NULL) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
570 return NULL; |
|
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 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
|
573 ret = bytesio_write(self, item); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
574 Py_DECREF(item); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
575 if (ret == NULL) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
576 Py_DECREF(it); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
577 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
578 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
579 Py_DECREF(ret); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
580 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
581 Py_DECREF(it); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
582 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
583 /* See if PyIter_Next failed */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
584 if (PyErr_Occurred()) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
585 return NULL; |
|
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_RETURN_NONE; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
590 PyDoc_STRVAR(close_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
591 "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
|
592 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
593 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
594 bytesio_close(BytesIOObject *self) |
|
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 if (self->buf != NULL) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
597 PyMem_Free(self->buf); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
598 self->buf = NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
599 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
600 Py_RETURN_NONE; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
603 static void |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
604 bytesio_dealloc(BytesIOObject *self) |
|
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 if (self->buf != NULL) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
607 PyMem_Free(self->buf); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
608 self->buf = NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
609 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
610 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
|
611 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
612 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
613 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
614 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
|
615 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
616 BytesIOObject *self; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
617 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
618 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
|
619 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
|
620 if (self == NULL) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
621 return NULL; |
|
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 self->string_size = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
624 self->pos = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
625 self->buf_size = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
626 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
|
627 if (self->buf == NULL) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
628 Py_DECREF(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
629 return PyErr_NoMemory(); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
630 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
631 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
632 return (PyObject *)self; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
633 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
634 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
635 static int |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
636 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
|
637 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
638 PyObject *initvalue = NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
639 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
640 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
|
641 return -1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
642 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
643 /* 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
|
644 self->string_size = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
645 self->pos = 0; |
|
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 (initvalue && initvalue != Py_None) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
648 PyObject *res; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
649 res = bytesio_write(self, initvalue); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
650 if (res == NULL) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
651 return -1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
652 Py_DECREF(res); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
653 self->pos = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
654 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
655 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
656 return 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
657 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
658 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
659 static PyGetSetDef bytesio_getsetlist[] = { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
660 {"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
|
661 "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
|
662 {0}, /* sentinel */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
663 }; |
|
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 static struct PyMethodDef bytesio_methods[] = { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
666 {"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
|
667 {"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
|
668 {"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
|
669 {"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
|
670 {"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
|
671 {"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
|
672 {"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
|
673 {"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
|
674 {"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
|
675 {"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
|
676 {"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
|
677 {"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
|
678 {"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
|
679 {"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
|
680 {"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
|
681 {"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
|
682 {"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
|
683 {NULL, NULL} /* sentinel */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
684 }; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
685 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
686 PyDoc_STRVAR(bytesio_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
687 "BytesIO([buffer]) -> object\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
688 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
689 "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
|
690 "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
|
691 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
692 static PyTypeObject BytesIO_Type = { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
693 PyVarObject_HEAD_INIT(NULL, 0) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
694 "_bytesio._BytesIO", /*tp_name*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
695 sizeof(BytesIOObject), /*tp_basicsize*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
696 0, /*tp_itemsize*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
697 (destructor)bytesio_dealloc, /*tp_dealloc*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
698 0, /*tp_print*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
699 0, /*tp_getattr*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
700 0, /*tp_setattr*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
701 0, /*tp_compare*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
702 0, /*tp_repr*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
703 0, /*tp_as_number*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
704 0, /*tp_as_sequence*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
705 0, /*tp_as_mapping*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
706 0, /*tp_hash*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
707 0, /*tp_call*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
708 0, /*tp_str*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
709 0, /*tp_getattro*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
710 0, /*tp_setattro*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
711 0, /*tp_as_buffer*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
712 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
713 bytesio_doc, /*tp_doc*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
714 0, /*tp_traverse*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
715 0, /*tp_clear*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
716 0, /*tp_richcompare*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
717 0, /*tp_weaklistoffset*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
718 PyObject_SelfIter, /*tp_iter*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
719 (iternextfunc)bytesio_iternext, /*tp_iternext*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
720 bytesio_methods, /*tp_methods*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
721 0, /*tp_members*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
722 bytesio_getsetlist, /*tp_getset*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
723 0, /*tp_base*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
724 0, /*tp_dict*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
725 0, /*tp_descr_get*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
726 0, /*tp_descr_set*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
727 0, /*tp_dictoffset*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
728 (initproc)bytesio_init, /*tp_init*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
729 0, /*tp_alloc*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
730 bytesio_new, /*tp_new*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
731 }; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
732 |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
733 |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
734 static struct PyModuleDef _bytesiomodule = { |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
735 PyModuleDef_HEAD_INIT, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
736 "_bytesio", |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
737 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
738 -1, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
739 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
740 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
741 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
742 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
743 NULL |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
744 }; |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
745 |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
746 PyMODINIT_FUNC |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
747 PyInit__bytesio(void) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
748 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
749 PyObject *m; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
750 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
751 if (PyType_Ready(&BytesIO_Type) < 0) |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
752 return NULL; |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
753 m = PyModule_Create(&_bytesiomodule); |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
754 if (m == NULL) |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
755 return NULL; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
756 Py_INCREF(&BytesIO_Type); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
757 PyModule_AddObject(m, "_BytesIO", (PyObject *)&BytesIO_Type); |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
758 return m; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
759 } |
