# HG changeset patch -- Bitbucket.org
# Project py
# URL http://bitbucket.org/hpk42/py/overview
# User holger krekel <hol...@merlinux.eu>
# Date 1290692265 -3600
# Node ID 8d9ddabb8c3f60367341ce56344d60b9685a9013
# Parent  ceb0875f32dcd662c4ca8991b7bb6a4363bcb4ee
fix assertion source finding raising some more

--- a/setup.py
+++ b/setup.py
@@ -9,7 +9,7 @@ def main():
         name='py',
         description='library with cross-python path, ini-parsing, io, code, 
log facilities',
         long_description = open('README.txt').read(),
-        version='1.4.0a3',
+        version='1.4.0a5',
         url='http://pylib.org',
         license='MIT license',
         platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

--- a/testing/code/test_assertion.py
+++ b/testing/code/test_assertion.py
@@ -273,3 +273,32 @@ def test_assert_long_source_2():
         s = str(e)
         assert 're-run' not in s
         assert 'somet text' in s
+
+def test_assert_raise_alias(testdir):
+    testdir.makepyfile("""
+    import sys
+    EX = AssertionError
+    def test_hello():
+        raise EX("hello"
+            "multi"
+            "line")
+    """)
+    result = testdir.runpytest()
+    result.stdout.fnmatch_lines([
+        "*def test_hello*",
+        "*raise EX*",
+        "*1 failed*",
+    ])
+
+
+def test_assert_raise_subclass():
+    class SomeEx(AssertionError):
+        def __init__(self, *args):
+            super(SomeEx, self).__init__()
+    try:
+        raise SomeEx("hello")
+    except AssertionError:
+        s = str(exvalue())
+        assert 're-run' not in s
+        assert 'could not determine' in s
+

--- a/py/_code/assertion.py
+++ b/py/_code/assertion.py
@@ -65,18 +65,22 @@ class AssertionError(BuiltinAssertionErr
             try:
                 source = f.code.fullsource
                 if source is not None:
-                    source = source.getstatement(f.lineno, assertion=True)
-                    source = str(source.deindent()).strip()
+                    try:
+                        source = source.getstatement(f.lineno, assertion=True)
+                    except IndexError:
+                        source = None
+                    else:
+                        source = str(source.deindent()).strip()
             except py.error.ENOENT:
                 source = None
                 # this can also occur during reinterpretation, when the
                 # co_filename is set to "<run>".
             if source:
                 self.msg = reinterpret(source, f, should_fail=True)
-                if not self.args:
-                    self.args = (self.msg,)
             else:
-                self.msg = None
+                self.msg = "<could not determine information>"
+            if not self.args:
+                self.args = (self.msg,)
 
 if sys.version_info > (3, 0):
     AssertionError.__module__ = "builtins"

--- a/py/_code/source.py
+++ b/py/_code/source.py
@@ -118,9 +118,12 @@ class Source(object):
         from codeop import compile_command
         for start in range(lineno, -1, -1):
             if assertion:
-                if "assert" not in self.lines[start]:
+                line = self.lines[start]
+                # the following lines are not fully tested, change with care
+                if 'super' in line and 'self' in line and '__init__' in line:
+                    raise IndexError("likely a subclass")
+                if "assert" not in line and "raise" not in line:
                     continue
-                
             trylines = self.lines[start:lineno+1]
             # quick hack to indent the source and get it as a string in one go
             trylines.insert(0, 'def xxx():')

--- a/py/__init__.py
+++ b/py/__init__.py
@@ -8,7 +8,7 @@ dictionary or an import path.
 
 (c) Holger Krekel and others, 2004-2010
 """
-__version__ = '1.4.0a3'
+__version__ = '1.4.0a5'
 
 from py import _apipkg
_______________________________________________
py-svn mailing list
py-svn@codespeak.net
http://codespeak.net/mailman/listinfo/py-svn

Reply via email to