[py-svn] r23424 - in py/dist/py: code code/testing test/terminal test/testing test/testing/data

hpk at codespeak.net hpk at codespeak.net
Thu Feb 16 19:56:19 CET 2006


Author: hpk
Date: Thu Feb 16 19:56:15 2006
New Revision: 23424

Removed:
   py/dist/py/code/safe_repr.py
   py/dist/py/code/testing/test_safe_repr.py
   py/dist/py/test/testing/data/brokenrepr.py
Modified:
   py/dist/py/code/frame.py
   py/dist/py/test/terminal/terminal.py
   py/dist/py/test/testing/test_session.py
Log:
bail out last patch from Jan/Tom - this breaks 
PyPy testing which provides different frame classes 
so we need to check more carefully. 

Jan, can you checkout codespeak.net/svn/pypy/dist and run 

    py.test -l pypy/tool/pytest/pypy_test_failure_demo.py

which will result in py.test bailing out (without
the patch it doesn't).  So we need to take some 
more care.  Probably we should add some tests 
to the py lib that provide frame/code classes
with fewer attributes (much like pypy does)
so that we can check our assumtions.  It's obviously
very hard for a py lib committer to know about
the involved problems otherwise. 

sorry for not having noticed this problem earlier. 



Modified: py/dist/py/code/frame.py
==============================================================================
--- py/dist/py/code/frame.py	(original)
+++ py/dist/py/code/frame.py	Thu Feb 16 19:56:15 2006
@@ -1,5 +1,5 @@
 import py
-import py.__.code.safe_repr
+
 
 class Frame(object):
     """Wrapper around a Python frame holding f_locals and f_globals
@@ -27,7 +27,7 @@
         exec code in self.f_globals, f_locals 
 
     def repr(self, object):
-        return py.__.code.safe_repr._repr(object)
+        return repr(object)
 
     def is_true(self, object):
         return object

Deleted: /py/dist/py/code/safe_repr.py
==============================================================================
--- /py/dist/py/code/safe_repr.py	Thu Feb 16 19:56:15 2006
+++ (empty file)
@@ -1,63 +0,0 @@
-"""Defines a safe repr function. This will always return a string of "reasonable" length
-no matter what the object does in it's own repr function. Let's examine what can go wrong
-in an arbitrary repr function.
-The default repr will return something like (on Win32 anyway):
-<foo.bar object at 0x008D5650>. Well behaved user-defined repr() methods will do similar.
-The usual expectation is that repr will return a single line string.
-
-1. However, the repr method can raise an exception of an arbitrary type.
-
-Also, the return value may not be as expected:
- 2. The return value may not be a string!
- 3. The return value may not be a single line string, it may contain line breaks.
- 4. The method may enter a loop and never return.
- 5. The return value may be enormous, eg range(100000)
- 
- The standard library has a nice implementation in the repr module that will do the job,
- but the exception
- handling is silent, so the the output contains no clue that repr() call raised an
- exception. I would like to be told if repr raises an exception, it's a serious error, so 
- a sublass of repr overrides the method that does repr for class instances."""
-
-
-import repr
-import __builtin__
-
- 
-class SafeRepr(repr.Repr):
-    def __init__(self, *args, **kwargs):
-        repr.Repr.__init__(self, *args, **kwargs)
-        # Do we need a commandline switch for this?
-        self.maxstring = 240 # 3 * 80 chars
-        self.maxother = 160    # 2 * 80 chars
-    def repr_instance(self, x, level):
-        try:
-            # Try the vanilla repr and make sure that the result is a string
-            s = str(__builtin__.repr(x))
-        except (KeyboardInterrupt, MemoryError, SystemExit):
-            raise
-        except Exception ,e:
-            try:
-                exc_name = e.__class__.__name__
-            except:
-                exc_name = 'unknown'
-            try:
-                exc_info = str(e)
-            except:
-                exc_info = 'unknown'
-            return '<[%s("%s") raised in repr()] %s object at 0x%x>' % \
-                (exc_name, exc_info, x.__class__.__name__, id(x))
-        except:
-            try:
-                name = x.__class__.__name__
-            except:
-                name = 'unknown'
-            return '<[unknown exception raised in repr()] %s object at 0x%x>' % \
-                (name, id(x))
-        if len(s) > self.maxstring:
-            i = max(0, (self.maxstring-3)//2)
-            j = max(0, self.maxstring-3-i)
-            s = s[:i] + '...' + s[len(s)-j:]
-        return s
-
-_repr = SafeRepr().repr

Deleted: /py/dist/py/code/testing/test_safe_repr.py
==============================================================================
--- /py/dist/py/code/testing/test_safe_repr.py	Thu Feb 16 19:56:15 2006
+++ (empty file)
@@ -1,34 +0,0 @@
-
-import py
-from py.__.code import safe_repr
-
-def test_simple_repr():
-    assert safe_repr._repr(1) == '1'
-    assert safe_repr._repr(None) == 'None'
-    
-class BrokenRepr:
-    def __init__(self, ex):
-        self.ex = ex
-        foo = 0
-    def __repr__(self):
-        raise self.ex
-        
-def test_exception():
-    assert 'Exception' in safe_repr._repr(BrokenRepr(Exception("broken")))
-
-class BrokenReprException(Exception):
-    __str__ = None 
-    __repr__ = None
-    
-def test_broken_exception():
-    assert 'Exception' in safe_repr._repr(BrokenRepr(BrokenReprException("really broken")))
-
-def test_string_exception():
-    assert 'unknown' in safe_repr._repr(BrokenRepr("string"))
-
-def test_big_repr():
-    assert len(safe_repr._repr(range(1000))) <= \
-           len('[' + safe_repr.SafeRepr().maxlist * "1000" + ']')
-
-        
-    

Modified: py/dist/py/test/terminal/terminal.py
==============================================================================
--- py/dist/py/test/terminal/terminal.py	(original)
+++ py/dist/py/test/terminal/terminal.py	Thu Feb 16 19:56:15 2006
@@ -3,7 +3,6 @@
 from time import time as now
 Item = py.test.Item
 from py.__.test.terminal.out import getout 
-import py.__.code.safe_repr
 
 def getrelpath(source, dest): 
     base = source.common(dest)
@@ -435,16 +434,12 @@
             for name, value in entry.frame.f_locals.items():
                 if name == '__builtins__': 
                     self.out.line("__builtins__ = <builtins>")
-                else:
-                    # This formatting could all be handled by the _repr() function, which is 
-                    # only repr.Repr in disguise, so is very configurable.
-                    str_repr = py.__.code.safe_repr._repr(value)
-                    if len(str_repr) < 70 or not isinstance(value,
+                elif len(repr(value)) < 70 or not isinstance(value,
                                                 (list, tuple, dict)):
-                        self.out.line("%-10s = %s" %(name, str_repr))
-                    else:
-                        self.out.line("%-10s =\\" % (name,))
-                        py.std.pprint.pprint(value, stream=self.out)
+                    self.out.line("%-10s = %r" %(name, value))
+                else:
+                    self.out.line("%-10s =\\" % (name,))
+                    py.std.pprint.pprint(value, stream=self.out)
 
 def repr_pythonversion():
     v = py.std.sys.version_info

Deleted: /py/dist/py/test/testing/data/brokenrepr.py
==============================================================================
--- /py/dist/py/test/testing/data/brokenrepr.py	Thu Feb 16 19:56:15 2006
+++ (empty file)
@@ -1,31 +0,0 @@
-import py
-
-class BrokenRepr1:
-    """A broken class with lots of broken methods. Let's try to make the test framework 
-    immune to these."""
-    foo=0
-    def __repr__(self):
-        raise Exception("Ha Ha fooled you, I'm a broken repr().")
-
-class BrokenRepr2:
-    """A broken class with lots of broken methods. Let's try to make the test framework 
-    immune to these."""
-    foo=0
-    def __repr__(self):
-        raise "Ha Ha fooled you, I'm a broken repr()."
-
-    
-class TestBrokenClass:
-
-    def test_explicit_bad_repr(self):
-        t = BrokenRepr1()
-        py.test.raises(Exception, 'repr(t)')
-        
-    def test_implicit_bad_repr1(self):
-        t = BrokenRepr1()
-        assert t.foo == 1
-
-    def test_implicit_bad_repr2(self):
-        t = BrokenRepr2()
-        assert t.foo == 1
-

Modified: py/dist/py/test/testing/test_session.py
==============================================================================
--- py/dist/py/test/testing/test_session.py	(original)
+++ py/dist/py/test/testing/test_session.py	Thu Feb 16 19:56:15 2006
@@ -293,19 +293,6 @@
         item, outcome = l[0]
         assert str(outcome.excinfo).find('does_not_work') != -1 
 
-    def test_safe_repr(self):
-        session = self.session
-        session.main([str(datadir / 'brokenrepr.py')])
-        out = self.file.getvalue()
-        print 'Output of simulated "py.test brokenrepr.py":'
-        print out
-        
-        l = session.getitemoutcomepairs(py.test.Item.Failed)
-        assert len(l) == 2
-        assert out.find("""[Exception("Ha Ha fooled you, I'm a broken repr().") raised in repr()]""") != -1 #'
-        assert out.find("[unknown exception raised in repr()]") != -1
-
-
 from py.__.test.terminal.remote import getrootdir 
 class TestRemote: 
     def test_rootdir_is_package(self): 



More information about the pytest-commit mailing list