Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-47061: deprecate audioop #32392

Merged
merged 1 commit into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ Deprecated
slated for removal in Python 3.13:

* :mod:`aifc`
* :mod:`audioop`

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

Expand Down
32 changes: 24 additions & 8 deletions Lib/aifc.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,23 +451,31 @@ def readframes(self, nframes):
#

def _alaw2lin(self, data):
import audioop
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=DeprecationWarning)
import audioop
brettcannon marked this conversation as resolved.
Show resolved Hide resolved
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
data, self._adpcmstate = audioop.adpcm2lin(data, 2, self._adpcmstate)
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):
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 7 additions & 2 deletions Lib/sunau.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"""

from collections import namedtuple
import warnings


_sunau_params = namedtuple('_sunau_params',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_aifc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_audioop.py
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_ossaudiodev.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion Lib/test/test_pyclbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_sunau.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 3 additions & 4 deletions Lib/test/test_wave.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest
from test import audiotests
from test import support
from audioop import byteswap
import io
import struct
import sys
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down
15 changes: 12 additions & 3 deletions Lib/wave.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@

from chunk import Chunk
from collections import namedtuple
import audioop
import builtins
import struct
import sys
Expand All @@ -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:

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate audioop.
7 changes: 7 additions & 0 deletions Modules/audioop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}