Don't use the deprecated imghdr module

This commit is contained in:
pagedown 2023-03-01 12:03:56 +08:00
parent cd8bb462c3
commit 08c0321fc4
No known key found for this signature in database
GPG Key ID: E921CF18AC8FF6EB
3 changed files with 12 additions and 5 deletions

View File

@ -1,12 +1,12 @@
#!/usr/bin/env python #!/usr/bin/env python
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
import imghdr
import os import os
from base64 import standard_b64decode, standard_b64encode from base64 import standard_b64decode, standard_b64encode
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING, Optional
from kitty.types import AsyncResponse from kitty.types import AsyncResponse
from kitty.utils import is_png
from .base import ( from .base import (
MATCH_WINDOW_OPTION, MATCH_WINDOW_OPTION,
@ -89,7 +89,7 @@ failed, the command will exit with a success code.
if path.lower() == 'none': if path.lower() == 'none':
ret['data'] = '-' ret['data'] = '-'
return ret return ret
if imghdr.what(path) != 'png': if not is_png(path):
self.fatal(f'{path} is not a PNG image') self.fatal(f'{path} is not a PNG image')
def file_pipe(path: str) -> CmdGenerator: def file_pipe(path: str) -> CmdGenerator:

View File

@ -1,13 +1,12 @@
#!/usr/bin/env python #!/usr/bin/env python
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
import imghdr
import os import os
from base64 import standard_b64decode, standard_b64encode from base64 import standard_b64decode, standard_b64encode
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING, Optional
from kitty.types import AsyncResponse from kitty.types import AsyncResponse
from kitty.utils import is_png
from .base import ( from .base import (
MATCH_WINDOW_OPTION, MATCH_WINDOW_OPTION,
@ -85,7 +84,7 @@ failed, the command will exit with a success code.
if path.lower() == 'none': if path.lower() == 'none':
ret['data'] = '-' ret['data'] = '-'
return ret return ret
if imghdr.what(path) != 'png': if not is_png(path):
self.fatal(f'{path} is not a PNG image') self.fatal(f'{path} is not a PNG image')
def file_pipe(path: str) -> CmdGenerator: def file_pipe(path: str) -> CmdGenerator:

View File

@ -1137,3 +1137,11 @@ def extract_all_from_tarfile_safely(tf: 'tarfile.TarFile', dest: str) -> None:
tar.extractall(path, tar.getmembers(), numeric_owner=numeric_owner) tar.extractall(path, tar.getmembers(), numeric_owner=numeric_owner)
safe_extract(tf, dest) safe_extract(tf, dest)
def is_png(path: str) -> bool:
if path:
with suppress(Exception), open(path, 'rb') as f:
header = f.read(8)
return header.startswith(b'\211PNG\r\n\032\n')
return False