Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1bc4bb2

Browse files
Issue #20193: The _bz2 module now uses Argument Clinic.
1 parent 8d00d73 commit 1bc4bb2

2 files changed

Lines changed: 241 additions & 72 deletions

File tree

Modules/_bz2module.c

Lines changed: 92 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -196,44 +196,61 @@ compress(BZ2Compressor *c, char *data, size_t len, int action)
196196
return NULL;
197197
}
198198

199-
PyDoc_STRVAR(BZ2Compressor_compress__doc__,
200-
"compress(data) -> bytes\n"
201-
"\n"
202-
"Provide data to the compressor object. Returns a chunk of\n"
203-
"compressed data if possible, or b'' otherwise.\n"
204-
"\n"
205-
"When you have finished providing data to the compressor, call the\n"
206-
"flush() method to finish the compression process.\n");
199+
/*[clinic input]
200+
output preset file
201+
module _bz2
202+
class _bz2.BZ2Compressor
203+
class _bz2.BZ2Decompressor
204+
[clinic start generated code]*/
205+
/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
206+
207+
#include "_bz2module.clinic.c"
208+
209+
/*[clinic input]
210+
_bz2.BZ2Compressor.compress
211+
212+
self: self(type="BZ2Compressor *")
213+
data: Py_buffer
214+
/
215+
216+
Provide data to the compressor object.
217+
218+
Returns a chunk of compressed data if possible, or b'' otherwise.
219+
220+
When you have finished providing data to the compressor, call the
221+
flush() method to finish the compression process.
222+
[clinic start generated code]*/
207223

208224
static PyObject *
209-
BZ2Compressor_compress(BZ2Compressor *self, PyObject *args)
225+
_bz2_BZ2Compressor_compress_impl(BZ2Compressor *self, Py_buffer *data)
226+
/*[clinic end generated code: checksum=59365426e941fbcc4c7a4d0eef85ca7e19196eaa]*/
210227
{
211-
Py_buffer buffer;
212228
PyObject *result = NULL;
213229

214-
if (!PyArg_ParseTuple(args, "y*:compress", &buffer))
215-
return NULL;
216-
217230
ACQUIRE_LOCK(self);
218231
if (self->flushed)
219232
PyErr_SetString(PyExc_ValueError, "Compressor has been flushed");
220233
else
221-
result = compress(self, buffer.buf, buffer.len, BZ_RUN);
234+
result = compress(self, data->buf, data->len, BZ_RUN);
222235
RELEASE_LOCK(self);
223-
PyBuffer_Release(&buffer);
224236
return result;
225237
}
226238

227-
PyDoc_STRVAR(BZ2Compressor_flush__doc__,
228-
"flush() -> bytes\n"
229-
"\n"
230-
"Finish the compression process. Returns the compressed data left\n"
231-
"in internal buffers.\n"
232-
"\n"
233-
"The compressor object may not be used after this method is called.\n");
239+
/*[clinic input]
240+
_bz2.BZ2Compressor.flush
241+
242+
self: self(type="BZ2Compressor *")
243+
244+
Finish the compression process.
245+
246+
Returns the compressed data left in internal buffers.
247+
248+
The compressor object may not be used after this method is called.
249+
[clinic start generated code]*/
234250

235251
static PyObject *
236-
BZ2Compressor_flush(BZ2Compressor *self, PyObject *noargs)
252+
_bz2_BZ2Compressor_flush_impl(BZ2Compressor *self)
253+
/*[clinic end generated code: checksum=3ef03fc1b092a701b382b97096c7fd50db87190b]*/
237254
{
238255
PyObject *result = NULL;
239256

@@ -274,14 +291,25 @@ BZ2_Free(void* ctx, void *ptr)
274291
PyMem_RawFree(ptr);
275292
}
276293

