Skip to content

Commit

Permalink
Context manager support for pure Python Gaviota
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Oct 16, 2015
1 parent b55af43 commit 83cf049
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions chess/gaviota.py
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,12 @@ def egtb_block_park(self, egkey, block, stream):
stream.seek(i)
return i

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
self.close()


class NativeTablebases(object):
"""Provides access to Gaviota tablebases via the shared library libgtb."""
Expand Down
8 changes: 8 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2379,6 +2379,14 @@ def test_wdl(self):
board = chess.Board("8/8/1p2K3/8/8/3k4/8/8 b - - 0 1")
self.assertEqual(self.tablebases.probe_wdl(board), 1)

def test_context_manager(self):
self.assertTrue(self.tablebases.available_tables)

with self.tablebases:
pass

self.assertFalse(self.tablebases.available_tables)


if __name__ == "__main__":
if "-v" in sys.argv or "--verbose" in sys.argv:
Expand Down

0 comments on commit 83cf049

Please sign in to comment.