Skip to content

Commit

Permalink
Keep variation stack in bound during PGN parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Oct 14, 2014
1 parent c2ac13e commit b364bd5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions chess/pgn.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,9 +593,11 @@ def read_game(handle):

in_variation = False
elif token == ")":
# Found a close variation token.
variation_stack.pop()
board_stack.pop()
# Found a close variation token. Always leave at least the
# root node on the stack.
if len(variation_stack) > 1:
variation_stack.pop()
board_stack.pop()
elif token in ["1-0", "0-1", "1/2-1/2", "*"] and len(variation_stack) == 1:
# Found a result token.
found_content = True
Expand Down
6 changes: 6 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,12 @@ def test_comment_at_eol(self):
self.assertTrue(5 in node.variation(1).nags)
self.assertEqual(node.variation(1).comment, "/\\ Ne7, c6")

def test_variation_stack(self):
pgn = StringIO("1. e4 (1. d4))) !? *")
game = chess.pgn.read_game(pgn)
self.assertEqual(game.variation(0).move, chess.Move.from_uci("e2e4"))
self.assertEqual(game.variation(1).move, chess.Move.from_uci("d2d4"))

def test_annotation_symbols(self):
pgn = StringIO("1. b4?! g6 2. Bb2 Nc6? 3. Bxh8!!")
game = chess.pgn.read_game(pgn)
Expand Down

0 comments on commit b364bd5

Please sign in to comment.