mirror of
https://github.com/python/cpython.git
synced 2025-10-17 20:28:43 +00:00
gh-91217: deprecate-sndhdr (#91806)
Also inline necessary functionality from `sndhdr` into `email.mime.audio` for `MIMEAudio`. Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
This commit is contained in:
parent
5576ddbbbc
commit
e7929cba16
6 changed files with 49 additions and 16 deletions
|
@ -146,7 +146,7 @@ Here are the classes:
|
||||||
A subclass of :class:`~email.mime.nonmultipart.MIMENonMultipart`, the
|
A subclass of :class:`~email.mime.nonmultipart.MIMENonMultipart`, the
|
||||||
:class:`MIMEAudio` class is used to create MIME message objects of major type
|
:class:`MIMEAudio` class is used to create MIME message objects of major type
|
||||||
:mimetype:`audio`. *_audiodata* is a string containing the raw audio data. If
|
:mimetype:`audio`. *_audiodata* is a string containing the raw audio data. If
|
||||||
this data can be decoded by the standard Python module :mod:`sndhdr`, then the
|
this data can be decoded as au, wav, aiff, or aifc, then the
|
||||||
subtype will be automatically included in the :mailheader:`Content-Type` header.
|
subtype will be automatically included in the :mailheader:`Content-Type` header.
|
||||||
Otherwise you can explicitly specify the audio subtype via the *_subtype*
|
Otherwise you can explicitly specify the audio subtype via the *_subtype*
|
||||||
argument. If the minor type could not be guessed and *_subtype* was not given,
|
argument. If the minor type could not be guessed and *_subtype* was not given,
|
||||||
|
|
|
@ -920,6 +920,7 @@ Deprecated
|
||||||
* :mod:`nntplib`
|
* :mod:`nntplib`
|
||||||
* :mod:`ossaudiodev`
|
* :mod:`ossaudiodev`
|
||||||
* :mod:`pipes`
|
* :mod:`pipes`
|
||||||
|
* :mod:`sndhdr`
|
||||||
|
|
||||||
(Contributed by Brett Cannon in :issue:`47061`.)
|
(Contributed by Brett Cannon in :issue:`47061`.)
|
||||||
|
|
||||||
|
|
|
@ -6,19 +6,43 @@
|
||||||
|
|
||||||
__all__ = ['MIMEAudio']
|
__all__ = ['MIMEAudio']
|
||||||
|
|
||||||
import sndhdr
|
|
||||||
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from email import encoders
|
from email import encoders
|
||||||
from email.mime.nonmultipart import MIMENonMultipart
|
from email.mime.nonmultipart import MIMENonMultipart
|
||||||
|
|
||||||
|
|
||||||
|
_tests = []
|
||||||
_sndhdr_MIMEmap = {'au' : 'basic',
|
|
||||||
'wav' :'x-wav',
|
def _test_aifc_aiff(h, f):
|
||||||
'aiff':'x-aiff',
|
if not h.startswith(b'FORM'):
|
||||||
'aifc':'x-aiff',
|
return None
|
||||||
}
|
if h[8:12] in {b'AIFC', b'AIFF'}:
|
||||||
|
return 'x-aiff'
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
_tests.append(_test_aifc_aiff)
|
||||||
|
|
||||||
|
|
||||||
|
def _test_au(h, f):
|
||||||
|
if h.startswith(b'.snd'):
|
||||||
|
return 'basic'
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
_tests.append(_test_au)
|
||||||
|
|
||||||
|
|
||||||
|
def _test_wav(h, f):
|
||||||
|
import wave
|
||||||
|
# 'RIFF' <len> 'WAVE' 'fmt ' <len>
|
||||||
|
if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return "x-wav"
|
||||||
|
|
||||||
|
_tests.append(_test_wav)
|
||||||
|
|
||||||
|
|
||||||
# There are others in sndhdr that don't have MIME types. :(
|
# There are others in sndhdr that don't have MIME types. :(
|
||||||
# Additional ones to be added to sndhdr? midi, mp3, realaudio, wma??
|
# Additional ones to be added to sndhdr? midi, mp3, realaudio, wma??
|
||||||
|
@ -31,14 +55,14 @@ def _whatsnd(data):
|
||||||
"""
|
"""
|
||||||
hdr = data[:512]
|
hdr = data[:512]
|
||||||
fakefile = BytesIO(hdr)
|
fakefile = BytesIO(hdr)
|
||||||
for testfn in sndhdr.tests:
|
for testfn in _tests:
|
||||||
res = testfn(hdr, fakefile)
|
res = testfn(hdr, fakefile)
|
||||||
if res is not None:
|
if res is not None:
|
||||||
return _sndhdr_MIMEmap.get(res[0])
|
return res
|
||||||
return None
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MIMEAudio(MIMENonMultipart):
|
class MIMEAudio(MIMENonMultipart):
|
||||||
"""Class for generating audio/* MIME documents."""
|
"""Class for generating audio/* MIME documents."""
|
||||||
|
|
||||||
|
@ -47,7 +71,7 @@ class MIMEAudio(MIMENonMultipart):
|
||||||
"""Create an audio/* type MIME document.
|
"""Create an audio/* type MIME document.
|
||||||
|
|
||||||
_audiodata is a string containing the raw audio data. If this data
|
_audiodata is a string containing the raw audio data. If this data
|
||||||
can be decoded by the standard Python `sndhdr' module, then the
|
can be decoded as au, wav, aiff, or aifc, then the
|
||||||
subtype will be automatically included in the Content-Type header.
|
subtype will be automatically included in the Content-Type header.
|
||||||
Otherwise, you can specify the specific audio subtype via the
|
Otherwise, you can specify the specific audio subtype via the
|
||||||
_subtype parameter. If _subtype is not given, and no subtype can be
|
_subtype parameter. If _subtype is not given, and no subtype can be
|
||||||
|
|
|
@ -27,13 +27,16 @@ option -r tells it to recurse down directories found inside
|
||||||
explicitly given directories.
|
explicitly given directories.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
warnings._deprecated(__name__, remove=(3, 13))
|
||||||
|
|
||||||
# The file structure is top-down except that the test program and its
|
# The file structure is top-down except that the test program and its
|
||||||
# subroutine come last.
|
# subroutine come last.
|
||||||
|
|
||||||
__all__ = ['what', 'whathdr']
|
__all__ = ['what', 'whathdr']
|
||||||
|
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
import warnings
|
|
||||||
|
|
||||||
SndHeaders = namedtuple('SndHeaders',
|
SndHeaders = namedtuple('SndHeaders',
|
||||||
'filetype framerate nchannels nframes sampwidth')
|
'filetype framerate nchannels nframes sampwidth')
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
import sndhdr
|
|
||||||
import pickle
|
import pickle
|
||||||
import unittest
|
import unittest
|
||||||
from test.support import findfile
|
from test.support import findfile
|
||||||
|
from test.support import warnings_helper
|
||||||
|
|
||||||
|
sndhdr = warnings_helper.import_deprecated("sndhdr")
|
||||||
|
|
||||||
|
|
||||||
class TestFormats(unittest.TestCase):
|
class TestFormats(unittest.TestCase):
|
||||||
def test_data(self):
|
def test_data(self):
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Deprecate the sndhdr module, as well as inline needed functionality for
|
||||||
|
``email.mime.MIMEAudio``.
|
Loading…
Add table
Add a link
Reference in a new issue