[Python-checkins] Deprecate audioop (GH-32392)

brettcannon webhook-mailer at python.org
Thu Apr 7 15:27:44 EDT 2022


https://github.com/python/cpython/commit/87eec70d97b250f820325b4f1b4f781b443b5180
commit: 87eec70d97b250f820325b4f1b4f781b443b5180
branch: main
author: Brett Cannon <brett at python.org>
committer: brettcannon <brett at python.org>
date: 2022-04-07T12:27:35-07:00
summary:

Deprecate audioop (GH-32392)

files:
A Misc/NEWS.d/next/Library/2022-04-06-18-01-28.bpo-47061.qoVTR9.rst
M Doc/whatsnew/3.11.rst
M Lib/aifc.py
M Lib/sunau.py
M Lib/test/test_aifc.py
M Lib/test/test_audioop.py
M Lib/test/test_ossaudiodev.py
M Lib/test/test_pyclbr.py
M Lib/test/test_sunau.py
M Lib/test/test_wave.py
M Lib/wave.py
M Modules/audioop.c

diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst
index 31cdf10f66656..bc4a1953c10c2 100644
--- a/Doc/whatsnew/3.11.rst
+++ b/Doc/whatsnew/3.11.rst
@@ -833,6 +833,7 @@ Deprecated
   slated for removal in Python 3.13:
 
   * :mod:`aifc`
+  * :mod:`audioop`
 
   (Contributed by Brett Cannon in :issue:`47061`.)
 
diff --git a/Lib/aifc.py b/Lib/aifc.py
index b5eab9215d6ef..314cfd230d6cb 100644
--- a/Lib/aifc.py
+++ b/Lib/aifc.py
@@ -451,15 +451,21 @@ def readframes(self, nframes):
     #
 
     def _alaw2lin(self, data):
-        import audioop
+        with warnings.catch_warnings():
+            warnings.simplefilter('ignore', category=DeprecationWarning)
+            import audioop
         return audioop.alaw2lin(data, 2)
 
     def _ulaw2lin(self, data):
-        import audioop
+        with warnings.catch_warnings():
+            warnings.simplefilter('ignore', category=DeprecationWarning)
+            import audioop
         return audioop.ulaw2lin(data, 2)
 
     def _adpcm2lin(self, data):
-        import audioop
+        with warnings.catch_warnings():
+            warnings.simplefilter('ignore', category=DeprecationWarning)
+            import audioop
         if not hasattr(self, '_adpcmstate'):
             # first time
             self._adpcmstate = None
@@ -467,7 +473,9 @@ def _adpcm2lin(self, data):
         return data
 
     def _sowt2lin(self, data):
-        import audioop
+        with warnings.catch_warnings():
+            warnings.simplefilter('ignore', category=DeprecationWarning)
+            import audioop
         return audioop.byteswap(data, 2)
 
     def _read_comm_chunk(self, chunk):
@@ -774,22 +782,30 @@ def close(self):
     #
 
     def _lin2alaw(self, data):
-        import audioop
+        with warnings.catch_warnings():
+            warnings.simplefilter('ignore', category=DeprecationWarning)
+            import audioop
         return audioop.lin2alaw(data, 2)
 
     def _lin2ulaw(self, data):
-        import audioop
+        with warnings.catch_warnings():
+            warnings.simplefilter('ignore', category=DeprecationWarning)
+            import audioop
         return audioop.lin2ulaw(data, 2)
 
     def _lin2adpcm(self, data):
-        import audioop
+        with warnings.catch_warnings():
+            warnings.simplefilter('ignore', category=DeprecationWarning)
+            import audioop
         if not hasattr(self, '_adpcmstate'):
             self._adpcmstate = None
         data, self._adpcmstate = audioop.lin2adpcm(data, 2, self._adpcmstate)
         return data
 
     def _lin2sowt(self, data):
-        import audioop
+        with warnings.catch_warnings():
+            warnings.simplefilter('ignore', category=DeprecationWarning)
+            import audioop
         return audioop.byteswap(data, 2)
 
     def _ensure_header_written(self, datasize):
