Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added type hints to MpegImagePlugin #7728

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading