Skip to content

Commit e18e437

Browse files
committed
Use a specific SourceNotFound in get_source_of_current_name().
Instead of leaking all possible exceptions (that the caller has to deal with), use a single `SourceNotFound` exception.
1 parent 28cb9f2 commit e18e437

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

bpython/cli.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -969,14 +969,13 @@ def p_key(self, key):
969969
elif key in key_dispatch[config.show_source_key]:
970970
try:
971971
source = self.get_source_of_current_name()
972+
except repl.SourceNotFound, e:
973+
self.statusbar.message(_(e))
974+
else:
972975
if config.highlight_show_source:
973976
source = format(PythonLexer().get_tokens(source),
974977
TerminalFormatter())
975978
page(source)
976-
except (ValueError, AttributeError, IOError, TypeError), e:
977-
self.statusbar.message(_(e))
978-
except NameError, e:
979-
self.statusbar.message(_('Cannot get source: %s' % e))
980979
return ''
981980

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

bpython/curtsiesfrontend/repl.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from curtsies import events
3030

3131
import bpython
32-
from bpython.repl import Repl as BpythonRepl
32+
from bpython.repl import Repl as BpythonRepl, SourceNotFound
3333
from bpython.config import Struct, loadini, default_config_path
3434
from bpython.formatter import BPythonFormatter
3535
from bpython import autocomplete, importcompletion
@@ -1302,14 +1302,13 @@ def pager(self, text):
13021302
def show_source(self):
13031303
try:
13041304
source = self.get_source_of_current_name()
1305+
except SourceNotFound, e:
1306+
self.status_bar.message(_(e))
1307+
else:
13051308
if self.config.highlight_show_source:
13061309
source = format(PythonLexer().get_tokens(source),
13071310
TerminalFormatter())
13081311
self.pager(source)
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))
13131312

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

bpython/repl.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ def file_prompt(self, s):
377377
raise NotImplementedError
378378

379379

380+
class SourceNotFound(Exception):
381+
"""Exception raised when the requested source could not be found."""
382+
383+
380384
class Repl(object):
381385
"""Implements the necessary guff for a Python-repl-alike interface
382386
@@ -587,23 +591,27 @@ def get_args(self):
587591

588592
def get_source_of_current_name(self):
589593
"""Return the source code of the object which is bound to the
590-
current name in the current input line. Throw exception if the
594+
current name in the current input line. Throw `SourceNotFound` if the
591595
source cannot be found."""
592596
obj = self.current_func
593-
if obj is None:
594-
line = self.current_line
595-
if line == "":
596-
raise ValueError("Nothing to get source of")
597-
if inspection.is_eval_safe_name(line):
598-
obj = self.get_object(line)
599597
try:
600-
return inspect.getsource(obj)
598+
if obj is None:
599+
line = self.current_line
600+
if not line.strip():
601+
raise SourceNotFound("Nothing to get source of")
602+
if inspection.is_eval_safe_name(line):
603+
obj = self.get_object(line)
604+
return inspect.getsource(obj)
605+
except (AttributeError, NameError), e:
606+
msg = "Cannot get source: " + str(e)
607+
except IOError, e:
608+
msg = str(e)
601609
except TypeError, e:
602-
msg = e.message
603-
if "built-in" in msg:
604-
raise TypeError("Cannot access source of <built-in function %s>" % self.current_line)
610+
if "built-in" in str(e):
611+
msg = "Cannot access source of %r" % (obj, )
605612
else:
606-
raise TypeError("No source code found for %s" % self.current_line)
613+
msg = "No source code found for %s" % (self.current_line, )
614+
raise SourceNotFound(msg)
607615

608616
def set_docstring(self):
609617
self.docstring = None

0 commit comments

Comments
 (0)