def test_lookup_special(self): from __pypy__ import lookup_special class X(object): def foo(self): return 42 x = X() x.foo = 23 x.bar = 80 assert lookup_special(x, "foo")() == 42 assert lookup_special(x, "bar") is None
def dir(*args): """dir([object]) -> list of strings Return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it: No argument: the names in the current scope. Module object: the module attributes. Type or class object: its attributes, and recursively the attributes of its bases. Otherwise: its attributes, its class's attributes, and recursively the attributes of its class's base classes. """ if len(args) > 1: raise TypeError("dir expected at most 1 arguments, got %d" % len(args)) if len(args) == 0: return sorted(_caller_locals().keys()) # 2 stackframes away obj = args[0] dir_meth = lookup_special(obj, '__dir__') if dir_meth is not None: # obscure: lookup_special won't bind None.__dir__! result = dir_meth(obj) if obj is None else dir_meth() # Will throw TypeError if not iterable return sorted(result) # we should never reach here since object.__dir__ exists return []
def dir(*args): """dir([object]) -> list of strings Return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it: No argument: the names in the current scope. Module object: the module attributes. Type or class object: its attributes, and recursively the attributes of its bases. Otherwise: its attributes, its class's attributes, and recursively the attributes of its class's base classes. """ if len(args) > 1: raise TypeError("dir expected at most 1 arguments, got %d" % len(args)) if len(args) == 0: return sorted(_caller_locals().keys()) # 2 stackframes away import types obj = args[0] dir_meth = lookup_special(obj, "__dir__") if dir_meth is not None: names = dir_meth() if not isinstance(names, list): raise TypeError("__dir__() must return a list, not %r" % ( type(names),)) names.sort() return names elif isinstance(obj, types.ModuleType): try: return sorted(obj.__dict__) except AttributeError: return [] elif isinstance(obj, type): # Don't look at __class__, as metaclass methods would be confusing. return sorted(_classdir(obj)) else: names = set() ns = getattr(obj, '__dict__', None) if isinstance(ns, dict): names.update(ns) klass = getattr(obj, '__class__', None) if klass is not None: names.update(_classdir(klass)) return sorted(names)
def dir(*args): """dir([object]) -> list of strings Return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it: No argument: the names in the current scope. Module object: the module attributes. Type or class object: its attributes, and recursively the attributes of its bases. Otherwise: its attributes, its class's attributes, and recursively the attributes of its class's base classes. """ if len(args) > 1: raise TypeError("dir expected at most 1 arguments, got %d" % len(args)) if len(args) == 0: return sorted(_caller_locals().keys()) # 2 stackframes away import types obj = args[0] dir_meth = lookup_special(obj, "__dir__") if dir_meth is not None: names = dir_meth() if not isinstance(names, list): raise TypeError("__dir__() must return a list, not %r" % (type(names), )) names.sort() return names elif isinstance(obj, types.ModuleType): try: return sorted(obj.__dict__) except AttributeError: return [] elif isinstance(obj, type): # Don't look at __class__, as metaclass methods would be confusing. return sorted(_classdir(obj)) else: names = set() ns = getattr(obj, '__dict__', None) if isinstance(ns, dict): names.update(ns) klass = getattr(obj, '__class__', None) if klass is not None: names.update(_classdir(klass)) return sorted(names)
def dir(*args): """dir([object]) -> list of strings Return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it: No argument: the names in the current scope. Module object: the module attributes. Type or class object: its attributes, and recursively the attributes of its bases. Otherwise: its attributes, its class's attributes, and recursively the attributes of its class's base classes. """ if len(args) > 1: raise TypeError("dir expected at most 1 arguments, got %d" % len(args)) if len(args) == 0: local_names = _caller_locals().keys() # 2 stackframes away if not isinstance(local_names, list): raise TypeError("expected locals().keys() to be a list") local_names.sort() return local_names # import types obj = args[0] if isinstance(obj, types.InstanceType): dir_meth = getattr(obj, '__dir__', None) else: dir_meth = lookup_special(obj, '__dir__') if dir_meth is not None: names = dir_meth() if not isinstance(names, list): raise TypeError("__dir__() must return a list, not %r" % (type(names), )) names.sort() return names # From here, this is python2-specific since in python3 # everything has a __dir__ elif isinstance(obj, types.ModuleType): try: return sorted(obj.__dict__) except AttributeError: return [] elif isinstance(obj, (types.TypeType, types.ClassType)): # Don't look at __class__, as metaclass methods would be confusing. return sorted(_classdir(obj)) else: names = set() ns = getattr(obj, '__dict__', None) if isinstance(ns, dict): names.update(ns) klass = getattr(obj, '__class__', None) if klass is not None: names.update(_classdir(klass)) ## Comment from object.c: ## /* Merge in __members__ and __methods__ (if any). ## XXX Would like this to go away someday; for now, it's ## XXX needed to get at im_self etc of method objects. */ for attr in '__members__', '__methods__': l = getattr(obj, attr, None) if not isinstance(l, list): continue names.extend(item for item in l if isinstance(item, str)) return sorted(names)
def isNumberType(obj, ): 'isNumberType(a) -- Return True if a has a numeric type, False otherwise.' return (__pypy__.lookup_special(obj, '__int__') is not None or __pypy__.lookup_special(obj, '__float__') is not None)
def dir(*args): """dir([object]) -> list of strings Return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it: No argument: the names in the current scope. Module object: the module attributes. Type or class object: its attributes, and recursively the attributes of its bases. Otherwise: its attributes, its class's attributes, and recursively the attributes of its class's base classes. """ if len(args) > 1: raise TypeError("dir expected at most 1 arguments, got %d" % len(args)) if len(args) == 0: local_names = _caller_locals().keys() # 2 stackframes away if not isinstance(local_names, list): raise TypeError("expected locals().keys() to be a list") local_names.sort() return local_names import types obj = args[0] dir_meth = None if isinstance(obj, types.InstanceType): try: dir_meth = getattr(obj, "__dir__") except AttributeError: pass else: dir_meth = lookup_special(obj, "__dir__") if dir_meth is not None: result = dir_meth() if not isinstance(result, list): raise TypeError("__dir__() must return a list, not %r" % ( type(result),)) result.sort() return result elif isinstance(obj, types.ModuleType): try: result = list(obj.__dict__) result.sort() return result except AttributeError: return [] elif isinstance(obj, (types.TypeType, types.ClassType)): #Don't look at __class__, as metaclass methods would be confusing. result = _classdir(obj).keys() result.sort() return result else: #(regular item) Dict = {} try: if isinstance(obj.__dict__, dict): Dict.update(obj.__dict__) except AttributeError: pass try: Dict.update(_classdir(obj.__class__)) except AttributeError: pass ## Comment from object.c: ## /* Merge in __members__ and __methods__ (if any). ## XXX Would like this to go away someday; for now, it's ## XXX needed to get at im_self etc of method objects. */ for attr in ['__members__','__methods__']: try: l = getattr(obj, attr) if not isinstance(l, list): continue for item in l: if isinstance(item, types.StringTypes): Dict[item] = None except (AttributeError, TypeError): pass result = Dict.keys() result.sort() return result
def dir(*args): """dir([object]) -> list of strings Return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it: No argument: the names in the current scope. Module object: the module attributes. Type or class object: its attributes, and recursively the attributes of its bases. Otherwise: its attributes, its class's attributes, and recursively the attributes of its class's base classes. """ if len(args) > 1: raise TypeError("dir expected at most 1 arguments, got %d" % len(args)) if len(args) == 0: local_names = _caller_locals().keys() # 2 stackframes away if not isinstance(local_names, list): raise TypeError("expected locals().keys() to be a list") local_names.sort() return local_names import types obj = args[0] if isinstance(obj, types.InstanceType): dir_meth = getattr(obj, '__dir__', None) else: dir_meth = lookup_special(obj, '__dir__') if dir_meth is not None: names = dir_meth() if not isinstance(names, list): raise TypeError("__dir__() must return a list, not %r" % ( type(names),)) names.sort() return names elif isinstance(obj, types.ModuleType): try: return sorted(obj.__dict__) except AttributeError: return [] elif isinstance(obj, (types.TypeType, types.ClassType)): # Don't look at __class__, as metaclass methods would be confusing. return sorted(_classdir(obj)) else: names = set() ns = getattr(obj, '__dict__', None) if isinstance(ns, dict): names.update(ns) klass = getattr(obj, '__class__', None) if klass is not None: names.update(_classdir(klass)) ## Comment from object.c: ## /* Merge in __members__ and __methods__ (if any). ## XXX Would like this to go away someday; for now, it's ## XXX needed to get at im_self etc of method objects. */ for attr in '__members__', '__methods__': l = getattr(obj, attr, None) if not isinstance(l, list): continue names.extend(item for item in l if isinstance(item, str)) return sorted(names)
def isNumberType(obj,): 'isNumberType(a) -- Return True if a has a numeric type, False otherwise.' return (__pypy__.lookup_special(obj, '__int__') is not None or __pypy__.lookup_special(obj, '__float__') is not None)
def dir(*args): """dir([object]) -> list of strings Return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it: No argument: the names in the current scope. Module object: the module attributes. Type or class object: its attributes, and recursively the attributes of its bases. Otherwise: its attributes, its class's attributes, and recursively the attributes of its class's base classes. """ if len(args) > 1: raise TypeError("dir expected at most 1 arguments, got %d" % len(args)) if len(args) == 0: local_names = _caller_locals().keys() # 2 stackframes away if not isinstance(local_names, list): raise TypeError("expected locals().keys() to be a list") local_names.sort() return local_names import types obj = args[0] dir_meth = None if isinstance(obj, types.InstanceType): try: dir_meth = getattr(obj, "__dir__") except AttributeError: pass else: dir_meth = lookup_special(obj, "__dir__") if dir_meth is not None: result = dir_meth() if not isinstance(result, list): raise TypeError("__dir__() must return a list, not %r" % (type(result), )) result.sort() return result elif isinstance(obj, types.ModuleType): try: result = list(obj.__dict__) result.sort() return result except AttributeError: return [] elif isinstance(obj, (types.TypeType, types.ClassType)): #Don't look at __class__, as metaclass methods would be confusing. result = _classdir(obj).keys() result.sort() return result else: #(regular item) Dict = {} try: if isinstance(obj.__dict__, dict): Dict.update(obj.__dict__) except AttributeError: pass try: Dict.update(_classdir(obj.__class__)) except AttributeError: pass ## Comment from object.c: ## /* Merge in __members__ and __methods__ (if any). ## XXX Would like this to go away someday; for now, it's ## XXX needed to get at im_self etc of method objects. */ for attr in ['__members__', '__methods__']: try: l = getattr(obj, attr) if not isinstance(l, list): continue for item in l: if isinstance(item, types.StringTypes): Dict[item] = None except (AttributeError, TypeError): pass result = Dict.keys() result.sort() return result