[pypy-commit] pypy default: introduce @not_rpython decorator instead of the weird "let's put something into

cfbolz pypy.commits at gmail.com
Mon Nov 7 13:26:44 EST 2016


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: 
Changeset: r88193:24605a73710b
Date: 2016-11-07 18:17 +0100
http://bitbucket.org/pypy/pypy/changeset/24605a73710b/

Log:	introduce @not_rpython decorator instead of the weird "let's put
	something into the docstring" approach

diff --git a/rpython/annotator/test/test_annrpython.py b/rpython/annotator/test/test_annrpython.py
--- a/rpython/annotator/test/test_annrpython.py
+++ b/rpython/annotator/test/test_annrpython.py
@@ -146,6 +146,27 @@
         # result should be an integer
         assert s.knowntype == int
 
+    def test_not_rpython(self):
+        def g(x):
+            """ NOT_RPYTHON """
+            return eval(x)
+        def f(x):
+            return g(str(x))
+        a = self.RPythonAnnotator()
+        with py.test.raises(ValueError):
+            a.build_types(f, [int])
+
+    def test_not_rpython_decorator(self):
+        from rpython.rlib.objectmodel import not_rpython
+        @not_rpython
+        def g(x):
+            return eval(x)
+        def f(x):
+            return g(str(x))
+        a = self.RPythonAnnotator()
+        with py.test.raises(ValueError):
+            a.build_types(f, [int])
+
     def test_lists(self):
         a = self.RPythonAnnotator()
         end_cell = a.build_types(snippet.poor_man_rev_range, [int])
diff --git a/rpython/doc/faq.rst b/rpython/doc/faq.rst
--- a/rpython/doc/faq.rst
+++ b/rpython/doc/faq.rst
@@ -80,6 +80,9 @@
 stops and reports this as an error. You can therefore mark functions as
 "NOT_RPYTHON" to make sure that they are never analyzed.
 
+This method of marking a function as not RPython is outdated. For new code,
+please use the ``@objectmodel.not_rpython`` decorator instead.
+
 
 Couldn't we simply take a Python syntax tree and turn it into Lisp?
 -------------------------------------------------------------------
@@ -143,7 +146,7 @@
   attempt to point newcomers at existing alternatives, which are more
   mainstream and where they will get help from many people.*
 
-  *If anybody seriously wants to promote RPython anyway, he is welcome
+  *If anybody seriously wants to promote RPython anyway, they are welcome
   to: we won't actively resist such a plan.  There are a lot of things
   that could be done to make RPython a better Java-ish language for
   example, starting with supporting non-GIL-based multithreading, but we
@@ -182,8 +185,8 @@
 patch the generated machine code.
 
 So the position of the core PyPy developers is that if anyone wants to
-make an N+1'th attempt with LLVM, he is welcome, and he will receive a
-bit of help on the IRC channel, but he is left with the burden of proof
+make an N+1'th attempt with LLVM, they are welcome, and they will receive a
+bit of help on the IRC channel, but they are left with the burden of proof
 that it works.
 
 
diff --git a/rpython/flowspace/objspace.py b/rpython/flowspace/objspace.py
--- a/rpython/flowspace/objspace.py
+++ b/rpython/flowspace/objspace.py
@@ -18,6 +18,8 @@
     except AttributeError:
         raise ValueError("%r is not RPython: it is likely an unexpected "
                          "built-in function or type" % (func,))
+    if getattr(func, "_not_rpython_", False):
+        raise ValueError("%r is tagged as @not_rpython" % (func,))
     if func.func_doc and func.func_doc.lstrip().startswith('NOT_RPYTHON'):
         raise ValueError("%r is tagged as NOT_RPYTHON" % (func,))
     if func.func_code.co_cellvars:
diff --git a/rpython/rlib/objectmodel.py b/rpython/rlib/objectmodel.py
--- a/rpython/rlib/objectmodel.py
+++ b/rpython/rlib/objectmodel.py
@@ -223,6 +223,13 @@
     func._always_inline_ = 'try'
     return func
 
+def not_rpython(func):
+    """ mark a function as not rpython. the translation process will raise an
+    error if it encounters the function. """
+    # test is in annotator/test/test_annrpython.py
+    func._not_rpython_ = True
+    return func
+
 
 # ____________________________________________________________
 
@@ -545,8 +552,9 @@
     return intmask(x)
 TAKE_NEXT = float(2**31)
 
+ at not_rpython
 def _hash_tuple(t):
-    """NOT_RPYTHON.  The algorithm behind compute_hash() for a tuple.
+    """The algorithm behind compute_hash() for a tuple.
     It is modelled after the old algorithm of Python 2.3, which is
     a bit faster than the one introduced by Python 2.4.  We assume
     that nested tuples are very uncommon in RPython, making the bad


More information about the pypy-commit mailing list