Skip to content

Commit 716a0b9

Browse files
committed
Require that EPD opcodes start with letter (fixes #1080)
1 parent 6af0ff4 commit 716a0b9

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

chess/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,9 +2739,7 @@ def _epd_operations(self, operations: Mapping[str, Union[None, str, int, float,
27392739
first_op = True
27402740

27412741
for opcode, operand in operations.items():
2742-
assert opcode != "-", "dash (-) is not a valid epd opcode"
2743-
for blacklisted in [" ", "\n", "\t", "\r"]:
2744-
assert blacklisted not in opcode, f"invalid character {blacklisted!r} in epd opcode: {opcode!r}"
2742+
self._validate_epd_opcode(opcode)
27452743

27462744
if not first_op:
27472745
epd.append(" ")
@@ -2819,6 +2817,17 @@ def epd(self, *, shredder: bool = False, en_passant: EnPassantSpec = "legal", pr
28192817

28202818
return " ".join(epd)
28212819

2820+
def _validate_epd_opcode(self, opcode: str) -> None:
2821+
if not opcode:
2822+
raise ValueError("empty string is not a valid epd opcode")
2823+
if opcode == "-":
2824+
raise ValueError("dash (-) is not a valid epd opcode")
2825+
if not opcode[0].isalpha():
2826+
raise ValueError(f"expected epd opcode to start with a letter, got: {opcode!r}")
2827+
for blacklisted in [" ", "\n", "\t", "\r"]:
2828+
if blacklisted in opcode:
2829+
raise ValueError(f"invalid character {blacklisted!r} in epd opcode: {opcode!r}")
2830+
28222831
def _parse_epd_ops(self: BoardT, operation_part: str, make_board: Callable[[], BoardT]) -> Dict[str, Union[None, str, int, float, Move, List[Move]]]:
28232832
operations: Dict[str, Union[None, str, int, float, Move, List[Move]]] = {}
28242833
state = "opcode"
@@ -2832,6 +2841,7 @@ def _parse_epd_ops(self: BoardT, operation_part: str, make_board: Callable[[], B
28322841
if opcode == "-":
28332842
opcode = ""
28342843
elif opcode:
2844+
self._validate_epd_opcode(opcode)
28352845
state = "after_opcode"
28362846
elif ch is None or ch == ";":
28372847
if opcode == "-":

test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,11 @@ def test_eret_epd(self):
11431143
self.assertEqual(ops["id"], "ERET 001 - Entlastung")
11441144
self.assertEqual(ops["bm"], [chess.Move.from_uci("f1f4")])
11451145

1146+
def test_set_fen_as_epd(self):
1147+
board = chess.Board()
1148+
with self.assertRaises(ValueError):
1149+
board.set_epd(board.fen()) # Move numbers are not valid opcodes
1150+
11461151
def test_null_moves(self):
11471152
self.assertEqual(str(chess.Move.null()), "0000")
11481153
self.assertEqual(chess.Move.null().uci(), "0000")

0 commit comments

Comments
 (0)