Deprecate audioop (GH-32392)

This commit is contained in:
Brett Cannon 2022-04-07 12:27:35 -07:00 committed by GitHub
parent 1df4298b62
commit 87eec70d97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 68 additions and 26 deletions

View File

@ -833,6 +833,7 @@ Deprecated
slated for removal in Python 3.13:
* :mod:`aifc`
* :mod:`audioop`
(Contributed by Brett Cannon in :issue:`47061`.)

View File

@ -451,15 +451,21 @@ class Aifc_read:
#
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 @@ class Aifc_read:
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 @@ class Aifc_write:
#
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):

View File

@ -104,6 +104,7 @@ is destroyed.
"""
from collections import namedtuple
import warnings
_sunau_params = namedtuple('_sunau_params',
@ -275,7 +276,9 @@ class Au_read:
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 @@ class Au_write:
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)

View File

@ -4,13 +4,13 @@ from test.support.warnings_helper import check_no_resource_warning, import_depre
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):

View File

@ -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)

View File

@ -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

View File

@ -217,7 +217,6 @@ class PyclbrTest(TestCase):
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'))

View File

@ -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):

View File

@ -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):

View File

@ -73,7 +73,6 @@ is destroyed.
from chunk import Chunk
from collections import namedtuple
import audioop
import builtins
import struct
import sys
@ -91,6 +90,16 @@ _array_fmts = None, 'b', 'h', None, 'i'
_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 @@ class Wave_read:
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 @@ class Wave_write:
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

View File

@ -0,0 +1 @@
Deprecate audioop.

View File

@ -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);
}