Skip to content

Commit 7c44637

Browse files
committed
Add AnalysisResult.would_block() (fixes #889)
1 parent 34d74f9 commit 7c44637

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

chess/engine.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,19 @@ async def get(self) -> InfoDict:
26012601

26022602
return info
26032603

2604+
def would_block(self) -> bool:
2605+
"""
2606+
Checks if calling :func:`~chess.engine.AnalysisResult.get()`,
2607+
calling :func:`~chess.engine.AnalysisResult.next()`,
2608+
calling :func:`~chess.engine.AnalysisResult.wait()`, or advancing the
2609+
iterator one step would require waiting for the engine.
2610+
2611+
All of these functions would return immediately information is pending
2612+
(queue is not :func:`empty <chess.engine.AnalysisResult.empty()>`)
2613+
or if the search is finished.
2614+
"""
2615+
return not self._seen_kork and self._queue.empty()
2616+
26042617
def empty(self) -> bool:
26052618
"""
26062619
Checks if all information has been consumed.
@@ -2891,6 +2904,14 @@ def wait(self) -> BestMove:
28912904
future = asyncio.run_coroutine_threadsafe(self.inner.wait(), self.simple_engine.protocol.loop)
28922905
return future.result()
28932906

2907+
def would_block(self) -> bool:
2908+
async def _would_block() -> bool:
2909+
return self.inner.would_block()
2910+
2911+
with self.simple_engine._not_shut_down():
2912+
future = asyncio.run_coroutine_threadsafe(_would_block(), self.simple_engine.protocol.loop)
2913+
return future.result()
2914+
28942915
def empty(self) -> bool:
28952916
async def _empty() -> bool:
28962917
return self.inner.empty()

test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,7 +2986,10 @@ def test_sf_analysis(self):
29862986
for info in analysis:
29872987
if "score" in info and info["score"].white() >= chess.engine.Mate(+2):
29882988
break
2989+
29892990
analysis.wait()
2991+
self.assertFalse(analysis.would_block())
2992+
29902993
self.assertEqual(analysis.info["score"].relative, chess.engine.Mate(+2))
29912994
self.assertEqual(analysis.multipv[0]["score"].black(), chess.engine.Mate(-2))
29922995

@@ -2997,6 +3000,7 @@ def test_sf_analysis(self):
29973000
was_really_empty = False
29983001
self.assertEqual(was_really_empty, was_empty)
29993002
self.assertTrue(analysis.empty())
3003+
self.assertFalse(analysis.would_block())
30003004
for info in analysis:
30013005
self.fail("all info should have been consumed")
30023006

@@ -3174,8 +3178,10 @@ async def main():
31743178
mock.expect("go infinite")
31753179
mock.expect("stop", ["bestmove e2e4"])
31763180
result = await protocol.analysis(chess.Board())
3181+
self.assertTrue(result.would_block())
31773182
result.stop()
31783183
best = await result.wait()
3184+
self.assertFalse(result.would_block())
31793185
self.assertEqual(best.move, chess.Move.from_uci("e2e4"))
31803186
self.assertTrue(best.ponder is None)
31813187
mock.assert_done()

0 commit comments

Comments
 (0)