_iomodule.h 6.6 KB

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