[pypy-commit] pypy default: Detect code that annotates as "raise None" and complain

arigo pypy.commits at gmail.com
Tue Jan 14 09:26:54 EST 2020


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r98532:f35d1ea7de4f
Date: 2020-01-14 15:26 +0100
http://bitbucket.org/pypy/pypy/changeset/f35d1ea7de4f/

Log:	Detect code that annotates as "raise None" and complain

diff --git a/rpython/rtyper/debug.py b/rpython/rtyper/debug.py
--- a/rpython/rtyper/debug.py
+++ b/rpython/rtyper/debug.py
@@ -32,6 +32,12 @@
         return s_x.nonnoneify()
 
     def specialize_call(self, hop):
+        from rpython.annotator import model as annmodel
+        from rpython.rtyper.error import TyperError
+        if annmodel.s_None.contains(hop.args_s[0]):
+            raise TyperError("ll_assert_not_none(None) detected.  This might "
+                             "come from something that annotates as "
+                             "'raise None'.")
         [v0] = hop.inputargs(hop.args_r[0])
         hop.exception_cannot_occur()
         hop.genop('debug_assert_not_none', [v0])
diff --git a/rpython/rtyper/test/test_exception.py b/rpython/rtyper/test/test_exception.py
--- a/rpython/rtyper/test/test_exception.py
+++ b/rpython/rtyper/test/test_exception.py
@@ -3,7 +3,7 @@
 from rpython.translator.translator import TranslationContext
 from rpython.rtyper.test.tool import BaseRtypingTest
 from rpython.rtyper.llinterp import LLException
-from rpython.rtyper.error import MissingRTypeOperation
+from rpython.rtyper.error import MissingRTypeOperation, TyperError
 
 
 class MyException(Exception):
@@ -164,3 +164,10 @@
             except OverflowError:
                 return 42
         py.test.raises(MissingRTypeOperation, self.interpret, f, [])
+
+    def test_cannot_raise_something_annotated_as_none(self):
+        def g():
+            return None
+        def f():
+            raise g()
+        py.test.raises(TyperError, rtype, f)


More information about the pypy-commit mailing list