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