summaryrefslogtreecommitdiffstats
path: root/Modules/_io/textio.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_io/textio.c')
-rw-r--r--Modules/_io/textio.c539
1 files changed, 321 insertions, 218 deletions
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 0c1b13e..b232b02 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -11,6 +11,20 @@
#include "structmember.h"
#include "_iomodule.h"
+/*[clinic input]
+module _io
+class _io.IncrementalNewlineDecoder "nldecoder_object *" "&PyIncrementalNewlineDecoder_Type"
+class _io.TextIOWrapper "textio *" "&TextIOWrapper_TYpe"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=2097a4fc85670c26]*/
+
+/*[python input]
+class io_ssize_t_converter(CConverter):
+ type = 'Py_ssize_t'
+ converter = '_PyIO_ConvertSsize_t'
+[python start generated code]*/
+/*[python end generated code: output=da39a3ee5e6b4b0d input=d0a811d3cbfd1b33]*/
+
_Py_IDENTIFIER(close);
_Py_IDENTIFIER(_dealloc_warn);
_Py_IDENTIFIER(decode);
@@ -210,38 +224,37 @@ PyTypeObject PyTextIOBase_Type = {
/* IncrementalNewlineDecoder */
-PyDoc_STRVAR(incrementalnewlinedecoder_doc,
- "Codec used when reading a file in universal newlines mode. It wraps\n"
- "another incremental decoder, translating \\r\\n and \\r into \\n. It also\n"
- "records the types of newlines encountered. When used with\n"
- "translate=False, it ensures that the newline sequence is returned in\n"
- "one piece. When used with decoder=None, it expects unicode strings as\n"
- "decode input and translates newlines without first invoking an external\n"
- "decoder.\n"
- );
-
typedef struct {
PyObject_HEAD
PyObject *decoder;
PyObject *errors;
- signed int pendingcr: 1;
- signed int translate: 1;
+ unsigned int pendingcr: 1;
+ unsigned int translate: 1;
unsigned int seennl: 3;
} nldecoder_object;
-static int
-incrementalnewlinedecoder_init(nldecoder_object *self,
- PyObject *args, PyObject *kwds)
-{
- PyObject *decoder;
- int translate;
- PyObject *errors = NULL;
- char *kwlist[] = {"decoder", "translate", "errors", NULL};
+/*[clinic input]
+_io.IncrementalNewlineDecoder.__init__
+ decoder: object
+ translate: int
+ errors: object(c_default="NULL") = "strict"
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oi|O:IncrementalNewlineDecoder",
- kwlist, &decoder, &translate, &errors))
- return -1;
+Codec used when reading a file in universal newlines mode.
+
+It wraps another incremental decoder, translating \r\n and \r into \n.
+It also records the types of newlines encountered. When used with
+translate=False, it ensures that the newline sequence is returned in
+one piece. When used with decoder=None, it expects unicode strings as
+decode input and translates newlines without first invoking an external
+decoder.
+[clinic start generated code]*/
+static int
+_io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self,
+ PyObject *decoder, int translate,
+ PyObject *errors)
+/*[clinic end generated code: output=fbd04d443e764ec2 input=89db6b19c6b126bf]*/
+{
self->decoder = decoder;
Py_INCREF(decoder);
@@ -495,22 +508,27 @@ _PyIncrementalNewlineDecoder_decode(PyObject *myself,
return NULL;
}
+/*[clinic input]
+_io.IncrementalNewlineDecoder.decode
+ input: object
+ final: int(c_default="0") = False
+[clinic start generated code]*/
+
static PyObject *
-incrementalnewlinedecoder_decode(nldecoder_object *self,
- PyObject *args, PyObject *kwds)
+_io_IncrementalNewlineDecoder_decode_impl(nldecoder_object *self,
+ PyObject *input, int final)
+/*[clinic end generated code: output=0d486755bb37a66e input=d65677385bfd6827]*/
{
- char *kwlist[] = {"input", "final", NULL};
- PyObject *input;
- int final = 0;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:IncrementalNewlineDecoder",
- kwlist, &input, &final))
- return NULL;
return _PyIncrementalNewlineDecoder_decode((PyObject *) self, input, final);
}
+/*[clinic input]
+_io.IncrementalNewlineDecoder.getstate
+[clinic start generated code]*/
+
static PyObject *
-incrementalnewlinedecoder_getstate(nldecoder_object *self, PyObject *args)
+_io_IncrementalNewlineDecoder_getstate_impl(nldecoder_object *self)
+/*[clinic end generated code: output=f0d2c9c136f4e0d0 input=f8ff101825e32e7f]*/
{
PyObject *buffer;
unsigned PY_LONG_LONG flag;
@@ -520,7 +538,7 @@ incrementalnewlinedecoder_getstate(nldecoder_object *self, PyObject *args)
_PyIO_str_getstate, NULL);
if (state == NULL)
return NULL;
- if (!PyArg_Parse(state, "(OK)", &buffer, &flag)) {
+ if (!PyArg_ParseTuple(state, "OK", &buffer, &flag)) {
Py_DECREF(state);
return NULL;
}
@@ -537,16 +555,24 @@ incrementalnewlinedecoder_getstate(nldecoder_object *self, PyObject *args)
return Py_BuildValue("NK", buffer, flag);
}
+/*[clinic input]
+_io.IncrementalNewlineDecoder.setstate
+ state: object
+ /
+[clinic start generated code]*/
+
static PyObject *
-incrementalnewlinedecoder_setstate(nldecoder_object *self, PyObject *state)
+_io_IncrementalNewlineDecoder_setstate(nldecoder_object *self,
+ PyObject *state)
+/*[clinic end generated code: output=c10c622508b576cb input=c53fb505a76dbbe2]*/
{
PyObject *buffer;
unsigned PY_LONG_LONG flag;
- if (!PyArg_Parse(state, "(OK)", &buffer, &flag))
+ if (!PyArg_ParseTuple(state, "OK", &buffer, &flag))
return NULL;
- self->pendingcr = (int) flag & 1;
+ self->pendingcr = (int) (flag & 1);
flag >>= 1;
if (self->decoder != Py_None)
@@ -556,8 +582,13 @@ incrementalnewlinedecoder_setstate(nldecoder_object *self, PyObject *state)
Py_RETURN_NONE;
}
+/*[clinic input]
+_io.IncrementalNewlineDecoder.reset
+[clinic start generated code]*/
+
static PyObject *
-incrementalnewlinedecoder_reset(nldecoder_object *self, PyObject *args)
+_io_IncrementalNewlineDecoder_reset_impl(nldecoder_object *self)
+/*[clinic end generated code: output=32fa40c7462aa8ff input=728678ddaea776df]*/
{
self->seennl = 0;
self->pendingcr = 0;
@@ -591,95 +622,8 @@ incrementalnewlinedecoder_newlines_get(nldecoder_object *self, void *context)
}
-
-static PyMethodDef incrementalnewlinedecoder_methods[] = {
- {"decode", (PyCFunction)incrementalnewlinedecoder_decode, METH_VARARGS|METH_KEYWORDS},
- {"getstate", (PyCFunction)incrementalnewlinedecoder_getstate, METH_NOARGS},
- {"setstate", (PyCFunction)incrementalnewlinedecoder_setstate, METH_O},
- {"reset", (PyCFunction)incrementalnewlinedecoder_reset, METH_NOARGS},
- {NULL}
-};
-
-static PyGetSetDef incrementalnewlinedecoder_getset[] = {
- {"newlines", (getter)incrementalnewlinedecoder_newlines_get, NULL, NULL},
- {NULL}
-};
-
-PyTypeObject PyIncrementalNewlineDecoder_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_io.IncrementalNewlineDecoder", /*tp_name*/
- sizeof(nldecoder_object), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- (destructor)incrementalnewlinedecoder_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_compare */
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash */
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
- incrementalnewlinedecoder_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /*tp_weaklistoffset*/
- 0, /* tp_iter */
- 0, /* tp_iternext */
- incrementalnewlinedecoder_methods, /* tp_methods */
- 0, /* tp_members */
- incrementalnewlinedecoder_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)incrementalnewlinedecoder_init, /* tp_init */
- 0, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
-};
-
-
/* TextIOWrapper */
-PyDoc_STRVAR(textiowrapper_doc,
- "Character and line based layer over a BufferedIOBase object, buffer.\n"
- "\n"
- "encoding gives the name of the encoding that the stream will be\n"
- "decoded or encoded with. It defaults to locale.getpreferredencoding(False).\n"
- "\n"
- "errors determines the strictness of encoding and decoding (see\n"
- "help(codecs.Codec) or the documentation for codecs.register) and\n"
- "defaults to \"strict\".\n"
- "\n"
- "newline controls how line endings are handled. It can be None, '',\n"
- "'\\n', '\\r', and '\\r\\n'. It works as follows:\n"
- "\n"
- "* On input, if newline is None, universal newlines mode is\n"
- " enabled. Lines in the input can end in '\\n', '\\r', or '\\r\\n', and\n"
- " these are translated into '\\n' before being returned to the\n"
- " caller. If it is '', universal newline mode is enabled, but line\n"
- " endings are returned to the caller untranslated. If it has any of\n"
- " the other legal values, input lines are only terminated by the given\n"
- " string, and the line ending is returned to the caller untranslated.\n"
- "\n"
- "* On output, if newline is None, any '\\n' characters written are\n"
- " translated to the system default line separator, os.linesep. If\n"
- " newline is '' or '\\n', no translation takes place. If newline is any\n"
- " of the other legal values, any '\\n' characters written are translated\n"
- " to the given string.\n"
- "\n"
- "If line_buffering is True, a call to flush is implied when a call to\n"
- "write contains a newline character."
- );
-
typedef PyObject *
(*encodefunc_t)(PyObject *, PyObject *);
@@ -742,7 +686,6 @@ typedef struct
PyObject *dict;
} textio;
-
/* A couple of specialized cases in order to bypass the slow incremental
encoding methods for the most popular encodings. */
@@ -843,28 +786,59 @@ static encodefuncentry encodefuncs[] = {
};
+/*[clinic input]
+_io.TextIOWrapper.__init__
+ buffer: object
+ encoding: str(accept={str, NoneType}) = NULL
+ errors: str(accept={str, NoneType}) = NULL
+ newline: str(accept={str, NoneType}) = NULL
+ line_buffering: int(c_default="0") = False
+ write_through: int(c_default="0") = False
+
+Character and line based layer over a BufferedIOBase object, buffer.
+
+encoding gives the name of the encoding that the stream will be
+decoded or encoded with. It defaults to locale.getpreferredencoding(False).
+
+errors determines the strictness of encoding and decoding (see
+help(codecs.Codec) or the documentation for codecs.register) and
+defaults to "strict".
+
+newline controls how line endings are handled. It can be None, '',
+'\n', '\r', and '\r\n'. It works as follows:
+
+* On input, if newline is None, universal newlines mode is
+ enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
+ these are translated into '\n' before being returned to the
+ caller. If it is '', universal newline mode is enabled, but line
+ endings are returned to the caller untranslated. If it has any of
+ the other legal values, input lines are only terminated by the given
+ string, and the line ending is returned to the caller untranslated.
+
+* On output, if newline is None, any '\n' characters written are
+ translated to the system default line separator, os.linesep. If
+ newline is '' or '\n', no translation takes place. If newline is any
+ of the other legal values, any '\n' characters written are translated
+ to the given string.
+
+If line_buffering is True, a call to flush is implied when a call to
+write contains a newline character.
+[clinic start generated code]*/
+
static int
-textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
-{
- char *kwlist[] = {"buffer", "encoding", "errors",
- "newline", "line_buffering", "write_through",
- NULL};
- PyObject *buffer, *raw, *codec_info = NULL;
- char *encoding = NULL;
- char *errors = NULL;
- char *newline = NULL;
- int line_buffering = 0, write_through = 0;
+_io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
+ const char *encoding, const char *errors,
+ const char *newline, int line_buffering,
+ int write_through)
+/*[clinic end generated code: output=56a83402ce2a8381 input=3126cb3101a2c99b]*/
+{
+ PyObject *raw, *codec_info = NULL;
_PyIO_State *state = NULL;
-
PyObject *res;
int r;
self->ok = 0;
self->detached = 0;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|zzzii:fileio",
- kwlist, &buffer, &encoding, &errors,
- &newline, &line_buffering, &write_through))
- return -1;
if (newline && newline[0] != '\0'
&& !(newline[0] == '\n' && newline[1] == '\0')
@@ -1244,8 +1218,13 @@ textiowrapper_closed_get(textio *self, void *context);
}
+/*[clinic input]
+_io.TextIOWrapper.detach
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_detach(textio *self)
+_io_TextIOWrapper_detach_impl(textio *self)
+/*[clinic end generated code: output=7ba3715cd032d5f2 input=e5a71fbda9e1d9f9]*/
{
PyObject *buffer, *res;
CHECK_ATTACHED(self);
@@ -1290,25 +1269,26 @@ _textiowrapper_writeflush(textio *self)
return 0;
}
+/*[clinic input]
+_io.TextIOWrapper.write
+ text: unicode
+ /
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_write(textio *self, PyObject *args)
+_io_TextIOWrapper_write_impl(textio *self, PyObject *text)
+/*[clinic end generated code: output=d2deb0d50771fcec input=fdf19153584a0e44]*/
{
PyObject *ret;
- PyObject *text; /* owned reference */
PyObject *b;
Py_ssize_t textlen;
int haslf = 0;
int needflush = 0, text_needflush = 0;
- CHECK_ATTACHED(self);
-
- if (!PyArg_ParseTuple(args, "U:write", &text)) {
- return NULL;
- }
-
if (PyUnicode_READY(text) == -1)
return NULL;
+ CHECK_ATTACHED(self);
CHECK_CLOSED(self);
if (self->encoder == NULL)
@@ -1441,6 +1421,7 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
PyObject *dec_buffer = NULL;
PyObject *dec_flags = NULL;
PyObject *input_chunk = NULL;
+ Py_buffer input_chunk_buf;
PyObject *decoded_chars, *chunk_size;
Py_ssize_t nbytes, nchars;
int eof;
@@ -1468,7 +1449,16 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
/* Given this, we know there was a valid snapshot point
* len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
*/
- if (PyArg_Parse(state, "(OO)", &dec_buffer, &dec_flags) < 0) {
+ if (PyArg_ParseTuple(state, "OO", &dec_buffer, &dec_flags) < 0) {
+ Py_DECREF(state);
+ return -1;
+ }
+
+ if (!PyBytes_Check(dec_buffer)) {
+ PyErr_Format(PyExc_TypeError,
+ "decoder getstate() should have returned a bytes "
+ "object, not '%.200s'",
+ Py_TYPE(dec_buffer)->tp_name);
Py_DECREF(state);
return -1;
}
@@ -1484,23 +1474,24 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
chunk_size = PyLong_FromSsize_t(Py_MAX(self->chunk_size, size_hint));
if (chunk_size == NULL)
goto fail;
+
input_chunk = PyObject_CallMethodObjArgs(self->buffer,
(self->has_read1 ? _PyIO_str_read1: _PyIO_str_read),
chunk_size, NULL);
Py_DECREF(chunk_size);
if (input_chunk == NULL)
goto fail;
- if (!PyBytes_Check(input_chunk)) {
+
+ if (PyObject_GetBuffer(input_chunk, &input_chunk_buf, 0) != 0) {
PyErr_Format(PyExc_TypeError,
- "underlying %s() should have returned a bytes object, "
+ "underlying %s() should have returned a bytes-like object, "
"not '%.200s'", (self->has_read1 ? "read1": "read"),
Py_TYPE(input_chunk)->tp_name);
goto fail;
}
- nbytes = PyBytes_Size(input_chunk);
+ nbytes = input_chunk_buf.len;
eof = (nbytes == 0);
-
if (Py_TYPE(self->decoder) == &PyIncrementalNewlineDecoder_Type) {
decoded_chars = _PyIncrementalNewlineDecoder_decode(
self->decoder, input_chunk, eof);
@@ -1509,6 +1500,7 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
decoded_chars = PyObject_CallMethodObjArgs(self->decoder,
_PyIO_str_decode, input_chunk, eof ? Py_True : Py_False, NULL);
}
+ PyBuffer_Release(&input_chunk_buf);
if (check_decoded(decoded_chars) < 0)
goto fail;
@@ -1525,18 +1517,12 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
/* At the snapshot point, len(dec_buffer) bytes before the read, the
* next input to be decoded is dec_buffer + input_chunk.
*/
- PyObject *next_input = PyNumber_Add(dec_buffer, input_chunk);
- if (next_input == NULL)
- goto fail;
- if (!PyBytes_Check(next_input)) {
- PyErr_Format(PyExc_TypeError,
- "decoder getstate() should have returned a bytes "
- "object, not '%.200s'",
- Py_TYPE(next_input)->tp_name);
- Py_DECREF(next_input);
+ PyObject *next_input = dec_buffer;
+ PyBytes_Concat(&next_input, input_chunk);
+ if (next_input == NULL) {
+ dec_buffer = NULL; /* Reference lost to PyBytes_Concat */
goto fail;
}
- Py_DECREF(dec_buffer);
Py_CLEAR(self->snapshot);
self->snapshot = Py_BuildValue("NN", dec_flags, next_input);
}
@@ -1551,17 +1537,19 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint)
return -1;
}
+/*[clinic input]
+_io.TextIOWrapper.read
+ size as n: io_ssize_t = -1
+ /
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_read(textio *self, PyObject *args)
+_io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
+/*[clinic end generated code: output=7e651ce6cc6a25a6 input=8c09398424085cca]*/
{
- Py_ssize_t n = -1;
PyObject *result = NULL, *chunks = NULL;
CHECK_ATTACHED(self);
-
- if (!PyArg_ParseTuple(args, "|O&:read", &_PyIO_ConvertSsize_t, &n))
- return NULL;
-
CHECK_CLOSED(self);
if (self->decoder == NULL)
@@ -1725,7 +1713,7 @@ _PyIO_find_line_ending(
else {
/* Non-universal mode. */
Py_ssize_t readnl_len = PyUnicode_GET_LENGTH(readnl);
- char *nl = PyUnicode_DATA(readnl);
+ Py_UCS1 *nl = PyUnicode_1BYTE_DATA(readnl);
/* Assume that readnl is an ASCII character. */
assert(PyUnicode_KIND(readnl) == PyUnicode_1BYTE_KIND);
if (readnl_len == 1) {
@@ -1927,16 +1915,18 @@ _textiowrapper_readline(textio *self, Py_ssize_t limit)
return NULL;
}
+/*[clinic input]
+_io.TextIOWrapper.readline
+ size: Py_ssize_t = -1
+ /
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_readline(textio *self, PyObject *args)
+_io_TextIOWrapper_readline_impl(textio *self, Py_ssize_t size)
+/*[clinic end generated code: output=344afa98804e8b25 input=56c7172483b36db6]*/
{
- Py_ssize_t limit = -1;
-
CHECK_ATTACHED(self);
- if (!PyArg_ParseTuple(args, "|n:readline", &limit)) {
- return NULL;
- }
- return _textiowrapper_readline(self, limit);
+ return _textiowrapper_readline(self, size);
}
/* Seek and Tell */
@@ -2068,19 +2058,23 @@ _textiowrapper_encoder_setstate(textio *self, cookie_type *cookie)
self, cookie->start_pos == 0 && cookie->dec_flags == 0);
}
+/*[clinic input]
+_io.TextIOWrapper.seek
+ cookie as cookieObj: object
+ whence: int = 0
+ /
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_seek(textio *self, PyObject *args)
+_io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
+/*[clinic end generated code: output=0a15679764e2d04d input=0458abeb3d7842be]*/
{
- PyObject *cookieObj, *posobj;
+ PyObject *posobj;
cookie_type cookie;
- int whence = 0;
PyObject *res;
int cmp;
CHECK_ATTACHED(self);
-
- if (!PyArg_ParseTuple(args, "O|i:seek", &cookieObj, &whence))
- return NULL;
CHECK_CLOSED(self);
Py_INCREF(cookieObj);
@@ -2252,8 +2246,13 @@ textiowrapper_seek(textio *self, PyObject *args)
}
+/*[clinic input]
+_io.TextIOWrapper.tell
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_tell(textio *self, PyObject *args)
+_io_TextIOWrapper_tell_impl(textio *self)
+/*[clinic end generated code: output=4f168c08bf34ad5f input=9a2caf88c24f9ddf]*/
{
PyObject *res;
PyObject *posobj = NULL;
@@ -2263,7 +2262,6 @@ textiowrapper_tell(textio *self, PyObject *args)
Py_ssize_t skip_bytes, skip_back;
PyObject *saved_state = NULL;
char *input, *input_end;
- char *dec_buffer;
Py_ssize_t dec_buffer_len;
int dec_flags;
@@ -2306,7 +2304,7 @@ textiowrapper_tell(textio *self, PyObject *args)
goto fail;
/* Skip backward to the snapshot point (see _read_chunk). */
- if (!PyArg_Parse(self->snapshot, "(iO)", &cookie.dec_flags, &next_input))
+ if (!PyArg_ParseTuple(self->snapshot, "iO", &cookie.dec_flags, &next_input))
goto fail;
assert (PyBytes_Check(next_input));
@@ -2328,14 +2326,24 @@ textiowrapper_tell(textio *self, PyObject *args)
goto fail;
#define DECODER_GETSTATE() do { \
+ PyObject *dec_buffer; \
PyObject *_state = PyObject_CallMethodObjArgs(self->decoder, \
_PyIO_str_getstate, NULL); \
if (_state == NULL) \
goto fail; \
- if (!PyArg_Parse(_state, "(y#i)", &dec_buffer, &dec_buffer_len, &dec_flags)) { \
+ if (!PyArg_ParseTuple(_state, "Oi", &dec_buffer, &dec_flags)) { \
+ Py_DECREF(_state); \
+ goto fail; \
+ } \
+ if (!PyBytes_Check(dec_buffer)) { \
+ PyErr_Format(PyExc_TypeError, \
+ "decoder getstate() should have returned a bytes " \
+ "object, not '%.200s'", \
+ Py_TYPE(dec_buffer)->tp_name); \
Py_DECREF(_state); \
goto fail; \
} \
+ dec_buffer_len = PyBytes_GET_SIZE(dec_buffer); \
Py_DECREF(_state); \
} while (0)
@@ -2460,16 +2468,19 @@ fail:
return NULL;
}
+/*[clinic input]
+_io.TextIOWrapper.truncate
+ pos: object = None
+ /
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_truncate(textio *self, PyObject *args)
+_io_TextIOWrapper_truncate_impl(textio *self, PyObject *pos)
+/*[clinic end generated code: output=90ec2afb9bb7745f input=56ec8baa65aea377]*/
{
- PyObject *pos = Py_None;
PyObject *res;
CHECK_ATTACHED(self)
- if (!PyArg_ParseTuple(args, "|O:truncate", &pos)) {
- return NULL;
- }
res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_flush, NULL);
if (res == NULL)
@@ -2534,36 +2545,61 @@ error:
/* Inquiries */
+/*[clinic input]
+_io.TextIOWrapper.fileno
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_fileno(textio *self, PyObject *args)
+_io_TextIOWrapper_fileno_impl(textio *self)
+/*[clinic end generated code: output=21490a4c3da13e6c input=c488ca83d0069f9b]*/
{
CHECK_ATTACHED(self);
return _PyObject_CallMethodId(self->buffer, &PyId_fileno, NULL);
}
+/*[clinic input]
+_io.TextIOWrapper.seekable
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_seekable(textio *self, PyObject *args)
+_io_TextIOWrapper_seekable_impl(textio *self)
+/*[clinic end generated code: output=ab223dbbcffc0f00 input=8b005ca06e1fca13]*/
{
CHECK_ATTACHED(self);
return _PyObject_CallMethodId(self->buffer, &PyId_seekable, NULL);
}
+/*[clinic input]
+_io.TextIOWrapper.readable
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_readable(textio *self, PyObject *args)
+_io_TextIOWrapper_readable_impl(textio *self)
+/*[clinic end generated code: output=72ff7ba289a8a91b input=0704ea7e01b0d3eb]*/
{
CHECK_ATTACHED(self);
return _PyObject_CallMethodId(self->buffer, &PyId_readable, NULL);
}
+/*[clinic input]
+_io.TextIOWrapper.writable
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_writable(textio *self, PyObject *args)
+_io_TextIOWrapper_writable_impl(textio *self)
+/*[clinic end generated code: output=a728c71790d03200 input=c41740bc9d8636e8]*/
{
CHECK_ATTACHED(self);
return _PyObject_CallMethodId(self->buffer, &PyId_writable, NULL);
}
+/*[clinic input]
+_io.TextIOWrapper.isatty
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_isatty(textio *self, PyObject *args)
+_io_TextIOWrapper_isatty_impl(textio *self)
+/*[clinic end generated code: output=12be1a35bace882e input=fb68d9f2c99bbfff]*/
{
CHECK_ATTACHED(self);
return _PyObject_CallMethodId(self->buffer, &PyId_isatty, NULL);
@@ -2577,8 +2613,13 @@ textiowrapper_getstate(textio *self, PyObject *args)
return NULL;
}
+/*[clinic input]
+_io.TextIOWrapper.flush
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_flush(textio *self, PyObject *args)
+_io_TextIOWrapper_flush_impl(textio *self)
+/*[clinic end generated code: output=59de9165f9c2e4d2 input=928c60590694ab85]*/
{
CHECK_ATTACHED(self);
CHECK_CLOSED(self);
@@ -2588,8 +2629,13 @@ textiowrapper_flush(textio *self, PyObject *args)
return _PyObject_CallMethodId(self->buffer, &PyId_flush, NULL);
}
+/*[clinic input]
+_io.TextIOWrapper.close
+[clinic start generated code]*/
+
static PyObject *
-textiowrapper_close(textio *self, PyObject *args)
+_io_TextIOWrapper_close_impl(textio *self)
+/*[clinic end generated code: output=056ccf8b4876e4f4 input=9c2114315eae1948]*/
{
PyObject *res;
int r;
@@ -2733,24 +2779,81 @@ textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context)
return 0;
}
+#include "clinic/textio.c.h"
+
+static PyMethodDef incrementalnewlinedecoder_methods[] = {
+ _IO_INCREMENTALNEWLINEDECODER_DECODE_METHODDEF
+ _IO_INCREMENTALNEWLINEDECODER_GETSTATE_METHODDEF
+ _IO_INCREMENTALNEWLINEDECODER_SETSTATE_METHODDEF
+ _IO_INCREMENTALNEWLINEDECODER_RESET_METHODDEF
+ {NULL}
+};
+
+static PyGetSetDef incrementalnewlinedecoder_getset[] = {
+ {"newlines", (getter)incrementalnewlinedecoder_newlines_get, NULL, NULL},
+ {NULL}
+};
+
+PyTypeObject PyIncrementalNewlineDecoder_Type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_io.IncrementalNewlineDecoder", /*tp_name*/
+ sizeof(nldecoder_object), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ (destructor)incrementalnewlinedecoder_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_compare */
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash */
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ _io_IncrementalNewlineDecoder___init____doc__, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /*tp_weaklistoffset*/
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ incrementalnewlinedecoder_methods, /* tp_methods */
+ 0, /* tp_members */
+ incrementalnewlinedecoder_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ _io_IncrementalNewlineDecoder___init__, /* tp_init */
+ 0, /* tp_alloc */
+ PyType_GenericNew, /* tp_new */
+};
+
+
static PyMethodDef textiowrapper_methods[] = {
- {"detach", (PyCFunction)textiowrapper_detach, METH_NOARGS},
- {"write", (PyCFunction)textiowrapper_write, METH_VARARGS},
- {"read", (PyCFunction)textiowrapper_read, METH_VARARGS},
- {"readline", (PyCFunction)textiowrapper_readline, METH_VARARGS},
- {"flush", (PyCFunction)textiowrapper_flush, METH_NOARGS},
- {"close", (PyCFunction)textiowrapper_close, METH_NOARGS},
-
- {"fileno", (PyCFunction)textiowrapper_fileno, METH_NOARGS},
- {"seekable", (PyCFunction)textiowrapper_seekable, METH_NOARGS},
- {"readable", (PyCFunction)textiowrapper_readable, METH_NOARGS},
- {"writable", (PyCFunction)textiowrapper_writable, METH_NOARGS},
- {"isatty", (PyCFunction)textiowrapper_isatty, METH_NOARGS},
+ _IO_TEXTIOWRAPPER_DETACH_METHODDEF
+ _IO_TEXTIOWRAPPER_WRITE_METHODDEF
+ _IO_TEXTIOWRAPPER_READ_METHODDEF
+ _IO_TEXTIOWRAPPER_READLINE_METHODDEF
+ _IO_TEXTIOWRAPPER_FLUSH_METHODDEF
+ _IO_TEXTIOWRAPPER_CLOSE_METHODDEF
+
+ _IO_TEXTIOWRAPPER_FILENO_METHODDEF
+ _IO_TEXTIOWRAPPER_SEEKABLE_METHODDEF
+ _IO_TEXTIOWRAPPER_READABLE_METHODDEF
+ _IO_TEXTIOWRAPPER_WRITABLE_METHODDEF
+ _IO_TEXTIOWRAPPER_ISATTY_METHODDEF
{"__getstate__", (PyCFunction)textiowrapper_getstate, METH_NOARGS},
- {"seek", (PyCFunction)textiowrapper_seek, METH_VARARGS},
- {"tell", (PyCFunction)textiowrapper_tell, METH_NOARGS},
- {"truncate", (PyCFunction)textiowrapper_truncate, METH_VARARGS},
+ _IO_TEXTIOWRAPPER_SEEK_METHODDEF
+ _IO_TEXTIOWRAPPER_TELL_METHODDEF
+ _IO_TEXTIOWRAPPER_TRUNCATE_METHODDEF
{NULL, NULL}
};
@@ -2796,7 +2899,7 @@ PyTypeObject PyTextIOWrapper_Type = {
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
- textiowrapper_doc, /* tp_doc */
+ _io_TextIOWrapper___init____doc__, /* tp_doc */
(traverseproc)textiowrapper_traverse, /* tp_traverse */
(inquiry)textiowrapper_clear, /* tp_clear */
0, /* tp_richcompare */
@@ -2811,7 +2914,7 @@ PyTypeObject PyTextIOWrapper_Type = {
0, /* tp_descr_get */
0, /* tp_descr_set */
offsetof(textio, dict), /*tp_dictoffset*/
- (initproc)textiowrapper_init, /* tp_init */
+ _io_TextIOWrapper___init__, /* tp_init */
0, /* tp_alloc */
PyType_GenericNew, /* tp_new */
0, /* tp_free */