From 519a0fd5e304db779e5dfea1c9755aaac6f98d23 Mon Sep 17 00:00:00 2001 From: Brian Curtin Date: Wed, 8 Nov 2017 17:00:38 -0500 Subject: [PATCH 1/5] Deprecate aifc.openfp aifc.openfp had pointed to aifc.open since 1993 and it was both undocumented and untested since being assigned as a matter of backwards compatibility. This change begins its formal deprecation. --- Lib/aifc.py | 5 ++++- Lib/test/test_aifc.py | 15 +++++++++++++++ .../2017-11-08-16-51-52.bpo-31985.dE_fOB.rst | 4 ++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2017-11-08-16-51-52.bpo-31985.dE_fOB.rst diff --git a/Lib/aifc.py b/Lib/aifc.py index 49a456a893ff2e..e51e8f8e484095 100644 --- a/Lib/aifc.py +++ b/Lib/aifc.py @@ -915,7 +915,10 @@ def open(f, mode=None): else: raise Error("mode must be 'r', 'rb', 'w', or 'wb'") -openfp = open # B/W compatibility +def openfp(f, mode=None): + warnings.warn("aifc.openfp is deprecated since Python 3.7. " + "Use aifc.open instead.", DeprecationWarning, stacklevel=2) + return open(f, mode=mode) if __name__ == '__main__': import sys diff --git a/Lib/test/test_aifc.py b/Lib/test/test_aifc.py index a731a5136ba5fd..2bd14f6a375779 100644 --- a/Lib/test/test_aifc.py +++ b/Lib/test/test_aifc.py @@ -7,6 +7,7 @@ import sys import struct import aifc +import warnings class AifcTest(audiotests.AudioWriteTests, @@ -145,6 +146,20 @@ class AifcALAWTest(AifcTest, unittest.TestCase): class AifcMiscTest(audiotests.AudioTests, unittest.TestCase): + + @mock.patch("aifc.open") + def test_openfp_deprecated(self, mock_open): + arg = "arg" + mode = "mode" + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + aifc.openfp(arg, mode=mode) + + self.assertTrue(len(w) == 1) + self.assertTrue(issubclass(w[0].category, DeprecationWarning)) + + mock_open.assert_called_with(arg, mode=mode) + def test_skipunknown(self): #Issue 2245 #This file contains chunk types aifc doesn't recognize. diff --git a/Misc/NEWS.d/next/Library/2017-11-08-16-51-52.bpo-31985.dE_fOB.rst b/Misc/NEWS.d/next/Library/2017-11-08-16-51-52.bpo-31985.dE_fOB.rst new file mode 100644 index 00000000000000..054f1a9e902306 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-11-08-16-51-52.bpo-31985.dE_fOB.rst @@ -0,0 +1,4 @@ +Formally deprecated aifc.openfp. Since change +7bc817d5ba917528e8bd07ec461c635291e7b06a in 1993, this had been pointing to +aifc.open as a matter of backwards compatibility, though it had been both +untested and undocumented. From ebe685d2bf95d990b0e6a907f166fb1d921cc3a9 Mon Sep 17 00:00:00 2001 From: Brian Curtin Date: Thu, 9 Nov 2017 07:25:40 -0500 Subject: [PATCH 2/5] Switch to using assertWarns By using assertWarns we no longer need to check the number of warnings or the category of the warning, as assertWarns already handles that for us. --- Lib/test/test_aifc.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Lib/test/test_aifc.py b/Lib/test/test_aifc.py index 2bd14f6a375779..40d4581fa2d57d 100644 --- a/Lib/test/test_aifc.py +++ b/Lib/test/test_aifc.py @@ -151,13 +151,9 @@ class AifcMiscTest(audiotests.AudioTests, unittest.TestCase): def test_openfp_deprecated(self, mock_open): arg = "arg" mode = "mode" - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") + with self.assertWarns(DeprecationWarning): aifc.openfp(arg, mode=mode) - self.assertTrue(len(w) == 1) - self.assertTrue(issubclass(w[0].category, DeprecationWarning)) - mock_open.assert_called_with(arg, mode=mode) def test_skipunknown(self): From 915817c960628efe0846bd4fa5c7e239d258de32 Mon Sep 17 00:00:00 2001 From: Brian Curtin Date: Thu, 9 Nov 2017 18:34:11 -0500 Subject: [PATCH 3/5] Add sunau and wave deprecations for openfp This additionally moves the test to Lib/audiotests.py and has MiscTests in each module's tests inherit from the new AudioMiscTests which does the deprecation test. --- Lib/sunau.py | 6 +++++- Lib/test/audiotests.py | 12 ++++++++++++ Lib/test/test_aifc.py | 12 ++---------- Lib/test/test_sunau.py | 4 ++++ Lib/test/test_wave.py | 4 +++- Lib/wave.py | 6 +++++- .../Library/2017-11-08-16-51-52.bpo-31985.dE_fOB.rst | 8 ++++---- 7 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Lib/sunau.py b/Lib/sunau.py index 0473e9e4ca15cd..dbad3db8392d95 100644 --- a/Lib/sunau.py +++ b/Lib/sunau.py @@ -104,6 +104,7 @@ """ from collections import namedtuple +import warnings _sunau_params = namedtuple('_sunau_params', 'nchannels sampwidth framerate nframes comptype compname') @@ -522,4 +523,7 @@ def open(f, mode=None): else: raise Error("mode must be 'r', 'rb', 'w', or 'wb'") -openfp = open +def openfp(f, mode=None): + warnings.warn("sunau.openfp is deprecated since Python 3.7. " + "Use sunau.open instead.", DeprecationWarning, stacklevel=2) + return open(f, mode=mode) diff --git a/Lib/test/audiotests.py b/Lib/test/audiotests.py index d3e8e9ee44a134..0dad01722922a9 100644 --- a/Lib/test/audiotests.py +++ b/Lib/test/audiotests.py @@ -1,6 +1,7 @@ from test.support import findfile, TESTFN, unlink import array import io +from unittest import mock import pickle @@ -49,6 +50,17 @@ def check_params(self, f, nchannels, sampwidth, framerate, nframes, self.assertEqual(pickle.loads(dump), params) +class AudioMiscTests(AudioTests): + + def test_openfp_deprecated(self): + arg = "arg" + mode = "mode" + with mock.patch(f"{self.module.__name__}.open") as mock_open, \ + self.assertWarns(DeprecationWarning): + self.module.openfp(arg, mode=mode) + mock_open.assert_called_with(arg, mode=mode) + + class AudioWriteTests(AudioTests): def create_file(self, testfile): diff --git a/Lib/test/test_aifc.py b/Lib/test/test_aifc.py index 40d4581fa2d57d..a064a324705799 100644 --- a/Lib/test/test_aifc.py +++ b/Lib/test/test_aifc.py @@ -145,16 +145,8 @@ class AifcALAWTest(AifcTest, unittest.TestCase): frames = byteswap(frames, 2) -class AifcMiscTest(audiotests.AudioTests, unittest.TestCase): - - @mock.patch("aifc.open") - def test_openfp_deprecated(self, mock_open): - arg = "arg" - mode = "mode" - with self.assertWarns(DeprecationWarning): - aifc.openfp(arg, mode=mode) - - mock_open.assert_called_with(arg, mode=mode) +class AifcMiscTest(audiotests.AudioMiscTests, unittest.TestCase): + module = aifc def test_skipunknown(self): #Issue 2245 diff --git a/Lib/test/test_sunau.py b/Lib/test/test_sunau.py index bc1f46c0ebd306..966224b1df5a0f 100644 --- a/Lib/test/test_sunau.py +++ b/Lib/test/test_sunau.py @@ -117,5 +117,9 @@ class SunauULAWTest(SunauTest, unittest.TestCase): frames = byteswap(frames, 2) +class SunauMiscTests(audiotests.AudioMiscTests, unittest.TestCase): + module = sunau + + if __name__ == "__main__": unittest.main() diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py index 8666f7269cb1f8..c5d2e02450ef57 100644 --- a/Lib/test/test_wave.py +++ b/Lib/test/test_wave.py @@ -103,7 +103,9 @@ class WavePCM32Test(WaveTest, unittest.TestCase): frames = byteswap(frames, 4) -class MiscTestCase(unittest.TestCase): +class MiscTestCase(audiotests.AudioMiscTests, unittest.TestCase): + module = wave + def test__all__(self): blacklist = {'WAVE_FORMAT_PCM'} support.check__all__(self, wave, blacklist=blacklist) diff --git a/Lib/wave.py b/Lib/wave.py index f71f7e5bf94980..cf94d5af72b4c0 100644 --- a/Lib/wave.py +++ b/Lib/wave.py @@ -87,6 +87,7 @@ class Error(Exception): import sys from chunk import Chunk from collections import namedtuple +import warnings _wave_params = namedtuple('_wave_params', 'nchannels sampwidth framerate nframes comptype compname') @@ -502,4 +503,7 @@ def open(f, mode=None): else: raise Error("mode must be 'r', 'rb', 'w', or 'wb'") -openfp = open # B/W compatibility +def openfp(f, mode=None): + warnings.warn("wave.openfp is deprecated since Python 3.7. " + "Use wave.open instead.", DeprecationWarning, stacklevel=2) + return open(f, mode=mode) diff --git a/Misc/NEWS.d/next/Library/2017-11-08-16-51-52.bpo-31985.dE_fOB.rst b/Misc/NEWS.d/next/Library/2017-11-08-16-51-52.bpo-31985.dE_fOB.rst index 054f1a9e902306..9f55ef5d9ce4f3 100644 --- a/Misc/NEWS.d/next/Library/2017-11-08-16-51-52.bpo-31985.dE_fOB.rst +++ b/Misc/NEWS.d/next/Library/2017-11-08-16-51-52.bpo-31985.dE_fOB.rst @@ -1,4 +1,4 @@ -Formally deprecated aifc.openfp. Since change -7bc817d5ba917528e8bd07ec461c635291e7b06a in 1993, this had been pointing to -aifc.open as a matter of backwards compatibility, though it had been both -untested and undocumented. +Formally deprecated aifc.openfp, sunau.openfp, and wave.openfp. Since change +7bc817d5ba917528e8bd07ec461c635291e7b06a in 1993, openfp in each of the three +modules had been pointing to that module's open funciton as a matter of +backwards compatibility, though it had been both untested and undocumented. From c90e4f43e36450ccc43e047ee083610ff4d0a9e0 Mon Sep 17 00:00:00 2001 From: Brian Curtin Date: Fri, 10 Nov 2017 10:33:47 -0500 Subject: [PATCH 4/5] Added versiondeprecated in sunau and wave docs This additionally adds a TODO in test_pyclbr around using openfp, though it shouldn't be changed until after 3.7. --- Doc/library/sunau.rst | 2 ++ Doc/library/wave.rst | 2 ++ Lib/sndhdr.py | 2 +- Lib/test/test_pyclbr.py | 2 ++ 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Doc/library/sunau.rst b/Doc/library/sunau.rst index c8357e4fcc85e2..54b28eec495d33 100644 --- a/Doc/library/sunau.rst +++ b/Doc/library/sunau.rst @@ -63,6 +63,8 @@ The :mod:`sunau` module defines the following functions: A synonym for :func:`.open`, maintained for backwards compatibility. + .. versiondeprecated:: 3.7 + The :mod:`sunau` module defines the following exception: diff --git a/Doc/library/wave.rst b/Doc/library/wave.rst index a9b3205322d7d2..5343007c3fa7b8 100644 --- a/Doc/library/wave.rst +++ b/Doc/library/wave.rst @@ -51,6 +51,8 @@ The :mod:`wave` module defines the following function and exception: A synonym for :func:`.open`, maintained for backwards compatibility. + .. versiondeprecated:: 3.7 + .. exception:: Error diff --git a/Lib/sndhdr.py b/Lib/sndhdr.py index 7ecafb40e821cd..594353136f5c37 100644 --- a/Lib/sndhdr.py +++ b/Lib/sndhdr.py @@ -160,7 +160,7 @@ def test_wav(h, f): return None f.seek(0) try: - w = wave.openfp(f, 'r') + w = wave.open(f, 'r') except (EOFError, wave.Error): return None return ('wav', w.getframerate(), w.getnchannels(), diff --git a/Lib/test/test_pyclbr.py b/Lib/test/test_pyclbr.py index 238eb71cd87a92..eaab591f74efe4 100644 --- a/Lib/test/test_pyclbr.py +++ b/Lib/test/test_pyclbr.py @@ -223,6 +223,8 @@ def test_others(self): cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator cm('cgi', ignore=('log',)) # set with = in module cm('pickle', ignore=('partial',)) + # TODO(briancurtin): openfp is deprecated as of 3.7. + # Update this once it has been removed. cm('aifc', ignore=('openfp', '_aifc_params')) # set with = in module cm('sre_parse', ignore=('dump', 'groups', 'pos')) # from sre_constants import *; property cm('pdb') From 4f8e2f419ba9431a5cde39d25481b0f42f3b1f6a Mon Sep 17 00:00:00 2001 From: Brian Curtin Date: Fri, 10 Nov 2017 11:05:46 -0500 Subject: [PATCH 5/5] Update deprecation directive to deprecated-removed These are being deprecated now for 3.7 and will be removed in 3.9. --- Doc/library/sunau.rst | 2 +- Doc/library/wave.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/sunau.rst b/Doc/library/sunau.rst index 54b28eec495d33..2064fd7e20bfc2 100644 --- a/Doc/library/sunau.rst +++ b/Doc/library/sunau.rst @@ -63,7 +63,7 @@ The :mod:`sunau` module defines the following functions: A synonym for :func:`.open`, maintained for backwards compatibility. - .. versiondeprecated:: 3.7 + .. deprecated-removed:: 3.7 3.9 The :mod:`sunau` module defines the following exception: diff --git a/Doc/library/wave.rst b/Doc/library/wave.rst index 5343007c3fa7b8..5c315c5161757c 100644 --- a/Doc/library/wave.rst +++ b/Doc/library/wave.rst @@ -51,7 +51,7 @@ The :mod:`wave` module defines the following function and exception: A synonym for :func:`.open`, maintained for backwards compatibility. - .. versiondeprecated:: 3.7 + .. deprecated-removed:: 3.7 3.9 .. exception:: Error