annotate Modules/_io/bufferedio.c @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 1102e86b8739
children 751a91e332d9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1 /*
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2 An implementation of Buffered I/O as defined by PEP 3116 - "New I/O"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
3
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
4 Classes defined here: BufferedIOBase, BufferedReader, BufferedWriter,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
5 BufferedRandom.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
6
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
7 Written by Amaury Forgeot d'Arc and Antoine Pitrou
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
8 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
9
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
10 #define PY_SSIZE_T_CLEAN
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
11 #include "Python.h"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
12 #include "structmember.h"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
13 #include "pythread.h"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
14 #include "_iomodule.h"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
15
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
16 /*
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
17 * BufferedIOBase class, inherits from IOBase.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
18 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
19 PyDoc_STRVAR(bufferediobase_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
20 "Base class for buffered IO objects.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
21 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
22 "The main difference with RawIOBase is that the read() method\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
23 "supports omitting the size argument, and does not have a default\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
24 "implementation that defers to readinto().\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
25 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
26 "In addition, read(), readinto() and write() may raise\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
27 "BlockingIOError if the underlying raw stream is in non-blocking\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
28 "mode and not ready; unlike their raw counterparts, they will never\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
29 "return None.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
30 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
31 "A typical implementation should not inherit from a RawIOBase\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
32 "implementation, but wrap one.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
33 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
34
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
35 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
36 bufferediobase_readinto(PyObject *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
37 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
38 Py_buffer buf;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
39 Py_ssize_t len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
40 PyObject *data;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
41
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
42 if (!PyArg_ParseTuple(args, "w*:readinto", &buf)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
43 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
44 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
45
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
46 data = PyObject_CallMethod(self, "read", "n", buf.len);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
47 if (data == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
48 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
49
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
50 if (!PyBytes_Check(data)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
51 Py_DECREF(data);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
52 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
53 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
54 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
55
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
56 len = Py_SIZE(data);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
57 memcpy(buf.buf, PyBytes_AS_STRING(data), len);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
58
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
59 PyBuffer_Release(&buf);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
60 Py_DECREF(data);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
61
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
62 return PyLong_FromSsize_t(len);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
63
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
64 error:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
65 PyBuffer_Release(&buf);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
66 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
67 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
68
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
69 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
70 bufferediobase_unsupported(const char *message)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
71 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
72 PyErr_SetString(_PyIO_unsupported_operation, message);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
73 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
74 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
75
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
76 PyDoc_STRVAR(bufferediobase_detach_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
77 "Disconnect this buffer from its underlying raw stream and return it.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
78 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
79 "After the raw stream has been detached, the buffer is in an unusable\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
80 "state.\n");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
81
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
82 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
83 bufferediobase_detach(PyObject *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
84 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
85 return bufferediobase_unsupported("detach");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
86 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
87
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
88 PyDoc_STRVAR(bufferediobase_read_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
89 "Read and return up to n bytes.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
90 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
91 "If the argument is omitted, None, or negative, reads and\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
92 "returns all data until EOF.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
93 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
94 "If the argument is positive, and the underlying raw stream is\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
95 "not 'interactive', multiple raw reads may be issued to satisfy\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
96 "the byte count (unless EOF is reached first). But for\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
97 "interactive raw streams (as well as sockets and pipes), at most\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
98 "one raw read will be issued, and a short result does not imply\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
99 "that EOF is imminent.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
100 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
101 "Returns an empty bytes object on EOF.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
102 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
103 "Returns None if the underlying raw stream was open in non-blocking\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
104 "mode and no data is available at the moment.\n");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
105
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
106 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
107 bufferediobase_read(PyObject *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
108 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
109 return bufferediobase_unsupported("read");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
110 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
111
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
112 PyDoc_STRVAR(bufferediobase_read1_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
113 "Read and return up to n bytes, with at most one read() call\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
114 "to the underlying raw stream. A short result does not imply\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
115 "that EOF is imminent.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
116 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
117 "Returns an empty bytes object on EOF.\n");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
118
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
119 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
120 bufferediobase_read1(PyObject *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
121 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
122 return bufferediobase_unsupported("read1");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
123 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
124
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
125 PyDoc_STRVAR(bufferediobase_write_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
126 "Write the given buffer to the IO stream.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
127 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
128 "Returns the number of bytes written, which is never less than\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
129 "len(b).\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
130 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
131 "Raises BlockingIOError if the buffer is full and the\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
132 "underlying raw stream cannot accept more data at the moment.\n");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
133
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
134 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
135 bufferediobase_write(PyObject *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
136 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
137 return bufferediobase_unsupported("write");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
138 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
139
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
140
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
141 static PyMethodDef bufferediobase_methods[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
142 {"detach", (PyCFunction)bufferediobase_detach, METH_NOARGS, bufferediobase_detach_doc},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
143 {"read", bufferediobase_read, METH_VARARGS, bufferediobase_read_doc},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
144 {"read1", bufferediobase_read1, METH_VARARGS, bufferediobase_read1_doc},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
145 {"readinto", bufferediobase_readinto, METH_VARARGS, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
146 {"write", bufferediobase_write, METH_VARARGS, bufferediobase_write_doc},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
147 {NULL, NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
148 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
149
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
150 PyTypeObject PyBufferedIOBase_Type = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
151 PyVarObject_HEAD_INIT(NULL, 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
152 "_io._BufferedIOBase", /*tp_name*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
153 0, /*tp_basicsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
154 0, /*tp_itemsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
155 0, /*tp_dealloc*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
156 0, /*tp_print*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
157 0, /*tp_getattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
158 0, /*tp_setattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
159 0, /*tp_compare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
160 0, /*tp_repr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
161 0, /*tp_as_number*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
162 0, /*tp_as_sequence*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
163 0, /*tp_as_mapping*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
164 0, /*tp_hash */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
165 0, /*tp_call*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
166 0, /*tp_str*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
167 0, /*tp_getattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
168 0, /*tp_setattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
169 0, /*tp_as_buffer*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
170 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
171 bufferediobase_doc, /* tp_doc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
172 0, /* tp_traverse */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
173 0, /* tp_clear */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
174 0, /* tp_richcompare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
175 0, /* tp_weaklistoffset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
176 0, /* tp_iter */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
177 0, /* tp_iternext */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
178 bufferediobase_methods, /* tp_methods */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
179 0, /* tp_members */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
180 0, /* tp_getset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
181 &PyIOBase_Type, /* tp_base */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
182 0, /* tp_dict */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
183 0, /* tp_descr_get */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
184 0, /* tp_descr_set */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
185 0, /* tp_dictoffset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
186 0, /* tp_init */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
187 0, /* tp_alloc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
188 0, /* tp_new */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
189 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
190
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
191
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
192 typedef struct {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
193 PyObject_HEAD
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
194
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
195 PyObject *raw;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
196 int ok; /* Initialized? */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
197 int detached;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
198 int readable;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
199 int writable;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
200
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
201 /* True if this is a vanilla Buffered object (rather than a user derived
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
202 class) *and* the raw stream is a vanilla FileIO object. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
203 int fast_closed_checks;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
204
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
205 /* Absolute position inside the raw stream (-1 if unknown). */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
206 Py_off_t abs_pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
207
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
208 /* A static buffer of size `buffer_size` */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
209 char *buffer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
210 /* Current logical position in the buffer. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
211 Py_off_t pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
212 /* Position of the raw stream in the buffer. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
213 Py_off_t raw_pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
214
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
215 /* Just after the last buffered byte in the buffer, or -1 if the buffer
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
216 isn't ready for reading. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
217 Py_off_t read_end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
218
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
219 /* Just after the last byte actually written */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
220 Py_off_t write_pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
221 /* Just after the last byte waiting to be written, or -1 if the buffer
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
222 isn't ready for writing. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
223 Py_off_t write_end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
224
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
225 #ifdef WITH_THREAD
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
226 PyThread_type_lock lock;
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
227 volatile long owner;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
228 #endif
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
229
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
230 Py_ssize_t buffer_size;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
231 Py_ssize_t buffer_mask;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
232
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
233 PyObject *dict;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
234 PyObject *weakreflist;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
235 } buffered;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
236
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
237 /*
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
238 Implementation notes:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
239
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
240 * BufferedReader, BufferedWriter and BufferedRandom try to share most
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
241 methods (this is helped by the members `readable` and `writable`, which
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
242 are initialized in the respective constructors)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
243 * They also share a single buffer for reading and writing. This enables
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
244 interleaved reads and writes without flushing. It also makes the logic
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
245 a bit trickier to get right.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
246 * The absolute position of the raw stream is cached, if possible, in the
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
247 `abs_pos` member. It must be updated every time an operation is done
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
248 on the raw stream. If not sure, it can be reinitialized by calling
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
249 _buffered_raw_tell(), which queries the raw stream (_buffered_raw_seek()
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
250 also does it). To read it, use RAW_TELL().
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
251 * Three helpers, _bufferedreader_raw_read, _bufferedwriter_raw_write and
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
252 _bufferedwriter_flush_unlocked do a lot of useful housekeeping.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
253
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
254 NOTE: we should try to maintain block alignment of reads and writes to the
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
255 raw stream (according to the buffer size), but for now it is only done
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
256 in read() and friends.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
257
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
258 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
259
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
260 /* These macros protect the buffered object against concurrent operations. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
261
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
262 #ifdef WITH_THREAD
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
263
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
264 static int
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
265 _enter_buffered_busy(buffered *self)
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
266 {
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
267 if (self->owner == PyThread_get_thread_ident()) {
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
268 PyObject *r = PyObject_Repr((PyObject *) self);
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
269 if (r != NULL) {
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
270 PyErr_Format(PyExc_RuntimeError,
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
271 "reentrant call inside %s",
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
272 PyString_AS_STRING(r));
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
273 Py_DECREF(r);
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
274 }
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
275 return 0;
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
276 }
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
277 Py_BEGIN_ALLOW_THREADS
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
278 PyThread_acquire_lock(self->lock, 1);
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
279 Py_END_ALLOW_THREADS
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
280 return 1;
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
281 }
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
282
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
283 #define ENTER_BUFFERED(self) \
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
284 ( (PyThread_acquire_lock(self->lock, 0) ? \
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
285 1 : _enter_buffered_busy(self)) \
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
286 && (self->owner = PyThread_get_thread_ident(), 1) )
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
287
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
288 #define LEAVE_BUFFERED(self) \
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
289 do { \
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
290 self->owner = 0; \
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
291 PyThread_release_lock(self->lock); \
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
292 } while(0);
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
293
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
294 #else
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
295 #define ENTER_BUFFERED(self) 1
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
296 #define LEAVE_BUFFERED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
297 #endif
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
298
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
299 #define CHECK_INITIALIZED(self) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
300 if (self->ok <= 0) { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
301 if (self->detached) { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
302 PyErr_SetString(PyExc_ValueError, \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
303 "raw stream has been detached"); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
304 } else { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
305 PyErr_SetString(PyExc_ValueError, \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
306 "I/O operation on uninitialized object"); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
307 } \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
308 return NULL; \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
309 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
310
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
311 #define CHECK_INITIALIZED_INT(self) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
312 if (self->ok <= 0) { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
313 if (self->detached) { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
314 PyErr_SetString(PyExc_ValueError, \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
315 "raw stream has been detached"); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
316 } else { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
317 PyErr_SetString(PyExc_ValueError, \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
318 "I/O operation on uninitialized object"); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
319 } \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
320 return -1; \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
321 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
322
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
323 #define IS_CLOSED(self) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
324 (self->fast_closed_checks \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
325 ? _PyFileIO_closed(self->raw) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
326 : buffered_closed(self))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
327
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
328 #define CHECK_CLOSED(self, error_msg) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
329 if (IS_CLOSED(self)) { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
330 PyErr_SetString(PyExc_ValueError, error_msg); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
331 return NULL; \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
332 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
333
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
334
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
335 #define VALID_READ_BUFFER(self) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
336 (self->readable && self->read_end != -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
337
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
338 #define VALID_WRITE_BUFFER(self) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
339 (self->writable && self->write_end != -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
340
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
341 #define ADJUST_POSITION(self, _new_pos) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
342 do { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
343 self->pos = _new_pos; \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
344 if (VALID_READ_BUFFER(self) && self->read_end < self->pos) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
345 self->read_end = self->pos; \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
346 } while(0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
347
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
348 #define READAHEAD(self) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
349 ((self->readable && VALID_READ_BUFFER(self)) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
350 ? (self->read_end - self->pos) : 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
351
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
352 #define RAW_OFFSET(self) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
353 (((VALID_READ_BUFFER(self) || VALID_WRITE_BUFFER(self)) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
354 && self->raw_pos >= 0) ? self->raw_pos - self->pos : 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
355
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
356 #define RAW_TELL(self) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
357 (self->abs_pos != -1 ? self->abs_pos : _buffered_raw_tell(self))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
358
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
359 #define MINUS_LAST_BLOCK(self, size) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
360 (self->buffer_mask ? \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
361 (size & ~self->buffer_mask) : \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
362 (self->buffer_size * (size / self->buffer_size)))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
363
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
364
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
365 static void
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
366 buffered_dealloc(buffered *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
367 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
368 if (self->ok && _PyIOBase_finalize((PyObject *) self) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
369 return;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
370 _PyObject_GC_UNTRACK(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
371 self->ok = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
372 if (self->weakreflist != NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
373 PyObject_ClearWeakRefs((PyObject *)self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
374 Py_CLEAR(self->raw);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
375 if (self->buffer) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
376 PyMem_Free(self->buffer);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
377 self->buffer = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
378 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
379 #ifdef WITH_THREAD
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
380 if (self->lock) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
381 PyThread_free_lock(self->lock);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
382 self->lock = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
383 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
384 #endif
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
385 Py_CLEAR(self->dict);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
386 Py_TYPE(self)->tp_free((PyObject *)self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
387 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
388
78320
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
389 static PyObject *
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
390 buffered_sizeof(buffered *self, void *unused)
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
391 {
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
392 Py_ssize_t res;
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
393
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
394 res = sizeof(buffered);
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
395 if (self->buffer)
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
396 res += self->buffer_size;
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
397 return PyLong_FromSsize_t(res);
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
398 }
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
399
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
400 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
401 buffered_traverse(buffered *self, visitproc visit, void *arg)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
402 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
403 Py_VISIT(self->raw);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
404 Py_VISIT(self->dict);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
405 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
406 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
407
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
408 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
409 buffered_clear(buffered *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
410 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
411 if (self->ok && _PyIOBase_finalize((PyObject *) self) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
412 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
413 self->ok = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
414 Py_CLEAR(self->raw);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
415 Py_CLEAR(self->dict);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
416 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
417 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
418
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
419 /*
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
420 * _BufferedIOMixin methods
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
421 * This is not a class, just a collection of methods that will be reused
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
422 * by BufferedReader and BufferedWriter
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
423 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
424
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
425 /* Flush and close */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
426
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
427 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
428 buffered_simple_flush(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
429 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
430 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
431 return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_flush, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
432 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
433
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
434 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
435 buffered_closed(buffered *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
436 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
437 int closed;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
438 PyObject *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
439 CHECK_INITIALIZED_INT(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
440 res = PyObject_GetAttr(self->raw, _PyIO_str_closed);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
441 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
442 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
443 closed = PyObject_IsTrue(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
444 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
445 return closed;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
446 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
447
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
448 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
449 buffered_closed_get(buffered *self, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
450 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
451 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
452 return PyObject_GetAttr(self->raw, _PyIO_str_closed);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
453 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
454
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
455 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
456 buffered_close(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
457 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
458 PyObject *res = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
459 int r;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
460
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
461 CHECK_INITIALIZED(self)
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
462 if (!ENTER_BUFFERED(self))
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
463 return NULL;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
464
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
465 r = buffered_closed(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
466 if (r < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
467 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
468 if (r > 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
469 res = Py_None;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
470 Py_INCREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
471 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
472 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
473 /* flush() will most probably re-take the lock, so drop it first */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
474 LEAVE_BUFFERED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
475 res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
476 if (!ENTER_BUFFERED(self))
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
477 return NULL;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
478 if (res == NULL) {
61180
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 57703
diff changeset
479 goto end;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
480 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
481 Py_XDECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
482
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
483 res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_close, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
484
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
485 end:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
486 LEAVE_BUFFERED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
487 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
488 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
489
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
490 /* detach */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
491
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
492 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
493 buffered_detach(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
494 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
495 PyObject *raw, *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
496 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
497 res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
498 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
499 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
500 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
501 raw = self->raw;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
502 self->raw = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
503 self->detached = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
504 self->ok = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
505 return raw;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
506 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
507
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
508 /* Inquiries */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
509
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
510 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
511 buffered_seekable(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
512 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
513 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
514 return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_seekable, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
515 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
516
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
517 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
518 buffered_readable(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
519 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
520 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
521 return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_readable, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
522 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
523
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
524 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
525 buffered_writable(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
526 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
527 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
528 return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_writable, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
529 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
530
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
531 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
532 buffered_name_get(buffered *self, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
533 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
534 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
535 return PyObject_GetAttrString(self->raw, "name");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
536 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
537
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
538 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
539 buffered_mode_get(buffered *self, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
540 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
541 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
542 return PyObject_GetAttrString(self->raw, "mode");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
543 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
544
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
545 /* Lower-level APIs */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
546
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
547 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
548 buffered_fileno(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
549 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
550 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
551 return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_fileno, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
552 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
553
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
554 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
555 buffered_isatty(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
556 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
557 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
558 return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_isatty, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
559 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
560
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
561
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
562 /* Forward decls */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
563 static PyObject *
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
564 _bufferedwriter_flush_unlocked(buffered *);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
565 static Py_ssize_t
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
566 _bufferedreader_fill_buffer(buffered *self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
567 static void
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
568 _bufferedreader_reset_buf(buffered *self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
569 static void
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
570 _bufferedwriter_reset_buf(buffered *self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
571 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
572 _bufferedreader_peek_unlocked(buffered *self, Py_ssize_t);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
573 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
574 _bufferedreader_read_all(buffered *self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
575 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
576 _bufferedreader_read_fast(buffered *self, Py_ssize_t);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
577 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
578 _bufferedreader_read_generic(buffered *self, Py_ssize_t);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
579
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
580
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
581 /*
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
582 * Helpers
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
583 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
584
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
585 /* Sets the current error to BlockingIOError */
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
586 static void
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
587 _set_BlockingIOError(char *msg, Py_ssize_t written)
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
588 {
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
589 PyObject *err;
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
590 err = PyObject_CallFunction(PyExc_BlockingIOError, "isn",
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
591 errno, msg, written);
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
592 if (err)
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
593 PyErr_SetObject(PyExc_BlockingIOError, err);
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
594 Py_XDECREF(err);
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
595 }
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
596
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
597 /* Returns the address of the `written` member if a BlockingIOError was
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
598 raised, NULL otherwise. The error is always re-raised. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
599 static Py_ssize_t *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
600 _buffered_check_blocking_error(void)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
601 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
602 PyObject *t, *v, *tb;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
603 PyBlockingIOErrorObject *err;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
604
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
605 PyErr_Fetch(&t, &v, &tb);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
606 if (v == NULL || !PyErr_GivenExceptionMatches(v, PyExc_BlockingIOError)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
607 PyErr_Restore(t, v, tb);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
608 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
609 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
610 err = (PyBlockingIOErrorObject *) v;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
611 /* TODO: sanity check (err->written >= 0) */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
612 PyErr_Restore(t, v, tb);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
613 return &err->written;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
614 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
615
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
616 static Py_off_t
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
617 _buffered_raw_tell(buffered *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
618 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
619 Py_off_t n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
620 PyObject *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
621 res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_tell, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
622 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
623 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
624 n = PyNumber_AsOff_t(res, PyExc_ValueError);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
625 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
626 if (n < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
627 if (!PyErr_Occurred())
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
628 PyErr_Format(PyExc_IOError,
57441
2cd1fb104233 Issue #7228: Fix format mismatch when printing something of type off_t.
Mark Dickinson <dickinsm@gmail.com>
parents: 57023
diff changeset
629 "Raw stream returned invalid position %" PY_PRIdOFF,
2cd1fb104233 Issue #7228: Fix format mismatch when printing something of type off_t.
Mark Dickinson <dickinsm@gmail.com>
parents: 57023
diff changeset
630 (PY_OFF_T_COMPAT)n);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
631 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
632 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
633 self->abs_pos = n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
634 return n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
635 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
636
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
637 static Py_off_t
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
638 _buffered_raw_seek(buffered *self, Py_off_t target, int whence)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
639 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
640 PyObject *res, *posobj, *whenceobj;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
641 Py_off_t n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
642
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
643 posobj = PyLong_FromOff_t(target);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
644 if (posobj == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
645 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
646 whenceobj = PyLong_FromLong(whence);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
647 if (whenceobj == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
648 Py_DECREF(posobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
649 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
650 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
651 res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_seek,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
652 posobj, whenceobj, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
653 Py_DECREF(posobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
654 Py_DECREF(whenceobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
655 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
656 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
657 n = PyNumber_AsOff_t(res, PyExc_ValueError);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
658 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
659 if (n < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
660 if (!PyErr_Occurred())
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
661 PyErr_Format(PyExc_IOError,
57441
2cd1fb104233 Issue #7228: Fix format mismatch when printing something of type off_t.
Mark Dickinson <dickinsm@gmail.com>
parents: 57023
diff changeset
662 "Raw stream returned invalid position %" PY_PRIdOFF,
2cd1fb104233 Issue #7228: Fix format mismatch when printing something of type off_t.
Mark Dickinson <dickinsm@gmail.com>
parents: 57023
diff changeset
663 (PY_OFF_T_COMPAT)n);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
664 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
665 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
666 self->abs_pos = n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
667 return n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
668 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
669
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
670 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
671 _buffered_init(buffered *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
672 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
673 Py_ssize_t n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
674 if (self->buffer_size <= 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
675 PyErr_SetString(PyExc_ValueError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
676 "buffer size must be strictly positive");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
677 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
678 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
679 if (self->buffer)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
680 PyMem_Free(self->buffer);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
681 self->buffer = PyMem_Malloc(self->buffer_size);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
682 if (self->buffer == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
683 PyErr_NoMemory();
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
684 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
685 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
686 #ifdef WITH_THREAD
63428
0fe55c286edf Merged revisions 83411 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61180
diff changeset
687 if (self->lock)
0fe55c286edf Merged revisions 83411 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61180
diff changeset
688 PyThread_free_lock(self->lock);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
689 self->lock = PyThread_allocate_lock();
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
690 if (self->lock == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
691 PyErr_SetString(PyExc_RuntimeError, "can't allocate read lock");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
692 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
693 }
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
694 self->owner = 0;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
695 #endif
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
696 /* Find out whether buffer_size is a power of 2 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
697 /* XXX is this optimization useful? */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
698 for (n = self->buffer_size - 1; n & 1; n >>= 1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
699 ;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
700 if (n == 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
701 self->buffer_mask = self->buffer_size - 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
702 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
703 self->buffer_mask = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
704 if (_buffered_raw_tell(self) == -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
705 PyErr_Clear();
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
706 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
707 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
708
68028
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
709 /* Return 1 if an EnvironmentError with errno == EINTR is set (and then
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
710 clears the error indicator), 0 otherwise.
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
711 Should only be called when PyErr_Occurred() is true.
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
712 */
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
713 static int
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
714 _trap_eintr(void)
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
715 {
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
716 static PyObject *eintr_int = NULL;
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
717 PyObject *typ, *val, *tb;
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
718 PyEnvironmentErrorObject *env_err;
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
719
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
720 if (eintr_int == NULL) {
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
721 eintr_int = PyLong_FromLong(EINTR);
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
722 assert(eintr_int != NULL);
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
723 }
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
724 if (!PyErr_ExceptionMatches(PyExc_EnvironmentError))
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
725 return 0;
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
726 PyErr_Fetch(&typ, &val, &tb);
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
727 PyErr_NormalizeException(&typ, &val, &tb);
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
728 env_err = (PyEnvironmentErrorObject *) val;
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
729 assert(env_err != NULL);
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
730 if (env_err->myerrno != NULL &&
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
731 PyObject_RichCompareBool(env_err->myerrno, eintr_int, Py_EQ) > 0) {
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
732 Py_DECREF(typ);
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
733 Py_DECREF(val);
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
734 Py_XDECREF(tb);
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
735 return 1;
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
736 }
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
737 /* This silences any error set by PyObject_RichCompareBool() */
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
738 PyErr_Restore(typ, val, tb);
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
739 return 0;
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
740 }
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
741
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
742 /*
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
743 * Shared methods and wrappers
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
744 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
745
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
746 static PyObject *
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
747 buffered_flush_and_rewind_unlocked(buffered *self)
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
748 {
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
749 PyObject *res;
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
750
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
751 res = _bufferedwriter_flush_unlocked(self);
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
752 if (res == NULL)
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
753 return NULL;
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
754 Py_DECREF(res);
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
755
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
756 if (self->readable) {
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
757 /* Rewind the raw stream so that its position corresponds to
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
758 the current logical position. */
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
759 Py_off_t n;
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
760 n = _buffered_raw_seek(self, -RAW_OFFSET(self), 1);
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
761 _bufferedreader_reset_buf(self);
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
762 if (n == -1)
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
763 return NULL;
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
764 }
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
765 Py_RETURN_NONE;
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
766 }
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
767
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
768 static PyObject *
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
769 buffered_flush(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
770 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
771 PyObject *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
772
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
773 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
774 CHECK_CLOSED(self, "flush of closed file")
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
775
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
776 if (!ENTER_BUFFERED(self))
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
777 return NULL;
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
778 res = buffered_flush_and_rewind_unlocked(self);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
779 LEAVE_BUFFERED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
780
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
781 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
782 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
783
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
784 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
785 buffered_peek(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
786 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
787 Py_ssize_t n = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
788 PyObject *res = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
789
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
790 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
791 if (!PyArg_ParseTuple(args, "|n:peek", &n)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
792 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
793 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
794
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
795 if (!ENTER_BUFFERED(self))
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
796 return NULL;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
797
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
798 if (self->writable) {
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
799 res = buffered_flush_and_rewind_unlocked(self);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
800 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
801 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
802 Py_CLEAR(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
803 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
804 res = _bufferedreader_peek_unlocked(self, n);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
805
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
806 end:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
807 LEAVE_BUFFERED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
808 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
809 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
810
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
811 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
812 buffered_read(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
813 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
814 Py_ssize_t n = -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
815 PyObject *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
816
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
817 CHECK_INITIALIZED(self)
57703
54f84558ad2b accept None as the same as having passed no argument in file types #7349
Benjamin Peterson <benjamin@python.org>
parents: 57441
diff changeset
818 if (!PyArg_ParseTuple(args, "|O&:read", &_PyIO_ConvertSsize_t, &n)) {
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
819 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
820 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
821 if (n < -1) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
822 PyErr_SetString(PyExc_ValueError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
823 "read length must be positive or -1");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
824 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
825 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
826
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
827 CHECK_CLOSED(self, "read of closed file")
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
828
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
829 if (n == -1) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
830 /* The number of bytes is unspecified, read until the end of stream */
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
831 if (!ENTER_BUFFERED(self))
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
832 return NULL;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
833 res = _bufferedreader_read_all(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
834 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
835 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
836 res = _bufferedreader_read_fast(self, n);
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
837 if (res != Py_None)
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
838 return res;
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
839 Py_DECREF(res);
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
840 if (!ENTER_BUFFERED(self))
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
841 return NULL;
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
842 res = _bufferedreader_read_generic(self, n);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
843 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
844
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
845 LEAVE_BUFFERED(self)
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
846 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
847 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
848
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
849 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
850 buffered_read1(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
851 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
852 Py_ssize_t n, have, r;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
853 PyObject *res = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
854
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
855 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
856 if (!PyArg_ParseTuple(args, "n:read1", &n)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
857 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
858 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
859
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
860 if (n < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
861 PyErr_SetString(PyExc_ValueError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
862 "read length must be positive");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
863 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
864 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
865 if (n == 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
866 return PyBytes_FromStringAndSize(NULL, 0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
867
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
868 if (!ENTER_BUFFERED(self))
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
869 return NULL;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
870
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
871 /* Return up to n bytes. If at least one byte is buffered, we
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
872 only return buffered bytes. Otherwise, we do one raw read. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
873
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
874 /* XXX: this mimicks the io.py implementation but is probably wrong.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
875 If we need to read from the raw stream, then we could actually read
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
876 all `n` bytes asked by the caller (and possibly more, so as to fill
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
877 our buffer for the next reads). */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
878
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
879 have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
880 if (have > 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
881 if (n > have)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
882 n = have;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
883 res = PyBytes_FromStringAndSize(self->buffer + self->pos, n);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
884 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
885 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
886 self->pos += n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
887 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
888 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
889
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
890 if (self->writable) {
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
891 res = buffered_flush_and_rewind_unlocked(self);
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
892 if (res == NULL)
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
893 goto end;
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
894 Py_DECREF(res);
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
895 }
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
896
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
897 /* Fill the buffer from the raw stream, and copy it to the result. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
898 _bufferedreader_reset_buf(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
899 r = _bufferedreader_fill_buffer(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
900 if (r == -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
901 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
902 if (r == -2)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
903 r = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
904 if (n > r)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
905 n = r;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
906 res = PyBytes_FromStringAndSize(self->buffer, n);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
907 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
908 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
909 self->pos = n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
910
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
911 end:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
912 LEAVE_BUFFERED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
913 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
914 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
915
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
916 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
917 buffered_readinto(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
918 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
919 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
920
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
921 /* TODO: use raw.readinto() (or a direct copy from our buffer) instead! */
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
922 return bufferediobase_readinto((PyObject *)self, args);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
923 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
924
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
925 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
926 _buffered_readline(buffered *self, Py_ssize_t limit)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
927 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
928 PyObject *res = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
929 PyObject *chunks = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
930 Py_ssize_t n, written = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
931 const char *start, *s, *end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
932
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
933 CHECK_CLOSED(self, "readline of closed file")
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
934
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
935 /* First, try to find a line in the buffer. This can run unlocked because
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
936 the calls to the C API are simple enough that they can't trigger
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
937 any thread switch. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
938 n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
939 if (limit >= 0 && n > limit)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
940 n = limit;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
941 start = self->buffer + self->pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
942 s = memchr(start, '\n', n);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
943 if (s != NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
944 res = PyBytes_FromStringAndSize(start, s - start + 1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
945 if (res != NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
946 self->pos += s - start + 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
947 goto end_unlocked;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
948 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
949 if (n == limit) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
950 res = PyBytes_FromStringAndSize(start, n);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
951 if (res != NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
952 self->pos += n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
953 goto end_unlocked;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
954 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
955
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
956 if (!ENTER_BUFFERED(self))
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
957 goto end_unlocked;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
958
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
959 /* Now we try to get some more from the raw stream */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
960 chunks = PyList_New(0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
961 if (chunks == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
962 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
963 if (n > 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
964 res = PyBytes_FromStringAndSize(start, n);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
965 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
966 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
967 if (PyList_Append(chunks, res) < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
968 Py_CLEAR(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
969 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
970 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
971 Py_CLEAR(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
972 written += n;
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
973 self->pos += n;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
974 if (limit >= 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
975 limit -= n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
976 }
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
977 if (self->writable) {
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
978 PyObject *r = buffered_flush_and_rewind_unlocked(self);
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
979 if (r == NULL)
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
980 goto end;
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
981 Py_DECREF(r);
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
982 }
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
983
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
984 for (;;) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
985 _bufferedreader_reset_buf(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
986 n = _bufferedreader_fill_buffer(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
987 if (n == -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
988 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
989 if (n <= 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
990 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
991 if (limit >= 0 && n > limit)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
992 n = limit;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
993 start = self->buffer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
994 end = start + n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
995 s = start;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
996 while (s < end) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
997 if (*s++ == '\n') {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
998 res = PyBytes_FromStringAndSize(start, s - start);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
999 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1000 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1001 self->pos = s - start;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1002 goto found;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1003 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1004 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1005 res = PyBytes_FromStringAndSize(start, n);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1006 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1007 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1008 if (n == limit) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1009 self->pos = n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1010 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1011 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1012 if (PyList_Append(chunks, res) < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1013 Py_CLEAR(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1014 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1015 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1016 Py_CLEAR(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1017 written += n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1018 if (limit >= 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1019 limit -= n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1020 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1021 found:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1022 if (res != NULL && PyList_Append(chunks, res) < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1023 Py_CLEAR(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1024 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1025 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1026 Py_CLEAR(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1027 res = _PyBytes_Join(_PyIO_empty_bytes, chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1028
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1029 end:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1030 LEAVE_BUFFERED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1031 end_unlocked:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1032 Py_XDECREF(chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1033 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1034 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1035
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1036 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1037 buffered_readline(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1038 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1039 Py_ssize_t limit = -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1040
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1041 CHECK_INITIALIZED(self)
57703
54f84558ad2b accept None as the same as having passed no argument in file types #7349
Benjamin Peterson <benjamin@python.org>
parents: 57441
diff changeset
1042 if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit))
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1043 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1044 return _buffered_readline(self, limit);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1045 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1046
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1047
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1048 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1049 buffered_tell(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1050 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1051 Py_off_t pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1052
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1053 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1054 pos = _buffered_raw_tell(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1055 if (pos == -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1056 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1057 pos -= RAW_OFFSET(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1058 /* TODO: sanity check (pos >= 0) */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1059 return PyLong_FromOff_t(pos);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1060 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1061
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1062 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1063 buffered_seek(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1064 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1065 Py_off_t target, n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1066 int whence = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1067 PyObject *targetobj, *res = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1068
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1069 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1070 if (!PyArg_ParseTuple(args, "O|i:seek", &targetobj, &whence)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1071 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1072 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1073 if (whence < 0 || whence > 2) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1074 PyErr_Format(PyExc_ValueError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1075 "whence must be between 0 and 2, not %d", whence);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1076 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1077 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1078
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1079 CHECK_CLOSED(self, "seek of closed file")
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1080
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1081 target = PyNumber_AsOff_t(targetobj, PyExc_ValueError);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1082 if (target == -1 && PyErr_Occurred())
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1083 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1084
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1085 if (whence != 2 && self->readable) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1086 Py_off_t current, avail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1087 /* Check if seeking leaves us inside the current buffer,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1088 so as to return quickly if possible. Also, we needn't take the
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1089 lock in this fast path.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1090 Don't know how to do that when whence == 2, though. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1091 /* NOTE: RAW_TELL() can release the GIL but the object is in a stable
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1092 state at this point. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1093 current = RAW_TELL(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1094 avail = READAHEAD(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1095 if (avail > 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1096 Py_off_t offset;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1097 if (whence == 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1098 offset = target - (current - RAW_OFFSET(self));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1099 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1100 offset = target;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1101 if (offset >= -self->pos && offset <= avail) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1102 self->pos += offset;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1103 return PyLong_FromOff_t(current - avail + offset);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1104 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1105 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1106 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1107
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
1108 if (!ENTER_BUFFERED(self))
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
1109 return NULL;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1110
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1111 /* Fallback: invoke raw seek() method and clear buffer */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1112 if (self->writable) {
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1113 res = _bufferedwriter_flush_unlocked(self);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1114 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1115 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1116 Py_CLEAR(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1117 _bufferedwriter_reset_buf(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1118 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1119
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1120 /* TODO: align on block boundary and read buffer if needed? */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1121 if (whence == 1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1122 target -= RAW_OFFSET(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1123 n = _buffered_raw_seek(self, target, whence);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1124 if (n == -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1125 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1126 self->raw_pos = -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1127 res = PyLong_FromOff_t(n);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1128 if (res != NULL && self->readable)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1129 _bufferedreader_reset_buf(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1130
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1131 end:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1132 LEAVE_BUFFERED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1133 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1134 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1135
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1136 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1137 buffered_truncate(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1138 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1139 PyObject *pos = Py_None;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1140 PyObject *res = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1141
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1142 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1143 if (!PyArg_ParseTuple(args, "|O:truncate", &pos)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1144 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1145 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1146
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
1147 if (!ENTER_BUFFERED(self))
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
1148 return NULL;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1149
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1150 if (self->writable) {
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
1151 res = buffered_flush_and_rewind_unlocked(self);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1152 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1153 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1154 Py_CLEAR(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1155 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1156 res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_truncate, pos, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1157 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1158 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1159 /* Reset cached position */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1160 if (_buffered_raw_tell(self) == -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1161 PyErr_Clear();
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1162
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1163 end:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1164 LEAVE_BUFFERED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1165 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1166 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1167
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1168 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1169 buffered_iternext(buffered *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1170 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1171 PyObject *line;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1172 PyTypeObject *tp;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1173
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1174 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1175
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1176 tp = Py_TYPE(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1177 if (tp == &PyBufferedReader_Type ||
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1178 tp == &PyBufferedRandom_Type) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1179 /* Skip method call overhead for speed */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1180 line = _buffered_readline(self, -1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1181 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1182 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1183 line = PyObject_CallMethodObjArgs((PyObject *)self,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1184 _PyIO_str_readline, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1185 if (line && !PyBytes_Check(line)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1186 PyErr_Format(PyExc_IOError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1187 "readline() should have returned a bytes object, "
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1188 "not '%.200s'", Py_TYPE(line)->tp_name);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1189 Py_DECREF(line);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1190 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1191 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1192 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1193
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1194 if (line == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1195 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1196
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1197 if (PyBytes_GET_SIZE(line) == 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1198 /* Reached EOF or would have blocked */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1199 Py_DECREF(line);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1200 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1201 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1202
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1203 return line;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1204 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1205
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1206 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1207 buffered_repr(buffered *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1208 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1209 PyObject *nameobj, *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1210
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1211 nameobj = PyObject_GetAttrString((PyObject *) self, "name");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1212 if (nameobj == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1213 if (PyErr_ExceptionMatches(PyExc_AttributeError))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1214 PyErr_Clear();
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1215 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1216 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1217 res = PyString_FromFormat("<%s>", Py_TYPE(self)->tp_name);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1218 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1219 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1220 PyObject *repr = PyObject_Repr(nameobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1221 Py_DECREF(nameobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1222 if (repr == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1223 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1224 res = PyString_FromFormat("<%s name=%s>",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1225 Py_TYPE(self)->tp_name,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1226 PyString_AS_STRING(repr));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1227 Py_DECREF(repr);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1228 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1229 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1230 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1231
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1232 /*
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1233 * class BufferedReader
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1234 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1235
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1236 PyDoc_STRVAR(bufferedreader_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1237 "Create a new buffered reader using the given readable raw IO object.");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1238
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1239 static void _bufferedreader_reset_buf(buffered *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1240 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1241 self->read_end = -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1242 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1243
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1244 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1245 bufferedreader_init(buffered *self, PyObject *args, PyObject *kwds)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1246 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1247 char *kwlist[] = {"raw", "buffer_size", NULL};
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1248 Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1249 PyObject *raw;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1250
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1251 self->ok = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1252 self->detached = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1253
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1254 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:BufferedReader", kwlist,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1255 &raw, &buffer_size)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1256 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1257 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1258
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1259 if (_PyIOBase_check_readable(raw, Py_True) == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1260 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1261
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1262 Py_CLEAR(self->raw);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1263 Py_INCREF(raw);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1264 self->raw = raw;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1265 self->buffer_size = buffer_size;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1266 self->readable = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1267 self->writable = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1268
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1269 if (_buffered_init(self) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1270 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1271 _bufferedreader_reset_buf(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1272
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1273 self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedReader_Type &&
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1274 Py_TYPE(raw) == &PyFileIO_Type);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1275
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1276 self->ok = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1277 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1278 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1279
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1280 static Py_ssize_t
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1281 _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1282 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1283 Py_buffer buf;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1284 PyObject *memobj, *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1285 Py_ssize_t n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1286 /* NOTE: the buffer needn't be released as its object is NULL. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1287 if (PyBuffer_FillInfo(&buf, NULL, start, len, 0, PyBUF_CONTIG) == -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1288 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1289 memobj = PyMemoryView_FromBuffer(&buf);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1290 if (memobj == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1291 return -1;
68028
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1292 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1293 occurs so we needn't do it ourselves.
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1294 We then retry reading, ignoring the signal if no handler has
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1295 raised (see issue #10956).
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1296 */
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1297 do {
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1298 res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_readinto, memobj, NULL);
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1299 } while (res == NULL && _trap_eintr());
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1300 Py_DECREF(memobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1301 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1302 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1303 if (res == Py_None) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1304 /* Non-blocking stream would have blocked. Special return code! */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1305 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1306 return -2;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1307 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1308 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1309 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1310 if (n < 0 || n > len) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1311 PyErr_Format(PyExc_IOError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1312 "raw readinto() returned invalid length %zd "
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1313 "(should have been between 0 and %zd)", n, len);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1314 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1315 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1316 if (n > 0 && self->abs_pos != -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1317 self->abs_pos += n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1318 return n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1319 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1320
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1321 static Py_ssize_t
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1322 _bufferedreader_fill_buffer(buffered *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1323 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1324 Py_ssize_t start, len, n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1325 if (VALID_READ_BUFFER(self))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1326 start = Py_SAFE_DOWNCAST(self->read_end, Py_off_t, Py_ssize_t);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1327 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1328 start = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1329 len = self->buffer_size - start;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1330 n = _bufferedreader_raw_read(self, self->buffer + start, len);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1331 if (n <= 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1332 return n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1333 self->read_end = start + n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1334 self->raw_pos = start + n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1335 return n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1336 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1337
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1338 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1339 _bufferedreader_read_all(buffered *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1340 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1341 Py_ssize_t current_size;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1342 PyObject *res, *data = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1343 PyObject *chunks = PyList_New(0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1344
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1345 if (chunks == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1346 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1347
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1348 /* First copy what we have in the current buffer. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1349 current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1350 if (current_size) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1351 data = PyBytes_FromStringAndSize(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1352 self->buffer + self->pos, current_size);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1353 if (data == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1354 Py_DECREF(chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1355 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1356 }
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
1357 self->pos += current_size;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1358 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1359 /* We're going past the buffer's bounds, flush it */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1360 if (self->writable) {
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
1361 res = buffered_flush_and_rewind_unlocked(self);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1362 if (res == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1363 Py_DECREF(chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1364 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1365 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1366 Py_CLEAR(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1367 }
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
1368 _bufferedreader_reset_buf(self);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1369 while (1) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1370 if (data) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1371 if (PyList_Append(chunks, data) < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1372 Py_DECREF(data);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1373 Py_DECREF(chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1374 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1375 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1376 Py_DECREF(data);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1377 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1378
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1379 /* Read until EOF or until read() would block. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1380 data = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_read, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1381 if (data == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1382 Py_DECREF(chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1383 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1384 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1385 if (data != Py_None && !PyBytes_Check(data)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1386 Py_DECREF(data);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1387 Py_DECREF(chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1388 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1389 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1390 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1391 if (data == Py_None || PyBytes_GET_SIZE(data) == 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1392 if (current_size == 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1393 Py_DECREF(chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1394 return data;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1395 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1396 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1397 res = _PyBytes_Join(_PyIO_empty_bytes, chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1398 Py_DECREF(data);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1399 Py_DECREF(chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1400 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1401 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1402 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1403 current_size += PyBytes_GET_SIZE(data);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1404 if (self->abs_pos != -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1405 self->abs_pos += PyBytes_GET_SIZE(data);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1406 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1407 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1408
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1409 /* Read n bytes from the buffer if it can, otherwise return None.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1410 This function is simple enough that it can run unlocked. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1411 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1412 _bufferedreader_read_fast(buffered *self, Py_ssize_t n)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1413 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1414 Py_ssize_t current_size;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1415
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1416 current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1417 if (n <= current_size) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1418 /* Fast path: the data to read is fully buffered. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1419 PyObject *res = PyBytes_FromStringAndSize(self->buffer + self->pos, n);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1420 if (res != NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1421 self->pos += n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1422 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1423 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1424 Py_RETURN_NONE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1425 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1426
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1427 /* Generic read function: read from the stream until enough bytes are read,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1428 * or until an EOF occurs or until read() would block.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1429 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1430 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1431 _bufferedreader_read_generic(buffered *self, Py_ssize_t n)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1432 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1433 PyObject *res = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1434 Py_ssize_t current_size, remaining, written;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1435 char *out;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1436
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1437 current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1438 if (n <= current_size)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1439 return _bufferedreader_read_fast(self, n);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1440
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1441 res = PyBytes_FromStringAndSize(NULL, n);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1442 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1443 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1444 out = PyBytes_AS_STRING(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1445 remaining = n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1446 written = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1447 if (current_size > 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1448 memcpy(out, self->buffer + self->pos, current_size);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1449 remaining -= current_size;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1450 written += current_size;
71984
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
1451 self->pos += current_size;
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
1452 }
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
1453 /* Flush the write buffer if necessary */
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
1454 if (self->writable) {
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
1455 PyObject *r = buffered_flush_and_rewind_unlocked(self);
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
1456 if (r == NULL)
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
1457 goto error;
cf2010e9f941 Issue #12213: Fix a buffering bug with interleaved reads and writes that
Antoine Pitrou <solipsis@pitrou.net>
parents: 70060
diff changeset
1458 Py_DECREF(r);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1459 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1460 _bufferedreader_reset_buf(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1461 while (remaining > 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1462 /* We want to read a whole block at the end into buffer.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1463 If we had readv() we could do this in one pass. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1464 Py_ssize_t r = MINUS_LAST_BLOCK(self, remaining);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1465 if (r == 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1466 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1467 r = _bufferedreader_raw_read(self, out + written, r);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1468 if (r == -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1469 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1470 if (r == 0 || r == -2) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1471 /* EOF occurred or read() would block. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1472 if (r == 0 || written > 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1473 if (_PyBytes_Resize(&res, written))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1474 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1475 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1476 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1477 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1478 Py_INCREF(Py_None);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1479 return Py_None;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1480 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1481 remaining -= r;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1482 written += r;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1483 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1484 assert(remaining <= self->buffer_size);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1485 self->pos = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1486 self->raw_pos = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1487 self->read_end = 0;
63921
c3ff6e910068 Merged revisions 83944 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63428
diff changeset
1488 /* NOTE: when the read is satisfied, we avoid issuing any additional
c3ff6e910068 Merged revisions 83944 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63428
diff changeset
1489 reads, which could block indefinitely (e.g. on a socket).
c3ff6e910068 Merged revisions 83944 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63428
diff changeset
1490 See issue #9550. */
c3ff6e910068 Merged revisions 83944 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63428
diff changeset
1491 while (remaining > 0 && self->read_end < self->buffer_size) {
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1492 Py_ssize_t r = _bufferedreader_fill_buffer(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1493 if (r == -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1494 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1495 if (r == 0 || r == -2) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1496 /* EOF occurred or read() would block. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1497 if (r == 0 || written > 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1498 if (_PyBytes_Resize(&res, written))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1499 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1500 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1501 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1502 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1503 Py_INCREF(Py_None);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1504 return Py_None;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1505 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1506 if (remaining > r) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1507 memcpy(out + written, self->buffer + self->pos, r);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1508 written += r;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1509 self->pos += r;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1510 remaining -= r;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1511 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1512 else if (remaining > 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1513 memcpy(out + written, self->buffer + self->pos, remaining);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1514 written += remaining;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1515 self->pos += remaining;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1516 remaining = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1517 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1518 if (remaining == 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1519 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1520 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1521
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1522 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1523
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1524 error:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1525 Py_XDECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1526 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1527 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1528
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1529 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1530 _bufferedreader_peek_unlocked(buffered *self, Py_ssize_t n)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1531 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1532 Py_ssize_t have, r;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1533
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1534 have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1535 /* Constraints:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1536 1. we don't want to advance the file position.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1537 2. we don't want to lose block alignment, so we can't shift the buffer
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1538 to make some place.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1539 Therefore, we either return `have` bytes (if > 0), or a full buffer.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1540 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1541 if (have > 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1542 return PyBytes_FromStringAndSize(self->buffer + self->pos, have);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1543 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1544
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1545 /* Fill the buffer from the raw stream, and copy it to the result. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1546 _bufferedreader_reset_buf(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1547 r = _bufferedreader_fill_buffer(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1548 if (r == -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1549 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1550 if (r == -2)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1551 r = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1552 self->pos = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1553 return PyBytes_FromStringAndSize(self->buffer, r);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1554 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1555
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1556 static PyMethodDef bufferedreader_methods[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1557 /* BufferedIOMixin methods */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1558 {"detach", (PyCFunction)buffered_detach, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1559 {"flush", (PyCFunction)buffered_simple_flush, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1560 {"close", (PyCFunction)buffered_close, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1561 {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1562 {"readable", (PyCFunction)buffered_readable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1563 {"writable", (PyCFunction)buffered_writable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1564 {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1565 {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1566
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1567 {"read", (PyCFunction)buffered_read, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1568 {"peek", (PyCFunction)buffered_peek, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1569 {"read1", (PyCFunction)buffered_read1, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1570 {"readline", (PyCFunction)buffered_readline, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1571 {"seek", (PyCFunction)buffered_seek, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1572 {"tell", (PyCFunction)buffered_tell, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1573 {"truncate", (PyCFunction)buffered_truncate, METH_VARARGS},
78320
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
1574 {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS},
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1575 {NULL, NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1576 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1577
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1578 static PyMemberDef bufferedreader_members[] = {
67075
595f258806ef Merged revisions 87427 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66660
diff changeset
1579 {"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1580 {NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1581 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1582
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1583 static PyGetSetDef bufferedreader_getset[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1584 {"closed", (getter)buffered_closed_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1585 {"name", (getter)buffered_name_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1586 {"mode", (getter)buffered_mode_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1587 {NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1588 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1589
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1590
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1591 PyTypeObject PyBufferedReader_Type = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1592 PyVarObject_HEAD_INIT(NULL, 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1593 "_io.BufferedReader", /*tp_name*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1594 sizeof(buffered), /*tp_basicsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1595 0, /*tp_itemsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1596 (destructor)buffered_dealloc, /*tp_dealloc*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1597 0, /*tp_print*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1598 0, /*tp_getattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1599 0, /*tp_setattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1600 0, /*tp_compare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1601 (reprfunc)buffered_repr, /*tp_repr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1602 0, /*tp_as_number*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1603 0, /*tp_as_sequence*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1604 0, /*tp_as_mapping*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1605 0, /*tp_hash */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1606 0, /*tp_call*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1607 0, /*tp_str*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1608 0, /*tp_getattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1609 0, /*tp_setattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1610 0, /*tp_as_buffer*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1611 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1612 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1613 bufferedreader_doc, /* tp_doc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1614 (traverseproc)buffered_traverse, /* tp_traverse */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1615 (inquiry)buffered_clear, /* tp_clear */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1616 0, /* tp_richcompare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1617 offsetof(buffered, weakreflist), /*tp_weaklistoffset*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1618 0, /* tp_iter */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1619 (iternextfunc)buffered_iternext, /* tp_iternext */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1620 bufferedreader_methods, /* tp_methods */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1621 bufferedreader_members, /* tp_members */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1622 bufferedreader_getset, /* tp_getset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1623 0, /* tp_base */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1624 0, /* tp_dict */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1625 0, /* tp_descr_get */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1626 0, /* tp_descr_set */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1627 offsetof(buffered, dict), /* tp_dictoffset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1628 (initproc)bufferedreader_init, /* tp_init */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1629 0, /* tp_alloc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1630 PyType_GenericNew, /* tp_new */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1631 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1632
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1633
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1634
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1635 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1636 complain_about_max_buffer_size(void)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1637 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1638 if (PyErr_WarnEx(PyExc_DeprecationWarning,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1639 "max_buffer_size is deprecated", 1) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1640 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1641 return 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1642 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1643
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1644 /*
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1645 * class BufferedWriter
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1646 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1647 PyDoc_STRVAR(bufferedwriter_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1648 "A buffer for a writeable sequential RawIO object.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1649 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1650 "The constructor creates a BufferedWriter for the given writeable raw\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1651 "stream. If the buffer_size is not given, it defaults to\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1652 "DEFAULT_BUFFER_SIZE. max_buffer_size isn't used anymore.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1653 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1654
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1655 static void
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1656 _bufferedwriter_reset_buf(buffered *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1657 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1658 self->write_pos = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1659 self->write_end = -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1660 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1661
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1662 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1663 bufferedwriter_init(buffered *self, PyObject *args, PyObject *kwds)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1664 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1665 /* TODO: properly deprecate max_buffer_size */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1666 char *kwlist[] = {"raw", "buffer_size", "max_buffer_size", NULL};
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1667 Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1668 Py_ssize_t max_buffer_size = -234;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1669 PyObject *raw;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1670
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1671 self->ok = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1672 self->detached = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1673
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1674 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|nn:BufferedReader", kwlist,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1675 &raw, &buffer_size, &max_buffer_size)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1676 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1677 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1678
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1679 if (max_buffer_size != -234 && !complain_about_max_buffer_size())
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1680 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1681
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1682 if (_PyIOBase_check_writable(raw, Py_True) == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1683 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1684
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1685 Py_CLEAR(self->raw);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1686 Py_INCREF(raw);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1687 self->raw = raw;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1688 self->readable = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1689 self->writable = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1690
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1691 self->buffer_size = buffer_size;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1692 if (_buffered_init(self) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1693 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1694 _bufferedwriter_reset_buf(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1695 self->pos = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1696
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1697 self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedWriter_Type &&
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1698 Py_TYPE(raw) == &PyFileIO_Type);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1699
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1700 self->ok = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1701 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1702 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1703
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1704 static Py_ssize_t
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1705 _bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1706 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1707 Py_buffer buf;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1708 PyObject *memobj, *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1709 Py_ssize_t n;
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1710 int errnum;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1711 /* NOTE: the buffer needn't be released as its object is NULL. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1712 if (PyBuffer_FillInfo(&buf, NULL, start, len, 1, PyBUF_CONTIG_RO) == -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1713 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1714 memobj = PyMemoryView_FromBuffer(&buf);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1715 if (memobj == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1716 return -1;
68028
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1717 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1718 occurs so we needn't do it ourselves.
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1719 We then retry writing, ignoring the signal if no handler has
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1720 raised (see issue #10956).
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1721 */
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1722 do {
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1723 errno = 0;
68028
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1724 res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_write, memobj, NULL);
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1725 errnum = errno;
68028
675b48c4c88a Merged revisions 88610 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67075
diff changeset
1726 } while (res == NULL && _trap_eintr());
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1727 Py_DECREF(memobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1728 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1729 return -1;
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1730 if (res == Py_None) {
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1731 /* Non-blocking stream would have blocked. Special return code!
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1732 Being paranoid we reset errno in case it is changed by code
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1733 triggered by a decref. errno is used by _set_BlockingIOError(). */
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1734 Py_DECREF(res);
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1735 errno = errnum;
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1736 return -2;
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1737 }
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1738 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1739 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1740 if (n < 0 || n > len) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1741 PyErr_Format(PyExc_IOError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1742 "raw write() returned invalid length %zd "
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1743 "(should have been between 0 and %zd)", n, len);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1744 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1745 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1746 if (n > 0 && self->abs_pos != -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1747 self->abs_pos += n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1748 return n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1749 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1750
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1751 /* `restore_pos` is 1 if we need to restore the raw stream position at
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1752 the end, 0 otherwise. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1753 static PyObject *
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1754 _bufferedwriter_flush_unlocked(buffered *self)
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1755 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1756 Py_ssize_t written = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1757 Py_off_t n, rewind;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1758
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1759 if (!VALID_WRITE_BUFFER(self) || self->write_pos == self->write_end)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1760 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1761 /* First, rewind */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1762 rewind = RAW_OFFSET(self) + (self->pos - self->write_pos);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1763 if (rewind != 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1764 n = _buffered_raw_seek(self, -rewind, 1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1765 if (n < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1766 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1767 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1768 self->raw_pos -= rewind;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1769 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1770 while (self->write_pos < self->write_end) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1771 n = _bufferedwriter_raw_write(self,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1772 self->buffer + self->write_pos,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1773 Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1774 Py_off_t, Py_ssize_t));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1775 if (n == -1) {
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1776 goto error;
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1777 }
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1778 else if (n == -2) {
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1779 _set_BlockingIOError("write could not complete without blocking",
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1780 0);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1781 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1782 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1783 self->write_pos += n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1784 self->raw_pos = self->write_pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1785 written += Py_SAFE_DOWNCAST(n, Py_off_t, Py_ssize_t);
64209
dfd0229748ef Merged revisions 84239 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63921
diff changeset
1786 /* Partial writes can return successfully when interrupted by a
dfd0229748ef Merged revisions 84239 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63921
diff changeset
1787 signal (see write(2)). We must run signal handlers before
dfd0229748ef Merged revisions 84239 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63921
diff changeset
1788 blocking another time, possibly indefinitely. */
dfd0229748ef Merged revisions 84239 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63921
diff changeset
1789 if (PyErr_CheckSignals() < 0)
dfd0229748ef Merged revisions 84239 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63921
diff changeset
1790 goto error;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1791 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1792
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1793 _bufferedwriter_reset_buf(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1794
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1795 end:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1796 Py_RETURN_NONE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1797
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1798 error:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1799 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1800 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1801
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1802 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1803 bufferedwriter_write(buffered *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1804 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1805 PyObject *res = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1806 Py_buffer buf;
56392
b4d1be7102b3 Fix compilation warning on Windows, where size_t is 32bit but file offsets are 64bit.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55564
diff changeset
1807 Py_ssize_t written, avail, remaining;
b4d1be7102b3 Fix compilation warning on Windows, where size_t is 32bit but file offsets are 64bit.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55564
diff changeset
1808 Py_off_t offset;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1809
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1810 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1811 if (!PyArg_ParseTuple(args, "s*:write", &buf)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1812 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1813 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1814
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1815 if (IS_CLOSED(self)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1816 PyErr_SetString(PyExc_ValueError, "write to closed file");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1817 PyBuffer_Release(&buf);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1818 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1819 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1820
66660
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
1821 if (!ENTER_BUFFERED(self)) {
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
1822 PyBuffer_Release(&buf);
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
1823 return NULL;
5a723b7aefb7 Merged revisions 86981,86984 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64209
diff changeset
1824 }
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1825
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1826 /* Fast path: the data to write can be fully buffered. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1827 if (!VALID_READ_BUFFER(self) && !VALID_WRITE_BUFFER(self)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1828 self->pos = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1829 self->raw_pos = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1830 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1831 avail = Py_SAFE_DOWNCAST(self->buffer_size - self->pos, Py_off_t, Py_ssize_t);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1832 if (buf.len <= avail) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1833 memcpy(self->buffer + self->pos, buf.buf, buf.len);
70060
0d24d4c537a6 Issue #12062: In the `io` module, fix a flushing bug when doing a certain
Antoine Pitrou <solipsis@pitrou.net>
parents: 68028
diff changeset
1834 if (!VALID_WRITE_BUFFER(self) || self->write_pos > self->pos) {
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1835 self->write_pos = self->pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1836 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1837 ADJUST_POSITION(self, self->pos + buf.len);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1838 if (self->pos > self->write_end)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1839 self->write_end = self->pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1840 written = buf.len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1841 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1842 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1843
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1844 /* First write the current buffer */
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1845 res = _bufferedwriter_flush_unlocked(self);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1846 if (res == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1847 Py_ssize_t *w = _buffered_check_blocking_error();
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1848 if (w == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1849 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1850 if (self->readable)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1851 _bufferedreader_reset_buf(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1852 /* Make some place by shifting the buffer. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1853 assert(VALID_WRITE_BUFFER(self));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1854 memmove(self->buffer, self->buffer + self->write_pos,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1855 Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1856 Py_off_t, Py_ssize_t));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1857 self->write_end -= self->write_pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1858 self->raw_pos -= self->write_pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1859 self->pos -= self->write_pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1860 self->write_pos = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1861 avail = Py_SAFE_DOWNCAST(self->buffer_size - self->write_end,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1862 Py_off_t, Py_ssize_t);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1863 if (buf.len <= avail) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1864 /* Everything can be buffered */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1865 PyErr_Clear();
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1866 memcpy(self->buffer + self->write_end, buf.buf, buf.len);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1867 self->write_end += buf.len;
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1868 self->pos += buf.len;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1869 written = buf.len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1870 goto end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1871 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1872 /* Buffer as much as possible. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1873 memcpy(self->buffer + self->write_end, buf.buf, avail);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1874 self->write_end += avail;
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1875 self->pos += avail;
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1876 /* XXX Modifying the existing exception e using the pointer w
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1877 will change e.characters_written but not e.args[2].
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1878 Therefore we just replace with a new error. */
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1879 _set_BlockingIOError("write could not complete without blocking",
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1880 avail);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1881 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1882 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1883 Py_CLEAR(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1884
55564
2f641e337c9f Issue #6629: Fix a data corruption issue in the new `io` package, which could
Antoine Pitrou <solipsis@pitrou.net>
parents: 54717
diff changeset
1885 /* Adjust the raw stream position if it is away from the logical stream
2f641e337c9f Issue #6629: Fix a data corruption issue in the new `io` package, which could
Antoine Pitrou <solipsis@pitrou.net>
parents: 54717
diff changeset
1886 position. This happens if the read buffer has been filled but not
2f641e337c9f Issue #6629: Fix a data corruption issue in the new `io` package, which could
Antoine Pitrou <solipsis@pitrou.net>
parents: 54717
diff changeset
1887 modified (and therefore _bufferedwriter_flush_unlocked() didn't rewind
2f641e337c9f Issue #6629: Fix a data corruption issue in the new `io` package, which could
Antoine Pitrou <solipsis@pitrou.net>
parents: 54717
diff changeset
1888 the raw stream by itself).
2f641e337c9f Issue #6629: Fix a data corruption issue in the new `io` package, which could
Antoine Pitrou <solipsis@pitrou.net>
parents: 54717
diff changeset
1889 Fixes issue #6629.
2f641e337c9f Issue #6629: Fix a data corruption issue in the new `io` package, which could
Antoine Pitrou <solipsis@pitrou.net>
parents: 54717
diff changeset
1890 */
56392
b4d1be7102b3 Fix compilation warning on Windows, where size_t is 32bit but file offsets are 64bit.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55564
diff changeset
1891 offset = RAW_OFFSET(self);
b4d1be7102b3 Fix compilation warning on Windows, where size_t is 32bit but file offsets are 64bit.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55564
diff changeset
1892 if (offset != 0) {
b4d1be7102b3 Fix compilation warning on Windows, where size_t is 32bit but file offsets are 64bit.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55564
diff changeset
1893 if (_buffered_raw_seek(self, -offset, 1) < 0)
55564
2f641e337c9f Issue #6629: Fix a data corruption issue in the new `io` package, which could
Antoine Pitrou <solipsis@pitrou.net>
parents: 54717
diff changeset
1894 goto error;
56392
b4d1be7102b3 Fix compilation warning on Windows, where size_t is 32bit but file offsets are 64bit.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55564
diff changeset
1895 self->raw_pos -= offset;
55564
2f641e337c9f Issue #6629: Fix a data corruption issue in the new `io` package, which could
Antoine Pitrou <solipsis@pitrou.net>
parents: 54717
diff changeset
1896 }
2f641e337c9f Issue #6629: Fix a data corruption issue in the new `io` package, which could
Antoine Pitrou <solipsis@pitrou.net>
parents: 54717
diff changeset
1897
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1898 /* Then write buf itself. At this point the buffer has been emptied. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1899 remaining = buf.len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1900 written = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1901 while (remaining > self->buffer_size) {
56392
b4d1be7102b3 Fix compilation warning on Windows, where size_t is 32bit but file offsets are 64bit.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55564
diff changeset
1902 Py_ssize_t n = _bufferedwriter_raw_write(
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1903 self, (char *) buf.buf + written, buf.len - written);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1904 if (n == -1) {
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1905 goto error;
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1906 } else if (n == -2) {
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1907 /* Write failed because raw file is non-blocking */
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1908 if (remaining > self->buffer_size) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1909 /* Can't buffer everything, still buffer as much as possible */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1910 memcpy(self->buffer,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1911 (char *) buf.buf + written, self->buffer_size);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1912 self->raw_pos = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1913 ADJUST_POSITION(self, self->buffer_size);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1914 self->write_end = self->buffer_size;
73667
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1915 written += self->buffer_size;
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1916 _set_BlockingIOError("write could not complete without "
e84e17643eeb Issue #13322: Fix BufferedWriter.write() to ensure that BlockingIOError is
Antoine Pitrou <solipsis@pitrou.net>
parents: 72754
diff changeset
1917 "blocking", written);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1918 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1919 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1920 PyErr_Clear();
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1921 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1922 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1923 written += n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1924 remaining -= n;
64209
dfd0229748ef Merged revisions 84239 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63921
diff changeset
1925 /* Partial writes can return successfully when interrupted by a
dfd0229748ef Merged revisions 84239 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63921
diff changeset
1926 signal (see write(2)). We must run signal handlers before
dfd0229748ef Merged revisions 84239 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63921
diff changeset
1927 blocking another time, possibly indefinitely. */
dfd0229748ef Merged revisions 84239 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63921
diff changeset
1928 if (PyErr_CheckSignals() < 0)
dfd0229748ef Merged revisions 84239 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63921
diff changeset
1929 goto error;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1930 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1931 if (self->readable)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1932 _bufferedreader_reset_buf(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1933 if (remaining > 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1934 memcpy(self->buffer, (char *) buf.buf + written, remaining);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1935 written += remaining;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1936 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1937 self->write_pos = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1938 /* TODO: sanity check (remaining >= 0) */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1939 self->write_end = remaining;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1940 ADJUST_POSITION(self, remaining);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1941 self->raw_pos = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1942
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1943 end:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1944 res = PyLong_FromSsize_t(written);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1945
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1946 error:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1947 LEAVE_BUFFERED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1948 PyBuffer_Release(&buf);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1949 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1950 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1951
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1952 static PyMethodDef bufferedwriter_methods[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1953 /* BufferedIOMixin methods */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1954 {"close", (PyCFunction)buffered_close, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1955 {"detach", (PyCFunction)buffered_detach, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1956 {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1957 {"readable", (PyCFunction)buffered_readable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1958 {"writable", (PyCFunction)buffered_writable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1959 {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1960 {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1961
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1962 {"write", (PyCFunction)bufferedwriter_write, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1963 {"truncate", (PyCFunction)buffered_truncate, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1964 {"flush", (PyCFunction)buffered_flush, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1965 {"seek", (PyCFunction)buffered_seek, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1966 {"tell", (PyCFunction)buffered_tell, METH_NOARGS},
78320
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
1967 {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS},
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1968 {NULL, NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1969 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1970
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1971 static PyMemberDef bufferedwriter_members[] = {
67075
595f258806ef Merged revisions 87427 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66660
diff changeset
1972 {"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1973 {NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1974 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1975
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1976 static PyGetSetDef bufferedwriter_getset[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1977 {"closed", (getter)buffered_closed_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1978 {"name", (getter)buffered_name_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1979 {"mode", (getter)buffered_mode_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1980 {NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1981 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1982
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1983
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1984 PyTypeObject PyBufferedWriter_Type = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1985 PyVarObject_HEAD_INIT(NULL, 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1986 "_io.BufferedWriter", /*tp_name*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1987 sizeof(buffered), /*tp_basicsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1988 0, /*tp_itemsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1989 (destructor)buffered_dealloc, /*tp_dealloc*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1990 0, /*tp_print*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1991 0, /*tp_getattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1992 0, /*tp_setattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1993 0, /*tp_compare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1994 (reprfunc)buffered_repr, /*tp_repr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1995 0, /*tp_as_number*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1996 0, /*tp_as_sequence*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1997 0, /*tp_as_mapping*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1998 0, /*tp_hash */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1999 0, /*tp_call*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2000 0, /*tp_str*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2001 0, /*tp_getattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2002 0, /*tp_setattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2003 0, /*tp_as_buffer*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2004 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2005 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2006 bufferedwriter_doc, /* tp_doc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2007 (traverseproc)buffered_traverse, /* tp_traverse */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2008 (inquiry)buffered_clear, /* tp_clear */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2009 0, /* tp_richcompare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2010 offsetof(buffered, weakreflist), /*tp_weaklistoffset*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2011 0, /* tp_iter */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2012 0, /* tp_iternext */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2013 bufferedwriter_methods, /* tp_methods */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2014 bufferedwriter_members, /* tp_members */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2015 bufferedwriter_getset, /* tp_getset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2016 0, /* tp_base */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2017 0, /* tp_dict */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2018 0, /* tp_descr_get */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2019 0, /* tp_descr_set */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2020 offsetof(buffered, dict), /* tp_dictoffset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2021 (initproc)bufferedwriter_init, /* tp_init */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2022 0, /* tp_alloc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2023 PyType_GenericNew, /* tp_new */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2024 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2025
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2026
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2027
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2028 /*
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2029 * BufferedRWPair
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2030 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2031
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2032 PyDoc_STRVAR(bufferedrwpair_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2033 "A buffered reader and writer object together.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2034 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2035 "A buffered reader object and buffered writer object put together to\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2036 "form a sequential IO object that can read and write. This is typically\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2037 "used with a socket or two-way pipe.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2038 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2039 "reader and writer are RawIOBase objects that are readable and\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2040 "writeable respectively. If the buffer_size is omitted it defaults to\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2041 "DEFAULT_BUFFER_SIZE.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2042 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2043
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2044 /* XXX The usefulness of this (compared to having two separate IO objects) is
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2045 * questionable.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2046 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2047
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2048 typedef struct {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2049 PyObject_HEAD
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2050 buffered *reader;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2051 buffered *writer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2052 PyObject *dict;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2053 PyObject *weakreflist;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2054 } rwpair;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2055
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2056 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2057 bufferedrwpair_init(rwpair *self, PyObject *args, PyObject *kwds)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2058 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2059 PyObject *reader, *writer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2060 Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2061 Py_ssize_t max_buffer_size = -234;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2062
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2063 if (!PyArg_ParseTuple(args, "OO|nn:BufferedRWPair", &reader, &writer,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2064 &buffer_size, &max_buffer_size)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2065 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2066 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2067
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2068 if (max_buffer_size != -234 && !complain_about_max_buffer_size())
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2069 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2070
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2071 if (_PyIOBase_check_readable(reader, Py_True) == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2072 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2073 if (_PyIOBase_check_writable(writer, Py_True) == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2074 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2075
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2076 self->reader = (buffered *) PyObject_CallFunction(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2077 (PyObject *) &PyBufferedReader_Type, "On", reader, buffer_size);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2078 if (self->reader == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2079 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2080
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2081 self->writer = (buffered *) PyObject_CallFunction(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2082 (PyObject *) &PyBufferedWriter_Type, "On", writer, buffer_size);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2083 if (self->writer == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2084 Py_CLEAR(self->reader);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2085 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2086 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2087
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2088 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2089 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2090
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2091 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2092 bufferedrwpair_traverse(rwpair *self, visitproc visit, void *arg)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2093 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2094 Py_VISIT(self->dict);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2095 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2096 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2097
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2098 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2099 bufferedrwpair_clear(rwpair *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2100 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2101 Py_CLEAR(self->reader);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2102 Py_CLEAR(self->writer);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2103 Py_CLEAR(self->dict);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2104 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2105 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2106
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2107 static void
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2108 bufferedrwpair_dealloc(rwpair *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2109 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2110 _PyObject_GC_UNTRACK(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2111 Py_CLEAR(self->reader);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2112 Py_CLEAR(self->writer);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2113 Py_CLEAR(self->dict);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2114 Py_TYPE(self)->tp_free((PyObject *) self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2115 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2116
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2117 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2118 _forward_call(buffered *self, const char *name, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2119 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2120 PyObject *func = PyObject_GetAttrString((PyObject *)self, name);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2121 PyObject *ret;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2122
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2123 if (func == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2124 PyErr_SetString(PyExc_AttributeError, name);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2125 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2126 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2127
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2128 ret = PyObject_CallObject(func, args);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2129 Py_DECREF(func);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2130 return ret;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2131 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2132
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2133 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2134 bufferedrwpair_read(rwpair *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2135 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2136 return _forward_call(self->reader, "read", args);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2137 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2138
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2139 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2140 bufferedrwpair_peek(rwpair *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2141 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2142 return _forward_call(self->reader, "peek", args);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2143 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2144
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2145 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2146 bufferedrwpair_read1(rwpair *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2147 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2148 return _forward_call(self->reader, "read1", args);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2149 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2150
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2151 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2152 bufferedrwpair_readinto(rwpair *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2153 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2154 return _forward_call(self->reader, "readinto", args);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2155 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2156
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2157 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2158 bufferedrwpair_write(rwpair *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2159 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2160 return _forward_call(self->writer, "write", args);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2161 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2162
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2163 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2164 bufferedrwpair_flush(rwpair *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2165 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2166 return _forward_call(self->writer, "flush", args);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2167 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2168
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2169 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2170 bufferedrwpair_readable(rwpair *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2171 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2172 return _forward_call(self->reader, "readable", args);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2173 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2174
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2175 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2176 bufferedrwpair_writable(rwpair *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2177 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2178 return _forward_call(self->writer, "writable", args);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2179 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2180
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2181 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2182 bufferedrwpair_close(rwpair *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2183 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2184 PyObject *ret = _forward_call(self->writer, "close", args);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2185 if (ret == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2186 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2187 Py_DECREF(ret);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2188
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2189 return _forward_call(self->reader, "close", args);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2190 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2191
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2192 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2193 bufferedrwpair_isatty(rwpair *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2194 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2195 PyObject *ret = _forward_call(self->writer, "isatty", args);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2196
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2197 if (ret != Py_False) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2198 /* either True or exception */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2199 return ret;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2200 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2201 Py_DECREF(ret);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2202
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2203 return _forward_call(self->reader, "isatty", args);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2204 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2205
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2206 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2207 bufferedrwpair_closed_get(rwpair *self, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2208 {
72754
89b9e4bf6f1f Issue #13070: Fix a crash when a TextIOWrapper caught in a reference cycle
Charles-François Natali <neologix@free.fr>
parents: 71984
diff changeset
2209 if (self->writer == NULL) {
89b9e4bf6f1f Issue #13070: Fix a crash when a TextIOWrapper caught in a reference cycle
Charles-François Natali <neologix@free.fr>
parents: 71984
diff changeset
2210 PyErr_SetString(PyExc_RuntimeError,
89b9e4bf6f1f Issue #13070: Fix a crash when a TextIOWrapper caught in a reference cycle
Charles-François Natali <neologix@free.fr>
parents: 71984
diff changeset
2211 "the BufferedRWPair object is being garbage-collected");
89b9e4bf6f1f Issue #13070: Fix a crash when a TextIOWrapper caught in a reference cycle
Charles-François Natali <neologix@free.fr>
parents: 71984
diff changeset
2212 return NULL;
89b9e4bf6f1f Issue #13070: Fix a crash when a TextIOWrapper caught in a reference cycle
Charles-François Natali <neologix@free.fr>
parents: 71984
diff changeset
2213 }
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2214 return PyObject_GetAttr((PyObject *) self->writer, _PyIO_str_closed);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2215 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2216
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2217 static PyMethodDef bufferedrwpair_methods[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2218 {"read", (PyCFunction)bufferedrwpair_read, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2219 {"peek", (PyCFunction)bufferedrwpair_peek, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2220 {"read1", (PyCFunction)bufferedrwpair_read1, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2221 {"readinto", (PyCFunction)bufferedrwpair_readinto, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2222
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2223 {"write", (PyCFunction)bufferedrwpair_write, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2224 {"flush", (PyCFunction)bufferedrwpair_flush, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2225
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2226 {"readable", (PyCFunction)bufferedrwpair_readable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2227 {"writable", (PyCFunction)bufferedrwpair_writable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2228
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2229 {"close", (PyCFunction)bufferedrwpair_close, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2230 {"isatty", (PyCFunction)bufferedrwpair_isatty, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2231
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2232 {NULL, NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2233 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2234
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2235 static PyGetSetDef bufferedrwpair_getset[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2236 {"closed", (getter)bufferedrwpair_closed_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2237 {NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2238 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2239
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2240 PyTypeObject PyBufferedRWPair_Type = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2241 PyVarObject_HEAD_INIT(NULL, 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2242 "_io.BufferedRWPair", /*tp_name*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2243 sizeof(rwpair), /*tp_basicsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2244 0, /*tp_itemsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2245 (destructor)bufferedrwpair_dealloc, /*tp_dealloc*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2246 0, /*tp_print*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2247 0, /*tp_getattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2248 0, /*tp_setattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2249 0, /*tp_compare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2250 0, /*tp_repr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2251 0, /*tp_as_number*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2252 0, /*tp_as_sequence*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2253 0, /*tp_as_mapping*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2254 0, /*tp_hash */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2255 0, /*tp_call*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2256 0, /*tp_str*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2257 0, /*tp_getattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2258 0, /*tp_setattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2259 0, /*tp_as_buffer*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2260 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2261 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2262 bufferedrwpair_doc, /* tp_doc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2263 (traverseproc)bufferedrwpair_traverse, /* tp_traverse */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2264 (inquiry)bufferedrwpair_clear, /* tp_clear */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2265 0, /* tp_richcompare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2266 offsetof(rwpair, weakreflist), /*tp_weaklistoffset*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2267 0, /* tp_iter */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2268 0, /* tp_iternext */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2269 bufferedrwpair_methods, /* tp_methods */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2270 0, /* tp_members */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2271 bufferedrwpair_getset, /* tp_getset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2272 0, /* tp_base */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2273 0, /* tp_dict */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2274 0, /* tp_descr_get */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2275 0, /* tp_descr_set */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2276 offsetof(rwpair, dict), /* tp_dictoffset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2277 (initproc)bufferedrwpair_init, /* tp_init */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2278 0, /* tp_alloc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2279 PyType_GenericNew, /* tp_new */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2280 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2281
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2282
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2283
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2284 /*
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2285 * BufferedRandom
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2286 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2287
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2288 PyDoc_STRVAR(bufferedrandom_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2289 "A buffered interface to random access streams.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2290 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2291 "The constructor creates a reader and writer for a seekable stream,\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2292 "raw, given in the first argument. If the buffer_size is omitted it\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2293 "defaults to DEFAULT_BUFFER_SIZE. max_buffer_size isn't used anymore.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2294 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2295
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2296 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2297 bufferedrandom_init(buffered *self, PyObject *args, PyObject *kwds)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2298 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2299 char *kwlist[] = {"raw", "buffer_size", "max_buffer_size", NULL};
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2300 Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2301 Py_ssize_t max_buffer_size = -234;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2302 PyObject *raw;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2303
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2304 self->ok = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2305 self->detached = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2306
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2307 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|nn:BufferedReader", kwlist,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2308 &raw, &buffer_size, &max_buffer_size)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2309 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2310 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2311
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2312 if (max_buffer_size != -234 && !complain_about_max_buffer_size())
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2313 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2314
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2315 if (_PyIOBase_check_seekable(raw, Py_True) == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2316 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2317 if (_PyIOBase_check_readable(raw, Py_True) == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2318 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2319 if (_PyIOBase_check_writable(raw, Py_True) == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2320 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2321
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2322 Py_CLEAR(self->raw);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2323 Py_INCREF(raw);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2324 self->raw = raw;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2325 self->buffer_size = buffer_size;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2326 self->readable = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2327 self->writable = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2328
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2329 if (_buffered_init(self) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2330 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2331 _bufferedreader_reset_buf(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2332 _bufferedwriter_reset_buf(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2333 self->pos = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2334
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2335 self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedRandom_Type &&
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2336 Py_TYPE(raw) == &PyFileIO_Type);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2337
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2338 self->ok = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2339 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2340 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2341
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2342 static PyMethodDef bufferedrandom_methods[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2343 /* BufferedIOMixin methods */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2344 {"close", (PyCFunction)buffered_close, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2345 {"detach", (PyCFunction)buffered_detach, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2346 {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2347 {"readable", (PyCFunction)buffered_readable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2348 {"writable", (PyCFunction)buffered_writable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2349 {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2350 {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2351
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2352 {"flush", (PyCFunction)buffered_flush, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2353
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2354 {"seek", (PyCFunction)buffered_seek, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2355 {"tell", (PyCFunction)buffered_tell, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2356 {"truncate", (PyCFunction)buffered_truncate, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2357 {"read", (PyCFunction)buffered_read, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2358 {"read1", (PyCFunction)buffered_read1, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2359 {"readinto", (PyCFunction)buffered_readinto, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2360 {"readline", (PyCFunction)buffered_readline, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2361 {"peek", (PyCFunction)buffered_peek, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2362 {"write", (PyCFunction)bufferedwriter_write, METH_VARARGS},
78320
1102e86b8739 Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73667
diff changeset
2363 {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS},
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2364 {NULL, NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2365 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2366
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2367 static PyMemberDef bufferedrandom_members[] = {
67075
595f258806ef Merged revisions 87427 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66660
diff changeset
2368 {"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2369 {NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2370 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2371
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2372 static PyGetSetDef bufferedrandom_getset[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2373 {"closed", (getter)buffered_closed_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2374 {"name", (getter)buffered_name_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2375 {"mode", (getter)buffered_mode_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2376 {NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2377 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2378
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2379
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2380 PyTypeObject PyBufferedRandom_Type = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2381 PyVarObject_HEAD_INIT(NULL, 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2382 "_io.BufferedRandom", /*tp_name*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2383 sizeof(buffered), /*tp_basicsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2384 0, /*tp_itemsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2385 (destructor)buffered_dealloc, /*tp_dealloc*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2386 0, /*tp_print*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2387 0, /*tp_getattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2388 0, /*tp_setattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2389 0, /*tp_compare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2390 (reprfunc)buffered_repr, /*tp_repr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2391 0, /*tp_as_number*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2392 0, /*tp_as_sequence*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2393 0, /*tp_as_mapping*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2394 0, /*tp_hash */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2395 0, /*tp_call*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2396 0, /*tp_str*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2397 0, /*tp_getattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2398 0, /*tp_setattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2399 0, /*tp_as_buffer*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2400 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2401 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2402 bufferedrandom_doc, /* tp_doc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2403 (traverseproc)buffered_traverse, /* tp_traverse */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2404 (inquiry)buffered_clear, /* tp_clear */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2405 0, /* tp_richcompare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2406 offsetof(buffered, weakreflist), /*tp_weaklistoffset*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2407 0, /* tp_iter */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2408 (iternextfunc)buffered_iternext, /* tp_iternext */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2409 bufferedrandom_methods, /* tp_methods */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2410 bufferedrandom_members, /* tp_members */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2411 bufferedrandom_getset, /* tp_getset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2412 0, /* tp_base */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2413 0, /*tp_dict*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2414 0, /* tp_descr_get */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2415 0, /* tp_descr_set */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2416 offsetof(buffered, dict), /*tp_dictoffset*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2417 (initproc)bufferedrandom_init, /* tp_init */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2418 0, /* tp_alloc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2419 PyType_GenericNew, /* tp_new */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2420 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2421