Skip to content

Commit 1aa08dc

Browse files
author
gaoflow
committed
pgn: accept tag pairs with leading whitespace (fixes #1115)
The PGN standard specifies that whitespace is not significant. `read_game()` was checking `line.startswith("[")` and matching `TAG_REGEX` against the raw line, both of which fail when a tag pair is preceded by horizontal whitespace (e.g. a file that begins with " [Event ...]"). Strip leading whitespace before the `startswith` guard and the regex match so that indented tag pairs are recognised correctly.
1 parent 8330cfd commit 1aa08dc

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

chess/pgn.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,13 +1636,14 @@ def read_game(handle: TextIO, *, Visitor: Any = GameBuilder) -> Any:
16361636
if not isinstance(managed_headers, Headers):
16371637
unmanaged_headers = Headers({})
16381638

1639-
if not line.startswith("["):
1639+
stripped = line.lstrip()
1640+
if not stripped.startswith("["):
16401641
break
16411642

16421643
consecutive_empty_lines = 0
16431644

16441645
if not skipping_game:
1645-
tag_match = TAG_REGEX.match(line)
1646+
tag_match = TAG_REGEX.match(stripped)
16461647
if tag_match:
16471648
visitor.visit_header(tag_match.group(1), tag_match.group(2))
16481649
if unmanaged_headers is not None:

0 commit comments

Comments
 (0)