Lc@sdZdZdZddkZddkZddkZddkZddkZddkZddk Z ddk Z ddk Z ddk l Z ddklZd]\ZZZZd^\ZZZd_ZdZdZdZdZdZeedo dZn dZeedo dZn dZdZ dZ!dZ"dZ#dZ$dZ%d Z&d!Z'd"Z(dd#Z*ed$d%Z+d&Z,d'Z-d(Z.d)Z/d*Z0d+Z1d,Z2ed-d.Z3d/Z4d0Z5d1Z6dd2Z7hZ8hZ9dd3Z:d4Z;d5Z<d6e=fd7YZ>d8d`d9YZ?d:Z@d;ZAd<ZBd=ZCd>d?ZDed@dAZEdBZFedCdDZGdEZHedFdGZIdHZJdIZKeKdJZLdddeMdKdLdMeKdNZNeMdOdPdQeKdRZOedSdTZPddUZQdVZRddWZSddXZTeedYo eiUZVn ddZZVdd[ZWdd\ZXdS(asGet 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(), isgeneratorfunction(), isgenerator(), 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 attrgetter(t namedtupleiiiiii i@icCst|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/usr/lib64/python2.6/inspect.pytismodule3scCs 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/usr/lib64/python2.6/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/usr/lib64/python2.6/inspect.pytismethodCs 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__(R R t isfunctionR (R((s/usr/lib64/python2.6/inspect.pytismethoddescriptorNs 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.RR (R (R((s/usr/lib64/python2.6/inspect.pytisdatadescriptorbstMemberDescriptorTypecCst|tiS(sReturn true if the object is a member descriptor. Member descriptors are specialized descriptors defined in extension modules.(RRR(R((s/usr/lib64/python2.6/inspect.pytismemberdescriptornscCstS(sReturn true if the object is a member descriptor. Member descriptors are specialized descriptors defined in extension modules.(tFalse(R((s/usr/lib64/python2.6/inspect.pyRvstGetSetDescriptorTypecCst|tiS(sReturn true if the object is a getset descriptor. getset descriptors are specialized descriptors defined in extension modules.(RRR(R((s/usr/lib64/python2.6/inspect.pytisgetsetdescriptorscCstS(sReturn true if the object is a getset descriptor. getset descriptors are specialized descriptors defined in extension modules.(R(R((s/usr/lib64/python2.6/inspect.pyRscCst|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/usr/lib64/python2.6/inspect.pyRs cCs.tt|p t|o|iit@S(sReturn true if the object is a user-defined generator function. Generator function objects provides same attributes as functions. See isfunction.__doc__ for attributes listing.(tboolRR t func_codetco_flagst CO_GENERATOR(R((s/usr/lib64/python2.6/inspect.pytisgeneratorfunctionscCst|tiS(s Return true if the object is a generator. Generator objects provide these attributes: __iter__ defined to support interation over container close raises a new GeneratorExit exception inside the generator to terminate the iteration gi_code code object gi_frame frame object or possibly None once the generator has been exhausted gi_running set to 1 when generator is executing, 0 otherwise next return the next item from the container send resumes the generator and "sends" a value that becomes the result of the current yield-expression throw used to raise an exception inside the generator(RRt GeneratorType(R((s/usr/lib64/python2.6/inspect.pyt isgeneratorscCst|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/usr/lib64/python2.6/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/usr/lib64/python2.6/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/usr/lib64/python2.6/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/usr/lib64/python2.6/inspect.pyt isbuiltinscCs1t|p$t|pt|p t|S(s<Return true if the object is any kind of function or method.(R&RR R(R((s/usr/lib64/python2.6/inspect.pyt isroutines   cCst|to |it@S(s:Return true if the object is an abstract base class (ABC).(Rttypet __flags__tTPFLAGS_IS_ABSTRACT(R((s/usr/lib64/python2.6/inspect.pyt isabstractscCsfg}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/usr/lib64/python2.6/inspect.pyt getmemberss  t Attributesname kind defining_class objectc Cst|}t|}g}x^|D]V}||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 t || ||q%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 classmethodR7R RR.R5( tclstmrotnamestresulttnametobjthomeclstbasetobj_via_getattrtkind((s/usr/lib64/python2.6/inspect.pytclassify_class_attrss<           cCsD||jodS|i|x|iD]}t||q)WdS(N(R.Rt _searchbases(R?taccumRF((s/usr/lib64/python2.6/inspect.pyRJKs    cCs9t|do|iSg}t||t|SdS(sHReturn tuple of base classes (including cls) in method resolution order.t__mro__N(R RLRJttuple(R?RB((s/usr/lib64/python2.6/inspect.pyR:Ss  cCs,ti|}t|tti|S(sBReturn the indent size, in spaces, at the start of a line of text.(tstringt expandtabstlentlstrip(tlinetexpline((s/usr/lib64/python2.6/inspect.pyt indentsize]scCsFy |i}Wntj odSXt|tipdSt|S(sGet the documentation string for an object. All tabs are expanded to spaces. To clean up docstrings that are indented to line up with blocks of code, any whitespace than can be uniformly removed from the second line onwards is removed.N(t__doc__tAttributeErrorR<RRt StringTypestcleandoc(Rtdoc((s/usr/lib64/python2.6/inspect.pytgetdocbs c CsYytiti|d}Wntj odSXti}xQ|dD]E}tti|}|o#t||}t ||}qJqJW|o|di|dsN(tostpathtbasenametmaptimpt get_suffixesR/Rr(Rvtfilenametsuffixestneglentsuffixtmodetmtype((s/usr/lib64/python2.6/inspect.pyt getmoduleinfos  cCs t|}|o |dSdS(s1Return the module name for a given file, or None.iN(R(RvRs((s/usr/lib64/python2.6/inspect.pyt getmodulenames cCst|}ti|ddjo|d d}nxPtiD]B\}}}d|jo&ti|t| |jodSqEWtii |o|St t ||do|SdS( sEReturn the Python source file an object was defined in, if it exists.is.pycs.pyos.pytbt __loader__N(s.pycs.pyo( RqRNtlowerRyRzRPR<RuRvtexistsR t getmodule(RR{R~RRH((s/usr/lib64/python2.6/inspect.pyt getsourcefiles  . 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<RRqRuRvtnormcasetabspath(Rt _filename((s/usr/lib64/python2.6/inspect.pyt getabsfiles c Cst|o|St|dotii|iS|dj o"|tjotiit|Syt||}Wnt j odSX|tjotiit|Sxtii D]\}}t|ott|dod|i }|t i|djoqn|t |s                        G         . = -)  :       !