annotate Modules/_io/bytesio.c @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents 90c8fa612e5b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
78852
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
124 object. Returns the number of bytes written, or -1 on error. */
46694
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
78852
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
174 PyDoc_STRVAR(readable_doc,
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
175 "readable() -> bool. Returns True if the IO object can be read.");
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
176
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
177 PyDoc_STRVAR(writable_doc,
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
178 "writable() -> bool. Returns True if the IO object can be written.");
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
179
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
180 PyDoc_STRVAR(seekable_doc,
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
181 "seekable() -> bool. Returns True if the IO object can be seeked.");
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
182
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
183 /* 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
184 static PyObject *
78852
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
185 return_not_closed(bytesio *self)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
186 {
78852
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
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_TRUE;
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
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
191 PyDoc_STRVAR(flush_doc,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
192 "flush() -> None. Does nothing.");
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
193
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
194 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
195 bytesio_flush(bytesio *self)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
196 {
61183
ca01abd8ad87 Merged revisions 80722 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58631
diff changeset
197 CHECK_CLOSED(self);
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
198 Py_RETURN_NONE;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
199 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
200
64496
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
201 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
202 "getbuffer() -> bytes.\n"
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
203 "\n"
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
204 "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
205
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
206 static PyObject *
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
207 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
208 {
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
209 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
210 bytesiobuf *buf;
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
211 PyObject *view;
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
212
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
213 CHECK_CLOSED(self);
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
214
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
215 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
216 if (buf == NULL)
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
217 return NULL;
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
218 Py_INCREF(self);
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
219 buf->source = self;
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
220 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
221 Py_DECREF(buf);
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
222 return view;
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
223 }
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
224
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
225 PyDoc_STRVAR(getval_doc,
46722
831a30cd23a2 Fixed bytes naming in the docstrings of _bytesio.c.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46715
diff changeset
226 "getvalue() -> bytes.\n"
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
227 "\n"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
228 "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
229
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
230 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
231 bytesio_getvalue(bytesio *self)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
232 {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
233 CHECK_CLOSED(self);
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46722
diff changeset
234 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
235 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
236
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
237 PyDoc_STRVAR(isatty_doc,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
238 "isatty() -> False.\n"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
239 "\n"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
240 "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
241 "to a tty-like device.");
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_isatty(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 Py_RETURN_FALSE;
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(tell_doc,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
251 "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
252
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
253 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
254 bytesio_tell(bytesio *self)
46694
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 CHECK_CLOSED(self);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
257 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
258 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
259
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
260 PyDoc_STRVAR(read_doc,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
261 "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
262 "\n"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
263 "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
264 "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
265
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
266 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
267 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
268 {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
269 Py_ssize_t size, n;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
270 char *output;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
271 PyObject *arg = Py_None;
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 CHECK_CLOSED(self);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
274
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
275 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
276 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
277
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
278 if (PyLong_Check(arg)) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
279 size = PyLong_AsSsize_t(arg);
49352
b2801aca9d6b Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47580
diff changeset
280 if (size == -1 && PyErr_Occurred())
b2801aca9d6b Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47580
diff changeset
281 return NULL;
46694
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 else if (arg == Py_None) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
284 /* 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
285 size = -1;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
286 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
287 else {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
288 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
289 Py_TYPE(arg)->tp_name);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
290 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
291 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
292
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
293 /* adjust invalid sizes */
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
294 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
295 if (size < 0 || size > n) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
296 size = n;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
297 if (size < 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
298 size = 0;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
299 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
300
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
301 assert(self->buf != NULL);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
302 output = self->buf + self->pos;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
303 self->pos += size;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
304
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46722
diff changeset
305 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
306 }
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
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
309 PyDoc_STRVAR(read1_doc,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
310 "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
311 "\n"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
312 "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
313 "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
314
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
315 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
316 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
317 {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
318 PyObject *arg, *res;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
319
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
320 arg = PyTuple_Pack(1, n);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
321 if (arg == NULL)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
322 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
323 res = bytesio_read(self, arg);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
324 Py_DECREF(arg);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
325 return res;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
326 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
327
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
328 PyDoc_STRVAR(readline_doc,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
329 "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
330 "\n"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
331 "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
332 "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
333 "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
334
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
335 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
336 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
337 {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
338 Py_ssize_t size, n;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
339 char *output;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
340 PyObject *arg = Py_None;
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 CHECK_CLOSED(self);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
343
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
344 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
345 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
346
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
347 if (PyLong_Check(arg)) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
348 size = PyLong_AsSsize_t(arg);
49352
b2801aca9d6b Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47580
diff changeset
349 if (size == -1 && PyErr_Occurred())
b2801aca9d6b Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47580
diff changeset
350 return NULL;
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
351 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
352 else if (arg == Py_None) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
353 /* No size limit, by default. */
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
354 size = -1;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
355 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
356 else {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
357 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
358 Py_TYPE(arg)->tp_name);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
359 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
360 }
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 n = get_line(self, &output);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
363
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
364 if (size >= 0 && size < n) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
365 size = n - size;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
366 n -= size;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
367 self->pos -= size;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
368 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
369
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46722
diff changeset
370 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
371 }
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 PyDoc_STRVAR(readlines_doc,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
374 "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
375 "\n"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
376 "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
377 "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
378 "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
379
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
380 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
381 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
382 {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
383 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
384 PyObject *result, *line;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
385 char *output;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
386 PyObject *arg = Py_None;
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 CHECK_CLOSED(self);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
389
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
390 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
391 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
392
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
393 if (PyLong_Check(arg)) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
394 maxsize = PyLong_AsSsize_t(arg);
49352
b2801aca9d6b Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47580
diff changeset
395 if (maxsize == -1 && PyErr_Occurred())
b2801aca9d6b Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47580
diff changeset
396 return NULL;
46694
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 else if (arg == Py_None) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
399 /* No size limit, by default. */
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
400 maxsize = -1;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
401 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
402 else {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
403 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
404 Py_TYPE(arg)->tp_name);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
405 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
406 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
407
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
408 size = 0;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
409 result = PyList_New(0);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
410 if (!result)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
411 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
412
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
413 while ((n = get_line(self, &output)) != 0) {
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46722
diff changeset
414 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
415 if (!line)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
416 goto on_error;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
417 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
418 Py_DECREF(line);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
419 goto on_error;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
420 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
421 Py_DECREF(line);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
422 size += n;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
423 if (maxsize > 0 && size >= maxsize)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
424 break;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
425 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
426 return result;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
427
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
428 on_error:
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
429 Py_DECREF(result);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
430 return NULL;
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
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
433 PyDoc_STRVAR(readinto_doc,
46722
831a30cd23a2 Fixed bytes naming in the docstrings of _bytesio.c.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46715
diff changeset
434 "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
435 "\n"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
436 "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
437 "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
438
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
439 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
440 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
441 {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
442 void *raw_buffer;
66302
7cdbdbf3480b Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64383
diff changeset
443 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
444
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
445 CHECK_CLOSED(self);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
446
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
447 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
448 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
449
66302
7cdbdbf3480b Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64383
diff changeset
450 /* adjust invalid sizes */
7cdbdbf3480b Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64383
diff changeset
451 n = self->string_size - self->pos;
7cdbdbf3480b Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64383
diff changeset
452 if (len > n) {
7cdbdbf3480b Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64383
diff changeset
453 len = n;
7cdbdbf3480b Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64383
diff changeset
454 if (len < 0)
7cdbdbf3480b Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64383
diff changeset
455 len = 0;
7cdbdbf3480b Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64383
diff changeset
456 }
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
457
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
458 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
459 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
460 assert(len >= 0);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
461 self->pos += len;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
462
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
463 return PyLong_FromSsize_t(len);
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
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
466 PyDoc_STRVAR(truncate_doc,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
467 "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
468 "\n"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
469 "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
470 "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
471
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
472 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
473 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
474 {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
475 Py_ssize_t size;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
476 PyObject *arg = Py_None;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
477
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
478 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
479 CHECK_EXPORTS(self);
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
480
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
481 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
482 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
483
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
484 if (PyLong_Check(arg)) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
485 size = PyLong_AsSsize_t(arg);
49352
b2801aca9d6b Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47580
diff changeset
486 if (size == -1 && PyErr_Occurred())
b2801aca9d6b Merged revisions 66693 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47580
diff changeset
487 return NULL;
46694
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 else if (arg == Py_None) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
490 /* 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
491 size = self->pos;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
492 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
493 else {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
494 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
495 Py_TYPE(arg)->tp_name);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
496 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
497 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
498
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
499 if (size < 0) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
500 PyErr_Format(PyExc_ValueError,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
501 "negative size value %zd", size);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
502 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
503 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
504
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
505 if (size < self->string_size) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
506 self->string_size = size;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
507 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
508 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
509 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
510
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
511 return PyLong_FromSsize_t(size);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
512 }
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 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
515 bytesio_iternext(bytesio *self)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
516 {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
517 char *next;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
518 Py_ssize_t n;
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 CHECK_CLOSED(self);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
521
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
522 n = get_line(self, &next);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
523
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
524 if (!next || n == 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
525 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
526
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46722
diff changeset
527 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
528 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
529
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
530 PyDoc_STRVAR(seek_doc,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
531 "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
532 "\n"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
533 "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
534 " 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
535 " 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
536 " 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
537 "Returns the new absolute position.");
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
538
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
539 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
540 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
541 {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
542 Py_ssize_t pos;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
543 int mode = 0;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
544
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
545 CHECK_CLOSED(self);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
546
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
547 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
548 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
549
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
550 if (pos < 0 && mode == 0) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
551 PyErr_Format(PyExc_ValueError,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
552 "negative seek value %zd", pos);
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
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
556 /* 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
557 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
558 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
559 if (mode == 1) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
560 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
561 PyErr_SetString(PyExc_OverflowError,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
562 "new position too large");
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
563 return NULL;
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 pos += self->pos;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
566 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
567 else if (mode == 2) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
568 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
569 PyErr_SetString(PyExc_OverflowError,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
570 "new position too large");
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
571 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
572 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
573 pos += self->string_size;
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 else if (mode != 0) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
576 PyErr_Format(PyExc_ValueError,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
577 "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
578 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
579 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
580
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
581 if (pos < 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
582 pos = 0;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
583 self->pos = pos;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
584
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
585 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
586 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
587
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
588 PyDoc_STRVAR(write_doc,
46722
831a30cd23a2 Fixed bytes naming in the docstrings of _bytesio.c.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46715
diff changeset
589 "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
590 "\n"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
591 "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
592
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
593 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
594 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
595 {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
596 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
597 Py_buffer buf;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
598 PyObject *result = NULL;
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
599
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
600 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
601 CHECK_EXPORTS(self);
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
602
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
603 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
604 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
605
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
606 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
607 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
608 if (n >= 0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
609 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
610
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
611 PyBuffer_Release(&buf);
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
612 return result;
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
613 }
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 PyDoc_STRVAR(writelines_doc,
46715
38b94c097a55 Fixed docstring of _bytesio._BytesIO.writelines().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46714
diff changeset
616 "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
617 "\n"
46715
38b94c097a55 Fixed docstring of _bytesio._BytesIO.writelines().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46714
diff changeset
618 "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
619 "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
620 "each string.");
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
621
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
622 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
623 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
624 {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
625 PyObject *it, *item;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
626 PyObject *ret;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
627
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
628 CHECK_CLOSED(self);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
629
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
630 it = PyObject_GetIter(v);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
631 if (it == NULL)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
632 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
633
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
634 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
635 ret = bytesio_write(self, item);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
636 Py_DECREF(item);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
637 if (ret == NULL) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
638 Py_DECREF(it);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
639 return NULL;
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 Py_DECREF(ret);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
642 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
643 Py_DECREF(it);
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 /* See if PyIter_Next failed */
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
646 if (PyErr_Occurred())
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
647 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
648
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
649 Py_RETURN_NONE;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
650 }
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 PyDoc_STRVAR(close_doc,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
653 "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
654
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
655 static PyObject *
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
656 bytesio_close(bytesio *self)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
657 {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
658 if (self->buf != NULL) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
659 PyMem_Free(self->buf);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
660 self->buf = NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
661 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
662 Py_RETURN_NONE;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
663 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
664
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
665 /* Pickling support.
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
666
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
667 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
668 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
669
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
670 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
671 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
672
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
673 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
674 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
675 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
676 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
677 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
678 */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
679
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
680 static PyObject *
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
681 bytesio_getstate(bytesio *self)
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 PyObject *initvalue = bytesio_getvalue(self);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
684 PyObject *dict;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
685 PyObject *state;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
686
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
687 if (initvalue == NULL)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
688 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
689 if (self->dict == NULL) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
690 Py_INCREF(Py_None);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
691 dict = Py_None;
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 else {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
694 dict = PyDict_Copy(self->dict);
78876
df663603e67a Issue #15868: Fix refleak in bytesio.c (Coverity #715365).
Stefan Krah <skrah@bytereef.org>
parents: 78852
diff changeset
695 if (dict == NULL) {
df663603e67a Issue #15868: Fix refleak in bytesio.c (Coverity #715365).
Stefan Krah <skrah@bytereef.org>
parents: 78852
diff changeset
696 Py_DECREF(initvalue);
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
697 return NULL;
78876
df663603e67a Issue #15868: Fix refleak in bytesio.c (Coverity #715365).
Stefan Krah <skrah@bytereef.org>
parents: 78852
diff changeset
698 }
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
699 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
700
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
701 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
702 Py_DECREF(initvalue);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
703 return state;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
704 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
705
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
706 static PyObject *
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
707 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
708 {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
709 PyObject *result;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
710 PyObject *position_obj;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
711 PyObject *dict;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
712 Py_ssize_t pos;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
713
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
714 assert(state != NULL);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
715
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
716 /* 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
717 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
718 backward-compatibility. */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
719 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
720 PyErr_Format(PyExc_TypeError,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
721 "%.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
722 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
723 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
724 }
64496
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
725 CHECK_EXPORTS(self);
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
726 /* 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
727 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
728 self->string_size = 0;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
729 self->pos = 0;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
730
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
731 /* 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
732 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
733 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
734 if (result == NULL)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
735 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
736 Py_DECREF(result);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
737
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
738 /* 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
739 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
740 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
741 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
742 if (!PyLong_Check(position_obj)) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
743 PyErr_Format(PyExc_TypeError,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
744 "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
745 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
746 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
747 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
748 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
749 if (pos == -1 && PyErr_Occurred())
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
750 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
751 if (pos < 0) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
752 PyErr_SetString(PyExc_ValueError,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
753 "position value cannot be negative");
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
754 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
755 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
756 self->pos = pos;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
757
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
758 /* 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
759 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
760 if (dict != Py_None) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
761 if (!PyDict_Check(dict)) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
762 PyErr_Format(PyExc_TypeError,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
763 "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
764 Py_TYPE(dict)->tp_name);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
765 return NULL;
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 if (self->dict) {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
768 /* 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
769 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
770 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
771 return NULL;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
772 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
773 else {
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
774 Py_INCREF(dict);
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
775 self->dict = dict;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
776 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
777 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
778
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
779 Py_RETURN_NONE;
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
780 }
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
781
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
782 static void
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
783 bytesio_dealloc(bytesio *self)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
784 {
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
785 _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
786 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
787 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
788 "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
789 PyErr_Print();
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
790 }
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
791 if (self->buf != NULL) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
792 PyMem_Free(self->buf);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
793 self->buf = NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
794 }
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
795 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
796 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
797 PyObject_ClearWeakRefs((PyObject *) self);
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
798 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
799 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
800
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
801 static PyObject *
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
802 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
803 {
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
804 bytesio *self;
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
805
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
806 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
807 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
808 if (self == NULL)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
809 return NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
810
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
811 /* 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
812 initialize them here. */
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55395
diff changeset
813
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
814 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
815 if (self->buf == NULL) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
816 Py_DECREF(self);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
817 return PyErr_NoMemory();
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
818 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
819
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
820 return (PyObject *)self;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
821 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
822
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
823 static int
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
824 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
825 {
55581
1acd0277d225 Merged revisions 74316,74335 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54710
diff changeset
826 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
827 PyObject *initvalue = NULL;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
828
55581
1acd0277d225 Merged revisions 74316,74335 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54710
diff changeset
829 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
830 &initvalue))
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
831 return -1;
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 /* 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
834 self->string_size = 0;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
835 self->pos = 0;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
836
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
837 if (initvalue && initvalue != Py_None) {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
838 PyObject *res;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
839 res = bytesio_write(self, initvalue);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
840 if (res == NULL)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
841 return -1;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
842 Py_DECREF(res);
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
843 self->pos = 0;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
844 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
845
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
846 return 0;
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
847 }
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
848
78321
1d3155750808 Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
849 static PyObject *
1d3155750808 Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
850 bytesio_sizeof(bytesio *self, void *unused)
1d3155750808 Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
851 {
1d3155750808 Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
852 Py_ssize_t res;
1d3155750808 Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
853
1d3155750808 Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
854 res = sizeof(bytesio);
1d3155750808 Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
855 if (self->buf)
1d3155750808 Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
856 res += self->buf_size;
1d3155750808 Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
857 return PyLong_FromSsize_t(res);
1d3155750808 Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
858 }
1d3155750808 Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
859
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
860 static int
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
861 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
862 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
863 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
864 return 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
865 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
866
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
867 static int
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
868 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
869 {
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
870 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
871 return 0;
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
872 }
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
873
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
874
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
875 static PyGetSetDef bytesio_getsetlist[] = {
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
876 {"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
877 "True if the file is closed."},
53259
b7b8fbd010ca use NULL for the ends of tables
Benjamin Peterson <benjamin@python.org>
parents: 52843
diff changeset
878 {NULL}, /* sentinel */
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
879 };
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 static struct PyMethodDef bytesio_methods[] = {
78852
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
882 {"readable", (PyCFunction)return_not_closed, METH_NOARGS, readable_doc},
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
883 {"seekable", (PyCFunction)return_not_closed, METH_NOARGS, seekable_doc},
524931e5aebf Issue #15841: The readable(), writable() and seekable() methods of BytesIO
Antoine Pitrou <solipsis@pitrou.net>
parents: 78321
diff changeset
884 {"writable", (PyCFunction)return_not_closed, METH_NOARGS, writable_doc},
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
885 {"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
886 {"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
887 {"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
888 {"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
889 {"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
890 {"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
891 {"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
892 {"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
893 {"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
894 {"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
895 {"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
896 {"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
897 {"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
898 {"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
899 {"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
900 {"__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
901 {"__setstate__", (PyCFunction)bytesio_setstate, METH_O, NULL},
78321
1d3155750808 Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
902 {"__sizeof__", (PyCFunction)bytesio_sizeof, METH_NOARGS, NULL},
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
903 {NULL, NULL} /* sentinel */
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
904 };
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
905
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
906 PyDoc_STRVAR(bytesio_doc,
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
907 "BytesIO([buffer]) -> object\n"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
908 "\n"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
909 "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
910 "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
911
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
912 PyTypeObject PyBytesIO_Type = {
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
913 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
914 "_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
915 sizeof(bytesio), /*tp_basicsize*/
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
916 0, /*tp_itemsize*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
917 (destructor)bytesio_dealloc, /*tp_dealloc*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
918 0, /*tp_print*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
919 0, /*tp_getattr*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
920 0, /*tp_setattr*/
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49352
diff changeset
921 0, /*tp_reserved*/
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
922 0, /*tp_repr*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
923 0, /*tp_as_number*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
924 0, /*tp_as_sequence*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
925 0, /*tp_as_mapping*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
926 0, /*tp_hash*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
927 0, /*tp_call*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
928 0, /*tp_str*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
929 0, /*tp_getattro*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
930 0, /*tp_setattro*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
931 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
932 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
933 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
934 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
935 (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
936 (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
937 0, /*tp_richcompare*/
54710
fcbcd931338b move to a naming scheme with all lowercase and underscores
Benjamin Peterson <benjamin@python.org>
parents: 53259
diff changeset
938 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
939 PyObject_SelfIter, /*tp_iter*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
940 (iternextfunc)bytesio_iternext, /*tp_iternext*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
941 bytesio_methods, /*tp_methods*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
942 0, /*tp_members*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
943 bytesio_getsetlist, /*tp_getset*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
944 0, /*tp_base*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
945 0, /*tp_dict*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
946 0, /*tp_descr_get*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
947 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
948 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
949 (initproc)bytesio_init, /*tp_init*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
950 0, /*tp_alloc*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
951 bytesio_new, /*tp_new*/
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
952 };
64496
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
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
955 /*
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
956 * 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
957 * 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
958 * 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
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_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
963 {
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
964 int ret;
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
965 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
966 if (view == NULL) {
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
967 b->exports++;
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
968 return 0;
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
969 }
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
970 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
971 0, flags);
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
972 if (ret >= 0) {
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
973 b->exports++;
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 return ret;
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
976 }
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
977
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
978 static void
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
979 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
980 {
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
981 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
982 b->exports--;
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
983 }
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
984
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
985 static int
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
986 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
987 {
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
988 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
989 return 0;
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
990 }
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
991
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
992 static void
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
993 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
994 {
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
995 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
996 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
997 }
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
998
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
999 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
1000 (getbufferproc) bytesiobuf_getbuffer,
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1001 (releasebufferproc) bytesiobuf_releasebuffer,
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1002 };
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1003
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1004 PyTypeObject _PyBytesIOBuffer_Type = {
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1005 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
1006 "_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
1007 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
1008 0, /*tp_itemsize*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1009 (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
1010 0, /*tp_print*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1011 0, /*tp_getattr*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1012 0, /*tp_setattr*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1013 0, /*tp_reserved*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1014 0, /*tp_repr*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1015 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
1016 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
1017 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
1018 0, /*tp_hash*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1019 0, /*tp_call*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1020 0, /*tp_str*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1021 0, /*tp_getattro*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1022 0, /*tp_setattro*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1023 &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
1024 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
1025 0, /*tp_doc*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1026 (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
1027 0, /*tp_clear*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1028 0, /*tp_richcompare*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1029 0, /*tp_weaklistoffset*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1030 0, /*tp_iter*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1031 0, /*tp_iternext*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1032 0, /*tp_methods*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1033 0, /*tp_members*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1034 0, /*tp_getset*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1035 0, /*tp_base*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1036 0, /*tp_dict*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1037 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
1038 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
1039 0, /*tp_dictoffset*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1040 0, /*tp_init*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1041 0, /*tp_alloc*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1042 0, /*tp_new*/
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64380
diff changeset
1043 };