Branch data Line data Source code
1 : : /* 2 : : * Declarations shared between the different parts of the io module 3 : : */ 4 : : 5 : : #include "exports.h" 6 : : 7 : : #include "pycore_moduleobject.h" // _PyModule_GetState() 8 : : #include "structmember.h" 9 : : 10 : : /* ABCs */ 11 : : extern PyTypeObject PyIOBase_Type; 12 : : extern PyTypeObject PyRawIOBase_Type; 13 : : extern PyTypeObject PyBufferedIOBase_Type; 14 : : extern PyTypeObject PyTextIOBase_Type; 15 : : 16 : : /* Concrete classes */ 17 : : extern PyTypeObject PyIncrementalNewlineDecoder_Type; 18 : : 19 : : /* Type specs */ 20 : : extern PyType_Spec bufferedrandom_spec; 21 : : extern PyType_Spec bufferedreader_spec; 22 : : extern PyType_Spec bufferedrwpair_spec; 23 : : extern PyType_Spec bufferedwriter_spec; 24 : : extern PyType_Spec bytesio_spec; 25 : : extern PyType_Spec fileio_spec; 26 : : extern PyType_Spec stringio_spec; 27 : : extern PyType_Spec textiowrapper_spec; 28 : : 29 : : #ifdef HAVE_WINDOWS_CONSOLE_IO 30 : : extern PyTypeObject PyWindowsConsoleIO_Type; 31 : : #endif /* HAVE_WINDOWS_CONSOLE_IO */ 32 : : 33 : : /* These functions are used as METH_NOARGS methods, are normally called 34 : : * with args=NULL, and return a new reference. 35 : : * BUT when args=Py_True is passed, they return a borrowed reference. 36 : : */ 37 : : extern PyObject* _PyIOBase_check_readable(PyObject *self, PyObject *args); 38 : : extern PyObject* _PyIOBase_check_writable(PyObject *self, PyObject *args); 39 : : extern PyObject* _PyIOBase_check_seekable(PyObject *self, PyObject *args); 40 : : extern PyObject* _PyIOBase_check_closed(PyObject *self, PyObject *args); 41 : : 42 : : /* Helper for finalization. 43 : : This function will revive an object ready to be deallocated and try to 44 : : close() it. It returns 0 if the object can be destroyed, or -1 if it 45 : : is alive again. */ 46 : : extern int _PyIOBase_finalize(PyObject *self); 47 : : 48 : : /* Returns true if the given FileIO object is closed. 49 : : Doesn't check the argument type, so be careful! */ 50 : : extern int _PyFileIO_closed(PyObject *self); 51 : : 52 : : /* Shortcut to the core of the IncrementalNewlineDecoder.decode method */ 53 : : extern PyObject *_PyIncrementalNewlineDecoder_decode( 54 : : PyObject *self, PyObject *input, int final); 55 : : 56 : : /* Finds the first line ending between `start` and `end`. 57 : : If found, returns the index after the line ending and doesn't touch 58 : : `*consumed`. 59 : : If not found, returns -1 and sets `*consumed` to the number of characters 60 : : which can be safely put aside until another search. 61 : : 62 : : NOTE: for performance reasons, `end` must point to a NUL character ('\0'). 63 : : Otherwise, the function will scan further and return garbage. 64 : : 65 : : There are three modes, in order of priority: 66 : : * translated: Only find \n (assume newlines already translated) 67 : : * universal: Use universal newlines algorithm 68 : : * Otherwise, the line ending is specified by readnl, a str object */ 69 : : extern Py_ssize_t _PyIO_find_line_ending( 70 : : int translated, int universal, PyObject *readnl, 71 : : int kind, const char *start, const char *end, Py_ssize_t *consumed); 72 : : 73 : : /* Return 1 if an OSError with errno == EINTR is set (and then 74 : : clears the error indicator), 0 otherwise. 75 : : Should only be called when PyErr_Occurred() is true. 76 : : */ 77 : : extern int _PyIO_trap_eintr(void); 78 : : 79 : : #define DEFAULT_BUFFER_SIZE (8 * 1024) /* bytes */ 80 : : 81 : : /* 82 : : * Offset type for positioning. 83 : : */ 84 : : 85 : : /* Printing a variable of type off_t (with e.g., PyUnicode_FromFormat) 86 : : correctly and without producing compiler warnings is surprisingly painful. 87 : : We identify an integer type whose size matches off_t and then: (1) cast the 88 : : off_t to that integer type and (2) use the appropriate conversion 89 : : specification. The cast is necessary: gcc complains about formatting a 90 : : long with "%lld" even when both long and long long have the same 91 : : precision. */ 92 : : 93 : : #ifdef MS_WINDOWS 94 : : 95 : : /* Windows uses long long for offsets */ 96 : : typedef long long Py_off_t; 97 : : # define PyLong_AsOff_t PyLong_AsLongLong 98 : : # define PyLong_FromOff_t PyLong_FromLongLong 99 : : # define PY_OFF_T_MAX LLONG_MAX 100 : : # define PY_OFF_T_MIN LLONG_MIN 101 : : # define PY_OFF_T_COMPAT long long /* type compatible with off_t */ 102 : : # define PY_PRIdOFF "lld" /* format to use for that type */ 103 : : 104 : : #else 105 : : 106 : : /* Other platforms use off_t */ 107 : : typedef off_t Py_off_t; 108 : : #if (SIZEOF_OFF_T == SIZEOF_SIZE_T) 109 : : # define PyLong_AsOff_t PyLong_AsSsize_t 110 : : # define PyLong_FromOff_t PyLong_FromSsize_t 111 : : # define PY_OFF_T_MAX PY_SSIZE_T_MAX 112 : : # define PY_OFF_T_MIN PY_SSIZE_T_MIN 113 : : # define PY_OFF_T_COMPAT Py_ssize_t 114 : : # define PY_PRIdOFF "zd" 115 : : #elif (SIZEOF_OFF_T == SIZEOF_LONG_LONG) 116 : : # define PyLong_AsOff_t PyLong_AsLongLong 117 : : # define PyLong_FromOff_t PyLong_FromLongLong 118 : : # define PY_OFF_T_MAX LLONG_MAX 119 : : # define PY_OFF_T_MIN LLONG_MIN 120 : : # define PY_OFF_T_COMPAT long long 121 : : # define PY_PRIdOFF "lld" 122 : : #elif (SIZEOF_OFF_T == SIZEOF_LONG) 123 : : # define PyLong_AsOff_t PyLong_AsLong 124 : : # define PyLong_FromOff_t PyLong_FromLong 125 : : # define PY_OFF_T_MAX LONG_MAX 126 : : # define PY_OFF_T_MIN LONG_MIN 127 : : # define PY_OFF_T_COMPAT long 128 : : # define PY_PRIdOFF "ld" 129 : : #else 130 : : # error off_t does not match either size_t, long, or long long! 131 : : #endif 132 : : 133 : : #endif 134 : : 135 : : extern Py_off_t PyNumber_AsOff_t(PyObject *item, PyObject *err); 136 : : 137 : : /* Implementation details */ 138 : : 139 : : /* IO module structure */ 140 : : 141 : : extern PyModuleDef _PyIO_Module; 142 : : 143 : : typedef struct { 144 : : int initialized; 145 : : PyObject *locale_module; 146 : : 147 : : PyObject *unsupported_operation; 148 : : 149 : : /* Types */ 150 : : PyTypeObject *PyBufferedRWPair_Type; 151 : : PyTypeObject *PyBufferedRandom_Type; 152 : : PyTypeObject *PyBufferedReader_Type; 153 : : PyTypeObject *PyBufferedWriter_Type; 154 : : PyTypeObject *PyBytesIO_Type; 155 : : PyTypeObject *PyFileIO_Type; 156 : : PyTypeObject *PyStringIO_Type; 157 : : PyTypeObject *PyTextIOWrapper_Type; 158 : : } _PyIO_State; 159 : : 160 : : #define IO_MOD_STATE(mod) ((_PyIO_State *)PyModule_GetState(mod)) 161 : : #define IO_STATE() _PyIO_get_module_state() 162 : : 163 : : static inline _PyIO_State * 164 : 3934 : get_io_state(PyObject *module) 165 : : { 166 : 3934 : void *state = _PyModule_GetState(module); 167 : : assert(state != NULL); 168 : 3934 : return (_PyIO_State *)state; 169 : : } 170 : : 171 : : static inline _PyIO_State * 172 : 2105 : find_io_state_by_def(PyTypeObject *type) 173 : : { 174 : 2105 : PyObject *mod = PyType_GetModuleByDef(type, &_PyIO_Module); 175 : : assert(mod != NULL); 176 : 2105 : return get_io_state(mod); 177 : : } 178 : : 179 : : extern _PyIO_State *_PyIO_get_module_state(void); 180 : : 181 : : #ifdef HAVE_WINDOWS_CONSOLE_IO 182 : : extern char _PyIO_get_console_type(PyObject *); 183 : : #endif 184 : : 185 : : extern Py_EXPORTED_SYMBOL PyTypeObject _PyBytesIOBuffer_Type;