##// END OF EJS Templates
Fix bug reported by Jeremy Jones where %pfile would fail on object...
Fernando Perez -
Show More
@@ -155,7 +155,12 b' def getsource(obj,is_binary=False):'
155 if is_binary:
155 if is_binary:
156 return None
156 return None
157 else:
157 else:
158 return inspect.getsource(obj)
158 try:
159 src = inspect.getsource(obj)
160 except TypeError:
161 if hasattr(obj,'__class__'):
162 src = inspect.getsource(obj.__class__)
163 return src
159
164
160 #****************************************************************************
165 #****************************************************************************
161 # Class definitions
166 # Class definitions
@@ -293,11 +298,22 b' class Inspector:'
293
298
294 def pfile(self,obj,oname=''):
299 def pfile(self,obj,oname=''):
295 """Show the whole file where an object was defined."""
300 """Show the whole file where an object was defined."""
301
302 try:
296 try:
303 try:
297 sourcelines,lineno = inspect.getsourcelines(obj)
304 lineno = inspect.getsourcelines(obj)[1]
305 except TypeError:
306 # For instances, try the class object like getsource() does
307 if hasattr(obj,'__class__'):
308 lineno = inspect.getsourcelines(obj.__class__)[1]
309 # Adjust the inspected object so getabsfile() below works
310 obj = obj.__class__
298 except:
311 except:
299 self.noinfo('file',oname)
312 self.noinfo('file',oname)
300 else:
313 return
314
315 # We only reach this point if object was successfully queried
316
301 # run contents of file through pager starting at line
317 # run contents of file through pager starting at line
302 # where the object is defined
318 # where the object is defined
303 ofile = inspect.getabsfile(obj)
319 ofile = inspect.getabsfile(obj)
@@ -307,9 +323,10 b' class Inspector:'
307 elif not os.path.isfile(ofile):
323 elif not os.path.isfile(ofile):
308 print 'File %r does not exist, not printing.' % ofile
324 print 'File %r does not exist, not printing.' % ofile
309 else:
325 else:
310 # Print only text files, not extension binaries.
326 # Print only text files, not extension binaries. Note that
311 page(self.format(open(ofile).read()),lineno)
327 # getsourcelines returns lineno with 1-offset and page() uses
312 #page(self.format(open(inspect.getabsfile(obj)).read()),lineno)
328 # 0-offset, so we must adjust.
329 page(self.format(open(ofile).read()),lineno-1)
313
330
314 def pinfo(self,obj,oname='',formatter=None,info=None,detail_level=0):
331 def pinfo(self,obj,oname='',formatter=None,info=None,detail_level=0):
315 """Show detailed information about an object.
332 """Show detailed information about an object.
@@ -1529,6 +1529,9 b' def page(strng,start=0,screen_lines=0,pager_cmd = None):'
1529 written in python, very simplistic.
1529 written in python, very simplistic.
1530 """
1530 """
1531
1531
1532 # Some routines may auto-compute start offsets incorrectly and pass a
1533 # negative value. Offset to 0 for robustness.
1534 start = max(0,start)
1532
1535
1533 # first, try the hook
1536 # first, try the hook
1534 ip = IPython.ipapi.get()
1537 ip = IPython.ipapi.get()
General Comments 0
You need to be logged in to leave comments. Login now