blob: 0c329bff840bd3c95ef4af4ca63840811354a2e7 [file] [log] [blame]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001/*
2 An implementation of the I/O abstract base classes hierarchy
3 as defined by PEP 3116 - "New I/O"
Victor Stinnercc024d12013-10-29 02:23:46 +01004
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00005 Classes defined here: IOBase, RawIOBase.
Victor Stinnercc024d12013-10-29 02:23:46 +01006
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00007 Written by Amaury Forgeot d'Arc and Antoine Pitrou
8*/
9
10
11#define PY_SSIZE_T_CLEAN
12#include "Python.h"
13#include "structmember.h"
14#include "_iomodule.h"
15
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030016/*[clinic input]
17module _io
18class _io._IOBase "PyObject *" "&PyIOBase_Type"
19class _io._RawIOBase "PyObject *" "&PyRawIOBase_Type"
20[clinic start generated code]*/
21/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d29a4d076c2b211c]*/
22
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000023/*
24 * IOBase class, an abstract class
25 */
26
27typedef struct {
28 PyObject_HEAD
Victor Stinnercc024d12013-10-29 02:23:46 +010029
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000030 PyObject *dict;
31 PyObject *weakreflist;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000032} iobase;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000033
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000034PyDoc_STRVAR(iobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000035 "The abstract base class for all I/O classes, acting on streams of\n"
36 "bytes. There is no public constructor.\n"
37 "\n"
38 "This class provides dummy implementations for many methods that\n"
39 "derived classes can override selectively; the default implementations\n"
40 "represent a file that cannot be read, written or seeked.\n"
41 "\n"
42 "Even though IOBase does not declare read, readinto, or write because\n"
43 "their signatures will vary, implementations and clients should\n"
44 "consider those methods part of the interface. Also, implementations\n"
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +000045 "may raise UnsupportedOperation when operations they do not support are\n"
46 "called.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000047 "\n"
48 "The basic type used for binary data read from or written to a file is\n"
Martin Panter6bb91f32016-05-28 00:41:57 +000049 "bytes. Other bytes-like objects are accepted as method arguments too.\n"
50 "In some cases (such as readinto), a writable object is required. Text\n"
51 "I/O classes work with str data.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000052 "\n"
Andrew Kuchling76466202014-04-15 21:11:36 -040053 "Note that calling any method (except additional calls to close(),\n"
54 "which are ignored) on a closed stream should raise a ValueError.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000055 "\n"
56 "IOBase (and its subclasses) support the iterator protocol, meaning\n"
57 "that an IOBase object can be iterated over yielding the lines in a\n"
58 "stream.\n"
59 "\n"
60 "IOBase also supports the :keyword:`with` statement. In this example,\n"
Ezio Melotti13925002011-03-16 11:05:33 +020061 "fp is closed after the suite of the with statement is complete:\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000062 "\n"
63 "with open('spam.txt', 'r') as fp:\n"
64 " fp.write('Spam and eggs!')\n");
65
66/* Use this macro whenever you want to check the internal `closed` status
67 of the IOBase object rather than the virtual `closed` attribute as returned
68 by whatever subclass. */
69
Martin v. Löwis767046a2011-10-14 15:35:36 +020070_Py_IDENTIFIER(__IOBase_closed);
Victor Stinner3f36a572013-11-12 21:39:02 +010071_Py_IDENTIFIER(read);
72
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +020073
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000074/* Internal methods */
75static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000076iobase_unsupported(const char *message)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000077{
Antoine Pitrou712cb732013-12-21 15:51:54 +010078 _PyIO_State *state = IO_STATE();
79 if (state != NULL)
80 PyErr_SetString(state->unsupported_operation, message);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000081 return NULL;
82}
83
Raymond Hettinger15f44ab2016-08-30 10:47:49 -070084/* Positioning */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000085
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000086PyDoc_STRVAR(iobase_seek_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000087 "Change stream position.\n"
88 "\n"
Terry Jan Reedy0158af32013-03-11 17:42:46 -040089 "Change the stream position to the given byte offset. The offset is\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000090 "interpreted relative to the position indicated by whence. Values\n"
91 "for whence are:\n"
92 "\n"
93 "* 0 -- start of stream (the default); offset should be zero or positive\n"
94 "* 1 -- current stream position; offset may be negative\n"
95 "* 2 -- end of stream; offset is usually negative\n"
96 "\n"
97 "Return the new absolute position.");
98
99static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000100iobase_seek(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000101{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000102 return iobase_unsupported("seek");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000103}
104
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300105/*[clinic input]
106_io._IOBase.tell
107
108Return current stream position.
109[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000110
111static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300112_io__IOBase_tell_impl(PyObject *self)
113/*[clinic end generated code: output=89a1c0807935abe2 input=04e615fec128801f]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000114{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200115 _Py_IDENTIFIER(seek);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200116
117 return _PyObject_CallMethodId(self, &PyId_seek, "ii", 0, 1);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000118}
119
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000120PyDoc_STRVAR(iobase_truncate_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000121 "Truncate file to size bytes.\n"
122 "\n"
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000123 "File pointer is left unchanged. Size defaults to the current IO\n"
124 "position as reported by tell(). Returns the new size.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000125
126static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000127iobase_truncate(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000128{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000129 return iobase_unsupported("truncate");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000130}
131
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200132static int
133iobase_is_closed(PyObject *self)
134{
135 PyObject *res;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200136 int ret;
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200137 /* This gets the derived attribute, which is *not* __IOBase_closed
138 in most cases! */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200139 ret = _PyObject_LookupAttrId(self, &PyId___IOBase_closed, &res);
140 Py_XDECREF(res);
141 return ret;
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200142}
143
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000144/* Flush and close methods */
145
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300146/*[clinic input]
147_io._IOBase.flush
148
149Flush write buffers, if applicable.
150
151This is not implemented for read-only and non-blocking streams.
152[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000153
154static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300155_io__IOBase_flush_impl(PyObject *self)
156/*[clinic end generated code: output=7cef4b4d54656a3b input=773be121abe270aa]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000157{
158 /* XXX Should this return the number of bytes written??? */
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200159 int closed = iobase_is_closed(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000160
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200161 if (!closed) {
162 Py_RETURN_NONE;
163 }
164 if (closed > 0) {
165 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
166 }
167 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000168}
169
170static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000171iobase_closed_get(PyObject *self, void *context)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000172{
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200173 int closed = iobase_is_closed(self);
174 if (closed < 0) {
175 return NULL;
176 }
177 return PyBool_FromLong(closed);
178}
179
180static int
181iobase_check_closed(PyObject *self)
182{
183 PyObject *res;
184 int closed;
185 /* This gets the derived attribute, which is *not* __IOBase_closed
186 in most cases! */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200187 closed = _PyObject_LookupAttr(self, _PyIO_str_closed, &res);
188 if (closed > 0) {
189 closed = PyObject_IsTrue(res);
190 Py_DECREF(res);
191 if (closed > 0) {
192 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200193 return -1;
194 }
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200195 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200196 return closed;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000197}
198
199PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000200_PyIOBase_check_closed(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000201{
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200202 if (iobase_check_closed(self)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000203 return NULL;
204 }
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200205 if (args == Py_True) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000206 return Py_None;
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200207 }
208 Py_RETURN_NONE;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000209}
210
211/* XXX: IOBase thinks it has to maintain its own internal state in
212 `__IOBase_closed` and call flush() by itself, but it is redundant with
213 whatever behaviour a non-trivial derived class will implement. */
214
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300215/*[clinic input]
216_io._IOBase.close
217
218Flush and close the IO object.
219
220This method has no effect if the file is already closed.
221[clinic start generated code]*/
222
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000223static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300224_io__IOBase_close_impl(PyObject *self)
225/*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000226{
227 PyObject *res;
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200228 int closed = iobase_is_closed(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000229
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200230 if (closed < 0) {
231 return NULL;
232 }
233 if (closed) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000234 Py_RETURN_NONE;
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200235 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000236
237 res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
Victor Stinneraa5bbfa2013-11-08 00:29:41 +0100238
239 if (_PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True) < 0) {
240 Py_XDECREF(res);
Antoine Pitrou6be88762010-05-03 16:48:20 +0000241 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000242 }
Victor Stinneraa5bbfa2013-11-08 00:29:41 +0100243
244 if (res == NULL)
245 return NULL;
246
247 Py_DECREF(res);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000248 Py_RETURN_NONE;
249}
250
251/* Finalization and garbage collection support */
252
Antoine Pitrou796564c2013-07-30 19:59:21 +0200253static void
254iobase_finalize(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000255{
256 PyObject *res;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200257 PyObject *error_type, *error_value, *error_traceback;
258 int closed;
259 _Py_IDENTIFIER(_finalizing);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000260
Antoine Pitrou796564c2013-07-30 19:59:21 +0200261 /* Save the current exception, if any. */
262 PyErr_Fetch(&error_type, &error_value, &error_traceback);
263
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000264 /* If `closed` doesn't exist or can't be evaluated as bool, then the
265 object is probably in an unusable state, so ignore. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200266 if (_PyObject_LookupAttr(self, _PyIO_str_closed, &res) <= 0) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000267 PyErr_Clear();
Christian Heimes72f455e2013-07-31 01:33:50 +0200268 closed = -1;
269 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000270 else {
271 closed = PyObject_IsTrue(res);
272 Py_DECREF(res);
273 if (closed == -1)
274 PyErr_Clear();
275 }
276 if (closed == 0) {
Antoine Pitrou796564c2013-07-30 19:59:21 +0200277 /* Signal close() that it was called as part of the object
278 finalization process. */
279 if (_PyObject_SetAttrId(self, &PyId__finalizing, Py_True))
280 PyErr_Clear();
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000281 res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close,
282 NULL);
283 /* Silencing I/O errors is bad, but printing spurious tracebacks is
284 equally as bad, and potentially more frequent (because of
285 shutdown issues). */
286 if (res == NULL)
287 PyErr_Clear();
288 else
289 Py_DECREF(res);
290 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200291
292 /* Restore the saved exception. */
293 PyErr_Restore(error_type, error_value, error_traceback);
294}
295
296int
297_PyIOBase_finalize(PyObject *self)
298{
299 int is_zombie;
300
301 /* If _PyIOBase_finalize() is called from a destructor, we need to
302 resurrect the object as calling close() can invoke arbitrary code. */
303 is_zombie = (Py_REFCNT(self) == 0);
304 if (is_zombie)
305 return PyObject_CallFinalizerFromDealloc(self);
306 else {
307 PyObject_CallFinalizer(self);
308 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000309 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000310}
311
312static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000313iobase_traverse(iobase *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000314{
315 Py_VISIT(self->dict);
316 return 0;
317}
318
319static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000320iobase_clear(iobase *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000321{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000322 Py_CLEAR(self->dict);
323 return 0;
324}
325
326/* Destructor */
327
328static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000329iobase_dealloc(iobase *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000330{
331 /* NOTE: since IOBaseObject has its own dict, Python-defined attributes
332 are still available here for close() to use.
333 However, if the derived class declares a __slots__, those slots are
334 already gone.
335 */
336 if (_PyIOBase_finalize((PyObject *) self) < 0) {
337 /* When called from a heap type's dealloc, the type will be
338 decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
339 if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE))
340 Py_INCREF(Py_TYPE(self));
341 return;
342 }
343 _PyObject_GC_UNTRACK(self);
344 if (self->weakreflist != NULL)
345 PyObject_ClearWeakRefs((PyObject *) self);
346 Py_CLEAR(self->dict);
347 Py_TYPE(self)->tp_free((PyObject *) self);
348}
349
350/* Inquiry methods */
351
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300352/*[clinic input]
353_io._IOBase.seekable
354
355Return whether object supports random access.
356
Martin Panter754aab22016-03-31 07:21:56 +0000357If False, seek(), tell() and truncate() will raise OSError.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300358This method may need to do a test seek().
359[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000360
361static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300362_io__IOBase_seekable_impl(PyObject *self)
Martin Panter754aab22016-03-31 07:21:56 +0000363/*[clinic end generated code: output=4c24c67f5f32a43d input=b976622f7fdf3063]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000364{
365 Py_RETURN_FALSE;
366}
367
368PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000369_PyIOBase_check_seekable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000370{
371 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL);
372 if (res == NULL)
373 return NULL;
374 if (res != Py_True) {
375 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000376 iobase_unsupported("File or stream is not seekable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000377 return NULL;
378 }
379 if (args == Py_True) {
380 Py_DECREF(res);
381 }
382 return res;
383}
384
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300385/*[clinic input]
386_io._IOBase.readable
387
388Return whether object was opened for reading.
389
Martin Panter754aab22016-03-31 07:21:56 +0000390If False, read() will raise OSError.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300391[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000392
393static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300394_io__IOBase_readable_impl(PyObject *self)
Martin Panter754aab22016-03-31 07:21:56 +0000395/*[clinic end generated code: output=e48089250686388b input=285b3b866a0ec35f]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000396{
397 Py_RETURN_FALSE;
398}
399
400/* May be called with any object */
401PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000402_PyIOBase_check_readable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000403{
404 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL);
405 if (res == NULL)
406 return NULL;
407 if (res != Py_True) {
408 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000409 iobase_unsupported("File or stream is not readable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000410 return NULL;
411 }
412 if (args == Py_True) {
413 Py_DECREF(res);
414 }
415 return res;
416}
417
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300418/*[clinic input]
419_io._IOBase.writable
420
421Return whether object was opened for writing.
422
Martin Panter754aab22016-03-31 07:21:56 +0000423If False, write() will raise OSError.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300424[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000425
426static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300427_io__IOBase_writable_impl(PyObject *self)
Martin Panter754aab22016-03-31 07:21:56 +0000428/*[clinic end generated code: output=406001d0985be14f input=9dcac18a013a05b5]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000429{
430 Py_RETURN_FALSE;
431}
432
433/* May be called with any object */
434PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000435_PyIOBase_check_writable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000436{
437 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL);
438 if (res == NULL)
439 return NULL;
440 if (res != Py_True) {
441 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000442 iobase_unsupported("File or stream is not writable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000443 return NULL;
444 }
445 if (args == Py_True) {
446 Py_DECREF(res);
447 }
448 return res;
449}
450
451/* Context manager */
452
453static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000454iobase_enter(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000455{
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200456 if (iobase_check_closed(self))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000457 return NULL;
458
459 Py_INCREF(self);
460 return self;
461}
462
463static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000464iobase_exit(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000465{
466 return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL);
467}
468
469/* Lower-level APIs */
470
471/* XXX Should these be present even if unimplemented? */
472
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300473/*[clinic input]
474_io._IOBase.fileno
475
476Returns underlying file descriptor if one exists.
477
Martin Panter754aab22016-03-31 07:21:56 +0000478OSError is raised if the IO object does not use a file descriptor.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300479[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000480
481static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300482_io__IOBase_fileno_impl(PyObject *self)
Martin Panter754aab22016-03-31 07:21:56 +0000483/*[clinic end generated code: output=7cc0973f0f5f3b73 input=4e37028947dc1cc8]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000484{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000485 return iobase_unsupported("fileno");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000486}
487
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300488/*[clinic input]
489_io._IOBase.isatty
490
491Return whether this is an 'interactive' stream.
492
493Return False if it can't be determined.
494[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000495
496static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300497_io__IOBase_isatty_impl(PyObject *self)
498/*[clinic end generated code: output=60cab77cede41cdd input=9ef76530d368458b]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000499{
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200500 if (iobase_check_closed(self))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000501 return NULL;
502 Py_RETURN_FALSE;
503}
504
505/* Readline(s) and writelines */
506
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300507/*[clinic input]
508_io._IOBase.readline
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300509 size as limit: Py_ssize_t(accept={int, NoneType}) = -1
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300510 /
511
512Read and return a line from the stream.
513
514If size is specified, at most size bytes will be read.
515
516The line terminator is always b'\n' for binary files; for text
517files, the newlines argument to open can be used to select the line
518terminator(s) recognized.
519[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000520
521static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300522_io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit)
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300523/*[clinic end generated code: output=4479f79b58187840 input=d0c596794e877bff]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000524{
525 /* For backwards compatibility, a (slowish) readline(). */
526
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200527 PyObject *peek, *buffer, *result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000528 Py_ssize_t old_size = -1;
529
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200530 if (_PyObject_LookupAttr(self, _PyIO_str_peek, &peek) < 0) {
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200531 return NULL;
532 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000533
534 buffer = PyByteArray_FromStringAndSize(NULL, 0);
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200535 if (buffer == NULL) {
536 Py_XDECREF(peek);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000537 return NULL;
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200538 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000539
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200540 while (limit < 0 || PyByteArray_GET_SIZE(buffer) < limit) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000541 Py_ssize_t nreadahead = 1;
542 PyObject *b;
543
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200544 if (peek != NULL) {
545 PyObject *readahead = PyObject_CallFunctionObjArgs(peek, _PyLong_One, NULL);
Gregory P. Smith51359922012-06-23 23:55:39 -0700546 if (readahead == NULL) {
547 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
548 when EINTR occurs so we needn't do it ourselves. */
549 if (_PyIO_trap_eintr()) {
550 continue;
551 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000552 goto fail;
Gregory P. Smith51359922012-06-23 23:55:39 -0700553 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000554 if (!PyBytes_Check(readahead)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300555 PyErr_Format(PyExc_OSError,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000556 "peek() should have returned a bytes object, "
557 "not '%.200s'", Py_TYPE(readahead)->tp_name);
558 Py_DECREF(readahead);
559 goto fail;
560 }
561 if (PyBytes_GET_SIZE(readahead) > 0) {
562 Py_ssize_t n = 0;
563 const char *buf = PyBytes_AS_STRING(readahead);
564 if (limit >= 0) {
565 do {
566 if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)
567 break;
568 if (buf[n++] == '\n')
569 break;
570 } while (1);
571 }
572 else {
573 do {
574 if (n >= PyBytes_GET_SIZE(readahead))
575 break;
576 if (buf[n++] == '\n')
577 break;
578 } while (1);
579 }
580 nreadahead = n;
581 }
582 Py_DECREF(readahead);
583 }
584
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200585 b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead);
Gregory P. Smith51359922012-06-23 23:55:39 -0700586 if (b == NULL) {
587 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
588 when EINTR occurs so we needn't do it ourselves. */
589 if (_PyIO_trap_eintr()) {
590 continue;
591 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000592 goto fail;
Gregory P. Smith51359922012-06-23 23:55:39 -0700593 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000594 if (!PyBytes_Check(b)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300595 PyErr_Format(PyExc_OSError,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000596 "read() should have returned a bytes object, "
597 "not '%.200s'", Py_TYPE(b)->tp_name);
598 Py_DECREF(b);
599 goto fail;
600 }
601 if (PyBytes_GET_SIZE(b) == 0) {
602 Py_DECREF(b);
603 break;
604 }
605
606 old_size = PyByteArray_GET_SIZE(buffer);
Victor Stinnercc024d12013-10-29 02:23:46 +0100607 if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) {
608 Py_DECREF(b);
609 goto fail;
610 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000611 memcpy(PyByteArray_AS_STRING(buffer) + old_size,
612 PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));
613
614 Py_DECREF(b);
615
616 if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n')
617 break;
618 }
619
620 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer),
621 PyByteArray_GET_SIZE(buffer));
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200622 Py_XDECREF(peek);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000623 Py_DECREF(buffer);
624 return result;
625 fail:
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200626 Py_XDECREF(peek);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000627 Py_DECREF(buffer);
628 return NULL;
629}
630
631static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000632iobase_iter(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000633{
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200634 if (iobase_check_closed(self))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000635 return NULL;
636
637 Py_INCREF(self);
638 return self;
639}
640
641static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000642iobase_iternext(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000643{
644 PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL);
645
646 if (line == NULL)
647 return NULL;
648
Serhiy Storchakabf623ae2017-04-19 20:03:52 +0300649 if (PyObject_Size(line) <= 0) {
650 /* Error or empty */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000651 Py_DECREF(line);
652 return NULL;
653 }
654
655 return line;
656}
657
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300658/*[clinic input]
659_io._IOBase.readlines
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300660 hint: Py_ssize_t(accept={int, NoneType}) = -1
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300661 /
662
663Return a list of lines from the stream.
664
665hint can be specified to control the number of lines read: no more
666lines will be read if the total size (in bytes/characters) of all
667lines so far exceeds hint.
668[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000669
670static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300671_io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint)
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300672/*[clinic end generated code: output=2f50421677fa3dea input=9400c786ea9dc416]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000673{
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300674 Py_ssize_t length = 0;
Xiang Zhang026435c2017-04-15 12:47:28 +0800675 PyObject *result, *it = NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000676
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000677 result = PyList_New(0);
678 if (result == NULL)
679 return NULL;
680
681 if (hint <= 0) {
682 /* XXX special-casing this made sense in the Python version in order
683 to remove the bytecode interpretation overhead, but it could
684 probably be removed here. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200685 _Py_IDENTIFIER(extend);
Victor Stinner61bdb0d2016-12-09 15:39:28 +0100686 PyObject *ret = _PyObject_CallMethodIdObjArgs(result, &PyId_extend,
687 self, NULL);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200688
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000689 if (ret == NULL) {
Xiang Zhang026435c2017-04-15 12:47:28 +0800690 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000691 }
692 Py_DECREF(ret);
693 return result;
694 }
695
Xiang Zhang026435c2017-04-15 12:47:28 +0800696 it = PyObject_GetIter(self);
697 if (it == NULL) {
698 goto error;
699 }
700
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000701 while (1) {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +0300702 Py_ssize_t line_length;
Xiang Zhang026435c2017-04-15 12:47:28 +0800703 PyObject *line = PyIter_Next(it);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000704 if (line == NULL) {
705 if (PyErr_Occurred()) {
Xiang Zhang026435c2017-04-15 12:47:28 +0800706 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000707 }
708 else
709 break; /* StopIteration raised */
710 }
711
712 if (PyList_Append(result, line) < 0) {
713 Py_DECREF(line);
Xiang Zhang026435c2017-04-15 12:47:28 +0800714 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000715 }
Serhiy Storchakabf623ae2017-04-19 20:03:52 +0300716 line_length = PyObject_Size(line);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000717 Py_DECREF(line);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +0300718 if (line_length < 0) {
719 goto error;
720 }
721 if (line_length > hint - length)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000722 break;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +0300723 length += line_length;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000724 }
Xiang Zhang026435c2017-04-15 12:47:28 +0800725
726 Py_DECREF(it);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000727 return result;
Xiang Zhang026435c2017-04-15 12:47:28 +0800728
729 error:
730 Py_XDECREF(it);
731 Py_DECREF(result);
732 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000733}
734
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300735/*[clinic input]
736_io._IOBase.writelines
737 lines: object
738 /
739[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000740
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300741static PyObject *
742_io__IOBase_writelines(PyObject *self, PyObject *lines)
743/*[clinic end generated code: output=976eb0a9b60a6628 input=432e729a8450b3cb]*/
744{
745 PyObject *iter, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000746
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +0200747 if (iobase_check_closed(self))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000748 return NULL;
749
750 iter = PyObject_GetIter(lines);
751 if (iter == NULL)
752 return NULL;
753
754 while (1) {
755 PyObject *line = PyIter_Next(iter);
756 if (line == NULL) {
757 if (PyErr_Occurred()) {
758 Py_DECREF(iter);
759 return NULL;
760 }
761 else
762 break; /* Stop Iteration */
763 }
764
Gregory P. Smithb9817b02013-02-01 13:03:39 -0800765 res = NULL;
766 do {
767 res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);
768 } while (res == NULL && _PyIO_trap_eintr());
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000769 Py_DECREF(line);
770 if (res == NULL) {
771 Py_DECREF(iter);
772 return NULL;
773 }
774 Py_DECREF(res);
775 }
776 Py_DECREF(iter);
777 Py_RETURN_NONE;
778}
779
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300780#include "clinic/iobase.c.h"
781
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000782static PyMethodDef iobase_methods[] = {
783 {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300784 _IO__IOBASE_TELL_METHODDEF
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000785 {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300786 _IO__IOBASE_FLUSH_METHODDEF
787 _IO__IOBASE_CLOSE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000788
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300789 _IO__IOBASE_SEEKABLE_METHODDEF
790 _IO__IOBASE_READABLE_METHODDEF
791 _IO__IOBASE_WRITABLE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000792
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000793 {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},
794 {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},
795 {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},
796 {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000797
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300798 _IO__IOBASE_FILENO_METHODDEF
799 _IO__IOBASE_ISATTY_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000800
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000801 {"__enter__", iobase_enter, METH_NOARGS},
802 {"__exit__", iobase_exit, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000803
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300804 _IO__IOBASE_READLINE_METHODDEF
805 _IO__IOBASE_READLINES_METHODDEF
806 _IO__IOBASE_WRITELINES_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000807
808 {NULL, NULL}
809};
810
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000811static PyGetSetDef iobase_getset[] = {
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500812 {"__dict__", PyObject_GenericGetDict, NULL, NULL},
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000813 {"closed", (getter)iobase_closed_get, NULL, NULL},
Benjamin Peterson1fea3212009-04-19 03:15:20 +0000814 {NULL}
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000815};
816
817
818PyTypeObject PyIOBase_Type = {
819 PyVarObject_HEAD_INIT(NULL, 0)
820 "_io._IOBase", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000821 sizeof(iobase), /*tp_basicsize*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000822 0, /*tp_itemsize*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000823 (destructor)iobase_dealloc, /*tp_dealloc*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000824 0, /*tp_print*/
825 0, /*tp_getattr*/
826 0, /*tp_setattr*/
827 0, /*tp_compare */
828 0, /*tp_repr*/
829 0, /*tp_as_number*/
830 0, /*tp_as_sequence*/
831 0, /*tp_as_mapping*/
832 0, /*tp_hash */
833 0, /*tp_call*/
834 0, /*tp_str*/
835 0, /*tp_getattro*/
836 0, /*tp_setattro*/
837 0, /*tp_as_buffer*/
838 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +0200839 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000840 iobase_doc, /* tp_doc */
841 (traverseproc)iobase_traverse, /* tp_traverse */
842 (inquiry)iobase_clear, /* tp_clear */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000843 0, /* tp_richcompare */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000844 offsetof(iobase, weakreflist), /* tp_weaklistoffset */
845 iobase_iter, /* tp_iter */
846 iobase_iternext, /* tp_iternext */
847 iobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000848 0, /* tp_members */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000849 iobase_getset, /* tp_getset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000850 0, /* tp_base */
851 0, /* tp_dict */
852 0, /* tp_descr_get */
853 0, /* tp_descr_set */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000854 offsetof(iobase, dict), /* tp_dictoffset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000855 0, /* tp_init */
856 0, /* tp_alloc */
857 PyType_GenericNew, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200858 0, /* tp_free */
859 0, /* tp_is_gc */
860 0, /* tp_bases */
861 0, /* tp_mro */
862 0, /* tp_cache */
863 0, /* tp_subclasses */
864 0, /* tp_weaklist */
865 0, /* tp_del */
866 0, /* tp_version_tag */
Victor Stinner928bff02016-03-19 10:36:36 +0100867 iobase_finalize, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000868};
869
870
871/*
872 * RawIOBase class, Inherits from IOBase.
873 */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000874PyDoc_STRVAR(rawiobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000875 "Base class for raw binary I/O.");
876
877/*
878 * The read() method is implemented by calling readinto(); derived classes
879 * that want to support read() only need to implement readinto() as a
880 * primitive operation. In general, readinto() can be more efficient than
881 * read().
882 *
883 * (It would be tempting to also provide an implementation of readinto() in
884 * terms of read(), in case the latter is a more suitable primitive operation,
885 * but that would lead to nasty recursion in case a subclass doesn't implement
886 * either.)
887*/
888
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300889/*[clinic input]
890_io._RawIOBase.read
891 size as n: Py_ssize_t = -1
892 /
893[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000894
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300895static PyObject *
896_io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n)
897/*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/
898{
899 PyObject *b, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000900
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200901 if (n < 0) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200902 _Py_IDENTIFIER(readall);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200903
904 return _PyObject_CallMethodId(self, &PyId_readall, NULL);
905 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000906
907 /* TODO: allocate a bytes object directly instead and manually construct
908 a writable memoryview pointing to it. */
909 b = PyByteArray_FromStringAndSize(NULL, n);
910 if (b == NULL)
911 return NULL;
912
913 res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000914 if (res == NULL || res == Py_None) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000915 Py_DECREF(b);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000916 return res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000917 }
918
919 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
920 Py_DECREF(res);
921 if (n == -1 && PyErr_Occurred()) {
922 Py_DECREF(b);
923 return NULL;
924 }
925
926 res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
927 Py_DECREF(b);
928 return res;
929}
930
931
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300932/*[clinic input]
933_io._RawIOBase.readall
934
935Read until EOF, using multiple read() call.
936[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000937
938static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300939_io__RawIOBase_readall_impl(PyObject *self)
940/*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000941{
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000942 int r;
943 PyObject *chunks = PyList_New(0);
944 PyObject *result;
Victor Stinnercc024d12013-10-29 02:23:46 +0100945
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000946 if (chunks == NULL)
947 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000948
949 while (1) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200950 PyObject *data = _PyObject_CallMethodId(self, &PyId_read,
951 "i", DEFAULT_BUFFER_SIZE);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000952 if (!data) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700953 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
954 when EINTR occurs so we needn't do it ourselves. */
955 if (_PyIO_trap_eintr()) {
956 continue;
957 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000958 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000959 return NULL;
960 }
Victor Stinnera80987f2011-05-25 22:47:16 +0200961 if (data == Py_None) {
962 if (PyList_GET_SIZE(chunks) == 0) {
963 Py_DECREF(chunks);
964 return data;
965 }
966 Py_DECREF(data);
967 break;
968 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000969 if (!PyBytes_Check(data)) {
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000970 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000971 Py_DECREF(data);
972 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
973 return NULL;
974 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000975 if (PyBytes_GET_SIZE(data) == 0) {
976 /* EOF */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000977 Py_DECREF(data);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000978 break;
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000979 }
980 r = PyList_Append(chunks, data);
981 Py_DECREF(data);
982 if (r < 0) {
983 Py_DECREF(chunks);
984 return NULL;
985 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000986 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000987 result = _PyBytes_Join(_PyIO_empty_bytes, chunks);
988 Py_DECREF(chunks);
989 return result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000990}
991
Antoine Pitrou45d61562015-05-20 21:50:59 +0200992static PyObject *
993rawiobase_readinto(PyObject *self, PyObject *args)
994{
995 PyErr_SetNone(PyExc_NotImplementedError);
996 return NULL;
997}
998
999static PyObject *
1000rawiobase_write(PyObject *self, PyObject *args)
1001{
1002 PyErr_SetNone(PyExc_NotImplementedError);
1003 return NULL;
1004}
1005
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001006static PyMethodDef rawiobase_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001007 _IO__RAWIOBASE_READ_METHODDEF
1008 _IO__RAWIOBASE_READALL_METHODDEF
Antoine Pitrou45d61562015-05-20 21:50:59 +02001009 {"readinto", rawiobase_readinto, METH_VARARGS},
1010 {"write", rawiobase_write, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001011 {NULL, NULL}
1012};
1013
1014PyTypeObject PyRawIOBase_Type = {
1015 PyVarObject_HEAD_INIT(NULL, 0)
1016 "_io._RawIOBase", /*tp_name*/
1017 0, /*tp_basicsize*/
1018 0, /*tp_itemsize*/
1019 0, /*tp_dealloc*/
1020 0, /*tp_print*/
1021 0, /*tp_getattr*/
1022 0, /*tp_setattr*/
1023 0, /*tp_compare */
1024 0, /*tp_repr*/
1025 0, /*tp_as_number*/
1026 0, /*tp_as_sequence*/
1027 0, /*tp_as_mapping*/
1028 0, /*tp_hash */
1029 0, /*tp_call*/
1030 0, /*tp_str*/
1031 0, /*tp_getattro*/
1032 0, /*tp_setattro*/
1033 0, /*tp_as_buffer*/
Antoine Pitrou796564c2013-07-30 19:59:21 +02001034 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001035 rawiobase_doc, /* tp_doc */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001036 0, /* tp_traverse */
1037 0, /* tp_clear */
1038 0, /* tp_richcompare */
1039 0, /* tp_weaklistoffset */
1040 0, /* tp_iter */
1041 0, /* tp_iternext */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001042 rawiobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001043 0, /* tp_members */
1044 0, /* tp_getset */
1045 &PyIOBase_Type, /* tp_base */
1046 0, /* tp_dict */
1047 0, /* tp_descr_get */
1048 0, /* tp_descr_set */
1049 0, /* tp_dictoffset */
1050 0, /* tp_init */
1051 0, /* tp_alloc */
1052 0, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001053 0, /* tp_free */
1054 0, /* tp_is_gc */
1055 0, /* tp_bases */
1056 0, /* tp_mro */
1057 0, /* tp_cache */
1058 0, /* tp_subclasses */
1059 0, /* tp_weaklist */
1060 0, /* tp_del */
1061 0, /* tp_version_tag */
1062 0, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001063};