Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions bpython/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,14 +968,16 @@ def p_key(self, key):
return ''

elif key in key_dispatch[config.show_source_key]:
source = self.get_source_of_current_name()
if source is not None:
try:
source = self.get_source_of_current_name()
if config.highlight_show_source:
source = format(PythonLexer().get_tokens(source),
TerminalFormatter())
page(source)
else:
self.statusbar.message(_('Cannot show source.'))
except (ValueError, AttributeError, IOError, TypeError), e:
self.statusbar.message(_(e))
except (NameError), e:
self.statusbar.message(_('Cannot get source: %s' % e))
return ''

elif key in ('\n', '\r', 'PADENTER'):
Expand Down
15 changes: 9 additions & 6 deletions bpython/curtsiesfrontend/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,14 +1300,17 @@ def pager(self, text):
self.focus_on_subprocess(command + [tmp.name])

def show_source(self):
source = self.get_source_of_current_name()
if source is None:
self.status_bar.message(_('Cannot show source.'))
else:
try:
source = self.get_source_of_current_name()
if self.config.highlight_show_source:
source = format(PythonLexer().get_tokens(source), TerminalFormatter())
source = format(PythonLexer().get_tokens(source),
TerminalFormatter())
self.pager(source)

except (ValueError, AttributeError, IOError, TypeError), e:
self.status_bar.message(_(e))
except (NameError), e:
self.status_bar.message(_('Cannot get source: %s' % e))

def help_text(self):
return (self.version_help_text() + '\n' + self.key_help_text()).encode('utf8')

Expand Down
30 changes: 16 additions & 14 deletions bpython/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,21 +589,23 @@ def get_args(self):

def get_source_of_current_name(self):
"""Return the source code of the object which is bound to the
current name in the current input line. Return `None` if the
current name in the current input line. Throw exception if the
source cannot be found."""
try:
obj = self.current_func
if obj is None:
line = self.current_line
if inspection.is_eval_safe_name(line):
obj = self.get_object(line)
if obj is None:
return None
source = inspect.getsource(obj)
except (AttributeError, IOError, NameError, TypeError):
return None
else:
return source
obj = self.current_func
if obj is None:
line = self.current_line
if line == "":
raise ValueError("Nothing to get source of")
if inspection.is_eval_safe_name(line):
obj = self.get_object(line)
try:
inspect.getsource(obj)
except TypeError, e:
msg = e.message
if "built-in" in msg:
raise TypeError("Cannot access source of <built-in function %s>" % self.current_line)
else:
raise TypeError("No source code found for %s" % self.current_line)

def set_docstring(self):
self.docstring = None
Expand Down