Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 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 | /* These functions are used as METH_NOARGS methods, are normally called |
| 23 | * with args=NULL, and return a new reference. |
| 24 | * BUT when args=Py_True is passed, they return a borrowed reference. |
| 25 | */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 26 | extern PyObject* _PyIOBase_check_readable(PyObject *self, PyObject *args); |
| 27 | extern PyObject* _PyIOBase_check_writable(PyObject *self, PyObject *args); |
| 28 | extern PyObject* _PyIOBase_check_seekable(PyObject *self, PyObject *args); |
| 29 | extern PyObject* _PyIOBase_check_closed(PyObject *self, PyObject *args); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 30 | |
| 31 | /* Helper for finalization. |
| 32 | This function will revive an object ready to be deallocated and try to |
| 33 | close() it. It returns 0 if the object can be destroyed, or -1 if it |
| 34 | is alive again. */ |
| 35 | extern int _PyIOBase_finalize(PyObject *self); |
| 36 | |
| 37 | /* Returns true if the given FileIO object is closed. |
| 38 | Doesn't check the argument type, so be careful! */ |
| 39 | extern int _PyFileIO_closed(PyObject *self); |
| 40 | |
| 41 | /* Shortcut to the core of the IncrementalNewlineDecoder.decode method */ |
| 42 | extern PyObject *_PyIncrementalNewlineDecoder_decode( |
| 43 | PyObject *self, PyObject *input, int final); |
| 44 | |
| 45 | /* Finds the first line ending between `start` and `end`. |
| 46 | If found, returns the index after the line ending and doesn't touch |
| 47 | `*consumed`. |
| 48 | If not found, returns -1 and sets `*consumed` to the number of characters |
| 49 | which can be safely put aside until another search. |
| 50 | |
| 51 | NOTE: for performance reasons, `end` must point to a NUL character ('\0'). |
| 52 | Otherwise, the function will scan further and return garbage. */ |
| 53 | extern Py_ssize_t _PyIO_find_line_ending( |
| 54 | int translated, int universal, PyObject *readnl, |
| 55 | Py_UNICODE *start, Py_UNICODE *end, Py_ssize_t *consumed); |
| 56 | |
| 57 | |
| 58 | #define DEFAULT_BUFFER_SIZE (8 * 1024) /* bytes */ |
| 59 | |
| 60 | typedef struct { |
| 61 | PyException_HEAD |
| 62 | PyObject *myerrno; |
| 63 | PyObject *strerror; |
| 64 | PyObject *filename; /* Not used, but part of the IOError object */ |
| 65 | Py_ssize_t written; |
| 66 | } PyBlockingIOErrorObject; |
Benjamin Peterson | 36a30ce | 2009-03-05 21:41:50 +0000 | [diff] [blame] | 67 | PyAPI_DATA(PyObject *) PyExc_BlockingIOError; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * Offset type for positioning. |
| 71 | */ |
| 72 | |
Mark Dickinson | 121fbe4 | 2009-10-27 21:51:51 +0000 | [diff] [blame] | 73 | /* Printing a variable of type off_t correctly and without producing |
| 74 | compiler warnings is surprisingly painful. We identify an integer |
| 75 | type whose size matches off_t and then: (1) cast the off_t to that |
| 76 | integer type and (2) use the appropriate conversion specification |
| 77 | for printf. The cast is necessary: gcc complains about formatting |
| 78 | a long with "%lld" even when both long and long long have the same |
| 79 | precision. */ |
| 80 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 81 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 82 | |
| 83 | /* Windows uses long long for offsets */ |
| 84 | typedef PY_LONG_LONG Py_off_t; |
| 85 | # define PyLong_AsOff_t PyLong_AsLongLong |
| 86 | # define PyLong_FromOff_t PyLong_FromLongLong |
| 87 | # define PY_OFF_T_MAX PY_LLONG_MAX |
| 88 | # define PY_OFF_T_MIN PY_LLONG_MIN |
Mark Dickinson | b4bddf5 | 2009-10-28 07:50:03 +0000 | [diff] [blame^] | 89 | # define PY_PRIdOFF "I64d" /* format to use in printf with type off_t */ |
Mark Dickinson | b7365bc | 2009-10-28 07:25:44 +0000 | [diff] [blame] | 90 | # define PY_OFF_T_COMPAT PY_LONG_LONG /* type compatible with off_t */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 91 | #else |
| 92 | |
| 93 | /* Other platforms use off_t */ |
| 94 | typedef off_t Py_off_t; |
Mark Dickinson | 6576848 | 2009-10-27 18:43:44 +0000 | [diff] [blame] | 95 | #if (HAVE_LONG_LONG && SIZEOF_OFF_T == SIZEOF_LONG_LONG) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 96 | # define PyLong_AsOff_t PyLong_AsLongLong |
| 97 | # define PyLong_FromOff_t PyLong_FromLongLong |
| 98 | # define PY_OFF_T_MAX PY_LLONG_MAX |
| 99 | # define PY_OFF_T_MIN PY_LLONG_MIN |
Mark Dickinson | 6576848 | 2009-10-27 18:43:44 +0000 | [diff] [blame] | 100 | # define PY_PRIdOFF "lld" |
Mark Dickinson | b7365bc | 2009-10-28 07:25:44 +0000 | [diff] [blame] | 101 | # define PY_OFF_T_COMPAT PY_LONG_LONG |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 102 | #elif (SIZEOF_OFF_T == SIZEOF_LONG) |
| 103 | # define PyLong_AsOff_t PyLong_AsLong |
| 104 | # define PyLong_FromOff_t PyLong_FromLong |
| 105 | # define PY_OFF_T_MAX LONG_MAX |
| 106 | # define PY_OFF_T_MIN LONG_MIN |
Mark Dickinson | 6576848 | 2009-10-27 18:43:44 +0000 | [diff] [blame] | 107 | # define PY_PRIdOFF "ld" |
Mark Dickinson | 121fbe4 | 2009-10-27 21:51:51 +0000 | [diff] [blame] | 108 | # define PY_OFF_T_COMPAT long |
Mark Dickinson | 6576848 | 2009-10-27 18:43:44 +0000 | [diff] [blame] | 109 | #elif (SIZEOF_OFF_T == SIZEOF_SIZE_T) |
| 110 | # define PyLong_AsOff_t PyLong_AsSsize_t |
| 111 | # define PyLong_FromOff_t PyLong_FromSsize_t |
| 112 | # define PY_OFF_T_MAX PY_SSIZE_T_MAX |
| 113 | # define PY_OFF_T_MIN PY_SSIZE_T_MIN |
| 114 | # define PY_PRIdOFF "zd" |
Mark Dickinson | 121fbe4 | 2009-10-27 21:51:51 +0000 | [diff] [blame] | 115 | # define PY_OFF_T_COMPAT Py_ssize_t |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 116 | #else |
| 117 | # error off_t does not match either size_t, long, or long long! |
| 118 | #endif |
| 119 | |
| 120 | #endif |
| 121 | |
| 122 | extern Py_off_t PyNumber_AsOff_t(PyObject *item, PyObject *err); |
| 123 | |
| 124 | /* Implementation details */ |
| 125 | |
| 126 | /* IO module structure */ |
| 127 | |
| 128 | extern PyModuleDef _PyIO_Module; |
| 129 | |
| 130 | typedef struct { |
| 131 | int initialized; |
| 132 | PyObject *os_module; |
| 133 | PyObject *locale_module; |
| 134 | |
| 135 | PyObject *unsupported_operation; |
| 136 | } _PyIO_State; |
| 137 | |
| 138 | #define IO_MOD_STATE(mod) ((_PyIO_State *)PyModule_GetState(mod)) |
| 139 | #define IO_STATE IO_MOD_STATE(PyState_FindModule(&_PyIO_Module)) |
| 140 | |
| 141 | extern PyObject *_PyIO_str_close; |
| 142 | extern PyObject *_PyIO_str_closed; |
| 143 | extern PyObject *_PyIO_str_decode; |
| 144 | extern PyObject *_PyIO_str_encode; |
| 145 | extern PyObject *_PyIO_str_fileno; |
| 146 | extern PyObject *_PyIO_str_flush; |
| 147 | extern PyObject *_PyIO_str_getstate; |
| 148 | extern PyObject *_PyIO_str_isatty; |
| 149 | extern PyObject *_PyIO_str_newlines; |
| 150 | extern PyObject *_PyIO_str_nl; |
| 151 | extern PyObject *_PyIO_str_read; |
| 152 | extern PyObject *_PyIO_str_read1; |
| 153 | extern PyObject *_PyIO_str_readable; |
| 154 | extern PyObject *_PyIO_str_readinto; |
| 155 | extern PyObject *_PyIO_str_readline; |
| 156 | extern PyObject *_PyIO_str_reset; |
| 157 | extern PyObject *_PyIO_str_seek; |
| 158 | extern PyObject *_PyIO_str_seekable; |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 159 | extern PyObject *_PyIO_str_setstate; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 160 | extern PyObject *_PyIO_str_tell; |
| 161 | extern PyObject *_PyIO_str_truncate; |
| 162 | extern PyObject *_PyIO_str_writable; |
| 163 | extern PyObject *_PyIO_str_write; |
| 164 | |
| 165 | extern PyObject *_PyIO_empty_str; |
| 166 | extern PyObject *_PyIO_empty_bytes; |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 167 | extern PyObject *_PyIO_zero; |