summaryrefslogtreecommitdiffstats
path: root/Lib/email
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2022-04-22 22:48:03 (GMT)
committerGitHub <noreply@github.com>2022-04-22 22:48:03 (GMT)
commite7929cba169349776e78ff86143b24d0122b2cdd (patch)
treeadab4f0c114c25ab829cadbbdb5d4bcbe20a2d6e /Lib/email
parent5576ddbbbc9c1d7a7819abc961e5d604ae0f7dd7 (diff)
downloadcpython-e7929cba169349776e78ff86143b24d0122b2cdd.zip
cpython-e7929cba169349776e78ff86143b24d0122b2cdd.tar.gz
cpython-e7929cba169349776e78ff86143b24d0122b2cdd.tar.bz2
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>
Diffstat (limited to 'Lib/email')
-rw-r--r--Lib/email/mime/audio.py50
1 files changed, 37 insertions, 13 deletions
diff --git a/Lib/email/mime/audio.py b/Lib/email/mime/audio.py
index 4bcd7b2..e859c2e 100644
--- a/Lib/email/mime/audio.py
+++ b/Lib/email/mime/audio.py
@@ -6,19 +6,43 @@
__all__ = ['MIMEAudio']
-import sndhdr
-
from io import BytesIO
from email import encoders
from email.mime.nonmultipart import MIMENonMultipart
-
-_sndhdr_MIMEmap = {'au' : 'basic',
- 'wav' :'x-wav',
- 'aiff':'x-aiff',
- 'aifc':'x-aiff',
- }
+_tests = []
+
+def _test_aifc_aiff(h, f):
+ if not h.startswith(b'FORM'):
+ 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. :(
# Additional ones to be added to sndhdr? midi, mp3, realaudio, wma??
@@ -31,14 +55,14 @@ def _whatsnd(data):
"""
hdr = data[:512]
fakefile = BytesIO(hdr)
- for testfn in sndhdr.tests:
+ for testfn in _tests:
res = testfn(hdr, fakefile)
if res is not None:
- return _sndhdr_MIMEmap.get(res[0])
- return None
+ return res
+ else:
+ return None
-
class MIMEAudio(MIMENonMultipart):
"""Class for generating audio/* MIME documents."""
@@ -47,7 +71,7 @@ class MIMEAudio(MIMENonMultipart):
"""Create an audio/* type MIME document.
_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.
Otherwise, you can specify the specific audio subtype via the
_subtype parameter. If _subtype is not given, and no subtype can be