8000 gh-101819: Adapt _io types to heap types, batch 1 by erlend-aasland · Pull Request #101949 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-101819: Adapt _io types to heap types, batch 1 #101949

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

Merged
merged 20 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Avoid PyState_FindModule as far as possible for now
  • Loading branch information
erlend-aasland committed Feb 16, 2023
commit 254bfaf3db42db6000170c3a9fabc0aacb6af2df
8 changes: 8 additions & 0 deletions Modules/_io/_iomodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ get_io_state(PyObject *module)
return (_PyIO_State *)state;
}

static inline _PyIO_State *
find_io_state_by_def(PyTypeObject *type)
{
PyObject *mod = PyType_GetModuleByDef(type, &_PyIO_Module);
assert(mod != NULL);
return get_io_state(mod);
}

extern _PyIO_State *_PyIO_get_module_state(void);

#ifdef MS_WINDOWS
Expand Down
12 changes: 6 additions & 6 deletions Modules/_io/bufferedio.c
"On", reader, buffer_size);
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,7 @@ buffered_iternext(buffered *self)

CHECK_INITIALIZED(self);

_PyIO_State *state = IO_STATE();
_PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
tp = Py_TYPE(self);
if (Py_IS_TYPE(tp, state->PyBufferedReader_Type) ||
Py_IS_TYPE(tp, state->PyBufferedRandom_Type))
Expand Down Expand Up @@ -1433,7 +1433,7 @@ _io_BufferedReader___init___impl(buffered *self, PyObject *raw,
return -1;
_bufferedreader_reset_buf(self);

_PyIO_State *state = IO_STATE();
_PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
self->fast_closed_checks = (
Py_IS_TYPE(self, state->PyBufferedReader_Type) &&
Py_IS_TYPE(raw, state->PyFileIO_Type)
Expand Down Expand Up @@ -1791,7 +1791,7 @@ _io_BufferedWriter___init___impl(buffered *self, PyObject *raw,
_bufferedwriter_reset_buf(self);
self->pos = 0;

_PyIO_State *state = IO_STATE();
_PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
self->fast_closed_checks = (
Py_IS_TYPE(self, state->PyBufferedWriter_Type) &&
Py_IS_TYPE(raw, state->PyFileIO_Type)
Expand Down Expand Up @@ -2101,7 +2101,7 @@ _io_BufferedRWPair___init___impl(rwpair *self, PyObject *reader,
if (_PyIOBase_check_writable(writer, Py_True) == NULL)
return -1;

_PyIO_State *state = IO_STATE();
_PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
self->reader = (buffered *) PyObject_CallFunction(
(PyObject *)state->PyBufferedReader_Type,
Expand Down Expand Up @@ -2311,15 +2311,15 @@ _io_BufferedRandom___init___impl(buffered *self, PyObject *raw,
_bufferedwriter_reset_buf(self);
self->pos = 0;

_PyIO_State *state = IO_STATE();
_PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
self->fast_closed_checks = (Py_IS_TYPE(self, state->PyBufferedRandom_Type) &&
Py_IS_TYPE(raw, state->PyFileIO_Type));

self->ok = 1;
return 0;
}

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

Expand Down
2 changes: 1 addition & 1 deletion Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ bytesio_clear(bytesio *self)
}


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

Expand Down
2 changes: 1 addition & 1 deletion Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
int fstat_result;
int async_err = 0;

_PyIO_State *state = IO_STATE();
_PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
assert(PyFileIO_Check(state, self));
if (self->fd >= 0) {
if (self->closefd) {
Expand Down
8 changes: 4 additions & 4 deletions Modules/_io/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ typedef struct {

PyObject *dict;
PyObject *weakreflist;
_PyIO_State *module_state;
} stringio;

static int _io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwargs);
Expand Down Expand Up @@ -401,8 +402,7 @@ stringio_iternext(stringio *self)
CHECK_CLOSED(self);
ENSURE_REALIZED(self);

_PyIO_State *state = IO_STATE();
if (Py_IS_TYPE(self, state->PyStringIO_Type)) {
if (Py_IS_TYPE(self, self->module_state->PyStringIO_Type)) {
/* Skip method call overhead for speed */
line = _stringio_readline(self, -1);
}
Expand Down Expand Up @@ -750,7 +750,7 @@ _io_StringIO___init___impl(stringio *self, PyObject *value,
self->state = STATE_ACCUMULATING;
}
self->pos = 0;

self->module_state = find_io_state_by_def(Py_TYPE(self));
self->closed = 0;
self->ok = 1;
return 0;
Expand Down Expand Up @@ -968,7 +968,7 @@ stringio_newlines(stringio *self, void *context)
return PyObject_GetAttr(self->decoder, &_Py_ID(newlines));
}

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

Expand Down
7 changes: 3 additions & 4 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
/* Finished sorting out the codec details */
Py_CLEAR(codec_info);

_PyIO_State *state = IO_STATE();
_PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
if (Py_IS_TYPE(buffer, state->PyBufferedReader_Type) ||
Py_IS_TYPE(buffer, state->PyBufferedWriter_Type) ||
Py_IS_TYPE(buffer, state->PyBufferedRandom_Type))
Expand Down Expand Up @@ -3060,8 +3060,7 @@ textiowrapper_iternext(textio *self)
CHECK_ATTACHED(self);

self->telling = 0;
_PyIO_State *state = IO_STATE();
if (Py_IS_TYPE(self, state->PyTextIOWrapper_Type)) {
if (Py_IS_TYPE(self, self->state->PyTextIOWrapper_Type)) {
/* Skip method call overhead for speed */
line = _textiowrapper_readline(self, -1);
}
Expand Down Expand Up @@ -3153,7 +3152,7 @@ textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context)
return 0;
}

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

Expand Down
0