MCPcopy Index your code
hub / github.com/bpython/bpython / get_source_of_current_name

Method get_source_of_current_name

bpython/repl.py:718–742  ·  view source on GitHub ↗

Return the unicode source code of the object which is bound to the current name in the current input line. Throw `SourceNotFound` if the source cannot be found.

(self)

Source from the content-addressed store, hash-verified

716 return False
717
718 def get_source_of_current_name(self) -> str:
719 """Return the unicode source code of the object which is bound to the
720 current name in the current input line. Throw `SourceNotFound` if the
721 source cannot be found."""
722
723 obj: Callable | None = self.current_func
724 try:
725 if obj is None:
726 line = self.current_line
727 if not line.strip():
728 raise SourceNotFound(_("Nothing to get source of"))
729 if inspection.is_eval_safe_name(line):
730 obj = self.get_object(line)
731 # Ignoring the next mypy error because we want this to fail if obj is None
732 return inspect.getsource(obj) # type:ignore[arg-type]
733 except (AttributeError, NameError) as e:
734 msg = _("Cannot get source: %s") % (e,)
735 except OSError as e:
736 msg = f"{e}"
737 except TypeError as e:
738 if "built-in" in f"{e}":
739 msg = _("Cannot access source of %r") % (obj,)
740 else:
741 msg = _("No source code found for %s") % (self.current_line,)
742 raise SourceNotFound(msg)
743
744 def set_docstring(self) -> None:
745 self.docstring = None

Callers 4

test_current_functionMethod · 0.80
test_current_lineMethod · 0.80
show_sourceMethod · 0.80

Calls 3

get_objectMethod · 0.95
_Function · 0.90
SourceNotFoundClass · 0.85