Issue #8657: Make the audioop module PY_SSIZE_T_CLEAN.

This commit is contained in:
Mark Dickinson 2010-05-11 13:34:35 +00:00
parent 393b97a7b6
commit 81fece2d63
2 changed files with 73 additions and 64 deletions

View File

@ -351,6 +351,9 @@ C-API
Library Library
------- -------
- The audioop module now supports sound fragments of length greater
than 2**31 bytes on 64-bit machines, and is PY_SSIZE_T_CLEAN.
- Issue #4972: Add support for the context manager protocol to the ftplib.FTP - Issue #4972: Add support for the context manager protocol to the ftplib.FTP
class. class.

View File

@ -1,6 +1,8 @@
/* audioopmodule - Module to detect peak values in arrays */ /* audioopmodule - Module to detect peak values in arrays */
#define PY_SSIZE_T_CLEAN
#include "Python.h" #include "Python.h"
#if SIZEOF_INT == 4 #if SIZEOF_INT == 4
@ -299,10 +301,10 @@ static PyObject *
audioop_getsample(PyObject *self, PyObject *args) audioop_getsample(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
int len, size, val = 0; Py_ssize_t len, i;
int i; int size, val = 0;
if ( !PyArg_ParseTuple(args, "s#ii:getsample", &cp, &len, &size, &i) ) if ( !PyArg_ParseTuple(args, "s#in:getsample", &cp, &len, &size, &i) )
return 0; return 0;
if ( size != 1 && size != 2 && size != 4 ) { if ( size != 1 && size != 2 && size != 4 ) {
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
@ -322,8 +324,8 @@ static PyObject *
audioop_max(PyObject *self, PyObject *args) audioop_max(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
int len, size, val = 0; Py_ssize_t len, i;
int i; int size, val = 0;
int max = 0; int max = 0;
if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) ) if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
@ -346,8 +348,8 @@ static PyObject *
audioop_minmax(PyObject *self, PyObject *args) audioop_minmax(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
int len, size, val = 0; Py_ssize_t len, i;
int i; int size, val = 0;
int min = 0x7fffffff, max = -0x7fffffff; int min = 0x7fffffff, max = -0x7fffffff;
if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size)) if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
@ -370,8 +372,8 @@ static PyObject *
audioop_avg(PyObject *self, PyObject *args) audioop_avg(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
int len, size, val = 0; Py_ssize_t len, i;
int i; int size, val = 0;
double avg = 0.0; double avg = 0.0;
if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) ) if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
@ -397,8 +399,8 @@ static PyObject *
audioop_rms(PyObject *self, PyObject *args) audioop_rms(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
int len, size, val = 0; Py_ssize_t len, i;
int i; int size, val = 0;
double sum_squares = 0.0; double sum_squares = 0.0;
if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) ) if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
@ -420,9 +422,9 @@ audioop_rms(PyObject *self, PyObject *args)
return PyLong_FromLong(val); return PyLong_FromLong(val);
} }
static double _sum2(short *a, short *b, int len) static double _sum2(short *a, short *b, Py_ssize_t len)
{ {
int i; Py_ssize_t i;
double sum = 0.0; double sum = 0.0;
for( i=0; i<len; i++) { for( i=0; i<len; i++) {
@ -467,8 +469,8 @@ static PyObject *
audioop_findfit(PyObject *self, PyObject *args) audioop_findfit(PyObject *self, PyObject *args)
{ {
short *cp1, *cp2; short *cp1, *cp2;
int len1, len2; Py_ssize_t len1, len2;
int j, best_j; Py_ssize_t j, best_j;
double aj_m1, aj_lm1; double aj_m1, aj_lm1;
double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor; double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
@ -519,7 +521,7 @@ audioop_findfit(PyObject *self, PyObject *args)
factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2; factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
return Py_BuildValue("(if)", best_j, factor); return Py_BuildValue("(nf)", best_j, factor);
} }
/* /*
@ -530,7 +532,7 @@ static PyObject *
audioop_findfactor(PyObject *self, PyObject *args) audioop_findfactor(PyObject *self, PyObject *args)
{ {
short *cp1, *cp2; short *cp1, *cp2;
int len1, len2; Py_ssize_t len1, len2;
double sum_ri_2, sum_aij_ri, result; double sum_ri_2, sum_aij_ri, result;
if ( !PyArg_ParseTuple(args, "s#s#:findfactor", if ( !PyArg_ParseTuple(args, "s#s#:findfactor",
@ -561,12 +563,12 @@ static PyObject *
audioop_findmax(PyObject *self, PyObject *args) audioop_findmax(PyObject *self, PyObject *args)
{ {
short *cp1; short *cp1;
int len1, len2; Py_ssize_t len1, len2;
int j, best_j; Py_ssize_t j, best_j;
double aj_m1, aj_lm1; double aj_m1, aj_lm1;
double result, best_result; double result, best_result;
if ( !PyArg_ParseTuple(args, "s#i:findmax", if ( !PyArg_ParseTuple(args, "s#n:findmax",
(char**)&cp1, &len1, &len2) ) (char**)&cp1, &len1, &len2) )
return 0; return 0;
if ( len1 & 1 ) { if ( len1 & 1 ) {
@ -599,16 +601,16 @@ audioop_findmax(PyObject *self, PyObject *args)
} }
return PyLong_FromLong(best_j); return PyLong_FromSsize_t(best_j);
} }
static PyObject * static PyObject *
audioop_avgpp(PyObject *self, PyObject *args) audioop_avgpp(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
int len, size, val = 0, prevval = 0, prevextremevalid = 0, Py_ssize_t len, i;
int size, val = 0, prevval = 0, prevextremevalid = 0,
prevextreme = 0; prevextreme = 0;
int i;
double avg = 0.0; double avg = 0.0;
int diff, prevdiff, extremediff, nextreme = 0; int diff, prevdiff, extremediff, nextreme = 0;
@ -663,9 +665,9 @@ static PyObject *
audioop_maxpp(PyObject *self, PyObject *args) audioop_maxpp(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
int len, size, val = 0, prevval = 0, prevextremevalid = 0, Py_ssize_t len, i;
int size, val = 0, prevval = 0, prevextremevalid = 0,
prevextreme = 0; prevextreme = 0;
int i;
int max = 0; int max = 0;
int diff, prevdiff, extremediff; int diff, prevdiff, extremediff;
@ -716,9 +718,10 @@ static PyObject *
audioop_cross(PyObject *self, PyObject *args) audioop_cross(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
int len, size, val = 0; Py_ssize_t len, i;
int i; int size, val = 0;
int prevval, ncross; int prevval;
Py_ssize_t ncross;
if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) ) if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
return 0; return 0;
@ -736,17 +739,17 @@ audioop_cross(PyObject *self, PyObject *args)
if ( val != prevval ) ncross++; if ( val != prevval ) ncross++;
prevval = val; prevval = val;
} }
return PyLong_FromLong(ncross); return PyLong_FromSsize_t(ncross);
} }
static PyObject * static PyObject *
audioop_mul(PyObject *self, PyObject *args) audioop_mul(PyObject *self, PyObject *args)
{ {
signed char *cp, *ncp; signed char *cp, *ncp;
int len, size, val = 0; Py_ssize_t len, i;
int size, val = 0;
double factor, fval, maxval; double factor, fval, maxval;
PyObject *rv; PyObject *rv;
int i;
if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) ) if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
return 0; return 0;
@ -785,10 +788,10 @@ audioop_tomono(PyObject *self, PyObject *args)
{ {
Py_buffer pcp; Py_buffer pcp;
signed char *cp, *ncp; signed char *cp, *ncp;
int len, size, val1 = 0, val2 = 0; Py_ssize_t len, i;
int size, val1 = 0, val2 = 0;
double fac1, fac2, fval, maxval; double fac1, fac2, fval, maxval;
PyObject *rv; PyObject *rv;
int i;
if ( !PyArg_ParseTuple(args, "s*idd:tomono", if ( !PyArg_ParseTuple(args, "s*idd:tomono",
&pcp, &size, &fac1, &fac2 ) ) &pcp, &size, &fac1, &fac2 ) )
@ -834,10 +837,10 @@ static PyObject *
audioop_tostereo(PyObject *self, PyObject *args) audioop_tostereo(PyObject *self, PyObject *args)
{ {
signed char *cp, *ncp; signed char *cp, *ncp;
int len, size, val1, val2, val = 0; Py_ssize_t len, i;
int size, val1, val2, val = 0;
double fac1, fac2, fval, maxval; double fac1, fac2, fval, maxval;
PyObject *rv; PyObject *rv;
int i;
if ( !PyArg_ParseTuple(args, "s#idd:tostereo", if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
&cp, &len, &size, &fac1, &fac2 ) ) &cp, &len, &size, &fac1, &fac2 ) )
@ -851,7 +854,7 @@ audioop_tostereo(PyObject *self, PyObject *args)
return 0; return 0;
} }
if (len > INT_MAX/2) { if (len > PY_SSIZE_T_MAX/2) {
PyErr_SetString(PyExc_MemoryError, PyErr_SetString(PyExc_MemoryError,
"not enough memory for output buffer"); "not enough memory for output buffer");
return 0; return 0;
@ -893,9 +896,9 @@ static PyObject *
audioop_add(PyObject *self, PyObject *args) audioop_add(PyObject *self, PyObject *args)
{ {
signed char *cp1, *cp2, *ncp; signed char *cp1, *cp2, *ncp;
int len1, len2, size, val1 = 0, val2 = 0, maxval, newval; Py_ssize_t len1, len2, i;
int size, val1 = 0, val2 = 0, maxval, newval;
PyObject *rv; PyObject *rv;
int i;
if ( !PyArg_ParseTuple(args, "s#s#i:add", if ( !PyArg_ParseTuple(args, "s#s#i:add",
&cp1, &len1, &cp2, &len2, &size ) ) &cp1, &len1, &cp2, &len2, &size ) )
@ -946,9 +949,9 @@ static PyObject *
audioop_bias(PyObject *self, PyObject *args) audioop_bias(PyObject *self, PyObject *args)
{ {
signed char *cp, *ncp; signed char *cp, *ncp;
int len, size, val = 0; Py_ssize_t len, i;
int size, val = 0;
PyObject *rv; PyObject *rv;
int i;
int bias; int bias;
if ( !PyArg_ParseTuple(args, "s#ii:bias", if ( !PyArg_ParseTuple(args, "s#ii:bias",
@ -983,9 +986,9 @@ audioop_reverse(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
unsigned char *ncp; unsigned char *ncp;
int len, size, val = 0; Py_ssize_t len, i, j;
int size, val = 0;
PyObject *rv; PyObject *rv;
int i, j;
if ( !PyArg_ParseTuple(args, "s#i:reverse", if ( !PyArg_ParseTuple(args, "s#i:reverse",
&cp, &len, &size) ) &cp, &len, &size) )
@ -1020,9 +1023,9 @@ audioop_lin2lin(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
unsigned char *ncp; unsigned char *ncp;
int len, size, size2, val = 0; Py_ssize_t len, i, j;
int size, size2, val = 0;
PyObject *rv; PyObject *rv;
int i, j;
if ( !PyArg_ParseTuple(args, "s#ii:lin2lin", if ( !PyArg_ParseTuple(args, "s#ii:lin2lin",
&cp, &len, &size, &size2) ) &cp, &len, &size, &size2) )
@ -1034,7 +1037,7 @@ audioop_lin2lin(PyObject *self, PyObject *args)
return 0; return 0;
} }
if (len/size > INT_MAX/size2) { if (len/size > PY_SSIZE_T_MAX/size2) {
PyErr_SetString(PyExc_MemoryError, PyErr_SetString(PyExc_MemoryError,
"not enough memory for output buffer"); "not enough memory for output buffer");
return 0; return 0;
@ -1071,7 +1074,8 @@ static PyObject *
audioop_ratecv(PyObject *self, PyObject *args) audioop_ratecv(PyObject *self, PyObject *args)
{ {
char *cp, *ncp; char *cp, *ncp;
int len, size, nchannels, inrate, outrate, weightA, weightB; Py_ssize_t len;
int size, nchannels, inrate, outrate, weightA, weightB;
int chan, d, *prev_i, *cur_i, cur_o; int chan, d, *prev_i, *cur_i, cur_o;
PyObject *state, *samps, *str, *rv = NULL; PyObject *state, *samps, *str, *rv = NULL;
int bytes_per_frame; int bytes_per_frame;
@ -1164,8 +1168,8 @@ audioop_ratecv(PyObject *self, PyObject *args)
case ceiling(len/inrate) * outrate. */ case ceiling(len/inrate) * outrate. */
/* compute ceiling(len/inrate) without overflow */ /* compute ceiling(len/inrate) without overflow */
int q = len > 0 ? 1 + (len - 1) / inrate : 0; Py_ssize_t q = len > 0 ? 1 + (len - 1) / inrate : 0;
if (outrate > INT_MAX / q / bytes_per_frame) if (outrate > PY_SSIZE_T_MAX / q / bytes_per_frame)
str = NULL; str = NULL;
else else
str = PyBytes_FromStringAndSize(NULL, str = PyBytes_FromStringAndSize(NULL,
@ -1194,7 +1198,7 @@ audioop_ratecv(PyObject *self, PyObject *args)
goto exit; goto exit;
/* We have checked before that the length /* We have checked before that the length
* of the string fits into int. */ * of the string fits into int. */
len = (int)(ncp - PyBytes_AsString(str)); len = (Py_ssize_t)(ncp - PyBytes_AsString(str));
rv = PyBytes_FromStringAndSize rv = PyBytes_FromStringAndSize
(PyBytes_AsString(str), len); (PyBytes_AsString(str), len);
Py_DECREF(str); Py_DECREF(str);
@ -1253,9 +1257,9 @@ audioop_lin2ulaw(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
unsigned char *ncp; unsigned char *ncp;
int len, size, val = 0; Py_ssize_t len, i;
int size, val = 0;
PyObject *rv; PyObject *rv;
int i;
if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw", if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw",
&cp, &len, &size) ) &cp, &len, &size) )
@ -1287,9 +1291,9 @@ audioop_ulaw2lin(PyObject *self, PyObject *args)
unsigned char *cp; unsigned char *cp;
unsigned char cval; unsigned char cval;
signed char *ncp; signed char *ncp;
int len, size, val; Py_ssize_t len, i;
int size, val;
PyObject *rv; PyObject *rv;
int i;
if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin", if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin",
&cp, &len, &size) ) &cp, &len, &size) )
@ -1300,7 +1304,7 @@ audioop_ulaw2lin(PyObject *self, PyObject *args)
return 0; return 0;
} }
if (len > INT_MAX/size) { if (len > PY_SSIZE_T_MAX/size) {
PyErr_SetString(PyExc_MemoryError, PyErr_SetString(PyExc_MemoryError,
"not enough memory for output buffer"); "not enough memory for output buffer");
return 0; return 0;
@ -1326,9 +1330,9 @@ audioop_lin2alaw(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
unsigned char *ncp; unsigned char *ncp;
int len, size, val = 0; Py_ssize_t len, i;
int size, val = 0;
PyObject *rv; PyObject *rv;
int i;
if ( !PyArg_ParseTuple(args, "s#i:lin2alaw", if ( !PyArg_ParseTuple(args, "s#i:lin2alaw",
&cp, &len, &size) ) &cp, &len, &size) )
@ -1360,9 +1364,9 @@ audioop_alaw2lin(PyObject *self, PyObject *args)
unsigned char *cp; unsigned char *cp;
unsigned char cval; unsigned char cval;
signed char *ncp; signed char *ncp;
int len, size, val; Py_ssize_t len, i;
int size, val;
PyObject *rv; PyObject *rv;
int i;
if ( !PyArg_ParseTuple(args, "s#i:alaw2lin", if ( !PyArg_ParseTuple(args, "s#i:alaw2lin",
&cp, &len, &size) ) &cp, &len, &size) )
@ -1373,7 +1377,7 @@ audioop_alaw2lin(PyObject *self, PyObject *args)
return 0; return 0;
} }
if (len > INT_MAX/size) { if (len > PY_SSIZE_T_MAX/size) {
PyErr_SetString(PyExc_MemoryError, PyErr_SetString(PyExc_MemoryError,
"not enough memory for output buffer"); "not enough memory for output buffer");
return 0; return 0;
@ -1399,10 +1403,11 @@ audioop_lin2adpcm(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
signed char *ncp; signed char *ncp;
int len, size, val = 0, step, valpred, delta, Py_ssize_t len, i;
int size, val = 0, step, valpred, delta,
index, sign, vpdiff, diff; index, sign, vpdiff, diff;
PyObject *rv, *state, *str; PyObject *rv, *state, *str;
int i, outputbuffer = 0, bufferstep; int outputbuffer = 0, bufferstep;
if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm", if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm",
&cp, &len, &size, &state) ) &cp, &len, &size, &state) )
@ -1508,9 +1513,10 @@ audioop_adpcm2lin(PyObject *self, PyObject *args)
{ {
signed char *cp; signed char *cp;
signed char *ncp; signed char *ncp;
int len, size, valpred, step, delta, index, sign, vpdiff; Py_ssize_t len, i;
int size, valpred, step, delta, index, sign, vpdiff;
PyObject *rv, *str, *state; PyObject *rv, *str, *state;
int i, inputbuffer = 0, bufferstep; int inputbuffer = 0, bufferstep;
if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin", if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin",
&cp, &len, &size, &state) ) &cp, &len, &size, &state) )
@ -1530,7 +1536,7 @@ audioop_adpcm2lin(PyObject *self, PyObject *args)
} else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) ) } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
return 0; return 0;
if (len > (INT_MAX/2)/size) { if (len > (PY_SSIZE_T_MAX/2)/size) {
PyErr_SetString(PyExc_MemoryError, PyErr_SetString(PyExc_MemoryError,
"not enough memory for output buffer"); "not enough memory for output buffer");
return 0; return 0;