[pypy-commit] pypy py3.6: Modernise raises() syntax

rlamy pypy.commits at gmail.com
Tue Dec 3 16:47:54 EST 2019


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: py3.6
Changeset: r98230:df95b7b0b5c6
Date: 2019-12-03 21:47 +0000
http://bitbucket.org/pypy/pypy/changeset/df95b7b0b5c6/

Log:	Modernise raises() syntax

diff --git a/pypy/interpreter/test/test_generator.py b/pypy/interpreter/test/test_generator.py
--- a/pypy/interpreter/test/test_generator.py
+++ b/pypy/interpreter/test/test_generator.py
@@ -10,7 +10,8 @@
             yield 1
         g = f()
         assert next(g) == 1
-        raises(StopIteration, next, g)
+        with raises(StopIteration):
+            next(g)
 
     def test_attributes(self):
         def f():
@@ -23,7 +24,8 @@
         assert not g.gi_running
         next(g)
         assert not g.gi_running
-        raises(StopIteration, next, g)
+        with raises(StopIteration):
+            next(g)
         assert not g.gi_running
         assert g.gi_frame is None
         assert g.gi_code is f.__code__
@@ -79,7 +81,8 @@
         g = f()
         assert next(g) == 1
         assert g.throw(NameError("Error")) == 3
-        raises(StopIteration, next, g)
+        with raises(StopIteration):
+            next(g)
 
     def test_throw4(self):
         d = {}
@@ -96,7 +99,8 @@
         assert next(g) == 1
         assert next(g) == 2
         assert g.throw(NameError("Error")) == 3
-        raises(StopIteration, next, g)
+        with raises(StopIteration):
+            next(g)
 
     def test_throw5(self):
         def f():
@@ -171,8 +175,10 @@
         g = f()
         res = next(g)
         assert res == 1
-        raises(StopIteration, next, g)
-        raises(NameError, g.throw, NameError)
+        with raises(StopIteration):
+            next(g)
+        with raises(NameError):
+            g.throw(NameError)
 
     def test_throw_tb(self):
         def f():
@@ -294,7 +300,8 @@
             i = next(me)
             yield i
         me = g()
-        raises(ValueError, next, me)
+        with raises(ValueError):
+            next(me)
 
     def test_generator_expression(self):
         d = {}
@@ -427,10 +434,14 @@
         g.__qualname__ = "j.k"
         assert g.__name__ == "h.i"
         assert g.__qualname__ == "j.k"
-        raises(TypeError, "g.__name__ = 42")
-        raises(TypeError, "g.__qualname__ = 42")
-        raises((TypeError, AttributeError), "del g.__name__")
-        raises((TypeError, AttributeError), "del g.__qualname__")
+        with raises(TypeError):
+            g.__name__ = 42
+        with raises(TypeError):
+            g.__qualname__ = 42
+        with raises((TypeError, AttributeError)):
+            del g.__name__
+        with raises((TypeError, AttributeError)):
+            del g.__qualname__
 
     def test_gi_yieldfrom(self): """
         def g(x):
@@ -466,7 +477,8 @@
         gen = g()
         assert next(gen) == 42
         closed = []
-        raises(GeneratorExit, gen.throw, GeneratorExit)
+        with raises(GeneratorExit):
+            gen.throw(GeneratorExit)
         assert closed == [True]
         """
 
@@ -487,7 +499,8 @@
             assert sys.exc_info()[0] is IndexError
             assert next(gen) is ValueError
             assert sys.exc_info()[0] is IndexError
-            raises(StopIteration, next, gen)
+            with raises(StopIteration):
+                next(gen)
             assert sys.exc_info()[0] is IndexError
 
     def test_exc_info_in_generator_2(self):
@@ -638,9 +651,10 @@
         for i in range(2):
             x = next(g)
             trace.append("Yielded %s" % (x,))