diff --git a/Lib/sunau.py b/Lib/sunau.py
index 79750a9d23ebd..9b3533d930676 100644
--- a/Lib/sunau.py
+++ b/Lib/sunau.py
@@ -104,6 +104,7 @@
 """
 
 from collections import namedtuple
+import warnings
 
 
 _sunau_params = namedtuple('_sunau_params',
@@ -275,7 +276,9 @@ def readframes(self, nframes):
                 data = self._file.read(nframes * self._framesize)
             self._soundpos += len(data) // self._framesize
             if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
-                import audioop
+                with warnings.catch_warnings():
+                    warnings.simplefilter('ignore', category=DeprecationWarning)
+                    import audioop
                 data = audioop.ulaw2lin(data, self._sampwidth)
             return data
         return None             # XXX--not implemented yet
@@ -421,7 +424,9 @@ def writeframesraw(self, data):
             data = memoryview(data).cast('B')
         self._ensure_header_written()
         if self._comptype == 'ULAW':
-            import audioop
+            with warnings.catch_warnings():
+                warnings.simplefilter('ignore', category=DeprecationWarning)
+                import audioop
             data = audioop.lin2ulaw(data, self._sampwidth)
         nframes = len(data) // self._framesize
         self._file.write(data)
diff --git a/Lib/test/test_aifc.py b/Lib/test/test_aifc.py
index ad8a7ee053cca..d3863d4915d44 100644
--- a/Lib/test/test_aifc.py
+++ b/Lib/test/test_aifc.py
@@ -4,13 +4,13 @@
 import unittest
 from unittest import mock
 from test import audiotests
-from audioop import byteswap
 import io
 import sys
 import struct
 
 
 aifc = import_deprecated("aifc")
+audioop = import_deprecated("audioop")
 
 
 class AifcTest(audiotests.AudioWriteTests,
@@ -124,7 +124,7 @@ class AifcULAWTest(AifcTest, unittest.TestCase):
       E5040CBC 617C0A3C 08BC0A3C 2C7C0B3C 517C0E3C 8A8410FC B6840EBC 457C0A3C \
       """)
     if sys.byteorder != 'big':
-        frames = byteswap(frames, 2)
+        frames = audioop.byteswap(frames, 2)
 
 
 class AifcALAWTest(AifcTest, unittest.TestCase):
@@ -145,7 +145,7 @@ class AifcALAWTest(AifcTest, unittest.TestCase):
       E4800CC0 62000A40 08C00A40 2B000B40 52000E40 8A001180 B6000EC0 46000A40 \
       """)
     if sys.byteorder != 'big':
-        frames = byteswap(frames, 2)
+        frames = audioop.byteswap(frames, 2)
 
 
 class AifcMiscTest(unittest.TestCase):
diff --git a/Lib/test/test_audioop.py b/Lib/test/test_audioop.py
index 9baa62ad45c04..05c0f20e122a4 100644
--- a/Lib/test/test_audioop.py
+++ b/Lib/test/test_audioop.py
@@ -1,7 +1,10 @@
-import audioop
 import sys
+from test.support import warnings_helper
 import unittest
 
+audioop = warnings_helper.import_deprecated("audioop")
+
+
 def pack(width, data):
     return b''.join(v.to_bytes(width, sys.byteorder, signed=True) for v in data)
 
diff --git a/Lib/test/test_ossaudiodev.py b/Lib/test/test_ossaudiodev.py
index ebce3e9c272f9..37d2d1f5ff441 100644
--- a/Lib/test/test_ossaudiodev.py
+++ b/Lib/test/test_ossaudiodev.py
@@ -1,16 +1,16 @@
 from test import support
-from test.support import import_helper
+from test.support import import_helper, warnings_helper
 support.requires('audio')
 
 from test.support import findfile
 
 ossaudiodev = import_helper.import_module('ossaudiodev')
+audioop = warnings_helper.import_deprecated('audioop')
 
 import errno
 import sys
 import sunau
 import time
-import audioop
 import unittest
 
 # Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a
diff --git a/Lib/test/test_pyclbr.py b/Lib/test/test_pyclbr.py
index 329acf0c64295..ad7b31aef1ddd 100644
--- a/Lib/test/test_pyclbr.py
+++ b/Lib/test/test_pyclbr.py
@@ -217,7 +217,6 @@ def test_others(self):
         cm = self.checkModule
 
         # These were once some of the longest modules.
-        cm('aifc', ignore=('_aifc_params',))  # set with = in module
         cm('random', ignore=('Random',))  # from _random import Random as CoreGenerator
         cm('cgi', ignore=('log',))      # set with = in module
         cm('pickle', ignore=('partial', 'PickleBuffer'))
diff --git a/Lib/test/test_sunau.py b/Lib/test/test_sunau.py
index 7f1c0a5cbdeda..e65742b69f2e9 100644
--- a/Lib/test/test_sunau.py
+++ b/Lib/test/test_sunau.py
@@ -1,10 +1,12 @@
 import unittest
 from test import audiotests
-from audioop import byteswap
 import io
 import struct
 import sys
 import sunau
+from test.support import warnings_helper
+
+audioop = warnings_helper.import_deprecated("audioop")
 
 
 class SunauTest(audiotests.AudioWriteTests,
@@ -116,7 +118,7 @@ class SunauULAWTest(SunauTest, unittest.TestCase):
       E5040CBC 617C0A3C 08BC0A3C 2C7C0B3C 517C0E3C 8A8410FC B6840EBC 457C0A3C \
       """)
     if sys.byteorder != 'big':
