[pypy-commit] pypy py3.3: integrate cleanup from default branch, simplify

pjenvey noreply at buildbot.pypy.org
Sun Aug 3 21:56:40 CEST 2014


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: py3.3
Changeset: r72676:2015801d9e67
Date: 2014-08-03 11:25 -0700
http://bitbucket.org/pypy/pypy/changeset/2015801d9e67/

Log:	integrate cleanup from default branch, simplify

diff --git a/pypy/interpreter/module.py b/pypy/interpreter/module.py
--- a/pypy/interpreter/module.py
+++ b/pypy/interpreter/module.py
@@ -129,4 +129,4 @@
         if not space.isinstance_w(w_dict, space.w_dict):
             raise oefmt(space.w_TypeError, "%N.__dict__ is not a dictionary",
                         self)
-        return space.newlist(space.listview(w_dict))
+        return space.call_function(space.w_list, w_dict)
diff --git a/pypy/module/__builtin__/app_inspect.py b/pypy/module/__builtin__/app_inspect.py
--- a/pypy/module/__builtin__/app_inspect.py
+++ b/pypy/module/__builtin__/app_inspect.py
@@ -44,12 +44,9 @@
         return local_names
 
     obj = args[0]
-    dir_meth = lookup_special(obj, "__dir__")
+    dir_meth = lookup_special(obj, '__dir__')
     if dir_meth is not None:
-        result = dir_meth()
-        if not isinstance(result, list):
-            result = list(result)  # Will throw TypeError if not iterable
-        result.sort()
-        return result
-
-    return []  # we should never reach here since object.__dir__ exists
+        # Will throw TypeError if not iterable
+        return sorted(dir_meth())
+    # we should never reach here since object.__dir__ exists
+    return []
diff --git a/pypy/objspace/std/objecttype.py b/pypy/objspace/std/objecttype.py
--- a/pypy/objspace/std/objecttype.py
+++ b/pypy/objspace/std/objecttype.py
@@ -20,38 +20,8 @@
     return w_obj.getrepr(space, u'%s object' % (classname,))
 
 def descr__dir__(space, w_obj):
-    w_result = space.appexec([w_obj], """(obj):
-        def _classdir(klass):
-            Dict = {}
-            try:
-                Dict.update(klass.__dict__)
-            except AttributeError: pass
-            try:
-                bases = klass.__mro__
-            except AttributeError: pass
-            else:
-                try:
-                    #Note that since we are only interested in the keys,
-                    #  the order we merge classes is unimportant
-                    for base in bases:
-                        Dict.update(base.__dict__)
-                except TypeError: pass
-            return Dict
-
-        Dict = {}
-        try:
-            if isinstance(obj.__dict__, dict):
-                Dict.update(obj.__dict__)
-        except AttributeError:
-            pass
-        try:
-            Dict.update(_classdir(obj.__class__))
-        except AttributeError:
-            pass
-        result = list(Dict.keys())
-        return result
-    """)
-    return w_result
+    from pypy.objspace.std.util import _objectdir
+    return space.call_function(space.w_list, _objectdir(space, w_obj))
 
 def descr__str__(space, w_obj):
     w_type = space.type(w_obj)
diff --git a/pypy/objspace/std/typeobject.py b/pypy/objspace/std/typeobject.py
--- a/pypy/objspace/std/typeobject.py
+++ b/pypy/objspace/std/typeobject.py
@@ -731,28 +731,8 @@
         return space.get(w_result, space.w_None, w_type)
 
 def descr__dir(space, w_type):
-    w_result = space.appexec([w_type], """(obj):
-        def _classdir(klass):
-            Dict = {}
-            try:
-                Dict.update(klass.__dict__)
-            except AttributeError: pass
-            try:
-                bases = klass.__mro__
-            except AttributeError: pass
-            else:
-                try:
-                    #Note that since we are only interested in the keys,
-                    #  the order we merge classes is unimportant
-                    for base in bases:
-                        Dict.update(base.__dict__)
-                except TypeError: pass
-            return Dict
-
-        result = list(_classdir(obj).keys())
-        return result
-    """)
-    return w_result
+    from pypy.objspace.std.util import _classdir
+    return space.call_function(space.w_list, _classdir(space, w_type))
 
 def descr__flags(space, w_type):
     from copy_reg import _HEAPTYPE
diff --git a/pypy/objspace/std/util.py b/pypy/objspace/std/util.py
--- a/pypy/objspace/std/util.py
+++ b/pypy/objspace/std/util.py
@@ -24,3 +24,41 @@
         where = length
     assert where >= 0
     return where
+
+app = gateway.applevel(r'''
+    def _classdir(klass):
+        """__dir__ for type objects
+
+        This includes all attributes of klass and all of the base
+        classes recursively.
+        """
+        names = set()
+        ns = getattr(klass, '__dict__', None)
+        if ns is not None:
+            names.update(ns)
+        bases = getattr(klass, '__bases__', None)
+        if bases is not None:
+            # Note that since we are only interested in the keys, the order
+            # we merge classes is unimportant
+            for base in bases:
+                names.update(_classdir(base))
+        return names
+
+    def _objectdir(obj):
+        """__dir__ for generic objects
+
+         Returns __dict__, __class__ and recursively up the
+         __class__.__bases__ chain.
+        """
+        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 names
+''', filename=__file__)
+
+_classdir = app.interphook('_classdir')
+_objectdir = app.interphook('_objectdir')


More information about the pypy-commit mailing list