##// 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
@@ -278,7 +283,7 b' class Inspector:'
278 self.noinfo('documentation',oname)
283 self.noinfo('documentation',oname)
279 return
284 return
280 page(output)
285 page(output)
281
286
282 def psource(self,obj,oname=''):
287 def psource(self,obj,oname=''):
283 """Print the source code for an object."""
288 """Print the source code for an object."""
284
289
@@ -293,23 +298,35 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
296 try:
302 try:
297 sourcelines,lineno = inspect.getsourcelines(obj)
303 try:
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)
313 return
314
315 # We only reach this point if object was successfully queried
316
317 # run contents of file through pager starting at line
318 # where the object is defined
319 ofile = inspect.getabsfile(obj)
320
321 if (ofile.endswith('.so') or ofile.endswith('.dll')):
322 print 'File %r is binary, not printing.' % ofile
323 elif not os.path.isfile(ofile):
324 print 'File %r does not exist, not printing.' % ofile
300 else:
325 else:
301 # run contents of file through pager starting at line
326 # Print only text files, not extension binaries. Note that
302 # where the object is defined
327 # getsourcelines returns lineno with 1-offset and page() uses
303 ofile = inspect.getabsfile(obj)
328 # 0-offset, so we must adjust.
304
329 page(self.format(open(ofile).read()),lineno-1)
305 if (ofile.endswith('.so') or ofile.endswith('.dll')):
306 print 'File %r is binary, not printing.' % ofile
307 elif not os.path.isfile(ofile):
308 print 'File %r does not exist, not printing.' % ofile
309 else:
310 # Print only text files, not extension binaries.
311 page(self.format(open(ofile).read()),lineno)
312 #page(self.format(open(inspect.getabsfile(obj)).read()),lineno)
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.
@@ -1528,7 +1528,10 b' def page(strng,start=0,screen_lines=0,pager_cmd = None):'
1528 If no system pager works, the string is sent through a 'dumb pager'
1528 If no system pager works, the string is sent through a 'dumb pager'
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