-        exc = raises(ValueError, g.close)
-        assert exc.value.args[0] == "nybbles have exploded with delight"
-        assert isinstance(exc.value.__context__, GeneratorExit)
+        with raises(ValueError) as excinfo:
+            g.close()
+        assert excinfo.value.args[0] == "nybbles have exploded with delight"
+        assert isinstance(excinfo.value.__context__, GeneratorExit)
         assert trace == [
             "Starting g1",
             "Yielded g1 ham",
@@ -679,8 +693,9 @@
             x = next(g)
             trace.append("Yielded %s" % (x,))
         e = ValueError("tomato ejected")
-        exc = raises(ValueError, g.throw, e)
-        assert exc.value.args[0] == "tomato ejected"
+        with raises(ValueError) as excinfo:
+            g.throw(e)
+        assert excinfo.value.args[0] == "tomato ejected"
         assert trace == [
             "Starting g1",
             "Yielded g1 ham",
@@ -709,8 +724,9 @@
         for i in range(5):
             x = next(gi)
             trace.append("Yielded %s" % (x,))
-        exc = raises(ValueError, gi.throw, ValueError("tomato ejected"))
-        assert exc.value.args[0] == "tomato ejected"
+        with raises(ValueError) as excinfo:
+            gi.throw(ValueError("tomato ejected"))
+        assert excinfo.value.args[0] == "tomato ejected"
         assert trace == [
             "Starting g",
             "Yielded 0",
@@ -742,16 +758,18 @@
 
         gi = g()
         assert next(gi) == 1
-        raises(ZeroDivisionError, gi.send, 1)
+        with raises(ZeroDivisionError):
+            gi.send(1)
 
         gi = g()
         assert next(gi) == 1
-        raises(ZeroDivisionError, gi.throw, RuntimeError)
+        with raises(ZeroDivisionError):
+            gi.throw(RuntimeError)
 
         gi = g()
         assert next(gi) == 1
-        import io, sys
-        sys.stderr = io.StringIO()
+        import _io, sys
+        sys.stderr = _io.StringIO()
         gi.close()
         assert 'ZeroDivisionError' in sys.stderr.getvalue()
 
@@ -859,7 +877,8 @@
         def f(x):
             raise StopIteration
             yield x
-        raises(StopIteration, next, f(5))
+        with raises(StopIteration):
+            next(f(5))
 
     def test_future_generator_stop(self):
         d = {}
@@ -870,7 +889,8 @@
     yield x
 """, d)
         f = d['f']
-        raises(RuntimeError, next, f(5))
+        with raises(RuntimeError):
+            next(f(5))
 
     def test_generator_stop_cause(self):
         d = {}
@@ -882,7 +902,8 @@
         my_gen = d['gen1']()
         assert next(my_gen) == 42
         stop_exc = StopIteration('spam')
-        e = raises(RuntimeError, my_gen.throw, StopIteration, stop_exc, None)
+        with raises(RuntimeError) as e:
+            my_gen.throw(StopIteration, stop_exc, None)
         assert e.value.__cause__ is stop_exc
         assert e.value.__context__ is stop_exc
 
@@ -892,8 +913,9 @@
 
         gen = d['gen1']()
         assert next(gen) == 1
-        exc = raises(StopIteration, gen.send, (2,))
-        assert exc.value.value == (2,)
+        with raises(StopIteration) as excinfo:
+            gen.send((2,))
+        assert excinfo.value.value == (2,)
 
     def test_return_stopiteration(self):
         d = {}
@@ -901,6 +923,7 @@
 
         gen = d['gen1']()
         assert next(gen) == 1
-        exc = raises(StopIteration, gen.send, StopIteration(2))
-        assert isinstance(exc.value, StopIteration)
-        assert exc.value.value.value == 2
+        with raises(StopIteration) as excinfo:
+            gen.send(StopIteration(2))
+        assert isinstance(excinfo.value, StopIteration)
+        assert excinfo.value.value.value == 2


More information about the pypy-commit mailing list