studio: catch KeyboardInterrupt in samples and format imports.

* Adds exception handler block to simulator samples to allow clean Ctrl+C exits.
* Formats import statements inside code generator validator.

PiperOrigin-RevId: 950821976
Change-Id: I283533d87619d5d9b54d9bec970cdefe538fd752
This commit is contained in:
Matija Kecman
2026-07-20 07:19:26 -07:00
committed by Copybara-Service
parent a62bf19e64
commit 8196ca17f8
3 changed files with 21 additions and 9 deletions
@@ -184,9 +184,13 @@ def main(argv: list[str]) -> None:
handle.send_to_viewer(messages.ModelEvent(model=model))
step_control = sim.StepControl()
while handle.is_running():
step_control.advance(model, data)
model, data, step_control = handle.sync(model, data, step_control)
try:
while handle.is_running():
step_control.advance(model, data)
model, data, step_control = handle.sync(model, data, step_control)
except KeyboardInterrupt:
# Ctrl+C is the documented way to quit; exit cleanly, no traceback.
print('\nShutting down.', flush=True)
if __name__ == '__main__':
@@ -237,9 +237,13 @@ def main(argv: list[str]) -> None:
handle.send_to_viewer(messages.ModelEvent(model=model))
step_control = sim.StepControl()
while handle.is_running():
step_control.advance(model, data)
model, data, step_control = handle.sync(model, data, step_control)
try:
while handle.is_running():
step_control.advance(model, data)
model, data, step_control = handle.sync(model, data, step_control)
except KeyboardInterrupt:
# Ctrl+C is the documented way to quit; exit cleanly, no traceback.
print('\nShutting down.', flush=True)
if __name__ == '__main__':
+7 -3
View File
@@ -61,9 +61,13 @@ def main(argv: list[str]) -> None:
# Run the simulation.
step_control = sim.StepControl()
while handle.is_running():
step_control.advance(model, data)
model, data, step_control = handle.sync(model, data, step_control)
try:
while handle.is_running():
step_control.advance(model, data)
model, data, step_control = handle.sync(model, data, step_control)
except KeyboardInterrupt:
# Ctrl+C is the documented way to quit; exit cleanly, no traceback.
print('\nShutting down.', flush=True)
_app.run(main)