Lines Matching refs:object
12 isroutine() - check object types
13 getmembers() - get members of an object that satisfy a given condition
15 getfile(), getsourcefile(), getsource() - find an object's source code
16 getdoc(), getcomments() - get documentation on an object
17 getmodule() - determine the module that an object came from
47 # See Include/object.h
51 def ismodule(object):
52 """Return true if the object is a module.
57 return isinstance(object, types.ModuleType)
59 def isclass(object):
60 """Return true if the object is a class.
65 return isinstance(object, (type, types.ClassType))
67 def ismethod(object):
68 """Return true if the object is an instance method.
73 im_class class object in which this method belongs
74 im_func function object containing implementation of method
76 return isinstance(object, types.MethodType)
78 def ismethoddescriptor(object):
79 """Return true if the object is a method descriptor.
84 An object passing this test has a __get__ attribute but not a __set__
91 im_func attribute (etc) when an object passes ismethod()."""
92 return (hasattr(object, "__get__")
93 and not hasattr(object, "__set__") # else it's a data descriptor
94 and not ismethod(object) # mutual exclusion
95 and not isfunction(object)
96 and not isclass(object))
98 def isdatadescriptor(object):
99 """Return true if the object is a data descriptor.
106 return (hasattr(object, "__set__") and hasattr(object, "__get__"))
110 def ismemberdescriptor(object):
111 """Return true if the object is a member descriptor.
115 return isinstance(object, types.MemberDescriptorType)
118 def ismemberdescriptor(object):
119 """Return true if the object is a member descriptor.
127 def isgetsetdescriptor(object):
128 """Return true if the object is a getset descriptor.
132 return isinstance(object, types.GetSetDescriptorType)
135 def isgetsetdescriptor(object):
136 """Return true if the object is a getset descriptor.
142 def isfunction(object):
143 """Return true if the object is a user-defined function.
148 func_code code object containing compiled function bytecode
153 return isinstance(object, types.FunctionType)
155 def isgeneratorfunction(object):
156 """Return true if the object is a user-defined generator function.
161 return bool((isfunction(object) or ismethod(object)) and
162 object.func_code.co_flags & CO_GENERATOR)
164 def isgenerator(object):
165 """Return true if the object is a generator.
171 gi_code code object
172 gi_frame frame object or possibly None once the generator has
179 return isinstance(object, types.GeneratorType)
181 def istraceback(object):
182 """Return true if the object is a traceback.
185 tb_frame frame object at this level
188 tb_next next inner traceback object (called by this level)"""
189 return isinstance(object, types.TracebackType)
191 def isframe(object):
192 """Return true if the object is a frame object.
195 f_back next outer frame object (this frame's caller)
197 f_code code object being executed in this frame
207 return isinstance(object, types.FrameType)
209 def iscode(object):
210 """Return true if the object is a code object.
216 co_filename name of file in which this code object was created
220 co_name name with which this code object was defined
225 return isinstance(object, types.CodeType)
227 def isbuiltin(object):
228 """Return true if the object is a built-in function or method.
234 return isinstance(object, types.BuiltinFunctionType)
236 def isroutine(object):
237 """Return true if the object is any kind of function or method."""
238 return (isbuiltin(object)
239 or isfunction(object)
240 or ismethod(object)
241 or ismethoddescriptor(object))
243 def isabstract(object):
244 """Return true if the object is an abstract base class (ABC)."""
245 return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT)
247 def getmembers(object, predicate=None):
248 """Return all members of an object as (name, value) pairs sorted by name.
251 for key in dir(object):
253 value = getattr(object, key)
261 Attribute = namedtuple('Attribute', 'name kind defining_class object')
280 3. The object as obtained directly from the defining class's
282 data attributes: C.data is just a data object, but
291 # Get the object associated with the name, and where it was defined.
307 # Classify the object.
355 def getdoc(object):
356 """Get the documentation string for an object.
362 doc = object.__doc__
398 def getfile(object):
399 """Work out which source or compiled file an object was defined in."""
400 if ismodule(object):
401 if hasattr(object, '__file__'):
402 return object.__file__
403 raise TypeError('{!r} is a built-in module'.format(object))
404 if isclass(object):
405 object = sys.modules.get(object.__module__)
406 if hasattr(object, '__file__'):
407 return object.__file__
408 raise TypeError('{!r} is a built-in class'.format(object))
409 if ismethod(object):
410 object = object.im_func
411 object):
412 object = object.func_code
413 if istraceback(object):
414 object = object.tb_frame
415 if isframe(object):
416 object = object.f_code
417 if iscode(object):
418 return object.co_filename
420 'function, traceback, frame, or code object'.format(object))
440 def getsourcefile(object):
441 """Return the filename that can be used to locate an object's source.
444 filename = getfile(object)
454 if hasattr(getmodule(object, filename), '__loader__'):
460 def getabsfile(object, _filename=None):
461 """Return an absolute path to the source or compiled file for an object.
463 The idea is for each object to have a unique origin, so this routine
466 _filename = getsourcefile(object) or getfile(object)
472 def getmodule(object, _filename=None):
473 """Return the module an object was defined in, or None if not found."""
474 if ismodule(object):
475 return object
476 if hasattr(object, '__module__'):
477 return sys.modules.get(object.__module__)
483 file = getabsfile(object, _filename)
505 if not hasattr(object, '__name__'):
507 if hasattr(main, object.__name__):
508 mainobject = getattr(main, object.__name__)
509 if mainobject is object:
513 if hasattr(builtin, object.__name__):
514 builtinobject = getattr(builtin, object.__name__)
515 if builtinobject is object:
518 def findsource(object):
519 """Return the entire source file and starting line number for an object.
522 or code object. The source code is returned as a list of all the lines
526 file = getfile(object)
527 sourcefile = getsourcefile(object)
532 module = getmodule(object, file)
540 if ismodule(object):
543 if isclass(object):
544 name = object.__name__
566 if ismethod(object):
567 object = object.im_func
568 if isfunction(object):
569 object = object.func_code
570 if istraceback(object):
571 object = object.tb_frame
572 if isframe(object):
573 object = object.f_code
574 if iscode(object):
575 if not hasattr(object, 'co_firstlineno'):
577 lnum = object.co_firstlineno - 1
583 raise IOError('could not find code object')
585 def getcomments(object):
586 """Get lines of comments immediately preceding an object's source code.
591 lines, lnum = findsource(object)
595 if ismodule(object):
682 def getsourcelines(object):
683 """Return a list of source lines and starting line number for an object.
686 or code object. The source code is returned as a list of the lines
687 corresponding to the object and the line number indicates where in the
690 lines, lnum = findsource(object)
692 if ismodule(object): return lines, 0
695 def getsource(object):
696 """Return the text of the source code for an object.
699 or code object. The source code is returned as a single string. An
701 lines, lnum = getsourcelines(object)
744 """Get information about the arguments accepted by a code object.
751 raise TypeError('{!r} is not a code object'.format(co))
837 def strseq(object, convert, join=joinseq):
839 if type(object) in (list, tuple):
840 return join(map(lambda o, c=convert, j=join: strseq(o, c, j), object))
842 return convert(object)
988 """Get information about a frame or traceback object.
1001 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
1021 """Get the line number from a frame object, allowing for optimization."""
1028 Each record contains a frame object, filename, line number, function
1039 Each record contains a frame object, filename, line number, function