Merge pull request #7728 from radarhere/type_hints_mpeg

This commit is contained in:
Hugo van Kemenade 2024-01-18 14:47:28 +02:00 committed by GitHub
commit c23904a4bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,6 +14,8 @@
# #
from __future__ import annotations from __future__ import annotations
from io import BytesIO
from . import Image, ImageFile from . import Image, ImageFile
from ._binary import i8 from ._binary import i8
@ -22,15 +24,15 @@ from ._binary import i8
class BitStream: class BitStream:
def __init__(self, fp): def __init__(self, fp: BytesIO) -> None:
self.fp = fp self.fp = fp
self.bits = 0 self.bits = 0
self.bitbuffer = 0 self.bitbuffer = 0
def next(self): def next(self) -> int:
return i8(self.fp.read(1)) return i8(self.fp.read(1))
def peek(self, bits): def peek(self, bits: int) -> int:
while self.bits < bits: while self.bits < bits:
c = self.next() c = self.next()
if c < 0: if c < 0:
@ -40,13 +42,13 @@ class BitStream:
self.bits += 8 self.bits += 8
return self.bitbuffer >> (self.bits - bits) & (1 << bits) - 1 return self.bitbuffer >> (self.bits - bits) & (1 << bits) - 1
def skip(self, bits): def skip(self, bits: int) -> None:
while self.bits < bits: while self.bits < bits:
self.bitbuffer = (self.bitbuffer << 8) + i8(self.fp.read(1)) self.bitbuffer = (self.bitbuffer << 8) + i8(self.fp.read(1))
self.bits += 8 self.bits += 8
self.bits = self.bits - bits self.bits = self.bits - bits
def read(self, bits): def read(self, bits: int) -> int:
v = self.peek(bits) v = self.peek(bits)
self.bits = self.bits - bits self.bits = self.bits - bits
return v return v
@ -61,9 +63,10 @@ class MpegImageFile(ImageFile.ImageFile):
format = "MPEG" format = "MPEG"
format_description = "MPEG" format_description = "MPEG"
def _open(self): def _open(self) -> None:
s = BitStream(self.fp) assert self.fp is not None
s = BitStream(self.fp)
if s.read(32) != 0x1B3: if s.read(32) != 0x1B3:
msg = "not an MPEG file" msg = "not an MPEG file"
raise SyntaxError(msg) raise SyntaxError(msg)