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

Applied typing to pwexplode.py #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 12 additions & 10 deletions pwexplode.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
# called hundred or thousand times per stream.

# Import stuff
import types, typing
import struct
import inspect

Expand All @@ -52,14 +53,15 @@

# Quick & dirty debug printing function; prints only if debugflag is True; gets line number and everything from
# the currentframes _parent_ (f_back!)
def debug_print(text):
def debug_print(text: str) -> None:
if debugflag:
cf = inspect.currentframe()
print("pwexplode.%s(), %d: %s" % (inspect.getframeinfo(cf.f_back)[2], cf.f_back.f_lineno, text))
cf = typing.cast(types.FrameType, inspect.currentframe())
bf = typing.cast(types.FrameType, cf.f_back)
print("pwexplode.%s(), %d: %s" % (inspect.getframeinfo(bf)[2], bf.f_lineno, text))


# Checks if a string just consists of 0 and 1
def is_bitstring(bitstring):
def is_bitstring(bitstring: str) -> bool:
# bitstring should be a string...
if type(bitstring) is not str:
raise RuntimeError("is_bitstring(bitstring): bitstring is not a str but %s.", type(bitstring))
Expand All @@ -74,7 +76,7 @@ def is_bitstring(bitstring):


# This gives back the literals; returns a tuple of code and error; error is -1 if nothing is found
def get_literals(bitstring):
def get_literals(bitstring: str) -> typing.Tuple[int, int]:
# bitstring should be a string...
if type(bitstring) is not str:
raise RuntimeError("get_literals(bitstring): bitstring is not a str but %s.", type(bitstring))
Expand Down Expand Up @@ -153,7 +155,7 @@ def get_literals(bitstring):


# This gives back the copy-length; returns a tuple of length and error; error is -1 if nothing is found
def get_copylength(bitstring):
def get_copylength(bitstring: str) -> typing.Tuple[int, int]:
# bitstring should be a string...
if type(bitstring) is not str:
raise RuntimeError("get_copylength(bitstring): bitstring is not a str but %s.", type(bitstring))
Expand Down Expand Up @@ -297,7 +299,7 @@ def get_copylength(bitstring):


# This gives back the copy-offset; returns a tuple of offset and error; error is -1 if nothing is found
def get_copyoffset(bitstring):
def get_copyoffset(bitstring: str) -> typing.Tuple[int, int]:
# bitstring should be a string...
if type(bitstring) is not str:
raise RuntimeError("get_copyoffset(bitstring): bitstring is not a str but %s.", type(bitstring))
Expand Down Expand Up @@ -329,7 +331,7 @@ def get_copyoffset(bitstring):


# This function takes a compressed bytestring and decompresses it; returns the uncompressed data if successful
def explode(compressedstring):
def explode(compressedstring: bytes) -> bytes:
# compressedstring should be a string...
if type(compressedstring) is not bytes:
raise RuntimeError("explode(compressedstring): compressedstring is not of type 'bytes' but %s." % type(compressedstring))
Expand All @@ -356,8 +358,8 @@ def explode(compressedstring):
bitstream = bitstream[16:]

# Print
debug_print("Compressed data (%d bytes) is '%s'" % (len(compressedstring), compressedstring))
debug_print("Bitstream data (%d bits) is '%s'" % (len(bitstream), bitstream))
debug_print("Compressed data (%d bytes) is '%r'" % (len(compressedstring), compressedstring))
debug_print("Bitstream data (%d bits) is '%r'" % (len(bitstream), bitstream))

# Start decompression
debug_print("Starting decompression...")
Expand Down