Skip to content

Commit 87eec70

Browse files
authored
Deprecate audioop (GH-32392)
1 parent 1df4298 commit 87eec70

File tree

12 files changed

+68
-26
lines changed

12 files changed

+68
-26
lines changed

Doc/whatsnew/3.11.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ Deprecated
833833
slated for removal in Python 3.13:
834834

835835
* :mod:`aifc`
836+
* :mod:`audioop`
836837

837838
(Contributed by Brett Cannon in :issue:`47061`.)
838839

Lib/aifc.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -451,23 +451,31 @@ def readframes(self, nframes):
451451
#
452452

453453
def _alaw2lin(self, data):
454-
import audioop
454+
with warnings.catch_warnings():
455+
warnings.simplefilter('ignore', category=DeprecationWarning)
456+
import audioop
455457
return audioop.alaw2lin(data, 2)
456458

457459
def _ulaw2lin(self, data):
458-
import audioop
460+
with warnings.catch_warnings():
461+
warnings.simplefilter('ignore', category=DeprecationWarning)
462+
import audioop
459463
return audioop.ulaw2lin(data, 2)
460464

461465
def _adpcm2lin(self, data):
462-
import audioop
466+
with warnings.catch_warnings():
467+
warnings.simplefilter('ignore', category=DeprecationWarning)
468+
import audioop
463469
if not hasattr(self, '_adpcmstate'):
464470
# first time
465471
self._adpcmstate = None
466472
data, self._adpcmstate = audioop.adpcm2lin(data, 2, self._adpcmstate)
467473
return data
468474

469475
def _sowt2lin(self, data):
470-
import audioop
476+
with warnings.catch_warnings():
477+
warnings.simplefilter('ignore', category=DeprecationWarning)
478+
import audioop
471479
return audioop.byteswap(data, 2)
472480

473481
def _read_comm_chunk(self, chunk):
@@ -774,22 +782,30 @@ def close(self):
774782
#
775783

776784
def _lin2alaw(self, data):
777-
import audioop
785+
with warnings.catch_warnings():
786+
warnings.simplefilter('ignore', category=DeprecationWarning)
787+
import audioop
778788
return audioop.lin2alaw(data, 2)
779789

780790
def _lin2ulaw(self, data):
781-
import audioop
791+
with warnings.catch_warnings():
792+
warnings.simplefilter('ignore', category=DeprecationWarning)
793+
import audioop
782794
return audioop.lin2ulaw(data, 2)
783795

784796
def _lin2adpcm(self, data):
785-
import audioop
797+
with warnings.catch_warnings():
798+
warnings.simplefilter('ignore', category=DeprecationWarning)
799+
import audioop
786800
if not hasattr(self, '_adpcmstate'):
787801
self._adpcmstate = None
788802
data, self._adpcmstate = audioop.lin2adpcm(data, 2, self._adpcmstate)
789803
return data
790804

791805
def _lin2sowt(self, data):
792-
import audioop
806+
with warnings.catch_warnings():
807+
warnings.simplefilter('ignore', category=DeprecationWarning)
808+
import audioop
793809
return audioop.byteswap(data, 2)
794810

795811
def _ensure_header_written(self, datasize):

Lib/sunau.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
"""
105105

106106
from collections import namedtuple
107+
import warnings
107108

108109

109110
_sunau_params = namedtuple('_sunau_params',
@@ -275,7 +276,9 @@ def readframes(self, nframes):
275276
data = self._file.read(nframes * self._framesize)
276277
self._soundpos += len(data) // self._framesize
277278
if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
278-
import audioop
279+
with warnings.catch_warnings():
280+
warnings.simplefilter('ignore', category=DeprecationWarning)
281+
import audioop
279282
data = audioop.ulaw2lin(data, self._sampwidth)
280283
return data
281284
return None # XXX--not implemented yet
@@ -421,7 +424,9 @@ def writeframesraw(self, data):
421424
data = memoryview(data).cast('B')
422425
self._ensure_header_written()
423426
if self._comptype == 'ULAW':
424-
import audioop
427+
with warnings.catch_warnings():
428+
warnings.simplefilter('ignore', category=DeprecationWarning)
429+
import audioop
425430
data = audioop.lin2ulaw(data, self._sampwidth)
426431
nframes = len(data) // self._framesize
427432
self._file.write(data)

Lib/test/test_aifc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import unittest
55
from unittest import mock
66
from test import audiotests
7-
from audioop import byteswap
87
import io
98
import sys
109
import struct
1110

1211

1312
aifc = import_deprecated("aifc")
13+
audioop = import_deprecated("audioop")
1414

1515

1616
class AifcTest(audiotests.AudioWriteTests,
@@ -124,7 +124,7 @@ class AifcULAWTest(AifcTest, unittest.TestCase):
124124
E5040CBC 617C0A3C 08BC0A3C 2C7C0B3C 517C0E3C 8A8410FC B6840EBC 457C0A3C \
125125
""")
126126
if sys.byteorder != 'big':
127-
frames = byteswap(frames, 2)
127+
frames = audioop.byteswap(frames, 2)
128128

129129

130130
class AifcALAWTest(AifcTest, unittest.TestCase):
@@ -145,7 +145,7 @@ class AifcALAWTest(AifcTest, unittest.TestCase):
145145
E4800CC0 62000A40 08C00A40 2B000B40 52000E40 8A001180 B6000EC0 46000A40 \
146146
""")
147147
if sys.byteorder != 'big':
148-
frames = byteswap(frames, 2)
148+
frames = audioop.byteswap(frames, 2)
149149

150150

151151
class AifcMiscTest(unittest.TestCase):

Lib/test/test_audioop.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import audioop
21
import sys
2+
from test.support import warnings_helper
33
import unittest
44

5+
audioop = warnings_helper.import_deprecated("audioop")
6+
7+
58
def pack(width, data):
69
return b''.join(v.to_bytes(width, sys.byteorder, signed=True) for v in data)
710

Lib/test/test_ossaudiodev.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from test import support
2-
from test.support import import_helper
2+
from test.support import import_helper, warnings_helper
33
support.requires('audio')
44

55
from test.support import findfile
66

77
ossaudiodev = import_helper.import_module('ossaudiodev')
8+
audioop = warnings_helper.import_deprecated('audioop')
89

910
import errno
1011
import sys
1112
import sunau
1213
import time
13-
import audioop
1414
import unittest
1515

1616
# Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a

Lib/test/test_pyclbr.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ def test_others(self):
217217
cm = self.checkModule
218218

219219
# These were once some of the longest modules.
220-
cm('aifc', ignore=('_aifc_params',)) # set with = in module
221220
cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator
222221
cm('cgi', ignore=('log',)) # set with = in module
223222
cm('pickle', ignore=('partial', 'PickleBuffer'))

Lib/test/test_sunau.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import unittest
22
from test import audiotests
3-
from audioop import byteswap
43
import io
54
import struct
65
import sys
76
import sunau
7+
from test.support import warnings_helper
8+
9+
audioop = warnings_helper.import_deprecated("audioop")
810

911

1012
class SunauTest(audiotests.AudioWriteTests,
@@ -116,7 +118,7 @@ class SunauULAWTest(SunauTest, unittest.TestCase):
116118
E5040CBC 617C0A3C 08BC0A3C 2C7C0B3C 517C0E3C 8A8410FC B6840EBC 457C0A3C \
117119
""")
118120
if sys.byteorder != 'big':
119-
frames = byteswap(frames, 2)
121+
frames = audioop.byteswap(frames, 2)
120122

121123

122124
class SunauLowLevelTest(unittest.TestCase):

Lib/test/test_wave.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import unittest
22
from test import audiotests
33
from test import support
4-
from audioop import byteswap
54
import io
65
import struct
76
import sys
@@ -48,7 +47,7 @@ class WavePCM16Test(WaveTest, unittest.TestCase):
4847
E4B50CEB 63440A5A 08CA0A1F 2BBA0B0B 51460E47 8BCB113C B6F50EEA 44150A59 \
4948
""")
5049
if sys.byteorder != 'big':
51-
frames = byteswap(frames, 2)
50+
frames = wave._byteswap(frames, 2)
5251

5352

5453
class WavePCM24Test(WaveTest, unittest.TestCase):
@@ -75,7 +74,7 @@ class WavePCM24Test(WaveTest, unittest.TestCase):
7574
51486F0E44E1 8BCC64113B05 B6F4EC0EEB36 4413170A5B48 \
7675
""")
7776
if sys.byteorder != 'big':
78-
frames = byteswap(frames, 3)
77+
frames = wave._byteswap(frames, 3)
7978

8079

8180
class WavePCM32Test(WaveTest, unittest.TestCase):
@@ -102,7 +101,7 @@ class WavePCM32Test(WaveTest, unittest.TestCase):
102101
51486F800E44E190 8BCC6480113B0580 B6F4EC000EEB3630 441317800A5B48A0 \
103102
""")
104103
if sys.byteorder != 'big':
105-
frames = byteswap(frames, 4)
104+
frames = wave._byteswap(frames, 4)
106105

107106

108107
class MiscTestCase(unittest.TestCase):

Lib/wave.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373

7474
from chunk import Chunk
7575
from collections import namedtuple
76-
import audioop
7776
import builtins
7877
import struct
7978
import sys
@@ -91,6 +90,16 @@ class Error(Exception):
9190
_wave_params = namedtuple('_wave_params',
9291
'nchannels sampwidth framerate nframes comptype compname')
9392

93+
def _byteswap(data, width):
94+
swapped_data = bytearray(len(data))
95+
96+
for i in range(0, len(data), width):
97+
for j in range(width):
98+
swapped_data[i + width - 1 - j] = data[i + j]
99+
100+
return bytes(swapped_data)
101+
102+
94103
class Wave_read:
95104
"""Variables used in this class:
96105
@@ -241,7 +250,7 @@ def readframes(self, nframes):
241250
return b''
242251
data = self._data_chunk.read(nframes * self._framesize)
243252
if self._sampwidth != 1 and sys.byteorder == 'big':
244-
data = audioop.byteswap(data, self._sampwidth)
253+
data = _byteswap(data, self._sampwidth)
245254
if self._convert and data:
246255
data = self._convert(data)
247256
self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
@@ -428,7 +437,7 @@ def writeframesraw(self, data):
428437
if self._convert:
429438
data = self._convert(data)
430439
if self._sampwidth != 1 and sys.byteorder == 'big':
431-
data = audioop.byteswap(data, self._sampwidth)
440+
data = _byteswap(data, self._sampwidth)
432441
self._file.write(data)
433442
self._datawritten += len(data)
434443
self._nframeswritten = self._nframeswritten + nframes
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deprecate audioop.

Modules/audioop.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,5 +1975,12 @@ static struct PyModuleDef audioopmodule = {
19751975
PyMODINIT_FUNC
19761976
PyInit_audioop(void)
19771977
{
1978+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
1979+
"'audioop' is deprecated and slated for removal in "
1980+
"Python 3.13",
1981+
7)) {
1982+
return NULL;
1983+
}
1984+
19781985
return PyModuleDef_Init(&audioopmodule);
19791986
}

0 commit comments

Comments
 (0)