File: 0002-remove-imghdr.patch

package info (click to toggle)
python-pybadges 3.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 576 kB
  • sloc: python: 864; sh: 13; makefile: 4
file content (41 lines) | stat: -rw-r--r-- 1,101 bytes parent folder | download | duplicates (2)
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
--- a/pybadges/__init__.py
+++ b/pybadges/__init__.py
@@ -30,7 +30,6 @@
 """
 
 import base64
-import imghdr
 import mimetypes
 from typing import Optional
 import urllib.parse
@@ -71,6 +70,21 @@
     'inactive': '#9f9f9f',
 }
 
+def imghdr_what(data: bytes) -> Optional[str]:
+    """Replacement for the deprecated imghdr module"""
+    if len(data) <= 32:
+        return None
+    header = data[0:32]
+    if header[6:10] in (b'JFIF', b'Exif'):
+        return 'jpeg'
+    elif header[:4] == b'\xff\xd8\xff\xdb':
+        return 'jpeg'
+    elif header.startswith(b'\211PNG\r\n\032\n'):
+        return 'png'
+    elif header[:6] in (b'GIF87a', b'GIF89a'):
+        return 'gif'
+    else:
+        return None
 
 def _remove_blanks(node):
     for x in node.childNodes:
@@ -102,7 +116,7 @@
     else:
         with open(url, 'rb') as f:
             image_data = f.read()
-        image_type = imghdr.what(None, image_data)
+        image_type = imghdr_what(image_data)
         if not image_type:
             mime_type, _ = mimetypes.guess_type(url, strict=False)
             if not mime_type: