Skip to content

Commit

Permalink
Made small changes in the is_fen_valid function (#108)
Browse files Browse the repository at this point in the history
* Call __del__ for the temp object before returning.
* Use a hash of 1 MB instead of 16 for the temp object.
  • Loading branch information
johndoknjas authored Jul 5, 2022
1 parent 97d9c76 commit 1a6c36b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion stockfish/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def _is_fen_syntax_valid(fen: str) -> bool:
def is_fen_valid(self, fen: str) -> bool:
if not Stockfish._is_fen_syntax_valid(fen):
return False
temp_sf = Stockfish(path=self._path)
temp_sf = Stockfish(path=self._path, parameters={"Hash": 1})
# Using a new temporary SF instance, in case the fen is an illegal position that causes
# the SF process to crash.
best_move = None
Expand All @@ -411,6 +411,11 @@ def is_fen_valid(self, fen: str) -> bool:
return False
else:
return best_move is not None
finally:
temp_sf.__del__()
# Calling this function before returning from either the except or else block above.
# The __del__ function should generally be called implicitly by python when this
# temp_sf object goes out of scope, but calling it explicitly guarantees this will happen.

def is_move_correct(self, move_value: str) -> bool:
"""Checks new move.
Expand Down
4 changes: 2 additions & 2 deletions tests/stockfish/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ def test_invalid_fen_king_attacked(self, stockfish, fen):
# Since for that FEN, SF 15 actually outputs a best move without crashing (unlike SF 14 and earlier).
return
assert not stockfish.is_fen_valid(fen)
assert Stockfish._del_counter == old_del_counter + 1
assert Stockfish._del_counter == old_del_counter + 2

stockfish.set_fen_position(fen)
with pytest.raises(StockfishException):
Expand Down Expand Up @@ -921,7 +921,7 @@ def test_is_fen_valid(self, stockfish):
assert not stockfish.is_fen_valid(invalid_syntax_fen)
assert stockfish._is_fen_syntax_valid(correct_fen)
assert not stockfish._is_fen_syntax_valid(invalid_syntax_fen)
assert Stockfish._del_counter == old_del_counter + 1
assert Stockfish._del_counter == old_del_counter + 2

time.sleep(2.0)
assert stockfish._stockfish.poll() is None
Expand Down

0 comments on commit 1a6c36b

Please sign in to comment.