lJc@sdZdZdZddkZddkZddkZddkZddkZddkZddk Z ddk Z ddk Z ddk l Z dZdZdZd Zd Zeed o d Zn d Zeedo dZn dZdZdZdZdZdZdZddZdZdZdZ dZ!dZ"dZ#dZ$dZ%d Z&dd!Z'hZ(hZ)dd"Z*d#Z+d$Z,d%e-fd&YZ.d'dFd(YZ/d)Z0d*Z1d+Z2d,Z3d-d.Z4dG\Z5Z6Z7Z8d3Z9d4Z:d5Z;d6Z<e<d7Z=ddde>d8d9d:e<d;Z?e>d<d=d>e<d?Z@d/d@ZAdAZBd/dBZCd/dCZDeiEZFd/dDZGd/dEZHdS(HsuGet 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 2001iN(t attrgettercCst|tiS(sReturn true if the object is a module. Module objects provide these attributes: __doc__ documentation string __file__ filename (missing for built-in modules)(t isinstancettypest ModuleType(tobject((s/sys/lib/python/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__(RRt ClassTypethasattr(R((s/sys/lib/python/inspect.pytisclass+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 None(RRt MethodType(R((s/sys/lib/python/inspect.pytismethod3s cCsHt|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__(RR t isfunctionR (R((s/sys/lib/python/inspect.pytismethoddescriptor>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 (R(R((s/sys/lib/python/inspect.pytisdatadescriptorRstMemberDescriptorTypecCst|tiS(sReturn true if the object is a member descriptor. Member descriptors are specialized descriptors defined in extension modules.(RRR(R((s/sys/lib/python/inspect.pytismemberdescriptor^scCstS(sReturn true if the object is a member descriptor. Member descriptors are specialized descriptors defined in extension modules.(tFalse(R((s/sys/lib/python/inspect.pyRfstGetSetDescriptorTypecCst|tiS(sReturn true if the object is a getset descriptor. getset descriptors are specialized descriptors defined in extension modules.(RRR(R((s/sys/lib/python/inspect.pytisgetsetdescriptoroscCstS(sReturn true if the object is a getset descriptor. getset descriptors are specialized descriptors defined in extension modules.(R(R((s/sys/lib/python/inspect.pyRwscCst|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__)(RRt FunctionType(R((s/sys/lib/python/inspect.pyR~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)(RRt TracebackType(R((s/sys/lib/python/inspect.pyt istracebackscCst|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 None(RRt FrameType(R((s/sys/lib/python/inspect.pytisframescCst|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 variables(RRtCodeType(R((s/sys/lib/python/inspect.pytiscodescCst|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 None(RRtBuiltinFunctionType(R((s/sys/lib/python/inspect.pyt isbuiltinscCs1t|p$t|pt|p t|S(s<Return true if the object is any kind of function or method.(RRR R(R((s/sys/lib/python/inspect.pyt isroutines   cCsfg}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.(tdirtgetattrtappendtsort(Rt predicatetresultstkeytvalue((s/sys/lib/python/inspect.pyt 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( tgetmroR t__dict__R!tNoneRt staticmethodt classmethodR*R RR"( tclstmrotnamestresulttnametobjthomeclstbasetobj_via_getattrtkind((s/sys/lib/python/inspect.pytclassify_class_attrss<          cCsG||jodSn|i|x|iD]}t||q,WdS(N(R"Rt _searchbases(R2taccumR9((s/sys/lib/python/inspect.pyR=s    cCs<t|do |iSng}t||t|SdS(sHReturn tuple of base classes (including cls) in method resolution order.t__mro__N(RR?R=ttuple(R2R5((s/sys/lib/python/inspect.pyR-#s   cCs,ti|}t|tti|S(sBReturn the indent size, in spaces, at the start of a line of text.(tstringt expandtabstlentlstrip(tlinetexpline((s/sys/lib/python/inspect.pyt indentsize-scCsy |i}Wntj o dSnXt|tipdSnytiti|d}Wnt j o dSn!Xt i }xQ|dD]E}t ti |}|o#t ||}t||}qqW|o|di |dqsN(tostpathtbasenametmaptimpt get_suffixesR#(RktfilenametsuffixestneglenRfRgRh((s/sys/lib/python/inspect.pyt getmoduleinfons  cCs#t|}|o |dSndS(s1Return the module name for a given file, or None.iN(Rs(Rktinfo((s/sys/lib/python/inspect.pyt getmodulenamexs cCst|}ti|ddjo|d d}nxStiD]E\}}}d|jo)ti|t| |jodSqEqEWtii |o|Snt t ||do|SndS( sEReturn the Python source file an object was defined in, if it exists.is.pycs.pyos.pytbt __loader__N(s.pycs.pyo( RdRAtlowerRnRoRCR/RjRktexistsRt getmodule(RRpRfRgR;((s/sys/lib/python/inspect.pyt getsourcefile}s  . cCsF|djot|p t|}ntiitii|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(R/R{RdRjRktnormcasetabspath(Rt _filename((s/sys/lib/python/inspect.pyt getabsfiles c Cs8t|o|Snt|dotii|iSn|dj o%|tjotiit|Snyt||}Wnt j o dSnX|tjotiit|Snxtii D]\}}t|ott|dod|i }|t i|djoqn|t |sl            G   $     . = -'  ;       !