_iomodule.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. An implementation of the new I/O lib as defined by PEP 3116 - "New I/O"
  3. Classes defined here: UnsupportedOperation, BlockingIOError.
  4. Functions defined here: open().
  5. Mostly written by Amaury Forgeot d'Arc
  6. */
  7. #define PY_SSIZE_T_CLEAN
  8. #include "Python.h"
  9. #include "_iomodule.h"
  10. #include "pycore_pystate.h" // _PyInterpreterState_GET()
  11. #include "pycore_initconfig.h" // _PyStatus_OK()
  12. #ifdef HAVE_SYS_TYPES_H
  13. #include <sys/types.h>
  14. #endif /* HAVE_SYS_TYPES_H */
  15. #ifdef HAVE_SYS_STAT_H
  16. #include <sys/stat.h>
  17. #endif /* HAVE_SYS_STAT_H */
  18. #ifdef MS_WINDOWS
  19. #include <windows.h>
  20. #endif
  21. PyDoc_STRVAR(module_doc,
  22. "The io module provides the Python interfaces to stream handling. The\n"
  23. "builtin open function is defined in this module.\n"
  24. "\n"
  25. "At the top of the I/O hierarchy is the abstract base class IOBase. It\n"
  26. "defines the basic interface to a stream. Note, however, that there is no\n"
  27. "separation between reading and writing to streams; implementations are\n"
  28. "allowed to raise an OSError if they do not support a given operation.\n"
  29. "\n"
  30. "Extending IOBase is RawIOBase which deals simply with the reading and\n"
  31. "writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide\n"
  32. "an interface to OS files.\n"
  33. "\n"
  34. "BufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its\n"
  35. "subclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer\n"
  36. "streams that are readable, writable, and both respectively.\n"
  37. "BufferedRandom provides a buffered interface to random access\n"
  38. "streams. BytesIO is a simple stream of in-memory bytes.\n"
  39. "\n"
  40. "Another IOBase subclass, TextIOBase, deals with the encoding and decoding\n"
  41. "of streams into text. TextIOWrapper, which extends it, is a buffered text\n"
  42. "interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO\n"
  43. "is an in-memory stream for text.\n"
  44. "\n"
  45. "Argument names are not part of the specification, and only the arguments\n"
  46. "of open() are intended to be used as keyword arguments.\n"
  47. "\n"
  48. "data:\n"
  49. "\n"
  50. "DEFAULT_BUFFER_SIZE\n"
  51. "\n"
  52. " An int containing the default buffer size used by the module's buffered\n"
  53. " I/O classes. open() uses the file's blksize (as obtained by os.stat) if\n"
  54. " possible.\n"
  55. );
  56. /*
  57. * The main open() function
  58. */
  59. /*[clinic input]
  60. module _io
  61. _io.open
  62. file: object
  63. mode: str = "r"
  64. buffering: int = -1
  65. encoding: str(accept={str, NoneType}) = None
  66. errors: str(accept={str, NoneType}) = None
  67. newline: str(accept={str, NoneType}) = None
  68. closefd: bool = True
  69. opener: object = None
  70. Open file and return a stream. Raise OSError upon failure.
  71. file is either a text or byte string giving the name (and the path
  72. if the file isn't in the current working directory) of the file to
  73. be opened or an integer file descriptor of the file to be
  74. wrapped. (If a file descriptor is given, it is closed when the
  75. returned I/O object is closed, unless closefd is set to False.)
  76. mode is an optional string that specifies the mode in which the file
  77. is opened. It defaults to 'r' which means open for reading in text
  78. mode. Other common values are 'w' for writing (truncating the file if
  79. it already exists), 'x' for creating and writing to a new file, and
  80. 'a' for appending (which on some Unix systems, means that all writes
  81. append to the end of the file regardless of the current seek position).
  82. In text mode, if encoding is not specified the encoding used is platform
  83. dependent: locale.getencoding() is called to get the current locale encoding.
  84. (For reading and writing raw bytes use binary mode and leave encoding
  85. unspecified.) The available modes are:
  86. ========= ===============================================================
  87. Character Meaning
  88. --------- ---------------------------------------------------------------
  89. 'r' open for reading (default)
  90. 'w' open for writing, truncating the file first
  91. 'x' create a new file and open it for writing
  92. 'a' open for writing, appending to the end of the file if it exists
  93. 'b' binary mode
  94. 't' text mode (default)
  95. '+' open a disk file for updating (reading and writing)
  96. ========= ===============================================================
  97. The default mode is 'rt' (open for reading text). For binary random
  98. access, the mode 'w+b' opens and truncates the file to 0 bytes, while
  99. 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
  100. raises an `FileExistsError` if the file already exists.
  101. Python distinguishes between files opened in binary and text modes,
  102. even when the underlying operating system doesn't. Files opened in
  103. binary mode (appending 'b' to the mode argument) return contents as
  104. bytes objects without any decoding. In text mode (the default, or when
  105. 't' is appended to the mode argument), the contents of the file are
  106. returned as strings, the bytes having been first decoded using a
  107. platform-dependent encoding or using the specified encoding if given.
  108. buffering is an optional integer used to set the buffering policy.
  109. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
  110. line buffering (only usable in text mode), and an integer > 1 to indicate
  111. the size of a fixed-size chunk buffer. When no buffering argument is
  112. given, the default buffering policy works as follows:
  113. * Binary files are buffered in fixed-size chunks; the size of the buffer
  114. is chosen using a heuristic trying to determine the underlying device's
  115. "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
  116. On many systems, the buffer will typically be 4096 or 8192 bytes long.
  117. * "Interactive" text files (files for which isatty() returns True)
  118. use line buffering. Other text files use the policy described above
  119. for binary files.
  120. encoding is the name of the encoding used to decode or encode the
  121. file. This should only be used in text mode. The default encoding is
  122. platform dependent, but any encoding supported by Python can be
  123. passed. See the codecs module for the list of supported encodings.
  124. errors is an optional string that specifies how encoding errors are to
  125. be handled---this argument should not be used in binary mode. Pass
  126. 'strict' to raise a ValueError exception if there is an encoding error
  127. (the default of None has the same effect), or pass 'ignore' to ignore
  128. errors. (Note that ignoring encoding errors can lead to data loss.)
  129. See the documentation for codecs.register or run 'help(codecs.Codec)'
  130. for a list of the permitted encoding error strings.
  131. newline controls how universal newlines works (it only applies to text
  132. mode). It can be None, '', '\n', '\r', and '\r\n'. It works as
  133. follows:
  134. * On input, if newline is None, universal newlines mode is
  135. enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
  136. these are translated into '\n' before being returned to the
  137. caller. If it is '', universal newline mode is enabled, but line
  138. endings are returned to the caller untranslated. If it has any of
  139. the other legal values, input lines are only terminated by the given
  140. string, and the line ending is returned to the caller untranslated.
  141. * On output, if newline is None, any '\n' characters written are
  142. translated to the system default line separator, os.linesep. If
  143. newline is '' or '\n', no translation takes place. If newline is any
  144. of the other legal values, any '\n' characters written are translated
  145. to the given string.
  146. If closefd is False, the underlying file descriptor will be kept open
  147. when the file is closed. This does not work when a file name is given
  148. and must be True in that case.
  149. A custom opener can be used by passing a callable as *opener*. The
  150. underlying file descriptor for the file object is then obtained by
  151. calling *opener* with (*file*, *flags*). *opener* must return an open
  152. file descriptor (passing os.open as *opener* results in functionality
  153. similar to passing None).
  154. open() returns a file object whose type depends on the mode, and
  155. through which the standard file operations such as reading and writing
  156. are performed. When open() is used to open a file in a text mode ('w',
  157. 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
  158. a file in a binary mode, the returned class varies: in read binary
  159. mode, it returns a BufferedReader; in write binary and append binary
  160. modes, it returns a BufferedWriter, and in read/write mode, it returns
  161. a BufferedRandom.
  162. It is also possible to use a string or bytearray as a file for both
  163. reading and writing. For strings StringIO can be used like a file
  164. opened in a text mode, and for bytes a BytesIO can be used like a file
  165. opened in a binary mode.
  166. [clinic start generated code]*/
  167. static PyObject *
  168. _io_open_impl(PyObject *module, PyObject *file, const char *mode,
  169. int buffering, const char *encoding, const char *errors,
  170. const char *newline, int closefd, PyObject *opener)
  171. /*[clinic end generated code: output=aefafc4ce2b46dc0 input=cd034e7cdfbf4e78]*/
  172. {
  173. unsigned i;
  174. int creating = 0, reading = 0, writing = 0, appending = 0, updating = 0;
  175. int text = 0, binary = 0;
  176. char rawmode[6], *m;
  177. int line_buffering, is_number, isatty = 0;
  178. PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL, *path_or_fd = NULL;
  179. is_number = PyNumber_Check(file);
  180. if (is_number) {
  181. path_or_fd = Py_NewRef(file);
  182. } else {
  183. path_or_fd = PyOS_FSPath(file);
  184. if (path_or_fd == NULL) {
  185. return NULL;
  186. }
  187. }
  188. if (!is_number &&
  189. !PyUnicode_Check(path_or_fd) &&
  190. !PyBytes_Check(path_or_fd)) {
  191. PyErr_Format(PyExc_TypeError, "invalid file: %R", file);
  192. goto error;
  193. }
  194. /* Decode mode */
  195. for (i = 0; i < strlen(mode); i++) {
  196. char c = mode[i];
  197. switch (c) {
  198. case 'x':
  199. creating = 1;
  200. break;
  201. case 'r':
  202. reading = 1;
  203. break;
  204. case 'w':
  205. writing = 1;
  206. break;
  207. case 'a':
  208. appending = 1;
  209. break;
  210. case '+':
  211. updating = 1;
  212. break;
  213. case 't':
  214. text = 1;
  215. break;
  216. case 'b':
  217. binary = 1;
  218. break;
  219. default:
  220. goto invalid_mode;
  221. }
  222. /* c must not be duplicated */
  223. if (strchr(mode+i+1, c)) {
  224. invalid_mode:
  225. PyErr_Format(PyExc_ValueError, "invalid mode: '%s'", mode);
  226. goto error;
  227. }
  228. }
  229. m = rawmode;
  230. if (creating) *(m++) = 'x';
  231. if (reading) *(m++) = 'r';
  232. if (writing) *(m++) = 'w';
  233. if (appending) *(m++) = 'a';
  234. if (updating) *(m++) = '+';
  235. *m = '\0';
  236. /* Parameters validation */
  237. if (text && binary) {
  238. PyErr_SetString(PyExc_ValueError,
  239. "can't have text and binary mode at once");
  240. goto error;
  241. }
  242. if (creating + reading + writing + appending > 1) {
  243. PyErr_SetString(PyExc_ValueError,
  244. "must have exactly one of create/read/write/append mode");
  245. goto error;
  246. }
  247. if (binary && encoding != NULL) {
  248. PyErr_SetString(PyExc_ValueError,
  249. "binary mode doesn't take an encoding argument");
  250. goto error;
  251. }
  252. if (binary && errors != NULL) {
  253. PyErr_SetString(PyExc_ValueError,
  254. "binary mode doesn't take an errors argument");
  255. goto error;
  256. }
  257. if (binary && newline != NULL) {
  258. PyErr_SetString(PyExc_ValueError,
  259. "binary mode doesn't take a newline argument");
  260. goto error;
  261. }
  262. if (binary && buffering == 1) {
  263. if (PyErr_WarnEx(PyExc_RuntimeWarning,
  264. "line buffering (buffering=1) isn't supported in "
  265. "binary mode, the default buffer size will be used",
  266. 1) < 0) {
  267. goto error;
  268. }
  269. }
  270. /* Create the Raw file stream */
  271. _PyIO_State *state = get_io_state(module);
  272. {
  273. PyObject *RawIO_class = (PyObject *)state->PyFileIO_Type;
  274. #ifdef HAVE_WINDOWS_CONSOLE_IO
  275. const PyConfig *config = _Py_GetConfig();
  276. if (!config->legacy_windows_stdio && _PyIO_get_console_type(path_or_fd) != '\0') {
  277. RawIO_class = (PyObject *)state->PyWindowsConsoleIO_Type;
  278. encoding = "utf-8";
  279. }
  280. #endif
  281. raw = PyObject_CallFunction(RawIO_class, "OsOO",
  282. path_or_fd, rawmode,
  283. closefd ? Py_True : Py_False,
  284. opener);
  285. }
  286. if (raw == NULL)
  287. goto error;
  288. result = raw;
  289. Py_SETREF(path_or_fd, NULL);
  290. modeobj = PyUnicode_FromString(mode);
  291. if (modeobj == NULL)
  292. goto error;
  293. /* buffering */
  294. if (buffering < 0) {
  295. PyObject *res = PyObject_CallMethodNoArgs(raw, &_Py_ID(isatty));
  296. if (res == NULL)
  297. goto error;
  298. isatty = PyObject_IsTrue(res);
  299. Py_DECREF(res);
  300. if (isatty < 0)
  301. goto error;
  302. }
  303. if (buffering == 1 || isatty) {
  304. buffering = -1;
  305. line_buffering = 1;
  306. }
  307. else
  308. line_buffering = 0;
  309. if (buffering < 0) {
  310. PyObject *blksize_obj;
  311. blksize_obj = PyObject_GetAttr(raw, &_Py_ID(_blksize));
  312. if (blksize_obj == NULL)
  313. goto error;
  314. buffering = PyLong_AsLong(blksize_obj);
  315. Py_DECREF(blksize_obj);
  316. if (buffering == -1 && PyErr_Occurred())
  317. goto error;
  318. }
  319. if (buffering < 0) {
  320. PyErr_SetString(PyExc_ValueError,
  321. "invalid buffering size");
  322. goto error;
  323. }
  324. /* if not buffering, returns the raw file object */
  325. if (buffering == 0) {
  326. if (!binary) {
  327. PyErr_SetString(PyExc_ValueError,
  328. "can't have unbuffered text I/O");
  329. goto error;
  330. }
  331. Py_DECREF(modeobj);
  332. return result;
  333. }
  334. /* wraps into a buffered file */
  335. {
  336. PyObject *Buffered_class;
  337. if (updating) {
  338. Buffered_class = (PyObject *)state->PyBufferedRandom_Type;
  339. }
  340. else if (creating || writing || appending) {
  341. Buffered_class = (PyObject *)state->PyBufferedWriter_Type;
  342. }
  343. else if (reading) {
  344. Buffered_class = (PyObject *)state->PyBufferedReader_Type;
  345. }
  346. else {
  347. PyErr_Format(PyExc_ValueError,
  348. "unknown mode: '%s'", mode);
  349. goto error;
  350. }
  351. buffer = PyObject_CallFunction(Buffered_class, "Oi", raw, buffering);
  352. }
  353. if (buffer == NULL)
  354. goto error;
  355. result = buffer;
  356. Py_DECREF(raw);
  357. /* if binary, returns the buffered file */
  358. if (binary) {
  359. Py_DECREF(modeobj);
  360. return result;
  361. }
  362. /* wraps into a TextIOWrapper */
  363. wrapper = PyObject_CallFunction((PyObject *)state->PyTextIOWrapper_Type,
  364. "OsssO",
  365. buffer,
  366. encoding, errors, newline,
  367. line_buffering ? Py_True : Py_False);
  368. if (wrapper == NULL)
  369. goto error;
  370. result = wrapper;
  371. Py_DECREF(buffer);
  372. if (PyObject_SetAttr(wrapper, &_Py_ID(mode), modeobj) < 0)
  373. goto error;
  374. Py_DECREF(modeobj);
  375. return result;
  376. error:
  377. if (result != NULL) {
  378. PyObject *exc = PyErr_GetRaisedException();
  379. PyObject *close_result = PyObject_CallMethodNoArgs(result, &_Py_ID(close));
  380. _PyErr_ChainExceptions1(exc);
  381. Py_XDECREF(close_result);
  382. Py_DECREF(result);
  383. }
  384. Py_XDECREF(path_or_fd);
  385. Py_XDECREF(modeobj);
  386. return NULL;
  387. }
  388. /*[clinic input]
  389. _io.text_encoding
  390. encoding: object
  391. stacklevel: int = 2
  392. /
  393. A helper function to choose the text encoding.
  394. When encoding is not None, this function returns it.
  395. Otherwise, this function returns the default text encoding
  396. (i.e. "locale" or "utf-8" depends on UTF-8 mode).
  397. This function emits an EncodingWarning if encoding is None and
  398. sys.flags.warn_default_encoding is true.
  399. This can be used in APIs with an encoding=None parameter.
  400. However, please consider using encoding="utf-8" for new APIs.
  401. [clinic start generated code]*/
  402. static PyObject *
  403. _io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel)
  404. /*[clinic end generated code: output=91b2cfea6934cc0c input=4999aa8b3d90f3d4]*/
  405. {
  406. if (encoding == NULL || encoding == Py_None) {
  407. PyInterpreterState *interp = _PyInterpreterState_GET();
  408. if (_PyInterpreterState_GetConfig(interp)->warn_default_encoding) {
  409. if (PyErr_WarnEx(PyExc_EncodingWarning,
  410. "'encoding' argument not specified", stacklevel)) {
  411. return NULL;
  412. }
  413. }
  414. const PyPreConfig *preconfig = &_PyRuntime.preconfig;
  415. if (preconfig->utf8_mode) {
  416. _Py_DECLARE_STR(utf_8, "utf-8");
  417. encoding = &_Py_STR(utf_8);
  418. }
  419. else {
  420. encoding = &_Py_ID(locale);
  421. }
  422. }
  423. return Py_NewRef(encoding);
  424. }
  425. /*[clinic input]
  426. _io.open_code
  427. path : unicode
  428. Opens the provided file with the intent to import the contents.
  429. This may perform extra validation beyond open(), but is otherwise interchangeable
  430. with calling open(path, 'rb').
  431. [clinic start generated code]*/
  432. static PyObject *
  433. _io_open_code_impl(PyObject *module, PyObject *path)
  434. /*[clinic end generated code: output=2fe4ecbd6f3d6844 input=f5c18e23f4b2ed9f]*/
  435. {
  436. return PyFile_OpenCodeObject(path);
  437. }
  438. /*
  439. * Private helpers for the io module.
  440. */
  441. Py_off_t
  442. PyNumber_AsOff_t(PyObject *item, PyObject *err)
  443. {
  444. Py_off_t result;
  445. PyObject *runerr;
  446. PyObject *value = _PyNumber_Index(item);
  447. if (value == NULL)
  448. return -1;
  449. /* We're done if PyLong_AsSsize_t() returns without error. */
  450. result = PyLong_AsOff_t(value);
  451. if (result != -1 || !(runerr = PyErr_Occurred()))
  452. goto finish;
  453. /* Error handling code -- only manage OverflowError differently */
  454. if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
  455. goto finish;
  456. PyErr_Clear();
  457. /* If no error-handling desired then the default clipping
  458. is sufficient.
  459. */
  460. if (!err) {
  461. assert(PyLong_Check(value));
  462. /* Whether or not it is less than or equal to
  463. zero is determined by the sign of ob_size
  464. */
  465. if (_PyLong_Sign(value) < 0)
  466. result = PY_OFF_T_MIN;
  467. else
  468. result = PY_OFF_T_MAX;
  469. }
  470. else {
  471. /* Otherwise replace the error with caller's error object. */
  472. PyErr_Format(err,
  473. "cannot fit '%.200s' into an offset-sized integer",
  474. Py_TYPE(item)->tp_name);
  475. }
  476. finish:
  477. Py_DECREF(value);
  478. return result;
  479. }
  480. static int
  481. iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
  482. _PyIO_State *state = get_io_state(mod);
  483. Py_VISIT(state->unsupported_operation);
  484. Py_VISIT(state->PyIOBase_Type);
  485. Py_VISIT(state->PyIncrementalNewlineDecoder_Type);
  486. Py_VISIT(state->PyRawIOBase_Type);
  487. Py_VISIT(state->PyBufferedIOBase_Type);
  488. Py_VISIT(state->PyBufferedRWPair_Type);
  489. Py_VISIT(state->PyBufferedRandom_Type);
  490. Py_VISIT(state->PyBufferedReader_Type);
  491. Py_VISIT(state->PyBufferedWriter_Type);
  492. Py_VISIT(state->PyBytesIOBuffer_Type);
  493. Py_VISIT(state->PyBytesIO_Type);
  494. Py_VISIT(state->PyFileIO_Type);
  495. Py_VISIT(state->PyStringIO_Type);
  496. Py_VISIT(state->PyTextIOBase_Type);
  497. Py_VISIT(state->PyTextIOWrapper_Type);
  498. #ifdef HAVE_WINDOWS_CONSOLE_IO
  499. Py_VISIT(state->PyWindowsConsoleIO_Type);
  500. #endif
  501. return 0;
  502. }
  503. static int
  504. iomodule_clear(PyObject *mod) {
  505. _PyIO_State *state = get_io_state(mod);
  506. Py_CLEAR(state->unsupported_operation);
  507. Py_CLEAR(state->PyIOBase_Type);
  508. Py_CLEAR(state->PyIncrementalNewlineDecoder_Type);
  509. Py_CLEAR(state->PyRawIOBase_Type);
  510. Py_CLEAR(state->PyBufferedIOBase_Type);
  511. Py_CLEAR(state->PyBufferedRWPair_Type);
  512. Py_CLEAR(state->PyBufferedRandom_Type);
  513. Py_CLEAR(state->PyBufferedReader_Type);
  514. Py_CLEAR(state->PyBufferedWriter_Type);
  515. Py_CLEAR(state->PyBytesIOBuffer_Type);
  516. Py_CLEAR(state->PyBytesIO_Type);
  517. Py_CLEAR(state->PyFileIO_Type);
  518. Py_CLEAR(state->PyStringIO_Type);
  519. Py_CLEAR(state->PyTextIOBase_Type);
  520. Py_CLEAR(state->PyTextIOWrapper_Type);
  521. #ifdef HAVE_WINDOWS_CONSOLE_IO
  522. Py_CLEAR(state->PyWindowsConsoleIO_Type);
  523. #endif
  524. return 0;
  525. }
  526. static void
  527. iomodule_free(void *mod)
  528. {
  529. (void)iomodule_clear((PyObject *)mod);
  530. }
  531. /*
  532. * Module definition
  533. */
  534. #define clinic_state() (get_io_state(module))
  535. #include "clinic/_iomodule.c.h"
  536. #undef clinic_state
  537. static PyMethodDef module_methods[] = {
  538. _IO_OPEN_METHODDEF
  539. _IO_TEXT_ENCODING_METHODDEF
  540. _IO_OPEN_CODE_METHODDEF
  541. {NULL, NULL}
  542. };
  543. #define ADD_TYPE(module, type, spec, base) \
  544. do { \
  545. type = (PyTypeObject *)PyType_FromModuleAndSpec(module, spec, \
  546. (PyObject *)base); \
  547. if (type == NULL) { \
  548. return -1; \
  549. } \
  550. if (PyModule_AddType(module, type) < 0) { \
  551. return -1; \
  552. } \
  553. } while (0)
  554. static int
  555. iomodule_exec(PyObject *m)
  556. {
  557. _PyIO_State *state = get_io_state(m);
  558. /* DEFAULT_BUFFER_SIZE */
  559. if (PyModule_AddIntMacro(m, DEFAULT_BUFFER_SIZE) < 0)
  560. return -1;
  561. /* UnsupportedOperation inherits from ValueError and OSError */
  562. state->unsupported_operation = PyObject_CallFunction(
  563. (PyObject *)&PyType_Type, "s(OO){}",
  564. "UnsupportedOperation", PyExc_OSError, PyExc_ValueError);
  565. if (state->unsupported_operation == NULL)
  566. return -1;
  567. if (PyModule_AddObjectRef(m, "UnsupportedOperation",
  568. state->unsupported_operation) < 0)
  569. {
  570. return -1;
  571. }
  572. /* BlockingIOError, for compatibility */
  573. if (PyModule_AddObjectRef(m, "BlockingIOError",
  574. (PyObject *) PyExc_BlockingIOError) < 0) {
  575. return -1;
  576. }
  577. // Base classes
  578. ADD_TYPE(m, state->PyIncrementalNewlineDecoder_Type, &nldecoder_spec, NULL);
  579. ADD_TYPE(m, state->PyBytesIOBuffer_Type, &bytesiobuf_spec, NULL);
  580. ADD_TYPE(m, state->PyIOBase_Type, &iobase_spec, NULL);
  581. // PyIOBase_Type subclasses
  582. ADD_TYPE(m, state->PyTextIOBase_Type, &textiobase_spec,
  583. state->PyIOBase_Type);
  584. ADD_TYPE(m, state->PyBufferedIOBase_Type, &bufferediobase_spec,
  585. state->PyIOBase_Type);
  586. ADD_TYPE(m, state->PyRawIOBase_Type, &rawiobase_spec,
  587. state->PyIOBase_Type);
  588. // PyBufferedIOBase_Type(PyIOBase_Type) subclasses
  589. ADD_TYPE(m, state->PyBytesIO_Type, &bytesio_spec, state->PyBufferedIOBase_Type);
  590. ADD_TYPE(m, state->PyBufferedWriter_Type, &bufferedwriter_spec,
  591. state->PyBufferedIOBase_Type);
  592. ADD_TYPE(m, state->PyBufferedReader_Type, &bufferedreader_spec,
  593. state->PyBufferedIOBase_Type);
  594. ADD_TYPE(m, state->PyBufferedRWPair_Type, &bufferedrwpair_spec,
  595. state->PyBufferedIOBase_Type);
  596. ADD_TYPE(m, state->PyBufferedRandom_Type, &bufferedrandom_spec,
  597. state->PyBufferedIOBase_Type);
  598. // PyRawIOBase_Type(PyIOBase_Type) subclasses
  599. ADD_TYPE(m, state->PyFileIO_Type, &fileio_spec, state->PyRawIOBase_Type);
  600. #ifdef HAVE_WINDOWS_CONSOLE_IO
  601. ADD_TYPE(m, state->PyWindowsConsoleIO_Type, &winconsoleio_spec,
  602. state->PyRawIOBase_Type);
  603. #endif
  604. // PyTextIOBase_Type(PyIOBase_Type) subclasses
  605. ADD_TYPE(m, state->PyStringIO_Type, &stringio_spec, state->PyTextIOBase_Type);
  606. ADD_TYPE(m, state->PyTextIOWrapper_Type, &textiowrapper_spec,
  607. state->PyTextIOBase_Type);
  608. #undef ADD_TYPE
  609. return 0;
  610. }
  611. static struct PyModuleDef_Slot iomodule_slots[] = {
  612. {Py_mod_exec, iomodule_exec},
  613. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  614. {0, NULL},
  615. };
  616. struct PyModuleDef _PyIO_Module = {
  617. .m_base = PyModuleDef_HEAD_INIT,
  618. .m_name = "io",
  619. .m_doc = module_doc,
  620. .m_size = sizeof(_PyIO_State),
  621. .m_methods = module_methods,
  622. .m_traverse = iomodule_traverse,
  623. .m_clear = iomodule_clear,
  624. .m_free = iomodule_free,
  625. .m_slots = iomodule_slots,
  626. };
  627. PyMODINIT_FUNC
  628. PyInit__io(void)
  629. {
  630. return PyModuleDef_Init(&_PyIO_Module);
  631. }