Home | History | Annotate | Download | only in _io
      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 extern int _PyIO_ConvertSsize_t(PyObject *, void *);
     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  */
     36 extern PyObject* _PyIOBase_check_readable(PyObject *self, PyObject *args);
     37 extern PyObject* _PyIOBase_check_writable(PyObject *self, PyObject *args);
     38 extern PyObject* _PyIOBase_check_seekable(PyObject *self, PyObject *args);
     39 extern 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. */
     45 extern 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! */
     49 extern int _PyFileIO_closed(PyObject *self);
     50 
     51 /* Shortcut to the core of the IncrementalNewlineDecoder.decode method */
     52 extern 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 */
     68 extern 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 EnvironmentError 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 */
     76 extern 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 */
     95 typedef 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 */
    106 typedef 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 
    134 extern Py_off_t PyNumber_AsOff_t(PyObject *item, PyObject *err);
    135 
    136 /* Implementation details */
    137 
    138 /* IO module structure */
    139 
    140 extern PyModuleDef _PyIO_Module;
    141 
    142 typedef 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 
    152 extern _PyIO_State *_PyIO_get_module_state(void);
    153 extern PyObject *_PyIO_get_locale_module(_PyIO_State *);
    154 
    155 #ifdef MS_WINDOWS
    156 extern char _PyIO_get_console_type(PyObject *);
    157 #endif
    158 
    159 extern PyObject *_PyIO_str_close;
    160 extern PyObject *_PyIO_str_closed;
    161 extern PyObject *_PyIO_str_decode;
    162 extern PyObject *_PyIO_str_encode;
    163 extern PyObject *_PyIO_str_fileno;
    164 extern PyObject *_PyIO_str_flush;
    165 extern PyObject *_PyIO_str_getstate;
    166 extern PyObject *_PyIO_str_isatty;
    167 extern PyObject *_PyIO_str_newlines;
    168 extern PyObject *_PyIO_str_nl;
    169 extern PyObject *_PyIO_str_read;
    170 extern PyObject *_PyIO_str_read1;
    171 extern PyObject *_PyIO_str_readable;
    172 extern PyObject *_PyIO_str_readall;
    173 extern PyObject *_PyIO_str_readinto;
    174 extern PyObject *_PyIO_str_readline;
    175 extern PyObject *_PyIO_str_reset;
    176 extern PyObject *_PyIO_str_seek;
    177 extern PyObject *_PyIO_str_seekable;
    178 extern PyObject *_PyIO_str_setstate;
    179 extern PyObject *_PyIO_str_tell;
    180 extern PyObject *_PyIO_str_truncate;
    181 extern PyObject *_PyIO_str_writable;
    182 extern PyObject *_PyIO_str_write;
    183 
    184 extern PyObject *_PyIO_empty_str;
    185 extern PyObject *_PyIO_empty_bytes;
    186 extern PyObject *_PyIO_zero;
    187 
    188 extern PyTypeObject _PyBytesIOBuffer_Type;
    189