code: def set_document(self, value, bypass_readonly=False):
          
          assert isinstance(value, Document)
  
          # Don't allow editing of read-only buffers.
          if not bypass_readonly and self.read_only():
              raise EditReadOnlyBuffer()
  
          # Set text and cursor position first.
          text_changed = self._set_text(value.text)
          cursor_position_changed = self._set_cursor_position(value.cursor_position)
  
          # Now handle change events. (We do this when text/cursor position is
          # both set and consistent.)
          if text_changed:
              self._text_changed()
  
          if cursor_position_changed:
              self._cursor_position_changed()
docs: Set :class:`~prompt_toolkit.document.Document` instance. Like the
          ``document`` property, but accept an ``bypass_readonly`` argument.
  
          :param bypass_readonly: When True, don't raise an
                                  :class:`.EditReadOnlyBuffer` exception, even
                                  when the buffer is read-only.