Skip to content

Commit ffa49f0

Browse files
Merge pull request #395 from lohmataja/fix_384
Fix #384
2 parents 5588af3 + 2f9c8cf commit ffa49f0

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

bpython/cli.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -968,14 +968,16 @@ def p_key(self, key):
968968
return ''
969969

970970
elif key in key_dispatch[config.show_source_key]:
971-
source = self.get_source_of_current_name()
972-
if source is not None:
971+
try:
972+
source = self.get_source_of_current_name()
973973
if config.highlight_show_source:
974974
source = format(PythonLexer().get_tokens(source),
975975
TerminalFormatter())
976976
page(source)
977-
else:
978-
self.statusbar.message(_('Cannot show source.'))
977+
except (ValueError, AttributeError, IOError, TypeError), e:
978+
self.statusbar.message(_(e))
979+
except (NameError), e:
980+
self.statusbar.message(_('Cannot get source: %s' % e))
979981
return ''
980982

981983
elif key in ('\n', '\r', 'PADENTER'):

bpython/curtsiesfrontend/repl.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,14 +1300,17 @@ def pager(self, text):
13001300
self.focus_on_subprocess(command + [tmp.name])
13011301

13021302
def show_source(self):
1303-
source = self.get_source_of_current_name()
1304-
if source is None:
1305-
self.status_bar.message(_('Cannot show source.'))
1306-
else:
1303+
try:
1304+
source = self.get_source_of_current_name()
13071305
if self.config.highlight_show_source:
1308-
source = format(PythonLexer().get_tokens(source), TerminalFormatter())
1306+
source = format(PythonLexer().get_tokens(source),
1307+
TerminalFormatter())
13091308
self.pager(source)
1310-
1309+
except (ValueError, AttributeError, IOError, TypeError), e:
1310+
self.status_bar.message(_(e))
1311+
except (NameError), e:
1312+
self.status_bar.message(_('Cannot get source: %s' % e))
1313+
13111314
def help_text(self):
13121315
return (self.version_help_text() + '\n' + self.key_help_text()).encode('utf8')
13131316

bpython/repl.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -589,21 +589,23 @@ def get_args(self):
589589

590590
def get_source_of_current_name(self):
591591
"""Return the source code of the object which is bound to the
592-
current name in the current input line. Return `None` if the
592+
current name in the current input line. Throw exception if the
593593
source cannot be found."""
594-
try:
595-
obj = self.current_func
596-
if obj is None:
597-
line = self.current_line
598-
if inspection.is_eval_safe_name(line):
599-
obj = self.get_object(line)
600-
if obj is None:
601-
return None
602-
source = inspect.getsource(obj)
603-
except (AttributeError, IOError, NameError, TypeError):
604-
return None
605-
else:
606-
return source
594+
obj = self.current_func
595+
if obj is None:
596+
line = self.current_line
597+
if line == "":
598+
raise ValueError("Nothing to get source of")
599+
if inspection.is_eval_safe_name(line):
600+
obj = self.get_object(line)
601+
try:
602+
inspect.getsource(obj)
603+
except TypeError, e:
604+
msg = e.message
605+
if "built-in" in msg:
606+
raise TypeError("Cannot access source of <built-in function %s>" % self.current_line)
607+
else:
608+
raise TypeError("No source code found for %s" % self.current_line)
607609

608610
def set_docstring(self):
609611
self.docstring = None

0 commit comments

Comments
 (0)