summaryrefslogtreecommitdiffstats
path: root/contrib/python/future/py3/patches/08-support-python-3.13.patch
blob: 9b726f25e98a587bbe2e8b2c29199cf27f49bceb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
--- contrib/python/future/py3/future/backports/email/mime/audio.py	(index)
+++ contrib/python/future/py3/future/backports/email/mime/audio.py	(working tree)
@@ -9,37 +9,11 @@ from __future__ import absolute_import
 
 __all__ = ['MIMEAudio']
 
-import sndhdr
-
 from io import BytesIO
 from future.backports.email import encoders
 from future.backports.email.mime.nonmultipart import MIMENonMultipart
 
 
-_sndhdr_MIMEmap = {'au'  : 'basic',
-                   'wav' :'x-wav',
-                   'aiff':'x-aiff',
-                   'aifc':'x-aiff',
-                   }
-
-# There are others in sndhdr that don't have MIME types. :(
-# Additional ones to be added to sndhdr? midi, mp3, realaudio, wma??
-def _whatsnd(data):
-    """Try to identify a sound file type.
-
-    sndhdr.what() has a pretty cruddy interface, unfortunately.  This is why
-    we re-do it here.  It would be easier to reverse engineer the Unix 'file'
-    command and use the standard 'magic' file, as shipped with a modern Unix.
-    """
-    hdr = data[:512]
-    fakefile = BytesIO(hdr)
-    for testfn in sndhdr.tests:
-        res = testfn(hdr, fakefile)
-        if res is not None:
-            return _sndhdr_MIMEmap.get(res[0])
-    return None
-
-
 class MIMEAudio(MIMENonMultipart):
     """Class for generating audio/* MIME documents."""
 
@@ -66,9 +40,61 @@ class MIMEAudio(MIMENonMultipart):
         header.
         """
         if _subtype is None:
-            _subtype = _whatsnd(_audiodata)
+            _subtype = _what(_audiodata)
         if _subtype is None:
             raise TypeError('Could not find audio MIME subtype')
         MIMENonMultipart.__init__(self, 'audio', _subtype, **_params)
         self.set_payload(_audiodata)
         _encoder(self)
+
+
+_rules = []
+
+
+# Originally from the sndhdr module.
+#
+# There are others in sndhdr that don't have MIME types. :(
+# Additional ones to be added to sndhdr? midi, mp3, realaudio, wma??
+def _what(data):
+    # Try to identify a sound file type.
+    #
+    # sndhdr.what() had a pretty cruddy interface, unfortunately.  This is why
+    # we re-do it here.  It would be easier to reverse engineer the Unix 'file'
+    # command and use the standard 'magic' file, as shipped with a modern Unix.
+    for testfn in _rules:
+        if res := testfn(data):
+            return res
+    else:
+        return None
+
+
+def rule(rulefunc):
+    _rules.append(rulefunc)
+    return rulefunc
+
+
+@rule
+def _aiff(h):
+    if not h.startswith(b'FORM'):
+        return None
+    if h[8:12] in {b'AIFC', b'AIFF'}:
+        return 'x-aiff'
+    else:
+        return None
+
+
+@rule
+def _au(h):
+    if h.startswith(b'.snd'):
+        return 'basic'
+    else:
+        return None
+
+
+@rule
+def _wav(h):
+    # '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"