def import_name(s1): if s1[0] in NON_TERMINAL: if s1[0] in NON_TERMINAL and s1[0] == 286: dot_name = "" module_name = "" for name in s1[1]: if not isinstance(name, int): module_name += name[1] if len(s1) == 2: dot_name = module_name elif len(s1) == 4: dot_name = s1[3][1] try: module = importlib.import_module(module_name) except ImportError: print("Import Error, No module named " + module_name) exit() new_module = Module(module_name) new_module.SYMBOL_LIST = [] syms = inspect.getmembers(module) for sym in syms: if inspect.isfunction(sym[1]): #new_module.SYMBOL_LIST.append(Function(dot_name+'.' + sym[0])) new_module.SYMBOL_LIST.append(Function(sym[0])) elif inspect.isbuiltin(sym[1]): new_module.SYMBOL_LIST.append(Function(sym[0])) elif inspect.ismethod(sym[1]): pass elif inspect.isgeneratorfunction: new_module.SYMBOL_LIST.append(Function(sym[0])) elif inspect.isgenerator(sym[1]): pass elif inspect.istraceback(sym[1]): pass elif inspect.isframe(sym[1]): pass elif inspect.iscode(sym[1]): pass elif inspect.isroutine(sym[1]): pass elif inspect.isabstract(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isgetsetdescriptor(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isclass(sym[1]): new_module.SYMBOL_LIST.append(Class(sym[0], [], [])) else: print(sym[0]) self.local_names.append(new_module) else: for j in range(1,len(s1)): import_name(s1[j])
def import_from(s1, module): syms = inspect.getmembers(module) str_syms = dir(module) name_as = "" if len(s1) == 4: name_as = s1[3][1] if not (s1[1][1] in str_syms): print("import error") exit() else: for sym in syms: if sym[0] == s1[1][1]: if inspect.isfunction(sym[1]): if len(s1) == 4: GLOBAL_SYMBOL_LIST.append(Function(name_as)) else: GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.isbuiltin(sym[1]): if len(s1) == 4: GLOBAL_SYMBOL_LIST.append(Function(name_as)) else: GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.ismethod(sym[1]): pass elif inspect.isgeneratorfunction: if len(s1) == 4: GLOBAL_SYMBOL_LIST.append(Function(name_as)) else: GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.isgenerator(sym[1]): pass elif inspect.istraceback(sym[1]): pass elif inspect.isframe(sym[1]): pass elif inspect.iscode(sym[1]): pass elif inspect.isroutine(sym[1]): pass elif inspect.isabstract(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isgetsetdescriptor(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isclass(sym[1]): if len(s1) == 4: GLOBAL_SYMBOL_LIST.append(Class(name_as)) else: GLOBAL_SYMBOL_LIST.append(Class(sym[0])) else: print(sym[0])
def parse_import(st): if st[0] == 283: import_name(st[2]) elif st[0] == 284: module_name = "" if type(st[2]) != type(1): for name in st[2]: if type(name) != type(1): module_name += name[1] try: module = importlib.import_module(module_name) except ImportError: print("Import Error, No module named " + module_name) exit() if len(st) == 5 and st[4][1] == "*": syms = inspect.getmembers(module) str_syms = dir(module) name_as = "" for sym in syms: if inspect.isfunction(sym[1]): GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.isbuiltin(sym[1]): GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.ismethod(sym[1]): pass elif inspect.isgeneratorfunction: GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.isgenerator(sym[1]): pass elif inspect.istraceback(sym[1]): pass elif inspect.isframe(sym[1]): pass elif inspect.iscode(sym[1]): pass elif inspect.isroutine(sym[1]): pass elif inspect.isabstract(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isgetsetdescriptor(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isclass(sym[1]): GLOBAL_SYMBOL_LIST.append(Class(sym[0])) else: print(sym[0]) else: for counter in range(len(st[4])): if not isinstance(st[4][counter], int) and st[4][counter][0] == 285: import_from(st[4][counter], module)
def import_name(s1): if s1[0] in NON_TERMINAL: if s1[0] in NON_TERMINAL and s1[0] == 286: dot_name = "" module_name = "" for name in s1[1]: if type(name) != type(1): module_name += name[1] if len(s1) == 2: dot_name = module_name elif len(s1) == 4: dot_name = s1[3][1] try: module = importlib.import_module(module_name) except ImportError: print("Import Error, No module named " + module_name) exit() a = dir(module) syms = inspect.getmembers(module) for sym in syms: if inspect.isfunction(sym[1]): GLOBAL_SYMBOL_LIST.append(Function(dot_name + "." + sym[0])) elif inspect.isbuiltin(sym[1]): GLOBAL_SYMBOL_LIST.append(Function(dot_name + "." + sym[0])) elif inspect.ismethod(sym[1]): pass elif inspect.isgeneratorfunction: GLOBAL_SYMBOL_LIST.append(Function(dot_name + "." + sym[0])) elif inspect.isgenerator(sym[1]): pass elif inspect.istraceback(sym[1]): pass elif inspect.isframe(sym[1]): pass elif inspect.iscode(sym[1]): pass elif inspect.isroutine(sym[1]): pass elif inspect.isabstract(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isgetsetdescriptor(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isclass(sym[1]): GLOBAL_SYMBOL_LIST.append(Class(dot_name + "." + sym[0], [], [])) else: print(sym[0]) else: for j in range(1, len(s1)): import_name(s1[j])
def parse_import(st): if st[0] == 283: import_name(st[2]) elif st[0] == 284: module_name = "" if type(st[2]) != type(1): for name in st[2]: if type(name) != type(1): module_name += name[1] try: module = importlib.import_module(module_name) except ImportError: print("Import Error, No module named " + module_name) exit() if len(st)==5 and st[4][1] == "*": syms = inspect.getmembers(module) str_syms = dir(module) name_as = "" for sym in syms: if inspect.isfunction(sym[1]): GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.isbuiltin(sym[1]): GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.ismethod(sym[1]): pass elif inspect.isgeneratorfunction: GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.isgenerator(sym[1]): pass elif inspect.istraceback(sym[1]): pass elif inspect.isframe(sym[1]): pass elif inspect.iscode(sym[1]): pass elif inspect.isroutine(sym[1]): pass elif inspect.isabstract(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isgetsetdescriptor(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isclass(sym[1]): GLOBAL_SYMBOL_LIST.append(Class(sym[0])) else: print(sym[0]) else: for counter in range(len(st[4])): if not isinstance(st[4][counter],int) and st[4][counter][0] == 285: import_from(st[4][counter], module)
def test_excluding_predicates(self): self.istest(inspect.isbuiltin, 'sys.exit') if check_impl_detail(): self.istest(inspect.isbuiltin, '[].append') self.istest(inspect.iscode, 'mod.spam.func_code') self.istest(inspect.isframe, 'tb.tb_frame') self.istest(inspect.isfunction, 'mod.spam') self.istest(inspect.ismethod, 'mod.StupidGit.abuse') self.istest(inspect.ismethod, 'git.argue') self.istest(inspect.ismodule, 'mod') self.istest(inspect.istraceback, 'tb') self.istest(inspect.isdatadescriptor, '__builtin__.file.closed') self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace') self.istest(inspect.isgenerator, '(x for x in xrange(2))') self.istest(inspect.isgeneratorfunction, 'generator_function_example') if hasattr(types, 'GetSetDescriptorType'): self.istest(inspect.isgetsetdescriptor, 'type(tb.tb_frame).f_locals') else: self.assertFalse( inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals)) if hasattr(types, 'MemberDescriptorType'): # App-level slots are member descriptors on both PyPy and # CPython, but the various built-in attributes are all # getsetdescriptors on PyPy. So check ismemberdescriptor() # with an app-level slot. self.istest(inspect.ismemberdescriptor, 'ExampleClassWithSlot.myslot') else: self.assertFalse( inspect.ismemberdescriptor(type(lambda: None).func_globals))
def get_size(obj, seen=None): """Recursively finds size of objects in bytes""" size = sys.getsizeof(obj) if seen is None: seen = set() obj_id = id(obj) if obj_id in seen: return 0 # Important mark as seen *before* entering recursion to gracefully handle # self-referential objects seen.add(obj_id) if hasattr(obj, '__dict__'): for cls in obj.__class__.__mro__: if '__dict__' in cls.__dict__: d = cls.__dict__['__dict__'] if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor(d): size += get_size(obj.__dict__, seen) break if isinstance(obj, dict): size += sum((get_size(v, seen) for v in obj.values())) size += sum((get_size(k, seen) for k in obj.keys())) elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)): size += sum((get_size(i, seen) for i in obj)) if hasattr(obj, '__slots__'): # can have __slots__ with __dict__ size += sum(get_size(getattr(obj, s), seen) for s in obj.__slots__ if hasattr(obj, s)) return size
def test_union1(self): obj1 = self.__union_prep1() obj2 = self.obj_constructor(**self.obj_data[1]) obj = obj1 + obj2 for x in inspect.getmembers(obj.__class__): if (inspect.isdatadescriptor(x[1]) or\ inspect.isgetsetdescriptor(x[1]) or\ inspect.ismemberdescriptor(x[1])) and\ not x[0].startswith("__") and\ not inspect.ismethod(x[1]): if getattr(obj2, x[0]): self.assertTrue(getattr(obj, x[0]) == getattr(obj2, x[0])) else: self.assertTrue(getattr(obj, x[0]) == getattr(obj1, x[0])) for member, v in _iteritems(self.obj_list_members): _list = getattr(obj, member) self.assertTrue(len(_list) == 4) for member, v in _iteritems(self.obj_dict_members): _d1 = dict([(k, v) for k, v in _iteritems(getattr(obj1, member))]) _d2 = dict([(k, v) for k, v in _iteritems(getattr(obj2, member))]) _d1.update(_d2) _dict = getattr(obj, member) _d = dict([(k,v) for k,v in _iteritems(_dict)]) self.assertTrue(_d == _d1)
def test_excluding_predicates(self): # XXX: Jython's PySystemState needs more work before this will # be doable. if not is_jython: self.istest(inspect.isbuiltin, 'sys.exit') self.istest(inspect.isbuiltin, '[].append') self.istest(inspect.isclass, 'mod.StupidGit') self.istest(inspect.iscode, 'mod.spam.func_code') self.istest(inspect.isframe, 'tb.tb_frame') self.istest(inspect.isfunction, 'mod.spam') self.istest(inspect.ismethod, 'mod.StupidGit.abuse') self.istest(inspect.ismethod, 'git.argue') self.istest(inspect.ismodule, 'mod') self.istest(inspect.istraceback, 'tb') self.istest(inspect.isdatadescriptor, '__builtin__.file.closed') self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace') self.istest(inspect.isgenerator, '(x for x in xrange(2))') self.istest(inspect.isgeneratorfunction, 'generator_function_example') if hasattr(types, 'GetSetDescriptorType'): self.istest(inspect.isgetsetdescriptor, 'type(tb.tb_frame).f_locals') else: self.failIf(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals)) if hasattr(types, 'MemberDescriptorType'): self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days') else: self.failIf(inspect.ismemberdescriptor(datetime.timedelta.days))
def get_size(obj, seen=None): """ Calcule la taille en octets d'un objet Python quelconque :param obj: Objet :param seen: Liste des objets déjà calculés (utilisé uniquement par la récursivité) :return: Taille en octets de l'objet """ size = sys.getsizeof(obj) if seen is None: seen = set() obj_id = id(obj) if obj_id in seen: return 0 # Important mark as seen *before* entering recursion to gracefully handle # self-referential objects seen.add(obj_id) if hasattr(obj, '__dict__'): for cls in obj.__class__.__mro__: if '__dict__' in cls.__dict__: d = cls.__dict__['__dict__'] if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor(d): size += get_size(obj.__dict__, seen) break if isinstance(obj, dict): size += sum((get_size(v, seen) for v in obj.values())) size += sum((get_size(k, seen) for k in obj.keys())) elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)): size += sum((get_size(i, seen) for i in obj)) return size
def get_scope_objects(names): """ Looks for the names defined with dir() in an objects and divides them into different object types. """ classes = {} funcs = {} stmts = {} members = {} for n in names: if '__' in n and n not in mixin_funcs: continue try: # this has a builtin_function_or_method exe = getattr(scope, n) except AttributeError: # happens e.g. in properties of # PyQt4.QtGui.QStyleOptionComboBox.currentText # -> just set it to None members[n] = None else: if inspect.isclass(scope): if is_in_base_classes(scope, n, exe): continue if inspect.isbuiltin(exe) or inspect.ismethod(exe) \ or inspect.ismethoddescriptor(exe): funcs[n] = exe elif inspect.isclass(exe): classes[n] = exe elif inspect.ismemberdescriptor(exe): members[n] = exe else: stmts[n] = exe return classes, funcs, stmts, members
def handleSlots(self, obj): pyobj = obj.pyobj flat = self.flatTypeDict(type(pyobj)) # Relies on type dictionary being flattened. for name, member in flat.iteritems(): assert not isinstance(name, program.AbstractObject), name assert not isinstance(member, program.AbstractObject), member # TODO Directly test for slot wrapper? # TODO slot wrapper for methods? isMember = inspect.ismemberdescriptor(member) # HACK some getsets may as well be members isMember |= inspect.isgetsetdescriptor(member) and member in self.getsetMember if isMember: try: value = member.__get__(pyobj, type(pyobj)) except: print "Error getting attribute %s" % name print "obj", pyobj for k, v in inspect.getmembers(pyobj): print '\t', k, repr(v) raise mangledName = self.compiler.slots.uniqueSlotName(member) obj.addSlot(self.__getObject(mangledName), self.__getObject(value))
def get_size(obj, seen=None): """Recursively finds size of objects in bytes https://github.com/bosswissam/pysize/blob/master/pysize.py 単体では動くが、locals().keys()を渡して回すとエラー """ size = sys.getsizeof(obj) if seen is None: seen = set() obj_id = id(obj) if obj_id in seen: return 0 # Important mark as seen *before* entering recursion to gracefully handle # self-referential objects seen.add(obj_id) if hasattr(obj, '__dict__'): for cls in obj.__class__.__mro__: if '__dict__' in cls.__dict__: d = cls.__dict__['__dict__'] if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor(d): size += get_size(obj.__dict__, seen) break #以下でtype型を検出したときにTypeError, その他にUnsupportedOperationがRaiseされること有り if isinstance(obj, dict): size += sum((get_size(v, seen) for v in obj.values())) size += sum((get_size(k, seen) for k in obj.keys())) elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)): size += sum((get_size(i, seen) for i in obj)) return size
def get_scope_objects(names): """ Looks for the names defined with dir() in an objects and divides them into different object types. """ classes = {} funcs = {} stmts = {} members = {} for n in names: try: # this has a builtin_function_or_method exe = getattr(scope, n) except AttributeError: # happens e.g. in properties of # PyQt4.QtGui.QStyleOptionComboBox.currentText # -> just set it to None members[n] = None else: if inspect.isclass(scope): if is_in_base_classes(scope, n, exe): continue if inspect.isbuiltin(exe) or inspect.ismethod(exe) \ or inspect.ismethoddescriptor(exe): funcs[n] = exe elif inspect.isclass(exe) or inspect.ismodule(exe): classes[n] = exe elif inspect.ismemberdescriptor(exe): members[n] = exe else: stmts[n] = exe return classes, funcs, stmts, members
def get_size(obj, seen=None): """ Recursively finds size of objects in bytes, from https://github.com/bosswissam/pysize/blob/master/pysize.py """ size = sys.getsizeof(obj) if seen is None: seen = set() obj_id = id(obj) if obj_id in seen: return 0 # Important mark as seen *before* entering recursion to gracefully handle # self-referential objects seen.add(obj_id) if hasattr(obj, '__dict__'): for cls in obj.__class__.__mro__: if '__dict__' in cls.__dict__: d = cls.__dict__['__dict__'] if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor( d): size += get_size(obj.__dict__, seen) break if isinstance(obj, dict): size += sum((get_size(v, seen) for v in obj.values())) size += sum((get_size(k, seen) for k in obj.keys())) elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)): size += sum((get_size(i, seen) for i in obj)) if hasattr(obj, '__slots__'): # can have __slots__ with __dict__ size += sum( get_size(getattr(obj, s), seen) for s in obj.__slots__ if hasattr(obj, s)) return float(size)
def test_excluding_predicates(self): #XXX: Jython's PySystemState needs more work before this #will be doable. if not test_support.is_jython: self.istest(inspect.isbuiltin, 'sys.exit') self.istest(inspect.isbuiltin, '[].append') self.istest(inspect.isclass, 'mod.StupidGit') self.istest(inspect.iscode, 'mod.spam.func_code') self.istest(inspect.isframe, 'tb.tb_frame') self.istest(inspect.isfunction, 'mod.spam') self.istest(inspect.ismethod, 'mod.StupidGit.abuse') self.istest(inspect.ismethod, 'git.argue') self.istest(inspect.ismodule, 'mod') self.istest(inspect.istraceback, 'tb') self.istest(inspect.isdatadescriptor, '__builtin__.file.closed') self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace') if hasattr(types, 'GetSetDescriptorType'): self.istest(inspect.isgetsetdescriptor, 'type(tb.tb_frame).f_locals') #XXX: This detail of PyFrames is not yet supported in Jython elif not test_support.is_jython: self.failIf(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals)) if hasattr(types, 'MemberDescriptorType'): self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days') else: self.failIf(inspect.ismemberdescriptor(datetime.timedelta.days))
def test_headers(): ''' Print the attribute names, sizes and offsets in the C structure Assuming that the sizes are correct and add up to an offset of 4100 bytes, everything should add up correctly. This information was taken from the WinSpec 2.6 Spectroscopy Software User Manual version 2.6B, page 251. If this table doesn't add up, something changed in the definitions of the datatype widths. Fix this in winspec.structs file and let me know! ''' import inspect, re A = Header() for i in [Header, AxisCalibration, ROIinfo]: fields = [] print '\n{:30s}[{:4s}]\tsize'.format(i, 'offs') for name, obj in inspect.getmembers(i): if inspect.isdatadescriptor(obj) and not inspect.ismemberdescriptor(obj) \ and not inspect.isgetsetdescriptor(obj): fields.append((name, obj)) fields.sort( key=lambda x: re.search('(?<=ofs=)([0-9]+)', str(x[1])).group(0), cmp=lambda x, y: cmp(int(x), int(y))) fields for name, obj in fields: print '{:30s}[{:4d}]\t{:4d}'.format(name, obj.size, obj.offset)
def print_variable_type(self, objType): default_vars=["__builtins__", "__doc__","__path__", "__cached__", "__file__", "__name__", "__package__", "__version__"] self.dprint("[ %s ]" %objType.__name__) self.indent();self.indent() for ModObj in self.Modules: for name in dir(ModObj): obj = getattr(ModObj, name) #print(name, ":", type(obj)) if not (inspect.isclass(obj) or inspect.isfunction(obj) or inspect.isroutine(obj) or inspect.isfunction(obj) or inspect.isgeneratorfunction(obj) or inspect.isgenerator(obj) or inspect.istraceback(obj) or inspect.isframe(obj) or inspect.iscode(obj) or inspect.isabstract(obj) or inspect.ismethoddescriptor(obj) or inspect.isdatadescriptor(obj) or inspect.isgetsetdescriptor(obj) or inspect.ismemberdescriptor(obj) or inspect.isbuiltin(obj)): if name not in default_vars: if type(obj) is objType: ObjName = ModObj.__name__ + '.' + name self.dprint("%s" %ObjName) self.dedent();self.dedent()
def _get_size(obj, seen=None): """ Recursively find the actual size of an object, in bytes. Taken as-is (with a tweak in function name) from https://github.com/bosswissam/pysize. """ size = sys.getsizeof(obj) if seen is None: seen = set() obj_id = id(obj) if obj_id in seen: return 0 # Important mark as seen *before* entering recursion to gracefully handle # self-referential objects seen.add(obj_id) if hasattr(obj, "__dict__"): for cls in obj.__class__.__mro__: if "__dict__" in cls.__dict__: d = cls.__dict__["__dict__"] if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor(d): size += _get_size(obj.__dict__, seen) break if isinstance(obj, dict): size += sum((_get_size(v, seen) for v in obj.values())) size += sum((_get_size(k, seen) for k in obj.keys())) elif hasattr(obj, "__iter__") and not isinstance(obj, (str, bytes, bytearray)): size += sum((_get_size(i, seen) for i in obj)) return size
def test_getset(self): obj = self.obj_constructor(**self.obj_data[0]) data_desc = [] for x in inspect.getmembers(obj.__class__): if (inspect.isdatadescriptor(x[1]) or\ inspect.isgetsetdescriptor(x[1]) or\ inspect.ismemberdescriptor(x[1])) and not x[0].startswith("__"): data_desc.append(x) for attr in data_desc: #z = getattr(obj, x[0]) attr_types = self.obj_getset[attr[0]] for _type, vals in _iteritems(self.obj_types): if _type not in attr_types: with self.assertRaises(TypeError): setattr(obj, attr[0], vals[0]) else: for val in vals: setattr(obj, attr[0], val) with self.assertRaises(TypeError): self.assertTrue(delattr(obj, x[0]), x[0]) setattr(obj, attr[0], self.obj_types[self.obj_getset[attr[0]][0]][0]) z = getattr(obj, attr[0]) self.assertTrue(self.obj_types[self.obj_getset[attr[0]][0]][0] == z)
def handleSlots(self, obj): pyobj = obj.pyobj flat = self.flatTypeDict(type(pyobj)) # Relies on type dictionary being flattened. for name, member in flat.iteritems(): assert not isinstance(name, program.AbstractObject), name assert not isinstance(member, program.AbstractObject), member # TODO Directly test for slot wrapper? # TODO slot wrapper for methods? isMember = inspect.ismemberdescriptor(member) # HACK some getsets may as well be members isMember |= inspect.isgetsetdescriptor( member) and member in self.getsetMember if isMember: try: value = member.__get__(pyobj, type(pyobj)) except: print "Error getting attribute %s" % name print "obj", pyobj for k, v in inspect.getmembers(pyobj): print '\t', k, repr(v) raise mangledName = self.compiler.slots.uniqueSlotName(member) obj.addSlot(self.__getObject(mangledName), self.__getObject(value))
def describe_module(module, kind, constants=[]): """ Describe the module object passed as argument including its classes and functions. """ module_name = module.__name__ if kind == object_types.LIBRARY: klass = Library(module_name) else: klass = Module(module_name, kind) klass.docs = getdoc(module) klass.comments = getcomments(module) klass.filename = module.__file__ inheritance_diagram = [] count = 0 for name in dir(module): if name in EXCLUDED_ATTRS: continue obj = getattr(module, name) if ismodule(obj): continue if ismemberdescriptor(obj) or isgetsetdescriptor(obj): continue if isclass(obj): count += 1 describe_class(obj, klass, module_name, constants) if obj.__module__ == module.__name__: inheritance_diagram.append(obj) elif isbuiltin(obj): count += 1 elif ismethod(obj) or isfunction(obj) or ismethoddescriptor(obj) or \ isinstance(obj, types.MethodType): count += 1 describe_func(obj, klass, module_name) else: attribute = Attribute(module_name + '.' + name, type(obj), obj) klass.Add(attribute) if constants: attribute.is_redundant = name not in constants if kind not in [object_types.PACKAGE, object_types.LIBRARY]: if inheritance_diagram and len(inheritance_diagram) < 20: klass.inheritance_diagram = inheritance.InheritanceDiagram( inheritance_diagram, klass) return klass, count
def calculate_byte_size_recursively(obj: object, seen: set = None) -> int: """ Recursively calculate size of objects in memory in bytes. From: https://github.com/bosswissam/pysize. Meant as a helper function for `get_byte_size`. Args: obj (object): The python object to get the size of seen (set, optional): This variable is needed to for the recusrive function evaluations, to ensure each object only gets counted once. Leave it at "None" to get the full byte size of an object. Defaults to None. Returns: int: The size of the object in bytes """ # Note: getsizeof alone is not enough, as it only returns the size of the top # level object, not of its member variables/objects. Hence the recursive calls. size = getsizeof(obj) if seen is None: # At first iteration (top level object), initialize 'seen' as empty set seen = set() obj_id = id(obj) if obj_id in seen: # If object was already counted, return 0 size to avoid double counting. return 0 # Important: Mark as seen *before* entering recursion to handle # self-referential objects seen.add(obj_id) if hasattr(obj, "__dict__"): # handles class objects for cls in obj.__class__.__mro__: if "__dict__" in cls.__dict__: d = cls.__dict__["__dict__"] if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor( d): # Recursively calculate size of member objects & variables size += calculate_byte_size_recursively(obj.__dict__, seen) break if isinstance(obj, dict): # handles dictionaries size += sum( (calculate_byte_size_recursively(v, seen) for v in obj.values())) size += sum( (calculate_byte_size_recursively(k, seen) for k in obj.keys())) elif hasattr(obj, "__iter__") and not isinstance(obj, (str, bytes, bytearray)): # handles array like objects (need to exclude str, bytes bytearray since they # also implement __iter__) size += sum((calculate_byte_size_recursively(i, seen) for i in obj)) if hasattr(obj, "__slots__"): # can have __slots__ with __dict__ size += sum( calculate_byte_size_recursively(getattr(obj, s), seen) for s in obj.__slots__ if hasattr(obj, s)) return size
def print_offsets(): ''' Print the attribute names, sizes and offsets in the C structure Assuming that the sizes are correct and add up to an offset of 4100 bytes, everything should add up correctly. This information was taken from the WinSpec 2.6 Spectroscopy Software User Manual version 2.6B, page 251. If this table doesn't add up, something changed in the definitions of the datatype widths. Fix this in winspec.structs file and let me know! ''' import inspect, re A = Header() for i in [Header, AxisCalibration, ROIinfo]: fields = [] print('\n{:30s}[{:4s}]\tsize'.format(repr(i), 'offs')) for name,obj in inspect.getmembers(i): if inspect.isdatadescriptor(obj) and not inspect.ismemberdescriptor(obj) \ and not inspect.isgetsetdescriptor(obj): fields.append((name, obj)) fields = sorted(fields, key=lambda x: x[1].offset) for name, obj in fields: print('{:30s}[{:4d}]\t{:4d}'.format(name, obj.size, obj.offset))
def test_excluding_predicates(self): #self.istest(inspect.isbuiltin, 'sys.exit') # Not valid for Jython self.istest(inspect.isbuiltin, '[].append') self.istest(inspect.iscode, 'mod.spam.func_code') self.istest(inspect.isframe, 'tb.tb_frame') self.istest(inspect.isfunction, 'mod.spam') self.istest(inspect.ismethod, 'mod.StupidGit.abuse') self.istest(inspect.ismethod, 'git.argue') self.istest(inspect.ismodule, 'mod') self.istest(inspect.istraceback, 'tb') self.istest(inspect.isdatadescriptor, '__builtin__.file.closed') self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace') self.istest(inspect.isgenerator, '(x for x in xrange(2))') self.istest(inspect.isgeneratorfunction, 'generator_function_example') if hasattr(types, 'GetSetDescriptorType'): self.istest(inspect.isgetsetdescriptor, 'type(tb.tb_frame).f_locals') else: self.assertFalse(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals)) if hasattr(types, 'MemberDescriptorType'): # Not valid for Jython # self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days') pass else: self.assertFalse(inspect.ismemberdescriptor(datetime.timedelta.days))
def struct_code(thing): """ returns code structure, given an object or a path to an object.""" result = dict() obj, result['name'] = resolve(thing) result['type'], name = describe(obj) module = inspect.getmodule(obj) result['module_name'] = module.__name__ result['module_doc'] = module.__doc__ try: result['file'] = inspect.getabsfile(obj) except TypeError: result['file'] = '(built-in)' if type(obj) is _OLD_INSTANCE_TYPE: # If the passed object is an instance of an old-style class, # document its available methods instead of its value. obj = obj.__class__ elif not (inspect.ismodule(obj) or inspect.isclass(obj) or inspect.isroutine(obj) or inspect.isgetsetdescriptor(obj) or inspect.ismemberdescriptor(obj) or isinstance(obj, property)): # If the passed object is a piece of data or an instance, # document its available methods instead of its value. obj = type(obj) code_struct = PyCodeStruct() result['struct'] = code_struct.struct(obj, result['name']) return result
def test_headers(): ''' Print the attribute names, sizes and offsets in the C structure Assuming that the sizes are correct and add up to an offset of 4100 bytes, everything should add up correctly. This information was taken from the WinSpec 2.6 Spectroscopy Software User Manual version 2.6B, page 251. If this table doesn't add up, something changed in the definitions of the datatype widths. Fix this in winspec.structs file and let me know! ''' import inspect, re A = Header() for i in [Header, AxisCalibration, ROIinfo]: fields = [] print '\n{:30s}[{:4s}]\tsize'.format(i, 'offs') for name,obj in inspect.getmembers(i): if inspect.isdatadescriptor(obj) and not inspect.ismemberdescriptor(obj) \ and not inspect.isgetsetdescriptor(obj): fields.append((name, obj)) fields.sort(key=lambda x: re.search('(?<=ofs=)([0-9]+)', str(x[1])).group(0), cmp=lambda x,y: cmp(int(x),int(y))); fields for name, obj in fields: print '{:30s}[{:4d}]\t{:4d}'.format(name, obj.size, obj.offset)
def get_size(obj, seen=None): """Recursively finds size of objects in bytes""" import sys import inspect size = sys.getsizeof(obj) if seen is None: seen = set() obj_id = id(obj) if obj_id in seen: return 0 # Important mark as seen *before* entering recursion to gracefully handle # self-referential objects seen.add(obj_id) if hasattr(obj, '__dict__'): for cls in obj.__class__.__mro__: if '__dict__' in cls.__dict__: d = cls.__dict__['__dict__'] if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor( d): size += get_size(obj.__dict__, seen) break if isinstance(obj, dict): size += sum((get_size(v, seen) for v in obj.values())) size += sum((get_size(k, seen) for k in obj.keys())) elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)): size += sum((get_size(i, seen) for i in obj)) return size
def test_excluding_predicates(self): self.istest(inspect.isbuiltin, 'sys.exit') self.istest(inspect.isbuiltin, '[].append') self.istest(inspect.iscode, 'mod.spam.__code__') self.istest(inspect.isframe, 'tb.tb_frame') self.istest(inspect.isfunction, 'mod.spam') self.istest(inspect.isfunction, 'mod.StupidGit.abuse') self.istest(inspect.ismethod, 'git.argue') self.istest(inspect.ismodule, 'mod') self.istest(inspect.istraceback, 'tb') self.istest(inspect.isdatadescriptor, 'collections.defaultdict.default_factory') self.istest(inspect.isgenerator, '(x for x in range(2))') self.istest(inspect.isgeneratorfunction, 'generator_function_example') if hasattr(types, 'GetSetDescriptorType'): self.istest(inspect.isgetsetdescriptor, 'type(tb.tb_frame).f_locals') else: self.assertFalse( inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals)) if hasattr(types, 'MemberDescriptorType'): self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days') else: self.assertFalse( inspect.ismemberdescriptor(datetime.timedelta.days))
def print_variable_type(self, objType): default_vars = [ "__builtins__", "__doc__", "__path__", "__cached__", "__file__", "__name__", "__package__", "__version__" ] self.dprint("[@ %s ]" % objType.__name__) self.indent() self.indent() for ModObj in self.Modules: for name in dir(ModObj): obj = getattr(ModObj, name) #print(name, ":", type(obj)) if not (inspect.isclass(obj) or inspect.isfunction(obj) or inspect.isroutine(obj) or inspect.isfunction(obj) or inspect.isgeneratorfunction(obj) or inspect.isgenerator(obj) or inspect.istraceback(obj) or inspect.isframe(obj) or inspect.iscode(obj) or inspect.isabstract(obj) or inspect.ismethoddescriptor(obj) or inspect.isdatadescriptor(obj) or inspect.isgetsetdescriptor(obj) or inspect.ismemberdescriptor(obj) or inspect.isbuiltin(obj)): if name not in default_vars: if type(obj) is objType: ObjName = ModObj.__name__ + '.' + name self.dprint("%s" % ObjName) self.dedent() self.dedent()
def print_offsets(): ''' Print the attribute names, sizes and offsets in the C structure Assuming that the sizes are correct and add up to an offset of 4100 bytes, everything should add up correctly. This information was taken from the WinSpec 2.6 Spectroscopy Software User Manual version 2.6B, page 251. If this table doesn't add up, something changed in the definitions of the datatype widths. Fix this in winspec.structs file and let me know! ''' import inspect, re A = Header() for i in [Header, AxisCalibration, ROIinfo]: fields = [] print('\n{:30s}[{:4s}]\tsize'.format(repr(i), 'offs')) for name, obj in inspect.getmembers(i): if inspect.isdatadescriptor(obj) and not inspect.ismemberdescriptor(obj) \ and not inspect.isgetsetdescriptor(obj): fields.append((name, obj)) fields = sorted(fields, key=lambda x: x[1].offset) for name, obj in fields: print('{:30s}[{:4d}]\t{:4d}'.format(name, obj.size, obj.offset))
def check_class(klass): assert inspect.isclass(klass) docstrings = [] fields = [] properties = [] methods = [] for name, member in inspect.getmembers(klass): # print(name, "::", member) if inspect.ismemberdescriptor(member): pass # print("Method desc", name, "::", member) elif inspect.isroutine(member): methods.append(member) elif inspect.ismemberdescriptor(member): pass # print("mem decs", name, "::", member) elif isinstance(member, property): # print("property", name, "::", member) properties.append((name, member)) elif name == "__doc__": if member is not None: docstrings.append(member) elif name in ["__class__", "__module__"]: pass else: pass # print("Unknown type", name, "::", member) for field in fields: #print("FIELD", field.__doc__) pass # write properties for prop_name, prop in properties: T1, T2 = get_property_type(prop, module_name=klass.__module__) assert has_no_cpp_type_in_signature(prop.fget.__doc__) if T2 != "None": assert has_no_cpp_type_in_signature(prop.fset.__doc__) for method in methods: signatures = get_function_signature(method) for sign in signatures: assert has_no_cpp_type_in_signature(method.__doc__)
def describe_module(module, kind, constants=[]): """ Describe the module object passed as argument including its classes and functions. """ module_name = module.__name__ if kind == object_types.LIBRARY: klass = Library(module_name) else: klass = Module(module_name, kind) klass.docs = getdoc(module) klass.comments = getcomments(module) klass.filename = module.__file__ inheritance_diagram = [] count = 0 for name in dir(module): if name in EXCLUDED_ATTRS: continue obj = getattr(module, name) if ismodule(obj): continue if ismemberdescriptor(obj) or isgetsetdescriptor(obj): continue if isclass(obj): count += 1 describe_class(obj, klass, module_name, constants) if obj.__module__ == module.__name__: inheritance_diagram.append(obj) elif isbuiltin(obj): count += 1 elif ismethod(obj) or isfunction(obj) or ismethoddescriptor(obj) or \ isinstance(obj, types.MethodType): count +=1 describe_func(obj, klass, module_name) else: attribute = Attribute(module_name + '.' + name, type(obj), obj) klass.Add(attribute) if constants: attribute.is_redundant = name not in constants if kind not in [object_types.PACKAGE, object_types.LIBRARY]: if inheritance_diagram and len(inheritance_diagram) < 20: klass.inheritance_diagram = inheritance.InheritanceDiagram(inheritance_diagram, klass) return klass, count
def _help_aux(obj, attr_pred): attributes = {} properties = {} exception_properties = {} methods = {} class_methods = {} class_attrs = {} others = {} for attr in dir(obj): if not attr_pred(attr): continue attr_value = _inspect.getattr_static(obj, attr) if _inspect.ismemberdescriptor(attr_value) and not hasattr(obj, attr): # This happens for empty slots. continue try: value = getattr(obj, attr) except Exception as e: exception_properties[attr] = attr_value, e continue if _inspect.ismemberdescriptor(attr_value): attributes[attr] = value elif _inspect.isdatadescriptor(attr_value): properties[attr] = attr_value, value elif isinstance(attr_value, classmethod) or isinstance( attr_value, staticmethod): class_methods[attr] = attr_value elif _inspect.isfunction(attr_value): methods[attr] = attr_value else: if value is not attr_value: others[attr] = attr_value, value # If an attribute is in both the type and the instance it will be said to be a class attribute. if hasattr(type(obj), attr) and value is getattr(type(obj), attr): class_attrs[attr] = attr_value else: attributes[attr] = attr_value return dict(attributes=attributes, properties=properties, exception_properties=exception_properties, methods=methods, class_methods=class_methods, class_attrs=class_attrs, others=others)
def get_size(obj, seen=None): """Recursively finds size of objects in bytes. Original source: https://github.com/bosswissam/pysize MIT License Copyright (c) [2018] [Wissam Jarjoui] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ size = sys.getsizeof(obj) if seen is None: seen = set() obj_id = id(obj) if obj_id in seen: return 0 # Important mark as seen *before* entering recursion to gracefully handle # self-referential objects seen.add(obj_id) if hasattr(obj, '__dict__'): for cls in obj.__class__.__mro__: if '__dict__' in cls.__dict__: d = cls.__dict__['__dict__'] if (inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor(d)): size += get_size(obj.__dict__, seen) break if isinstance(obj, dict): size += sum((get_size(v, seen) for v in obj.values())) size += sum((get_size(k, seen) for k in obj.keys())) elif (hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray))): size += sum((get_size(i, seen) for i in obj)) if hasattr(obj, '__slots__'): # can have __slots__ with __dict__ size += sum( get_size(getattr(obj, s), seen) for s in obj.__slots__ if hasattr(obj, s)) return size
def get_class_descriptors(cls: 'Type[object]') -> Sequence[str]: import inspect # Lazy import for minor startup speed win # Maintain a cache of type -> attributes defined by descriptors in the class # (that is, attributes from __slots__ and C extension classes) if cls not in fields_cache: members = inspect.getmembers( cls, lambda o: inspect.isgetsetdescriptor(o) or inspect.ismemberdescriptor(o)) fields_cache[cls] = [x for x, y in members if x != '__weakref__' and x != '__dict__'] return fields_cache[cls]
def _is_visible_type(attribute): return not (inspect.isfunction(attribute) or inspect.ismethod(attribute) or inspect.isbuiltin(attribute) or inspect.isroutine(attribute) or inspect.isclass(attribute) or inspect.ismodule(attribute) or inspect.istraceback(attribute) or inspect.isframe(attribute) or inspect.iscode(attribute) or inspect.isabstract(attribute) or inspect.ismethoddescriptor(attribute) or inspect.isdatadescriptor(attribute) or inspect.isgetsetdescriptor(attribute) or inspect.ismemberdescriptor(attribute))
def ismemberdescriptor(self): """Return true if the object is a member descriptor. Member descriptors are attributes defined in extension modules via PyMemberDef structures. For Python implementations without such types, this method will always return False. New in version 2.5. """ return inspect.ismemberdescriptor(self.obj)
def __init__(self, name, module, obj, *, docstring=None): assert isinstance(obj, type) if docstring is None: init_doc = inspect.getdoc(obj.__init__) or '' if init_doc == object.__init__.__doc__: init_doc = '' docstring = ((inspect.getdoc(obj) or '') + '\n\n' + init_doc).strip() super().__init__(name, module, obj, docstring=docstring) self.doc = {} """A mapping from identifier name to a `pdoc.Doc` objects.""" public_objs = [ (name, inspect.unwrap(obj)) for name, obj in inspect.getmembers(self.obj) # Filter only *own* members. The rest are inherited # in Class._fill_inheritance() if name in self.obj.__dict__ and _is_public(name) ] index = list(self.obj.__dict__).index public_objs.sort(key=lambda i: index(i[0])) var_docstrings, instance_var_docstrings = _pep224_docstrings(self) # Convert the public Python objects to documentation objects. for name, obj in public_objs: if _is_function(obj): self.doc[name] = Function( name, self.module, obj, cls=self, method=not self._method_type(self.obj, name)) else: self.doc[name] = Variable( name, self.module, docstring=var_docstrings.get(name) or inspect.getdoc(obj), cls=self, obj=getattr(obj, 'fget', getattr(obj, '__get__', obj)), instance_var=(inspect.isdatadescriptor(obj) or inspect.ismethoddescriptor(obj) or inspect.isgetsetdescriptor(obj) or inspect.ismemberdescriptor(obj) or name in getattr(self.obj, '__slots__', ()))) for name, docstring in instance_var_docstrings.items(): self.doc[name] = Variable(name, self.module, docstring, cls=self, obj=getattr(self.obj, name, None), instance_var=True)
def helpNonVerbose(thing, title='Python Library Documentation: %s', forceload=0): """ Utility method to return python help in the form of a string (based on the code in pydoc.py) Note: only a string (including unicode) should be passed in for "thing" """ import pydoc as pydocs import inspect import string result="" # Important for converting an incoming c++ unicode character string! thingStr=str(thing) """Display text documentation, given an object or a path to an object.""" try: # Possible two-stage object resolution! # Sometimes we get docs for strings, other times for objects # try: object, name = pydocs.resolve(thingStr, forceload) except: # Get an object from a string thingObj=eval(thingStr) object, name = pydocs.resolve(thingObj, forceload) desc = pydocs.describe(object) module = inspect.getmodule(object) if name and '.' in name: desc += ' in ' + name[:name.rfind('.')] elif module and module is not object: desc += ' in module ' + module.__name__ if not (inspect.ismodule(object) or inspect.isclass(object) or inspect.isroutine(object) or inspect.isgetsetdescriptor(object) or inspect.ismemberdescriptor(object) or isinstance(object, property)): # If the passed object is a piece of data or an instance, # document its available methods instead of its value. object = type(object) desc += ' object' text = pydocs.TextDoc() result=pydocs.plain(title % desc + '\n\n' + text.document(object, name)) # Remove multiple empty lines result = [ line for line in result.splitlines() if line.strip() ] result = string.join(result,"\n") except: pass return result
def get_desc(thing): if inspect.isclass(thing): return "Class" if inspect.ismethod(thing): return "Method" if inspect.ismemberdescriptor(thing): return "Member" if inspect.isfunction(thing): return "Function" return ""
def _clonable(of): sets = of.__dict__.copy() sets.update(type(of).__dict__.copy()) final = sets.copy() for key in sets: v = sets[key] if isfunction(v) or ismethod(v) or isgeneratorfunction(v) or isgenerator(v) \ or isroutine(v) or isabstract(v) or isclass(v) or ismodule(v) or istraceback(v) \ or isframe(v) or iscode(v) or isbuiltin(v) or ismethoddescriptor(v) \ or isdatadescriptor(v) or isgetsetdescriptor(v) or ismemberdescriptor(v) \ or v is None or v == '__main__' or key == '__module__': final.pop(key) return final
def struct(self, obj, name=None, *args): """Generate structure for an object.""" args = (obj, name) + args if inspect.isgetsetdescriptor(obj): return self.struct_data(*args) if inspect.ismemberdescriptor(obj): return self.struct_data(*args) if inspect.ismodule(obj): return self.struct_module(*args) if inspect.isclass(obj): return self.struct_class(*args) if inspect.isroutine(obj): return self.struct_routine(*args) if isinstance(obj, property): return self.struct_property(*args) return self.struct_date(*args)
def is_data_object(obj): """Return True of obj holds data This routine is used to distinguish objects we should show help for (like modules, classes, methods, and so forth) from other types of object. """ # Test borrowed from pydoc.py return not (inspect.ismodule(obj) or inspect.isclass(obj) or inspect.isroutine(obj) or inspect.isgetsetdescriptor(obj) or inspect.ismemberdescriptor(obj) or isinstance(obj, property))
def import_builtin(): module = importlib.import_module("builtins") syms = inspect.getmembers(module) for sym in syms: if inspect.isfunction(sym[1]): GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.isbuiltin(sym[1]): GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.ismethod(sym[1]): pass elif inspect.isgeneratorfunction: # print(sym[0]) GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.isgenerator(sym[1]): pass elif inspect.istraceback(sym[1]): pass elif inspect.isframe(sym[1]): pass elif inspect.iscode(sym[1]): pass elif inspect.isroutine(sym[1]): pass elif inspect.isabstract(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isgetsetdescriptor(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isclass(sym[1]): GLOBAL_SYMBOL_LIST.append(Class(sym[0])) else: print(sym[0])
def import_builtin(): module = importlib.import_module('builtins') syms = inspect.getmembers(module) for sym in syms: if inspect.isfunction(sym[1]): GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.isbuiltin(sym[1]): GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.ismethod(sym[1]): pass elif inspect.isgeneratorfunction: GLOBAL_SYMBOL_LIST.append(Function(sym[0])) elif inspect.isgenerator(sym[1]): pass elif inspect.istraceback(sym[1]): pass elif inspect.isframe(sym[1]): pass elif inspect.iscode(sym[1]): pass elif inspect.isroutine(sym[1]): pass elif inspect.isabstract(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isdatadescriptor(sym[1]): pass elif inspect.isgetsetdescriptor(sym[1]): pass elif inspect.ismemberdescriptor(sym[1]): pass elif inspect.isclass(sym[1]): GLOBAL_SYMBOL_LIST.append(Class(sym[0])) else: print(sym[0])
def resolve_from_type(obj, name): raw = type_lookup(obj, name) # Resolve any descriptors. if (inspect.ismethoddescriptor(raw) or inspect.isgetsetdescriptor(raw) or inspect.ismemberdescriptor(raw)): getter = raw.__get__ else: try: getter = resolve_from_type(raw, '__get__') except AttributeError: # Not a descriptor. return raw return getter(obj, type(obj))
def _render_member_icon(member, cell): pbcache = PixbufCache.getInstance() if inspect.ismethod(member) or inspect.ismethoddescriptor(member): pbname = 'dfeet-method.png' elif inspect.isdatadescriptor(member) or ( hasattr(inspect, 'ismemberdescriptor') and inspect.ismemberdescriptor(member)): pbname = 'dfeet-property.png' else: pbname = 'dfeet-object.png' pixbuf = pbcache.get(pbname, size=16, trystock=True, stocksize=gtk.ICON_SIZE_MENU) cell.set_property('pixbuf', pixbuf)
def _is_visible_type(attribute): return not ( inspect.isfunction(attribute) or inspect.ismethod(attribute) or inspect.isbuiltin(attribute) or inspect.isroutine(attribute) or inspect.isclass(attribute) or inspect.ismodule(attribute) or inspect.istraceback(attribute) or inspect.isframe(attribute) or inspect.iscode(attribute) or inspect.isabstract(attribute) or inspect.ismethoddescriptor(attribute) or inspect.isdatadescriptor(attribute) or inspect.isgetsetdescriptor(attribute) or inspect.ismemberdescriptor(attribute) )
def dump_value(value, depth=0): if depth > 10: return 'max_recursion_depth_exceeded' if isinstance(value, type): return dump_class(value, depth + 1) if inspect.isfunction(value): return dump_function(value) if callable(value): return {'type': 'callable'} # TODO more information if isinstance(value, types.ModuleType): return {'type': 'module'} # TODO module name if inspect.isdatadescriptor(value): return {'type': 'datadescriptor'} if inspect.ismemberdescriptor(value): return {'type': 'memberdescriptor'} return dump_simple(value)
def document(self, object, name=None, *args): """Generate documentation for an object.""" args = (object, name) + args # 'try' clause is to attempt to handle the possibility that inspect # identifies something in a way that pydoc itself has issues handling; # think 'super' and how it is a descriptor (which raises the exception # by lacking a __name__ attribute) and an instance. if inspect.isgetsetdescriptor(object): return self.docdata(*args) if inspect.ismemberdescriptor(object): return self.docdata(*args) try: if inspect.ismodule(object): return self.docmodule(*args) if inspect.isclass(object): return self.docclass(*args) if inspect.isroutine(object): return self.docroutine(*args) except AttributeError: pass if isinstance(object, property): return self.docproperty(*args) return self.docother(*args)
def getClassInfo(_class): parentClassName = inspect.getclasstree([_class])[0][0].__name__ writeDefinition("class %s(%s):\n" % (_class.__name__, parentClassName)) writeDefinition("\n") indent() for memberName in _class.__dict__: member = getattr(_class, memberName) if inspect.isbuiltin(member): getMemberInfo(member, isBuiltIn=True) continue if inspect.ismethod(member): getMemberInfo(member) continue if inspect.isfunction(member): getMemberInfo(member) continue if inspect.isroutine(member): getMemberInfo(member, isBuiltIn=True) continue if inspect.istraceback(member): getMemberInfo(member) continue if inspect.isframe(member): getMemberInfo(member) continue if inspect.iscode(member): getMemberInfo(member) continue if inspect.ismethoddescriptor(member): getMemberInfo(member) continue if inspect.isdatadescriptor(member): getMemberInfo(member) continue if inspect.isgetsetdescriptor(member): getMemberInfo(member) continue if inspect.ismemberdescriptor(member): getMemberInfo(member) dedent()
def describe(thing): """Produce a short description of the given thing. returns thing's type and name """ # inspect.isgeneratorfunction(object) # inspect.isgenerator(object) # inspect.istraceback(object) # inspect.isframe(object) # inspect.iscode(object) # inspect.isroutine(object) # inspect.isabstract(object) # inspect.ismethoddescriptor(object) # inspect.isdatadescriptor(object) if inspect.ismodule(thing): if thing.__name__ in sys.builtin_module_names: return ('built-in module', thing.__name__) if hasattr(thing, '__path__'): return ('package', thing.__name__) else: return ('module', thing.__name__) if inspect.isbuiltin(thing): return ('built-in function', thing.__name__) if inspect.isgetsetdescriptor(thing): return ( 'getset descriptor', '%s.%s.%s' % ( thing.__objclass__.__module__, thing.__objclass__.__name__, thing.__name__)) if inspect.ismemberdescriptor(thing): return ( 'member descriptor', '%s.%s.%s' % ( thing.__objclass__.__module__, thing.__objclass__.__name__, thing.__name__)) if inspect.isclass(thing): return ('class', thing.__name__) if inspect.isfunction(thing): return ('function', thing.__name__) if inspect.ismethod(thing): return ('method', thing.__name__) if type(thing) is types.InstanceType: return ('instance of', thing.__class__.__name__) return (type(thing).__name__, '')
def determine_object_type(obj): if inspect.ismodule(obj): return 'Module', obj.__str__() elif inspect.isclass(obj): return 'Class', obj.__str__() elif inspect.ismethod(obj): return 'Method', obj.__str__() elif inspect.isfunction(obj): return 'Function', obj.__str__() elif inspect.isgeneratorfunction(obj): return 'Generator Function', obj.__str__() elif inspect.isgenerator(obj): return 'Generator', obj.__str__() elif inspect.iscoroutinefunction(obj): return 'Coroutine Function', obj.__str__() elif inspect.iscoroutine(obj): return 'Coroutine', obj.__str__() elif inspect.isawaitable(obj): return 'Awaitable', obj.__str__() elif inspect.istraceback(obj): return 'Traceback', obj.__str__() elif inspect.isframe(obj): return 'Frame', obj.__str__() elif inspect.iscode(obj): return 'Code', obj.__str__() elif inspect.isbuiltin(obj): return 'Builtin', obj.__str__() elif inspect.isroutine(obj): return 'Routine', obj.__str__() elif inspect.isabstract(obj): return 'Abstract Base Class', obj.__str__() elif inspect.ismethoddescriptor(obj): return 'Method Descriptor', obj.__str__() elif inspect.isdatadescriptor(obj): return 'Data Descriptor', obj.__str__() elif inspect.isgetsetdescriptor(obj): return 'Getset Descriptor', obj.__str__() elif inspect.ismemberdescriptor(obj): return 'Member Descriptor', obj.__str__() elif inspect.isbuiltin(obj): return type(obj), obj.__str__() else: return type(obj), obj.__str__()
def _propsdict(obj): """Returns the @properties in an object. Example: >>> class PropertyCarrier(object): ... __slots__ = 'prop2' ... def __init__(self): ... self.prop2 = 3 ... @property ... def prop(self): ... return self.prop2 ... @prop.setter ... def prop(self, prop): ... self.prop2 = prop >>> _propsdict(PropertyCarrier()) {'prop': 3} """ descriptors = inspect.getmembers(obj.__class__, inspect.isdatadescriptor) # All data descriptors except slots and __weakref__ # See: http://docs.python.org/2/reference/datamodel.html return {dname: value.__get__(obj) for dname, value in descriptors if '__weakref__' != dname and not inspect.ismemberdescriptor(value)}