blob: 1383ffc6c850101df7d5b068c2fbdcf95a8f71e7 [file] [log] [blame]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001/*
2 * Declarations shared between the different parts of the io module
3 */
4
5/* ABCs */
6extern PyTypeObject PyIOBase_Type;
7extern PyTypeObject PyRawIOBase_Type;
8extern PyTypeObject PyBufferedIOBase_Type;
9extern PyTypeObject PyTextIOBase_Type;
10
11/* Concrete classes */
12extern PyTypeObject PyFileIO_Type;
13extern PyTypeObject PyBytesIO_Type;
14extern PyTypeObject PyStringIO_Type;
15extern PyTypeObject PyBufferedReader_Type;
16extern PyTypeObject PyBufferedWriter_Type;
17extern PyTypeObject PyBufferedRWPair_Type;
18extern PyTypeObject PyBufferedRandom_Type;
19extern PyTypeObject PyTextIOWrapper_Type;
20extern 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 Peterson680bf1a2009-06-12 02:07:12 +000026extern PyObject* _PyIOBase_check_readable(PyObject *self, PyObject *args);
27extern PyObject* _PyIOBase_check_writable(PyObject *self, PyObject *args);
28extern PyObject* _PyIOBase_check_seekable(PyObject *self, PyObject *args);
29extern PyObject* _PyIOBase_check_closed(PyObject *self, PyObject *args);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000030
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. */
35extern 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! */
39extern int _PyFileIO_closed(PyObject *self);
40
41/* Shortcut to the core of the IncrementalNewlineDecoder.decode method */
42extern 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. */
53extern 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
60typedef 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 Peterson36a30ce2009-03-05 21:41:50 +000067PyAPI_DATA(PyObject *) PyExc_BlockingIOError;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000068
69/*
70 * Offset type for positioning.
71 */
72
Mark Dickinson121fbe42009-10-27 21:51:51 +000073/* 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 Peterson4fa88fa2009-03-04 00:14:51 +000081#if defined(MS_WIN64) || defined(MS_WINDOWS)
82
83/* Windows uses long long for offsets */
84typedef 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 Dickinsonb4bddf52009-10-28 07:50:03 +000089# define PY_PRIdOFF "I64d" /* format to use in printf with type off_t */
Mark Dickinsonb7365bc2009-10-28 07:25:44 +000090# define PY_OFF_T_COMPAT PY_LONG_LONG /* type compatible with off_t */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000091#else
92
93/* Other platforms use off_t */
94typedef off_t Py_off_t;
Mark Dickinson65768482009-10-27 18:43:44 +000095#if (HAVE_LONG_LONG && SIZEOF_OFF_T == SIZEOF_LONG_LONG)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000096# 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 Dickinson65768482009-10-27 18:43:44 +0000100# define PY_PRIdOFF "lld"
Mark Dickinsonb7365bc2009-10-28 07:25:44 +0000101# define PY_OFF_T_COMPAT PY_LONG_LONG
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000102#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 Dickinson65768482009-10-27 18:43:44 +0000107# define PY_PRIdOFF "ld"
Mark Dickinson121fbe42009-10-27 21:51:51 +0000108# define PY_OFF_T_COMPAT long
Mark Dickinson65768482009-10-27 18:43:44 +0000109#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 Dickinson121fbe42009-10-27 21:51:51 +0000115# define PY_OFF_T_COMPAT Py_ssize_t
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000116#else
117# error off_t does not match either size_t, long, or long long!
118#endif
119
120#endif
121
122extern Py_off_t PyNumber_AsOff_t(PyObject *item, PyObject *err);
123
124/* Implementation details */
125
126/* IO module structure */
127
128extern PyModuleDef _PyIO_Module;
129
130typedef 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
141extern PyObject *_PyIO_str_close;
142extern PyObject *_PyIO_str_closed;
143extern PyObject *_PyIO_str_decode;
144extern PyObject *_PyIO_str_encode;
145extern PyObject *_PyIO_str_fileno;
146extern PyObject *_PyIO_str_flush;
147extern PyObject *_PyIO_str_getstate;
148extern PyObject *_PyIO_str_isatty;
149extern PyObject *_PyIO_str_newlines;
150extern PyObject *_PyIO_str_nl;
151extern PyObject *_PyIO_str_read;
152extern PyObject *_PyIO_str_read1;
153extern PyObject *_PyIO_str_readable;
154extern PyObject *_PyIO_str_readinto;
155extern PyObject *_PyIO_str_readline;
156extern PyObject *_PyIO_str_reset;
157extern PyObject *_PyIO_str_seek;
158extern PyObject *_PyIO_str_seekable;
Antoine Pitroue4501852009-05-14 18:55:55 +0000159extern PyObject *_PyIO_str_setstate;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000160extern PyObject *_PyIO_str_tell;
161extern PyObject *_PyIO_str_truncate;
162extern PyObject *_PyIO_str_writable;
163extern PyObject *_PyIO_str_write;
164
165extern PyObject *_PyIO_empty_str;
166extern PyObject *_PyIO_empty_bytes;
Antoine Pitroue4501852009-05-14 18:55:55 +0000167extern PyObject *_PyIO_zero;