consolidate py/code files into code.py, simplify SafeRepr code and docs.

--HG--
branch : trunk
This commit is contained in:
holger krekel
2009-08-25 20:24:43 +02:00
parent 94aef0b771
commit 8ee7bef638
14 changed files with 713 additions and 758 deletions

View File

@@ -1,6 +1,7 @@
from __future__ import generators
import py
import new
import sys, new
from py.__.code.code import safe_repr
def test_newcode():
source = "i = 3"
@@ -99,3 +100,69 @@ def test_code_source():
expected = """def x():
pass"""
assert str(src) == expected
def test_frame_getsourcelineno_myself():
def func():
return sys._getframe(0)
f = func()
f = py.code.Frame(f)
source, lineno = f.code.fullsource, f.lineno
assert source[lineno].startswith(" return sys._getframe(0)")
def test_getstatement_empty_fullsource():
def func():
return sys._getframe(0)
f = func()
f = py.code.Frame(f)
prop = f.code.__class__.fullsource
try:
f.code.__class__.fullsource = None
assert f.statement == py.code.Source("")
finally:
f.code.__class__.fullsource = prop
def test_code_from_func():
co = py.code.Code(test_frame_getsourcelineno_myself)
assert co.firstlineno
assert co.path
class TestSafeRepr:
def test_simple_repr(self):
assert safe_repr(1) == '1'
assert safe_repr(None) == 'None'
def test_exceptions(self):
class BrokenRepr:
def __init__(self, ex):
self.ex = ex
foo = 0
def __repr__(self):
raise self.ex
class BrokenReprException(Exception):
__str__ = None
__repr__ = None
assert 'Exception' in safe_repr(BrokenRepr(Exception("broken")))
s = safe_repr(BrokenReprException("really broken"))
assert 'TypeError' in s
if py.std.sys.version_info < (2,6):
assert 'unknown' in safe_repr(BrokenRepr("string"))
else:
assert 'TypeError' in safe_repr(BrokenRepr("string"))
def test_big_repr(self):
from py.__.code.code import SafeRepr
assert len(safe_repr(range(1000))) <= \
len('[' + SafeRepr().maxlist * "1000" + ']')
def test_repr_on_newstyle(self):
class Function(object):
def __repr__(self):
return "<%s>" %(self.name)
try:
s = safe_repr(Function())
except Exception, e:
py.test.fail("saferepr failed for newstyle class")

View File

@@ -1,5 +1,5 @@
import py
from py.__.code.excinfo import FormattedExcinfo, ReprExceptionInfo
from py.__.code.code import FormattedExcinfo, ReprExceptionInfo
class TWMock:
def __init__(self):

View File

@@ -1,27 +1,2 @@
import sys
import py
def test_frame_getsourcelineno_myself():
def func():
return sys._getframe(0)
f = func()
f = py.code.Frame(f)
source, lineno = f.code.fullsource, f.lineno
assert source[lineno].startswith(" return sys._getframe(0)")
def test_getstatement_empty_fullsource():
def func():
return sys._getframe(0)
f = func()
f = py.code.Frame(f)
prop = f.code.__class__.fullsource
try:
f.code.__class__.fullsource = None
assert f.statement == py.code.Source("")
finally:
f.code.__class__.fullsource = prop
def test_code_from_func():
co = py.code.Code(test_frame_getsourcelineno_myself)
assert co.firstlineno
assert co.path

View File

@@ -1,48 +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():
if py.std.sys.version_info < (2,6):
assert 'unknown' in safe_repr._repr(BrokenRepr("string"))
else:
assert 'TypeError' in safe_repr._repr(BrokenRepr("string"))
def test_big_repr():
assert len(safe_repr._repr(range(1000))) <= \
len('[' + safe_repr.SafeRepr().maxlist * "1000" + ']')
def test_repr_on_newstyle():
class Function(object):
def __repr__(self):
return "<%s>" %(self.name)
try:
s = safe_repr._repr(Function())
except Exception, e:
py.test.fail("saferepr failed for newstyle class")