294+
/*[clinic input]
295+
_bz2.BZ2Compressor.__init__
296+
297+
self: self(type="BZ2Compressor *")
298+
compresslevel: int = 9
299+
Compression level, as a number between 1 and 9.
300+
/
301+
302+
Create a compressor object for compressing data incrementally.
303+
304+
For one-shot compression, use the compress() function instead.
305+
[clinic start generated code]*/
306+
277307
static int
278-
BZ2Compressor_init(BZ2Compressor *self, PyObject *args, PyObject *kwargs)
308+
_bz2_BZ2Compressor___init___impl(BZ2Compressor *self, int compresslevel)
309+
/*[clinic end generated code: checksum=c4e6adfd02963827075a1cc9309dc6df184b1246]*/
279310
{
280-
int compresslevel = 9;
281311
int bzerror;
282312

283-
if (!PyArg_ParseTuple(args, "|i:BZ2Compressor", &compresslevel))
284-
return -1;
285313
if (!(1 <= compresslevel && compresslevel <= 9)) {
286314
PyErr_SetString(PyExc_ValueError,
287315
"compresslevel must be between 1 and 9");
@@ -325,22 +353,12 @@ BZ2Compressor_dealloc(BZ2Compressor *self)
325353
}
326354

327355
static PyMethodDef BZ2Compressor_methods[] = {
328-
{"compress", (PyCFunction)BZ2Compressor_compress, METH_VARARGS,
329-
BZ2Compressor_compress__doc__},
330-
{"flush", (PyCFunction)BZ2Compressor_flush, METH_NOARGS,
331-
BZ2Compressor_flush__doc__},
356+
_BZ2_BZ2COMPRESSOR_COMPRESS_METHODDEF
357+
_BZ2_BZ2COMPRESSOR_FLUSH_METHODDEF
332358
{"__getstate__", (PyCFunction)BZ2Compressor_getstate, METH_NOARGS},
333359
{NULL}
334360
};
335361

336-
PyDoc_STRVAR(BZ2Compressor__doc__,
337-
"BZ2Compressor(compresslevel=9)\n"
338-
"\n"
339-
"Create a compressor object for compressing data incrementally.\n"
340-
"\n"
341-
"compresslevel, if given, must be a number between 1 and 9.\n"
342-
"\n"
343-
"For one-shot compression, use the compress() function instead.\n");
344362

345363
static PyTypeObject BZ2Compressor_Type = {
346364
PyVarObject_HEAD_INIT(NULL, 0)
@@ -363,7 +381,7 @@ static PyTypeObject BZ2Compressor_Type = {
363381
0, /* tp_setattro */
364382
0, /* tp_as_buffer */
365383
Py_TPFLAGS_DEFAULT, /* tp_flags */
366-
BZ2Compressor__doc__, /* tp_doc */
384+
_bz2_BZ2Compressor___init____doc__, /* tp_doc */
367385
0, /* tp_traverse */
368386
0, /* tp_clear */
369387
0, /* tp_richcompare */
@@ -378,7 +396,7 @@ static PyTypeObject BZ2Compressor_Type = {
378396
0, /* tp_descr_get */
379397
0, /* tp_descr_set */
380398
0, /* tp_dictoffset */
381-
(initproc)BZ2Compressor_init, /* tp_init */
399+
_bz2_BZ2Compressor___init__, /* tp_init */
382400
0, /* tp_alloc */
383401
PyType_GenericNew, /* tp_new */
384402
};
@@ -451,32 +469,34 @@ decompress(BZ2Decompressor *d, char *data, size_t len)
451469
return NULL;
452470
}
453471

454-
PyDoc_STRVAR(BZ2Decompressor_decompress__doc__,
455-
"decompress(data) -> bytes\n"
456-
"\n"
457-
"Provide data to the decompressor object. Returns a chunk of\n"
458-
"decompressed data if possible, or b'' otherwise.\n"
459-
"\n"
460-
"Attempting to decompress data after the end of stream is reached\n"
461-
"raises an EOFError. Any data found after the end of the stream\n"
462-
"is ignored and saved in the unused_data attribute.\n");
472+
/*[clinic input]
473+
_bz2.BZ2Decompressor.decompress
474+
475+
self: self(type="BZ2Decompressor *")
476+
data: Py_buffer
477+
/
478+
479+
Provide data to the decompressor object.
480+
481+
Returns a chunk of decompressed data if possible, or b'' otherwise.
482+
483+
Attempting to decompress data after the end of stream is reached
484+
raises an EOFError. Any data found after the end of the stream
485+
is ignored and saved in the unused_data attribute.
486+
[clinic start generated code]*/
463487

464488
static PyObject *
465-
BZ2Decompressor_decompress(BZ2Decompressor *self, PyObject *args)
489+
_bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data)
490+
/*[clinic end generated code: checksum=086e4b99e60cb3f67c0481959591eae0735320bc]*/
466491
{
467-
Py_buffer buffer;
468492
PyObject *result = NULL;
469493

470-
if (!PyArg_ParseTuple(args, "y*:decompress", &buffer))
471-
return NULL;
472-
473494
ACQUIRE_LOCK(self);
474495
if (self->eof)
475496
PyErr_SetString(PyExc_EOFError, "End of stream already reached");
476497
else
477-
result = decompress(self, buffer.buf, buffer.len);
498+
result = decompress(self, data->buf, data->len);
478499
RELEASE_LOCK(self);
479-
PyBuffer_Release(&buffer);
480500
return result;
481501
}
482502

@@ -488,14 +508,22 @@ BZ2Decompressor_getstate(BZ2Decompressor *self, PyObject *noargs)
488508
return NULL;
489509
}
490510

