Author: Philip Jenvey <[email protected]>
Branch: py3.3
Changeset: r72683:799debfe0709
Date: 2014-08-03 12:57 -0700
http://bitbucket.org/pypy/pypy/changeset/799debfe0709/

Log:    Merged in kvas/pypy/py3.3 (pull request #261)

        Fixes for remaining test failures in test_builtin.py

diff --git a/pypy/interpreter/pytraceback.py b/pypy/interpreter/pytraceback.py
--- a/pypy/interpreter/pytraceback.py
+++ b/pypy/interpreter/pytraceback.py
@@ -50,6 +50,10 @@
         self.lasti = space.int_w(w_lasti)
         self.next = space.interp_w(PyTraceback, w_next, can_be_None=True)
 
+    def descr__dir__(self, space):
+        return space.newtuple([space.wrap(n) for n in
+            ['tb_frame', 'tb_next', 'tb_lasti', 'tb_lineno']])
+
 
 def record_application_traceback(space, operror, frame, last_instruction):
     if frame.pycode.hidden_applevel:
diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py
--- a/pypy/interpreter/typedef.py
+++ b/pypy/interpreter/typedef.py
@@ -910,6 +910,7 @@
 PyTraceback.typedef = TypeDef("traceback",
     __reduce__ = interp2app(PyTraceback.descr__reduce__),
     __setstate__ = interp2app(PyTraceback.descr__setstate__),
+    __dir__ = interp2app(PyTraceback.descr__dir__),
     tb_frame = interp_attrproperty('frame', cls=PyTraceback),
     tb_lasti = interp_attrproperty('lasti', cls=PyTraceback),
     tb_lineno = GetSetProperty(PyTraceback.descr_tb_lineno),
diff --git a/pypy/module/__builtin__/test/test_dir.py 
b/pypy/module/__builtin__/test/test_dir.py
--- a/pypy/module/__builtin__/test/test_dir.py
+++ b/pypy/module/__builtin__/test/test_dir.py
@@ -24,3 +24,82 @@
             def __dir__(self):
                 return 42
         raises(TypeError, dir, Foo())
+
+    def test_dir_traceback(self):
+        """Test dir() of traceback."""
+        import sys
+
+        try:
+            raise IndexError
+        except:
+            tb_dir = dir(sys.exc_info()[2])
+            assert tb_dir == ['tb_frame', 'tb_lasti', 'tb_lineno', 'tb_next']
+
+
+    def test_dir_object_inheritance(self):
+        """Dir should behave the same regardless of inheriting from object."""
+        class A:
+            pass
+
+        class B(object):
+            pass
+        assert dir(A) == dir(B)
+
+    def test_dir_sanity(self):
+        """Test that dir returns reasonable items."""
+        class A(object):
+            a = 1
+
+        class B(A):
+            y = 2
+
+        b = B()
+        b.z = 1
+
+        names = dir(b)
+        for name in 'ayz':
+            assert name in names
+
+        assert '__doc__' in names
+        assert '__module__' in names
+        assert '__dict__' in names
+        assert '__dir__' in names
+        assert '__weakref__' in names
+        assert '__class__' in names
+        assert '__format__' in names
+        # Not an exhaustive list, but will be enough if dir is very broken.
+
+    def test_dir_module(self):
+        import sys
+        assert dir(sys) == list(sorted(sys.__dict__))
+
+    def test_dir_list(self):
+        """Check that dir([]) has methods from list and from object."""
+        names = dir([])
+
+        dct = {}
+        dct.update(list.__dict__)
+        dct.update(object.__dict__)
+
+        assert names == sorted(dct)
+
+    def test_dir_builtins(self):
+        """Test that builtin objects have sane __dir__()."""
+        import sys
+
+        for builtin in [sys, object(), [], {}, {1}, "", 1, (), sys,
+                map(ord, "abc"), filter(None, "abc"), zip([1, 2], [3, 4]),
+                compile('1', '', 'exec')]:
+            assert sorted(builtin.__dir__()) == dir(builtin)
+
+    def test_dir_type(self):
+        """Test .__dir__() and dir(...) behavior on types.
+
+        * t.__dir__() throws a TypeError,
+        * dir(t) == sorted(t().__dir__())
+
+        This is the behavior that I observe with cpython3.3.2.
+        """
+        for t in [int, list, tuple, set, str]:
+            raises(TypeError, t.__dir__)
+            assert dir(t) == sorted(t().__dir__())
diff --git a/pypy/module/__builtin__/test/test_format.py 
b/pypy/module/__builtin__/test/test_format.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/__builtin__/test/test_format.py
@@ -0,0 +1,38 @@
+class AppTestFormat:
+
+    def test_format(self):
+        """Test deprecation warnings from format(object(), 'nonempty')"""
+
+        import warnings
+
+        def test_deprecated(obj, fmt_str, should_raise_warning):
+            with warnings.catch_warnings(record=True) as w:
+                warnings.simplefilter("always", DeprecationWarning)
+                format(obj, fmt_str)
+            if should_raise_warning:
+                assert len(w) == 1
+                assert isinstance(w[0].message, DeprecationWarning)
+                assert 'object.__format__ with a non-empty format string '\
+                        in str(w[0].message)
+            else:
+                assert len(w) == 0
+
+        fmt_strs = ['', 's']
+
+        class A:
+            def __format__(self, fmt_str):
+                return format('', fmt_str)
+
+        for fmt_str in fmt_strs:
+            test_deprecated(A(), fmt_str, False)
+
+        class B:
+            pass
+
+        class C(object):
+            pass
+
+        for cls in [object, B, C]:
+            for fmt_str in fmt_strs:
+                print(cls, fmt_str)
+                test_deprecated(cls(), fmt_str, len(fmt_str) != 0)
diff --git a/pypy/module/_io/interp_textio.py b/pypy/module/_io/interp_textio.py
--- a/pypy/module/_io/interp_textio.py
+++ b/pypy/module/_io/interp_textio.py
@@ -287,7 +287,8 @@
     try:
         w_locale = space.call_method(space.builtin, '__import__',
                                      space.wrap('locale'))
-        w_encoding = space.call_method(w_locale, 'getpreferredencoding')
+        w_encoding = space.call_method(w_locale, 'getpreferredencoding',
+                space.w_False)
     except OperationError as e:
         # getpreferredencoding() may also raise ImportError
         if not e.match(space, space.w_ImportError):
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
@@ -124,7 +124,7 @@
         raise OperationError(space.w_TypeError, space.wrap(msg))
     if space.len_w(w_format_spec) > 0:
         msg = "object.__format__ with a non-empty format string is deprecated"
-        space.warn(space.wrap(msg), space.w_PendingDeprecationWarning)
+        space.warn(space.wrap(msg), space.w_DeprecationWarning)
     return space.format(w_as_str, w_format_spec)
 
 def descr___subclasshook__(space, __args__):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to