Skip to content

Commit

Permalink
Merge pull request #7728 from radarhere/type_hints_mpeg
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jan 18, 2024
2 parents 1e00c03 + 6a85653 commit c23904a
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/PIL/MpegImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#
from __future__ import annotations

from io import BytesIO

from . import Image, ImageFile
from ._binary import i8

Expand All @@ -22,15 +24,15 @@


class BitStream:
def __init__(self, fp):
def __init__(self, fp: BytesIO) -> None:
self.fp = fp
self.bits = 0
self.bitbuffer = 0

def next(self):
def next(self) -> int:
return i8(self.fp.read(1))

def peek(self, bits):
def peek(self, bits: int) -> int:
while self.bits < bits:
c = self.next()
if c < 0:
Expand All @@ -40,13 +42,13 @@ def peek(self, bits):
self.bits += 8
return self.bitbuffer >> (self.bits - bits) & (1 << bits) - 1

def skip(self, bits):
def skip(self, bits: int) -> None:
while self.bits < bits:
self.bitbuffer = (self.bitbuffer << 8) + i8(self.fp.read(1))
self.bits += 8
self.bits = self.bits - bits

def read(self, bits):
def read(self, bits: int) -> int:
v = self.peek(bits)
self.bits = self.bits - bits
return v
Expand All @@ -61,9 +63,10 @@ class MpegImageFile(ImageFile.ImageFile):
format = "MPEG"
format_description = "MPEG"

def _open(self):
s = BitStream(self.fp)
def _open(self) -> None:
assert self.fp is not None

s = BitStream(self.fp)
if s.read(32) != 0x1B3:
msg = "not an MPEG file"
raise SyntaxError(msg)
Expand Down

0 comments on commit c23904a

Please sign in to comment.