-        frames = byteswap(frames, 2)
+        frames = audioop.byteswap(frames, 2)
 
 
 class SunauLowLevelTest(unittest.TestCase):
diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py
index f85e40b31d010..0cc94e88b437d 100644
--- a/Lib/test/test_wave.py
+++ b/Lib/test/test_wave.py
@@ -1,7 +1,6 @@
 import unittest
 from test import audiotests
 from test import support
-from audioop import byteswap
 import io
 import struct
 import sys
@@ -48,7 +47,7 @@ class WavePCM16Test(WaveTest, unittest.TestCase):
       E4B50CEB 63440A5A 08CA0A1F 2BBA0B0B 51460E47 8BCB113C B6F50EEA 44150A59 \
       """)
     if sys.byteorder != 'big':
-        frames = byteswap(frames, 2)
+        frames = wave._byteswap(frames, 2)
 
 
 class WavePCM24Test(WaveTest, unittest.TestCase):
@@ -75,7 +74,7 @@ class WavePCM24Test(WaveTest, unittest.TestCase):
       51486F0E44E1 8BCC64113B05 B6F4EC0EEB36 4413170A5B48 \
       """)
     if sys.byteorder != 'big':
-        frames = byteswap(frames, 3)
+        frames = wave._byteswap(frames, 3)
 
 
 class WavePCM32Test(WaveTest, unittest.TestCase):
@@ -102,7 +101,7 @@ class WavePCM32Test(WaveTest, unittest.TestCase):
       51486F800E44E190 8BCC6480113B0580 B6F4EC000EEB3630 441317800A5B48A0 \
       """)
     if sys.byteorder != 'big':
-        frames = byteswap(frames, 4)
+        frames = wave._byteswap(frames, 4)
 
 
 class MiscTestCase(unittest.TestCase):
diff --git a/Lib/wave.py b/Lib/wave.py
index b7071198e6b84..47a233df0a3ab 100644
--- a/Lib/wave.py
+++ b/Lib/wave.py
@@ -73,7 +73,6 @@
 
 from chunk import Chunk
 from collections import namedtuple
-import audioop
 import builtins
 import struct
 import sys
@@ -91,6 +90,16 @@ class Error(Exception):
 _wave_params = namedtuple('_wave_params',
                      'nchannels sampwidth framerate nframes comptype compname')
 
+def _byteswap(data, width):
+    swapped_data = bytearray(len(data))
+
+    for i in range(0, len(data), width):
+        for j in range(width):
+            swapped_data[i + width - 1 - j] = data[i + j]
+
+    return bytes(swapped_data)
+
+
 class Wave_read:
     """Variables used in this class:
 
@@ -241,7 +250,7 @@ def readframes(self, nframes):
             return b''
         data = self._data_chunk.read(nframes * self._framesize)
         if self._sampwidth != 1 and sys.byteorder == 'big':
-            data = audioop.byteswap(data, self._sampwidth)
+            data = _byteswap(data, self._sampwidth)
         if self._convert and data:
             data = self._convert(data)
         self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
@@ -428,7 +437,7 @@ def writeframesraw(self, data):
         if self._convert:
             data = self._convert(data)
         if self._sampwidth != 1 and sys.byteorder == 'big':
-            data = audioop.byteswap(data, self._sampwidth)
+            data = _byteswap(data, self._sampwidth)
         self._file.write(data)
         self._datawritten += len(data)
         self._nframeswritten = self._nframeswritten + nframes
diff --git a/Misc/NEWS.d/next/Library/2022-04-06-18-01-28.bpo-47061.qoVTR9.rst b/Misc/NEWS.d/next/Library/2022-04-06-18-01-28.bpo-47061.qoVTR9.rst
new file mode 100644
index 0000000000000..65ffa2e180791
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-04-06-18-01-28.bpo-47061.qoVTR9.rst
@@ -0,0 +1 @@
+Deprecate audioop.
diff --git a/Modules/audioop.c b/Modules/audioop.c
index 32237ca6177cd..d74e634ac4488 100644
--- a/Modules/audioop.c
+++ b/Modules/audioop.c
@@ -1975,5 +1975,12 @@ static struct PyModuleDef audioopmodule = {
 PyMODINIT_FUNC
 PyInit_audioop(void)
 {
+    if (PyErr_WarnEx(PyExc_DeprecationWarning,
+                     "'audioop' is deprecated and slated for removal in "
+                     "Python 3.13",
+                     7)) {
+        return NULL;
+    }
+
     return PyModuleDef_Init(&audioopmodule);
 }



More information about the Python-checkins mailing list