def do_scratch_n_times(self, n): for _ in range(n): try: # Get the number of characters to delete from the current # window's stack. Discard the state flags. scratch_number, _ = self._get_window_stack().pop() Key("backspace:%d" % scratch_number).execute() except IndexError: handle = self._current_window_handle window = Window.get_window(handle) exe = os.path.basename(window.executable) title = window.title print("Nothing in scratch memory for %r window " "(title=%r, id=%d)" % (exe, title, handle)) break