From c20472640cff9f2cb513b1443897814d0328bea1 Mon Sep 17 00:00:00 2001 From: Larry Hastings Date: Sat, 25 Jan 2014 20:43:29 -0800 Subject: Issue #20390: Small fixes and improvements for Argument Clinic. --- Include/object.h | 8 +- Misc/NEWS | 23 +- Modules/_bz2module.c | 11 +- Modules/_bz2module.clinic.c | 149 ----- Modules/_cursesmodule.c | 10 +- Modules/_datetimemodule.c | 2 +- Modules/_dbmmodule.c | 2 +- Modules/_lzmamodule.c | 1389 ---------------------------------------- Modules/_pickle.c | 49 +- Modules/_sre.c | 3 +- Modules/audioop.c | 1 - Modules/audioop.clinic.c | 836 ------------------------ Modules/binascii.clinic.c | 430 ------------- Modules/clinic/_bz2module.c.h | 152 +++++ Modules/clinic/_lzmamodule.c.h | 1389 ++++++++++++++++++++++++++++++++++++++++ Modules/clinic/audioop.c.h | 836 ++++++++++++++++++++++++ Modules/clinic/binascii.c.h | 430 +++++++++++++ Modules/unicodedata.c | 13 +- Modules/zlibmodule.c | 8 +- Objects/dictobject.c | 9 +- Objects/unicodeobject.c | 2 +- Tools/clinic/clinic.py | 133 +++- 22 files changed, 2962 insertions(+), 2923 deletions(-) delete mode 100644 Modules/_bz2module.clinic.c delete mode 100644 Modules/_lzmamodule.c delete mode 100644 Modules/audioop.clinic.c delete mode 100644 Modules/binascii.clinic.c create mode 100644 Modules/clinic/_bz2module.c.h create mode 100644 Modules/clinic/_lzmamodule.c.h create mode 100644 Modules/clinic/audioop.c.h create mode 100644 Modules/clinic/binascii.c.h diff --git a/Include/object.h b/Include/object.h index a09421b..015d216 100644 --- a/Include/object.h +++ b/Include/object.h @@ -492,8 +492,12 @@ PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject * PyAPI_FUNC(unsigned int) PyType_ClearCache(void); PyAPI_FUNC(void) PyType_Modified(PyTypeObject *); -PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *name, const char *internal_doc); -PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *name, const char *internal_doc); +#ifndef Py_LIMITED_API +PyAPI_FUNC(PyObject *) +_PyType_GetDocFromInternalDoc(const char *, const char *); +PyAPI_FUNC(PyObject *) +_PyType_GetTextSignatureFromInternalDoc(const char *, const char *); +#endif /* Generic operations on objects */ struct _Py_Identifier; diff --git a/Misc/NEWS b/Misc/NEWS index 9187391..f9ff1be 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -2,10 +2,10 @@ Python News +++++++++++ -What's New in Python 3.4.0 Release Candidate 1? -=============================================== +What's New in Python 3.4.0 Beta 3? +================================== -Release date: 2014-01-19 +Release date: 2014-01-25 Core and Builtins ----------------- @@ -147,6 +147,23 @@ Tests Tools/Demos ----------- +- Issue #20390: Argument Clinic's "file" output preset now defaults to + "{dirname}/clinic/{basename}.h". + +- Issue #20390: Argument Clinic's "class" directive syntax has been extended + with two new required arguments: "typedef" and "type_object". + +- Issue #20390: Argument Clinic: If __new__ or __init__ functions didn't use + kwargs (or args), the PyArg_NoKeywords (or PyArg_NoPositional) calls + generated are only run when the type object is an exact match. + +- Issue #20390: Argument Clinic now fails if you have required parameters after + optional parameters. + +- Issue #20390: Argument Clinic converters now have a new template they can + inject code into: "modifiers". Code put there is run in the parsing + function after argument parsing but before the call to the impl. + - Issue #20376: Argument Clinic now escapes backslashes in docstrings. - Issue #20381: Argument Clinic now sanity checks the default argument when diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c index cdaa837..30e0340 100644 --- a/Modules/_bz2module.c +++ b/Modules/_bz2module.c @@ -199,8 +199,8 @@ error: /*[clinic input] output preset file module _bz2 -class _bz2.BZ2Compressor -class _bz2.BZ2Decompressor +class _bz2.BZ2Compressor "BZ2Compressor *" "&BZ2Compressor_Type" +class _bz2.BZ2Decompressor "BZ2Decompressor *" "&BZ2Decompressor_Type" [clinic start generated code]*/ /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ @@ -209,7 +209,6 @@ class _bz2.BZ2Decompressor /*[clinic input] _bz2.BZ2Compressor.compress - self: self(type="BZ2Compressor *") data: Py_buffer / @@ -239,8 +238,6 @@ _bz2_BZ2Compressor_compress_impl(BZ2Compressor *self, Py_buffer *data) /*[clinic input] _bz2.BZ2Compressor.flush - self: self(type="BZ2Compressor *") - Finish the compression process. Returns the compressed data left in internal buffers. @@ -294,7 +291,6 @@ BZ2_Free(void* ctx, void *ptr) /*[clinic input] _bz2.BZ2Compressor.__init__ - self: self(type="BZ2Compressor *") compresslevel: int = 9 Compression level, as a number between 1 and 9. / @@ -472,7 +468,6 @@ error: /*[clinic input] _bz2.BZ2Decompressor.decompress - self: self(type="BZ2Decompressor *") data: Py_buffer / @@ -511,8 +506,6 @@ BZ2Decompressor_getstate(BZ2Decompressor *self, PyObject *noargs) /*[clinic input] _bz2.BZ2Decompressor.__init__ - self: self(type="BZ2Decompressor *") - Create a decompressor object for decompressing data incrementally. For one-shot decompression, use the decompress() function instead. diff --git a/Modules/_bz2module.clinic.c b/Modules/_bz2module.clinic.c deleted file mode 100644 index 68250af..0000000 --- a/Modules/_bz2module.clinic.c +++ /dev/null @@ -1,149 +0,0 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -PyDoc_STRVAR(_bz2_BZ2Compressor_compress__doc__, -"compress(self, data)\n" -"Provide data to the compressor object.\n" -"\n" -"Returns a chunk of compressed data if possible, or b\'\' otherwise.\n" -"\n" -"When you have finished providing data to the compressor, call the\n" -"flush() method to finish the compression process."); - -#define _BZ2_BZ2COMPRESSOR_COMPRESS_METHODDEF \ - {"compress", (PyCFunction)_bz2_BZ2Compressor_compress, METH_VARARGS, _bz2_BZ2Compressor_compress__doc__}, - -static PyObject * -_bz2_BZ2Compressor_compress_impl(BZ2Compressor *self, Py_buffer *data); - -static PyObject * -_bz2_BZ2Compressor_compress(BZ2Compressor *self, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer data = {NULL, NULL}; - - if (!PyArg_ParseTuple(args, - "y*:compress", - &data)) - goto exit; - return_value = _bz2_BZ2Compressor_compress_impl(self, &data); - -exit: - /* Cleanup for data */ - if (data.obj) - PyBuffer_Release(&data); - - return return_value; -} - -PyDoc_STRVAR(_bz2_BZ2Compressor_flush__doc__, -"flush(self)\n" -"Finish the compression process.\n" -"\n" -"Returns the compressed data left in internal buffers.\n" -"\n" -"The compressor object may not be used after this method is called."); - -#define _BZ2_BZ2COMPRESSOR_FLUSH_METHODDEF \ - {"flush", (PyCFunction)_bz2_BZ2Compressor_flush, METH_NOARGS, _bz2_BZ2Compressor_flush__doc__}, - -static PyObject * -_bz2_BZ2Compressor_flush_impl(BZ2Compressor *self); - -static PyObject * -_bz2_BZ2Compressor_flush(BZ2Compressor *self, PyObject *Py_UNUSED(ignored)) -{ - return _bz2_BZ2Compressor_flush_impl(self); -} - -PyDoc_STRVAR(_bz2_BZ2Compressor___init____doc__, -"BZ2Compressor(compresslevel=9)\n" -"Create a compressor object for compressing data incrementally.\n" -"\n" -" compresslevel\n" -" Compression level, as a number between 1 and 9.\n" -"\n" -"For one-shot compression, use the compress() function instead."); - -static int -_bz2_BZ2Compressor___init___impl(BZ2Compressor *self, int compresslevel); - -static int -_bz2_BZ2Compressor___init__(PyObject *self, PyObject *args, PyObject *kwargs) -{ - int return_value = -1; - int compresslevel = 9; - - if (!_PyArg_NoKeywords("BZ2Compressor", kwargs)) - goto exit; - if (!PyArg_ParseTuple(args, - "|i:BZ2Compressor", - &compresslevel)) - goto exit; - return_value = _bz2_BZ2Compressor___init___impl((BZ2Compressor *)self, compresslevel); - -exit: - return return_value; -} - -PyDoc_STRVAR(_bz2_BZ2Decompressor_decompress__doc__, -"decompress(self, data)\n" -"Provide data to the decompressor object.\n" -"\n" -"Returns a chunk of decompressed data if possible, or b\'\' otherwise.\n" -"\n" -"Attempting to decompress data after the end of stream is reached\n" -"raises an EOFError. Any data found after the end of the stream\n" -"is ignored and saved in the unused_data attribute."); - -#define _BZ2_BZ2DECOMPRESSOR_DECOMPRESS_METHODDEF \ - {"decompress", (PyCFunction)_bz2_BZ2Decompressor_decompress, METH_VARARGS, _bz2_BZ2Decompressor_decompress__doc__}, - -static PyObject * -_bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data); - -static PyObject * -_bz2_BZ2Decompressor_decompress(BZ2Decompressor *self, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer data = {NULL, NULL}; - - if (!PyArg_ParseTuple(args, - "y*:decompress", - &data)) - goto exit; - return_value = _bz2_BZ2Decompressor_decompress_impl(self, &data); - -exit: - /* Cleanup for data */ - if (data.obj) - PyBuffer_Release(&data); - - return return_value; -} - -PyDoc_STRVAR(_bz2_BZ2Decompressor___init____doc__, -"BZ2Decompressor()\n" -"Create a decompressor object for decompressing data incrementally.\n" -"\n" -"For one-shot decompression, use the decompress() function instead."); - -static int -_bz2_BZ2Decompressor___init___impl(BZ2Decompressor *self); - -static int -_bz2_BZ2Decompressor___init__(PyObject *self, PyObject *args, PyObject *kwargs) -{ - int return_value = -1; - - if (!_PyArg_NoPositional("BZ2Decompressor", args)) - goto exit; - if (!_PyArg_NoKeywords("BZ2Decompressor", kwargs)) - goto exit; - return_value = _bz2_BZ2Decompressor___init___impl((BZ2Decompressor *)self); - -exit: - return return_value; -} -/*[clinic end generated code: checksum=9bb33ae7d35494b7a5365f03f390e4b5b8b1bc49]*/ diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index d1b3019..534dcff 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -136,7 +136,7 @@ typedef chtype attr_t; /* No attr_t type is available */ /*[clinic input] module curses -class curses.window +class curses.window "PyCursesWindowObject *" "&PyCursesWindow_Type" [clinic start generated code]*/ /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ @@ -605,10 +605,10 @@ PyDoc_STRVAR(curses_window_addch__doc__, {"addch", (PyCFunction)curses_window_addch, METH_VARARGS, curses_window_addch__doc__}, static PyObject * -curses_window_addch_impl(PyObject *self, int group_left_1, int x, int y, PyObject *ch, int group_right_1, long attr); +curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1, int x, int y, PyObject *ch, int group_right_1, long attr); static PyObject * -curses_window_addch(PyObject *self, PyObject *args) +curses_window_addch(PyCursesWindowObject *self, PyObject *args) { PyObject *return_value = NULL; int group_left_1 = 0; @@ -650,8 +650,8 @@ exit: } static PyObject * -curses_window_addch_impl(PyObject *self, int group_left_1, int x, int y, PyObject *ch, int group_right_1, long attr) -/*[clinic end generated code: checksum=f6eeada77a9ec085125f3a27e4a2095f2a4c50be]*/ +curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1, int x, int y, PyObject *ch, int group_right_1, long attr) +/*[clinic end generated code: checksum=e1cdbd4f4e42fc6b36fd4755d7e4bd5b58751ea1]*/ { PyCursesWindowObject *cwself = (PyCursesWindowObject *)self; int coordinates_group = group_left_1; diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 4dfb84c..b78725e 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -18,7 +18,7 @@ /*[clinic input] module datetime -class datetime.datetime +class datetime.datetime "PyDateTime_DateTime *" "&PyDateTime_DateTimeType" [clinic start generated code]*/ /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c index 6cf2328..88dae16 100644 --- a/Modules/_dbmmodule.c +++ b/Modules/_dbmmodule.c @@ -30,7 +30,7 @@ static char *which_dbm = "Berkeley DB"; /*[clinic input] module dbm -class dbm.dbm +class dbm.dbm "dbmobject *" "&Dbmtype" [clinic start generated code]*/ /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c deleted file mode 100644 index 4e18892..0000000 --- a/Modules/_lzmamodule.c +++ /dev/null @@ -1,1389 +0,0 @@ -/* _lzma - Low-level Python interface to liblzma. - - Initial implementation by Per Øyvind Karlsen. - Rewritten by Nadeem Vawda. - -*/ - -#define PY_SSIZE_T_CLEAN - -#include "Python.h" -#include "structmember.h" -#ifdef WITH_THREAD -#include "pythread.h" -#endif - -#include -#include - -#include - - -#ifndef PY_LONG_LONG -#error "This module requires PY_LONG_LONG to be defined" -#endif - - -#ifdef WITH_THREAD -#define ACQUIRE_LOCK(obj) do { \ - if (!PyThread_acquire_lock((obj)->lock, 0)) { \ - Py_BEGIN_ALLOW_THREADS \ - PyThread_acquire_lock((obj)->lock, 1); \ - Py_END_ALLOW_THREADS \ - } } while (0) -#define RELEASE_LOCK(obj) PyThread_release_lock((obj)->lock) -#else -#define ACQUIRE_LOCK(obj) -#define RELEASE_LOCK(obj) -#endif - - -/* Container formats: */ -enum { - FORMAT_AUTO, - FORMAT_XZ, - FORMAT_ALONE, - FORMAT_RAW, -}; - -#define LZMA_CHECK_UNKNOWN (LZMA_CHECK_ID_MAX + 1) - - -typedef struct { - PyObject_HEAD - lzma_allocator alloc; - lzma_stream lzs; - int flushed; -#ifdef WITH_THREAD - PyThread_type_lock lock; -#endif -} Compressor; - -typedef struct { - PyObject_HEAD - lzma_allocator alloc; - lzma_stream lzs; - int check; - char eof; - PyObject *unused_data; -#ifdef WITH_THREAD - PyThread_type_lock lock; -#endif -} Decompressor; - -/* LZMAError class object. */ -static PyObject *Error; - -/* An empty tuple, used by the filter specifier parsing code. */ -static PyObject *empty_tuple; - - -/* Helper functions. */ - -static int -catch_lzma_error(lzma_ret lzret) -{ - switch (lzret) { - case LZMA_OK: - case LZMA_GET_CHECK: - case LZMA_NO_CHECK: - case LZMA_STREAM_END: - return 0; - case LZMA_UNSUPPORTED_CHECK: - PyErr_SetString(Error, "Unsupported integrity check"); - return 1; - case LZMA_MEM_ERROR: - PyErr_NoMemory(); - return 1; - case LZMA_MEMLIMIT_ERROR: - PyErr_SetString(Error, "Memory usage limit exceeded"); - return 1; - case LZMA_FORMAT_ERROR: - PyErr_SetString(Error, "Input format not supported by decoder"); - return 1; - case LZMA_OPTIONS_ERROR: - PyErr_SetString(Error, "Invalid or unsupported options"); - return 1; - case LZMA_DATA_ERROR: - PyErr_SetString(Error, "Corrupt input data"); - return 1; - case LZMA_BUF_ERROR: - PyErr_SetString(Error, "Insufficient buffer space"); - return 1; - case LZMA_PROG_ERROR: - PyErr_SetString(Error, "Internal error"); - return 1; - default: - PyErr_Format(Error, "Unrecognized error from liblzma: %d", lzret); - return 1; - } -} - -static void* -PyLzma_Malloc(void *opaque, size_t items, size_t size) -{ - if (items > (size_t)PY_SSIZE_T_MAX / size) - return NULL; - /* PyMem_Malloc() cannot be used: - the GIL is not held when lzma_code() is called */ - return PyMem_RawMalloc(items * size); -} - -static void -PyLzma_Free(void *opaque, void *ptr) -{ - PyMem_RawFree(ptr); -} - -#if BUFSIZ < 8192 -#define INITIAL_BUFFER_SIZE 8192 -#else -#define INITIAL_BUFFER_SIZE BUFSIZ -#endif - -static int -grow_buffer(PyObject **buf) -{ - size_t size = PyBytes_GET_SIZE(*buf); - return _PyBytes_Resize(buf, size + (size >> 3) + 6); -} - - -/* Some custom type conversions for PyArg_ParseTupleAndKeywords(), - since the predefined conversion specifiers do not suit our needs: - - uint32_t - the "I" (unsigned int) specifier is the right size, but - silently ignores overflows on conversion. - - lzma_vli - the "K" (unsigned PY_LONG_LONG) specifier is the right - size, but like "I" it silently ignores overflows on conversion. - - lzma_mode and lzma_match_finder - these are enumeration types, and - so the size of each is implementation-defined. Worse, different - enum types can be of different sizes within the same program, so - to be strictly correct, we need to define two separate converters. - */ - -#define INT_TYPE_CONVERTER_FUNC(TYPE, FUNCNAME) \ - static int \ - FUNCNAME(PyObject *obj, void *ptr) \ - { \ - unsigned PY_LONG_LONG val; \ - \ - val = PyLong_AsUnsignedLongLong(obj); \ - if (PyErr_Occurred()) \ - return 0; \ - if ((unsigned PY_LONG_LONG)(TYPE)val != val) { \ - PyErr_SetString(PyExc_OverflowError, \ - "Value too large for " #TYPE " type"); \ - return 0; \ - } \ - *(TYPE *)ptr = (TYPE)val; \ - return 1; \ - } - -INT_TYPE_CONVERTER_FUNC(uint32_t, uint32_converter) -INT_TYPE_CONVERTER_FUNC(lzma_vli, lzma_vli_converter) -INT_TYPE_CONVERTER_FUNC(lzma_mode, lzma_mode_converter) -INT_TYPE_CONVERTER_FUNC(lzma_match_finder, lzma_mf_converter) - -#undef INT_TYPE_CONVERTER_FUNC - - -/* Filter specifier parsing. - - This code handles converting filter specifiers (Python dicts) into - the C lzma_filter structs expected by liblzma. */ - -static void * -parse_filter_spec_lzma(PyObject *spec) -{ - static char *optnames[] = {"id", "preset", "dict_size", "lc", "lp", - "pb", "mode", "nice_len", "mf", "depth", NULL}; - PyObject *id; - PyObject *preset_obj; - uint32_t preset = LZMA_PRESET_DEFAULT; - lzma_options_lzma *options; - - /* First, fill in default values for all the options using a preset. - Then, override the defaults with any values given by the caller. */ - - preset_obj = PyMapping_GetItemString(spec, "preset"); - if (preset_obj == NULL) { - if (PyErr_ExceptionMatches(PyExc_KeyError)) - PyErr_Clear(); - else - return NULL; - } else { - int ok = uint32_converter(preset_obj, &preset); - Py_DECREF(preset_obj); - if (!ok) - return NULL; - } - - options = (lzma_options_lzma *)PyMem_Malloc(sizeof *options); - if (options == NULL) - return PyErr_NoMemory(); - memset(options, 0, sizeof *options); - - if (lzma_lzma_preset(options, preset)) { - PyMem_Free(options); - PyErr_Format(Error, "Invalid compression preset: %d", preset); - return NULL; - } - - if (!PyArg_ParseTupleAndKeywords(empty_tuple, spec, - "|OOO&O&O&O&O&O&O&O&", optnames, - &id, &preset_obj, - uint32_converter, &options->dict_size, - uint32_converter, &options->lc, - uint32_converter, &options->lp, - uint32_converter, &options->pb, - lzma_mode_converter, &options->mode, - uint32_converter, &options->nice_len, - lzma_mf_converter, &options->mf, - uint32_converter, &options->depth)) { - PyErr_SetString(PyExc_ValueError, - "Invalid filter specifier for LZMA filter"); - PyMem_Free(options); - options = NULL; - } - return options; -} - -static void * -parse_filter_spec_delta(PyObject *spec) -{ - static char *optnames[] = {"id", "dist", NULL}; - PyObject *id; - uint32_t dist = 1; - lzma_options_delta *options; - - if (!PyArg_ParseTupleAndKeywords(empty_tuple, spec, "|OO&", optnames, - &id, uint32_converter, &dist)) { - PyErr_SetString(PyExc_ValueError, - "Invalid filter specifier for delta filter"); - return NULL; - } - - options = (lzma_options_delta *)PyMem_Malloc(sizeof *options); - if (options == NULL) - return PyErr_NoMemory(); - memset(options, 0, sizeof *options); - options->type = LZMA_DELTA_TYPE_BYTE; - options->dist = dist; - return options; -} - -static void * -parse_filter_spec_bcj(PyObject *spec) -{ - static char *optnames[] = {"id", "start_offset", NULL}; - PyObject *id; - uint32_t start_offset = 0; - lzma_options_bcj *options; - - if (!PyArg_ParseTupleAndKeywords(empty_tuple, spec, "|OO&", optnames, - &id, uint32_converter, &start_offset)) { - PyErr_SetString(PyExc_ValueError, - "Invalid filter specifier for BCJ filter"); - return NULL; - } - - options = (lzma_options_bcj *)PyMem_Malloc(sizeof *options); - if (options == NULL) - return PyErr_NoMemory(); - memset(options, 0, sizeof *options); - options->start_offset = start_offset; - return options; -} - -static int -lzma_filter_converter(PyObject *spec, void *ptr) -{ - lzma_filter *f = (lzma_filter *)ptr; - PyObject *id_obj; - - if (!PyMapping_Check(spec)) { - PyErr_SetString(PyExc_TypeError, - "Filter specifier must be a dict or dict-like object"); - return 0; - } - id_obj = PyMapping_GetItemString(spec, "id"); - if (id_obj == NULL) { - if (PyErr_ExceptionMatches(PyExc_KeyError)) - PyErr_SetString(PyExc_ValueError, - "Filter specifier must have an \"id\" entry"); - return 0; - } - f->id = PyLong_AsUnsignedLongLong(id_obj); - Py_DECREF(id_obj); - if (PyErr_Occurred()) - return 0; - - switch (f->id) { - case LZMA_FILTER_LZMA1: - case LZMA_FILTER_LZMA2: - f->options = parse_filter_spec_lzma(spec); - return f->options != NULL; - case LZMA_FILTER_DELTA: - f->options = parse_filter_spec_delta(spec); - return f->options != NULL; - case LZMA_FILTER_X86: - case LZMA_FILTER_POWERPC: - case LZMA_FILTER_IA64: - case LZMA_FILTER_ARM: - case LZMA_FILTER_ARMTHUMB: - case LZMA_FILTER_SPARC: - f->options = parse_filter_spec_bcj(spec); - return f->options != NULL; - default: - PyErr_Format(PyExc_ValueError, "Invalid filter ID: %llu", f->id); - return 0; - } -} - -static void -free_filter_chain(lzma_filter filters[]) -{ - int i; - - for (i = 0; filters[i].id != LZMA_VLI_UNKNOWN; i++) - PyMem_Free(filters[i].options); -} - -static int -parse_filter_chain_spec(lzma_filter filters[], PyObject *filterspecs) -{ - Py_ssize_t i, num_filters; - - num_filters = PySequence_Length(filterspecs); - if (num_filters == -1) - return -1; - if (num_filters > LZMA_FILTERS_MAX) { - PyErr_Format(PyExc_ValueError, - "Too many filters - liblzma supports a maximum of %d", - LZMA_FILTERS_MAX); - return -1; - } - - for (i = 0; i < num_filters; i++) { - int ok = 1; - PyObject *spec = PySequence_GetItem(filterspecs, i); - if (spec == NULL || !lzma_filter_converter(spec, &filters[i])) - ok = 0; - Py_XDECREF(spec); - if (!ok) { - filters[i].id = LZMA_VLI_UNKNOWN; - free_filter_chain(filters); - return -1; - } - } - filters[num_filters].id = LZMA_VLI_UNKNOWN; - return 0; -} - - -/* Filter specifier construction. - - This code handles converting C lzma_filter structs into - Python-level filter specifiers (represented as dicts). */ - -static int -spec_add_field(PyObject *spec, _Py_Identifier *key, unsigned PY_LONG_LONG value) -{ - int status; - PyObject *value_object; - - value_object = PyLong_FromUnsignedLongLong(value); - if (value_object == NULL) - return -1; - - status = _PyDict_SetItemId(spec, key, value_object); - Py_DECREF(value_object); - return status; -} - -static PyObject * -build_filter_spec(const lzma_filter *f) -{ - PyObject *spec; - - spec = PyDict_New(); - if (spec == NULL) - return NULL; - -#define ADD_FIELD(SOURCE, FIELD) \ - do { \ - _Py_IDENTIFIER(FIELD); \ - if (spec_add_field(spec, &PyId_##FIELD, SOURCE->FIELD) == -1) \ - goto error;\ - } while (0) - - ADD_FIELD(f, id); - - switch (f->id) { - /* For LZMA1 filters, lzma_properties_{encode,decode}() only look at the - lc, lp, pb, and dict_size fields. For LZMA2 filters, only the - dict_size field is used. */ - case LZMA_FILTER_LZMA1: { - lzma_options_lzma *options = f->options; - ADD_FIELD(options, lc); - ADD_FIELD(options, lp); - ADD_FIELD(options, pb); - ADD_FIELD(options, dict_size); - break; - } - case LZMA_FILTER_LZMA2: { - lzma_options_lzma *options = f->options; - ADD_FIELD(options, dict_size); - break; - } - case LZMA_FILTER_DELTA: { - lzma_options_delta *options = f->options; - ADD_FIELD(options, dist); - break; - } - case LZMA_FILTER_X86: - case LZMA_FILTER_POWERPC: - case LZMA_FILTER_IA64: - case LZMA_FILTER_ARM: - case LZMA_FILTER_ARMTHUMB: - case LZMA_FILTER_SPARC: { - lzma_options_bcj *options = f->options; - ADD_FIELD(options, start_offset); - break; - } - default: - PyErr_Format(PyExc_ValueError, "Invalid filter ID: %llu", f->id); - goto error; - } - -#undef ADD_FIELD - - return spec; - -error: - Py_DECREF(spec); - return NULL; -} - - -/*[clinic input] -output preset file -module _lzma -class _lzma.LZMACompressor -class _lzma.LZMADecompressor -[clinic start generated code]*/ -/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ - -#include "_lzmamodule.clinic.c" - -/*[python input] - -class lzma_vli_converter(CConverter): - type = 'lzma_vli' - converter = 'lzma_vli_converter' - -class lzma_filter_converter(CConverter): - type = 'lzma_filter' - converter = 'lzma_filter_converter' - c_default = c_ignored_default = "{LZMA_VLI_UNKNOWN, NULL}" - - def cleanup(self): - name = ensure_legal_c_identifier(self.name) - return ('if (%(name)s.id != LZMA_VLI_UNKNOWN)\n' - ' PyMem_Free(%(name)s.options);\n') % {'name': name} - -[python start generated code]*/ -/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ - - -/* LZMACompressor class. */ - -static PyObject * -compress(Compressor *c, uint8_t *data, size_t len, lzma_action action) -{ - size_t data_size = 0; - PyObject *result; - - result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE); - if (result == NULL) - return NULL; - c->lzs.next_in = data; - c->lzs.avail_in = len; - c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result); - c->lzs.avail_out = PyBytes_GET_SIZE(result); - for (;;) { - lzma_ret lzret; - - Py_BEGIN_ALLOW_THREADS - lzret = lzma_code(&c->lzs, action); - data_size = (char *)c->lzs.next_out - PyBytes_AS_STRING(result); - Py_END_ALLOW_THREADS - if (catch_lzma_error(lzret)) - goto error; - if ((action == LZMA_RUN && c->lzs.avail_in == 0) || - (action == LZMA_FINISH && lzret == LZMA_STREAM_END)) { - break; - } else if (c->lzs.avail_out == 0) { - if (grow_buffer(&result) == -1) - goto error; - c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size; - c->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size; - } - } - if (data_size != PyBytes_GET_SIZE(result)) - if (_PyBytes_Resize(&result, data_size) == -1) - goto error; - return result; - -error: - Py_XDECREF(result); - return NULL; -} - -/*[clinic input] -_lzma.LZMACompressor.compress - - self: self(type="Compressor *") - data: Py_buffer - / - -Provide data to the compressor object. - -Returns a chunk of compressed data if possible, or b'' otherwise. - -When you have finished providing data to the compressor, call the -flush() method to finish the compression process. -[clinic start generated code]*/ - -static PyObject * -_lzma_LZMACompressor_compress_impl(Compressor *self, Py_buffer *data) -/*[clinic end generated code: checksum=31f615136963e00f26f8be33440ec1e3604565ba]*/ -{ - PyObject *result = NULL; - - ACQUIRE_LOCK(self); - if (self->flushed) - PyErr_SetString(PyExc_ValueError, "Compressor has been flushed"); - else - result = compress(self, data->buf, data->len, LZMA_RUN); - RELEASE_LOCK(self); - return result; -} - -/*[clinic input] -_lzma.LZMACompressor.flush - - self: self(type="Compressor *") - -Finish the compression process. - -Returns the compressed data left in internal buffers. - -The compressor object may not be used after this method is called. -[clinic start generated code]*/ - -static PyObject * -_lzma_LZMACompressor_flush_impl(Compressor *self) -/*[clinic end generated code: checksum=fec21f3e22504f500606ba60e1ba70d79eb22188]*/ -{ - PyObject *result = NULL; - - ACQUIRE_LOCK(self); - if (self->flushed) { - PyErr_SetString(PyExc_ValueError, "Repeated call to flush()"); - } else { - self->flushed = 1; - result = compress(self, NULL, 0, LZMA_FINISH); - } - RELEASE_LOCK(self); - return result; -} - -static PyObject * -Compressor_getstate(Compressor *self, PyObject *noargs) -{ - PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object", - Py_TYPE(self)->tp_name); - return NULL; -} - -static int -Compressor_init_xz(lzma_stream *lzs, int check, uint32_t preset, - PyObject *filterspecs) -{ - lzma_ret lzret; - - if (filterspecs == Py_None) { - lzret = lzma_easy_encoder(lzs, preset, check); - } else { - lzma_filter filters[LZMA_FILTERS_MAX + 1]; - - if (parse_filter_chain_spec(filters, filterspecs) == -1) - return -1; - lzret = lzma_stream_encoder(lzs, filters, check); - free_filter_chain(filters); - } - if (catch_lzma_error(lzret)) - return -1; - else - return 0; -} - -static int -Compressor_init_alone(lzma_stream *lzs, uint32_t preset, PyObject *filterspecs) -{ - lzma_ret lzret; - - if (filterspecs == Py_None) { - lzma_options_lzma options; - - if (lzma_lzma_preset(&options, preset)) { - PyErr_Format(Error, "Invalid compression preset: %d", preset); - return -1; - } - lzret = lzma_alone_encoder(lzs, &options); - } else { - lzma_filter filters[LZMA_FILTERS_MAX + 1]; - - if (parse_filter_chain_spec(filters, filterspecs) == -1) - return -1; - if (filters[0].id == LZMA_FILTER_LZMA1 && - filters[1].id == LZMA_VLI_UNKNOWN) { - lzret = lzma_alone_encoder(lzs, filters[0].options); - } else { - PyErr_SetString(PyExc_ValueError, - "Invalid filter chain for FORMAT_ALONE - " - "must be a single LZMA1 filter"); - lzret = LZMA_PROG_ERROR; - } - free_filter_chain(filters); - } - if (PyErr_Occurred() || catch_lzma_error(lzret)) - return -1; - else - return 0; -} - -static int -Compressor_init_raw(lzma_stream *lzs, PyObject *filterspecs) -{ - lzma_filter filters[LZMA_FILTERS_MAX + 1]; - lzma_ret lzret; - - if (filterspecs == Py_None) { - PyErr_SetString(PyExc_ValueError, - "Must specify filters for FORMAT_RAW"); - return -1; - } - if (parse_filter_chain_spec(filters, filterspecs) == -1) - return -1; - lzret = lzma_raw_encoder(lzs, filters); - free_filter_chain(filters); - if (catch_lzma_error(lzret)) - return -1; - else - return 0; -} - -/*[-clinic input] -_lzma.LZMACompressor.__init__ - - self: self(type="Compressor *") - format: int(c_default="FORMAT_XZ") = FORMAT_XZ - The container format to use for the output. This can - be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW. - - check: int(c_default="-1") = unspecified - The integrity check to use. For FORMAT_XZ, the default - is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not suport integrity - checks; for these formats, check must be omitted, or be CHECK_NONE. - - preset: object = None - If provided should be an integer in the range 0-9, optionally - OR-ed with the constant PRESET_EXTREME. - - filters: object = None - If provided should be a sequence of dicts. Each dict should - have an entry for "id" indicating the ID of the filter, plus - additional entries for options to the filter. - -Create a compressor object for compressing data incrementally. - -The settings used by the compressor can be specified either as a -preset compression level (with the 'preset' argument), or in detail -as a custom filter chain (with the 'filters' argument). For FORMAT_XZ -and FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset -level. For FORMAT_RAW, the caller must always specify a filter chain; -the raw compressor does not support preset compression levels. - -For one-shot compression, use the compress() function instead. -[-clinic start generated code]*/ -static int -Compressor_init(Compressor *self, PyObject *args, PyObject *kwargs) -{ - static char *arg_names[] = {"format", "check", "preset", "filters", NULL}; - int format = FORMAT_XZ; - int check = -1; - uint32_t preset = LZMA_PRESET_DEFAULT; - PyObject *preset_obj = Py_None; - PyObject *filterspecs = Py_None; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, - "|iiOO:LZMACompressor", arg_names, - &format, &check, &preset_obj, - &filterspecs)) - return -1; - - if (format != FORMAT_XZ && check != -1 && check != LZMA_CHECK_NONE) { - PyErr_SetString(PyExc_ValueError, - "Integrity checks are only supported by FORMAT_XZ"); - return -1; - } - - if (preset_obj != Py_None && filterspecs != Py_None) { - PyErr_SetString(PyExc_ValueError, - "Cannot specify both preset and filter chain"); - return -1; - } - - if (preset_obj != Py_None) - if (!uint32_converter(preset_obj, &preset)) - return -1; - - self->alloc.opaque = NULL; - self->alloc.alloc = PyLzma_Malloc; - self->alloc.free = PyLzma_Free; - self->lzs.allocator = &self->alloc; - -#ifdef WITH_THREAD - self->lock = PyThread_allocate_lock(); - if (self->lock == NULL) { - PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock"); - return -1; - } -#endif - - self->flushed = 0; - switch (format) { - case FORMAT_XZ: - if (check == -1) - check = LZMA_CHECK_CRC64; - if (Compressor_init_xz(&self->lzs, check, preset, filterspecs) != 0) - break; - return 0; - - case FORMAT_ALONE: - if (Compressor_init_alone(&self->lzs, preset, filterspecs) != 0) - break; - return 0; - - case FORMAT_RAW: - if (Compressor_init_raw(&self->lzs, filterspecs) != 0) - break; - return 0; - - default: - PyErr_Format(PyExc_ValueError, - "Invalid container format: %d", format); - break; - } - -#ifdef WITH_THREAD - PyThread_free_lock(self->lock); - self->lock = NULL; -#endif - return -1; -} - -static void -Compressor_dealloc(Compressor *self) -{ - lzma_end(&self->lzs); -#ifdef WITH_THREAD - if (self->lock != NULL) - PyThread_free_lock(self->lock); -#endif - Py_TYPE(self)->tp_free((PyObject *)self); -} - -static PyMethodDef Compressor_methods[] = { - _LZMA_LZMACOMPRESSOR_COMPRESS_METHODDEF - _LZMA_LZMACOMPRESSOR_FLUSH_METHODDEF - {"__getstate__", (PyCFunction)Compressor_getstate, METH_NOARGS}, - {NULL} -}; - -PyDoc_STRVAR(Compressor_doc, -"LZMACompressor(format=FORMAT_XZ, check=-1, preset=None, filters=None)\n" -"\n" -"Create a compressor object for compressing data incrementally.\n" -"\n" -"format specifies the container format to use for the output. This can\n" -"be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW.\n" -"\n" -"check specifies the integrity check to use. For FORMAT_XZ, the default\n" -"is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not suport integrity\n" -"checks; for these formats, check must be omitted, or be CHECK_NONE.\n" -"\n" -"The settings used by the compressor can be specified either as a\n" -"preset compression level (with the 'preset' argument), or in detail\n" -"as a custom filter chain (with the 'filters' argument). For FORMAT_XZ\n" -"and FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset\n" -"level. For FORMAT_RAW, the caller must always specify a filter chain;\n" -"the raw compressor does not support preset compression levels.\n" -"\n" -"preset (if provided) should be an integer in the range 0-9, optionally\n" -"OR-ed with the constant PRESET_EXTREME.\n" -"\n" -"filters (if provided) should be a sequence of dicts. Each dict should\n" -"have an entry for \"id\" indicating the ID of the filter, plus\n" -"additional entries for options to the filter.\n" -"\n" -"For one-shot compression, use the compress() function instead.\n"); - -static PyTypeObject Compressor_type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_lzma.LZMACompressor", /* tp_name */ - sizeof(Compressor), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor)Compressor_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_reserved */ - 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ - Compressor_doc, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - Compressor_methods, /* tp_methods */ - 0, /* tp_members */ - 0, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - (initproc)Compressor_init, /* tp_init */ - 0, /* tp_alloc */ - PyType_GenericNew, /* tp_new */ -}; - - -/* LZMADecompressor class. */ - -static PyObject * -decompress(Decompressor *d, uint8_t *data, size_t len) -{ - size_t data_size = 0; - PyObject *result; - - result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE); - if (result == NULL) - return NULL; - d->lzs.next_in = data; - d->lzs.avail_in = len; - d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result); - d->lzs.avail_out = PyBytes_GET_SIZE(result); - for (;;) { - lzma_ret lzret; - - Py_BEGIN_ALLOW_THREADS - lzret = lzma_code(&d->lzs, LZMA_RUN); - data_size = (char *)d->lzs.next_out - PyBytes_AS_STRING(result); - Py_END_ALLOW_THREADS - if (catch_lzma_error(lzret)) - goto error; - if (lzret == LZMA_GET_CHECK || lzret == LZMA_NO_CHECK) - d->check = lzma_get_check(&d->lzs); - if (lzret == LZMA_STREAM_END) { - d->eof = 1; - if (d->lzs.avail_in > 0) { - Py_CLEAR(d->unused_data); - d->unused_data = PyBytes_FromStringAndSize( - (char *)d->lzs.next_in, d->lzs.avail_in); - if (d->unused_data == NULL) - goto error; - } - break; - } else if (d->lzs.avail_in == 0) { - break; - } else if (d->lzs.avail_out == 0) { - if (grow_buffer(&result) == -1) - goto error; - d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size; - d->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size; - } - } - if (data_size != PyBytes_GET_SIZE(result)) - if (_PyBytes_Resize(&result, data_size) == -1) - goto error; - return result; - -error: - Py_XDECREF(result); - return NULL; -} - -/*[clinic input] -_lzma.LZMADecompressor.decompress - - self: self(type="Decompressor *") - data: Py_buffer - / - -Provide data to the decompressor object. - -Returns a chunk of decompressed data if possible, or b'' otherwise. - -Attempting to decompress data after the end of stream is reached -raises an EOFError. Any data found after the end of the stream -is ignored and saved in the unused_data attribute. -[clinic start generated code]*/ - -static PyObject * -_lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data) -/*[clinic end generated code: checksum=d86e78da7ff0ff219d511275b16b79476da8922e]*/ -{ - PyObject *result = NULL; - - ACQUIRE_LOCK(self); - if (self->eof) - PyErr_SetString(PyExc_EOFError, "Already at end of stream"); - else - result = decompress(self, data->buf, data->len); - RELEASE_LOCK(self); - return result; -} - -static PyObject * -Decompressor_getstate(Decompressor *self, PyObject *noargs) -{ - PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object", - Py_TYPE(self)->tp_name); - return NULL; -} - -static int -Decompressor_init_raw(lzma_stream *lzs, PyObject *filterspecs) -{ - lzma_filter filters[LZMA_FILTERS_MAX + 1]; - lzma_ret lzret; - - if (parse_filter_chain_spec(filters, filterspecs) == -1) - return -1; - lzret = lzma_raw_decoder(lzs, filters); - free_filter_chain(filters); - if (catch_lzma_error(lzret)) - return -1; - else - return 0; -} - -/*[clinic input] -_lzma.LZMADecompressor.__init__ - - self: self(type="Decompressor *") - format: int(c_default="FORMAT_AUTO") = FORMAT_AUTO - Specifies the container format of the input stream. If this is - FORMAT_AUTO (the default), the decompressor will automatically detect - whether the input is FORMAT_XZ or FORMAT_ALONE. Streams created with - FORMAT_RAW cannot be autodetected. - - memlimit: object = None - Limit the amount of memory used by the decompressor. This will cause - decompression to fail if the input cannot be decompressed within the - given limit. - - filters: object = None - A custom filter chain. This argument is required for FORMAT_RAW, and - not accepted with any other format. When provided, this should be a - sequence of dicts, each indicating the ID and options for a single - filter. - -Create a decompressor object for decompressing data incrementally. - -For one-shot decompression, use the decompress() function instead. -[clinic start generated code]*/ - -static int -_lzma_LZMADecompressor___init___impl(Decompressor *self, int format, PyObject *memlimit, PyObject *filters) -/*[clinic end generated code: checksum=9b119f6f2cc2d7a8e5be41c164a6c080ee82d0c2]*/ -{ - const uint32_t decoder_flags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK; - uint64_t memlimit_ = UINT64_MAX; - lzma_ret lzret; - - if (memlimit != Py_None) { - if (format == FORMAT_RAW) { - PyErr_SetString(PyExc_ValueError, - "Cannot specify memory limit with FORMAT_RAW"); - return -1; - } - memlimit_ = PyLong_AsUnsignedLongLong(memlimit); - if (PyErr_Occurred()) - return -1; - } - - if (format == FORMAT_RAW && filters == Py_None) { - PyErr_SetString(PyExc_ValueError, - "Must specify filters for FORMAT_RAW"); - return -1; - } else if (format != FORMAT_RAW && filters != Py_None) { - PyErr_SetString(PyExc_ValueError, - "Cannot specify filters except with FORMAT_RAW"); - return -1; - } - - self->alloc.opaque = NULL; - self->alloc.alloc = PyLzma_Malloc; - self->alloc.free = PyLzma_Free; - self->lzs.allocator = &self->alloc; - -#ifdef WITH_THREAD - self->lock = PyThread_allocate_lock(); - if (self->lock == NULL) { - PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock"); - return -1; - } -#endif - - self->check = LZMA_CHECK_UNKNOWN; - self->unused_data = PyBytes_FromStringAndSize(NULL, 0); - if (self->unused_data == NULL) - goto error; - - switch (format) { - case FORMAT_AUTO: - lzret = lzma_auto_decoder(&self->lzs, memlimit_, decoder_flags); - if (catch_lzma_error(lzret)) - break; - return 0; - - case FORMAT_XZ: - lzret = lzma_stream_decoder(&self->lzs, memlimit_, decoder_flags); - if (catch_lzma_error(lzret)) - break; - return 0; - - case FORMAT_ALONE: - self->check = LZMA_CHECK_NONE; - lzret = lzma_alone_decoder(&self->lzs, memlimit_); - if (catch_lzma_error(lzret)) - break; - return 0; - - case FORMAT_RAW: - self->check = LZMA_CHECK_NONE; - if (Decompressor_init_raw(&self->lzs, filters) == -1) - break; - return 0; - - default: - PyErr_Format(PyExc_ValueError, - "Invalid container format: %d", format); - break; - } - -error: - Py_CLEAR(self->unused_data); -#ifdef WITH_THREAD - PyThread_free_lock(self->lock); - self->lock = NULL; -#endif - return -1; -} - -static void -Decompressor_dealloc(Decompressor *self) -{ - lzma_end(&self->lzs); - Py_CLEAR(self->unused_data); -#ifdef WITH_THREAD - if (self->lock != NULL) - PyThread_free_lock(self->lock); -#endif - Py_TYPE(self)->tp_free((PyObject *)self); -} - -static PyMethodDef Decompressor_methods[] = { - _LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF - {"__getstate__", (PyCFunction)Decompressor_getstate, METH_NOARGS}, - {NULL} -}; - -PyDoc_STRVAR(Decompressor_check_doc, -"ID of the integrity check used by the input stream."); - -PyDoc_STRVAR(Decompressor_eof_doc, -"True if the end-of-stream marker has been reached."); - -PyDoc_STRVAR(Decompressor_unused_data_doc, -"Data found after the end of the compressed stream."); - -static PyMemberDef Decompressor_members[] = { - {"check", T_INT, offsetof(Decompressor, check), READONLY, - Decompressor_check_doc}, - {"eof", T_BOOL, offsetof(Decompressor, eof), READONLY, - Decompressor_eof_doc}, - {"unused_data", T_OBJECT_EX, offsetof(Decompressor, unused_data), READONLY, - Decompressor_unused_data_doc}, - {NULL} -}; - -static PyTypeObject Decompressor_type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_lzma.LZMADecompressor", /* tp_name */ - sizeof(Decompressor), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor)Decompressor_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_reserved */ - 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ - _lzma_LZMADecompressor___init____doc__, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - Decompressor_methods, /* tp_methods */ - Decompressor_members, /* tp_members */ - 0, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - _lzma_LZMADecompressor___init__, /* tp_init */ - 0, /* tp_alloc */ - PyType_GenericNew, /* tp_new */ -}; - - -/* Module-level functions. */ - -/*[clinic input] -_lzma.is_check_supported - check_id: int - / - -Test whether the given integrity check is supported. - -Always returns True for CHECK_NONE and CHECK_CRC32. -[clinic start generated code]*/ - -static PyObject * -_lzma_is_check_supported_impl(PyModuleDef *module, int check_id) -/*[clinic end generated code: checksum=bb828e90e00ad96ed61f66719c2fca7fde637418]*/ -{ - return PyBool_FromLong(lzma_check_is_supported(check_id)); -} - - -/*[clinic input] -_lzma._encode_filter_properties - filter: lzma_filter(c_default="{LZMA_VLI_UNKNOWN, NULL}") - / - -Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict). - -The result does not include the filter ID itself, only the options. -[clinic start generated code]*/ - -static PyObject * -_lzma__encode_filter_properties_impl(PyModuleDef *module, lzma_filter filter) -/*[clinic end generated code: checksum=b5fe690acd6b61d1abfc32f522ada5bdcf9b13da]*/ -{ - lzma_ret lzret; - uint32_t encoded_size; - PyObject *result = NULL; - - lzret = lzma_properties_size(&encoded_size, &filter); - if (catch_lzma_error(lzret)) - goto error; - - result = PyBytes_FromStringAndSize(NULL, encoded_size); - if (result == NULL) - goto error; - - lzret = lzma_properties_encode( - &filter, (uint8_t *)PyBytes_AS_STRING(result)); - if (catch_lzma_error(lzret)) - goto error; - - return result; - -error: - Py_XDECREF(result); - return NULL; -} - - -/*[clinic input] -_lzma._decode_filter_properties - filter_id: lzma_vli - encoded_props: Py_buffer - / - -Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict). - -The result does not include the filter ID itself, only the options. -[clinic start generated code]*/ - -static PyObject * -_lzma__decode_filter_properties_impl(PyModuleDef *module, lzma_vli filter_id, Py_buffer *encoded_props) -/*[clinic end generated code: checksum=235f7f5345d48744dcd21f781dafbbf05a717538]*/ -{ - lzma_filter filter; - lzma_ret lzret; - PyObject *result = NULL; - filter.id = filter_id; - - lzret = lzma_properties_decode( - &filter, NULL, encoded_props->buf, encoded_props->len); - if (catch_lzma_error(lzret)) - return NULL; - - result = build_filter_spec(&filter); - - /* We use vanilla free() here instead of PyMem_Free() - filter.options was - allocated by lzma_properties_decode() using the default allocator. */ - free(filter.options); - return result; -} - - -/* Module initialization. */ - -static PyMethodDef module_methods[] = { - _LZMA_IS_CHECK_SUPPORTED_METHODDEF - _LZMA__ENCODE_FILTER_PROPERTIES_METHODDEF - _LZMA__DECODE_FILTER_PROPERTIES_METHODDEF - {NULL} -}; - -static PyModuleDef _lzmamodule = { - PyModuleDef_HEAD_INIT, - "_lzma", - NULL, - -1, - module_methods, - NULL, - NULL, - NULL, - NULL, -}; - -/* Some of our constants are more than 32 bits wide, so PyModule_AddIntConstant - would not work correctly on platforms with 32-bit longs. */ -static int -module_add_int_constant(PyObject *m, const char *name, PY_LONG_LONG value) -{ - PyObject *o = PyLong_FromLongLong(value); - if (o == NULL) - return -1; - if (PyModule_AddObject(m, name, o) == 0) - return 0; - Py_DECREF(o); - return -1; -} - -#define ADD_INT_PREFIX_MACRO(m, macro) \ - module_add_int_constant(m, #macro, LZMA_ ## macro) - -PyMODINIT_FUNC -PyInit__lzma(void) -{ - PyObject *m; - - empty_tuple = PyTuple_New(0); - if (empty_tuple == NULL) - return NULL; - - m = PyModule_Create(&_lzmamodule); - if (m == NULL) - return NULL; - - if (PyModule_AddIntMacro(m, FORMAT_AUTO) == -1 || - PyModule_AddIntMacro(m, FORMAT_XZ) == -1 || - PyModule_AddIntMacro(m, FORMAT_ALONE) == -1 || - PyModule_AddIntMacro(m, FORMAT_RAW) == -1 || - ADD_INT_PREFIX_MACRO(m, CHECK_NONE) == -1 || - ADD_INT_PREFIX_MACRO(m, CHECK_CRC32) == -1 || - ADD_INT_PREFIX_MACRO(m, CHECK_CRC64) == -1 || - ADD_INT_PREFIX_MACRO(m, CHECK_SHA256) == -1 || - ADD_INT_PREFIX_MACRO(m, CHECK_ID_MAX) == -1 || - ADD_INT_PREFIX_MACRO(m, CHECK_UNKNOWN) == -1 || - ADD_INT_PREFIX_MACRO(m, FILTER_LZMA1) == -1 || - ADD_INT_PREFIX_MACRO(m, FILTER_LZMA2) == -1 || - ADD_INT_PREFIX_MACRO(m, FILTER_DELTA) == -1 || - ADD_INT_PREFIX_MACRO(m, FILTER_X86) == -1 || - ADD_INT_PREFIX_MACRO(m, FILTER_IA64) == -1 || - ADD_INT_PREFIX_MACRO(m, FILTER_ARM) == -1 || - ADD_INT_PREFIX_MACRO(m, FILTER_ARMTHUMB) == -1 || - ADD_INT_PREFIX_MACRO(m, FILTER_SPARC) == -1 || - ADD_INT_PREFIX_MACRO(m, FILTER_POWERPC) == -1 || - ADD_INT_PREFIX_MACRO(m, MF_HC3) == -1 || - ADD_INT_PREFIX_MACRO(m, MF_HC4) == -1 || - ADD_INT_PREFIX_MACRO(m, MF_BT2) == -1 || - ADD_INT_PREFIX_MACRO(m, MF_BT3) == -1 || - ADD_INT_PREFIX_MACRO(m, MF_BT4) == -1 || - ADD_INT_PREFIX_MACRO(m, MODE_FAST) == -1 || - ADD_INT_PREFIX_MACRO(m, MODE_NORMAL) == -1 || - ADD_INT_PREFIX_MACRO(m, PRESET_DEFAULT) == -1 || - ADD_INT_PREFIX_MACRO(m, PRESET_EXTREME) == -1) - return NULL; - - Error = PyErr_NewExceptionWithDoc( - "_lzma.LZMAError", "Call to liblzma failed.", NULL, NULL); - if (Error == NULL) - return NULL; - Py_INCREF(Error); - if (PyModule_AddObject(m, "LZMAError", Error) == -1) - return NULL; - - if (PyType_Ready(&Compressor_type) == -1) - return NULL; - Py_INCREF(&Compressor_type); - if (PyModule_AddObject(m, "LZMACompressor", - (PyObject *)&Compressor_type) == -1) - return NULL; - - if (PyType_Ready(&Decompressor_type) == -1) - return NULL; - Py_INCREF(&Decompressor_type); - if (PyModule_AddObject(m, "LZMADecompressor", - (PyObject *)&Decompressor_type) == -1) - return NULL; - - return m; -} diff --git a/Modules/_pickle.c b/Modules/_pickle.c index fa5f73d..dabd81e 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -6,28 +6,13 @@ PyDoc_STRVAR(pickle_module_doc, /*[clinic input] module _pickle -class _pickle.Pickler -class _pickle.PicklerMemoProxy -class _pickle.Unpickler -class _pickle.UnpicklerMemoProxy +class _pickle.Pickler "PicklerObject *" "&Pickler_Type" +class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType" +class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type" +class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType" [clinic start generated code]*/ /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ -/*[python input] -class PicklerObject_converter(self_converter): - type = "PicklerObject *" - -class PicklerMemoProxyObject_converter(self_converter): - type = "PicklerMemoProxyObject *" - -class UnpicklerObject_converter(self_converter): - type = "UnpicklerObject *" - -class UnpicklerMemoProxyObject_converter(self_converter): - type = "UnpicklerMemoProxyObject *" -[python start generated code]*/ -/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ - /* Bump this when new opcodes are added to the pickle protocol. */ enum { HIGHEST_PROTOCOL = 4, @@ -3878,8 +3863,6 @@ dump(PicklerObject *self, PyObject *obj) _pickle.Pickler.clear_memo - self: PicklerObject - Clears the pickler's "memo". The memo is the data structure that remembers which objects the @@ -3923,7 +3906,6 @@ _pickle_Pickler_clear_memo_impl(PicklerObject *self) _pickle.Pickler.dump - self: PicklerObject obj: object / @@ -4018,7 +4000,6 @@ Pickler_clear(PicklerObject *self) _pickle.Pickler.__init__ - self: PicklerObject file: object protocol: object = NULL fix_imports: bool = True @@ -4158,8 +4139,6 @@ typedef struct { /*[clinic input] _pickle.PicklerMemoProxy.clear - self: PicklerMemoProxyObject - Remove all items from memo. [clinic start generated code]*/ @@ -4191,8 +4170,6 @@ _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) /*[clinic input] _pickle.PicklerMemoProxy.copy - self: PicklerMemoProxyObject - Copy the memo to a new object. [clinic start generated code]*/ @@ -4254,8 +4231,6 @@ _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) /*[clinic input] _pickle.PicklerMemoProxy.__reduce__ - self: PicklerMemoProxyObject - Implement pickle support. [clinic start generated code]*/ @@ -6310,17 +6285,17 @@ PyDoc_STRVAR(_pickle_Unpickler_load__doc__, {"load", (PyCFunction)_pickle_Unpickler_load, METH_NOARGS, _pickle_Unpickler_load__doc__}, static PyObject * -_pickle_Unpickler_load_impl(PyObject *self); +_pickle_Unpickler_load_impl(UnpicklerObject *self); static PyObject * -_pickle_Unpickler_load(PyObject *self, PyObject *Py_UNUSED(ignored)) +_pickle_Unpickler_load(UnpicklerObject *self, PyObject *Py_UNUSED(ignored)) { return _pickle_Unpickler_load_impl(self); } static PyObject * -_pickle_Unpickler_load_impl(PyObject *self) -/*[clinic end generated code: checksum=fb1119422c5e03045d690d1cd6c457f1ca4c585d]*/ +_pickle_Unpickler_load_impl(UnpicklerObject *self) +/*[clinic end generated code: checksum=5ccece694e9898856d916e0a87f0133d4537ebb9]*/ { UnpicklerObject *unpickler = (UnpicklerObject*)self; @@ -6347,7 +6322,6 @@ _pickle_Unpickler_load_impl(PyObject *self) _pickle.Unpickler.find_class - self: UnpicklerObject module_name: object global_name: object / @@ -6551,7 +6525,6 @@ Unpickler_clear(UnpicklerObject *self) _pickle.Unpickler.__init__ - self: UnpicklerObject file: object * fix_imports: bool = True @@ -6692,8 +6665,6 @@ typedef struct { /*[clinic input] _pickle.UnpicklerMemoProxy.clear - self: UnpicklerMemoProxyObject - Remove all items from memo. [clinic start generated code]*/ @@ -6727,8 +6698,6 @@ _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) /*[clinic input] _pickle.UnpicklerMemoProxy.copy - self: UnpicklerMemoProxyObject - Copy the memo to a new object. [clinic start generated code]*/ @@ -6783,8 +6752,6 @@ error: /*[clinic input] _pickle.UnpicklerMemoProxy.__reduce__ - self: UnpicklerMemoProxyObject - Implement pickling support. [clinic start generated code]*/ diff --git a/Modules/_sre.c b/Modules/_sre.c index 776d6eb..41dca7d 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -528,11 +528,10 @@ sre_search(SRE_STATE* state, SRE_CODE* pattern) /*[clinic input] module _sre -class _sre.SRE_Pattern +class _sre.SRE_Pattern "PatternObject *" "&Pattern_Type" _sre.SRE_Pattern.match as pattern_match - self: self(type="PatternObject *") pattern: object pos: Py_ssize_t = 0 endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize diff --git a/Modules/audioop.c b/Modules/audioop.c index 7e0aef5..d9a22c5 100644 --- a/Modules/audioop.c +++ b/Modules/audioop.c @@ -393,7 +393,6 @@ audioop_check_parameters(Py_ssize_t len, int size) /*[clinic input] output preset file module audioop -class audioop.error [clinic start generated code]*/ /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ diff --git a/Modules/audioop.clinic.c b/Modules/audioop.clinic.c deleted file mode 100644 index 974fdb1..0000000 --- a/Modules/audioop.clinic.c +++ /dev/null @@ -1,836 +0,0 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -PyDoc_STRVAR(audioop_getsample__doc__, -"getsample(module, fragment, width, index)\n" -"Return the value of sample index from the fragment."); - -#define AUDIOOP_GETSAMPLE_METHODDEF \ - {"getsample", (PyCFunction)audioop_getsample, METH_VARARGS, audioop_getsample__doc__}, - -static PyObject * -audioop_getsample_impl(PyModuleDef *module, Py_buffer *fragment, int width, Py_ssize_t index); - -static PyObject * -audioop_getsample(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - Py_ssize_t index; - - if (!PyArg_ParseTuple(args, - "y*in:getsample", - &fragment, &width, &index)) - goto exit; - return_value = audioop_getsample_impl(module, &fragment, width, index); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_max__doc__, -"max(module, fragment, width)\n" -"Return the maximum of the absolute value of all samples in a fragment."); - -#define AUDIOOP_MAX_METHODDEF \ - {"max", (PyCFunction)audioop_max, METH_VARARGS, audioop_max__doc__}, - -static PyObject * -audioop_max_impl(PyModuleDef *module, Py_buffer *fragment, int width); - -static PyObject * -audioop_max(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*i:max", - &fragment, &width)) - goto exit; - return_value = audioop_max_impl(module, &fragment, width); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_minmax__doc__, -"minmax(module, fragment, width)\n" -"Return the minimum and maximum values of all samples in the sound fragment."); - -#define AUDIOOP_MINMAX_METHODDEF \ - {"minmax", (PyCFunction)audioop_minmax, METH_VARARGS, audioop_minmax__doc__}, - -static PyObject * -audioop_minmax_impl(PyModuleDef *module, Py_buffer *fragment, int width); - -static PyObject * -audioop_minmax(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*i:minmax", - &fragment, &width)) - goto exit; - return_value = audioop_minmax_impl(module, &fragment, width); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_avg__doc__, -"avg(module, fragment, width)\n" -"Return the average over all samples in the fragment."); - -#define AUDIOOP_AVG_METHODDEF \ - {"avg", (PyCFunction)audioop_avg, METH_VARARGS, audioop_avg__doc__}, - -static PyObject * -audioop_avg_impl(PyModuleDef *module, Py_buffer *fragment, int width); - -static PyObject * -audioop_avg(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*i:avg", - &fragment, &width)) - goto exit; - return_value = audioop_avg_impl(module, &fragment, width); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_rms__doc__, -"rms(module, fragment, width)\n" -"Return the root-mean-square of the fragment, i.e. sqrt(sum(S_i^2)/n)."); - -#define AUDIOOP_RMS_METHODDEF \ - {"rms", (PyCFunction)audioop_rms, METH_VARARGS, audioop_rms__doc__}, - -static PyObject * -audioop_rms_impl(PyModuleDef *module, Py_buffer *fragment, int width); - -static PyObject * -audioop_rms(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*i:rms", - &fragment, &width)) - goto exit; - return_value = audioop_rms_impl(module, &fragment, width); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_findfit__doc__, -"findfit(module, fragment, reference)\n" -"Try to match reference as well as possible to a portion of fragment."); - -#define AUDIOOP_FINDFIT_METHODDEF \ - {"findfit", (PyCFunction)audioop_findfit, METH_VARARGS, audioop_findfit__doc__}, - -static PyObject * -audioop_findfit_impl(PyModuleDef *module, Py_buffer *fragment, Py_buffer *reference); - -static PyObject * -audioop_findfit(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - Py_buffer reference = {NULL, NULL}; - - if (!PyArg_ParseTuple(args, - "y*y*:findfit", - &fragment, &reference)) - goto exit; - return_value = audioop_findfit_impl(module, &fragment, &reference); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - /* Cleanup for reference */ - if (reference.obj) - PyBuffer_Release(&reference); - - return return_value; -} - -PyDoc_STRVAR(audioop_findfactor__doc__, -"findfactor(module, fragment, reference)\n" -"Return a factor F such that rms(add(fragment, mul(reference, -F))) is minimal."); - -#define AUDIOOP_FINDFACTOR_METHODDEF \ - {"findfactor", (PyCFunction)audioop_findfactor, METH_VARARGS, audioop_findfactor__doc__}, - -static PyObject * -audioop_findfactor_impl(PyModuleDef *module, Py_buffer *fragment, Py_buffer *reference); - -static PyObject * -audioop_findfactor(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - Py_buffer reference = {NULL, NULL}; - - if (!PyArg_ParseTuple(args, - "y*y*:findfactor", - &fragment, &reference)) - goto exit; - return_value = audioop_findfactor_impl(module, &fragment, &reference); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - /* Cleanup for reference */ - if (reference.obj) - PyBuffer_Release(&reference); - - return return_value; -} - -PyDoc_STRVAR(audioop_findmax__doc__, -"findmax(module, fragment, length)\n" -"Search fragment for a slice of specified number of samples with maximum energy."); - -#define AUDIOOP_FINDMAX_METHODDEF \ - {"findmax", (PyCFunction)audioop_findmax, METH_VARARGS, audioop_findmax__doc__}, - -static PyObject * -audioop_findmax_impl(PyModuleDef *module, Py_buffer *fragment, Py_ssize_t length); - -static PyObject * -audioop_findmax(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - Py_ssize_t length; - - if (!PyArg_ParseTuple(args, - "y*n:findmax", - &fragment, &length)) - goto exit; - return_value = audioop_findmax_impl(module, &fragment, length); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_avgpp__doc__, -"avgpp(module, fragment, width)\n" -"Return the average peak-peak value over all samples in the fragment."); - -#define AUDIOOP_AVGPP_METHODDEF \ - {"avgpp", (PyCFunction)audioop_avgpp, METH_VARARGS, audioop_avgpp__doc__}, - -static PyObject * -audioop_avgpp_impl(PyModuleDef *module, Py_buffer *fragment, int width); - -static PyObject * -audioop_avgpp(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*i:avgpp", - &fragment, &width)) - goto exit; - return_value = audioop_avgpp_impl(module, &fragment, width); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_maxpp__doc__, -"maxpp(module, fragment, width)\n" -"Return the maximum peak-peak value in the sound fragment."); - -#define AUDIOOP_MAXPP_METHODDEF \ - {"maxpp", (PyCFunction)audioop_maxpp, METH_VARARGS, audioop_maxpp__doc__}, - -static PyObject * -audioop_maxpp_impl(PyModuleDef *module, Py_buffer *fragment, int width); - -static PyObject * -audioop_maxpp(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*i:maxpp", - &fragment, &width)) - goto exit; - return_value = audioop_maxpp_impl(module, &fragment, width); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_cross__doc__, -"cross(module, fragment, width)\n" -"Return the number of zero crossings in the fragment passed as an argument."); - -#define AUDIOOP_CROSS_METHODDEF \ - {"cross", (PyCFunction)audioop_cross, METH_VARARGS, audioop_cross__doc__}, - -static PyObject * -audioop_cross_impl(PyModuleDef *module, Py_buffer *fragment, int width); - -static PyObject * -audioop_cross(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*i:cross", - &fragment, &width)) - goto exit; - return_value = audioop_cross_impl(module, &fragment, width); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_mul__doc__, -"mul(module, fragment, width, factor)\n" -"Return a fragment that has all samples in the original fragment multiplied by the floating-point value factor."); - -#define AUDIOOP_MUL_METHODDEF \ - {"mul", (PyCFunction)audioop_mul, METH_VARARGS, audioop_mul__doc__}, - -static PyObject * -audioop_mul_impl(PyModuleDef *module, Py_buffer *fragment, int width, double factor); - -static PyObject * -audioop_mul(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - double factor; - - if (!PyArg_ParseTuple(args, - "y*id:mul", - &fragment, &width, &factor)) - goto exit; - return_value = audioop_mul_impl(module, &fragment, width, factor); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_tomono__doc__, -"tomono(module, fragment, width, lfactor, rfactor)\n" -"Convert a stereo fragment to a mono fragment."); - -#define AUDIOOP_TOMONO_METHODDEF \ - {"tomono", (PyCFunction)audioop_tomono, METH_VARARGS, audioop_tomono__doc__}, - -static PyObject * -audioop_tomono_impl(PyModuleDef *module, Py_buffer *fragment, int width, double lfactor, double rfactor); - -static PyObject * -audioop_tomono(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - double lfactor; - double rfactor; - - if (!PyArg_ParseTuple(args, - "y*idd:tomono", - &fragment, &width, &lfactor, &rfactor)) - goto exit; - return_value = audioop_tomono_impl(module, &fragment, width, lfactor, rfactor); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_tostereo__doc__, -"tostereo(module, fragment, width, lfactor, rfactor)\n" -"Generate a stereo fragment from a mono fragment."); - -#define AUDIOOP_TOSTEREO_METHODDEF \ - {"tostereo", (PyCFunction)audioop_tostereo, METH_VARARGS, audioop_tostereo__doc__}, - -static PyObject * -audioop_tostereo_impl(PyModuleDef *module, Py_buffer *fragment, int width, double lfactor, double rfactor); - -static PyObject * -audioop_tostereo(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - double lfactor; - double rfactor; - - if (!PyArg_ParseTuple(args, - "y*idd:tostereo", - &fragment, &width, &lfactor, &rfactor)) - goto exit; - return_value = audioop_tostereo_impl(module, &fragment, width, lfactor, rfactor); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_add__doc__, -"add(module, fragment1, fragment2, width)\n" -"Return a fragment which is the addition of the two samples passed as parameters."); - -#define AUDIOOP_ADD_METHODDEF \ - {"add", (PyCFunction)audioop_add, METH_VARARGS, audioop_add__doc__}, - -static PyObject * -audioop_add_impl(PyModuleDef *module, Py_buffer *fragment1, Py_buffer *fragment2, int width); - -static PyObject * -audioop_add(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment1 = {NULL, NULL}; - Py_buffer fragment2 = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*y*i:add", - &fragment1, &fragment2, &width)) - goto exit; - return_value = audioop_add_impl(module, &fragment1, &fragment2, width); - -exit: - /* Cleanup for fragment1 */ - if (fragment1.obj) - PyBuffer_Release(&fragment1); - /* Cleanup for fragment2 */ - if (fragment2.obj) - PyBuffer_Release(&fragment2); - - return return_value; -} - -PyDoc_STRVAR(audioop_bias__doc__, -"bias(module, fragment, width, bias)\n" -"Return a fragment that is the original fragment with a bias added to each sample."); - -#define AUDIOOP_BIAS_METHODDEF \ - {"bias", (PyCFunction)audioop_bias, METH_VARARGS, audioop_bias__doc__}, - -static PyObject * -audioop_bias_impl(PyModuleDef *module, Py_buffer *fragment, int width, int bias); - -static PyObject * -audioop_bias(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - int bias; - - if (!PyArg_ParseTuple(args, - "y*ii:bias", - &fragment, &width, &bias)) - goto exit; - return_value = audioop_bias_impl(module, &fragment, width, bias); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_reverse__doc__, -"reverse(module, fragment, width)\n" -"Reverse the samples in a fragment and returns the modified fragment."); - -#define AUDIOOP_REVERSE_METHODDEF \ - {"reverse", (PyCFunction)audioop_reverse, METH_VARARGS, audioop_reverse__doc__}, - -static PyObject * -audioop_reverse_impl(PyModuleDef *module, Py_buffer *fragment, int width); - -static PyObject * -audioop_reverse(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*i:reverse", - &fragment, &width)) - goto exit; - return_value = audioop_reverse_impl(module, &fragment, width); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_byteswap__doc__, -"byteswap(module, fragment, width)\n" -"Convert big-endian samples to little-endian and vice versa."); - -#define AUDIOOP_BYTESWAP_METHODDEF \ - {"byteswap", (PyCFunction)audioop_byteswap, METH_VARARGS, audioop_byteswap__doc__}, - -static PyObject * -audioop_byteswap_impl(PyModuleDef *module, Py_buffer *fragment, int width); - -static PyObject * -audioop_byteswap(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*i:byteswap", - &fragment, &width)) - goto exit; - return_value = audioop_byteswap_impl(module, &fragment, width); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_lin2lin__doc__, -"lin2lin(module, fragment, width, newwidth)\n" -"Convert samples between 1-, 2-, 3- and 4-byte formats."); - -#define AUDIOOP_LIN2LIN_METHODDEF \ - {"lin2lin", (PyCFunction)audioop_lin2lin, METH_VARARGS, audioop_lin2lin__doc__}, - -static PyObject * -audioop_lin2lin_impl(PyModuleDef *module, Py_buffer *fragment, int width, int newwidth); - -static PyObject * -audioop_lin2lin(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - int newwidth; - - if (!PyArg_ParseTuple(args, - "y*ii:lin2lin", - &fragment, &width, &newwidth)) - goto exit; - return_value = audioop_lin2lin_impl(module, &fragment, width, newwidth); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_ratecv__doc__, -"ratecv(module, fragment, width, nchannels, inrate, outrate, state, weightA=1, weightB=0)\n" -"Convert the frame rate of the input fragment."); - -#define AUDIOOP_RATECV_METHODDEF \ - {"ratecv", (PyCFunction)audioop_ratecv, METH_VARARGS, audioop_ratecv__doc__}, - -static PyObject * -audioop_ratecv_impl(PyModuleDef *module, Py_buffer *fragment, int width, int nchannels, int inrate, int outrate, PyObject *state, int weightA, int weightB); - -static PyObject * -audioop_ratecv(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - int nchannels; - int inrate; - int outrate; - PyObject *state; - int weightA = 1; - int weightB = 0; - - if (!PyArg_ParseTuple(args, - "y*iiiiO|ii:ratecv", - &fragment, &width, &nchannels, &inrate, &outrate, &state, &weightA, &weightB)) - goto exit; - return_value = audioop_ratecv_impl(module, &fragment, width, nchannels, inrate, outrate, state, weightA, weightB); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_lin2ulaw__doc__, -"lin2ulaw(module, fragment, width)\n" -"Convert samples in the audio fragment to u-LAW encoding."); - -#define AUDIOOP_LIN2ULAW_METHODDEF \ - {"lin2ulaw", (PyCFunction)audioop_lin2ulaw, METH_VARARGS, audioop_lin2ulaw__doc__}, - -static PyObject * -audioop_lin2ulaw_impl(PyModuleDef *module, Py_buffer *fragment, int width); - -static PyObject * -audioop_lin2ulaw(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*i:lin2ulaw", - &fragment, &width)) - goto exit; - return_value = audioop_lin2ulaw_impl(module, &fragment, width); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_ulaw2lin__doc__, -"ulaw2lin(module, fragment, width)\n" -"Convert sound fragments in u-LAW encoding to linearly encoded sound fragments."); - -#define AUDIOOP_ULAW2LIN_METHODDEF \ - {"ulaw2lin", (PyCFunction)audioop_ulaw2lin, METH_VARARGS, audioop_ulaw2lin__doc__}, - -static PyObject * -audioop_ulaw2lin_impl(PyModuleDef *module, Py_buffer *fragment, int width); - -static PyObject * -audioop_ulaw2lin(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*i:ulaw2lin", - &fragment, &width)) - goto exit; - return_value = audioop_ulaw2lin_impl(module, &fragment, width); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_lin2alaw__doc__, -"lin2alaw(module, fragment, width)\n" -"Convert samples in the audio fragment to a-LAW encoding."); - -#define AUDIOOP_LIN2ALAW_METHODDEF \ - {"lin2alaw", (PyCFunction)audioop_lin2alaw, METH_VARARGS, audioop_lin2alaw__doc__}, - -static PyObject * -audioop_lin2alaw_impl(PyModuleDef *module, Py_buffer *fragment, int width); - -static PyObject * -audioop_lin2alaw(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*i:lin2alaw", - &fragment, &width)) - goto exit; - return_value = audioop_lin2alaw_impl(module, &fragment, width); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_alaw2lin__doc__, -"alaw2lin(module, fragment, width)\n" -"Convert sound fragments in a-LAW encoding to linearly encoded sound fragments."); - -#define AUDIOOP_ALAW2LIN_METHODDEF \ - {"alaw2lin", (PyCFunction)audioop_alaw2lin, METH_VARARGS, audioop_alaw2lin__doc__}, - -static PyObject * -audioop_alaw2lin_impl(PyModuleDef *module, Py_buffer *fragment, int width); - -static PyObject * -audioop_alaw2lin(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - - if (!PyArg_ParseTuple(args, - "y*i:alaw2lin", - &fragment, &width)) - goto exit; - return_value = audioop_alaw2lin_impl(module, &fragment, width); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_lin2adpcm__doc__, -"lin2adpcm(module, fragment, width, state)\n" -"Convert samples to 4 bit Intel/DVI ADPCM encoding."); - -#define AUDIOOP_LIN2ADPCM_METHODDEF \ - {"lin2adpcm", (PyCFunction)audioop_lin2adpcm, METH_VARARGS, audioop_lin2adpcm__doc__}, - -static PyObject * -audioop_lin2adpcm_impl(PyModuleDef *module, Py_buffer *fragment, int width, PyObject *state); - -static PyObject * -audioop_lin2adpcm(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - PyObject *state; - - if (!PyArg_ParseTuple(args, - "y*iO:lin2adpcm", - &fragment, &width, &state)) - goto exit; - return_value = audioop_lin2adpcm_impl(module, &fragment, width, state); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} - -PyDoc_STRVAR(audioop_adpcm2lin__doc__, -"adpcm2lin(module, fragment, width, state)\n" -"Decode an Intel/DVI ADPCM coded fragment to a linear fragment."); - -#define AUDIOOP_ADPCM2LIN_METHODDEF \ - {"adpcm2lin", (PyCFunction)audioop_adpcm2lin, METH_VARARGS, audioop_adpcm2lin__doc__}, - -static PyObject * -audioop_adpcm2lin_impl(PyModuleDef *module, Py_buffer *fragment, int width, PyObject *state); - -static PyObject * -audioop_adpcm2lin(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer fragment = {NULL, NULL}; - int width; - PyObject *state; - - if (!PyArg_ParseTuple(args, - "y*iO:adpcm2lin", - &fragment, &width, &state)) - goto exit; - return_value = audioop_adpcm2lin_impl(module, &fragment, width, state); - -exit: - /* Cleanup for fragment */ - if (fragment.obj) - PyBuffer_Release(&fragment); - - return return_value; -} -/*[clinic end generated code: checksum=0d9fa2c5719e996b169f808350016cd622799562]*/ diff --git a/Modules/binascii.clinic.c b/Modules/binascii.clinic.c deleted file mode 100644 index 3e4a468..0000000 --- a/Modules/binascii.clinic.c +++ /dev/null @@ -1,430 +0,0 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -PyDoc_STRVAR(binascii_a2b_uu__doc__, -"a2b_uu(module, data)\n" -"Decode a line of uuencoded data."); - -#define BINASCII_A2B_UU_METHODDEF \ - {"a2b_uu", (PyCFunction)binascii_a2b_uu, METH_VARARGS, binascii_a2b_uu__doc__}, - -static PyObject * -binascii_a2b_uu_impl(PyModuleDef *module, Py_buffer *data); - -static PyObject * -binascii_a2b_uu(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer data; - - if (!PyArg_ParseTuple(args, - "O&:a2b_uu", - ascii_buffer_converter, &data)) - goto exit; - return_value = binascii_a2b_uu_impl(module, &data); - -exit: - return return_value; -} - -PyDoc_STRVAR(binascii_b2a_uu__doc__, -"b2a_uu(module, data)\n" -"Uuencode line of data."); - -#define BINASCII_B2A_UU_METHODDEF \ - {"b2a_uu", (PyCFunction)binascii_b2a_uu, METH_VARARGS, binascii_b2a_uu__doc__}, - -static PyObject * -binascii_b2a_uu_impl(PyModuleDef *module, Py_buffer *data); - -static PyObject * -binascii_b2a_uu(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer data = {NULL, NULL}; - - if (!PyArg_ParseTuple(args, - "y*:b2a_uu", - &data)) - goto exit; - return_value = binascii_b2a_uu_impl(module, &data); - -exit: - /* Cleanup for data */ - if (data.obj) - PyBuffer_Release(&data); - - return return_value; -} - -PyDoc_STRVAR(binascii_a2b_base64__doc__, -"a2b_base64(module, data)\n" -"Decode a line of base64 data."); - -#define BINASCII_A2B_BASE64_METHODDEF \ - {"a2b_base64", (PyCFunction)binascii_a2b_base64, METH_VARARGS, binascii_a2b_base64__doc__}, - -static PyObject * -binascii_a2b_base64_impl(PyModuleDef *module, Py_buffer *data); - -static PyObject * -binascii_a2b_base64(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer data; - - if (!PyArg_ParseTuple(args, - "O&:a2b_base64", - ascii_buffer_converter, &data)) - goto exit; - return_value = binascii_a2b_base64_impl(module, &data); - -exit: - return return_value; -} - -PyDoc_STRVAR(binascii_b2a_base64__doc__, -"b2a_base64(module, data)\n" -"Base64-code line of data."); - -#define BINASCII_B2A_BASE64_METHODDEF \ - {"b2a_base64", (PyCFunction)binascii_b2a_base64, METH_VARARGS, binascii_b2a_base64__doc__}, - -static PyObject * -binascii_b2a_base64_impl(PyModuleDef *module, Py_buffer *data); - -static PyObject * -binascii_b2a_base64(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer data = {NULL, NULL}; - - if (!PyArg_ParseTuple(args, - "y*:b2a_base64", - &data)) - goto exit; - return_value = binascii_b2a_base64_impl(module, &data); - -exit: - /* Cleanup for data */ - if (data.obj) - PyBuffer_Release(&data); - - return return_value; -} - -PyDoc_STRVAR(binascii_a2b_hqx__doc__, -"a2b_hqx(module, data)\n" -"Decode .hqx coding."); - -#define BINASCII_A2B_HQX_METHODDEF \ - {"a2b_hqx", (PyCFunction)binascii_a2b_hqx, METH_VARARGS, binascii_a2b_hqx__doc__}, - -static PyObject * -binascii_a2b_hqx_impl(PyModuleDef *module, Py_buffer *data); - -static PyObject * -binascii_a2b_hqx(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer data; - - if (!PyArg_ParseTuple(args, - "O&:a2b_hqx", - ascii_buffer_converter, &data)) - goto exit; - return_value = binascii_a2b_hqx_impl(module, &data); - -exit: - return return_value; -} - -PyDoc_STRVAR(binascii_rlecode_hqx__doc__, -"rlecode_hqx(module, data)\n" -"Binhex RLE-code binary data."); - -#define BINASCII_RLECODE_HQX_METHODDEF \ - {"rlecode_hqx", (PyCFunction)binascii_rlecode_hqx, METH_VARARGS, binascii_rlecode_hqx__doc__}, - -static PyObject * -binascii_rlecode_hqx_impl(PyModuleDef *module, Py_buffer *data); - -static PyObject * -binascii_rlecode_hqx(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer data = {NULL, NULL}; - - if (!PyArg_ParseTuple(args, - "y*:rlecode_hqx", - &data)) - goto exit; - return_value = binascii_rlecode_hqx_impl(module, &data); - -exit: - /* Cleanup for data */ - if (data.obj) - PyBuffer_Release(&data); - - return return_value; -} - -PyDoc_STRVAR(binascii_b2a_hqx__doc__, -"b2a_hqx(module, data)\n" -"Encode .hqx data."); - -#define BINASCII_B2A_HQX_METHODDEF \ - {"b2a_hqx", (PyCFunction)binascii_b2a_hqx, METH_VARARGS, binascii_b2a_hqx__doc__}, - -static PyObject * -binascii_b2a_hqx_impl(PyModuleDef *module, Py_buffer *data); - -static PyObject * -binascii_b2a_hqx(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer data = {NULL, NULL}; - - if (!PyArg_ParseTuple(args, - "y*:b2a_hqx", - &data)) - goto exit; - return_value = binascii_b2a_hqx_impl(module, &data); - -exit: - /* Cleanup for data */ - if (data.obj) - PyBuffer_Release(&data); - - return return_value; -} - -PyDoc_STRVAR(binascii_rledecode_hqx__doc__, -"rledecode_hqx(module, data)\n" -"Decode hexbin RLE-coded string."); - -#define BINASCII_RLEDECODE_HQX_METHODDEF \ - {"rledecode_hqx", (PyCFunction)binascii_rledecode_hqx, METH_VARARGS, binascii_rledecode_hqx__doc__}, - -static PyObject * -binascii_rledecode_hqx_impl(PyModuleDef *module, Py_buffer *data); - -static PyObject * -binascii_rledecode_hqx(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer data = {NULL, NULL}; - - if (!PyArg_ParseTuple(args, - "y*:rledecode_hqx", - &data)) - goto exit; - return_value = binascii_rledecode_hqx_impl(module, &data); - -exit: - /* Cleanup for data */ - if (data.obj) - PyBuffer_Release(&data); - - return return_value; -} - -PyDoc_STRVAR(binascii_crc_hqx__doc__, -"crc_hqx(module, data, crc)\n" -"Compute hqx CRC incrementally."); - -#define BINASCII_CRC_HQX_METHODDEF \ - {"crc_hqx", (PyCFunction)binascii_crc_hqx, METH_VARARGS, binascii_crc_hqx__doc__}, - -static int -binascii_crc_hqx_impl(PyModuleDef *module, Py_buffer *data, int crc); - -static PyObject * -binascii_crc_hqx(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer data = {NULL, NULL}; - int crc; - int _return_value; - - if (!PyArg_ParseTuple(args, - "y*i:crc_hqx", - &data, &crc)) - goto exit; - _return_value = binascii_crc_hqx_impl(module, &data, crc); - if ((_return_value == -1) && PyErr_Occurred()) - goto exit; - return_value = PyLong_FromLong((long)_return_value); - -exit: - /* Cleanup for data */ - if (data.obj) - PyBuffer_Release(&data); - - return return_value; -} - -PyDoc_STRVAR(binascii_crc32__doc__, -"crc32(module, data, crc=0)\n" -"Compute CRC-32 incrementally."); - -#define BINASCII_CRC32_METHODDEF \ - {"crc32", (PyCFunction)binascii_crc32, METH_VARARGS, binascii_crc32__doc__}, - -static unsigned int -binascii_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int crc); - -static PyObject * -binascii_crc32(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer data = {NULL, NULL}; - unsigned int crc = 0; - unsigned int _return_value; - - if (!PyArg_ParseTuple(args, - "y*|I:crc32", - &data, &crc)) - goto exit; - _return_value = binascii_crc32_impl(module, &data, crc); - if ((_return_value == -1) && PyErr_Occurred()) - goto exit; - return_value = PyLong_FromUnsignedLong((unsigned long)_return_value); - -exit: - /* Cleanup for data */ - if (data.obj) - PyBuffer_Release(&data); - - return return_value; -} - -PyDoc_STRVAR(binascii_b2a_hex__doc__, -"b2a_hex(module, data)\n" -"Hexadecimal representation of binary data.\n" -"\n" -"The return value is a bytes object. This function is also\n" -"available as \"hexlify()\"."); - -#define BINASCII_B2A_HEX_METHODDEF \ - {"b2a_hex", (PyCFunction)binascii_b2a_hex, METH_VARARGS, binascii_b2a_hex__doc__}, - -static PyObject * -binascii_b2a_hex_impl(PyModuleDef *module, Py_buffer *data); - -static PyObject * -binascii_b2a_hex(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer data = {NULL, NULL}; - - if (!PyArg_ParseTuple(args, - "y*:b2a_hex", - &data)) - goto exit; - return_value = binascii_b2a_hex_impl(module, &data); - -exit: - /* Cleanup for data */ - if (data.obj) - PyBuffer_Release(&data); - - return return_value; -} - -PyDoc_STRVAR(binascii_a2b_hex__doc__, -"a2b_hex(module, hexstr)\n" -"Binary data of hexadecimal representation.\n" -"\n" -"hexstr must contain an even number of hex digits (upper or lower case).\n" -"This function is also available as \"unhexlify()\"."); - -#define BINASCII_A2B_HEX_METHODDEF \ - {"a2b_hex", (PyCFunction)binascii_a2b_hex, METH_VARARGS, binascii_a2b_hex__doc__}, - -static PyObject * -binascii_a2b_hex_impl(PyModuleDef *module, Py_buffer *hexstr); - -static PyObject * -binascii_a2b_hex(PyModuleDef *module, PyObject *args) -{ - PyObject *return_value = NULL; - Py_buffer hexstr; - - if (!PyArg_ParseTuple(args, - "O&:a2b_hex", - ascii_buffer_converter, &hexstr)) - goto exit; - return_value = binascii_a2b_hex_impl(module, &hexstr); - -exit: - return return_value; -} - -PyDoc_STRVAR(binascii_a2b_qp__doc__, -"a2b_qp(module, data, header=False)\n" -"Decode a string of qp-encoded data."); - -#define BINASCII_A2B_QP_METHODDEF \ - {"a2b_qp", (PyCFunction)binascii_a2b_qp, METH_VARARGS|METH_KEYWORDS, binascii_a2b_qp__doc__}, - -static PyObject * -binascii_a2b_qp_impl(PyModuleDef *module, Py_buffer *data, int header); - -static PyObject * -binascii_a2b_qp(PyModuleDef *module, PyObject *args, PyObject *kwargs) -{ - PyObject *return_value = NULL; - static char *_keywords[] = {"data", "header", NULL}; - Py_buffer data; - int header = 0; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, - "O&|i:a2b_qp", _keywords, - ascii_buffer_converter, &data, &header)) - goto exit; - return_value = binascii_a2b_qp_impl(module, &data, header); - -exit: - return return_value; -} - -PyDoc_STRVAR(binascii_b2a_qp__doc__, -"b2a_qp(module, data, quotetabs=False, istext=True, header=False)\n" -"Encode a string using quoted-printable encoding.\n" -"\n" -"On encoding, when istext is set, newlines are not encoded, and white\n" -"space at end of lines is. When istext is not set, \\r and \\n (CR/LF)\n" -"are both encoded. When quotetabs is set, space and tabs are encoded."); - -#define BINASCII_B2A_QP_METHODDEF \ - {"b2a_qp", (PyCFunction)binascii_b2a_qp, METH_VARARGS|METH_KEYWORDS, binascii_b2a_qp__doc__}, - -static PyObject * -binascii_b2a_qp_impl(PyModuleDef *module, Py_buffer *data, int quotetabs, int istext, int header); - -static PyObject * -binascii_b2a_qp(PyModuleDef *module, PyObject *args, PyObject *kwargs) -{ - PyObject *return_value = NULL; - static char *_keywords[] = {"data", "quotetabs", "istext", "header", NULL}; - Py_buffer data = {NULL, NULL}; - int quotetabs = 0; - int istext = 1; - int header = 0; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, - "y*|iii:b2a_qp", _keywords, - &data, "etabs, &istext, &header)) - goto exit; - return_value = binascii_b2a_qp_impl(module, &data, quotetabs, istext, header); - -exit: - /* Cleanup for data */ - if (data.obj) - PyBuffer_Release(&data); - - return return_value; -} -/*[clinic end generated code: checksum=abe48ca8020fa3ec25e13bd9fa7414f6b3ee2946]*/ diff --git a/Modules/clinic/_bz2module.c.h b/Modules/clinic/_bz2module.c.h new file mode 100644 index 0000000..1ba87cf --- /dev/null +++ b/Modules/clinic/_bz2module.c.h @@ -0,0 +1,152 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +PyDoc_STRVAR(_bz2_BZ2Compressor_compress__doc__, +"compress(self, data)\n" +"Provide data to the compressor object.\n" +"\n" +"Returns a chunk of compressed data if possible, or b\'\' otherwise.\n" +"\n" +"When you have finished providing data to the compressor, call the\n" +"flush() method to finish the compression process."); + +#define _BZ2_BZ2COMPRESSOR_COMPRESS_METHODDEF \ + {"compress", (PyCFunction)_bz2_BZ2Compressor_compress, METH_VARARGS, _bz2_BZ2Compressor_compress__doc__}, + +static PyObject * +_bz2_BZ2Compressor_compress_impl(BZ2Compressor *self, Py_buffer *data); + +static PyObject * +_bz2_BZ2Compressor_compress(BZ2Compressor *self, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "y*:compress", + &data)) + goto exit; + return_value = _bz2_BZ2Compressor_compress_impl(self, &data); + +exit: + /* Cleanup for data */ + if (data.obj) + PyBuffer_Release(&data); + + return return_value; +} + +PyDoc_STRVAR(_bz2_BZ2Compressor_flush__doc__, +"flush(self)\n" +"Finish the compression process.\n" +"\n" +"Returns the compressed data left in internal buffers.\n" +"\n" +"The compressor object may not be used after this method is called."); + +#define _BZ2_BZ2COMPRESSOR_FLUSH_METHODDEF \ + {"flush", (PyCFunction)_bz2_BZ2Compressor_flush, METH_NOARGS, _bz2_BZ2Compressor_flush__doc__}, + +static PyObject * +_bz2_BZ2Compressor_flush_impl(BZ2Compressor *self); + +static PyObject * +_bz2_BZ2Compressor_flush(BZ2Compressor *self, PyObject *Py_UNUSED(ignored)) +{ + return _bz2_BZ2Compressor_flush_impl(self); +} + +PyDoc_STRVAR(_bz2_BZ2Compressor___init____doc__, +"BZ2Compressor(compresslevel=9)\n" +"Create a compressor object for compressing data incrementally.\n" +"\n" +" compresslevel\n" +" Compression level, as a number between 1 and 9.\n" +"\n" +"For one-shot compression, use the compress() function instead."); + +static int +_bz2_BZ2Compressor___init___impl(BZ2Compressor *self, int compresslevel); + +static int +_bz2_BZ2Compressor___init__(PyObject *self, PyObject *args, PyObject *kwargs) +{ + int return_value = -1; + int compresslevel = 9; + + if (({self_name} == {self_type_object}) && + !_PyArg_NoKeywords("BZ2Compressor", kwargs)) + goto exit; + if (!PyArg_ParseTuple(args, + "|i:BZ2Compressor", + &compresslevel)) + goto exit; + return_value = _bz2_BZ2Compressor___init___impl((BZ2Compressor *)self, compresslevel); + +exit: + return return_value; +} + +PyDoc_STRVAR(_bz2_BZ2Decompressor_decompress__doc__, +"decompress(self, data)\n" +"Provide data to the decompressor object.\n" +"\n" +"Returns a chunk of decompressed data if possible, or b\'\' otherwise.\n" +"\n" +"Attempting to decompress data after the end of stream is reached\n" +"raises an EOFError. Any data found after the end of the stream\n" +"is ignored and saved in the unused_data attribute."); + +#define _BZ2_BZ2DECOMPRESSOR_DECOMPRESS_METHODDEF \ + {"decompress", (PyCFunction)_bz2_BZ2Decompressor_decompress, METH_VARARGS, _bz2_BZ2Decompressor_decompress__doc__}, + +static PyObject * +_bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data); + +static PyObject * +_bz2_BZ2Decompressor_decompress(BZ2Decompressor *self, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "y*:decompress", + &data)) + goto exit; + return_value = _bz2_BZ2Decompressor_decompress_impl(self, &data); + +exit: + /* Cleanup for data */ + if (data.obj) + PyBuffer_Release(&data); + + return return_value; +} + +PyDoc_STRVAR(_bz2_BZ2Decompressor___init____doc__, +"BZ2Decompressor()\n" +"Create a decompressor object for decompressing data incrementally.\n" +"\n" +"For one-shot decompression, use the decompress() function instead."); + +static int +_bz2_BZ2Decompressor___init___impl(BZ2Decompressor *self); + +static int +_bz2_BZ2Decompressor___init__(PyObject *self, PyObject *args, PyObject *kwargs) +{ + int return_value = -1; + + if (({self_name} == {self_type_object}) && + !_PyArg_NoPositional("BZ2Decompressor", args)) + goto exit; + if (({self_name} == {self_type_object}) && + !_PyArg_NoKeywords("BZ2Decompressor", kwargs)) + goto exit; + return_value = _bz2_BZ2Decompressor___init___impl((BZ2Decompressor *)self); + +exit: + return return_value; +} +/*[clinic end generated code: checksum=79ee0d9731dfe404baec35b704b2ca2179b9a6c0]*/ diff --git a/Modules/clinic/_lzmamodule.c.h b/Modules/clinic/_lzmamodule.c.h new file mode 100644 index 0000000..c8899c9 --- /dev/null +++ b/Modules/clinic/_lzmamodule.c.h @@ -0,0 +1,1389 @@ +/* _lzma - Low-level Python interface to liblzma. + + Initial implementation by Per Øyvind Karlsen. + Rewritten by Nadeem Vawda. + +*/ + +#define PY_SSIZE_T_CLEAN + +#include "Python.h" +#include "structmember.h" +#ifdef WITH_THREAD +#include "pythread.h" +#endif + +#include +#include + +#include + + +#ifndef PY_LONG_LONG +#error "This module requires PY_LONG_LONG to be defined" +#endif + + +#ifdef WITH_THREAD +#define ACQUIRE_LOCK(obj) do { \ + if (!PyThread_acquire_lock((obj)->lock, 0)) { \ + Py_BEGIN_ALLOW_THREADS \ + PyThread_acquire_lock((obj)->lock, 1); \ + Py_END_ALLOW_THREADS \ + } } while (0) +#define RELEASE_LOCK(obj) PyThread_release_lock((obj)->lock) +#else +#define ACQUIRE_LOCK(obj) +#define RELEASE_LOCK(obj) +#endif + + +/* Container formats: */ +enum { + FORMAT_AUTO, + FORMAT_XZ, + FORMAT_ALONE, + FORMAT_RAW, +}; + +#define LZMA_CHECK_UNKNOWN (LZMA_CHECK_ID_MAX + 1) + + +typedef struct { + PyObject_HEAD + lzma_allocator alloc; + lzma_stream lzs; + int flushed; +#ifdef WITH_THREAD + PyThread_type_lock lock; +#endif +} Compressor; + +typedef struct { + PyObject_HEAD + lzma_allocator alloc; + lzma_stream lzs; + int check; + char eof; + PyObject *unused_data; +#ifdef WITH_THREAD + PyThread_type_lock lock; +#endif +} Decompressor; + +/* LZMAError class object. */ +static PyObject *Error; + +/* An empty tuple, used by the filter specifier parsing code. */ +static PyObject *empty_tuple; + + +/* Helper functions. */ + +static int +catch_lzma_error(lzma_ret lzret) +{ + switch (lzret) { + case LZMA_OK: + case LZMA_GET_CHECK: + case LZMA_NO_CHECK: + case LZMA_STREAM_END: + return 0; + case LZMA_UNSUPPORTED_CHECK: + PyErr_SetString(Error, "Unsupported integrity check"); + return 1; + case LZMA_MEM_ERROR: + PyErr_NoMemory(); + return 1; + case LZMA_MEMLIMIT_ERROR: + PyErr_SetString(Error, "Memory usage limit exceeded"); + return 1; + case LZMA_FORMAT_ERROR: + PyErr_SetString(Error, "Input format not supported by decoder"); + return 1; + case LZMA_OPTIONS_ERROR: + PyErr_SetString(Error, "Invalid or unsupported options"); + return 1; + case LZMA_DATA_ERROR: + PyErr_SetString(Error, "Corrupt input data"); + return 1; + case LZMA_BUF_ERROR: + PyErr_SetString(Error, "Insufficient buffer space"); + return 1; + case LZMA_PROG_ERROR: + PyErr_SetString(Error, "Internal error"); + return 1; + default: + PyErr_Format(Error, "Unrecognized error from liblzma: %d", lzret); + return 1; + } +} + +static void* +PyLzma_Malloc(void *opaque, size_t items, size_t size) +{ + if (items > (size_t)PY_SSIZE_T_MAX / size) + return NULL; + /* PyMem_Malloc() cannot be used: + the GIL is not held when lzma_code() is called */ + return PyMem_RawMalloc(items * size); +} + +static void +PyLzma_Free(void *opaque, void *ptr) +{ + PyMem_RawFree(ptr); +} + +#if BUFSIZ < 8192 +#define INITIAL_BUFFER_SIZE 8192 +#else +#define INITIAL_BUFFER_SIZE BUFSIZ +#endif + +static int +grow_buffer(PyObject **buf) +{ + size_t size = PyBytes_GET_SIZE(*buf); + return _PyBytes_Resize(buf, size + (size >> 3) + 6); +} + + +/* Some custom type conversions for PyArg_ParseTupleAndKeywords(), + since the predefined conversion specifiers do not suit our needs: + + uint32_t - the "I" (unsigned int) specifier is the right size, but + silently ignores overflows on conversion. + + lzma_vli - the "K" (unsigned PY_LONG_LONG) specifier is the right + size, but like "I" it silently ignores overflows on conversion. + + lzma_mode and lzma_match_finder - these are enumeration types, and + so the size of each is implementation-defined. Worse, different + enum types can be of different sizes within the same program, so + to be strictly correct, we need to define two separate converters. + */ + +#define INT_TYPE_CONVERTER_FUNC(TYPE, FUNCNAME) \ + static int \ + FUNCNAME(PyObject *obj, void *ptr) \ + { \ + unsigned PY_LONG_LONG val; \ + \ + val = PyLong_AsUnsignedLongLong(obj); \ + if (PyErr_Occurred()) \ + return 0; \ + if ((unsigned PY_LONG_LONG)(TYPE)val != val) { \ + PyErr_SetString(PyExc_OverflowError, \ + "Value too large for " #TYPE " type"); \ + return 0; \ + } \ + *(TYPE *)ptr = (TYPE)val; \ + return 1; \ + } + +INT_TYPE_CONVERTER_FUNC(uint32_t, uint32_converter) +INT_TYPE_CONVERTER_FUNC(lzma_vli, lzma_vli_converter) +INT_TYPE_CONVERTER_FUNC(lzma_mode, lzma_mode_converter) +INT_TYPE_CONVERTER_FUNC(lzma_match_finder, lzma_mf_converter) + +#undef INT_TYPE_CONVERTER_FUNC + + +/* Filter specifier parsing. + + This code handles converting filter specifiers (Python dicts) into + the C lzma_filter structs expected by liblzma. */ + +static void * +parse_filter_spec_lzma(PyObject *spec) +{ + static char *optnames[] = {"id", "preset", "dict_size", "lc", "lp", + "pb", "mode", "nice_len", "mf", "depth", NULL}; + PyObject *id; + PyObject *preset_obj; + uint32_t preset = LZMA_PRESET_DEFAULT; + lzma_options_lzma *options; + + /* First, fill in default values for all the options using a preset. + Then, override the defaults with any values given by the caller. */ + + preset_obj = PyMapping_GetItemString(spec, "preset"); + if (preset_obj == NULL) { + if (PyErr_ExceptionMatches(PyExc_KeyError)) + PyErr_Clear(); + else + return NULL; + } else { + int ok = uint32_converter(preset_obj, &preset); + Py_DECREF(preset_obj); + if (!ok) + return NULL; + } + + options = (lzma_options_lzma *)PyMem_Malloc(sizeof *options); + if (options == NULL) + return PyErr_NoMemory(); + memset(options, 0, sizeof *options); + + if (lzma_lzma_preset(options, preset)) { + PyMem_Free(options); + PyErr_Format(Error, "Invalid compression preset: %d", preset); + return NULL; + } + + if (!PyArg_ParseTupleAndKeywords(empty_tuple, spec, + "|OOO&O&O&O&O&O&O&O&", optnames, + &id, &preset_obj, + uint32_converter, &options->dict_size, + uint32_converter, &options->lc, + uint32_converter, &options->lp, + uint32_converter, &options->pb, + lzma_mode_converter, &options->mode, + uint32_converter, &options->nice_len, + lzma_mf_converter, &options->mf, + uint32_converter, &options->depth)) { + PyErr_SetString(PyExc_ValueError, + "Invalid filter specifier for LZMA filter"); + PyMem_Free(options); + options = NULL; + } + return options; +} + +static void * +parse_filter_spec_delta(PyObject *spec) +{ + static char *optnames[] = {"id", "dist", NULL}; + PyObject *id; + uint32_t dist = 1; + lzma_options_delta *options; + + if (!PyArg_ParseTupleAndKeywords(empty_tuple, spec, "|OO&", optnames, + &id, uint32_converter, &dist)) { + PyErr_SetString(PyExc_ValueError, + "Invalid filter specifier for delta filter"); + return NULL; + } + + options = (lzma_options_delta *)PyMem_Malloc(sizeof *options); + if (options == NULL) + return PyErr_NoMemory(); + memset(options, 0, sizeof *options); + options->type = LZMA_DELTA_TYPE_BYTE; + options->dist = dist; + return options; +} + +static void * +parse_filter_spec_bcj(PyObject *spec) +{ + static char *optnames[] = {"id", "start_offset", NULL}; + PyObject *id; + uint32_t start_offset = 0; + lzma_options_bcj *options; + + if (!PyArg_ParseTupleAndKeywords(empty_tuple, spec, "|OO&", optnames, + &id, uint32_converter, &start_offset)) { + PyErr_SetString(PyExc_ValueError, + "Invalid filter specifier for BCJ filter"); + return NULL; + } + + options = (lzma_options_bcj *)PyMem_Malloc(sizeof *options); + if (options == NULL) + return PyErr_NoMemory(); + memset(options, 0, sizeof *options); + options->start_offset = start_offset; + return options; +} + +static int +lzma_filter_converter(PyObject *spec, void *ptr) +{ + lzma_filter *f = (lzma_filter *)ptr; + PyObject *id_obj; + + if (!PyMapping_Check(spec)) { + PyErr_SetString(PyExc_TypeError, + "Filter specifier must be a dict or dict-like object"); + return 0; + } + id_obj = PyMapping_GetItemString(spec, "id"); + if (id_obj == NULL) { + if (PyErr_ExceptionMatches(PyExc_KeyError)) + PyErr_SetString(PyExc_ValueError, + "Filter specifier must have an \"id\" entry"); + return 0; + } + f->id = PyLong_AsUnsignedLongLong(id_obj); + Py_DECREF(id_obj); + if (PyErr_Occurred()) + return 0; + + switch (f->id) { + case LZMA_FILTER_LZMA1: + case LZMA_FILTER_LZMA2: + f->options = parse_filter_spec_lzma(spec); + return f->options != NULL; + case LZMA_FILTER_DELTA: + f->options = parse_filter_spec_delta(spec); + return f->options != NULL; + case LZMA_FILTER_X86: + case LZMA_FILTER_POWERPC: + case LZMA_FILTER_IA64: + case LZMA_FILTER_ARM: + case LZMA_FILTER_ARMTHUMB: + case LZMA_FILTER_SPARC: + f->options = parse_filter_spec_bcj(spec); + return f->options != NULL; + default: + PyErr_Format(PyExc_ValueError, "Invalid filter ID: %llu", f->id); + return 0; + } +} + +static void +free_filter_chain(lzma_filter filters[]) +{ + int i; + + for (i = 0; filters[i].id != LZMA_VLI_UNKNOWN; i++) + PyMem_Free(filters[i].options); +} + +static int +parse_filter_chain_spec(lzma_filter filters[], PyObject *filterspecs) +{ + Py_ssize_t i, num_filters; + + num_filters = PySequence_Length(filterspecs); + if (num_filters == -1) + return -1; + if (num_filters > LZMA_FILTERS_MAX) { + PyErr_Format(PyExc_ValueError, + "Too many filters - liblzma supports a maximum of %d", + LZMA_FILTERS_MAX); + return -1; + } + + for (i = 0; i < num_filters; i++) { + int ok = 1; + PyObject *spec = PySequence_GetItem(filterspecs, i); + if (spec == NULL || !lzma_filter_converter(spec, &filters[i])) + ok = 0; + Py_XDECREF(spec); + if (!ok) { + filters[i].id = LZMA_VLI_UNKNOWN; + free_filter_chain(filters); + return -1; + } + } + filters[num_filters].id = LZMA_VLI_UNKNOWN; + return 0; +} + + +/* Filter specifier construction. + + This code handles converting C lzma_filter structs into + Python-level filter specifiers (represented as dicts). */ + +static int +spec_add_field(PyObject *spec, _Py_Identifier *key, unsigned PY_LONG_LONG value) +{ + int status; + PyObject *value_object; + + value_object = PyLong_FromUnsignedLongLong(value); + if (value_object == NULL) + return -1; + + status = _PyDict_SetItemId(spec, key, value_object); + Py_DECREF(value_object); + return status; +} + +static PyObject * +build_filter_spec(const lzma_filter *f) +{ + PyObject *spec; + + spec = PyDict_New(); + if (spec == NULL) + return NULL; + +#define ADD_FIELD(SOURCE, FIELD) \ + do { \ + _Py_IDENTIFIER(FIELD); \ + if (spec_add_field(spec, &PyId_##FIELD, SOURCE->FIELD) == -1) \ + goto error;\ + } while (0) + + ADD_FIELD(f, id); + + switch (f->id) { + /* For LZMA1 filters, lzma_properties_{encode,decode}() only look at the + lc, lp, pb, and dict_size fields. For LZMA2 filters, only the + dict_size field is used. */ + case LZMA_FILTER_LZMA1: { + lzma_options_lzma *options = f->options; + ADD_FIELD(options, lc); + ADD_FIELD(options, lp); + ADD_FIELD(options, pb); + ADD_FIELD(options, dict_size); + break; + } + case LZMA_FILTER_LZMA2: { + lzma_options_lzma *options = f->options; + ADD_FIELD(options, dict_size); + break; + } + case LZMA_FILTER_DELTA: { + lzma_options_delta *options = f->options; + ADD_FIELD(options, dist); + break; + } + case LZMA_FILTER_X86: + case LZMA_FILTER_POWERPC: + case LZMA_FILTER_IA64: + case LZMA_FILTER_ARM: + case LZMA_FILTER_ARMTHUMB: + case LZMA_FILTER_SPARC: { + lzma_options_bcj *options = f->options; + ADD_FIELD(options, start_offset); + break; + } + default: + PyErr_Format(PyExc_ValueError, "Invalid filter ID: %llu", f->id); + goto error; + } + +#undef ADD_FIELD + + return spec; + +error: + Py_DECREF(spec); + return NULL; +} + + +/*[clinic input] +output preset file +module _lzma +class _lzma.LZMACompressor "Compressor *" "&Compressor_type" +class _lzma.LZMADecompressor "Decompressor *" "&Decompressor_type" +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + +#include "_lzmamodule.clinic.c" + +/*[python input] + +class lzma_vli_converter(CConverter): + type = 'lzma_vli' + converter = 'lzma_vli_converter' + +class lzma_filter_converter(CConverter): + type = 'lzma_filter' + converter = 'lzma_filter_converter' + c_default = c_ignored_default = "{LZMA_VLI_UNKNOWN, NULL}" + + def cleanup(self): + name = ensure_legal_c_identifier(self.name) + return ('if (%(name)s.id != LZMA_VLI_UNKNOWN)\n' + ' PyMem_Free(%(name)s.options);\n') % {'name': name} + +[python start generated code]*/ +/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + + +/* LZMACompressor class. */ + +static PyObject * +compress(Compressor *c, uint8_t *data, size_t len, lzma_action action) +{ + size_t data_size = 0; + PyObject *result; + + result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE); + if (result == NULL) + return NULL; + c->lzs.next_in = data; + c->lzs.avail_in = len; + c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result); + c->lzs.avail_out = PyBytes_GET_SIZE(result); + for (;;) { + lzma_ret lzret; + + Py_BEGIN_ALLOW_THREADS + lzret = lzma_code(&c->lzs, action); + data_size = (char *)c->lzs.next_out - PyBytes_AS_STRING(result); + Py_END_ALLOW_THREADS + if (catch_lzma_error(lzret)) + goto error; + if ((action == LZMA_RUN && c->lzs.avail_in == 0) || + (action == LZMA_FINISH && lzret == LZMA_STREAM_END)) { + break; + } else if (c->lzs.avail_out == 0) { + if (grow_buffer(&result) == -1) + goto error; + c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size; + c->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size; + } + } + if (data_size != PyBytes_GET_SIZE(result)) + if (_PyBytes_Resize(&result, data_size) == -1) + goto error; + return result; + +error: + Py_XDECREF(result); + return NULL; +} + +/*[clinic input] +_lzma.LZMACompressor.compress + + self: self(type="Compressor *") + data: Py_buffer + / + +Provide data to the compressor object. + +Returns a chunk of compressed data if possible, or b'' otherwise. + +When you have finished providing data to the compressor, call the +flush() method to finish the compression process. +[clinic start generated code]*/ + +static PyObject * +_lzma_LZMACompressor_compress_impl(Compressor *self, Py_buffer *data) +/*[clinic end generated code: checksum=31f615136963e00f26f8be33440ec1e3604565ba]*/ +{ + PyObject *result = NULL; + + ACQUIRE_LOCK(self); + if (self->flushed) + PyErr_SetString(PyExc_ValueError, "Compressor has been flushed"); + else + result = compress(self, data->buf, data->len, LZMA_RUN); + RELEASE_LOCK(self); + return result; +} + +/*[clinic input] +_lzma.LZMACompressor.flush + + self: self(type="Compressor *") + +Finish the compression process. + +Returns the compressed data left in internal buffers. + +The compressor object may not be used after this method is called. +[clinic start generated code]*/ + +static PyObject * +_lzma_LZMACompressor_flush_impl(Compressor *self) +/*[clinic end generated code: checksum=fec21f3e22504f500606ba60e1ba70d79eb22188]*/ +{ + PyObject *result = NULL; + + ACQUIRE_LOCK(self); + if (self->flushed) { + PyErr_SetString(PyExc_ValueError, "Repeated call to flush()"); + } else { + self->flushed = 1; + result = compress(self, NULL, 0, LZMA_FINISH); + } + RELEASE_LOCK(self); + return result; +} + +static PyObject * +Compressor_getstate(Compressor *self, PyObject *noargs) +{ + PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object", + Py_TYPE(self)->tp_name); + return NULL; +} + +static int +Compressor_init_xz(lzma_stream *lzs, int check, uint32_t preset, + PyObject *filterspecs) +{ + lzma_ret lzret; + + if (filterspecs == Py_None) { + lzret = lzma_easy_encoder(lzs, preset, check); + } else { + lzma_filter filters[LZMA_FILTERS_MAX + 1]; + + if (parse_filter_chain_spec(filters, filterspecs) == -1) + return -1; + lzret = lzma_stream_encoder(lzs, filters, check); + free_filter_chain(filters); + } + if (catch_lzma_error(lzret)) + return -1; + else + return 0; +} + +static int +Compressor_init_alone(lzma_stream *lzs, uint32_t preset, PyObject *filterspecs) +{ + lzma_ret lzret; + + if (filterspecs == Py_None) { + lzma_options_lzma options; + + if (lzma_lzma_preset(&options, preset)) { + PyErr_Format(Error, "Invalid compression preset: %d", preset); + return -1; + } + lzret = lzma_alone_encoder(lzs, &options); + } else { + lzma_filter filters[LZMA_FILTERS_MAX + 1]; + + if (parse_filter_chain_spec(filters, filterspecs) == -1) + return -1; + if (filters[0].id == LZMA_FILTER_LZMA1 && + filters[1].id == LZMA_VLI_UNKNOWN) { + lzret = lzma_alone_encoder(lzs, filters[0].options); + } else { + PyErr_SetString(PyExc_ValueError, + "Invalid filter chain for FORMAT_ALONE - " + "must be a single LZMA1 filter"); + lzret = LZMA_PROG_ERROR; + } + free_filter_chain(filters); + } + if (PyErr_Occurred() || catch_lzma_error(lzret)) + return -1; + else + return 0; +} + +static int +Compressor_init_raw(lzma_stream *lzs, PyObject *filterspecs) +{ + lzma_filter filters[LZMA_FILTERS_MAX + 1]; + lzma_ret lzret; + + if (filterspecs == Py_None) { + PyErr_SetString(PyExc_ValueError, + "Must specify filters for FORMAT_RAW"); + return -1; + } + if (parse_filter_chain_spec(filters, filterspecs) == -1) + return -1; + lzret = lzma_raw_encoder(lzs, filters); + free_filter_chain(filters); + if (catch_lzma_error(lzret)) + return -1; + else + return 0; +} + +/*[-clinic input] +_lzma.LZMACompressor.__init__ + + self: self(type="Compressor *") + format: int(c_default="FORMAT_XZ") = FORMAT_XZ + The container format to use for the output. This can + be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW. + + check: int(c_default="-1") = unspecified + The integrity check to use. For FORMAT_XZ, the default + is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not suport integrity + checks; for these formats, check must be omitted, or be CHECK_NONE. + + preset: object = None + If provided should be an integer in the range 0-9, optionally + OR-ed with the constant PRESET_EXTREME. + + filters: object = None + If provided should be a sequence of dicts. Each dict should + have an entry for "id" indicating the ID of the filter, plus + additional entries for options to the filter. + +Create a compressor object for compressing data incrementally. + +The settings used by the compressor can be specified either as a +preset compression level (with the 'preset' argument), or in detail +as a custom filter chain (with the 'filters' argument). For FORMAT_XZ +and FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset +level. For FORMAT_RAW, the caller must always specify a filter chain; +the raw compressor does not support preset compression levels. + +For one-shot compression, use the compress() function instead. +[-clinic start generated code]*/ +static int +Compressor_init(Compressor *self, PyObject *args, PyObject *kwargs) +{ + static char *arg_names[] = {"format", "check", "preset", "filters", NULL}; + int format = FORMAT_XZ; + int check = -1; + uint32_t preset = LZMA_PRESET_DEFAULT; + PyObject *preset_obj = Py_None; + PyObject *filterspecs = Py_None; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|iiOO:LZMACompressor", arg_names, + &format, &check, &preset_obj, + &filterspecs)) + return -1; + + if (format != FORMAT_XZ && check != -1 && check != LZMA_CHECK_NONE) { + PyErr_SetString(PyExc_ValueError, + "Integrity checks are only supported by FORMAT_XZ"); + return -1; + } + + if (preset_obj != Py_None && filterspecs != Py_None) { + PyErr_SetString(PyExc_ValueError, + "Cannot specify both preset and filter chain"); + return -1; + } + + if (preset_obj != Py_None) + if (!uint32_converter(preset_obj, &preset)) + return -1; + + self->alloc.opaque = NULL; + self->alloc.alloc = PyLzma_Malloc; + self->alloc.free = PyLzma_Free; + self->lzs.allocator = &self->alloc; + +#ifdef WITH_THREAD + self->lock = PyThread_allocate_lock(); + if (self->lock == NULL) { + PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock"); + return -1; + } +#endif + + self->flushed = 0; + switch (format) { + case FORMAT_XZ: + if (check == -1) + check = LZMA_CHECK_CRC64; + if (Compressor_init_xz(&self->lzs, check, preset, filterspecs) != 0) + break; + return 0; + + case FORMAT_ALONE: + if (Compressor_init_alone(&self->lzs, preset, filterspecs) != 0) + break; + return 0; + + case FORMAT_RAW: + if (Compressor_init_raw(&self->lzs, filterspecs) != 0) + break; + return 0; + + default: + PyErr_Format(PyExc_ValueError, + "Invalid container format: %d", format); + break; + } + +#ifdef WITH_THREAD + PyThread_free_lock(self->lock); + self->lock = NULL; +#endif + return -1; +} + +static void +Compressor_dealloc(Compressor *self) +{ + lzma_end(&self->lzs); +#ifdef WITH_THREAD + if (self->lock != NULL) + PyThread_free_lock(self->lock); +#endif + Py_TYPE(self)->tp_free((PyObject *)self); +} + +static PyMethodDef Compressor_methods[] = { + _LZMA_LZMACOMPRESSOR_COMPRESS_METHODDEF + _LZMA_LZMACOMPRESSOR_FLUSH_METHODDEF + {"__getstate__", (PyCFunction)Compressor_getstate, METH_NOARGS}, + {NULL} +}; + +PyDoc_STRVAR(Compressor_doc, +"LZMACompressor(format=FORMAT_XZ, check=-1, preset=None, filters=None)\n" +"\n" +"Create a compressor object for compressing data incrementally.\n" +"\n" +"format specifies the container format to use for the output. This can\n" +"be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW.\n" +"\n" +"check specifies the integrity check to use. For FORMAT_XZ, the default\n" +"is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not suport integrity\n" +"checks; for these formats, check must be omitted, or be CHECK_NONE.\n" +"\n" +"The settings used by the compressor can be specified either as a\n" +"preset compression level (with the 'preset' argument), or in detail\n" +"as a custom filter chain (with the 'filters' argument). For FORMAT_XZ\n" +"and FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset\n" +"level. For FORMAT_RAW, the caller must always specify a filter chain;\n" +"the raw compressor does not support preset compression levels.\n" +"\n" +"preset (if provided) should be an integer in the range 0-9, optionally\n" +"OR-ed with the constant PRESET_EXTREME.\n" +"\n" +"filters (if provided) should be a sequence of dicts. Each dict should\n" +"have an entry for \"id\" indicating the ID of the filter, plus\n" +"additional entries for options to the filter.\n" +"\n" +"For one-shot compression, use the compress() function instead.\n"); + +static PyTypeObject Compressor_type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_lzma.LZMACompressor", /* tp_name */ + sizeof(Compressor), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)Compressor_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + Compressor_doc, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Compressor_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)Compressor_init, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ +}; + + +/* LZMADecompressor class. */ + +static PyObject * +decompress(Decompressor *d, uint8_t *data, size_t len) +{ + size_t data_size = 0; + PyObject *result; + + result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE); + if (result == NULL) + return NULL; + d->lzs.next_in = data; + d->lzs.avail_in = len; + d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result); + d->lzs.avail_out = PyBytes_GET_SIZE(result); + for (;;) { + lzma_ret lzret; + + Py_BEGIN_ALLOW_THREADS + lzret = lzma_code(&d->lzs, LZMA_RUN); + data_size = (char *)d->lzs.next_out - PyBytes_AS_STRING(result); + Py_END_ALLOW_THREADS + if (catch_lzma_error(lzret)) + goto error; + if (lzret == LZMA_GET_CHECK || lzret == LZMA_NO_CHECK) + d->check = lzma_get_check(&d->lzs); + if (lzret == LZMA_STREAM_END) { + d->eof = 1; + if (d->lzs.avail_in > 0) { + Py_CLEAR(d->unused_data); + d->unused_data = PyBytes_FromStringAndSize( + (char *)d->lzs.next_in, d->lzs.avail_in); + if (d->unused_data == NULL) + goto error; + } + break; + } else if (d->lzs.avail_in == 0) { + break; + } else if (d->lzs.avail_out == 0) { + if (grow_buffer(&result) == -1) + goto error; + d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size; + d->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size; + } + } + if (data_size != PyBytes_GET_SIZE(result)) + if (_PyBytes_Resize(&result, data_size) == -1) + goto error; + return result; + +error: + Py_XDECREF(result); + return NULL; +} + +/*[clinic input] +_lzma.LZMADecompressor.decompress + + self: self(type="Decompressor *") + data: Py_buffer + / + +Provide data to the decompressor object. + +Returns a chunk of decompressed data if possible, or b'' otherwise. + +Attempting to decompress data after the end of stream is reached +raises an EOFError. Any data found after the end of the stream +is ignored and saved in the unused_data attribute. +[clinic start generated code]*/ + +static PyObject * +_lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data) +/*[clinic end generated code: checksum=d86e78da7ff0ff219d511275b16b79476da8922e]*/ +{ + PyObject *result = NULL; + + ACQUIRE_LOCK(self); + if (self->eof) + PyErr_SetString(PyExc_EOFError, "Already at end of stream"); + else + result = decompress(self, data->buf, data->len); + RELEASE_LOCK(self); + return result; +} + +static PyObject * +Decompressor_getstate(Decompressor *self, PyObject *noargs) +{ + PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object", + Py_TYPE(self)->tp_name); + return NULL; +} + +static int +Decompressor_init_raw(lzma_stream *lzs, PyObject *filterspecs) +{ + lzma_filter filters[LZMA_FILTERS_MAX + 1]; + lzma_ret lzret; + + if (parse_filter_chain_spec(filters, filterspecs) == -1) + return -1; + lzret = lzma_raw_decoder(lzs, filters); + free_filter_chain(filters); + if (catch_lzma_error(lzret)) + return -1; + else + return 0; +} + +/*[clinic input] +_lzma.LZMADecompressor.__init__ + + self: self(type="Decompressor *") + format: int(c_default="FORMAT_AUTO") = FORMAT_AUTO + Specifies the container format of the input stream. If this is + FORMAT_AUTO (the default), the decompressor will automatically detect + whether the input is FORMAT_XZ or FORMAT_ALONE. Streams created with + FORMAT_RAW cannot be autodetected. + + memlimit: object = None + Limit the amount of memory used by the decompressor. This will cause + decompression to fail if the input cannot be decompressed within the + given limit. + + filters: object = None + A custom filter chain. This argument is required for FORMAT_RAW, and + not accepted with any other format. When provided, this should be a + sequence of dicts, each indicating the ID and options for a single + filter. + +Create a decompressor object for decompressing data incrementally. + +For one-shot decompression, use the decompress() function instead. +[clinic start generated code]*/ + +static int +_lzma_LZMADecompressor___init___impl(Decompressor *self, int format, PyObject *memlimit, PyObject *filters) +/*[clinic end generated code: checksum=9b119f6f2cc2d7a8e5be41c164a6c080ee82d0c2]*/ +{ + const uint32_t decoder_flags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK; + uint64_t memlimit_ = UINT64_MAX; + lzma_ret lzret; + + if (memlimit != Py_None) { + if (format == FORMAT_RAW) { + PyErr_SetString(PyExc_ValueError, + "Cannot specify memory limit with FORMAT_RAW"); + return -1; + } + memlimit_ = PyLong_AsUnsignedLongLong(memlimit); + if (PyErr_Occurred()) + return -1; + } + + if (format == FORMAT_RAW && filters == Py_None) { + PyErr_SetString(PyExc_ValueError, + "Must specify filters for FORMAT_RAW"); + return -1; + } else if (format != FORMAT_RAW && filters != Py_None) { + PyErr_SetString(PyExc_ValueError, + "Cannot specify filters except with FORMAT_RAW"); + return -1; + } + + self->alloc.opaque = NULL; + self->alloc.alloc = PyLzma_Malloc; + self->alloc.free = PyLzma_Free; + self->lzs.allocator = &self->alloc; + +#ifdef WITH_THREAD + self->lock = PyThread_allocate_lock(); + if (self->lock == NULL) { + PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock"); + return -1; + } +#endif + + self->check = LZMA_CHECK_UNKNOWN; + self->unused_data = PyBytes_FromStringAndSize(NULL, 0); + if (self->unused_data == NULL) + goto error; + + switch (format) { + case FORMAT_AUTO: + lzret = lzma_auto_decoder(&self->lzs, memlimit_, decoder_flags); + if (catch_lzma_error(lzret)) + break; + return 0; + + case FORMAT_XZ: + lzret = lzma_stream_decoder(&self->lzs, memlimit_, decoder_flags); + if (catch_lzma_error(lzret)) + break; + return 0; + + case FORMAT_ALONE: + self->check = LZMA_CHECK_NONE; + lzret = lzma_alone_decoder(&self->lzs, memlimit_); + if (catch_lzma_error(lzret)) + break; + return 0; + + case FORMAT_RAW: + self->check = LZMA_CHECK_NONE; + if (Decompressor_init_raw(&self->lzs, filters) == -1) + break; + return 0; + + default: + PyErr_Format(PyExc_ValueError, + "Invalid container format: %d", format); + break; + } + +error: + Py_CLEAR(self->unused_data); +#ifdef WITH_THREAD + PyThread_free_lock(self->lock); + self->lock = NULL; +#endif + return -1; +} + +static void +Decompressor_dealloc(Decompressor *self) +{ + lzma_end(&self->lzs); + Py_CLEAR(self->unused_data); +#ifdef WITH_THREAD + if (self->lock != NULL) + PyThread_free_lock(self->lock); +#endif + Py_TYPE(self)->tp_free((PyObject *)self); +} + +static PyMethodDef Decompressor_methods[] = { + _LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF + {"__getstate__", (PyCFunction)Decompressor_getstate, METH_NOARGS}, + {NULL} +}; + +PyDoc_STRVAR(Decompressor_check_doc, +"ID of the integrity check used by the input stream."); + +PyDoc_STRVAR(Decompressor_eof_doc, +"True if the end-of-stream marker has been reached."); + +PyDoc_STRVAR(Decompressor_unused_data_doc, +"Data found after the end of the compressed stream."); + +static PyMemberDef Decompressor_members[] = { + {"check", T_INT, offsetof(Decompressor, check), READONLY, + Decompressor_check_doc}, + {"eof", T_BOOL, offsetof(Decompressor, eof), READONLY, + Decompressor_eof_doc}, + {"unused_data", T_OBJECT_EX, offsetof(Decompressor, unused_data), READONLY, + Decompressor_unused_data_doc}, + {NULL} +}; + +static PyTypeObject Decompressor_type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_lzma.LZMADecompressor", /* tp_name */ + sizeof(Decompressor), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)Decompressor_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + _lzma_LZMADecompressor___init____doc__, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Decompressor_methods, /* tp_methods */ + Decompressor_members, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + _lzma_LZMADecompressor___init__, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ +}; + + +/* Module-level functions. */ + +/*[clinic input] +_lzma.is_check_supported + check_id: int + / + +Test whether the given integrity check is supported. + +Always returns True for CHECK_NONE and CHECK_CRC32. +[clinic start generated code]*/ + +static PyObject * +_lzma_is_check_supported_impl(PyModuleDef *module, int check_id) +/*[clinic end generated code: checksum=bb828e90e00ad96ed61f66719c2fca7fde637418]*/ +{ + return PyBool_FromLong(lzma_check_is_supported(check_id)); +} + + +/*[clinic input] +_lzma._encode_filter_properties + filter: lzma_filter(c_default="{LZMA_VLI_UNKNOWN, NULL}") + / + +Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict). + +The result does not include the filter ID itself, only the options. +[clinic start generated code]*/ + +static PyObject * +_lzma__encode_filter_properties_impl(PyModuleDef *module, lzma_filter filter) +/*[clinic end generated code: checksum=b5fe690acd6b61d1abfc32f522ada5bdcf9b13da]*/ +{ + lzma_ret lzret; + uint32_t encoded_size; + PyObject *result = NULL; + + lzret = lzma_properties_size(&encoded_size, &filter); + if (catch_lzma_error(lzret)) + goto error; + + result = PyBytes_FromStringAndSize(NULL, encoded_size); + if (result == NULL) + goto error; + + lzret = lzma_properties_encode( + &filter, (uint8_t *)PyBytes_AS_STRING(result)); + if (catch_lzma_error(lzret)) + goto error; + + return result; + +error: + Py_XDECREF(result); + return NULL; +} + + +/*[clinic input] +_lzma._decode_filter_properties + filter_id: lzma_vli + encoded_props: Py_buffer + / + +Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict). + +The result does not include the filter ID itself, only the options. +[clinic start generated code]*/ + +static PyObject * +_lzma__decode_filter_properties_impl(PyModuleDef *module, lzma_vli filter_id, Py_buffer *encoded_props) +/*[clinic end generated code: checksum=235f7f5345d48744dcd21f781dafbbf05a717538]*/ +{ + lzma_filter filter; + lzma_ret lzret; + PyObject *result = NULL; + filter.id = filter_id; + + lzret = lzma_properties_decode( + &filter, NULL, encoded_props->buf, encoded_props->len); + if (catch_lzma_error(lzret)) + return NULL; + + result = build_filter_spec(&filter); + + /* We use vanilla free() here instead of PyMem_Free() - filter.options was + allocated by lzma_properties_decode() using the default allocator. */ + free(filter.options); + return result; +} + + +/* Module initialization. */ + +static PyMethodDef module_methods[] = { + _LZMA_IS_CHECK_SUPPORTED_METHODDEF + _LZMA__ENCODE_FILTER_PROPERTIES_METHODDEF + _LZMA__DECODE_FILTER_PROPERTIES_METHODDEF + {NULL} +}; + +static PyModuleDef _lzmamodule = { + PyModuleDef_HEAD_INIT, + "_lzma", + NULL, + -1, + module_methods, + NULL, + NULL, + NULL, + NULL, +}; + +/* Some of our constants are more than 32 bits wide, so PyModule_AddIntConstant + would not work correctly on platforms with 32-bit longs. */ +static int +module_add_int_constant(PyObject *m, const char *name, PY_LONG_LONG value) +{ + PyObject *o = PyLong_FromLongLong(value); + if (o == NULL) + return -1; + if (PyModule_AddObject(m, name, o) == 0) + return 0; + Py_DECREF(o); + return -1; +} + +#define ADD_INT_PREFIX_MACRO(m, macro) \ + module_add_int_constant(m, #macro, LZMA_ ## macro) + +PyMODINIT_FUNC +PyInit__lzma(void) +{ + PyObject *m; + + empty_tuple = PyTuple_New(0); + if (empty_tuple == NULL) + return NULL; + + m = PyModule_Create(&_lzmamodule); + if (m == NULL) + return NULL; + + if (PyModule_AddIntMacro(m, FORMAT_AUTO) == -1 || + PyModule_AddIntMacro(m, FORMAT_XZ) == -1 || + PyModule_AddIntMacro(m, FORMAT_ALONE) == -1 || + PyModule_AddIntMacro(m, FORMAT_RAW) == -1 || + ADD_INT_PREFIX_MACRO(m, CHECK_NONE) == -1 || + ADD_INT_PREFIX_MACRO(m, CHECK_CRC32) == -1 || + ADD_INT_PREFIX_MACRO(m, CHECK_CRC64) == -1 || + ADD_INT_PREFIX_MACRO(m, CHECK_SHA256) == -1 || + ADD_INT_PREFIX_MACRO(m, CHECK_ID_MAX) == -1 || + ADD_INT_PREFIX_MACRO(m, CHECK_UNKNOWN) == -1 || + ADD_INT_PREFIX_MACRO(m, FILTER_LZMA1) == -1 || + ADD_INT_PREFIX_MACRO(m, FILTER_LZMA2) == -1 || + ADD_INT_PREFIX_MACRO(m, FILTER_DELTA) == -1 || + ADD_INT_PREFIX_MACRO(m, FILTER_X86) == -1 || + ADD_INT_PREFIX_MACRO(m, FILTER_IA64) == -1 || + ADD_INT_PREFIX_MACRO(m, FILTER_ARM) == -1 || + ADD_INT_PREFIX_MACRO(m, FILTER_ARMTHUMB) == -1 || + ADD_INT_PREFIX_MACRO(m, FILTER_SPARC) == -1 || + ADD_INT_PREFIX_MACRO(m, FILTER_POWERPC) == -1 || + ADD_INT_PREFIX_MACRO(m, MF_HC3) == -1 || + ADD_INT_PREFIX_MACRO(m, MF_HC4) == -1 || + ADD_INT_PREFIX_MACRO(m, MF_BT2) == -1 || + ADD_INT_PREFIX_MACRO(m, MF_BT3) == -1 || + ADD_INT_PREFIX_MACRO(m, MF_BT4) == -1 || + ADD_INT_PREFIX_MACRO(m, MODE_FAST) == -1 || + ADD_INT_PREFIX_MACRO(m, MODE_NORMAL) == -1 || + ADD_INT_PREFIX_MACRO(m, PRESET_DEFAULT) == -1 || + ADD_INT_PREFIX_MACRO(m, PRESET_EXTREME) == -1) + return NULL; + + Error = PyErr_NewExceptionWithDoc( + "_lzma.LZMAError", "Call to liblzma failed.", NULL, NULL); + if (Error == NULL) + return NULL; + Py_INCREF(Error); + if (PyModule_AddObject(m, "LZMAError", Error) == -1) + return NULL; + + if (PyType_Ready(&Compressor_type) == -1) + return NULL; + Py_INCREF(&Compressor_type); + if (PyModule_AddObject(m, "LZMACompressor", + (PyObject *)&Compressor_type) == -1) + return NULL; + + if (PyType_Ready(&Decompressor_type) == -1) + return NULL; + Py_INCREF(&Decompressor_type); + if (PyModule_AddObject(m, "LZMADecompressor", + (PyObject *)&Decompressor_type) == -1) + return NULL; + + return m; +} diff --git a/Modules/clinic/audioop.c.h b/Modules/clinic/audioop.c.h new file mode 100644 index 0000000..974fdb1 --- /dev/null +++ b/Modules/clinic/audioop.c.h @@ -0,0 +1,836 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +PyDoc_STRVAR(audioop_getsample__doc__, +"getsample(module, fragment, width, index)\n" +"Return the value of sample index from the fragment."); + +#define AUDIOOP_GETSAMPLE_METHODDEF \ + {"getsample", (PyCFunction)audioop_getsample, METH_VARARGS, audioop_getsample__doc__}, + +static PyObject * +audioop_getsample_impl(PyModuleDef *module, Py_buffer *fragment, int width, Py_ssize_t index); + +static PyObject * +audioop_getsample(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + Py_ssize_t index; + + if (!PyArg_ParseTuple(args, + "y*in:getsample", + &fragment, &width, &index)) + goto exit; + return_value = audioop_getsample_impl(module, &fragment, width, index); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_max__doc__, +"max(module, fragment, width)\n" +"Return the maximum of the absolute value of all samples in a fragment."); + +#define AUDIOOP_MAX_METHODDEF \ + {"max", (PyCFunction)audioop_max, METH_VARARGS, audioop_max__doc__}, + +static PyObject * +audioop_max_impl(PyModuleDef *module, Py_buffer *fragment, int width); + +static PyObject * +audioop_max(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*i:max", + &fragment, &width)) + goto exit; + return_value = audioop_max_impl(module, &fragment, width); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_minmax__doc__, +"minmax(module, fragment, width)\n" +"Return the minimum and maximum values of all samples in the sound fragment."); + +#define AUDIOOP_MINMAX_METHODDEF \ + {"minmax", (PyCFunction)audioop_minmax, METH_VARARGS, audioop_minmax__doc__}, + +static PyObject * +audioop_minmax_impl(PyModuleDef *module, Py_buffer *fragment, int width); + +static PyObject * +audioop_minmax(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*i:minmax", + &fragment, &width)) + goto exit; + return_value = audioop_minmax_impl(module, &fragment, width); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_avg__doc__, +"avg(module, fragment, width)\n" +"Return the average over all samples in the fragment."); + +#define AUDIOOP_AVG_METHODDEF \ + {"avg", (PyCFunction)audioop_avg, METH_VARARGS, audioop_avg__doc__}, + +static PyObject * +audioop_avg_impl(PyModuleDef *module, Py_buffer *fragment, int width); + +static PyObject * +audioop_avg(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*i:avg", + &fragment, &width)) + goto exit; + return_value = audioop_avg_impl(module, &fragment, width); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_rms__doc__, +"rms(module, fragment, width)\n" +"Return the root-mean-square of the fragment, i.e. sqrt(sum(S_i^2)/n)."); + +#define AUDIOOP_RMS_METHODDEF \ + {"rms", (PyCFunction)audioop_rms, METH_VARARGS, audioop_rms__doc__}, + +static PyObject * +audioop_rms_impl(PyModuleDef *module, Py_buffer *fragment, int width); + +static PyObject * +audioop_rms(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*i:rms", + &fragment, &width)) + goto exit; + return_value = audioop_rms_impl(module, &fragment, width); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_findfit__doc__, +"findfit(module, fragment, reference)\n" +"Try to match reference as well as possible to a portion of fragment."); + +#define AUDIOOP_FINDFIT_METHODDEF \ + {"findfit", (PyCFunction)audioop_findfit, METH_VARARGS, audioop_findfit__doc__}, + +static PyObject * +audioop_findfit_impl(PyModuleDef *module, Py_buffer *fragment, Py_buffer *reference); + +static PyObject * +audioop_findfit(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + Py_buffer reference = {NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "y*y*:findfit", + &fragment, &reference)) + goto exit; + return_value = audioop_findfit_impl(module, &fragment, &reference); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + /* Cleanup for reference */ + if (reference.obj) + PyBuffer_Release(&reference); + + return return_value; +} + +PyDoc_STRVAR(audioop_findfactor__doc__, +"findfactor(module, fragment, reference)\n" +"Return a factor F such that rms(add(fragment, mul(reference, -F))) is minimal."); + +#define AUDIOOP_FINDFACTOR_METHODDEF \ + {"findfactor", (PyCFunction)audioop_findfactor, METH_VARARGS, audioop_findfactor__doc__}, + +static PyObject * +audioop_findfactor_impl(PyModuleDef *module, Py_buffer *fragment, Py_buffer *reference); + +static PyObject * +audioop_findfactor(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + Py_buffer reference = {NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "y*y*:findfactor", + &fragment, &reference)) + goto exit; + return_value = audioop_findfactor_impl(module, &fragment, &reference); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + /* Cleanup for reference */ + if (reference.obj) + PyBuffer_Release(&reference); + + return return_value; +} + +PyDoc_STRVAR(audioop_findmax__doc__, +"findmax(module, fragment, length)\n" +"Search fragment for a slice of specified number of samples with maximum energy."); + +#define AUDIOOP_FINDMAX_METHODDEF \ + {"findmax", (PyCFunction)audioop_findmax, METH_VARARGS, audioop_findmax__doc__}, + +static PyObject * +audioop_findmax_impl(PyModuleDef *module, Py_buffer *fragment, Py_ssize_t length); + +static PyObject * +audioop_findmax(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + Py_ssize_t length; + + if (!PyArg_ParseTuple(args, + "y*n:findmax", + &fragment, &length)) + goto exit; + return_value = audioop_findmax_impl(module, &fragment, length); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_avgpp__doc__, +"avgpp(module, fragment, width)\n" +"Return the average peak-peak value over all samples in the fragment."); + +#define AUDIOOP_AVGPP_METHODDEF \ + {"avgpp", (PyCFunction)audioop_avgpp, METH_VARARGS, audioop_avgpp__doc__}, + +static PyObject * +audioop_avgpp_impl(PyModuleDef *module, Py_buffer *fragment, int width); + +static PyObject * +audioop_avgpp(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*i:avgpp", + &fragment, &width)) + goto exit; + return_value = audioop_avgpp_impl(module, &fragment, width); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_maxpp__doc__, +"maxpp(module, fragment, width)\n" +"Return the maximum peak-peak value in the sound fragment."); + +#define AUDIOOP_MAXPP_METHODDEF \ + {"maxpp", (PyCFunction)audioop_maxpp, METH_VARARGS, audioop_maxpp__doc__}, + +static PyObject * +audioop_maxpp_impl(PyModuleDef *module, Py_buffer *fragment, int width); + +static PyObject * +audioop_maxpp(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*i:maxpp", + &fragment, &width)) + goto exit; + return_value = audioop_maxpp_impl(module, &fragment, width); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_cross__doc__, +"cross(module, fragment, width)\n" +"Return the number of zero crossings in the fragment passed as an argument."); + +#define AUDIOOP_CROSS_METHODDEF \ + {"cross", (PyCFunction)audioop_cross, METH_VARARGS, audioop_cross__doc__}, + +static PyObject * +audioop_cross_impl(PyModuleDef *module, Py_buffer *fragment, int width); + +static PyObject * +audioop_cross(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*i:cross", + &fragment, &width)) + goto exit; + return_value = audioop_cross_impl(module, &fragment, width); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_mul__doc__, +"mul(module, fragment, width, factor)\n" +"Return a fragment that has all samples in the original fragment multiplied by the floating-point value factor."); + +#define AUDIOOP_MUL_METHODDEF \ + {"mul", (PyCFunction)audioop_mul, METH_VARARGS, audioop_mul__doc__}, + +static PyObject * +audioop_mul_impl(PyModuleDef *module, Py_buffer *fragment, int width, double factor); + +static PyObject * +audioop_mul(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + double factor; + + if (!PyArg_ParseTuple(args, + "y*id:mul", + &fragment, &width, &factor)) + goto exit; + return_value = audioop_mul_impl(module, &fragment, width, factor); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_tomono__doc__, +"tomono(module, fragment, width, lfactor, rfactor)\n" +"Convert a stereo fragment to a mono fragment."); + +#define AUDIOOP_TOMONO_METHODDEF \ + {"tomono", (PyCFunction)audioop_tomono, METH_VARARGS, audioop_tomono__doc__}, + +static PyObject * +audioop_tomono_impl(PyModuleDef *module, Py_buffer *fragment, int width, double lfactor, double rfactor); + +static PyObject * +audioop_tomono(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + double lfactor; + double rfactor; + + if (!PyArg_ParseTuple(args, + "y*idd:tomono", + &fragment, &width, &lfactor, &rfactor)) + goto exit; + return_value = audioop_tomono_impl(module, &fragment, width, lfactor, rfactor); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_tostereo__doc__, +"tostereo(module, fragment, width, lfactor, rfactor)\n" +"Generate a stereo fragment from a mono fragment."); + +#define AUDIOOP_TOSTEREO_METHODDEF \ + {"tostereo", (PyCFunction)audioop_tostereo, METH_VARARGS, audioop_tostereo__doc__}, + +static PyObject * +audioop_tostereo_impl(PyModuleDef *module, Py_buffer *fragment, int width, double lfactor, double rfactor); + +static PyObject * +audioop_tostereo(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + double lfactor; + double rfactor; + + if (!PyArg_ParseTuple(args, + "y*idd:tostereo", + &fragment, &width, &lfactor, &rfactor)) + goto exit; + return_value = audioop_tostereo_impl(module, &fragment, width, lfactor, rfactor); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_add__doc__, +"add(module, fragment1, fragment2, width)\n" +"Return a fragment which is the addition of the two samples passed as parameters."); + +#define AUDIOOP_ADD_METHODDEF \ + {"add", (PyCFunction)audioop_add, METH_VARARGS, audioop_add__doc__}, + +static PyObject * +audioop_add_impl(PyModuleDef *module, Py_buffer *fragment1, Py_buffer *fragment2, int width); + +static PyObject * +audioop_add(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment1 = {NULL, NULL}; + Py_buffer fragment2 = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*y*i:add", + &fragment1, &fragment2, &width)) + goto exit; + return_value = audioop_add_impl(module, &fragment1, &fragment2, width); + +exit: + /* Cleanup for fragment1 */ + if (fragment1.obj) + PyBuffer_Release(&fragment1); + /* Cleanup for fragment2 */ + if (fragment2.obj) + PyBuffer_Release(&fragment2); + + return return_value; +} + +PyDoc_STRVAR(audioop_bias__doc__, +"bias(module, fragment, width, bias)\n" +"Return a fragment that is the original fragment with a bias added to each sample."); + +#define AUDIOOP_BIAS_METHODDEF \ + {"bias", (PyCFunction)audioop_bias, METH_VARARGS, audioop_bias__doc__}, + +static PyObject * +audioop_bias_impl(PyModuleDef *module, Py_buffer *fragment, int width, int bias); + +static PyObject * +audioop_bias(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + int bias; + + if (!PyArg_ParseTuple(args, + "y*ii:bias", + &fragment, &width, &bias)) + goto exit; + return_value = audioop_bias_impl(module, &fragment, width, bias); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_reverse__doc__, +"reverse(module, fragment, width)\n" +"Reverse the samples in a fragment and returns the modified fragment."); + +#define AUDIOOP_REVERSE_METHODDEF \ + {"reverse", (PyCFunction)audioop_reverse, METH_VARARGS, audioop_reverse__doc__}, + +static PyObject * +audioop_reverse_impl(PyModuleDef *module, Py_buffer *fragment, int width); + +static PyObject * +audioop_reverse(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*i:reverse", + &fragment, &width)) + goto exit; + return_value = audioop_reverse_impl(module, &fragment, width); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_byteswap__doc__, +"byteswap(module, fragment, width)\n" +"Convert big-endian samples to little-endian and vice versa."); + +#define AUDIOOP_BYTESWAP_METHODDEF \ + {"byteswap", (PyCFunction)audioop_byteswap, METH_VARARGS, audioop_byteswap__doc__}, + +static PyObject * +audioop_byteswap_impl(PyModuleDef *module, Py_buffer *fragment, int width); + +static PyObject * +audioop_byteswap(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*i:byteswap", + &fragment, &width)) + goto exit; + return_value = audioop_byteswap_impl(module, &fragment, width); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_lin2lin__doc__, +"lin2lin(module, fragment, width, newwidth)\n" +"Convert samples between 1-, 2-, 3- and 4-byte formats."); + +#define AUDIOOP_LIN2LIN_METHODDEF \ + {"lin2lin", (PyCFunction)audioop_lin2lin, METH_VARARGS, audioop_lin2lin__doc__}, + +static PyObject * +audioop_lin2lin_impl(PyModuleDef *module, Py_buffer *fragment, int width, int newwidth); + +static PyObject * +audioop_lin2lin(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + int newwidth; + + if (!PyArg_ParseTuple(args, + "y*ii:lin2lin", + &fragment, &width, &newwidth)) + goto exit; + return_value = audioop_lin2lin_impl(module, &fragment, width, newwidth); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_ratecv__doc__, +"ratecv(module, fragment, width, nchannels, inrate, outrate, state, weightA=1, weightB=0)\n" +"Convert the frame rate of the input fragment."); + +#define AUDIOOP_RATECV_METHODDEF \ + {"ratecv", (PyCFunction)audioop_ratecv, METH_VARARGS, audioop_ratecv__doc__}, + +static PyObject * +audioop_ratecv_impl(PyModuleDef *module, Py_buffer *fragment, int width, int nchannels, int inrate, int outrate, PyObject *state, int weightA, int weightB); + +static PyObject * +audioop_ratecv(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + int nchannels; + int inrate; + int outrate; + PyObject *state; + int weightA = 1; + int weightB = 0; + + if (!PyArg_ParseTuple(args, + "y*iiiiO|ii:ratecv", + &fragment, &width, &nchannels, &inrate, &outrate, &state, &weightA, &weightB)) + goto exit; + return_value = audioop_ratecv_impl(module, &fragment, width, nchannels, inrate, outrate, state, weightA, weightB); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_lin2ulaw__doc__, +"lin2ulaw(module, fragment, width)\n" +"Convert samples in the audio fragment to u-LAW encoding."); + +#define AUDIOOP_LIN2ULAW_METHODDEF \ + {"lin2ulaw", (PyCFunction)audioop_lin2ulaw, METH_VARARGS, audioop_lin2ulaw__doc__}, + +static PyObject * +audioop_lin2ulaw_impl(PyModuleDef *module, Py_buffer *fragment, int width); + +static PyObject * +audioop_lin2ulaw(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*i:lin2ulaw", + &fragment, &width)) + goto exit; + return_value = audioop_lin2ulaw_impl(module, &fragment, width); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_ulaw2lin__doc__, +"ulaw2lin(module, fragment, width)\n" +"Convert sound fragments in u-LAW encoding to linearly encoded sound fragments."); + +#define AUDIOOP_ULAW2LIN_METHODDEF \ + {"ulaw2lin", (PyCFunction)audioop_ulaw2lin, METH_VARARGS, audioop_ulaw2lin__doc__}, + +static PyObject * +audioop_ulaw2lin_impl(PyModuleDef *module, Py_buffer *fragment, int width); + +static PyObject * +audioop_ulaw2lin(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*i:ulaw2lin", + &fragment, &width)) + goto exit; + return_value = audioop_ulaw2lin_impl(module, &fragment, width); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_lin2alaw__doc__, +"lin2alaw(module, fragment, width)\n" +"Convert samples in the audio fragment to a-LAW encoding."); + +#define AUDIOOP_LIN2ALAW_METHODDEF \ + {"lin2alaw", (PyCFunction)audioop_lin2alaw, METH_VARARGS, audioop_lin2alaw__doc__}, + +static PyObject * +audioop_lin2alaw_impl(PyModuleDef *module, Py_buffer *fragment, int width); + +static PyObject * +audioop_lin2alaw(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*i:lin2alaw", + &fragment, &width)) + goto exit; + return_value = audioop_lin2alaw_impl(module, &fragment, width); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_alaw2lin__doc__, +"alaw2lin(module, fragment, width)\n" +"Convert sound fragments in a-LAW encoding to linearly encoded sound fragments."); + +#define AUDIOOP_ALAW2LIN_METHODDEF \ + {"alaw2lin", (PyCFunction)audioop_alaw2lin, METH_VARARGS, audioop_alaw2lin__doc__}, + +static PyObject * +audioop_alaw2lin_impl(PyModuleDef *module, Py_buffer *fragment, int width); + +static PyObject * +audioop_alaw2lin(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + + if (!PyArg_ParseTuple(args, + "y*i:alaw2lin", + &fragment, &width)) + goto exit; + return_value = audioop_alaw2lin_impl(module, &fragment, width); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_lin2adpcm__doc__, +"lin2adpcm(module, fragment, width, state)\n" +"Convert samples to 4 bit Intel/DVI ADPCM encoding."); + +#define AUDIOOP_LIN2ADPCM_METHODDEF \ + {"lin2adpcm", (PyCFunction)audioop_lin2adpcm, METH_VARARGS, audioop_lin2adpcm__doc__}, + +static PyObject * +audioop_lin2adpcm_impl(PyModuleDef *module, Py_buffer *fragment, int width, PyObject *state); + +static PyObject * +audioop_lin2adpcm(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + PyObject *state; + + if (!PyArg_ParseTuple(args, + "y*iO:lin2adpcm", + &fragment, &width, &state)) + goto exit; + return_value = audioop_lin2adpcm_impl(module, &fragment, width, state); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} + +PyDoc_STRVAR(audioop_adpcm2lin__doc__, +"adpcm2lin(module, fragment, width, state)\n" +"Decode an Intel/DVI ADPCM coded fragment to a linear fragment."); + +#define AUDIOOP_ADPCM2LIN_METHODDEF \ + {"adpcm2lin", (PyCFunction)audioop_adpcm2lin, METH_VARARGS, audioop_adpcm2lin__doc__}, + +static PyObject * +audioop_adpcm2lin_impl(PyModuleDef *module, Py_buffer *fragment, int width, PyObject *state); + +static PyObject * +audioop_adpcm2lin(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer fragment = {NULL, NULL}; + int width; + PyObject *state; + + if (!PyArg_ParseTuple(args, + "y*iO:adpcm2lin", + &fragment, &width, &state)) + goto exit; + return_value = audioop_adpcm2lin_impl(module, &fragment, width, state); + +exit: + /* Cleanup for fragment */ + if (fragment.obj) + PyBuffer_Release(&fragment); + + return return_value; +} +/*[clinic end generated code: checksum=0d9fa2c5719e996b169f808350016cd622799562]*/ diff --git a/Modules/clinic/binascii.c.h b/Modules/clinic/binascii.c.h new file mode 100644 index 0000000..3e4a468 --- /dev/null +++ b/Modules/clinic/binascii.c.h @@ -0,0 +1,430 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +PyDoc_STRVAR(binascii_a2b_uu__doc__, +"a2b_uu(module, data)\n" +"Decode a line of uuencoded data."); + +#define BINASCII_A2B_UU_METHODDEF \ + {"a2b_uu", (PyCFunction)binascii_a2b_uu, METH_VARARGS, binascii_a2b_uu__doc__}, + +static PyObject * +binascii_a2b_uu_impl(PyModuleDef *module, Py_buffer *data); + +static PyObject * +binascii_a2b_uu(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data; + + if (!PyArg_ParseTuple(args, + "O&:a2b_uu", + ascii_buffer_converter, &data)) + goto exit; + return_value = binascii_a2b_uu_impl(module, &data); + +exit: + return return_value; +} + +PyDoc_STRVAR(binascii_b2a_uu__doc__, +"b2a_uu(module, data)\n" +"Uuencode line of data."); + +#define BINASCII_B2A_UU_METHODDEF \ + {"b2a_uu", (PyCFunction)binascii_b2a_uu, METH_VARARGS, binascii_b2a_uu__doc__}, + +static PyObject * +binascii_b2a_uu_impl(PyModuleDef *module, Py_buffer *data); + +static PyObject * +binascii_b2a_uu(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "y*:b2a_uu", + &data)) + goto exit; + return_value = binascii_b2a_uu_impl(module, &data); + +exit: + /* Cleanup for data */ + if (data.obj) + PyBuffer_Release(&data); + + return return_value; +} + +PyDoc_STRVAR(binascii_a2b_base64__doc__, +"a2b_base64(module, data)\n" +"Decode a line of base64 data."); + +#define BINASCII_A2B_BASE64_METHODDEF \ + {"a2b_base64", (PyCFunction)binascii_a2b_base64, METH_VARARGS, binascii_a2b_base64__doc__}, + +static PyObject * +binascii_a2b_base64_impl(PyModuleDef *module, Py_buffer *data); + +static PyObject * +binascii_a2b_base64(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data; + + if (!PyArg_ParseTuple(args, + "O&:a2b_base64", + ascii_buffer_converter, &data)) + goto exit; + return_value = binascii_a2b_base64_impl(module, &data); + +exit: + return return_value; +} + +PyDoc_STRVAR(binascii_b2a_base64__doc__, +"b2a_base64(module, data)\n" +"Base64-code line of data."); + +#define BINASCII_B2A_BASE64_METHODDEF \ + {"b2a_base64", (PyCFunction)binascii_b2a_base64, METH_VARARGS, binascii_b2a_base64__doc__}, + +static PyObject * +binascii_b2a_base64_impl(PyModuleDef *module, Py_buffer *data); + +static PyObject * +binascii_b2a_base64(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "y*:b2a_base64", + &data)) + goto exit; + return_value = binascii_b2a_base64_impl(module, &data); + +exit: + /* Cleanup for data */ + if (data.obj) + PyBuffer_Release(&data); + + return return_value; +} + +PyDoc_STRVAR(binascii_a2b_hqx__doc__, +"a2b_hqx(module, data)\n" +"Decode .hqx coding."); + +#define BINASCII_A2B_HQX_METHODDEF \ + {"a2b_hqx", (PyCFunction)binascii_a2b_hqx, METH_VARARGS, binascii_a2b_hqx__doc__}, + +static PyObject * +binascii_a2b_hqx_impl(PyModuleDef *module, Py_buffer *data); + +static PyObject * +binascii_a2b_hqx(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data; + + if (!PyArg_ParseTuple(args, + "O&:a2b_hqx", + ascii_buffer_converter, &data)) + goto exit; + return_value = binascii_a2b_hqx_impl(module, &data); + +exit: + return return_value; +} + +PyDoc_STRVAR(binascii_rlecode_hqx__doc__, +"rlecode_hqx(module, data)\n" +"Binhex RLE-code binary data."); + +#define BINASCII_RLECODE_HQX_METHODDEF \ + {"rlecode_hqx", (PyCFunction)binascii_rlecode_hqx, METH_VARARGS, binascii_rlecode_hqx__doc__}, + +static PyObject * +binascii_rlecode_hqx_impl(PyModuleDef *module, Py_buffer *data); + +static PyObject * +binascii_rlecode_hqx(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "y*:rlecode_hqx", + &data)) + goto exit; + return_value = binascii_rlecode_hqx_impl(module, &data); + +exit: + /* Cleanup for data */ + if (data.obj) + PyBuffer_Release(&data); + + return return_value; +} + +PyDoc_STRVAR(binascii_b2a_hqx__doc__, +"b2a_hqx(module, data)\n" +"Encode .hqx data."); + +#define BINASCII_B2A_HQX_METHODDEF \ + {"b2a_hqx", (PyCFunction)binascii_b2a_hqx, METH_VARARGS, binascii_b2a_hqx__doc__}, + +static PyObject * +binascii_b2a_hqx_impl(PyModuleDef *module, Py_buffer *data); + +static PyObject * +binascii_b2a_hqx(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "y*:b2a_hqx", + &data)) + goto exit; + return_value = binascii_b2a_hqx_impl(module, &data); + +exit: + /* Cleanup for data */ + if (data.obj) + PyBuffer_Release(&data); + + return return_value; +} + +PyDoc_STRVAR(binascii_rledecode_hqx__doc__, +"rledecode_hqx(module, data)\n" +"Decode hexbin RLE-coded string."); + +#define BINASCII_RLEDECODE_HQX_METHODDEF \ + {"rledecode_hqx", (PyCFunction)binascii_rledecode_hqx, METH_VARARGS, binascii_rledecode_hqx__doc__}, + +static PyObject * +binascii_rledecode_hqx_impl(PyModuleDef *module, Py_buffer *data); + +static PyObject * +binascii_rledecode_hqx(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "y*:rledecode_hqx", + &data)) + goto exit; + return_value = binascii_rledecode_hqx_impl(module, &data); + +exit: + /* Cleanup for data */ + if (data.obj) + PyBuffer_Release(&data); + + return return_value; +} + +PyDoc_STRVAR(binascii_crc_hqx__doc__, +"crc_hqx(module, data, crc)\n" +"Compute hqx CRC incrementally."); + +#define BINASCII_CRC_HQX_METHODDEF \ + {"crc_hqx", (PyCFunction)binascii_crc_hqx, METH_VARARGS, binascii_crc_hqx__doc__}, + +static int +binascii_crc_hqx_impl(PyModuleDef *module, Py_buffer *data, int crc); + +static PyObject * +binascii_crc_hqx(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL}; + int crc; + int _return_value; + + if (!PyArg_ParseTuple(args, + "y*i:crc_hqx", + &data, &crc)) + goto exit; + _return_value = binascii_crc_hqx_impl(module, &data, crc); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return_value = PyLong_FromLong((long)_return_value); + +exit: + /* Cleanup for data */ + if (data.obj) + PyBuffer_Release(&data); + + return return_value; +} + +PyDoc_STRVAR(binascii_crc32__doc__, +"crc32(module, data, crc=0)\n" +"Compute CRC-32 incrementally."); + +#define BINASCII_CRC32_METHODDEF \ + {"crc32", (PyCFunction)binascii_crc32, METH_VARARGS, binascii_crc32__doc__}, + +static unsigned int +binascii_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int crc); + +static PyObject * +binascii_crc32(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL}; + unsigned int crc = 0; + unsigned int _return_value; + + if (!PyArg_ParseTuple(args, + "y*|I:crc32", + &data, &crc)) + goto exit; + _return_value = binascii_crc32_impl(module, &data, crc); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return_value = PyLong_FromUnsignedLong((unsigned long)_return_value); + +exit: + /* Cleanup for data */ + if (data.obj) + PyBuffer_Release(&data); + + return return_value; +} + +PyDoc_STRVAR(binascii_b2a_hex__doc__, +"b2a_hex(module, data)\n" +"Hexadecimal representation of binary data.\n" +"\n" +"The return value is a bytes object. This function is also\n" +"available as \"hexlify()\"."); + +#define BINASCII_B2A_HEX_METHODDEF \ + {"b2a_hex", (PyCFunction)binascii_b2a_hex, METH_VARARGS, binascii_b2a_hex__doc__}, + +static PyObject * +binascii_b2a_hex_impl(PyModuleDef *module, Py_buffer *data); + +static PyObject * +binascii_b2a_hex(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "y*:b2a_hex", + &data)) + goto exit; + return_value = binascii_b2a_hex_impl(module, &data); + +exit: + /* Cleanup for data */ + if (data.obj) + PyBuffer_Release(&data); + + return return_value; +} + +PyDoc_STRVAR(binascii_a2b_hex__doc__, +"a2b_hex(module, hexstr)\n" +"Binary data of hexadecimal representation.\n" +"\n" +"hexstr must contain an even number of hex digits (upper or lower case).\n" +"This function is also available as \"unhexlify()\"."); + +#define BINASCII_A2B_HEX_METHODDEF \ + {"a2b_hex", (PyCFunction)binascii_a2b_hex, METH_VARARGS, binascii_a2b_hex__doc__}, + +static PyObject * +binascii_a2b_hex_impl(PyModuleDef *module, Py_buffer *hexstr); + +static PyObject * +binascii_a2b_hex(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer hexstr; + + if (!PyArg_ParseTuple(args, + "O&:a2b_hex", + ascii_buffer_converter, &hexstr)) + goto exit; + return_value = binascii_a2b_hex_impl(module, &hexstr); + +exit: + return return_value; +} + +PyDoc_STRVAR(binascii_a2b_qp__doc__, +"a2b_qp(module, data, header=False)\n" +"Decode a string of qp-encoded data."); + +#define BINASCII_A2B_QP_METHODDEF \ + {"a2b_qp", (PyCFunction)binascii_a2b_qp, METH_VARARGS|METH_KEYWORDS, binascii_a2b_qp__doc__}, + +static PyObject * +binascii_a2b_qp_impl(PyModuleDef *module, Py_buffer *data, int header); + +static PyObject * +binascii_a2b_qp(PyModuleDef *module, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"data", "header", NULL}; + Py_buffer data; + int header = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O&|i:a2b_qp", _keywords, + ascii_buffer_converter, &data, &header)) + goto exit; + return_value = binascii_a2b_qp_impl(module, &data, header); + +exit: + return return_value; +} + +PyDoc_STRVAR(binascii_b2a_qp__doc__, +"b2a_qp(module, data, quotetabs=False, istext=True, header=False)\n" +"Encode a string using quoted-printable encoding.\n" +"\n" +"On encoding, when istext is set, newlines are not encoded, and white\n" +"space at end of lines is. When istext is not set, \\r and \\n (CR/LF)\n" +"are both encoded. When quotetabs is set, space and tabs are encoded."); + +#define BINASCII_B2A_QP_METHODDEF \ + {"b2a_qp", (PyCFunction)binascii_b2a_qp, METH_VARARGS|METH_KEYWORDS, binascii_b2a_qp__doc__}, + +static PyObject * +binascii_b2a_qp_impl(PyModuleDef *module, Py_buffer *data, int quotetabs, int istext, int header); + +static PyObject * +binascii_b2a_qp(PyModuleDef *module, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"data", "quotetabs", "istext", "header", NULL}; + Py_buffer data = {NULL, NULL}; + int quotetabs = 0; + int istext = 1; + int header = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "y*|iii:b2a_qp", _keywords, + &data, "etabs, &istext, &header)) + goto exit; + return_value = binascii_b2a_qp_impl(module, &data, quotetabs, istext, header); + +exit: + /* Cleanup for data */ + if (data.obj) + PyBuffer_Release(&data); + + return return_value; +} +/*[clinic end generated code: checksum=abe48ca8020fa3ec25e13bd9fa7414f6b3ee2946]*/ diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c index ff13e0b..ca1620f 100644 --- a/Modules/unicodedata.c +++ b/Modules/unicodedata.c @@ -19,7 +19,7 @@ /*[clinic input] module unicodedata -class unicodedata.UCD +class unicodedata.UCD 'PreviousDBVersion *' '&UCD_Type' [clinic start generated code]*/ /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ @@ -140,10 +140,10 @@ PyDoc_STRVAR(unicodedata_UCD_decimal__doc__, {"decimal", (PyCFunction)unicodedata_UCD_decimal, METH_VARARGS, unicodedata_UCD_decimal__doc__}, static PyObject * -unicodedata_UCD_decimal_impl(PyObject *self, PyUnicodeObject *unichr, PyObject *default_value); +unicodedata_UCD_decimal_impl(PreviousDBVersion *self, PyUnicodeObject *unichr, PyObject *default_value); static PyObject * -unicodedata_UCD_decimal(PyObject *self, PyObject *args) +unicodedata_UCD_decimal(PreviousDBVersion *self, PyObject *args) { PyObject *return_value = NULL; PyUnicodeObject *unichr; @@ -160,15 +160,14 @@ exit: } static PyObject * -unicodedata_UCD_decimal_impl(PyObject *self, PyUnicodeObject *unichr, PyObject *default_value) -/*[clinic end generated code: checksum=01826b179d497d8fd3842c56679ecbd4faddaa95]*/ +unicodedata_UCD_decimal_impl(PreviousDBVersion *self, PyUnicodeObject *unichr, PyObject *default_value) +/*[clinic end generated code: checksum=e1371a1a016e19fdd3cd2c1af1d1832df095f50b]*/ { - PyUnicodeObject *v = (PyUnicodeObject *)unichr; int have_old = 0; long rc; Py_UCS4 c; - c = getuchar(v); + c = getuchar(unichr); if (c == (Py_UCS4)-1) return NULL; diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index 24f1e39..78ee10b 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -83,8 +83,8 @@ zlib_error(z_stream zst, int err, char *msg) /*[clinic input] module zlib -class zlib.Compress -class zlib.Decompress +class zlib.Compress "compobject *" "&Comptype" +class zlib.Decompress "compobject *" "&Decomptype" [clinic start generated code]*/ /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ @@ -748,8 +748,6 @@ save_unconsumed_input(compobject *self, int err) zlib.Decompress.decompress - self: self(type="compobject *") - data: Py_buffer The binary data to decompress. max_length: uint = 0 @@ -1030,8 +1028,6 @@ PyZlib_flush(compobject *self, PyObject *args) /*[clinic input] zlib.Compress.copy - self: self(type="compobject *") - Return a copy of the compression object. [clinic start generated code]*/ diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 8eff999..2332e3f 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -70,7 +70,7 @@ to the combined-table form. #include "stringlib/eq.h" /*[clinic input] -class dict +class dict "PyDictObject *" "&PyDict_Type" [clinic start generated code]*/ /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ @@ -1694,7 +1694,6 @@ dict_items(PyDictObject *mp) /*[clinic input] @classmethod dict.fromkeys - iterable: object value: object=None / @@ -2217,10 +2216,10 @@ PyDoc_STRVAR(dict___contains____doc__, {"__contains__", (PyCFunction)dict___contains__, METH_O|METH_COEXIST, dict___contains____doc__}, static PyObject * -dict___contains__(PyObject *self, PyObject *key) -/*[clinic end generated code: checksum=c4f85a39baac4776c4275ad5f072f7732c5f0806]*/ +dict___contains__(PyDictObject *self, PyObject *key) +/*[clinic end generated code: checksum=744ca54369dda9815a596304087f1b37fafa5960]*/ { - register PyDictObject *mp = (PyDictObject *)self; + register PyDictObject *mp = self; Py_hash_t hash; PyDictKeyEntry *ep; PyObject **value_addr; diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 9acd37f..919b733 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -48,7 +48,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #endif /*[clinic input] -class str +class str "PyUnicodeObject *" "&PyUnicode_Type" [clinic start generated code]*/ /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 7f1b059..0dec258 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -300,6 +300,10 @@ class CRenderData: # Should be full lines with \n eol characters. self.initializers = [] + # The C statements needed to dynamically modify the values + # parsed by the parse call, before calling the impl. + self.modifications = [] + # The entries for the "keywords" array for PyArg_ParseTuple. # Should be individual strings representing the names. self.keywords = [] @@ -541,6 +545,7 @@ __________________________________________________ parser_definition_impl_call + {modifications} {return_value} = {c_basename}_impl({impl_arguments}); __________________________________________________ @@ -575,14 +580,14 @@ __________________________________________________ parser_definition_no_positional - if (!_PyArg_NoPositional("{name}", args)) + if ({self_type_check}!_PyArg_NoPositional("{name}", args)) goto exit; __________________________________________________ parser_definition_no_keywords - if (!_PyArg_NoKeywords("{name}", kwargs)) + if ({self_type_check}!_PyArg_NoKeywords("{name}", kwargs)) goto exit; __________________________________________________ @@ -691,7 +696,7 @@ __________________________________________________ impl_definition = templates['impl_definition'] impl_prototype = parser_prototype = parser_definition = None - parser_body_fields = None + parser_body_fields = () def parser_body(prototype, *fields): nonlocal parser_body_fields add, output = text_accumulator() @@ -1025,6 +1030,7 @@ __________________________________________________ template_dict['docstring'] = self.docstring_for_c_string(f) + template_dict['self_name'] = template_dict['self_type'] = template_dict['self_type_check'] = '' f_self.converter.set_template_dict(template_dict) f.return_converter.render(f, data) @@ -1032,6 +1038,7 @@ __________________________________________________ template_dict['declarations'] = "\n".join(data.declarations) template_dict['initializers'] = "\n\n".join(data.initializers) + template_dict['modifications'] = '\n\n'.join(data.modifications) template_dict['keywords'] = '"' + '", "'.join(data.keywords) + '"' template_dict['format_units'] = ''.join(data.format_units) template_dict['parse_arguments'] = ', '.join(data.parse_arguments) @@ -1060,6 +1067,7 @@ __________________________________________________ declarations=template_dict['declarations'], return_conversion=template_dict['return_conversion'], initializers=template_dict['initializers'], + modifications=template_dict['modifications'], cleanup=template_dict['cleanup'], ) @@ -1356,8 +1364,14 @@ class Destination: fail("Too many arguments for destination " + name + " new " + type) if type =='file': d = {} - d['filename'] = filename = clinic.filename - d['basename'], d['extension'] = os.path.splitext(filename) + filename = clinic.filename + d['path'] = filename + dirname, basename = os.path.split(filename) + if not dirname: + dirname = '.' + d['dirname'] = dirname + d['basename'] = basename + d['basename_root'], d['basename_extension'] = os.path.splitext(filename) self.filename = args[0].format_map(d) if type == 'two-pass': self.id = None @@ -1476,7 +1490,7 @@ impl_definition block self.add_destination("buffer", "buffer") self.add_destination("two-pass", "two-pass") if filename: - self.add_destination("file", "file", "{basename}.clinic{extension}") + self.add_destination("file", "file", "{dirname}/clinic/{basename}.h") d = self.destinations.get self.field_destinations = collections.OrderedDict(( @@ -1572,6 +1586,14 @@ impl_definition block if destination.type == 'file': try: + dirname = os.path.dirname(destination.filename) + try: + os.makedirs(dirname) + except FileExistsError: + if not os.path.isdir(dirname): + fail("Can't write to destination {}, " + "can't make directory {}!".format( + destination.filename, dirname)) with open(destination.filename, "rt") as f: parser_2 = BlockParser(f.read(), language=self.language) blocks = list(parser_2) @@ -1696,10 +1718,12 @@ class Module: return "" class Class: - def __init__(self, name, module=None, cls=None): + def __init__(self, name, module=None, cls=None, typedef=None, type_object=None): self.name = name self.module = module self.cls = cls + self.typedef = typedef + self.type_object = type_object self.parent = cls or module self.classes = collections.OrderedDict() @@ -1980,6 +2004,7 @@ class CConverter(metaclass=CConverterAutoRegister): # Should we show this parameter in the generated # __text_signature__? This is *almost* always True. + # (It's only False for __new__, __init__, and METH_STATIC functions.) show_in_signature = True # Overrides the name used in a text signature. @@ -2050,6 +2075,11 @@ class CConverter(metaclass=CConverterAutoRegister): if initializers: data.initializers.append('/* initializers for ' + name + ' */\n' + initializers.rstrip()) + # modifications + modifications = self.modify() + if modifications: + data.modifications.append('/* modifications for ' + name + ' */\n' + modifications.rstrip()) + # keywords data.keywords.append(original_name) @@ -2152,6 +2182,14 @@ class CConverter(metaclass=CConverterAutoRegister): """ return "" + def modify(self): + """ + The C statements required to modify this variable after parsing. + Returns a string containing this code indented at column 0. + If no initialization is necessary, returns an empty string. + """ + return "" + def cleanup(self): """ The C statements required to clean up after this variable. @@ -2463,6 +2501,12 @@ def correct_name_for_self(f): return "PyTypeObject *", "type" raise RuntimeError("Unhandled type of function f: " + repr(f.kind)) +def required_type_for_self_for_parser(f): + type, _ = correct_name_for_self(f) + if f.kind in (METHOD_INIT, METHOD_NEW, STATIC_METHOD, CLASS_METHOD): + return type + return None + class self_converter(CConverter): """ @@ -2526,12 +2570,7 @@ class self_converter(CConverter): @property def parser_type(self): - kind = self.function.kind - if kind == METHOD_NEW: - return "PyTypeObject *" - if kind == METHOD_INIT: - return "PyObject *" - return self.type + return required_type_for_self_for_parser(self.function) or self.type def render(self, parameter, data): """ @@ -2554,6 +2593,7 @@ class self_converter(CConverter): def set_template_dict(self, template_dict): template_dict['self_name'] = self.name template_dict['self_type'] = self.parser_type + template_dict['self_type_check'] = '({self_name} == {self_type_object}) &&\n ' @@ -2808,6 +2848,7 @@ class DSLParser: self.keyword_only = False self.group = 0 self.parameter_state = self.ps_start + self.seen_positional_with_default = False self.indent = IndentStack() self.kind = CALLABLE self.coexist = False @@ -2825,11 +2866,15 @@ class DSLParser: module, cls = self.clinic._module_and_class(fields) if cls: fail("Can't nest a module inside a class!") + + if name in module.classes: + fail("Already defined module " + repr(name) + "!") + m = Module(name, module) module.modules[name] = m self.block.signatures.append(m) - def directive_class(self, name): + def directive_class(self, name, typedef, type_object): fields = name.split('.') in_classes = False parent = self @@ -2837,11 +2882,12 @@ class DSLParser: so_far = [] module, cls = self.clinic._module_and_class(fields) - c = Class(name, module, cls) - if cls: - cls.classes[name] = c - else: - module.classes[name] = c + parent = cls or module + if name in parent.classes: + fail("Already defined class " + repr(name) + "!") + + c = Class(name, module, cls, typedef, type_object) + parent.classes[name] = c self.block.signatures.append(c) def directive_set(self, name, value): @@ -3035,6 +3081,8 @@ class DSLParser: else: existing_function = None if not existing_function: + print("class", cls, "module", module, "exsiting", existing) + print("cls. functions", cls.functions) fail("Couldn't find existing function " + repr(existing) + "!") fields = [x.strip() for x in full_name.split('.')] @@ -3113,8 +3161,11 @@ class DSLParser: self.block.signatures.append(self.function) # insert a self converter automatically - _, name = correct_name_for_self(self.function) - sc = self.function.self_converter = self_converter(name, self.function) + type, name = correct_name_for_self(self.function) + kwargs = {} + if cls and type == "PyObject *": + kwargs['type'] = cls.typedef + sc = self.function.self_converter = self_converter(name, self.function, **kwargs) p_self = Parameter(sc.name, inspect.Parameter.POSITIONAL_ONLY, function=self.function, converter=sc) self.function.parameters[sc.name] = p_self @@ -3175,18 +3226,21 @@ class DSLParser: # "parameter_state". (Previously the code was a miasma of ifs and # separate boolean state variables.) The states are: # - # [ [ a, b, ] c, ] d, e, f, [ g, h, [ i ] ] / <- line - # 01 2 3 4 5 6 <- state transitions + # [ [ a, b, ] c, ] d, e, f=3, [ g, h, [ i ] ] / <- line + # 01 2 3 4 5 6 7 <- state transitions # # 0: ps_start. before we've seen anything. legal transitions are to 1 or 3. # 1: ps_left_square_before. left square brackets before required parameters. # 2: ps_group_before. in a group, before required parameters. - # 3: ps_required. required parameters. (renumber left groups!) - # 4: ps_group_after. in a group, after required parameters. - # 5: ps_right_square_after. right square brackets after required parameters. - # 6: ps_seen_slash. seen slash. + # 3: ps_required. required parameters, positional-or-keyword or positional-only + # (we don't know yet). (renumber left groups!) + # 4: ps_optional. positional-or-keyword or positional-only parameters that + # now must have default values. + # 5: ps_group_after. in a group, after required parameters. + # 6: ps_right_square_after. right square brackets after required parameters. + # 7: ps_seen_slash. seen slash. ps_start, ps_left_square_before, ps_group_before, ps_required, \ - ps_group_after, ps_right_square_after, ps_seen_slash = range(7) + ps_optional, ps_group_after, ps_right_square_after, ps_seen_slash = range(8) def state_parameters_start(self, line): if self.ignore_line(line): @@ -3245,21 +3299,25 @@ class DSLParser: elif self.parameter_state == self.ps_group_before: if not self.group: self.to_required() - elif self.parameter_state == self.ps_group_after: + elif self.parameter_state in (self.ps_group_after, self.ps_optional): pass else: - fail("Function " + self.function.name + " has an unsupported group configuration. (Unexpected state " + str(self.parameter_state) + ")") + fail("Function " + self.function.name + " has an unsupported group configuration. (Unexpected state " + str(self.parameter_state) + ".a)") base, equals, default = line.rpartition('=') if not equals: base = default default = None + module = None try: ast_input = "def x({}): pass".format(base) module = ast.parse(ast_input) except SyntaxError: try: + # the last = was probably inside a function call, like + # i: int(nullable=True) + # so assume there was no actual default value. default = None ast_input = "def x({}): pass".format(line) module = ast.parse(ast_input) @@ -3275,13 +3333,18 @@ class DSLParser: name, legacy, kwargs = self.parse_converter(parameter.annotation) if not default: + if self.parameter_state == self.ps_optional: + fail("Can't have a parameter without a default (" + repr(parameter_name) + ")\nafter a parameter with a default!") value = unspecified if 'py_default' in kwargs: fail("You can't specify py_default without specifying a default value!") else: + if self.parameter_state == self.ps_required: + self.parameter_state = self.ps_optional default = default.strip() bad = False ast_input = "x = {}".format(default) + bad = False try: module = ast.parse(ast_input) @@ -3441,7 +3504,7 @@ class DSLParser: elif self.parameter_state in (self.ps_required, self.ps_group_after): self.parameter_state = self.ps_group_after else: - fail("Function " + self.function.name + " has an unsupported group configuration. (Unexpected state " + str(self.parameter_state) + ")") + fail("Function " + self.function.name + " has an unsupported group configuration. (Unexpected state " + str(self.parameter_state) + ".b)") self.group += 1 elif symbol == ']': if not self.group: @@ -3454,12 +3517,12 @@ class DSLParser: elif self.parameter_state in (self.ps_group_after, self.ps_right_square_after): self.parameter_state = self.ps_right_square_after else: - fail("Function " + self.function.name + " has an unsupported group configuration. (Unexpected state " + str(self.parameter_state) + ")") + fail("Function " + self.function.name + " has an unsupported group configuration. (Unexpected state " + str(self.parameter_state) + ".c)") elif symbol == '/': - # ps_required is allowed here, that allows positional-only without option groups + # ps_required and ps_optional are allowed here, that allows positional-only without option groups # to work (and have default values!) - if (self.parameter_state not in (self.ps_required, self.ps_right_square_after, self.ps_group_before)) or self.group: - fail("Function " + self.function.name + " has an unsupported group configuration. (Unexpected state " + str(self.parameter_state) + ")") + if (self.parameter_state not in (self.ps_required, self.ps_optional, self.ps_right_square_after, self.ps_group_before)) or self.group: + fail("Function " + self.function.name + " has an unsupported group configuration. (Unexpected state " + str(self.parameter_state) + ".d)") if self.keyword_only: fail("Function " + self.function.name + " mixes keyword-only and positional-only parameters, which is unsupported.") self.parameter_state = self.ps_seen_slash -- cgit v0.12