@@ -34,6 +34,8 @@ class TestAudioop(unittest.TestCase):
3434 def test_max (self ):
3535 for w in 1 , 2 , 3 , 4 :
3636 self .assertEqual (audioop .max (b'' , w ), 0 )
37+ self .assertEqual (audioop .max (bytearray (), w ), 0 )
38+ self .assertEqual (audioop .max (memoryview (b'' ), w ), 0 )
3739 p = packs [w ]
3840 self .assertEqual (audioop .max (p (5 ), w ), 5 )
3941 self .assertEqual (audioop .max (p (5 , - 8 , - 1 ), w ), 8 )
@@ -45,6 +47,10 @@ def test_minmax(self):
4547 for w in 1 , 2 , 3 , 4 :
4648 self .assertEqual (audioop .minmax (b'' , w ),
4749 (0x7fffffff , - 0x80000000 ))
50+ self .assertEqual (audioop .minmax (bytearray (), w ),
51+ (0x7fffffff , - 0x80000000 ))
52+ self .assertEqual (audioop .minmax (memoryview (b'' ), w ),
53+ (0x7fffffff , - 0x80000000 ))
4854 p = packs [w ]
4955 self .assertEqual (audioop .minmax (p (5 ), w ), (5 , 5 ))
5056 self .assertEqual (audioop .minmax (p (5 , - 8 , - 1 ), w ), (- 8 , 5 ))
@@ -58,6 +64,8 @@ def test_minmax(self):
5864 def test_maxpp (self ):
5965 for w in 1 , 2 , 3 , 4 :
6066 self .assertEqual (audioop .maxpp (b'' , w ), 0 )
67+ self .assertEqual (audioop .maxpp (bytearray (), w ), 0 )
68+ self .assertEqual (audioop .maxpp (memoryview (b'' ), w ), 0 )
6169 self .assertEqual (audioop .maxpp (packs [w ](* range (100 )), w ), 0 )
6270 self .assertEqual (audioop .maxpp (packs [w ](9 , 10 , 5 , 5 , 0 , 1 ), w ), 10 )
6371 self .assertEqual (audioop .maxpp (datas [w ], w ),
@@ -66,6 +74,8 @@ def test_maxpp(self):
6674 def test_avg (self ):
6775 for w in 1 , 2 , 3 , 4 :
6876 self .assertEqual (audioop .avg (b'' , w ), 0 )
77+ self .assertEqual (audioop .avg (bytearray (), w ), 0 )
78+ self .assertEqual (audioop .avg (memoryview (b'' ), w ), 0 )
6979 p = packs [w ]
7080 self .assertEqual (audioop .avg (p (5 ), w ), 5 )
7181 self .assertEqual (audioop .avg (p (5 , 8 ), w ), 6 )
@@ -82,6 +92,8 @@ def test_avg(self):
8292 def test_avgpp (self ):
8393 for w in 1 , 2 , 3 , 4 :
8494 self .assertEqual (audioop .avgpp (b'' , w ), 0 )
95+ self .assertEqual (audioop .avgpp (bytearray (), w ), 0 )
96+ self .assertEqual (audioop .avgpp (memoryview (b'' ), w ), 0 )
8597 self .assertEqual (audioop .avgpp (packs [w ](* range (100 )), w ), 0 )
8698 self .assertEqual (audioop .avgpp (packs [w ](9 , 10 , 5 , 5 , 0 , 1 ), w ), 10 )
8799 self .assertEqual (audioop .avgpp (datas [1 ], 1 ), 196 )
@@ -92,6 +104,8 @@ def test_avgpp(self):
92104 def test_rms (self ):
93105 for w in 1 , 2 , 3 , 4 :
94106 self .assertEqual (audioop .rms (b'' , w ), 0 )
107+ self .assertEqual (audioop .rms (bytearray (), w ), 0 )
108+ self .assertEqual (audioop .rms (memoryview (b'' ), w ), 0 )
95109 p = packs [w ]
96110 self .assertEqual (audioop .rms (p (* range (100 )), w ), 57 )
97111 self .assertAlmostEqual (audioop .rms (p (maxvalues [w ]) * 5 , w ),
@@ -106,6 +120,8 @@ def test_rms(self):
106120 def test_cross (self ):
107121 for w in 1 , 2 , 3 , 4 :
108122 self .assertEqual (audioop .cross (b'' , w ), - 1 )
123+ self .assertEqual (audioop .cross (bytearray (), w ), - 1 )
124+ self .assertEqual (audioop .cross (memoryview (b'' ), w ), - 1 )
109125 p = packs [w ]
110126 self .assertEqual (audioop .cross (p (0 , 1 , 2 ), w ), 0 )
111127 self .assertEqual (audioop .cross (p (1 , 2 , - 3 , - 4 ), w ), 1 )
@@ -116,6 +132,8 @@ def test_cross(self):
116132 def test_add (self ):
117133 for w in 1 , 2 , 3 , 4 :
118134 self .assertEqual (audioop .add (b'' , b'' , w ), b'' )
135+ self .assertEqual (audioop .add (bytearray (), bytearray (), w ), b'' )
136+ self .assertEqual (audioop .add (memoryview (b'' ), memoryview (b'' ), w ), b'' )
119137 self .assertEqual (audioop .add (datas [w ], b'\0 ' * len (datas [w ]), w ),
120138 datas [w ])
121139 self .assertEqual (audioop .add (datas [1 ], datas [1 ], 1 ),
@@ -133,6 +151,8 @@ def test_bias(self):
133151 for w in 1 , 2 , 3 , 4 :
134152 for bias in 0 , 1 , - 1 , 127 , - 128 , 0x7fffffff , - 0x80000000 :
135153 self .assertEqual (audioop .bias (b'' , w , bias ), b'' )
154+ self .assertEqual (audioop .bias (bytearray (), w , bias ), b'' )
155+ self .assertEqual (audioop .bias (memoryview (b'' ), w , bias ), b'' )
136156 self .assertEqual (audioop .bias (datas [1 ], 1 , 1 ),
137157 b'\x01 \x13 \x46 \xbc \x80 \x81 \x00 ' )
138158 self .assertEqual (audioop .bias (datas [1 ], 1 , - 1 ),
@@ -176,6 +196,10 @@ def test_bias(self):
176196 def test_lin2lin (self ):
177197 for w in 1 , 2 , 3 , 4 :
178198 self .assertEqual (audioop .lin2lin (datas [w ], w , w ), datas [w ])
199+ self .assertEqual (audioop .lin2lin (bytearray (datas [w ]), w , w ),
200+ datas [w ])
201+ self .assertEqual (audioop .lin2lin (memoryview (datas [w ]), w , w ),
202+ datas [w ])
179203
180204 self .assertEqual (audioop .lin2lin (datas [1 ], 1 , 2 ),
181205 packs [2 ](0 , 0x1200 , 0x4500 , - 0x4500 , 0x7f00 , - 0x8000 , - 0x100 ))
@@ -211,6 +235,10 @@ def test_lin2lin(self):
211235 def test_adpcm2lin (self ):
212236 self .assertEqual (audioop .adpcm2lin (b'\x07 \x7f \x7f ' , 1 , None ),
213237 (b'\x00 \x00 \x00 \xff \x00 \xff ' , (- 179 , 40 )))
238+ self .assertEqual (audioop .adpcm2lin (bytearray (b'\x07 \x7f \x7f ' ), 1 , None ),
239+ (b'\x00 \x00 \x00 \xff \x00 \xff ' , (- 179 , 40 )))
240+ self .assertEqual (audioop .adpcm2lin (memoryview (b'\x07 \x7f \x7f ' ), 1 , None ),
241+ (b'\x00 \x00 \x00 \xff \x00 \xff ' , (- 179 , 40 )))
214242 self .assertEqual (audioop .adpcm2lin (b'\x07 \x7f \x7f ' , 2 , None ),
215243 (packs [2 ](0 , 0xb , 0x29 , - 0x16 , 0x72 , - 0xb3 ), (- 179 , 40 )))
216244 self .assertEqual (audioop .adpcm2lin (b'\x07 \x7f \x7f ' , 3 , None ),
@@ -228,6 +256,10 @@ def test_adpcm2lin(self):
228256 def test_lin2adpcm (self ):
229257 self .assertEqual (audioop .lin2adpcm (datas [1 ], 1 , None ),
230258 (b'\x07 \x7f \x7f ' , (- 221 , 39 )))
259+ self .assertEqual (audioop .lin2adpcm (bytearray (datas [1 ]), 1 , None ),
260+ (b'\x07 \x7f \x7f ' , (- 221 , 39 )))
261+ self .assertEqual (audioop .lin2adpcm (memoryview (datas [1 ]), 1 , None ),
262+ (b'\x07 \x7f \x7f ' , (- 221 , 39 )))
231263 for w in 2 , 3 , 4 :
232264 self .assertEqual (audioop .lin2adpcm (datas [w ], w , None ),
233265 (b'\x07 \x7f \x7f ' , (31 , 39 )))
@@ -240,6 +272,10 @@ def test_lin2adpcm(self):
240272 def test_lin2alaw (self ):
241273 self .assertEqual (audioop .lin2alaw (datas [1 ], 1 ),
242274 b'\xd5 \x87 \xa4 \x24 \xaa \x2a \x5a ' )
275+ self .assertEqual (audioop .lin2alaw (bytearray (datas [1 ]), 1 ),
276+ b'\xd5 \x87 \xa4 \x24 \xaa \x2a \x5a ' )
277+ self .assertEqual (audioop .lin2alaw (memoryview (datas [1 ]), 1 ),
278+ b'\xd5 \x87 \xa4 \x24 \xaa \x2a \x5a ' )
243279 for w in 2 , 3 , 4 :
244280 self .assertEqual (audioop .lin2alaw (datas [w ], w ),
245281 b'\xd5 \x87 \xa4 \x24 \xaa \x2a \x55 ' )
@@ -250,8 +286,10 @@ def test_alaw2lin(self):
250286 src = [- 688 , - 720 , - 2240 , - 4032 , - 9 , - 3 , - 1 , - 27 , - 244 , - 82 , - 106 ,
251287 688 , 720 , 2240 , 4032 , 9 , 3 , 1 , 27 , 244 , 82 , 106 ]
252288 for w in 1 , 2 , 3 , 4 :
253- self .assertEqual (audioop .alaw2lin (encoded , w ),
254- packs [w ](* (x << (w * 8 ) >> 13 for x in src )))
289+ decoded = packs [w ](* (x << (w * 8 ) >> 13 for x in src ))
290+ self .assertEqual (audioop .alaw2lin (encoded , w ), decoded )
291+ self .assertEqual (audioop .alaw2lin (bytearray (encoded ), w ), decoded )
292+ self .assertEqual (audioop .alaw2lin (memoryview (encoded ), w ), decoded )
255293
256294 encoded = bytes (range (256 ))
257295 for w in 2 , 3 , 4 :
@@ -261,6 +299,10 @@ def test_alaw2lin(self):
261299 def test_lin2ulaw (self ):
262300 self .assertEqual (audioop .lin2ulaw (datas [1 ], 1 ),
263301 b'\xff \xad \x8e \x0e \x80 \x00 \x67 ' )
302+ self .assertEqual (audioop .lin2ulaw (bytearray (datas [1 ]), 1 ),
303+ b'\xff \xad \x8e \x0e \x80 \x00 \x67 ' )
304+ self .assertEqual (audioop .lin2ulaw (memoryview (datas [1 ]), 1 ),
305+ b'\xff \xad \x8e \x0e \x80 \x00 \x67 ' )
264306 for w in 2 , 3 , 4 :
265307 self .assertEqual (audioop .lin2ulaw (datas [w ], w ),
266308 b'\xff \xad \x8e \x0e \x80 \x00 \x7e ' )
@@ -271,8 +313,10 @@ def test_ulaw2lin(self):
271313 src = [- 8031 , - 4447 , - 1471 , - 495 , - 163 , - 53 , - 18 , - 6 , - 2 , 0 ,
272314 8031 , 4447 , 1471 , 495 , 163 , 53 , 18 , 6 , 2 , 0 ]
273315 for w in 1 , 2 , 3 , 4 :
274- self .assertEqual (audioop .ulaw2lin (encoded , w ),
275- packs [w ](* (x << (w * 8 ) >> 14 for x in src )))
316+ decoded = packs [w ](* (x << (w * 8 ) >> 14 for x in src ))
317+ self .assertEqual (audioop .ulaw2lin (encoded , w ), decoded )
318+ self .assertEqual (audioop .ulaw2lin (bytearray (encoded ), w ), decoded )
319+ self .assertEqual (audioop .ulaw2lin (memoryview (encoded ), w ), decoded )
276320
277321 # Current u-law implementation has two codes fo 0: 0x7f and 0xff.
278322 encoded = bytes (range (127 )) + bytes (range (128 , 256 ))
@@ -283,6 +327,8 @@ def test_ulaw2lin(self):
283327 def test_mul (self ):
284328 for w in 1 , 2 , 3 , 4 :
285329 self .assertEqual (audioop .mul (b'' , w , 2 ), b'' )
330+ self .assertEqual (audioop .mul (bytearray (), w , 2 ), b'' )
331+ self .assertEqual (audioop .mul (memoryview (b'' ), w , 2 ), b'' )
286332 self .assertEqual (audioop .mul (datas [w ], w , 0 ),
287333 b'\0 ' * len (datas [w ]))
288334 self .assertEqual (audioop .mul (datas [w ], w , 1 ),
@@ -302,6 +348,10 @@ def test_ratecv(self):
302348 for w in 1 , 2 , 3 , 4 :
303349 self .assertEqual (audioop .ratecv (b'' , w , 1 , 8000 , 8000 , None ),
304350 (b'' , (- 1 , ((0 , 0 ),))))
351+ self .assertEqual (audioop .ratecv (bytearray (), w , 1 , 8000 , 8000 , None ),
352+ (b'' , (- 1 , ((0 , 0 ),))))
353+ self .assertEqual (audioop .ratecv (memoryview (b'' ), w , 1 , 8000 , 8000 , None ),
354+ (b'' , (- 1 , ((0 , 0 ),))))
305355 self .assertEqual (audioop .ratecv (b'' , w , 5 , 8000 , 8000 , None ),
306356 (b'' , (- 1 , ((0 , 0 ),) * 5 )))
307357 self .assertEqual (audioop .ratecv (b'' , w , 1 , 8000 , 16000 , None ),
@@ -326,6 +376,8 @@ def test_ratecv(self):
326376 def test_reverse (self ):
327377 for w in 1 , 2 , 3 , 4 :
328378 self .assertEqual (audioop .reverse (b'' , w ), b'' )
379+ self .assertEqual (audioop .reverse (bytearray (), w ), b'' )
380+ self .assertEqual (audioop .reverse (memoryview (b'' ), w ), b'' )
329381 self .assertEqual (audioop .reverse (packs [w ](0 , 1 , 2 ), w ),
330382 packs [w ](2 , 1 , 0 ))
331383
@@ -340,6 +392,10 @@ def test_tomono(self):
340392 for k in range (w ):
341393 data2 [k + w ::2 * w ] = data1 [k ::w ]
342394 self .assertEqual (audioop .tomono (data2 , w , 0.5 , 0.5 ), data1 )
395+ self .assertEqual (audioop .tomono (bytearray (data2 ), w , 0.5 , 0.5 ),
396+ data1 )
397+ self .assertEqual (audioop .tomono (memoryview (data2 ), w , 0.5 , 0.5 ),
398+ data1 )
343399
344400 def test_tostereo (self ):
345401 for w in 1 , 2 , 3 , 4 :
@@ -352,26 +408,41 @@ def test_tostereo(self):
352408 for k in range (w ):
353409 data2 [k + w ::2 * w ] = data1 [k ::w ]
354410 self .assertEqual (audioop .tostereo (data1 , w , 1 , 1 ), data2 )
411+ self .assertEqual (audioop .tostereo (bytearray (data1 ), w , 1 , 1 ), data2 )
412+ self .assertEqual (audioop .tostereo (memoryview (data1 ), w , 1 , 1 ),
413+ data2 )
355414
356415 def test_findfactor (self ):
357416 self .assertEqual (audioop .findfactor (datas [2 ], datas [2 ]), 1.0 )
417+ self .assertEqual (audioop .findfactor (bytearray (datas [2 ]),
418+ bytearray (datas [2 ])), 1.0 )
419+ self .assertEqual (audioop .findfactor (memoryview (datas [2 ]),
420+ memoryview (datas [2 ])), 1.0 )
358421 self .assertEqual (audioop .findfactor (b'\0 ' * len (datas [2 ]), datas [2 ]),
359422 0.0 )
360423
361424 def test_findfit (self ):
362425 self .assertEqual (audioop .findfit (datas [2 ], datas [2 ]), (0 , 1.0 ))
426+ self .assertEqual (audioop .findfit (bytearray (datas [2 ]),
427+ bytearray (datas [2 ])), (0 , 1.0 ))
428+ self .assertEqual (audioop .findfit (memoryview (datas [2 ]),
429+ memoryview (datas [2 ])), (0 , 1.0 ))
363430 self .assertEqual (audioop .findfit (datas [2 ], packs [2 ](1 , 2 , 0 )),
364431 (1 , 8038.8 ))
365432 self .assertEqual (audioop .findfit (datas [2 ][:- 2 ] * 5 + datas [2 ], datas [2 ]),
366433 (30 , 1.0 ))
367434
368435 def test_findmax (self ):
369436 self .assertEqual (audioop .findmax (datas [2 ], 1 ), 5 )
437+ self .assertEqual (audioop .findmax (bytearray (datas [2 ]), 1 ), 5 )
438+ self .assertEqual (audioop .findmax (memoryview (datas [2 ]), 1 ), 5 )
370439
371440 def test_getsample (self ):
372441 for w in 1 , 2 , 3 , 4 :
373442 data = packs [w ](0 , 1 , - 1 , maxvalues [w ], minvalues [w ])
374443 self .assertEqual (audioop .getsample (data , w , 0 ), 0 )
444+ self .assertEqual (audioop .getsample (bytearray (data ), w , 0 ), 0 )
445+ self .assertEqual (audioop .getsample (memoryview (data ), w , 0 ), 0 )
375446 self .assertEqual (audioop .getsample (data , w , 1 ), 1 )
376447 self .assertEqual (audioop .getsample (data , w , 2 ), - 1 )
377448 self .assertEqual (audioop .getsample (data , w , 3 ), maxvalues [w ])
@@ -406,6 +477,29 @@ def test_issue7673(self):
406477 self .assertRaises (audioop .error , audioop .lin2alaw , data , size )
407478 self .assertRaises (audioop .error , audioop .lin2adpcm , data , size , state )
408479
480+ def test_string (self ):
481+ data = 'abcd'
482+ size = 2
483+ self .assertRaises (TypeError , audioop .getsample , data , size , 0 )
484+ self .assertRaises (TypeError , audioop .max , data , size )
485+ self .assertRaises (TypeError , audioop .minmax , data , size )
486+ self .assertRaises (TypeError , audioop .avg , data , size )
487+ self .assertRaises (TypeError , audioop .rms , data , size )
488+ self .assertRaises (TypeError , audioop .avgpp , data , size )
489+ self .assertRaises (TypeError , audioop .maxpp , data , size )
490+ self .assertRaises (TypeError , audioop .cross , data , size )
491+ self .assertRaises (TypeError , audioop .mul , data , size , 1.0 )
492+ self .assertRaises (TypeError , audioop .tomono , data , size , 0.5 , 0.5 )
493+ self .assertRaises (TypeError , audioop .tostereo , data , size , 0.5 , 0.5 )
494+ self .assertRaises (TypeError , audioop .add , data , data , size )
495+ self .assertRaises (TypeError , audioop .bias , data , size , 0 )
496+ self .assertRaises (TypeError , audioop .reverse , data , size )
497+ self .assertRaises (TypeError , audioop .lin2lin , data , size , size )
498+ self .assertRaises (TypeError , audioop .ratecv , data , size , 1 , 1 , 1 , None )
499+ self .assertRaises (TypeError , audioop .lin2ulaw , data , size )
500+ self .assertRaises (TypeError , audioop .lin2alaw , data , size )
501+ self .assertRaises (TypeError , audioop .lin2adpcm , data , size , None )
502+
409503 def test_wrongsize (self ):
410504 data = b'abcdefgh'
411505 state = None
0 commit comments