summaryrefslogtreecommitdiffstats
path: root/Modules/_io/_iomodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_io/_iomodule.c')
-rw-r--r--Modules/_io/_iomodule.c99
1 files changed, 62 insertions, 37 deletions
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
index 811b1d2..55b6535 100644
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -10,7 +10,6 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "_iomodule.h"
-#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#ifdef HAVE_SYS_TYPES_H
@@ -315,8 +314,9 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
}
/* Create the Raw file stream */
+ _PyIO_State *state = get_io_state(module);
{
- PyObject *RawIO_class = (PyObject *)&PyFileIO_Type;
+ PyObject *RawIO_class = (PyObject *)state->PyFileIO_Type;
#ifdef MS_WINDOWS
const PyConfig *config = _Py_GetConfig();
if (!config->legacy_windows_stdio && _PyIO_get_console_type(path_or_fd) != '\0') {
@@ -390,12 +390,15 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
{
PyObject *Buffered_class;
- if (updating)
- Buffered_class = (PyObject *)&PyBufferedRandom_Type;
- else if (creating || writing || appending)
- Buffered_class = (PyObject *)&PyBufferedWriter_Type;
- else if (reading)
- Buffered_class = (PyObject *)&PyBufferedReader_Type;
+ if (updating) {
+ Buffered_class = (PyObject *)state->PyBufferedRandom_Type;
+ }
+ else if (creating || writing || appending) {
+ Buffered_class = (PyObject *)state->PyBufferedWriter_Type;
+ }
+ else if (reading) {
+ Buffered_class = (PyObject *)state->PyBufferedReader_Type;
+ }
else {
PyErr_Format(PyExc_ValueError,
"unknown mode: '%s'", mode);
@@ -417,7 +420,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
}
/* wraps into a TextIOWrapper */
- wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type,
+ wrapper = PyObject_CallFunction((PyObject *)state->PyTextIOWrapper_Type,
"OsssO",
buffer,
encoding, errors, newline,
@@ -558,14 +561,6 @@ PyNumber_AsOff_t(PyObject *item, PyObject *err)
return result;
}
-static inline _PyIO_State*
-get_io_state(PyObject *module)
-{
- void *state = _PyModule_GetState(module);
- assert(state != NULL);
- return (_PyIO_State *)state;
-}
-
_PyIO_State *
_PyIO_get_module_state(void)
{
@@ -587,6 +582,15 @@ iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
return 0;
Py_VISIT(state->locale_module);
Py_VISIT(state->unsupported_operation);
+
+ Py_VISIT(state->PyBufferedRWPair_Type);
+ Py_VISIT(state->PyBufferedRandom_Type);
+ Py_VISIT(state->PyBufferedReader_Type);
+ Py_VISIT(state->PyBufferedWriter_Type);
+ Py_VISIT(state->PyBytesIO_Type);
+ Py_VISIT(state->PyFileIO_Type);
+ Py_VISIT(state->PyStringIO_Type);
+ Py_VISIT(state->PyTextIOWrapper_Type);
return 0;
}
@@ -599,6 +603,15 @@ iomodule_clear(PyObject *mod) {
if (state->locale_module != NULL)
Py_CLEAR(state->locale_module);
Py_CLEAR(state->unsupported_operation);
+
+ Py_CLEAR(state->PyBufferedRWPair_Type);
+ Py_CLEAR(state->PyBufferedRandom_Type);
+ Py_CLEAR(state->PyBufferedReader_Type);
+ Py_CLEAR(state->PyBufferedWriter_Type);
+ Py_CLEAR(state->PyBytesIO_Type);
+ Py_CLEAR(state->PyFileIO_Type);
+ Py_CLEAR(state->PyStringIO_Type);
+ Py_CLEAR(state->PyTextIOWrapper_Type);
return 0;
}
@@ -612,7 +625,9 @@ iomodule_free(PyObject *mod) {
* Module definition
*/
+#define clinic_state() (get_io_state(module))
#include "clinic/_iomodule.c.h"
+#undef clinic_state
static PyMethodDef module_methods[] = {
_IO_OPEN_METHODDEF
@@ -644,23 +659,11 @@ static PyTypeObject* static_types[] = {
&PyRawIOBase_Type,
&PyTextIOBase_Type,
- // PyBufferedIOBase_Type(PyIOBase_Type) subclasses
- &PyBytesIO_Type,
- &PyBufferedReader_Type,
- &PyBufferedWriter_Type,
- &PyBufferedRWPair_Type,
- &PyBufferedRandom_Type,
-
// PyRawIOBase_Type(PyIOBase_Type) subclasses
- &PyFileIO_Type,
&_PyBytesIOBuffer_Type,
#ifdef MS_WINDOWS
&PyWindowsConsoleIO_Type,
#endif
-
- // PyTextIOBase_Type(PyIOBase_Type) subclasses
- &PyStringIO_Type,
- &PyTextIOWrapper_Type,
};
@@ -673,6 +676,17 @@ _PyIO_Fini(void)
}
}
+#define ADD_TYPE(module, type, spec, base) \
+do { \
+ type = (PyTypeObject *)PyType_FromModuleAndSpec(module, spec, \
+ (PyObject *)base); \
+ if (type == NULL) { \
+ goto fail; \
+ } \
+ if (PyModule_AddType(module, type) < 0) { \
+ goto fail; \
+ } \
+} while (0)
PyMODINIT_FUNC
PyInit__io(void)
@@ -705,17 +719,9 @@ PyInit__io(void)
}
// Set type base classes
- PyFileIO_Type.tp_base = &PyRawIOBase_Type;
- PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type;
- PyStringIO_Type.tp_base = &PyTextIOBase_Type;
#ifdef MS_WINDOWS
PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type;
#endif
- PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type;
- PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type;
- PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type;
- PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type;
- PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type;
// Add types
for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) {
@@ -725,6 +731,25 @@ PyInit__io(void)
}
}
+ // PyBufferedIOBase_Type(PyIOBase_Type) subclasses
+ ADD_TYPE(m, state->PyBytesIO_Type, &bytesio_spec, &PyBufferedIOBase_Type);
+ ADD_TYPE(m, state->PyBufferedWriter_Type, &bufferedwriter_spec,
+ &PyBufferedIOBase_Type);
+ ADD_TYPE(m, state->PyBufferedReader_Type, &bufferedreader_spec,
+ &PyBufferedIOBase_Type);
+ ADD_TYPE(m, state->PyBufferedRWPair_Type, &bufferedrwpair_spec,
+ &PyBufferedIOBase_Type);
+ ADD_TYPE(m, state->PyBufferedRandom_Type, &bufferedrandom_spec,
+ &PyBufferedIOBase_Type);
+
+ // PyRawIOBase_Type(PyIOBase_Type) subclasses
+ ADD_TYPE(m, state->PyFileIO_Type, &fileio_spec, &PyRawIOBase_Type);
+
+ // PyTextIOBase_Type(PyIOBase_Type) subclasses
+ ADD_TYPE(m, state->PyStringIO_Type, &stringio_spec, &PyTextIOBase_Type);
+ ADD_TYPE(m, state->PyTextIOWrapper_Type, &textiowrapper_spec,
+ &PyTextIOBase_Type);
+
state->initialized = 1;
return m;