Mercurial > cpython-lru_cache-weakref
annotate Modules/_io/bytesio.c @ 70817:dd775b1561ba
Fix markup.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Tue, 14 Jun 2011 21:09:55 +0200 |
| parents | b39270c6b8d1 |
| children | 917295aaad76 |
| rev | line source |
|---|---|
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
1 #include "Python.h" |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
2 #include "structmember.h" /* for offsetof() */ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
3 #include "_iomodule.h" |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
4 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
5 typedef struct { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
6 PyObject_HEAD |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
7 char *buf; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
8 Py_ssize_t pos; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
9 Py_ssize_t string_size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
10 size_t buf_size; |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
11 PyObject *dict; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
12 PyObject *weakreflist; |
|
64496
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
13 Py_ssize_t exports; |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
14 } bytesio; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
15 |
|
64496
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
16 typedef struct { |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
17 PyObject_HEAD |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
18 bytesio *source; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
19 } bytesiobuf; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
20 |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
21 |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
22 #define CHECK_CLOSED(self) \ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
23 if ((self)->buf == NULL) { \ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
24 PyErr_SetString(PyExc_ValueError, \ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
25 "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
|
26 return NULL; \ |
|
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 |
|
64496
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
29 #define CHECK_EXPORTS(self) \ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
30 if ((self)->exports > 0) { \ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
31 PyErr_SetString(PyExc_BufferError, \ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
32 "Existing exports of data: object cannot be re-sized"); \ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
33 return NULL; \ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
34 } |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
35 |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
36 |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
37 /* 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
|
38 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
|
39 next newline character. */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
40 static Py_ssize_t |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
41 get_line(bytesio *self, char **output) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
42 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
43 char *n; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
44 const char *str_end; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
45 Py_ssize_t len; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
46 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
47 assert(self->buf != NULL); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
48 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
49 /* 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
|
50 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
|
51 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
|
52 n < str_end && *n != '\n'; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
53 n++); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
54 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
55 /* Skip the newline character */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
56 if (n < str_end) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
57 n++; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
58 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
59 /* 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
|
60 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
|
61 *output = self->buf + self->pos; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
62 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
63 assert(len >= 0); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
64 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
|
65 self->pos += len; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
66 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
67 return len; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
70 /* 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
|
71 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
|
72 0 on success, -1 otherwise. */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
73 static int |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
74 resize_buffer(bytesio *self, size_t size) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
75 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
76 /* 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
|
77 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
|
78 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
|
79 char *new_buf = NULL; |
|
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 assert(self->buf != NULL); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
82 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
83 /* 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
|
84 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
|
85 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
|
86 goto overflow; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
87 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
88 if (size < alloc / 2) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
89 /* 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
|
90 alloc = size + 1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
91 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
92 else if (size < alloc) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
93 /* Within allocated size; quick exit */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
94 return 0; |
|
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 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
|
97 /* 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
|
98 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
|
99 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
100 else { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
101 /* 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
|
102 alloc = size + 1; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
105 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
|
106 goto overflow; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
107 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
|
108 if (new_buf == NULL) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
109 PyErr_NoMemory(); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
110 return -1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
111 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
112 self->buf_size = alloc; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
113 self->buf = new_buf; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
114 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
115 return 0; |
|
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 overflow: |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
118 PyErr_SetString(PyExc_OverflowError, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
119 "new buffer size too large"); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
120 return -1; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
123 /* 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
|
124 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
|
125 static Py_ssize_t |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
126 write_bytes(bytesio *self, const char *bytes, Py_ssize_t len) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
127 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
128 assert(self->buf != NULL); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
129 assert(self->pos >= 0); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
130 assert(len >= 0); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
131 |
|
46714
5fbd97f1bc35
Changed _bytesio.c to avoid comparing a signed with an unsigned value.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46694
diff
changeset
|
132 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
|
133 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
|
134 return -1; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
137 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
|
138 /* 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
|
139 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
|
140 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
141 0 lo string_size hi |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
142 | |<---used--->|<----------available----------->| |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
143 | | <--to pad-->|<---to write---> | |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
144 0 buf position |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
145 */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
146 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
|
147 (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
|
148 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
149 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
150 /* 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
|
151 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
|
152 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
|
153 self->pos += len; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
154 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
155 /* 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
|
156 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
|
157 self->string_size = self->pos; |
|
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 return len; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
161 } |
|
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 * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
164 bytesio_get_closed(bytesio *self) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
165 { |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
166 if (self->buf == NULL) { |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
167 Py_RETURN_TRUE; |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
168 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
169 else { |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
170 Py_RETURN_FALSE; |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
171 } |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
172 } |
|
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 /* 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
|
175 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
176 return_true(bytesio *self) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
177 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
178 Py_RETURN_TRUE; |
|
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(flush_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
182 "flush() -> None. Does nothing."); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
183 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
184 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
185 bytesio_flush(bytesio *self) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
186 { |
|
61183
ca01abd8ad87
Merged revisions 80722 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58631
diff
changeset
|
187 CHECK_CLOSED(self); |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
188 Py_RETURN_NONE; |
|
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 |
|
64496
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
191 PyDoc_STRVAR(getbuffer_doc, |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
192 "getbuffer() -> bytes.\n" |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
193 "\n" |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
194 "Get a read-write view over the contents of the BytesIO object."); |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
195 |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
196 static PyObject * |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
197 bytesio_getbuffer(bytesio *self) |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
198 { |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
199 PyTypeObject *type = &_PyBytesIOBuffer_Type; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
200 bytesiobuf *buf; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
201 PyObject *view; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
202 |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
203 CHECK_CLOSED(self); |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
204 |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
205 buf = (bytesiobuf *) type->tp_alloc(type, 0); |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
206 if (buf == NULL) |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
207 return NULL; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
208 Py_INCREF(self); |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
209 buf->source = self; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
210 view = PyMemoryView_FromObject((PyObject *) buf); |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
211 Py_DECREF(buf); |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
212 return view; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
213 } |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
214 |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
215 PyDoc_STRVAR(getval_doc, |
|
46722
831a30cd23a2
Fixed bytes naming in the docstrings of _bytesio.c.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46715
diff
changeset
|
216 "getvalue() -> bytes.\n" |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
217 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
218 "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
|
219 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
220 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
221 bytesio_getvalue(bytesio *self) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
222 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
223 CHECK_CLOSED(self); |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46722
diff
changeset
|
224 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
|
225 } |
|
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 PyDoc_STRVAR(isatty_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
228 "isatty() -> False.\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
229 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
230 "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
|
231 "to a tty-like device."); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
232 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
233 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
234 bytesio_isatty(bytesio *self) |
|
46694
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 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
237 Py_RETURN_FALSE; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
238 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
239 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
240 PyDoc_STRVAR(tell_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
241 "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
|
242 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
243 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
244 bytesio_tell(bytesio *self) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
245 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
246 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
247 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
|
248 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
249 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
250 PyDoc_STRVAR(read_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
251 "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
|
252 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
253 "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
|
254 "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
|
255 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
256 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
257 bytesio_read(bytesio *self, PyObject *args) |
|
46694
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 Py_ssize_t size, n; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
260 char *output; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
261 PyObject *arg = Py_None; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
262 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
263 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
264 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
265 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
|
266 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
267 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
268 if (PyLong_Check(arg)) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
269 size = PyLong_AsSsize_t(arg); |
|
49352
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
270 if (size == -1 && PyErr_Occurred()) |
|
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
271 return NULL; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
272 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
273 else if (arg == Py_None) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
274 /* 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
|
275 size = -1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
276 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
277 else { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
278 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
|
279 Py_TYPE(arg)->tp_name); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
280 return NULL; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
283 /* adjust invalid sizes */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
284 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
|
285 if (size < 0 || size > n) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
286 size = n; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
287 if (size < 0) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
288 size = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
289 } |
|
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 assert(self->buf != NULL); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
292 output = self->buf + self->pos; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
293 self->pos += size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
294 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46722
diff
changeset
|
295 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
|
296 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
297 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
298 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
299 PyDoc_STRVAR(read1_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
300 "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
|
301 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
302 "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
|
303 "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
|
304 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
305 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
306 bytesio_read1(bytesio *self, PyObject *n) |
|
46694
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 PyObject *arg, *res; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
309 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
310 arg = PyTuple_Pack(1, n); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
311 if (arg == NULL) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
312 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
313 res = bytesio_read(self, arg); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
314 Py_DECREF(arg); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
315 return res; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
318 PyDoc_STRVAR(readline_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
319 "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
|
320 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
321 "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
|
322 "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
|
323 "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
|
324 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
325 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
326 bytesio_readline(bytesio *self, PyObject *args) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
327 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
328 Py_ssize_t size, n; |
|
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:readline", &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 size = PyLong_AsSsize_t(arg); |
|
49352
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
339 if (size == -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 size = -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 n = get_line(self, &output); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
353 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
354 if (size >= 0 && size < n) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
355 size = n - size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
356 n -= size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
357 self->pos -= size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
358 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
359 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46722
diff
changeset
|
360 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
|
361 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
362 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
363 PyDoc_STRVAR(readlines_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
364 "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
|
365 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
366 "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
|
367 "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
|
368 "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
|
369 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
370 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
371 bytesio_readlines(bytesio *self, PyObject *args) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
372 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
373 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
|
374 PyObject *result, *line; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
375 char *output; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
376 PyObject *arg = Py_None; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
377 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
378 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
379 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
380 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
|
381 return NULL; |
|
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 if (PyLong_Check(arg)) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
384 maxsize = PyLong_AsSsize_t(arg); |
|
49352
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
385 if (maxsize == -1 && PyErr_Occurred()) |
|
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
386 return NULL; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
387 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
388 else if (arg == Py_None) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
389 /* No size limit, by default. */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
390 maxsize = -1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
391 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
392 else { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
393 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
|
394 Py_TYPE(arg)->tp_name); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
395 return NULL; |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
398 size = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
399 result = PyList_New(0); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
400 if (!result) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
401 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
402 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
403 while ((n = get_line(self, &output)) != 0) { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46722
diff
changeset
|
404 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
|
405 if (!line) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
406 goto on_error; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
407 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
|
408 Py_DECREF(line); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
409 goto on_error; |
|
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 Py_DECREF(line); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
412 size += n; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
413 if (maxsize > 0 && size >= maxsize) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
414 break; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
415 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
416 return result; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
417 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
418 on_error: |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
419 Py_DECREF(result); |
|
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 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
423 PyDoc_STRVAR(readinto_doc, |
|
46722
831a30cd23a2
Fixed bytes naming in the docstrings of _bytesio.c.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46715
diff
changeset
|
424 "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
|
425 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
426 "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
|
427 "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
|
428 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
429 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
430 bytesio_readinto(bytesio *self, PyObject *buffer) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
431 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
432 void *raw_buffer; |
|
66302
7cdbdbf3480b
Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
64383
diff
changeset
|
433 Py_ssize_t len, n; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
434 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
435 CHECK_CLOSED(self); |
|
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 (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
|
438 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
439 |
|
66302
7cdbdbf3480b
Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
64383
diff
changeset
|
440 /* adjust invalid sizes */ |
|
7cdbdbf3480b
Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
64383
diff
changeset
|
441 n = self->string_size - self->pos; |
|
7cdbdbf3480b
Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
64383
diff
changeset
|
442 if (len > n) { |
|
7cdbdbf3480b
Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
64383
diff
changeset
|
443 len = n; |
|
7cdbdbf3480b
Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
64383
diff
changeset
|
444 if (len < 0) |
|
7cdbdbf3480b
Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
64383
diff
changeset
|
445 len = 0; |
|
7cdbdbf3480b
Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
64383
diff
changeset
|
446 } |
|
46694
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 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
|
449 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
|
450 assert(len >= 0); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
451 self->pos += len; |
|
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 return PyLong_FromSsize_t(len); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
454 } |
|
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 PyDoc_STRVAR(truncate_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
457 "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
|
458 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
459 "Size defaults to the current file position, as returned by tell().\n" |
|
58631
d887db501a22
Merged revisions 77895-77896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
55609
diff
changeset
|
460 "The current file position is unchanged. Returns the new size.\n"); |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
461 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
462 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
463 bytesio_truncate(bytesio *self, PyObject *args) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
464 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
465 Py_ssize_t size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
466 PyObject *arg = Py_None; |
|
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 CHECK_CLOSED(self); |
|
64496
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
469 CHECK_EXPORTS(self); |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
470 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
471 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
|
472 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
473 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
474 if (PyLong_Check(arg)) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
475 size = PyLong_AsSsize_t(arg); |
|
49352
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
476 if (size == -1 && PyErr_Occurred()) |
|
b2801aca9d6b
Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47580
diff
changeset
|
477 return NULL; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
478 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
479 else if (arg == Py_None) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
480 /* 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
|
481 size = self->pos; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
482 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
483 else { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
484 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
|
485 Py_TYPE(arg)->tp_name); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
486 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
487 } |
|
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 (size < 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 size value %zd", size); |
|
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 if (size < self->string_size) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
496 self->string_size = size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
497 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
|
498 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
499 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
500 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
501 return PyLong_FromSsize_t(size); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
502 } |
|
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 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
505 bytesio_iternext(bytesio *self) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
506 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
507 char *next; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
508 Py_ssize_t n; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
509 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
510 CHECK_CLOSED(self); |
|
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 n = get_line(self, &next); |
|
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 if (!next || n == 0) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
515 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
516 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46722
diff
changeset
|
517 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
|
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 PyDoc_STRVAR(seek_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
521 "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
|
522 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
523 "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
|
524 " 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
|
525 " 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
|
526 " 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
|
527 "Returns the new absolute position."); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
528 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
529 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
530 bytesio_seek(bytesio *self, PyObject *args) |
|
46694
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 Py_ssize_t pos; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
533 int mode = 0; |
|
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 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
536 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
537 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
|
538 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
539 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
540 if (pos < 0 && mode == 0) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
541 PyErr_Format(PyExc_ValueError, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
542 "negative seek value %zd", pos); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
543 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
544 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
545 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
546 /* 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
|
547 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
|
548 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
|
549 if (mode == 1) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
550 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
|
551 PyErr_SetString(PyExc_OverflowError, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
552 "new position too large"); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
553 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
554 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
555 pos += self->pos; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
556 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
557 else if (mode == 2) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
558 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
|
559 PyErr_SetString(PyExc_OverflowError, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
560 "new position too large"); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
561 return NULL; |
|
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 pos += self->string_size; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
564 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
565 else if (mode != 0) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
566 PyErr_Format(PyExc_ValueError, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
567 "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
|
568 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
569 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
570 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
571 if (pos < 0) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
572 pos = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
573 self->pos = pos; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
574 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
575 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
|
576 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
577 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
578 PyDoc_STRVAR(write_doc, |
|
46722
831a30cd23a2
Fixed bytes naming in the docstrings of _bytesio.c.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46715
diff
changeset
|
579 "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
|
580 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
581 "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
|
582 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
583 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
584 bytesio_write(bytesio *self, PyObject *obj) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
585 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
586 Py_ssize_t n = 0; |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
587 Py_buffer buf; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
588 PyObject *result = NULL; |
|
46694
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 CHECK_CLOSED(self); |
|
64496
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
591 CHECK_EXPORTS(self); |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
592 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
593 if (PyObject_GetBuffer(obj, &buf, PyBUF_CONTIG_RO) < 0) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
594 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
595 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
596 if (buf.len != 0) |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
597 n = write_bytes(self, buf.buf, buf.len); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
598 if (n >= 0) |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
599 result = PyLong_FromSsize_t(n); |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
600 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
601 PyBuffer_Release(&buf); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
602 return result; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
603 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
604 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
605 PyDoc_STRVAR(writelines_doc, |
|
46715
38b94c097a55
Fixed docstring of _bytesio._BytesIO.writelines().
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46714
diff
changeset
|
606 "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
|
607 "\n" |
|
46715
38b94c097a55
Fixed docstring of _bytesio._BytesIO.writelines().
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46714
diff
changeset
|
608 "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
|
609 "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
|
610 "each string."); |
|
46694
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 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
613 bytesio_writelines(bytesio *self, PyObject *v) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
614 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
615 PyObject *it, *item; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
616 PyObject *ret; |
|
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 CHECK_CLOSED(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
619 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
620 it = PyObject_GetIter(v); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
621 if (it == NULL) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
622 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
623 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
624 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
|
625 ret = bytesio_write(self, item); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
626 Py_DECREF(item); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
627 if (ret == NULL) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
628 Py_DECREF(it); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
629 return NULL; |
|
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 Py_DECREF(ret); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
632 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
633 Py_DECREF(it); |
|
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 /* See if PyIter_Next failed */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
636 if (PyErr_Occurred()) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
637 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
638 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
639 Py_RETURN_NONE; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
640 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
641 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
642 PyDoc_STRVAR(close_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
643 "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
|
644 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
645 static PyObject * |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
646 bytesio_close(bytesio *self) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
647 { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
648 if (self->buf != NULL) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
649 PyMem_Free(self->buf); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
650 self->buf = NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
651 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
652 Py_RETURN_NONE; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
653 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
654 |
|
55398
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
655 /* Pickling support. |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
656 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
657 Note that only pickle protocol 2 and onward are supported since we use |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
658 extended __reduce__ API of PEP 307 to make BytesIO instances picklable. |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
659 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
660 Providing support for protocol < 2 would require the __reduce_ex__ method |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
661 which is notably long-winded when defined properly. |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
662 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
663 For BytesIO, the implementation would similar to one coded for |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
664 object.__reduce_ex__, but slightly less general. To be more specific, we |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
665 could call bytesio_getstate directly and avoid checking for the presence of |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
666 a fallback __reduce__ method. However, we would still need a __newobj__ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
667 function to use the efficient instance representation of PEP 307. |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
668 */ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
669 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
670 static PyObject * |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
671 bytesio_getstate(bytesio *self) |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
672 { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
673 PyObject *initvalue = bytesio_getvalue(self); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
674 PyObject *dict; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
675 PyObject *state; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
676 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
677 if (initvalue == NULL) |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
678 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
679 if (self->dict == NULL) { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
680 Py_INCREF(Py_None); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
681 dict = Py_None; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
682 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
683 else { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
684 dict = PyDict_Copy(self->dict); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
685 if (dict == NULL) |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
686 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
687 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
688 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
689 state = Py_BuildValue("(OnN)", initvalue, self->pos, dict); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
690 Py_DECREF(initvalue); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
691 return state; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
692 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
693 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
694 static PyObject * |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
695 bytesio_setstate(bytesio *self, PyObject *state) |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
696 { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
697 PyObject *result; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
698 PyObject *position_obj; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
699 PyObject *dict; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
700 Py_ssize_t pos; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
701 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
702 assert(state != NULL); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
703 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
704 /* We allow the state tuple to be longer than 3, because we may need |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
705 someday to extend the object's state without breaking |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
706 backward-compatibility. */ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
707 if (!PyTuple_Check(state) || Py_SIZE(state) < 3) { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
708 PyErr_Format(PyExc_TypeError, |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
709 "%.200s.__setstate__ argument should be 3-tuple, got %.200s", |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
710 Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
711 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
712 } |
|
64496
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
713 CHECK_EXPORTS(self); |
|
55398
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
714 /* Reset the object to its default state. This is only needed to handle |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
715 the case of repeated calls to __setstate__. */ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
716 self->string_size = 0; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
717 self->pos = 0; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
718 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
719 /* Set the value of the internal buffer. If state[0] does not support the |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
720 buffer protocol, bytesio_write will raise the appropriate TypeError. */ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
721 result = bytesio_write(self, PyTuple_GET_ITEM(state, 0)); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
722 if (result == NULL) |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
723 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
724 Py_DECREF(result); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
725 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
726 /* Set carefully the position value. Alternatively, we could use the seek |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
727 method instead of modifying self->pos directly to better protect the |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
728 object internal state against errneous (or malicious) inputs. */ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
729 position_obj = PyTuple_GET_ITEM(state, 1); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
730 if (!PyLong_Check(position_obj)) { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
731 PyErr_Format(PyExc_TypeError, |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
732 "second item of state must be an integer, not %.200s", |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
733 Py_TYPE(position_obj)->tp_name); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
734 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
735 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
736 pos = PyLong_AsSsize_t(position_obj); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
737 if (pos == -1 && PyErr_Occurred()) |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
738 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
739 if (pos < 0) { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
740 PyErr_SetString(PyExc_ValueError, |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
741 "position value cannot be negative"); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
742 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
743 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
744 self->pos = pos; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
745 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
746 /* Set the dictionary of the instance variables. */ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
747 dict = PyTuple_GET_ITEM(state, 2); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
748 if (dict != Py_None) { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
749 if (!PyDict_Check(dict)) { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
750 PyErr_Format(PyExc_TypeError, |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
751 "third item of state should be a dict, got a %.200s", |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
752 Py_TYPE(dict)->tp_name); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
753 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
754 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
755 if (self->dict) { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
756 /* Alternatively, we could replace the internal dictionary |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
757 completely. However, it seems more practical to just update it. */ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
758 if (PyDict_Update(self->dict, dict) < 0) |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
759 return NULL; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
760 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
761 else { |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
762 Py_INCREF(dict); |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
763 self->dict = dict; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
764 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
765 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
766 |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
767 Py_RETURN_NONE; |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
768 } |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
769 |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
770 static void |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
771 bytesio_dealloc(bytesio *self) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
772 { |
|
55609
9d40586a6afc
Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55581
diff
changeset
|
773 _PyObject_GC_UNTRACK(self); |
|
64496
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
774 if (self->exports > 0) { |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
775 PyErr_SetString(PyExc_SystemError, |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
776 "deallocated BytesIO object has exported buffers"); |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
777 PyErr_Print(); |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
778 } |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
779 if (self->buf != NULL) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
780 PyMem_Free(self->buf); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
781 self->buf = NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
782 } |
|
55609
9d40586a6afc
Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55581
diff
changeset
|
783 Py_CLEAR(self->dict); |
|
9d40586a6afc
Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55581
diff
changeset
|
784 if (self->weakreflist != NULL) |
|
9d40586a6afc
Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55581
diff
changeset
|
785 PyObject_ClearWeakRefs((PyObject *) self); |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
786 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
|
787 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
788 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
789 static PyObject * |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
790 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
|
791 { |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
792 bytesio *self; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
793 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
794 assert(type != NULL && type->tp_alloc != NULL); |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
795 self = (bytesio *)type->tp_alloc(type, 0); |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
796 if (self == NULL) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
797 return NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
798 |
|
55398
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
799 /* tp_alloc initializes all the fields to zero. So we don't have to |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
800 initialize them here. */ |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
801 |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
802 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
|
803 if (self->buf == NULL) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
804 Py_DECREF(self); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
805 return PyErr_NoMemory(); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
806 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
807 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
808 return (PyObject *)self; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
809 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
810 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
811 static int |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
812 bytesio_init(bytesio *self, PyObject *args, PyObject *kwds) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
813 { |
|
55581
1acd0277d225
Merged revisions 74316,74335 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54710
diff
changeset
|
814 char *kwlist[] = {"initial_bytes", NULL}; |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
815 PyObject *initvalue = NULL; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
816 |
|
55581
1acd0277d225
Merged revisions 74316,74335 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54710
diff
changeset
|
817 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:BytesIO", kwlist, |
|
1acd0277d225
Merged revisions 74316,74335 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54710
diff
changeset
|
818 &initvalue)) |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
819 return -1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
820 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
821 /* 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
|
822 self->string_size = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
823 self->pos = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
824 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
825 if (initvalue && initvalue != Py_None) { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
826 PyObject *res; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
827 res = bytesio_write(self, initvalue); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
828 if (res == NULL) |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
829 return -1; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
830 Py_DECREF(res); |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
831 self->pos = 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
832 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
833 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
834 return 0; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
835 } |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
836 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
837 static int |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
838 bytesio_traverse(bytesio *self, visitproc visit, void *arg) |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
839 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
840 Py_VISIT(self->dict); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
841 return 0; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
842 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
843 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
844 static int |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
845 bytesio_clear(bytesio *self) |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
846 { |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
847 Py_CLEAR(self->dict); |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
848 return 0; |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
849 } |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
850 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
851 |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
852 static PyGetSetDef bytesio_getsetlist[] = { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
853 {"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
|
854 "True if the file is closed."}, |
|
53259
b7b8fbd010ca
use NULL for the ends of tables
Benjamin Peterson <benjamin@python.org>
parents:
52843
diff
changeset
|
855 {NULL}, /* sentinel */ |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
856 }; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
857 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
858 static struct PyMethodDef bytesio_methods[] = { |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
859 {"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
|
860 {"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
|
861 {"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
|
862 {"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
|
863 {"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
|
864 {"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
|
865 {"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
|
866 {"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
|
867 {"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
|
868 {"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
|
869 {"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
|
870 {"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
|
871 {"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
|
872 {"read", (PyCFunction)bytesio_read, METH_VARARGS, read_doc}, |
|
64496
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
873 {"getbuffer", (PyCFunction)bytesio_getbuffer, METH_NOARGS, getbuffer_doc}, |
|
64383
8b94f15cb650
Merged revisions 84438 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61183
diff
changeset
|
874 {"getvalue", (PyCFunction)bytesio_getvalue, METH_NOARGS, getval_doc}, |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
875 {"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
|
876 {"truncate", (PyCFunction)bytesio_truncate, METH_VARARGS, truncate_doc}, |
|
55398
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
877 {"__getstate__", (PyCFunction)bytesio_getstate, METH_NOARGS, NULL}, |
|
538850b38931
Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55395
diff
changeset
|
878 {"__setstate__", (PyCFunction)bytesio_setstate, METH_O, NULL}, |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
879 {NULL, NULL} /* sentinel */ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
880 }; |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
881 |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
882 PyDoc_STRVAR(bytesio_doc, |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
883 "BytesIO([buffer]) -> object\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
884 "\n" |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
885 "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
|
886 "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
|
887 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
888 PyTypeObject PyBytesIO_Type = { |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
889 PyVarObject_HEAD_INIT(NULL, 0) |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
890 "_io.BytesIO", /*tp_name*/ |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
891 sizeof(bytesio), /*tp_basicsize*/ |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
892 0, /*tp_itemsize*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
893 (destructor)bytesio_dealloc, /*tp_dealloc*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
894 0, /*tp_print*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
895 0, /*tp_getattr*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
896 0, /*tp_setattr*/ |
|
51271
87fbbf22adf3
Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
49352
diff
changeset
|
897 0, /*tp_reserved*/ |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
898 0, /*tp_repr*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
899 0, /*tp_as_number*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
900 0, /*tp_as_sequence*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
901 0, /*tp_as_mapping*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
902 0, /*tp_hash*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
903 0, /*tp_call*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
904 0, /*tp_str*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
905 0, /*tp_getattro*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
906 0, /*tp_setattro*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
907 0, /*tp_as_buffer*/ |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
908 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
909 Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
910 bytesio_doc, /*tp_doc*/ |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
911 (traverseproc)bytesio_traverse, /*tp_traverse*/ |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
51271
diff
changeset
|
912 (inquiry)bytesio_clear, /*tp_clear*/ |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
913 0, /*tp_richcompare*/ |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
914 offsetof(bytesio, weakreflist), /*tp_weaklistoffset*/ |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
915 PyObject_SelfIter, /*tp_iter*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
916 (iternextfunc)bytesio_iternext, /*tp_iternext*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
917 bytesio_methods, /*tp_methods*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
918 0, /*tp_members*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
919 bytesio_getsetlist, /*tp_getset*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
920 0, /*tp_base*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
921 0, /*tp_dict*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
922 0, /*tp_descr_get*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
923 0, /*tp_descr_set*/ |
|
54710
fcbcd931338b
move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents:
53259
diff
changeset
|
924 offsetof(bytesio, dict), /*tp_dictoffset*/ |
|
46694
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
925 (initproc)bytesio_init, /*tp_init*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
926 0, /*tp_alloc*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
927 bytesio_new, /*tp_new*/ |
|
cda816c3a1e4
Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff
changeset
|
928 }; |
|
64496
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
929 |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
930 |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
931 /* |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
932 * Implementation of the small intermediate object used by getbuffer(). |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
933 * getbuffer() returns a memoryview over this object, which should make it |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
934 * invisible from Python code. |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
935 */ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
936 |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
937 static int |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
938 bytesiobuf_getbuffer(bytesiobuf *obj, Py_buffer *view, int flags) |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
939 { |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
940 int ret; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
941 bytesio *b = (bytesio *) obj->source; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
942 if (view == NULL) { |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
943 b->exports++; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
944 return 0; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
945 } |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
946 ret = PyBuffer_FillInfo(view, (PyObject*)obj, b->buf, b->string_size, |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
947 0, flags); |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
948 if (ret >= 0) { |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
949 b->exports++; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
950 } |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
951 return ret; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
952 } |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
953 |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
954 static void |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
955 bytesiobuf_releasebuffer(bytesiobuf *obj, Py_buffer *view) |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
956 { |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
957 bytesio *b = (bytesio *) obj->source; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
958 b->exports--; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
959 } |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
960 |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
961 static int |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
962 bytesiobuf_traverse(bytesiobuf *self, visitproc visit, void *arg) |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
963 { |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
964 Py_VISIT(self->source); |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
965 return 0; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
966 } |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
967 |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
968 static void |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
969 bytesiobuf_dealloc(bytesiobuf *self) |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
970 { |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
971 Py_CLEAR(self->source); |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
972 Py_TYPE(self)->tp_free(self); |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
973 } |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
974 |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
975 static PyBufferProcs bytesiobuf_as_buffer = { |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
976 (getbufferproc) bytesiobuf_getbuffer, |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
977 (releasebufferproc) bytesiobuf_releasebuffer, |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
978 }; |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
979 |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
980 PyTypeObject _PyBytesIOBuffer_Type = { |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
981 PyVarObject_HEAD_INIT(NULL, 0) |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
982 "_io._BytesIOBuffer", /*tp_name*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
983 sizeof(bytesiobuf), /*tp_basicsize*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
984 0, /*tp_itemsize*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
985 (destructor)bytesiobuf_dealloc, /*tp_dealloc*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
986 0, /*tp_print*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
987 0, /*tp_getattr*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
988 0, /*tp_setattr*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
989 0, /*tp_reserved*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
990 0, /*tp_repr*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
991 0, /*tp_as_number*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
992 0, /*tp_as_sequence*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
993 0, /*tp_as_mapping*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
994 0, /*tp_hash*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
995 0, /*tp_call*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
996 0, /*tp_str*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
997 0, /*tp_getattro*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
998 0, /*tp_setattro*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
999 &bytesiobuf_as_buffer, /*tp_as_buffer*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1000 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1001 0, /*tp_doc*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1002 (traverseproc)bytesiobuf_traverse, /*tp_traverse*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1003 0, /*tp_clear*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1004 0, /*tp_richcompare*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1005 0, /*tp_weaklistoffset*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1006 0, /*tp_iter*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1007 0, /*tp_iternext*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1008 0, /*tp_methods*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1009 0, /*tp_members*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1010 0, /*tp_getset*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1011 0, /*tp_base*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1012 0, /*tp_dict*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1013 0, /*tp_descr_get*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1014 0, /*tp_descr_set*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1015 0, /*tp_dictoffset*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1016 0, /*tp_init*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1017 0, /*tp_alloc*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1018 0, /*tp_new*/ |
|
21405812f312
Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents:
64380
diff
changeset
|
1019 }; |
