Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust parsing of major version number to account for development builds #12

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,12 @@ Do not use this command during a search!
### Get current major version of stockfish engine
E.g., if the engine being used is Stockfish 14.1 or Stockfish 14, then the function would return 14.
Meanwhile, if a development build of the engine is being used (not an official release), then the function returns an
int with 5 or 6 digits, representing the date the engine was compiled on.
For example, 20122 is returned for the development build compiled on January 2, 2022.
int, representing the date the engine was compiled on. There were two different versioning systems.
Either it is a 5 or 6 digit long number with *ddmmyy* or an 8 digit long with *yyyymmdd*.
For example, 20122 is returned for the development build compiled on January 2, 2022 and 20230329 is for the build on March 29, 2023.

A return value of -1 from this function means that something did not work when parsing the version number.
If this happens to you, please open an issue and specify which release you used so we can fix the problem.
```python
stockfish.get_stockfish_major_version()
```
Expand Down
23 changes: 20 additions & 3 deletions stockfish/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def __init__(

self._has_quit_command_been_sent = False

self._stockfish_major_version: int = int(
self._read_line().split(" ")[1].split(".")[0].replace("-", "")
self._stockfish_major_version: int = self._extract_major_version(
self._read_line()
)

self._put("uci")
Expand Down Expand Up @@ -141,6 +141,23 @@ def reset_engine_parameters(self) -> None:
"""
self.update_engine_parameters(self._DEFAULT_STOCKFISH_PARAMS)

def _extract_major_version(self, output_line: str) -> int:
# To prevent the wrapper from not working at all,
# -1 is used as version number in case of problems
try:
version_specifier = output_line.split(" ")[1]
# Case: dev-20230329-3f01e3f4
if "dev" in version_specifier:
return int(version_specifier.split("-")[1])
# Case: 15.1
if "." in version_specifier:
return int(version_specifier.split(".")[0].replace("-", ""))
# Case: 200222
return int(version_specifier.replace("-", ""))

except ValueError:
return -1

def _prepare_for_new_position(self, send_ucinewgame_token: bool = True) -> None:
if send_ucinewgame_token:
self._put("ucinewgame")
Expand Down Expand Up @@ -750,7 +767,7 @@ def is_development_build_of_engine(self) -> bool:
"""
return (
self._stockfish_major_version >= 10109
and self._stockfish_major_version <= 311299
and self._stockfish_major_version <= 29991231
)

def send_quit_command(self) -> None:
Expand Down
12 changes: 12 additions & 0 deletions tests/stockfish/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,3 +938,15 @@ def test_send_quit_command(self, stockfish):
stockfish.__del__()
assert stockfish._stockfish.poll() is not None
assert Stockfish._del_counter == old_del_counter + 1

@pytest.mark.parametrize(
"line,expected",
[
("Stockfish 15.1 by", 15),
("Stockfish 200222 by", 200222),
("Stockfish dev-20230329-3f01e3f4 by", 20230329),
("Stockfish dev-15.1", -1),
],
)
def test_version_number_extraction(self, stockfish, line, expected):
assert expected == stockfish._extract_major_version(line)