@@ -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+
380384class 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