m %UIc @sdZdZdZdkZdkZdkZdkZdkZdkZdk Z dk Z dk Z dZ dZ dZdZdZd Zd Zd Zd Zd ZdZddZdZdZdZdZdZdZdZdZ dZ!dZ"hZ#dZ$dZ%dZ&de'fdYZ(dfd YZ)d!Z*d"Z+d#Z,d$Z-d%d&Z.d>\Z/Z0Z1Z2d+Z3d,Z4d-Z5d.Z6e6d/Z7ddde8d0d1d2e6d3Z9e8d4d5d6e6d7Z:d'd8Z;d9Z<d'd:Z=d'd;Z>ei?Z@d'd<ZAd'd=ZBdS(?suGet useful information from live Python objects. This module encapsulates the interface provided by the internal special attributes (func_*, co_*, im_*, tb_*, etc.) in a friendlier fashion. It also provides some help for examining source code and class layout. Here are some of the useful functions provided by this module: ismodule(), isclass(), ismethod(), isfunction(), istraceback(), isframe(), iscode(), isbuiltin(), isroutine() - check object types getmembers() - get members of an object that satisfy a given condition getfile(), getsourcefile(), getsource() - find an object's source code getdoc(), getcomments() - get documentation on an object getmodule() - determine the module that an object came from getclasstree() - arrange classes so as to represent their hierarchy getargspec(), getargvalues() - get info about function arguments formatargspec(), formatargvalues() - format an argument spec getouterframes(), getinnerframes() - get info about frames currentframe() - get the current stack frame stack(), trace() - get info about frames on the stack or in a traceback sKa-Ping Yee s 1 Jan 2001NcCst|tiS(sReturn true if the object is a module. Module objects provide these attributes: __doc__ documentation string __file__ filename (missing for built-in modules)N(t isinstancetobjectttypest ModuleType(R((t$/data/zmath/lib/python2.4/inspect.pytismodule"scCs t|tip t|dS(sReturn true if the object is a class. Class objects provide these attributes: __doc__ documentation string __module__ name of module in which this class was definedt __bases__N(RRRt ClassTypethasattr(R((Rtisclass*scCst|tiS(sReturn true if the object is an instance method. Instance method objects provide these attributes: __doc__ documentation string __name__ name with which this method was defined im_class class object in which this method belongs im_func function object containing implementation of method im_self instance to which this method is bound, or NoneN(RRRt MethodType(R((Rtismethod2scCsHt|do8t|d o't| ot| o t| S(sReturn true if the object is a method descriptor. But not if ismethod() or isclass() or isfunction() are true. This is new in Python 2.2, and, for example, is true of int.__add__. An object passing this test has a __get__ attribute but not a __set__ attribute, but beyond that the set of attributes varies. __name__ is usually sensible, and __doc__ often is. Methods implemented via descriptors that also pass one of the other tests return false from the ismethoddescriptor() test, simply because the other tests promise more -- you can, e.g., count on having the im_func attribute (etc) when an object passes ismethod().t__get__t__set__N(RRR t isfunctionR (R((Rtismethoddescriptor=s cCst|do t|dS(sReturn true if the object is a data descriptor. Data descriptors have both a __get__ and a __set__ attribute. Examples are properties (defined in Python) and getsets and members (defined in C). Typically, data descriptors will also have __name__ and __doc__ attributes (properties, getsets, and members have both of these attributes), but this is not guaranteed.R R N(RR(R((RtisdatadescriptorQscCst|tiS(sReturn true if the object is a user-defined function. Function objects provide these attributes: __doc__ documentation string __name__ name with which this function was defined func_code code object containing compiled function bytecode func_defaults tuple of any default values for arguments func_doc (same as __doc__) func_globals global namespace in which this function was defined func_name (same as __name__)N(RRRt FunctionType(R((RR[s cCst|tiS(sbReturn true if the object is a traceback. Traceback objects provide these attributes: tb_frame frame object at this level tb_lasti index of last attempted instruction in bytecode tb_lineno current line number in Python source code tb_next next inner traceback object (called by this level)N(RRRt TracebackType(R((Rt istracebackhscCst|tiS(s|Return true if the object is a frame object. Frame objects provide these attributes: f_back next outer frame object (this frame's caller) f_builtins built-in namespace seen by this frame f_code code object being executed in this frame f_exc_traceback traceback if raised in this frame, or None f_exc_type exception type if raised in this frame, or None f_exc_value exception value if raised in this frame, or None f_globals global namespace seen by this frame f_lasti index of last attempted instruction in bytecode f_lineno current line number in Python source code f_locals local namespace seen by this frame f_restricted 0 or 1 if frame is in restricted execution mode f_trace tracing function for this frame, or NoneN(RRRt FrameType(R((RtisframerscCst|tiS(suReturn true if the object is a code object. Code objects provide these attributes: co_argcount number of arguments (not including * or ** args) co_code string of raw compiled bytecode co_consts tuple of constants used in the bytecode co_filename name of file in which this code object was created co_firstlineno number of first line in Python source code co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg co_lnotab encoded mapping of line numbers to bytecode indices co_name name with which this code object was defined co_names tuple of names of local variables co_nlocals number of local variables co_stacksize virtual machine stack space required co_varnames tuple of names of arguments and local variablesN(RRRtCodeType(R((RtiscodescCst|tiS(s,Return true if the object is a built-in function or method. Built-in functions and methods provide these attributes: __doc__ documentation string __name__ original name of this function or method __self__ instance to which a method is bound, or NoneN(RRRtBuiltinFunctionType(R((Rt isbuiltinscCs1t|p$t|pt|p t|S(s<Return true if the object is any kind of function or method.N(RRRR R(R((Rt isroutinescCsfg}xOt|D]A}t||}| p ||o|i||fqqW|i|S(sReturn all members of an object as (name, value) pairs sorted by name. Optionally, only return members that satisfy a given predicate.N( tresultstdirRtkeytgetattrtvaluet predicatetappendtsort(RR RRR((Rt getmemberss  c Cst|}t|} g}x[| D]S}||ijo|i|}nt ||}t |dd}|djo0x-|D]!}||ijo |}PqqWn|dj o!||ijo|i|}nt ||}t|to d}n_t|to d}nEt|to d}n+t|p t|o d}nd}|i||||fq%W|S(sReturn list of attribute-descriptor tuples. For each name in dir(cls), the return list contains a 4-tuple with these elements: 0. The name (a string). 1. The kind of attribute this is, one of these strings: 'class method' created via classmethod() 'static method' created via staticmethod() 'property' created via property() 'method' any other flavor of method 'data' not a method 2. The class which defined this attribute (a class). 3. The object as obtained directly from the defining class's __dict__, not via getattr. This is especially important for data attributes: C.data is just a data object, but C.__dict__['data'] may be a data descriptor with additional info, like a __doc__ string. t __objclass__s static methods class methodtpropertytmethodtdataN(tgetmrotclstmroRtnamestresulttnamet__dict__tobjRtNonethomeclstbasetobj_via_getattrRt staticmethodtkindt classmethodR%R RR!( R)R/R-R5R*R,R2R3R1R+((Rtclassify_class_attrss<        cCsG||jodSn|i|x|iD]}t||q,WdS(N(R)taccumR!RR2t _searchbases(R)R8R2((RR9s    cCs<t|do |iSng}t||t|SdS(sHReturn tuple of base classes (including cls) in method resolution order.t__mro__N(RR)R:R,R9ttuple(R)R,((RR(s   cCs,ti|}t|tti|S(sBReturn the indent size, in spaces, at the start of a line of text.N(tstringt expandtabstlinetexplinetlentlstrip(R>R?((Rt indentsize scCsy |i}Wntj o dSnXt|tipdSnyti ti |d}Wnt j o dSn!Xt i}xQ|dD]E}tti|}|o#t||}t||}qqW|o|di|dR@RAtcontenttindenttmintrangetitpoptjoin(RRMRNRQRDRHRLR>((Rtgetdocs>     cCst|o+t|do |iSntdnt|o@tii|i }t|do |iSntdnt |o |i }nt |o |i }nt|o |i}nt|o |i}nt|o |iSntddS(s@Work out which source or compiled file an object was defined in.t__file__sarg is a built-in modulesarg is a built-in classsNarg is not a module, class, method, function, traceback, frame, or code objectN(RRRRUt TypeErrorR RJtmodulestgett __module__R tim_funcRt func_codeRttb_frameRtf_codeRt co_filename(R((Rtgetfile3s*              cCs~tii|}tdti}|ixC|D];\}}}}|||jo|| |||fSq;q;WdS(sDGet the module name, suffix, mode, and module type for a given file.cCs&|\}}}t| |||fS(N(tsuffixtmodetmtypeR@(t.0R`RaRb((RtNsN( tostpathtbasenametfilenametmaptimpt get_suffixestsuffixesR"tneglenR`RaRb(RfR`RmRhRbRaRl((Rt getmoduleinfoKs  cCs#t|}|o |dSndS(s1Return the module name for a given file, or None.iN(RnRftinfo(RfRo((Rt getmodulenameUs cCst|}ti|dddgjo|d d}nxStiD]E\}}}d|jo)ti|t | |jodSqKqKWt i i|o|SndS(sEReturn the Python source file an object was defined in, if it exists.is.pycs.pyos.pytbN(R_RRhR<tlowerRjRkR`RaR5R@R0ReRftexists(RR5R`RhRa((Rt getsourcefileZs   . cCs/tiitiit|p t|S(sReturn an absolute path to the source or compiled file for an object. The idea is for each object to have a unique origin, so this routine normalizes the result as much as possible.N(ReRftnormcasetabspathRtRR_(R((Rt getabsfilefscCst|o|Snt|dotii|iSnyt|}Wnt j o dSnX|t jotiit |SnxJtii D]9}t|do#|it tiit|RRRR((Rt tokeneaters,           )(RyRYRCRR(((RRs  cCsPt}y tit|i|iWnttfj onX||i S(s@Extract the block of code at the top of the given list of lines.N( Rt blockfinderRtiterRHtnextRRtIndentationErrorR(RHR((Rtgetblocks  cCsIt|\}}t|o|dfSnt|||dfSdS(sReturn a list of source lines and starting line number for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a list of the lines corresponding to the object and the line number indicates where in the original source file the first line of code was found. An IOError is raised if the source code cannot be retrieved.iiN(RRRHRRR(RRRH((Rtgetsourceliness  cCs"t|\}}ti|dS(sReturn the text of the source code for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a single string. An IOError is raised if the source code cannot be retrieved.RN(RRRHRR<RS(RRRH((Rt getsource&scCsrg}|iddxR|D]J}|i||if||jo!|it||||q q W|S(s-Recursive helper function for getclasstree().RcCs|i|ifS(N(tcRYRy(R((RRd3sN(RtclassesR"RR!Rtchildrentwalktree(RRtparentRR((RR0s %icCsh}g}x|D]}|io]xx|iD]K}||jog||RRRRR RBRpRR_RRRRwRRnRRRRRRRRjRRRRRRRRRRR<R R R#RRRJRR9RR(RRRRTRRR7RRRR|RRRRtR ReR((Rt?sfQ       G   $    * -'  ;   *! !