defisabstract(object):"""Return true if the object is an abstract base class (ABC)."""ifnotisinstance(object,type):returnFalseifobject.__flags__&TPFLAGS_IS_ABSTRACT:returnTrueifnotissubclass(type(object),abc.ABCMeta):returnFalseifhasattr(object,'__abstractmethods__'):# It looks like ABCMeta.__new__ has finished running;# TPFLAGS_IS_ABSTRACT should have been accurate.returnFalse# It looks like ABCMeta.__new__ has not finished running yet; we're# probably in __init_subclass__. We'll look for abstractmethods manually.forname,valueinobject.__dict__.items():ifgetattr(value,"__isabstractmethod__",False):returnTrueforbaseinobject.__bases__:fornameingetattr(base,"__abstractmethods__",()):value=getattr(object,name,None)ifgetattr(value,"__isabstractmethod__",False):returnTruereturnFalse
defisasyncgenfunction(obj):"""Return true if the object is an asynchronous generator function. Asynchronous generator functions are defined with "async def" syntax and have "yield" expressions in their body. """return_has_code_flag(obj,CO_ASYNC_GENERATOR)
defisawaitable(object):"""Return true if object can be passed to an ``await`` expression."""return(isinstance(object,types.CoroutineType)orisinstance(object,types.GeneratorType)andbool(object.gi_code.co_flags&CO_ITERABLE_COROUTINE)orisinstance(object,collections.abc.Awaitable))
defiscode(object):"""Return true if the object is a code object. Code objects provide these attributes: co_argcount number of arguments (not including *, ** args or keyword only arguments) co_code string of raw compiled bytecode co_cellvars tuple of names of cell variables 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 | 16=nested | 32=generator | 64=nofree | 128=coroutine | 256=iterable_coroutine | 512=async_generator co_freevars tuple of names of free variables co_posonlyargcount number of positional only arguments co_kwonlyargcount number of keyword only arguments (not including ** 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 other than arguments and function locals co_nlocals number of local variables co_stacksize virtual machine stack space required co_varnames tuple of names of arguments and local variables"""returnisinstance(object,types.CodeType)
defiscoroutinefunction(obj):"""Return true if the object is a coroutine function. Coroutine functions are normally defined with "async def" syntax, but may be marked via markcoroutinefunction. """return_has_code_flag(obj,CO_COROUTINE)or_has_coroutine_mark(obj)
defisdatadescriptor(object):"""Return true if the object is a data descriptor. But not if ismethod(), isclass() or isfunction() is true. Data descriptors have a __set__ or a __delete__ attribute. Examples are properties, getsets, and members. For the latter two (defined only in C extension modules) more specific tests are available as well: isgetsetdescriptor() and ismemberdescriptor(), respectively. Typically, data descriptors will also have __name__ and __doc__ attributes (properties, getsets, and members have both of these attributes), but this is not guaranteed."""ifisclass(object)orismethod(object)orisfunction(object):# mutual exclusionreturnFalsetp=type(object)returnhasattr(tp,"__set__")orhasattr(tp,"__delete__")
defisframe(object):"""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_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_trace tracing function for this frame, or None"""returnisinstance(object,types.FrameType)
defisfunction(object):"""Return 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 __code__ code object containing compiled function bytecode __defaults__ tuple of any default values for arguments __globals__ global namespace in which this function was defined __annotations__ dict of parameter annotations __kwdefaults__ dict of keyword only parameters with defaults"""returnisinstance(object,types.FunctionType)
defisgenerator(object):"""Return true if the object is a generator. Generator objects provide these attributes: __iter__ defined to support iteration 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 gi_suspended set to 1 when the generator is suspended at a yield point, 0 otherwise gi_yieldfrom object being iterated by yield from or None 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"""returnisinstance(object,types.GeneratorType)
defisgeneratorfunction(obj):"""Return true if the object is a user-defined generator function. Generator function objects provide the same attributes as functions. See help(isfunction) for a list of attributes."""return_has_code_flag(obj,CO_GENERATOR)
defisgetsetdescriptor(object):"""Return true if the object is a getset descriptor. getset descriptors are specialized descriptors defined in extension modules."""returnFalse
defismemberdescriptor(object):"""Return true if the object is a member descriptor. Member descriptors are specialized descriptors defined in extension modules."""returnFalse
defismethoddescriptor(object):"""Return true if the object is a method descriptor. But not if ismethod(), isclass() or isfunction() is true. An object passing this test (for example, int.__add__) has a __get__ attribute, but not a __set__ attribute or a __delete__ attribute. 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 (ismethod(), isclass(), isfunction()) make this function return false, simply because those other tests promise more -- you can, for example, count on having the __func__ attribute when an object passes ismethod()."""ifisclass(object)orismethod(object)orisfunction(object):# mutual exclusionreturnFalseifisinstance(object,functools.partial):# Lie for children. The addition of partial.__get__# doesn't currently change the partial objects behaviour,# not counting a warning about future changes.returnFalsetp=type(object)return(hasattr(tp,"__get__")andnothasattr(tp,"__set__")andnothasattr(tp,"__delete__"))
defisroutine(object):"""Return true if the object is any kind of function or method."""return(isbuiltin(object)orisfunction(object)orismethod(object)orismethoddescriptor(object)orismethodwrapper(object))
defistraceback(object):"""Return 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)"""returnisinstance(object,types.TracebackType)