Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-101819: Adapt _io.TextIOBase methods to Argument Clinic #104383

Merged
merged 2 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 166 additions & 1 deletion Modules/_io/clinic/textio.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 67 additions & 39 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@
module _io
class _io.IncrementalNewlineDecoder "nldecoder_object *" "clinic_state()->PyIncrementalNewlineDecoder_Type"
class _io.TextIOWrapper "textio *" "clinic_state()->TextIOWrapper_Type"
class _io._TextIOBase "PyObject *" "&PyTextIOBase_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=81f67cf54eaa6001]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=8b7f24fa13bfdd7f]*/

typedef struct nldecoder_object nldecoder_object;
typedef struct textio textio;

#define clinic_state() (find_io_state_by_def(Py_TYPE(self)))
#include "clinic/textio.c.h"
#undef clinic_state

/* TextIOBase */

Expand All @@ -42,52 +50,76 @@ _unsupported(const char *message)
return NULL;
}

PyDoc_STRVAR(textiobase_detach_doc,
"Separate the underlying buffer from the TextIOBase and return it.\n"
"\n"
"After the underlying buffer has been detached, the TextIO is in an\n"
"unusable state.\n"
);
/*[clinic input]
_io._TextIOBase.detach
cls: defining_class
/

Separate the underlying buffer from the TextIOBase and return it.

After the underlying buffer has been detached, the TextIO is in an unusable state.
[clinic start generated code]*/

static PyObject *
textiobase_detach(PyObject *self, PyObject *Py_UNUSED(ignored))
_io__TextIOBase_detach_impl(PyObject *self, PyTypeObject *cls)
/*[clinic end generated code: output=50915f40c609eaa4 input=987ca3640d0a3776]*/
{
return _unsupported("detach");
}

PyDoc_STRVAR(textiobase_read_doc,
"Read at most size characters from stream.\n"
"\n"
"Read from underlying buffer until we have size characters or we hit EOF.\n"
"If size is negative or omitted, read until EOF.\n"
);
/*[clinic input]
_io._TextIOBase.read
cls: defining_class
/
*args: object
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved

Read at most size characters from stream.

Read from underlying buffer until we have size characters or we hit EOF.
If size is negative or omitted, read until EOF.
[clinic start generated code]*/

static PyObject *
textiobase_read(PyObject *self, PyObject *args)
_io__TextIOBase_read_impl(PyObject *self, PyTypeObject *cls, PyObject *args)
/*[clinic end generated code: output=3adf28998831f461 input=cee1e84664a20de0]*/
{
return _unsupported("read");
}

PyDoc_STRVAR(textiobase_readline_doc,
"Read until newline or EOF.\n"
"\n"
"Returns an empty string if EOF is hit immediately.\n"
);
/*[clinic input]
_io._TextIOBase.readline
cls: defining_class
/
*args: object

Read until newline or EOF.

Return an empty string if EOF is hit immediately.
[clinic start generated code]*/

static PyObject *
textiobase_readline(PyObject *self, PyObject *args)
_io__TextIOBase_readline_impl(PyObject *self, PyTypeObject *cls,
PyObject *args)
/*[clinic end generated code: output=3073a948d02319f3 input=58f801259f7ff3ef]*/
{
return _unsupported("readline");
}

PyDoc_STRVAR(textiobase_write_doc,
"Write string to stream.\n"
"Returns the number of characters written (which is always equal to\n"
"the length of the string).\n"
);
/*[clinic input]
_io._TextIOBase.write
cls: defining_class
/
*args: object

Write string to stream.

Return the number of characters written
(which is always equal to the length of the string).
[clinic start generated code]*/

static PyObject *
textiobase_write(PyObject *self, PyObject *args)
_io__TextIOBase_write_impl(PyObject *self, PyTypeObject *cls, PyObject *args)
/*[clinic end generated code: output=5d985eb529472bc4 input=21b6961b5cba9496]*/
{
return _unsupported("write");
}
Expand Down Expand Up @@ -132,10 +164,10 @@ textiobase_errors_get(PyObject *self, void *context)


static PyMethodDef textiobase_methods[] = {
{"detach", textiobase_detach, METH_NOARGS, textiobase_detach_doc},
{"read", textiobase_read, METH_VARARGS, textiobase_read_doc},
{"readline", textiobase_readline, METH_VARARGS, textiobase_readline_doc},
{"write", textiobase_write, METH_VARARGS, textiobase_write_doc},
_IO__TEXTIOBASE_DETACH_METHODDEF
_IO__TEXTIOBASE_READ_METHODDEF
_IO__TEXTIOBASE_READLINE_METHODDEF
_IO__TEXTIOBASE_WRITE_METHODDEF
{NULL, NULL}
};

Expand Down Expand Up @@ -200,14 +232,14 @@ PyTypeObject PyTextIOBase_Type = {

/* IncrementalNewlineDecoder */

typedef struct {
struct nldecoder_object {
PyObject_HEAD
PyObject *decoder;
PyObject *errors;
unsigned int pendingcr: 1;
unsigned int translate: 1;
unsigned int seennl: 3;
} nldecoder_object;
};

/*[clinic input]
_io.IncrementalNewlineDecoder.__init__
Expand Down Expand Up @@ -645,7 +677,7 @@ incrementalnewlinedecoder_newlines_get(nldecoder_object *self, void *context)
typedef PyObject *
(*encodefunc_t)(PyObject *, PyObject *);

typedef struct
struct textio
{
PyObject_HEAD
int ok; /* initialized? */
Expand Down Expand Up @@ -704,7 +736,7 @@ typedef struct
PyObject *dict;

_PyIO_State *state;
} textio;
};

static void
textiowrapper_set_decoded_chars(textio *self, PyObject *chars);
Expand Down Expand Up @@ -3179,10 +3211,6 @@ textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context)
return 0;
}

#define clinic_state() (find_io_state_by_def(Py_TYPE(self)))
#include "clinic/textio.c.h"
#undef clinic_state

static PyMethodDef incrementalnewlinedecoder_methods[] = {
_IO_INCREMENTALNEWLINEDECODER_DECODE_METHODDEF
_IO_INCREMENTALNEWLINEDECODER_GETSTATE_METHODDEF
Expand Down