Skip to content

Commit 7aa2ec8

Browse files
committed
Parse the terminfo file header in one go
1 parent 52d1a19 commit 7aa2ec8

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

Lib/_pyrepl/terminfo.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,14 @@ def _parse_terminfo_file(self, terminal_name: str) -> None:
354354
"""
355355
data = _read_terminfo_file(terminal_name)
356356
too_short = f"TermInfo file for {terminal_name!r} too short"
357-
offset = 0
358-
if len(data) < 12:
357+
offset = 12
358+
if len(data) < offset:
359359
raise ValueError(too_short)
360360

361-
magic = struct.unpack("<H", data[offset : offset + 2])[0]
361+
magic, name_size, bool_count, num_count, str_count, str_size = (
362+
struct.unpack("<Hhhhhh", data[:offset])
363+
)
364+
362365
if magic == MAGIC16:
363366
number_format = "<h" # 16-bit signed
364367
number_size = 2
@@ -370,15 +373,6 @@ def _parse_terminfo_file(self, terminal_name: str) -> None:
370373
f"TermInfo file for {terminal_name!r} uses unknown magic"
371374
)
372375

373-
# Parse header
374-
name_size = struct.unpack("<h", data[2:4])[0]
375-
bool_count = struct.unpack("<h", data[4:6])[0]
376-
num_count = struct.unpack("<h", data[6:8])[0]
377-
str_count = struct.unpack("<h", data[8:10])[0]
378-
str_size = struct.unpack("<h", data[10:12])[0]
379-
380-
offset = 12
381-
382376
# Read terminal names
383377
if offset + name_size > len(data):
384378
raise ValueError(too_short)

0 commit comments

Comments
 (0)