From f9bf580f0aa57741c71f1bcbea6db5705a32ee3e Mon Sep 17 00:00:00 2001 From: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> Date: Thu, 26 Nov 2026 01:30:19 +0000 Subject: [PATCH] Parse the terminal port in agent server addresses Fixes #411. --- python/mujoco_mpc/agent.py | 2 +- python/mujoco_mpc/agent_test.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/python/mujoco_mpc/agent.py b/python/mujoco_mpc/agent.py index 1198fc685..9af752a64 100644 --- a/python/mujoco_mpc/agent.py +++ b/python/mujoco_mpc/agent.py @@ -59,7 +59,7 @@ def parse_port(server_addr: str) -> int: Returns: int: the port """ - match = re.search(r":(\d+)", server_addr) + match = re.search(r":([0-9]+)\Z", server_addr) if match is None: raise ValueError(f"Port not specified in server address: '{server_addr}'") return int(match.group(1)) diff --git a/python/mujoco_mpc/agent_test.py b/python/mujoco_mpc/agent_test.py index 68b40385a..1fb681695 100644 --- a/python/mujoco_mpc/agent_test.py +++ b/python/mujoco_mpc/agent_test.py @@ -43,6 +43,19 @@ def environment_reset(model, data): class AgentTest(parameterized.TestCase): + @parameterized.parameters( + ("localhost:12345", 12345), + ("[::1]:12345", 12345), + ("[2001:db8:12::1]:54321", 54321), + ) + def test_parse_port(self, address, expected_port): + self.assertEqual(agent_lib.parse_port(address), expected_port) + + @parameterized.parameters("[::1]", "localhost:123junk") + def test_parse_port_requires_terminal_port(self, address): + with self.assertRaisesRegex(ValueError, "Port not specified"): + agent_lib.parse_port(address) + def test_set_task_parameters(self): model_path = ( pathlib.Path(__file__).parent.parent.parent