511+
/*[clinic input]
512+
_bz2.BZ2Decompressor.__init__
513+
514+
self: self(type="BZ2Decompressor *")
515+
516+
Create a decompressor object for decompressing data incrementally.
517+
518+
For one-shot decompression, use the decompress() function instead.
519+
[clinic start generated code]*/
520+
491521
static int
492-
BZ2Decompressor_init(BZ2Decompressor *self, PyObject *args, PyObject *kwargs)
522+
_bz2_BZ2Decompressor___init___impl(BZ2Decompressor *self)
523+
/*[clinic end generated code: checksum=e4d2b9bb866ab8f1f4a8bb786ddb5b614ce323c0]*/
493524
{
494525
int bzerror;
495526

496-
if (!PyArg_ParseTuple(args, ":BZ2Decompressor"))
497-
return -1;
498-
499527
#ifdef WITH_THREAD
500528
self->lock = PyThread_allocate_lock();
501529
if (self->lock == NULL) {
@@ -536,8 +564,7 @@ BZ2Decompressor_dealloc(BZ2Decompressor *self)
536564
}
537565

538566
static PyMethodDef BZ2Decompressor_methods[] = {
539-
{"decompress", (PyCFunction)BZ2Decompressor_decompress, METH_VARARGS,
540-
BZ2Decompressor_decompress__doc__},
567+
_BZ2_BZ2DECOMPRESSOR_DECOMPRESS_METHODDEF
541568
{"__getstate__", (PyCFunction)BZ2Decompressor_getstate, METH_NOARGS},
542569
{NULL}
543570
};
@@ -556,13 +583,6 @@ static PyMemberDef BZ2Decompressor_members[] = {
556583
{NULL}
557584
};
558585

559-
PyDoc_STRVAR(BZ2Decompressor__doc__,
560-
"BZ2Decompressor()\n"
561-
"\n"
562-
"Create a decompressor object for decompressing data incrementally.\n"
563-
"\n"
564-
"For one-shot decompression, use the decompress() function instead.\n");
565-
566586
static PyTypeObject BZ2Decompressor_Type = {
567587
PyVarObject_HEAD_INIT(NULL, 0)
568588
"_bz2.BZ2Decompressor", /* tp_name */
@@ -584,7 +604,7 @@ static PyTypeObject BZ2Decompressor_Type = {
584604
0, /* tp_setattro */
585605
0, /* tp_as_buffer */
586606
Py_TPFLAGS_DEFAULT, /* tp_flags */
587-
BZ2Decompressor__doc__, /* tp_doc */
607+
_bz2_BZ2Decompressor___init____doc__, /* tp_doc */
588608
0, /* tp_traverse */
589609
0, /* tp_clear */
590610
0, /* tp_richcompare */
@@ -599,7 +619,7 @@ static PyTypeObject BZ2Decompressor_Type = {
599619
0, /* tp_descr_get */
600620
0, /* tp_descr_set */
601621
0, /* tp_dictoffset */
602-
(initproc)BZ2Decompressor_init, /* tp_init */
622+
_bz2_BZ2Decompressor___init__, /* tp_init */
603623
0, /* tp_alloc */
604624
PyType_GenericNew, /* tp_new */
605625
};

0 commit comments

Comments
 (0)