contrib/python-zstandard/c-ext/decompressionreader.c
changeset 40121 73fef626dae3
parent 37495 b1fb341d8a61
child 42070 675775c33ab6
equal deleted inserted replaced
40120:89742f1fa6cb 40121:73fef626dae3
    45 	if (self->entered) {
    45 	if (self->entered) {
    46 		PyErr_SetString(PyExc_ValueError, "cannot __enter__ multiple times");
    46 		PyErr_SetString(PyExc_ValueError, "cannot __enter__ multiple times");
    47 		return NULL;
    47 		return NULL;
    48 	}
    48 	}
    49 
    49 
    50 	if (ensure_dctx(self->decompressor, 1)) {
       
    51 		return NULL;
       
    52 	}
       
    53 
       
    54 	self->entered = 1;
    50 	self->entered = 1;
    55 
    51 
    56 	Py_INCREF(self);
    52 	Py_INCREF(self);
    57 	return self;
    53 	return self;
    58 }
    54 }
    94 }
    90 }
    95 
    91 
    96 static PyObject* reader_close(ZstdDecompressionReader* self) {
    92 static PyObject* reader_close(ZstdDecompressionReader* self) {
    97 	self->closed = 1;
    93 	self->closed = 1;
    98 	Py_RETURN_NONE;
    94 	Py_RETURN_NONE;
    99 }
       
   100 
       
   101 static PyObject* reader_closed(ZstdDecompressionReader* self) {
       
   102 	if (self->closed) {
       
   103 		Py_RETURN_TRUE;
       
   104 	}
       
   105 	else {
       
   106 		Py_RETURN_FALSE;
       
   107 	}
       
   108 }
    95 }
   109 
    96 
   110 static PyObject* reader_flush(PyObject* self) {
    97 static PyObject* reader_flush(PyObject* self) {
   111 	Py_RETURN_NONE;
    98 	Py_RETURN_NONE;
   112 }
    99 }
   125 	PyObject* result = NULL;
   112 	PyObject* result = NULL;
   126 	char* resultBuffer;
   113 	char* resultBuffer;
   127 	Py_ssize_t resultSize;
   114 	Py_ssize_t resultSize;
   128 	ZSTD_outBuffer output;
   115 	ZSTD_outBuffer output;
   129 	size_t zresult;
   116 	size_t zresult;
   130 
       
   131 	if (!self->entered) {
       
   132 		PyErr_SetString(ZstdError, "read() must be called from an active context manager");
       
   133 		return NULL;
       
   134 	}
       
   135 
   117 
   136 	if (self->closed) {
   118 	if (self->closed) {
   137 		PyErr_SetString(PyExc_ValueError, "stream is closed");
   119 		PyErr_SetString(PyExc_ValueError, "stream is closed");
   138 		return NULL;
   120 		return NULL;
   139 	}
   121 	}
   279 	Py_ssize_t pos;
   261 	Py_ssize_t pos;
   280 	int whence = 0;
   262 	int whence = 0;
   281 	unsigned long long readAmount = 0;
   263 	unsigned long long readAmount = 0;
   282 	size_t defaultOutSize = ZSTD_DStreamOutSize();
   264 	size_t defaultOutSize = ZSTD_DStreamOutSize();
   283 
   265 
   284 	if (!self->entered) {
       
   285 		PyErr_SetString(ZstdError, "seek() must be called from an active context manager");
       
   286 		return NULL;
       
   287 	}
       
   288 
       
   289 	if (self->closed) {
   266 	if (self->closed) {
   290 		PyErr_SetString(PyExc_ValueError, "stream is closed");
   267 		PyErr_SetString(PyExc_ValueError, "stream is closed");
   291 		return NULL;
   268 		return NULL;
   292 	}
   269 	}
   293 
   270 
   382 	PyDoc_STR("Enter a compression context") },
   359 	PyDoc_STR("Enter a compression context") },
   383 	{ "__exit__", (PyCFunction)reader_exit, METH_VARARGS,
   360 	{ "__exit__", (PyCFunction)reader_exit, METH_VARARGS,
   384 	PyDoc_STR("Exit a compression context") },
   361 	PyDoc_STR("Exit a compression context") },
   385 	{ "close", (PyCFunction)reader_close, METH_NOARGS,
   362 	{ "close", (PyCFunction)reader_close, METH_NOARGS,
   386 	PyDoc_STR("Close the stream so it cannot perform any more operations") },
   363 	PyDoc_STR("Close the stream so it cannot perform any more operations") },
   387 	{ "closed", (PyCFunction)reader_closed, METH_NOARGS,
       
   388 	PyDoc_STR("Whether stream is closed") },
       
   389 	{ "flush", (PyCFunction)reader_flush, METH_NOARGS, PyDoc_STR("no-ops") },
   364 	{ "flush", (PyCFunction)reader_flush, METH_NOARGS, PyDoc_STR("no-ops") },
   390 	{ "isatty", (PyCFunction)reader_isatty, METH_NOARGS, PyDoc_STR("Returns False") },
   365 	{ "isatty", (PyCFunction)reader_isatty, METH_NOARGS, PyDoc_STR("Returns False") },
   391 	{ "readable", (PyCFunction)reader_readable, METH_NOARGS,
   366 	{ "readable", (PyCFunction)reader_readable, METH_NOARGS,
   392 	PyDoc_STR("Returns True") },
   367 	PyDoc_STR("Returns True") },
   393 	{ "read", (PyCFunction)reader_read, METH_VARARGS | METH_KEYWORDS,
   368 	{ "read", (PyCFunction)reader_read, METH_VARARGS | METH_KEYWORDS,
   403 	{ "writable", (PyCFunction)reader_writable, METH_NOARGS,
   378 	{ "writable", (PyCFunction)reader_writable, METH_NOARGS,
   404 	PyDoc_STR("Returns False") },
   379 	PyDoc_STR("Returns False") },
   405 	{ "write", (PyCFunction)reader_write, METH_VARARGS, PyDoc_STR("unsupported operation") },
   380 	{ "write", (PyCFunction)reader_write, METH_VARARGS, PyDoc_STR("unsupported operation") },
   406 	{ "writelines", (PyCFunction)reader_writelines, METH_VARARGS, PyDoc_STR("unsupported operation") },
   381 	{ "writelines", (PyCFunction)reader_writelines, METH_VARARGS, PyDoc_STR("unsupported operation") },
   407 	{ NULL, NULL }
   382 	{ NULL, NULL }
       
   383 };
       
   384 
       
   385 static PyMemberDef reader_members[] = {
       
   386 	{ "closed", T_BOOL, offsetof(ZstdDecompressionReader, closed),
       
   387 	  READONLY, "whether stream is closed" },
       
   388 	{ NULL }
   408 };
   389 };
   409 
   390 
   410 PyTypeObject ZstdDecompressionReaderType = {
   391 PyTypeObject ZstdDecompressionReaderType = {
   411 	PyVarObject_HEAD_INIT(NULL, 0)
   392 	PyVarObject_HEAD_INIT(NULL, 0)
   412 	"zstd.ZstdDecompressionReader", /* tp_name */
   393 	"zstd.ZstdDecompressionReader", /* tp_name */
   434 	0, /* tp_richcompare */
   415 	0, /* tp_richcompare */
   435 	0, /* tp_weaklistoffset */
   416 	0, /* tp_weaklistoffset */
   436 	reader_iter, /* tp_iter */
   417 	reader_iter, /* tp_iter */
   437 	reader_iternext, /* tp_iternext */
   418 	reader_iternext, /* tp_iternext */
   438 	reader_methods, /* tp_methods */
   419 	reader_methods, /* tp_methods */
   439 	0, /* tp_members */
   420 	reader_members, /* tp_members */
   440 	0, /* tp_getset */
   421 	0, /* tp_getset */
   441 	0, /* tp_base */
   422 	0, /* tp_base */
   442 	0, /* tp_dict */
   423 	0, /* tp_dict */
   443 	0, /* tp_descr_get */
   424 	0, /* tp_descr_get */
   444 	0, /* tp_descr_set */
   425 	0, /* tp_descr_set */