def testParseInvalidIndicesStringsWithoutBrackets(self): with self.assertRaisesRegexp( ValueError, r"invalid literal for int\(\) with base 10: 'a'"): self.assertEqual([0], command_parser.parse_indices("0,a")) with self.assertRaisesRegexp( ValueError, r"invalid literal for int\(\) with base 10: '2\]'"): self.assertEqual([0], command_parser.parse_indices("1, 2]")) with self.assertRaisesRegexp( ValueError, r"invalid literal for int\(\) with base 10: ''"): self.assertEqual([0], command_parser.parse_indices("3, 4,"))
def testParseInvalidIndicesStringsWithoutBrackets(self): with self.assertRaisesRegex( ValueError, r"invalid literal for int\(\) with base 10: 'a'"): self.assertEqual([0], command_parser.parse_indices("0,a")) with self.assertRaisesRegex( ValueError, r"invalid literal for int\(\) with base 10: '2\]'"): self.assertEqual([0], command_parser.parse_indices("1, 2]")) with self.assertRaisesRegex( ValueError, r"invalid literal for int\(\) with base 10: ''"): self.assertEqual([0], command_parser.parse_indices("3, 4,"))
def testParseValidIndicesStringsWithoutBrackets(self): self.assertEqual([0], command_parser.parse_indices("0")) self.assertEqual([0], command_parser.parse_indices(" 0 ")) self.assertEqual([-1, 2], command_parser.parse_indices("-1, 2")) self.assertEqual([3, 4, -5], command_parser.parse_indices("3,4,-5"))
def testParseValidIndicesStringsWithBrackets(self): self.assertEqual([0], command_parser.parse_indices("[0]")) self.assertEqual([0], command_parser.parse_indices(" [0] ")) self.assertEqual([-1, 2], command_parser.parse_indices("[-1, 2]")) self.assertEqual([3, 4, -5], command_parser.parse_indices("[3,4,-5]"))
def testParseValidIndicesStringsWithoutBrackets(self): self.assertEqual([0], command_parser.parse_indices("0")) self.assertEqual([0], command_parser.parse_indices(" 0 ")) self.assertEqual([-1, 2], command_parser.parse_indices("-1, 2")) self.assertEqual([3, 4, -5], command_parser.parse_indices("3,4,-5"))
def testParseValidIndicesStringsWithBrackets(self): self.assertEqual([0], command_parser.parse_indices("[0]")) self.assertEqual([0], command_parser.parse_indices(" [0] ")) self.assertEqual([-1, 2], command_parser.parse_indices("[-1, 2]")) self.assertEqual([3, 4, -5], command_parser.parse_indices("[3,4,-5]"))
def _dispatch_command(self, command): """Dispatch user command. Args: command: (str) Command to dispatch. Returns: An exit token object. None value means that the UI loop should not exit. A non-None value means the UI loop should exit. """ if command in self.CLI_EXIT_COMMANDS: # Explicit user command-triggered exit: EXPLICIT_USER_EXIT as the exit # token. return debugger_cli_common.EXPLICIT_USER_EXIT if command: self._command_history_store.add_command(command) if (command.startswith(self.REGEX_SEARCH_PREFIX) and self._curr_unwrapped_output): if len(command) > len(self.REGEX_SEARCH_PREFIX): # Command is like "/regex". Perform regex search. regex = command[len(self.REGEX_SEARCH_PREFIX):] self._curr_search_regex = regex self._display_output(self._curr_unwrapped_output, highlight_regex=regex) elif self._unwrapped_regex_match_lines: # Command is "/". Continue scrolling down matching lines. self._display_output(self._curr_unwrapped_output, is_refresh=True, highlight_regex=self._curr_search_regex) self._command_pointer = 0 self._pending_command = "" return elif command.startswith(self.TENSOR_INDICES_NAVIGATION_PREFIX): indices_str = command[1:].strip() if indices_str: try: indices = command_parser.parse_indices(indices_str) omitted, line_index, _, _ = tensor_format.locate_tensor_element( self._curr_wrapped_output, indices) if not omitted: self._scroll_output(self._SCROLL_TO_LINE_INDEX, line_index=line_index) except Exception as e: # pylint: disable=broad-except self._error_toast(str(e)) else: self._error_toast("Empty indices.") return try: prefix, args, output_file_path = self._parse_command(command) except SyntaxError as e: self._error_toast(str(e)) return if not prefix: # Empty command: take no action. Should not exit. return screen_info = {"cols": self._max_x} exit_token = None if self._command_handler_registry.is_registered(prefix): try: screen_output = self._command_handler_registry.dispatch_command( prefix, args, screen_info=screen_info) except debugger_cli_common.CommandLineExit as e: exit_token = e.exit_token else: screen_output = debugger_cli_common.RichTextLines([ self.ERROR_MESSAGE_PREFIX + "Invalid command prefix \"%s\"" % prefix ]) # Clear active command history. Until next up/down history navigation # occurs, it will stay empty. self._active_command_history = [] if exit_token is not None: return exit_token self._display_output(screen_output) if output_file_path: try: screen_output.write_to_file(output_file_path) self._info_toast("Wrote output to %s" % output_file_path) except Exception: # pylint: disable=broad-except self._error_toast("Failed to write output to %s" % output_file_path) self._command_pointer = 0 self._pending_command = ""
def _dispatch_command(self, command): """Dispatch user command. Args: command: (str) Command to dispatch. Returns: An exit token object. None value means that the UI loop should not exit. A non-None value means the UI loop should exit. """ if command in self.CLI_EXIT_COMMANDS: # Explicit user command-triggered exit: EXPLICIT_USER_EXIT as the exit # token. return debugger_cli_common.EXPLICIT_USER_EXIT if command: self._command_history_store.add_command(command) if (command.startswith(self.REGEX_SEARCH_PREFIX) and self._curr_unwrapped_output): if len(command) > len(self.REGEX_SEARCH_PREFIX): # Command is like "/regex". Perform regex search. regex = command[len(self.REGEX_SEARCH_PREFIX):] self._curr_search_regex = regex self._display_output(self._curr_unwrapped_output, highlight_regex=regex) elif self._unwrapped_regex_match_lines: # Command is "/". Continue scrolling down matching lines. self._display_output( self._curr_unwrapped_output, is_refresh=True, highlight_regex=self._curr_search_regex) self._command_pointer = 0 self._pending_command = "" return elif command.startswith(self.TENSOR_INDICES_NAVIGATION_PREFIX): indices_str = command[1:].strip() if indices_str: try: indices = command_parser.parse_indices(indices_str) omitted, line_index, _, _ = tensor_format.locate_tensor_element( self._curr_wrapped_output, indices) if not omitted: self._scroll_output( self._SCROLL_TO_LINE_INDEX, line_index=line_index) except Exception as e: # pylint: disable=broad-except self._error_toast(str(e)) else: self._error_toast("Empty indices.") return prefix, args = self._parse_command(command) if not prefix: # Empty command: take no action. Should not exit. return screen_info = {"cols": self._max_x} exit_token = None if self._command_handler_registry.is_registered(prefix): try: screen_output = self._command_handler_registry.dispatch_command( prefix, args, screen_info=screen_info) except debugger_cli_common.CommandLineExit as e: exit_token = e.exit_token else: screen_output = debugger_cli_common.RichTextLines([ self.ERROR_MESSAGE_PREFIX + "Invalid command prefix \"%s\"" % prefix ]) # Clear active command history. Until next up/down history navigation # occurs, it will stay empty. self._active_command_history = [] if exit_token is not None: return exit_token self._display_output(screen_output) self._command_pointer = 0 self._pending_command = ""