Skip to content

Commit 1635fa4

Browse files
rhythmrx9terriko
andauthored
refactor: add type hints in strings.py and file.py (#1565)
part of #1539 Co-authored-by: Terri Oda <[email protected]>
1 parent 82c6814 commit 1635fa4

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

cve_bin_tool/file.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
from cve_bin_tool.async_utils import FileIO, run_coroutine
1212

1313

14-
async def aio_is_binary(filename):
14+
async def aio_is_binary(filename: str) -> bool:
1515
"""Read the magic bytes from a file and determine if it is an executable
1616
binary."""
17-
signature = await read_signature(filename)
17+
signature: bytes = await read_signature(filename)
1818
for name, method in inspect.getmembers(
1919
sys.modules[__name__], predicate=inspect.isfunction
2020
):
@@ -24,26 +24,26 @@ async def aio_is_binary(filename):
2424
return False
2525

2626

27-
def is_binary(filename):
27+
def is_binary(filename: str) -> bool:
2828
return run_coroutine(aio_is_binary(filename))
2929

3030

31-
async def read_signature(filename, length=4):
31+
async def read_signature(filename: str, length: int = 4) -> bytes:
3232
"""Read the signature, first length bytes, from filename."""
3333
async with FileIO(filename, "rb") as file_handle:
3434
return await file_handle.read(length)
3535

3636

37-
def check_elf(_filename, signature):
37+
def check_elf(_filename: str, signature: bytes) -> bool:
3838
"""Check for an ELF signature."""
3939
return signature == b"\x7f\x45\x4c\x46"
4040

4141

42-
def check_pe(_filename, signature):
42+
def check_pe(_filename: str, signature: bytes) -> bool:
4343
"""Check for windows/dos PE signature, aka 0x5a4d."""
4444
return signature[:4] == b"\x4d\x5a"
4545

4646

47-
def check_fake_test(_filename, signature):
47+
def check_fake_test(_filename: str, signature: bytes) -> bool:
4848
"""check for fake tests under windows."""
4949
return signature == b"MZ\x90\x00"

cve_bin_tool/strings.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@
77
and MacOS.
88
"""
99

10+
from typing import ClassVar, List, Set
11+
1012
from cve_bin_tool.async_utils import FileIO, run_coroutine
1113

1214

1315
class Strings:
1416
# printable characters
15-
PRINTABLE = set(range(32, 128))
17+
PRINTABLE: ClassVar[Set[int]] = set(range(32, 128))
1618
# add tab to the printable character
1719
PRINTABLE.add(9)
1820

19-
def __init__(self, filename=""):
21+
def __init__(self, filename: str = "") -> None:
2022
self.filename = filename
21-
self.output = ""
23+
self.output: str = ""
2224

23-
async def aio_parse(self):
25+
async def aio_parse(self) -> str:
2426
async with FileIO(self.filename, "rb") as f:
25-
tmp = []
27+
tmp: List[str] = []
2628
async for line in f:
2729
for char in line:
2830
# remove all unprintable characters
@@ -33,5 +35,5 @@ async def aio_parse(self):
3335
tmp = []
3436
return self.output
3537

36-
def parse(self):
38+
def parse(self) -> str:
3739
return run_coroutine(self.aio_parse())

0 commit comments

Comments
 (0)