compressionreader.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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 compressionreader_dealloc(ZstdCompressionReader *self) {
  11. Py_XDECREF(self->compressor);
  12. Py_XDECREF(self->reader);
  13. if (self->buffer.buf) {
  14. PyBuffer_Release(&self->buffer);
  15. memset(&self->buffer, 0, sizeof(self->buffer));
  16. }
  17. PyObject_Del(self);
  18. }
  19. static ZstdCompressionReader *
  20. compressionreader_enter(ZstdCompressionReader *self) {
  21. if (self->entered) {
  22. PyErr_SetString(PyExc_ValueError, "cannot __enter__ multiple times");
  23. return NULL;
  24. }
  25. if (self->closed) {
  26. PyErr_SetString(PyExc_ValueError, "stream is closed");
  27. return NULL;
  28. }
  29. self->entered = 1;
  30. Py_INCREF(self);
  31. return self;
  32. }
  33. static PyObject *compressionreader_exit(ZstdCompressionReader *self,
  34. PyObject *args) {
  35. PyObject *exc_type;
  36. PyObject *exc_value;
  37. PyObject *exc_tb;
  38. PyObject *result;
  39. if (!PyArg_ParseTuple(args, "OOO:__exit__", &exc_type, &exc_value,
  40. &exc_tb)) {
  41. return NULL;
  42. }
  43. self->entered = 0;
  44. result = PyObject_CallMethod((PyObject *)self, "close", NULL);
  45. if (NULL == result) {
  46. return NULL;
  47. }
  48. /* Release resources associated with source. */
  49. Py_CLEAR(self->reader);
  50. if (self->buffer.buf) {
  51. PyBuffer_Release(&self->buffer);
  52. memset(&self->buffer, 0, sizeof(self->buffer));
  53. }
  54. Py_CLEAR(self->compressor);
  55. Py_RETURN_FALSE;
  56. }
  57. static PyObject *compressionreader_readable(ZstdCompressionReader *self) {
  58. Py_RETURN_TRUE;
  59. }
  60. static PyObject *compressionreader_writable(ZstdCompressionReader *self) {
  61. Py_RETURN_FALSE;
  62. }
  63. static PyObject *compressionreader_seekable(ZstdCompressionReader *self) {
  64. Py_RETURN_FALSE;
  65. }
  66. static PyObject *compressionreader_readline(PyObject *self, PyObject *args) {
  67. set_io_unsupported_operation();
  68. return NULL;
  69. }
  70. static PyObject *compressionreader_readlines(PyObject *self, PyObject *args) {
  71. set_io_unsupported_operation();
  72. return NULL;
  73. }
  74. static PyObject *compressionreader_write(PyObject *self, PyObject *args) {
  75. PyErr_SetString(PyExc_OSError, "stream is not writable");
  76. return NULL;
  77. }
  78. static PyObject *compressionreader_writelines(PyObject *self, PyObject *args) {
  79. PyErr_SetString(PyExc_OSError, "stream is not writable");
  80. return NULL;
  81. }
  82. static PyObject *compressionreader_isatty(PyObject *self) {
  83. Py_RETURN_FALSE;
  84. }
  85. static PyObject *compressionreader_flush(PyObject *self) {
  86. Py_RETURN_NONE;
  87. }
  88. static PyObject *compressionreader_close(ZstdCompressionReader *self) {
  89. if (self->closed) {
  90. Py_RETURN_NONE;
  91. }
  92. self->closed = 1;
  93. if (self->closefd && self->reader != NULL &&
  94. PyObject_HasAttrString(self->reader, "close")) {
  95. return PyObject_CallMethod(self->reader, "close", NULL);
  96. }
  97. Py_RETURN_NONE;
  98. }
  99. static PyObject *compressionreader_tell(ZstdCompressionReader *self) {
  100. /* TODO should this raise OSError since stream isn't seekable? */
  101. return PyLong_FromUnsignedLongLong(self->bytesCompressed);
  102. }
  103. int read_compressor_input(ZstdCompressionReader *self) {
  104. if (self->finishedInput) {
  105. return 0;
  106. }
  107. if (self->input.pos != self->input.size) {
  108. return 0;
  109. }
  110. if (self->reader) {
  111. Py_buffer buffer;
  112. assert(self->readResult == NULL);
  113. self->readResult =
  114. PyObject_CallMethod(self->reader, "read", "k", self->readSize);
  115. if (NULL == self->readResult) {
  116. return -1;
  117. }
  118. memset(&buffer, 0, sizeof(buffer));
  119. if (0 !=
  120. PyObject_GetBuffer(self->readResult, &buffer, PyBUF_CONTIG_RO)) {
  121. return -1;
  122. }
  123. /* EOF */
  124. if (0 == buffer.len) {
  125. self->finishedInput = 1;
  126. Py_CLEAR(self->readResult);
  127. }
  128. else {
  129. self->input.src = buffer.buf;
  130. self->input.size = buffer.len;
  131. self->input.pos = 0;
  132. }
  133. PyBuffer_Release(&buffer);
  134. }
  135. else {
  136. assert(self->buffer.buf);
  137. self->input.src = self->buffer.buf;
  138. self->input.size = self->buffer.len;
  139. self->input.pos = 0;
  140. }
  141. return 1;
  142. }
  143. int compress_input(ZstdCompressionReader *self, ZSTD_outBuffer *output) {
  144. size_t oldPos;
  145. size_t zresult;
  146. /* If we have data left over, consume it. */
  147. if (self->input.pos < self->input.size) {
  148. oldPos = output->pos;
  149. Py_BEGIN_ALLOW_THREADS zresult = ZSTD_compressStream2(
  150. self->compressor->cctx, output, &self->input, ZSTD_e_continue);
  151. Py_END_ALLOW_THREADS
  152. self->bytesCompressed += output->pos - oldPos;
  153. /* Input exhausted. Clear out state tracking. */
  154. if (self->input.pos == self->input.size) {
  155. memset(&self->input, 0, sizeof(self->input));
  156. Py_CLEAR(self->readResult);
  157. if (self->buffer.buf) {
  158. self->finishedInput = 1;
  159. }
  160. }
  161. if (ZSTD_isError(zresult)) {
  162. PyErr_Format(ZstdError, "zstd compress error: %s",
  163. ZSTD_getErrorName(zresult));
  164. return -1;
  165. }
  166. }
  167. if (output->pos && output->pos == output->size) {
  168. return 1;
  169. }
  170. else {
  171. return 0;
  172. }
  173. }
  174. static PyObject *compressionreader_read(ZstdCompressionReader *self,
  175. PyObject *args, PyObject *kwargs) {
  176. static char *kwlist[] = {"size", NULL};
  177. Py_ssize_t size = -1;
  178. PyObject *result = NULL;
  179. char *resultBuffer;
  180. Py_ssize_t resultSize;
  181. size_t zresult;
  182. size_t oldPos;
  183. int readResult, compressResult;
  184. if (self->closed) {
  185. PyErr_SetString(PyExc_ValueError, "stream is closed");
  186. return NULL;
  187. }
  188. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|n", kwlist, &size)) {
  189. return NULL;
  190. }
  191. if (size < -1) {
  192. PyErr_SetString(PyExc_ValueError,
  193. "cannot read negative amounts less than -1");
  194. return NULL;
  195. }
  196. if (size == -1) {
  197. return PyObject_CallMethod((PyObject *)self, "readall", NULL);
  198. }
  199. if (self->finishedOutput || size == 0) {
  200. return PyBytes_FromStringAndSize("", 0);
  201. }
  202. result = PyBytes_FromStringAndSize(NULL, size);
  203. if (NULL == result) {
  204. return NULL;
  205. }
  206. PyBytes_AsStringAndSize(result, &resultBuffer, &resultSize);
  207. self->output.dst = resultBuffer;
  208. self->output.size = resultSize;
  209. self->output.pos = 0;
  210. readinput:
  211. compressResult = compress_input(self, &self->output);
  212. if (-1 == compressResult) {
  213. Py_XDECREF(result);
  214. return NULL;
  215. }
  216. else if (0 == compressResult) {
  217. /* There is room in the output. We fall through to below, which will
  218. * either get more input for us or will attempt to end the stream.
  219. */
  220. }
  221. else if (1 == compressResult) {
  222. memset(&self->output, 0, sizeof(self->output));
  223. return result;
  224. }
  225. else {
  226. assert(0);
  227. }
  228. readResult = read_compressor_input(self);
  229. if (-1 == readResult) {
  230. return NULL;
  231. }
  232. else if (0 == readResult) {
  233. }
  234. else if (1 == readResult) {
  235. }
  236. else {
  237. assert(0);
  238. }
  239. if (self->input.size) {
  240. goto readinput;
  241. }
  242. /* Else EOF */
  243. oldPos = self->output.pos;
  244. zresult = ZSTD_compressStream2(self->compressor->cctx, &self->output,
  245. &self->input, ZSTD_e_end);
  246. self->bytesCompressed += self->output.pos - oldPos;
  247. if (ZSTD_isError(zresult)) {
  248. PyErr_Format(ZstdError, "error ending compression stream: %s",
  249. ZSTD_getErrorName(zresult));
  250. Py_XDECREF(result);
  251. return NULL;
  252. }
  253. assert(self->output.pos);
  254. if (0 == zresult) {
  255. self->finishedOutput = 1;
  256. }
  257. if (safe_pybytes_resize(&result, self->output.pos)) {
  258. Py_XDECREF(result);
  259. return NULL;
  260. }
  261. memset(&self->output, 0, sizeof(self->output));
  262. return result;
  263. }
  264. static PyObject *compressionreader_read1(ZstdCompressionReader *self,
  265. PyObject *args, PyObject *kwargs) {
  266. static char *kwlist[] = {"size", NULL};
  267. Py_ssize_t size = -1;
  268. PyObject *result = NULL;
  269. char *resultBuffer;
  270. Py_ssize_t resultSize;
  271. ZSTD_outBuffer output;
  272. int compressResult;
  273. size_t oldPos;
  274. size_t zresult;
  275. if (self->closed) {
  276. PyErr_SetString(PyExc_ValueError, "stream is closed");
  277. return NULL;
  278. }
  279. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|n:read1", kwlist, &size)) {
  280. return NULL;
  281. }
  282. if (size < -1) {
  283. PyErr_SetString(PyExc_ValueError,
  284. "cannot read negative amounts less than -1");
  285. return NULL;
  286. }
  287. if (self->finishedOutput || size == 0) {
  288. return PyBytes_FromStringAndSize("", 0);
  289. }
  290. if (size == -1) {
  291. size = ZSTD_CStreamOutSize();
  292. }
  293. result = PyBytes_FromStringAndSize(NULL, size);
  294. if (NULL == result) {
  295. return NULL;
  296. }
  297. PyBytes_AsStringAndSize(result, &resultBuffer, &resultSize);
  298. output.dst = resultBuffer;
  299. output.size = resultSize;
  300. output.pos = 0;
  301. /* read1() is supposed to use at most 1 read() from the underlying stream.
  302. However, we can't satisfy this requirement with compression because
  303. not every input will generate output. We /could/ flush the compressor,
  304. but this may not be desirable. We allow multiple read() from the
  305. underlying stream. But unlike read(), we return as soon as output data
  306. is available.
  307. */
  308. compressResult = compress_input(self, &output);
  309. if (-1 == compressResult) {
  310. Py_XDECREF(result);
  311. return NULL;
  312. }
  313. else if (0 == compressResult || 1 == compressResult) {
  314. }
  315. else {
  316. assert(0);
  317. }
  318. if (output.pos) {
  319. goto finally;
  320. }
  321. while (!self->finishedInput) {
  322. int readResult = read_compressor_input(self);
  323. if (-1 == readResult) {
  324. Py_XDECREF(result);
  325. return NULL;
  326. }
  327. else if (0 == readResult || 1 == readResult) {
  328. }
  329. else {
  330. assert(0);
  331. }
  332. compressResult = compress_input(self, &output);
  333. if (-1 == compressResult) {
  334. Py_XDECREF(result);
  335. return NULL;
  336. }
  337. else if (0 == compressResult || 1 == compressResult) {
  338. }
  339. else {
  340. assert(0);
  341. }
  342. if (output.pos) {
  343. goto finally;
  344. }
  345. }
  346. /* EOF */
  347. oldPos = output.pos;
  348. zresult = ZSTD_compressStream2(self->compressor->cctx, &output,
  349. &self->input, ZSTD_e_end);
  350. self->bytesCompressed += output.pos - oldPos;
  351. if (ZSTD_isError(zresult)) {
  352. PyErr_Format(ZstdError, "error ending compression stream: %s",
  353. ZSTD_getErrorName(zresult));
  354. Py_XDECREF(result);
  355. return NULL;
  356. }
  357. if (zresult == 0) {
  358. self->finishedOutput = 1;
  359. }
  360. finally:
  361. if (result) {
  362. if (safe_pybytes_resize(&result, output.pos)) {
  363. Py_XDECREF(result);
  364. return NULL;
  365. }
  366. }
  367. return result;
  368. }
  369. static PyObject *compressionreader_readall(PyObject *self) {
  370. PyObject *chunks = NULL;
  371. PyObject *empty = NULL;
  372. PyObject *result = NULL;
  373. /* Our strategy is to collect chunks into a list then join all the
  374. * chunks at the end. We could potentially use e.g. an io.BytesIO. But
  375. * this feels simple enough to implement and avoids potentially expensive
  376. * reallocations of large buffers.
  377. */
  378. chunks = PyList_New(0);
  379. if (NULL == chunks) {
  380. return NULL;
  381. }
  382. while (1) {
  383. PyObject *chunk = PyObject_CallMethod(self, "read", "i", 1048576);
  384. if (NULL == chunk) {
  385. Py_DECREF(chunks);
  386. return NULL;
  387. }
  388. if (!PyBytes_Size(chunk)) {
  389. Py_DECREF(chunk);
  390. break;
  391. }
  392. if (PyList_Append(chunks, chunk)) {
  393. Py_DECREF(chunk);
  394. Py_DECREF(chunks);
  395. return NULL;
  396. }
  397. Py_DECREF(chunk);
  398. }
  399. empty = PyBytes_FromStringAndSize("", 0);
  400. if (NULL == empty) {
  401. Py_DECREF(chunks);
  402. return NULL;
  403. }
  404. result = PyObject_CallMethod(empty, "join", "O", chunks);
  405. Py_DECREF(empty);
  406. Py_DECREF(chunks);
  407. return result;
  408. }
  409. static PyObject *compressionreader_readinto(ZstdCompressionReader *self,
  410. PyObject *args) {
  411. Py_buffer dest;
  412. ZSTD_outBuffer output;
  413. int readResult, compressResult;
  414. PyObject *result = NULL;
  415. size_t zresult;
  416. size_t oldPos;
  417. if (self->closed) {
  418. PyErr_SetString(PyExc_ValueError, "stream is closed");
  419. return NULL;
  420. }
  421. if (self->finishedOutput) {
  422. return PyLong_FromLong(0);
  423. }
  424. if (!PyArg_ParseTuple(args, "w*:readinto", &dest)) {
  425. return NULL;
  426. }
  427. output.dst = dest.buf;
  428. output.size = dest.len;
  429. output.pos = 0;
  430. compressResult = compress_input(self, &output);
  431. if (-1 == compressResult) {
  432. goto finally;
  433. }
  434. else if (0 == compressResult) {
  435. }
  436. else if (1 == compressResult) {
  437. result = PyLong_FromSize_t(output.pos);
  438. goto finally;
  439. }
  440. else {
  441. assert(0);
  442. }
  443. while (!self->finishedInput) {
  444. readResult = read_compressor_input(self);
  445. if (-1 == readResult) {
  446. goto finally;
  447. }
  448. else if (0 == readResult || 1 == readResult) {
  449. }
  450. else {
  451. assert(0);
  452. }
  453. compressResult = compress_input(self, &output);
  454. if (-1 == compressResult) {
  455. goto finally;
  456. }
  457. else if (0 == compressResult) {
  458. }
  459. else if (1 == compressResult) {
  460. result = PyLong_FromSize_t(output.pos);
  461. goto finally;
  462. }
  463. else {
  464. assert(0);
  465. }
  466. }
  467. /* EOF */
  468. oldPos = output.pos;
  469. zresult = ZSTD_compressStream2(self->compressor->cctx, &output,
  470. &self->input, ZSTD_e_end);
  471. self->bytesCompressed += self->output.pos - oldPos;
  472. if (ZSTD_isError(zresult)) {
  473. PyErr_Format(ZstdError, "error ending compression stream: %s",
  474. ZSTD_getErrorName(zresult));
  475. goto finally;
  476. }
  477. assert(output.pos);
  478. if (0 == zresult) {
  479. self->finishedOutput = 1;
  480. }
  481. result = PyLong_FromSize_t(output.pos);
  482. finally:
  483. PyBuffer_Release(&dest);
  484. return result;
  485. }
  486. static PyObject *compressionreader_readinto1(ZstdCompressionReader *self,
  487. PyObject *args) {
  488. Py_buffer dest;
  489. PyObject *result = NULL;
  490. ZSTD_outBuffer output;
  491. int compressResult;
  492. size_t oldPos;
  493. size_t zresult;
  494. if (self->closed) {
  495. PyErr_SetString(PyExc_ValueError, "stream is closed");
  496. return NULL;
  497. }
  498. if (self->finishedOutput) {
  499. return PyLong_FromLong(0);
  500. }
  501. if (!PyArg_ParseTuple(args, "w*:readinto1", &dest)) {
  502. return NULL;
  503. }
  504. output.dst = dest.buf;
  505. output.size = dest.len;
  506. output.pos = 0;
  507. compressResult = compress_input(self, &output);
  508. if (-1 == compressResult) {
  509. goto finally;
  510. }
  511. else if (0 == compressResult || 1 == compressResult) {
  512. }
  513. else {
  514. assert(0);
  515. }
  516. if (output.pos) {
  517. result = PyLong_FromSize_t(output.pos);
  518. goto finally;
  519. }
  520. while (!self->finishedInput) {
  521. int readResult = read_compressor_input(self);
  522. if (-1 == readResult) {
  523. goto finally;
  524. }
  525. else if (0 == readResult || 1 == readResult) {
  526. }
  527. else {
  528. assert(0);
  529. }
  530. compressResult = compress_input(self, &output);
  531. if (-1 == compressResult) {
  532. goto finally;
  533. }
  534. else if (0 == compressResult) {
  535. }
  536. else if (1 == compressResult) {
  537. result = PyLong_FromSize_t(output.pos);
  538. goto finally;
  539. }
  540. else {
  541. assert(0);
  542. }
  543. /* If we produced output and we're not done with input, emit
  544. * that output now, as we've hit restrictions of read1().
  545. */
  546. if (output.pos && !self->finishedInput) {
  547. result = PyLong_FromSize_t(output.pos);
  548. goto finally;
  549. }
  550. /* Otherwise we either have no output or we've exhausted the
  551. * input. Either we try to get more input or we fall through
  552. * to EOF below */
  553. }
  554. /* EOF */
  555. oldPos = output.pos;
  556. zresult = ZSTD_compressStream2(self->compressor->cctx, &output,
  557. &self->input, ZSTD_e_end);
  558. self->bytesCompressed += self->output.pos - oldPos;
  559. if (ZSTD_isError(zresult)) {
  560. PyErr_Format(ZstdError, "error ending compression stream: %s",
  561. ZSTD_getErrorName(zresult));
  562. goto finally;
  563. }
  564. assert(output.pos);
  565. if (0 == zresult) {
  566. self->finishedOutput = 1;
  567. }
  568. result = PyLong_FromSize_t(output.pos);
  569. finally:
  570. PyBuffer_Release(&dest);
  571. return result;
  572. }
  573. static PyObject *compressionreader_iter(PyObject *self) {
  574. set_io_unsupported_operation();
  575. return NULL;
  576. }
  577. static PyObject *compressionreader_iternext(PyObject *self) {
  578. set_io_unsupported_operation();
  579. return NULL;
  580. }
  581. static PyMethodDef compressionreader_methods[] = {
  582. {"__enter__", (PyCFunction)compressionreader_enter, METH_NOARGS,
  583. PyDoc_STR("Enter a compression context")},
  584. {"__exit__", (PyCFunction)compressionreader_exit, METH_VARARGS,
  585. PyDoc_STR("Exit a compression context")},
  586. {"close", (PyCFunction)compressionreader_close, METH_NOARGS,
  587. PyDoc_STR("Close the stream so it cannot perform any more operations")},
  588. {"flush", (PyCFunction)compressionreader_flush, METH_NOARGS,
  589. PyDoc_STR("no-ops")},
  590. {"isatty", (PyCFunction)compressionreader_isatty, METH_NOARGS,
  591. PyDoc_STR("Returns False")},
  592. {"readable", (PyCFunction)compressionreader_readable, METH_NOARGS,
  593. PyDoc_STR("Returns True")},
  594. {"read", (PyCFunction)compressionreader_read, METH_VARARGS | METH_KEYWORDS,
  595. PyDoc_STR("read compressed data")},
  596. {"read1", (PyCFunction)compressionreader_read1,
  597. METH_VARARGS | METH_KEYWORDS, NULL},
  598. {"readall", (PyCFunction)compressionreader_readall, METH_NOARGS,
  599. PyDoc_STR("Not implemented")},
  600. {"readinto", (PyCFunction)compressionreader_readinto, METH_VARARGS, NULL},
  601. {"readinto1", (PyCFunction)compressionreader_readinto1, METH_VARARGS, NULL},
  602. {"readline", (PyCFunction)compressionreader_readline, METH_VARARGS,
  603. PyDoc_STR("Not implemented")},
  604. {"readlines", (PyCFunction)compressionreader_readlines, METH_VARARGS,
  605. PyDoc_STR("Not implemented")},
  606. {"seekable", (PyCFunction)compressionreader_seekable, METH_NOARGS,
  607. PyDoc_STR("Returns False")},
  608. {"tell", (PyCFunction)compressionreader_tell, METH_NOARGS,
  609. PyDoc_STR("Returns current number of bytes compressed")},
  610. {"writable", (PyCFunction)compressionreader_writable, METH_NOARGS,
  611. PyDoc_STR("Returns False")},
  612. {"write", compressionreader_write, METH_VARARGS,
  613. PyDoc_STR("Raises OSError")},
  614. {"writelines", compressionreader_writelines, METH_VARARGS,
  615. PyDoc_STR("Not implemented")},
  616. {NULL, NULL}};
  617. static PyMemberDef compressionreader_members[] = {
  618. {"closed", T_BOOL, offsetof(ZstdCompressionReader, closed), READONLY,
  619. "whether stream is closed"},
  620. {NULL}};
  621. PyType_Slot ZstdCompressionReaderSlots[] = {
  622. {Py_tp_dealloc, compressionreader_dealloc},
  623. {Py_tp_iter, compressionreader_iter},
  624. {Py_tp_iternext, compressionreader_iternext},
  625. {Py_tp_methods, compressionreader_methods},
  626. {Py_tp_members, compressionreader_members},
  627. {Py_tp_new, PyType_GenericNew},
  628. {0, NULL},
  629. };
  630. PyType_Spec ZstdCompressionReaderSpec = {
  631. "zstd.ZstdCompressionReader",
  632. sizeof(ZstdCompressionReader),
  633. 0,
  634. Py_TPFLAGS_DEFAULT,
  635. ZstdCompressionReaderSlots,
  636. };
  637. PyTypeObject *ZstdCompressionReaderType;
  638. void compressionreader_module_init(PyObject *mod) {
  639. /* TODO make reader a sub-class of io.RawIOBase */
  640. ZstdCompressionReaderType =
  641. (PyTypeObject *)PyType_FromSpec(&ZstdCompressionReaderSpec);
  642. if (PyType_Ready(ZstdCompressionReaderType) < 0) {
  643. return;
  644. }
  645. Py_INCREF((PyObject *)ZstdCompressionReaderType);
  646. PyModule_AddObject(mod, "ZstdCompressionReader",
  647. (PyObject *)ZstdCompressionReaderType);
  648. }