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