• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 extern int _PyIO_ConvertSsize_t(PyObject *, void *);
24 
25 /* These functions are used as METH_NOARGS methods, are normally called
26  * with args=NULL, and return a new reference.
27  * BUT when args=Py_True is passed, they return a borrowed reference.
28  */
29 extern PyObject* _PyIOBase_check_readable(PyObject *self, PyObject *args);
30 extern PyObject* _PyIOBase_check_writable(PyObject *self, PyObject *args);
31 extern PyObject* _PyIOBase_check_seekable(PyObject *self, PyObject *args);
32 extern PyObject* _PyIOBase_check_closed(PyObject *self, PyObject *args);
33 
34 /* Helper for finalization.
35    This function will revive an object ready to be deallocated and try to
36    close() it. It returns 0 if the object can be destroyed, or -1 if it
37    is alive again. */
38 extern int _PyIOBase_finalize(PyObject *self);
39 
40 /* Returns true if the given FileIO object is closed.
41    Doesn't check the argument type, so be careful! */
42 extern int _PyFileIO_closed(PyObject *self);
43 
44 /* Shortcut to the core of the IncrementalNewlineDecoder.decode method */
45 extern PyObject *_PyIncrementalNewlineDecoder_decode(
46     PyObject *self, PyObject *input, int final);
47 
48 /* Finds the first line ending between `start` and `end`.
49    If found, returns the index after the line ending and doesn't touch
50    `*consumed`.
51    If not found, returns -1 and sets `*consumed` to the number of characters
52    which can be safely put aside until another search.
53 
54    NOTE: for performance reasons, `end` must point to a NUL character ('\0').
55    Otherwise, the function will scan further and return garbage.
56 
57    There are three modes, in order of priority:
58    * translated: Only find \n (assume newlines already translated)
59    * universal: Use universal newlines algorithm
60    * Otherwise, the line ending is specified by readnl, a str object */
61 extern Py_ssize_t _PyIO_find_line_ending(
62     int translated, int universal, PyObject *readnl,
63     Py_UNICODE *start, Py_UNICODE *end, Py_ssize_t *consumed);
64 
65 /* Return 1 if an EnvironmentError with errno == EINTR is set (and then
66    clears the error indicator), 0 otherwise.
67    Should only be called when PyErr_Occurred() is true.
68 */
69 extern int _PyIO_trap_eintr(void);
70 
71 #define DEFAULT_BUFFER_SIZE (8 * 1024)  /* bytes */
72 
73 typedef struct {
74     /* This is the equivalent of PyException_HEAD in 3.x */
75     PyObject_HEAD
76     PyObject *dict;
77     PyObject *args;
78     PyObject *message;
79 
80     PyObject *myerrno;
81     PyObject *strerror;
82     PyObject *filename; /* Not used, but part of the IOError object */
83     Py_ssize_t written;
84 } PyBlockingIOErrorObject;
85 extern PyObject *PyExc_BlockingIOError;
86 
87 /*
88  * Offset type for positioning.
89  */
90 
91 /* Printing a variable of type off_t (with e.g., PyString_FromFormat)
92    correctly and without producing compiler warnings is surprisingly painful.
93    We identify an integer type whose size matches off_t and then: (1) cast the
94    off_t to that integer type and (2) use the appropriate conversion
95    specification.  The cast is necessary: gcc complains about formatting a
96    long with "%lld" even when both long and long long have the same
97    precision. */
98 
99 #if defined(MS_WIN64) || defined(MS_WINDOWS)
100 
101 /* Windows uses long long for offsets */
102 typedef PY_LONG_LONG Py_off_t;
103 # define PyLong_AsOff_t     PyLong_AsLongLong
104 # define PyLong_FromOff_t   PyLong_FromLongLong
105 # define PY_OFF_T_MAX       PY_LLONG_MAX
106 # define PY_OFF_T_MIN       PY_LLONG_MIN
107 # define PY_OFF_T_COMPAT    PY_LONG_LONG /* type compatible with off_t */
108 # define PY_PRIdOFF         "lld"        /* format to use for that type */
109 
110 #else
111 
112 /* Other platforms use off_t */
113 typedef off_t Py_off_t;
114 #if (SIZEOF_OFF_T == SIZEOF_SIZE_T)
115 # define PyLong_AsOff_t     PyLong_AsSsize_t
116 # define PyLong_FromOff_t   PyLong_FromSsize_t
117 # define PY_OFF_T_MAX       PY_SSIZE_T_MAX
118 # define PY_OFF_T_MIN       PY_SSIZE_T_MIN
119 # define PY_OFF_T_COMPAT    Py_ssize_t
120 # define PY_PRIdOFF         "zd"
121 #elif (HAVE_LONG_LONG && SIZEOF_OFF_T == SIZEOF_LONG_LONG)
122 # define PyLong_AsOff_t     PyLong_AsLongLong
123 # define PyLong_FromOff_t   PyLong_FromLongLong
124 # define PY_OFF_T_MAX       PY_LLONG_MAX
125 # define PY_OFF_T_MIN       PY_LLONG_MIN
126 # define PY_OFF_T_COMPAT    PY_LONG_LONG
127 # define PY_PRIdOFF         "lld"
128 #elif (SIZEOF_OFF_T == SIZEOF_LONG)
129 # define PyLong_AsOff_t     PyLong_AsLong
130 # define PyLong_FromOff_t   PyLong_FromLong
131 # define PY_OFF_T_MAX       LONG_MAX
132 # define PY_OFF_T_MIN       LONG_MIN
133 # define PY_OFF_T_COMPAT    long
134 # define PY_PRIdOFF         "ld"
135 #else
136 # error off_t does not match either size_t, long, or long long!
137 #endif
138 
139 #endif
140 
141 extern Py_off_t PyNumber_AsOff_t(PyObject *item, PyObject *err);
142 
143 /* Implementation details */
144 
145 extern PyObject *_PyIO_os_module;
146 extern PyObject *_PyIO_locale_module;
147 extern PyObject *_PyIO_unsupported_operation;
148 
149 extern PyObject *_PyIO_str_close;
150 extern PyObject *_PyIO_str_closed;
151 extern PyObject *_PyIO_str_decode;
152 extern PyObject *_PyIO_str_encode;
153 extern PyObject *_PyIO_str_fileno;
154 extern PyObject *_PyIO_str_flush;
155 extern PyObject *_PyIO_str_getstate;
156 extern PyObject *_PyIO_str_isatty;
157 extern PyObject *_PyIO_str_newlines;
158 extern PyObject *_PyIO_str_nl;
159 extern PyObject *_PyIO_str_read;
160 extern PyObject *_PyIO_str_read1;
161 extern PyObject *_PyIO_str_readable;
162 extern PyObject *_PyIO_str_readinto;
163 extern PyObject *_PyIO_str_readline;
164 extern PyObject *_PyIO_str_reset;
165 extern PyObject *_PyIO_str_seek;
166 extern PyObject *_PyIO_str_seekable;
167 extern PyObject *_PyIO_str_setstate;
168 extern PyObject *_PyIO_str_tell;
169 extern PyObject *_PyIO_str_truncate;
170 extern PyObject *_PyIO_str_writable;
171 extern PyObject *_PyIO_str_write;
172 
173 extern PyObject *_PyIO_empty_str;
174 extern PyObject *_PyIO_empty_bytes;
175 extern PyObject *_PyIO_zero;
176