Skip to content

Commit

Permalink
Remove unnecessary decorator for BaseSimulator.launch().
Browse files Browse the repository at this point in the history
This decorator was used for many functions, but we currently used it only
during `launch()`, so it only adds unnecessary complexity. This change moves
its main `try`/`except` logic inside `launch()` to simplify things.

PiperOrigin-RevId: 586650107
  • Loading branch information
kenjitoyama authored and copybara-github committed Nov 30, 2023
1 parent cbc508a commit 35a7467
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions android_env/components/simulators/base_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,6 @@
import numpy as np


def _print_logs_on_exception(func):
"""Decorator function for printing simulator logs upon any exception."""
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as error:
# Calls self.get_logs since self is the first arg.
for line in args[0].get_logs().splitlines():
logging.error(line)
raise errors.SimulatorError(
'Exception caught in simulator. Please see the simulator logs '
'above for more details.') from error
return wrapper


class BaseSimulator(metaclass=abc.ABCMeta):
"""An interface for communicating with an Android simulator."""

Expand Down Expand Up @@ -75,12 +60,19 @@ def create_adb_controller(self) -> adb_controller.AdbController:
def create_log_stream(self) -> log_stream.LogStream:
"""Creates a stream of logs from the simulator."""

@_print_logs_on_exception
def launch(self) -> None:
"""Starts the simulator."""

self._num_launch_attempts += 1
self._launch_impl()
try:
self._launch_impl()
except Exception as error:
for line in self.get_logs().splitlines():
logging.error(line)
raise errors.SimulatorError(
'Exception caught in simulator. Please see the simulator logs '
'above for more details.'
) from error

@abc.abstractmethod
def _launch_impl(self) -> None:
Expand Down

0 comments on commit 35a7467

Please sign in to comment.