Skip to content

Commit c85d8f1

Browse files
committed
[GR-12657] BaseException should allow set __traceback__.
1 parent 2bfb146 commit c85d8f1

File tree

5 files changed

+107
-9
lines changed

5 files changed

+107
-9
lines changed

graalpython/com.oracle.graal.python.test/src/graalpytest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ def assertSequenceEqual(self, expected, actual, msg=None):
158158
for expected_value in expected:
159159
assert expected_value == next(actual_iter), msg
160160

161+
def assertIsInstance(self, obj, cls, msg=None):
162+
"""Same as self.assertTrue(isinstance(obj, cls)), with a nicer
163+
default message."""
164+
if not isinstance(obj, cls):
165+
message = msg
166+
if msg is None:
167+
message = '%s is not an instance of %r' % (obj, cls)
168+
assert False, message
169+
161170
class assertRaises():
162171
def __init__(self, exc_type, function=None, *args, **kwargs):
163172
self.function = function
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates.
2+
# Copyright (c) 2013, Regents of the University of California
3+
#
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are
7+
# permitted provided that the following conditions are met:
8+
#
9+
# 1. Redistributions of source code must retain the above copyright notice, this list of
10+
# conditions and the following disclaimer.
11+
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
16+
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17+
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18+
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20+
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21+
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23+
# OF THE POSSIBILITY OF SUCH DAMAGE.
24+
25+
import unittest
26+
import sys
27+
28+
class ExceptionTests(unittest.TestCase):
29+
30+
def testInvalidTraceback(self):
31+
try:
32+
Exception().__traceback__ = 5
33+
except TypeError as e:
34+
self.assertIn("__traceback__ must be a traceback", str(e))
35+
else:
36+
self.fail("No exception raised")
37+
38+
def testNoneClearsTracebackAttr(self):
39+
try:
40+
raise IndexError(4)
41+
except:
42+
tb = sys.exc_info()[2]
43+
44+
e = Exception()
45+
e.__traceback__ = tb
46+
e.__traceback__ = None
47+
self.assertEqual(e.__traceback__, None)
48+
49+
def testWithTraceback(self):
50+
try:
51+
raise IndexError(4)
52+
except:
53+
tb = sys.exc_info()[2]
54+
55+
e = BaseException().with_traceback(tb)
56+
self.assertIsInstance(e, BaseException)
57+
# TODO this dosn't work yet
58+
#self.assertEqual(e.__traceback__, tb)
59+
60+
e = IndexError(5).with_traceback(tb)
61+
self.assertIsInstance(e, IndexError)
62+
# TODO this dosn't work yet
63+
#self.assertEqual(e.__traceback__, tb)
64+
65+
class MyException(Exception):
66+
pass
67+
68+
e = MyException().with_traceback(tb)
69+
self.assertIsInstance(e, MyException)
70+
# TODO this dosn't work yet
71+
#self.assertEqual(e.__traceback__, tb)

graalpython/com.oracle.graal.python.test/src/tests/test_zipimport.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2222
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
2323
# OF THE POSSIBILITY OF SUCH DAMAGE.
24-
# Qunaibit 02/05/2014
25-
# With Statement
2624

2725
import sys
2826
import os

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/BaseExceptionBuiltins.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@
4949
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5050
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
5151
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
52+
import com.oracle.graal.python.runtime.exception.PythonErrorType;
5253
import com.oracle.graal.python.runtime.formatting.ErrorMessageFormatter;
5354
import com.oracle.truffle.api.CompilerDirectives;
5455
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5556
import com.oracle.truffle.api.dsl.Cached;
57+
import com.oracle.truffle.api.dsl.Fallback;
5658
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
5759
import com.oracle.truffle.api.dsl.NodeFactory;
5860
import com.oracle.truffle.api.dsl.Specialization;
@@ -178,30 +180,47 @@ public boolean suppressContext(@SuppressWarnings("unused") PBaseException self)
178180
}
179181
}
180182

181-
@Builtin(name = __TRACEBACK__, fixedNumOfPositionalArgs = 1, isGetter = true)
183+
@Builtin(name = __TRACEBACK__, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true)
182184
@GenerateNodeFactory
183185
public abstract static class TracebackNode extends PythonBuiltinNode {
184186

185-
@Specialization
186-
public Object traceback(PBaseException self) {
187+
@Specialization(guards = "isNoValue(tb)")
188+
public Object getTraceback(PBaseException self, Object tb) {
187189
return self.getTraceback(factory());
188190
}
191+
192+
@Specialization
193+
public Object setTraceback(PBaseException self, PNone tb) {
194+
self.clearTraceback();
195+
return PNone.NONE;
196+
}
197+
198+
@Specialization
199+
public Object setTraceback(PBaseException self, PTraceback tb) {
200+
self.setTraceback(tb);
201+
return PNone.NONE;
202+
}
203+
204+
@Fallback
205+
public Object setTraceback(Object self, Object tb) {
206+
throw raise(PythonErrorType.TypeError, "__traceback__ must be a traceback or None");
207+
}
189208
}
190209

191210
@Builtin(name = "with_traceback", fixedNumOfPositionalArgs = 2)
192211
@GenerateNodeFactory
193212
public abstract static class WithTracebackNode extends PythonBuiltinNode {
194213

195214
@Specialization
196-
public Object withTraceback(PBaseException self, @SuppressWarnings("unused") PNone tb) {
215+
public PBaseException withTraceback(PBaseException self, @SuppressWarnings("unused") PNone tb) {
197216
self.clearTraceback();
198-
return PNone.NONE;
217+
return self;
199218
}
200219

201220
@Specialization
202-
public Object withTraceback(PBaseException self, PTraceback tb) {
221+
public PBaseException withTraceback(PBaseException self, PTraceback tb) {
203222
self.setTraceback(tb);
204-
return PNone.NONE;
223+
return self;
205224
}
206225
}
207226
}

mx.graalpython/copyrights/overrides

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ graalpython/com.oracle.graal.python.test/src/tests/test_compare.py,python.copyri
199199
graalpython/com.oracle.graal.python.test/src/tests/test_enumerate.py,python.copyright
200200
graalpython/com.oracle.graal.python.test/src/tests/test_euler11.py,benchmarks.copyright
201201
graalpython/com.oracle.graal.python.test/src/tests/test_euler31.py,benchmarks.copyright
202+
graalpython/com.oracle.graal.python.test/src/tests/test_exception.py,zippy.copyright
202203
graalpython/com.oracle.graal.python.test/src/tests/test_generator-accumulator.py,zippy.copyright
203204
graalpython/com.oracle.graal.python.test/src/tests/test_generator-continue.py,zippy.copyright
204205
graalpython/com.oracle.graal.python.test/src/tests/test_generator-expression-next.py,zippy.copyright

0 commit comments

Comments
 (0)