stringio.c.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. PyDoc_STRVAR(_io_StringIO_getvalue__doc__,
  9. "getvalue($self, /)\n"
  10. "--\n"
  11. "\n"
  12. "Retrieve the entire contents of the object.");
  13. #define _IO_STRINGIO_GETVALUE_METHODDEF \
  14. {"getvalue", (PyCFunction)_io_StringIO_getvalue, METH_NOARGS, _io_StringIO_getvalue__doc__},
  15. static PyObject *
  16. _io_StringIO_getvalue_impl(stringio *self);
  17. static PyObject *
  18. _io_StringIO_getvalue(stringio *self, PyObject *Py_UNUSED(ignored))
  19. {
  20. return _io_StringIO_getvalue_impl(self);
  21. }
  22. PyDoc_STRVAR(_io_StringIO_tell__doc__,
  23. "tell($self, /)\n"
  24. "--\n"
  25. "\n"
  26. "Tell the current file position.");
  27. #define _IO_STRINGIO_TELL_METHODDEF \
  28. {"tell", (PyCFunction)_io_StringIO_tell, METH_NOARGS, _io_StringIO_tell__doc__},
  29. static PyObject *
  30. _io_StringIO_tell_impl(stringio *self);
  31. static PyObject *
  32. _io_StringIO_tell(stringio *self, PyObject *Py_UNUSED(ignored))
  33. {
  34. return _io_StringIO_tell_impl(self);
  35. }
  36. PyDoc_STRVAR(_io_StringIO_read__doc__,
  37. "read($self, size=-1, /)\n"
  38. "--\n"
  39. "\n"
  40. "Read at most size characters, returned as a string.\n"
  41. "\n"
  42. "If the argument is negative or omitted, read until EOF\n"
  43. "is reached. Return an empty string at EOF.");
  44. #define _IO_STRINGIO_READ_METHODDEF \
  45. {"read", _PyCFunction_CAST(_io_StringIO_read), METH_FASTCALL, _io_StringIO_read__doc__},
  46. static PyObject *
  47. _io_StringIO_read_impl(stringio *self, Py_ssize_t size);
  48. static PyObject *
  49. _io_StringIO_read(stringio *self, PyObject *const *args, Py_ssize_t nargs)
  50. {
  51. PyObject *return_value = NULL;
  52. Py_ssize_t size = -1;
  53. if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
  54. goto exit;
  55. }
  56. if (nargs < 1) {
  57. goto skip_optional;
  58. }
  59. if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
  60. goto exit;
  61. }
  62. skip_optional:
  63. return_value = _io_StringIO_read_impl(self, size);
  64. exit:
  65. return return_value;
  66. }
  67. PyDoc_STRVAR(_io_StringIO_readline__doc__,
  68. "readline($self, size=-1, /)\n"
  69. "--\n"
  70. "\n"
  71. "Read until newline or EOF.\n"
  72. "\n"
  73. "Returns an empty string if EOF is hit immediately.");
  74. #define _IO_STRINGIO_READLINE_METHODDEF \
  75. {"readline", _PyCFunction_CAST(_io_StringIO_readline), METH_FASTCALL, _io_StringIO_readline__doc__},
  76. static PyObject *
  77. _io_StringIO_readline_impl(stringio *self, Py_ssize_t size);
  78. static PyObject *
  79. _io_StringIO_readline(stringio *self, PyObject *const *args, Py_ssize_t nargs)
  80. {
  81. PyObject *return_value = NULL;
  82. Py_ssize_t size = -1;
  83. if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
  84. goto exit;
  85. }
  86. if (nargs < 1) {
  87. goto skip_optional;
  88. }
  89. if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
  90. goto exit;
  91. }
  92. skip_optional:
  93. return_value = _io_StringIO_readline_impl(self, size);
  94. exit:
  95. return return_value;
  96. }
  97. PyDoc_STRVAR(_io_StringIO_truncate__doc__,
  98. "truncate($self, pos=None, /)\n"
  99. "--\n"
  100. "\n"
  101. "Truncate size to pos.\n"
  102. "\n"
  103. "The pos argument defaults to the current file position, as\n"
  104. "returned by tell(). The current file position is unchanged.\n"
  105. "Returns the new absolute position.");
  106. #define _IO_STRINGIO_TRUNCATE_METHODDEF \
  107. {"truncate", _PyCFunction_CAST(_io_StringIO_truncate), METH_FASTCALL, _io_StringIO_truncate__doc__},
  108. static PyObject *
  109. _io_StringIO_truncate_impl(stringio *self, Py_ssize_t size);
  110. static PyObject *
  111. _io_StringIO_truncate(stringio *self, PyObject *const *args, Py_ssize_t nargs)
  112. {
  113. PyObject *return_value = NULL;
  114. Py_ssize_t size = self->pos;
  115. if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
  116. goto exit;
  117. }
  118. if (nargs < 1) {
  119. goto skip_optional;
  120. }
  121. if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
  122. goto exit;
  123. }
  124. skip_optional:
  125. return_value = _io_StringIO_truncate_impl(self, size);
  126. exit:
  127. return return_value;
  128. }
  129. PyDoc_STRVAR(_io_StringIO_seek__doc__,
  130. "seek($self, pos, whence=0, /)\n"
  131. "--\n"
  132. "\n"
  133. "Change stream position.\n"
  134. "\n"
  135. "Seek to character offset pos relative to position indicated by whence:\n"
  136. " 0 Start of stream (the default). pos should be >= 0;\n"
  137. " 1 Current position - pos must be 0;\n"
  138. " 2 End of stream - pos must be 0.\n"
  139. "Returns the new absolute position.");
  140. #define _IO_STRINGIO_SEEK_METHODDEF \
  141. {"seek", _PyCFunction_CAST(_io_StringIO_seek), METH_FASTCALL, _io_StringIO_seek__doc__},
  142. static PyObject *
  143. _io_StringIO_seek_impl(stringio *self, Py_ssize_t pos, int whence);
  144. static PyObject *
  145. _io_StringIO_seek(stringio *self, PyObject *const *args, Py_ssize_t nargs)
  146. {
  147. PyObject *return_value = NULL;
  148. Py_ssize_t pos;
  149. int whence = 0;
  150. if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
  151. goto exit;
  152. }
  153. {
  154. Py_ssize_t ival = -1;
  155. PyObject *iobj = _PyNumber_Index(args[0]);
  156. if (iobj != NULL) {
  157. ival = PyLong_AsSsize_t(iobj);
  158. Py_DECREF(iobj);
  159. }
  160. if (ival == -1 && PyErr_Occurred()) {
  161. goto exit;
  162. }
  163. pos = ival;
  164. }
  165. if (nargs < 2) {
  166. goto skip_optional;
  167. }
  168. whence = _PyLong_AsInt(args[1]);
  169. if (whence == -1 && PyErr_Occurred()) {
  170. goto exit;
  171. }
  172. skip_optional:
  173. return_value = _io_StringIO_seek_impl(self, pos, whence);
  174. exit:
  175. return return_value;
  176. }
  177. PyDoc_STRVAR(_io_StringIO_write__doc__,
  178. "write($self, s, /)\n"
  179. "--\n"
  180. "\n"
  181. "Write string to file.\n"
  182. "\n"
  183. "Returns the number of characters written, which is always equal to\n"
  184. "the length of the string.");
  185. #define _IO_STRINGIO_WRITE_METHODDEF \
  186. {"write", (PyCFunction)_io_StringIO_write, METH_O, _io_StringIO_write__doc__},
  187. PyDoc_STRVAR(_io_StringIO_close__doc__,
  188. "close($self, /)\n"
  189. "--\n"
  190. "\n"
  191. "Close the IO object.\n"
  192. "\n"
  193. "Attempting any further operation after the object is closed\n"
  194. "will raise a ValueError.\n"
  195. "\n"
  196. "This method has no effect if the file is already closed.");
  197. #define _IO_STRINGIO_CLOSE_METHODDEF \
  198. {"close", (PyCFunction)_io_StringIO_close, METH_NOARGS, _io_StringIO_close__doc__},
  199. static PyObject *
  200. _io_StringIO_close_impl(stringio *self);
  201. static PyObject *
  202. _io_StringIO_close(stringio *self, PyObject *Py_UNUSED(ignored))
  203. {
  204. return _io_StringIO_close_impl(self);
  205. }
  206. PyDoc_STRVAR(_io_StringIO___init____doc__,
  207. "StringIO(initial_value=\'\', newline=\'\\n\')\n"
  208. "--\n"
  209. "\n"
  210. "Text I/O implementation using an in-memory buffer.\n"
  211. "\n"
  212. "The initial_value argument sets the value of object. The newline\n"
  213. "argument is like the one of TextIOWrapper\'s constructor.");
  214. static int
  215. _io_StringIO___init___impl(stringio *self, PyObject *value,
  216. PyObject *newline_obj);
  217. static int
  218. _io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
  219. {
  220. int return_value = -1;
  221. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  222. #define NUM_KEYWORDS 2
  223. static struct {
  224. PyGC_Head _this_is_not_used;
  225. PyObject_VAR_HEAD
  226. PyObject *ob_item[NUM_KEYWORDS];
  227. } _kwtuple = {
  228. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  229. .ob_item = { &_Py_ID(initial_value), &_Py_ID(newline), },
  230. };
  231. #undef NUM_KEYWORDS
  232. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  233. #else // !Py_BUILD_CORE
  234. # define KWTUPLE NULL
  235. #endif // !Py_BUILD_CORE
  236. static const char * const _keywords[] = {"initial_value", "newline", NULL};
  237. static _PyArg_Parser _parser = {
  238. .keywords = _keywords,
  239. .fname = "StringIO",
  240. .kwtuple = KWTUPLE,
  241. };
  242. #undef KWTUPLE
  243. PyObject *argsbuf[2];
  244. PyObject * const *fastargs;
  245. Py_ssize_t nargs = PyTuple_GET_SIZE(args);
  246. Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
  247. PyObject *value = NULL;
  248. PyObject *newline_obj = NULL;
  249. fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf);
  250. if (!fastargs) {
  251. goto exit;
  252. }
  253. if (!noptargs) {
  254. goto skip_optional_pos;
  255. }
  256. if (fastargs[0]) {
  257. value = fastargs[0];
  258. if (!--noptargs) {
  259. goto skip_optional_pos;
  260. }
  261. }
  262. newline_obj = fastargs[1];
  263. skip_optional_pos:
  264. return_value = _io_StringIO___init___impl((stringio *)self, value, newline_obj);
  265. exit:
  266. return return_value;
  267. }
  268. PyDoc_STRVAR(_io_StringIO_readable__doc__,
  269. "readable($self, /)\n"
  270. "--\n"
  271. "\n"
  272. "Returns True if the IO object can be read.");
  273. #define _IO_STRINGIO_READABLE_METHODDEF \
  274. {"readable", (PyCFunction)_io_StringIO_readable, METH_NOARGS, _io_StringIO_readable__doc__},
  275. static PyObject *
  276. _io_StringIO_readable_impl(stringio *self);
  277. static PyObject *
  278. _io_StringIO_readable(stringio *self, PyObject *Py_UNUSED(ignored))
  279. {
  280. return _io_StringIO_readable_impl(self);
  281. }
  282. PyDoc_STRVAR(_io_StringIO_writable__doc__,
  283. "writable($self, /)\n"
  284. "--\n"
  285. "\n"
  286. "Returns True if the IO object can be written.");
  287. #define _IO_STRINGIO_WRITABLE_METHODDEF \
  288. {"writable", (PyCFunction)_io_StringIO_writable, METH_NOARGS, _io_StringIO_writable__doc__},
  289. static PyObject *
  290. _io_StringIO_writable_impl(stringio *self);
  291. static PyObject *
  292. _io_StringIO_writable(stringio *self, PyObject *Py_UNUSED(ignored))
  293. {
  294. return _io_StringIO_writable_impl(self);
  295. }
  296. PyDoc_STRVAR(_io_StringIO_seekable__doc__,
  297. "seekable($self, /)\n"
  298. "--\n"
  299. "\n"
  300. "Returns True if the IO object can be seeked.");
  301. #define _IO_STRINGIO_SEEKABLE_METHODDEF \
  302. {"seekable", (PyCFunction)_io_StringIO_seekable, METH_NOARGS, _io_StringIO_seekable__doc__},
  303. static PyObject *
  304. _io_StringIO_seekable_impl(stringio *self);
  305. static PyObject *
  306. _io_StringIO_seekable(stringio *self, PyObject *Py_UNUSED(ignored))
  307. {
  308. return _io_StringIO_seekable_impl(self);
  309. }
  310. /*[clinic end generated code: output=533f20ae9b773126 input=a9049054013a1b77]*/