decompressionreader.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /**
  2. * Copyright (c) 2017-present, Gregory Szorc
  3. * All rights reserved.
  4. *
  5. * This software may be modified and distributed under the terms
  6. * of the BSD license. See the LICENSE file for details.
  7. */
  8. #include "python-zstandard.h"
  9. extern PyObject* ZstdError;
  10. static void set_unsupported_operation(void) {
  11. PyObject* iomod;
  12. PyObject* exc;
  13. iomod = PyImport_ImportModule("io");
  14. if (NULL == iomod) {
  15. return;
  16. }
  17. exc = PyObject_GetAttrString(iomod, "UnsupportedOperation");
  18. if (NULL == exc) {
  19. Py_DECREF(iomod);
  20. return;
  21. }
  22. PyErr_SetNone(exc);
  23. Py_DECREF(exc);
  24. Py_DECREF(iomod);
  25. }
  26. static void reader_dealloc(ZstdDecompressionReader* self) {
  27. Py_XDECREF(self->decompressor);
  28. Py_XDECREF(self->reader);
  29. if (self->buffer.buf) {
  30. PyBuffer_Release(&self->buffer);
  31. }
  32. PyObject_Del(self);
  33. }
  34. static ZstdDecompressionReader* reader_enter(ZstdDecompressionReader* self) {
  35. if (self->entered) {
  36. PyErr_SetString(PyExc_ValueError, "cannot __enter__ multiple times");
  37. return NULL;
  38. }
  39. self->entered = 1;
  40. Py_INCREF(self);
  41. return self;
  42. }
  43. static PyObject* reader_exit(ZstdDecompressionReader* self, PyObject* args) {
  44. PyObject* exc_type;
  45. PyObject* exc_value;
  46. PyObject* exc_tb;
  47. if (!PyArg_ParseTuple(args, "OOO:__exit__", &exc_type, &exc_value, &exc_tb)) {
  48. return NULL;
  49. }
  50. self->entered = 0;
  51. self->closed = 1;
  52. /* Release resources. */
  53. Py_CLEAR(self->reader);
  54. if (self->buffer.buf) {
  55. PyBuffer_Release(&self->buffer);
  56. memset(&self->buffer, 0, sizeof(self->buffer));
  57. }
  58. Py_CLEAR(self->decompressor);
  59. Py_RETURN_FALSE;
  60. }
  61. static PyObject* reader_readable(PyObject* self) {
  62. Py_RETURN_TRUE;
  63. }
  64. static PyObject* reader_writable(PyObject* self) {
  65. Py_RETURN_FALSE;
  66. }
  67. static PyObject* reader_seekable(PyObject* self) {
  68. Py_RETURN_TRUE;
  69. }
  70. static PyObject* reader_close(ZstdDecompressionReader* self) {
  71. self->closed = 1;
  72. Py_RETURN_NONE;
  73. }
  74. static PyObject* reader_flush(PyObject* self) {
  75. Py_RETURN_NONE;
  76. }
  77. static PyObject* reader_isatty(PyObject* self) {
  78. Py_RETURN_FALSE;
  79. }
  80. /**
  81. * Read available input.
  82. *
  83. * Returns 0 if no data was added to input.
  84. * Returns 1 if new input data is available.
  85. * Returns -1 on error and sets a Python exception as a side-effect.
  86. */
  87. int read_decompressor_input(ZstdDecompressionReader* self) {
  88. if (self->finishedInput) {
  89. return 0;
  90. }
  91. if (self->input.pos != self->input.size) {
  92. return 0;
  93. }
  94. if (self->reader) {
  95. Py_buffer buffer;
  96. assert(self->readResult == NULL);
  97. self->readResult = PyObject_CallMethod(self->reader, "read",
  98. "k", self->readSize);
  99. if (NULL == self->readResult) {
  100. return -1;
  101. }
  102. memset(&buffer, 0, sizeof(buffer));
  103. if (0 != PyObject_GetBuffer(self->readResult, &buffer, PyBUF_CONTIG_RO)) {
  104. return -1;
  105. }
  106. /* EOF */
  107. if (0 == buffer.len) {
  108. self->finishedInput = 1;
  109. Py_CLEAR(self->readResult);
  110. }
  111. else {
  112. self->input.src = buffer.buf;
  113. self->input.size = buffer.len;
  114. self->input.pos = 0;
  115. }
  116. PyBuffer_Release(&buffer);
  117. }
  118. else {
  119. assert(self->buffer.buf);
  120. /*
  121. * We should only get here once since expectation is we always
  122. * exhaust input buffer before reading again.
  123. */
  124. assert(self->input.src == NULL);
  125. self->input.src = self->buffer.buf;
  126. self->input.size = self->buffer.len;
  127. self->input.pos = 0;
  128. }
  129. return 1;
  130. }
  131. /**
  132. * Decompresses available input into an output buffer.
  133. *
  134. * Returns 0 if we need more input.
  135. * Returns 1 if output buffer should be emitted.
  136. * Returns -1 on error and sets a Python exception.
  137. */
  138. int decompress_input(ZstdDecompressionReader* self, ZSTD_outBuffer* output) {
  139. size_t zresult;
  140. if (self->input.pos >= self->input.size) {
  141. return 0;
  142. }
  143. Py_BEGIN_ALLOW_THREADS
  144. zresult = ZSTD_decompressStream(self->decompressor->dctx, output, &self->input);
  145. Py_END_ALLOW_THREADS
  146. /* Input exhausted. Clear our state tracking. */
  147. if (self->input.pos == self->input.size) {
  148. memset(&self->input, 0, sizeof(self->input));
  149. Py_CLEAR(self->readResult);
  150. if (self->buffer.buf) {
  151. self->finishedInput = 1;
  152. }
  153. }
  154. if (ZSTD_isError(zresult)) {
  155. PyErr_Format(ZstdError, "zstd decompress error: %s", ZSTD_getErrorName(zresult));
  156. return -1;
  157. }
  158. /* We fulfilled the full read request. Signal to emit. */
  159. if (output->pos && output->pos == output->size) {
  160. return 1;
  161. }
  162. /* We're at the end of a frame and we aren't allowed to return data
  163. spanning frames. */
  164. else if (output->pos && zresult == 0 && !self->readAcrossFrames) {
  165. return 1;
  166. }
  167. /* There is more room in the output. Signal to collect more data. */
  168. return 0;
  169. }
  170. static PyObject* reader_read(ZstdDecompressionReader* self, PyObject* args, PyObject* kwargs) {
  171. static char* kwlist[] = {
  172. "size",
  173. NULL
  174. };
  175. Py_ssize_t size = -1;
  176. PyObject* result = NULL;
  177. char* resultBuffer;
  178. Py_ssize_t resultSize;
  179. ZSTD_outBuffer output;
  180. int decompressResult, readResult;
  181. if (self->closed) {
  182. PyErr_SetString(PyExc_ValueError, "stream is closed");
  183. return NULL;
  184. }
  185. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|n", kwlist, &size)) {
  186. return NULL;
  187. }
  188. if (size < -1) {
  189. PyErr_SetString(PyExc_ValueError, "cannot read negative amounts less than -1");
  190. return NULL;
  191. }
  192. if (size == -1) {
  193. return PyObject_CallMethod((PyObject*)self, "readall", NULL);
  194. }
  195. if (self->finishedOutput || size == 0) {
  196. return PyBytes_FromStringAndSize("", 0);
  197. }
  198. result = PyBytes_FromStringAndSize(NULL, size);
  199. if (NULL == result) {
  200. return NULL;
  201. }
  202. PyBytes_AsStringAndSize(result, &resultBuffer, &resultSize);
  203. output.dst = resultBuffer;
  204. output.size = resultSize;
  205. output.pos = 0;
  206. readinput:
  207. decompressResult = decompress_input(self, &output);
  208. if (-1 == decompressResult) {
  209. Py_XDECREF(result);
  210. return NULL;
  211. }
  212. else if (0 == decompressResult) { }
  213. else if (1 == decompressResult) {
  214. self->bytesDecompressed += output.pos;
  215. if (output.pos != output.size) {
  216. if (safe_pybytes_resize(&result, output.pos)) {
  217. Py_XDECREF(result);
  218. return NULL;
  219. }
  220. }
  221. return result;
  222. }
  223. else {
  224. assert(0);
  225. }
  226. readResult = read_decompressor_input(self);
  227. if (-1 == readResult) {
  228. Py_XDECREF(result);
  229. return NULL;
  230. }
  231. else if (0 == readResult) {}
  232. else if (1 == readResult) {}
  233. else {
  234. assert(0);
  235. }
  236. if (self->input.size) {
  237. goto readinput;
  238. }
  239. /* EOF */
  240. self->bytesDecompressed += output.pos;
  241. if (safe_pybytes_resize(&result, output.pos)) {
  242. Py_XDECREF(result);
  243. return NULL;
  244. }
  245. return result;
  246. }
  247. static PyObject* reader_read1(ZstdDecompressionReader* self, PyObject* args, PyObject* kwargs) {
  248. static char* kwlist[] = {
  249. "size",
  250. NULL
  251. };
  252. Py_ssize_t size = -1;
  253. PyObject* result = NULL;
  254. char* resultBuffer;
  255. Py_ssize_t resultSize;
  256. ZSTD_outBuffer output;
  257. if (self->closed) {
  258. PyErr_SetString(PyExc_ValueError, "stream is closed");
  259. return NULL;
  260. }
  261. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|n", kwlist, &size)) {
  262. return NULL;
  263. }
  264. if (size < -1) {
  265. PyErr_SetString(PyExc_ValueError, "cannot read negative amounts less than -1");
  266. return NULL;
  267. }
  268. if (self->finishedOutput || size == 0) {
  269. return PyBytes_FromStringAndSize("", 0);
  270. }
  271. if (size == -1) {
  272. size = ZSTD_DStreamOutSize();
  273. }
  274. result = PyBytes_FromStringAndSize(NULL, size);
  275. if (NULL == result) {
  276. return NULL;
  277. }
  278. PyBytes_AsStringAndSize(result, &resultBuffer, &resultSize);
  279. output.dst = resultBuffer;
  280. output.size = resultSize;
  281. output.pos = 0;
  282. /* read1() is supposed to use at most 1 read() from the underlying stream.
  283. * However, we can't satisfy this requirement with decompression due to the
  284. * nature of how decompression works. Our strategy is to read + decompress
  285. * until we get any output, at which point we return. This satisfies the
  286. * intent of the read1() API to limit read operations.
  287. */
  288. while (!self->finishedInput) {
  289. int readResult, decompressResult;
  290. readResult = read_decompressor_input(self);
  291. if (-1 == readResult) {
  292. Py_XDECREF(result);
  293. return NULL;
  294. }
  295. else if (0 == readResult || 1 == readResult) { }
  296. else {
  297. assert(0);
  298. }
  299. decompressResult = decompress_input(self, &output);
  300. if (-1 == decompressResult) {
  301. Py_XDECREF(result);
  302. return NULL;
  303. }
  304. else if (0 == decompressResult || 1 == decompressResult) { }
  305. else {
  306. assert(0);
  307. }
  308. if (output.pos) {
  309. break;
  310. }
  311. }
  312. self->bytesDecompressed += output.pos;
  313. if (safe_pybytes_resize(&result, output.pos)) {
  314. Py_XDECREF(result);
  315. return NULL;
  316. }
  317. return result;
  318. }
  319. static PyObject* reader_readinto(ZstdDecompressionReader* self, PyObject* args) {
  320. Py_buffer dest;
  321. ZSTD_outBuffer output;
  322. int decompressResult, readResult;
  323. PyObject* result = NULL;
  324. if (self->closed) {
  325. PyErr_SetString(PyExc_ValueError, "stream is closed");
  326. return NULL;
  327. }
  328. if (self->finishedOutput) {
  329. return PyLong_FromLong(0);
  330. }
  331. if (!PyArg_ParseTuple(args, "w*:readinto", &dest)) {
  332. return NULL;
  333. }
  334. if (!PyBuffer_IsContiguous(&dest, 'C') || dest.ndim > 1) {
  335. PyErr_SetString(PyExc_ValueError,
  336. "destination buffer should be contiguous and have at most one dimension");
  337. goto finally;
  338. }
  339. output.dst = dest.buf;
  340. output.size = dest.len;
  341. output.pos = 0;
  342. readinput:
  343. decompressResult = decompress_input(self, &output);
  344. if (-1 == decompressResult) {
  345. goto finally;
  346. }
  347. else if (0 == decompressResult) { }
  348. else if (1 == decompressResult) {
  349. self->bytesDecompressed += output.pos;
  350. result = PyLong_FromSize_t(output.pos);
  351. goto finally;
  352. }
  353. else {
  354. assert(0);
  355. }
  356. readResult = read_decompressor_input(self);
  357. if (-1 == readResult) {
  358. goto finally;
  359. }
  360. else if (0 == readResult) {}
  361. else if (1 == readResult) {}
  362. else {
  363. assert(0);
  364. }
  365. if (self->input.size) {
  366. goto readinput;
  367. }
  368. /* EOF */
  369. self->bytesDecompressed += output.pos;
  370. result = PyLong_FromSize_t(output.pos);
  371. finally:
  372. PyBuffer_Release(&dest);
  373. return result;
  374. }
  375. static PyObject* reader_readinto1(ZstdDecompressionReader* self, PyObject* args) {
  376. Py_buffer dest;
  377. ZSTD_outBuffer output;
  378. PyObject* result = NULL;
  379. if (self->closed) {
  380. PyErr_SetString(PyExc_ValueError, "stream is closed");
  381. return NULL;
  382. }
  383. if (self->finishedOutput) {
  384. return PyLong_FromLong(0);
  385. }
  386. if (!PyArg_ParseTuple(args, "w*:readinto1", &dest)) {
  387. return NULL;
  388. }
  389. if (!PyBuffer_IsContiguous(&dest, 'C') || dest.ndim > 1) {
  390. PyErr_SetString(PyExc_ValueError,
  391. "destination buffer should be contiguous and have at most one dimension");
  392. goto finally;
  393. }
  394. output.dst = dest.buf;
  395. output.size = dest.len;
  396. output.pos = 0;
  397. while (!self->finishedInput && !self->finishedOutput) {
  398. int decompressResult, readResult;
  399. readResult = read_decompressor_input(self);
  400. if (-1 == readResult) {
  401. goto finally;
  402. }
  403. else if (0 == readResult || 1 == readResult) {}
  404. else {
  405. assert(0);
  406. }
  407. decompressResult = decompress_input(self, &output);
  408. if (-1 == decompressResult) {
  409. goto finally;
  410. }
  411. else if (0 == decompressResult || 1 == decompressResult) {}
  412. else {
  413. assert(0);
  414. }
  415. if (output.pos) {
  416. break;
  417. }
  418. }
  419. self->bytesDecompressed += output.pos;
  420. result = PyLong_FromSize_t(output.pos);
  421. finally:
  422. PyBuffer_Release(&dest);
  423. return result;
  424. }
  425. static PyObject* reader_readall(PyObject* self) {
  426. PyObject* chunks = NULL;
  427. PyObject* empty = NULL;
  428. PyObject* result = NULL;
  429. /* Our strategy is to collect chunks into a list then join all the
  430. * chunks at the end. We could potentially use e.g. an io.BytesIO. But
  431. * this feels simple enough to implement and avoids potentially expensive
  432. * reallocations of large buffers.
  433. */
  434. chunks = PyList_New(0);
  435. if (NULL == chunks) {
  436. return NULL;
  437. }
  438. while (1) {
  439. PyObject* chunk = PyObject_CallMethod(self, "read", "i", 1048576);
  440. if (NULL == chunk) {
  441. Py_DECREF(chunks);
  442. return NULL;
  443. }
  444. if (!PyBytes_Size(chunk)) {
  445. Py_DECREF(chunk);
  446. break;
  447. }
  448. if (PyList_Append(chunks, chunk)) {
  449. Py_DECREF(chunk);
  450. Py_DECREF(chunks);
  451. return NULL;
  452. }
  453. Py_DECREF(chunk);
  454. }
  455. empty = PyBytes_FromStringAndSize("", 0);
  456. if (NULL == empty) {
  457. Py_DECREF(chunks);
  458. return NULL;
  459. }
  460. result = PyObject_CallMethod(empty, "join", "O", chunks);
  461. Py_DECREF(empty);
  462. Py_DECREF(chunks);
  463. return result;
  464. }
  465. static PyObject* reader_readline(PyObject* self) {
  466. set_unsupported_operation();
  467. return NULL;
  468. }
  469. static PyObject* reader_readlines(PyObject* self) {
  470. set_unsupported_operation();
  471. return NULL;
  472. }
  473. static PyObject* reader_seek(ZstdDecompressionReader* self, PyObject* args) {
  474. Py_ssize_t pos;
  475. int whence = 0;
  476. unsigned long long readAmount = 0;
  477. size_t defaultOutSize = ZSTD_DStreamOutSize();
  478. if (self->closed) {
  479. PyErr_SetString(PyExc_ValueError, "stream is closed");
  480. return NULL;
  481. }
  482. if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &whence)) {
  483. return NULL;
  484. }
  485. if (whence == SEEK_SET) {
  486. if (pos < 0) {
  487. PyErr_SetString(PyExc_ValueError,
  488. "cannot seek to negative position with SEEK_SET");
  489. return NULL;
  490. }
  491. if ((unsigned long long)pos < self->bytesDecompressed) {
  492. PyErr_SetString(PyExc_ValueError,
  493. "cannot seek zstd decompression stream backwards");
  494. return NULL;
  495. }
  496. readAmount = pos - self->bytesDecompressed;
  497. }
  498. else if (whence == SEEK_CUR) {
  499. if (pos < 0) {
  500. PyErr_SetString(PyExc_ValueError,
  501. "cannot seek zstd decompression stream backwards");
  502. return NULL;
  503. }
  504. readAmount = pos;
  505. }
  506. else if (whence == SEEK_END) {
  507. /* We /could/ support this with pos==0. But let's not do that until someone
  508. needs it. */
  509. PyErr_SetString(PyExc_ValueError,
  510. "zstd decompression streams cannot be seeked with SEEK_END");
  511. return NULL;
  512. }
  513. /* It is a bit inefficient to do this via the Python API. But since there
  514. is a bit of state tracking involved to read from this type, it is the
  515. easiest to implement. */
  516. while (readAmount) {
  517. Py_ssize_t readSize;
  518. PyObject* readResult = PyObject_CallMethod((PyObject*)self, "read", "K",
  519. readAmount < defaultOutSize ? readAmount : defaultOutSize);
  520. if (!readResult) {
  521. return NULL;
  522. }
  523. readSize = PyBytes_GET_SIZE(readResult);
  524. Py_CLEAR(readResult);
  525. /* Empty read means EOF. */
  526. if (!readSize) {
  527. break;
  528. }
  529. readAmount -= readSize;
  530. }
  531. return PyLong_FromUnsignedLongLong(self->bytesDecompressed);
  532. }
  533. static PyObject* reader_tell(ZstdDecompressionReader* self) {
  534. /* TODO should this raise OSError since stream isn't seekable? */
  535. return PyLong_FromUnsignedLongLong(self->bytesDecompressed);
  536. }
  537. static PyObject* reader_write(PyObject* self, PyObject* args) {
  538. set_unsupported_operation();
  539. return NULL;
  540. }
  541. static PyObject* reader_writelines(PyObject* self, PyObject* args) {
  542. set_unsupported_operation();
  543. return NULL;
  544. }
  545. static PyObject* reader_iter(PyObject* self) {
  546. set_unsupported_operation();
  547. return NULL;
  548. }
  549. static PyObject* reader_iternext(PyObject* self) {
  550. set_unsupported_operation();
  551. return NULL;
  552. }
  553. static PyMethodDef reader_methods[] = {
  554. { "__enter__", (PyCFunction)reader_enter, METH_NOARGS,
  555. PyDoc_STR("Enter a compression context") },
  556. { "__exit__", (PyCFunction)reader_exit, METH_VARARGS,
  557. PyDoc_STR("Exit a compression context") },
  558. { "close", (PyCFunction)reader_close, METH_NOARGS,
  559. PyDoc_STR("Close the stream so it cannot perform any more operations") },
  560. { "flush", (PyCFunction)reader_flush, METH_NOARGS, PyDoc_STR("no-ops") },
  561. { "isatty", (PyCFunction)reader_isatty, METH_NOARGS, PyDoc_STR("Returns False") },
  562. { "readable", (PyCFunction)reader_readable, METH_NOARGS,
  563. PyDoc_STR("Returns True") },
  564. { "read", (PyCFunction)reader_read, METH_VARARGS | METH_KEYWORDS,
  565. PyDoc_STR("read compressed data") },
  566. { "read1", (PyCFunction)reader_read1, METH_VARARGS | METH_KEYWORDS,
  567. PyDoc_STR("read compressed data") },
  568. { "readinto", (PyCFunction)reader_readinto, METH_VARARGS, NULL },
  569. { "readinto1", (PyCFunction)reader_readinto1, METH_VARARGS, NULL },
  570. { "readall", (PyCFunction)reader_readall, METH_NOARGS, PyDoc_STR("Not implemented") },
  571. { "readline", (PyCFunction)reader_readline, METH_NOARGS, PyDoc_STR("Not implemented") },
  572. { "readlines", (PyCFunction)reader_readlines, METH_NOARGS, PyDoc_STR("Not implemented") },
  573. { "seek", (PyCFunction)reader_seek, METH_VARARGS, PyDoc_STR("Seek the stream") },
  574. { "seekable", (PyCFunction)reader_seekable, METH_NOARGS,
  575. PyDoc_STR("Returns True") },
  576. { "tell", (PyCFunction)reader_tell, METH_NOARGS,
  577. PyDoc_STR("Returns current number of bytes compressed") },
  578. { "writable", (PyCFunction)reader_writable, METH_NOARGS,
  579. PyDoc_STR("Returns False") },
  580. { "write", (PyCFunction)reader_write, METH_VARARGS, PyDoc_STR("unsupported operation") },
  581. { "writelines", (PyCFunction)reader_writelines, METH_VARARGS, PyDoc_STR("unsupported operation") },
  582. { NULL, NULL }
  583. };
  584. static PyMemberDef reader_members[] = {
  585. { "closed", T_BOOL, offsetof(ZstdDecompressionReader, closed),
  586. READONLY, "whether stream is closed" },
  587. { NULL }
  588. };
  589. PyTypeObject ZstdDecompressionReaderType = {
  590. PyVarObject_HEAD_INIT(NULL, 0)
  591. "zstd.ZstdDecompressionReader", /* tp_name */
  592. sizeof(ZstdDecompressionReader), /* tp_basicsize */
  593. 0, /* tp_itemsize */
  594. (destructor)reader_dealloc, /* tp_dealloc */
  595. 0, /* tp_print */
  596. 0, /* tp_getattr */
  597. 0, /* tp_setattr */
  598. 0, /* tp_compare */
  599. 0, /* tp_repr */
  600. 0, /* tp_as_number */
  601. 0, /* tp_as_sequence */
  602. 0, /* tp_as_mapping */
  603. 0, /* tp_hash */
  604. 0, /* tp_call */
  605. 0, /* tp_str */
  606. 0, /* tp_getattro */
  607. 0, /* tp_setattro */
  608. 0, /* tp_as_buffer */
  609. Py_TPFLAGS_DEFAULT, /* tp_flags */
  610. 0, /* tp_doc */
  611. 0, /* tp_traverse */
  612. 0, /* tp_clear */
  613. 0, /* tp_richcompare */
  614. 0, /* tp_weaklistoffset */
  615. reader_iter, /* tp_iter */
  616. reader_iternext, /* tp_iternext */
  617. reader_methods, /* tp_methods */
  618. reader_members, /* tp_members */
  619. 0, /* tp_getset */
  620. 0, /* tp_base */
  621. 0, /* tp_dict */
  622. 0, /* tp_descr_get */
  623. 0, /* tp_descr_set */
  624. 0, /* tp_dictoffset */
  625. 0, /* tp_init */
  626. 0, /* tp_alloc */
  627. PyType_GenericNew, /* tp_new */
  628. };
  629. void decompressionreader_module_init(PyObject* mod) {
  630. /* TODO make reader a sub-class of io.RawIOBase */
  631. Py_TYPE(&ZstdDecompressionReaderType) = &PyType_Type;
  632. if (PyType_Ready(&ZstdDecompressionReaderType) < 0) {
  633. return;
  634. }
  635. }