Author: Carl Friedrich Bolz <cfb...@gmx.de>
Branch: guard-compatible
Changeset: r85323:1ac481910937
Date: 2016-06-20 11:45 +0200
http://bitbucket.org/pypy/pypy/changeset/1ac481910937/

Log:    a testing mixin for making we_are_jitted() return random results at
        runtime. this is useful to check whether the semantics in both paths
        is really the same (which it has to be).

        Apply this in some objspace tests.

diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -1,6 +1,5 @@
 """The builtin str implementation"""
 
-from rpython.rlib.jit import we_are_jitted
 from rpython.rlib.objectmodel import (
     compute_hash, compute_unique_id, import_from_mixin)
 from rpython.rlib.buffer import StringBuffer
diff --git a/pypy/objspace/std/test/test_callmethod.py 
b/pypy/objspace/std/test/test_callmethod.py
--- a/pypy/objspace/std/test/test_callmethod.py
+++ b/pypy/objspace/std/test/test_callmethod.py
@@ -1,4 +1,6 @@
-class AppTestCallMethod:
+from rpython.rlib import jit
+
+class AppTestCallMethod(jit.RandomWeAreJittedTestMixin):
     # The exec hacking is needed to have the code snippets compiled
     # by our own compiler, not CPython's
 
diff --git a/pypy/objspace/std/test/test_mapdict.py 
b/pypy/objspace/std/test/test_mapdict.py
--- a/pypy/objspace/std/test/test_mapdict.py
+++ b/pypy/objspace/std/test/test_mapdict.py
@@ -1,6 +1,9 @@
 import pytest
 from pypy.objspace.std.test.test_dictmultiobject import FakeSpace, W_DictObject
 from pypy.objspace.std.mapdict import *
+from rpython.rlib import jit
+import random
+
 
 class Config:
     class objspace:
@@ -662,7 +665,7 @@
 
 # XXX write more
 
-class AppTestWithMapDict(object):
+class AppTestWithMapDict(jit.RandomWeAreJittedTestMixin):
 
     def test_simple(self):
         class A(object):
diff --git a/pypy/objspace/std/test/test_typeobject.py 
b/pypy/objspace/std/test/test_typeobject.py
--- a/pypy/objspace/std/test/test_typeobject.py
+++ b/pypy/objspace/std/test/test_typeobject.py
@@ -2,6 +2,7 @@
 from pypy.interpreter.baseobjspace import W_Root
 from pypy.interpreter.gateway import interp2app
 from pypy.interpreter.typedef import TypeDef
+from rpython.rlib import jit
 
 
 class TestTypeObject:
@@ -59,7 +60,7 @@
         assert len(warnings) == 2
 
 
-class AppTestTypeObject:
+class AppTestTypeObject(jit.RandomWeAreJittedTestMixin):
     def test_abstract_methods(self):
         class X(object):
             pass
diff --git a/rpython/rlib/jit.py b/rpython/rlib/jit.py
--- a/rpython/rlib/jit.py
+++ b/rpython/rlib/jit.py
@@ -378,17 +378,38 @@
         hop.exception_cannot_occur()
         return hop.genop('hint', [v, c_hint], resulttype=v.concretetype)
 
+def _we_are_jitted_interpreted():
+    return False # for monkey-patching
 
 def we_are_jitted():
     """ Considered as true during tracing and blackholing,
     so its consquences are reflected into jitted code """
     # during testing we return something randomly, to emulate the real
     # behaviour where you can switch to tracing a arbitrary points.
-    return False
+    return _we_are_jitted_interpreted()
 
 _we_are_jitted = CDefinedIntSymbolic('0 /* we are not jitted here */',
                                      default=0)
 
+
+class RandomWeAreJittedTestMixin(object):
+    def setup_method(self, meth):
+        global _we_are_jitted_interpreted
+        seed = random.random()
+        print "seed", seed
+        random.seed(seed)
+        self.orig_we_are_jitted = _we_are_jitted_interpreted
+        def _we_are_jitted_interpreted_random():
+            result = random.random() > 0.5
+            return result
+        _we_are_jitted_interpreted = _we_are_jitted_interpreted_random
+
+    def teardown_method(self, meth):
+        global _we_are_jitted_interpreted
+        _we_are_jitted_interpreted = self.orig_we_are_jitted
+
+
+
 def _get_virtualizable_token(frame):
     """ An obscure API to get vable token.
     Used by _vmprof
diff --git a/rpython/rlib/test/test_jit.py b/rpython/rlib/test/test_jit.py
--- a/rpython/rlib/test/test_jit.py
+++ b/rpython/rlib/test/test_jit.py
@@ -5,7 +5,8 @@
 from rpython.rlib.jit import (hint, we_are_jitted, JitDriver, elidable_promote,
     JitHintError, oopspec, isconstant, conditional_call,
     elidable, unroll_safe, dont_look_inside,
-    enter_portal_frame, leave_portal_frame, elidable_compatible)
+    enter_portal_frame, leave_portal_frame, elidable_compatible,
+    RandomWeAreJittedTestMixin)
 from rpython.rlib.rarithmetic import r_uint
 from rpython.rtyper.test.tool import BaseRtypingTest
 from rpython.rtyper.lltypesystem import lltype
@@ -335,3 +336,19 @@
             leave_portal_frame()
         t = Translation(g, [])
         t.compile_c() # does not crash
+
+class Test_we_are_jitted(RandomWeAreJittedTestMixin):
+    def test_bad_we_are_jitted(self):
+        def bad(x):
+            if we_are_jitted():
+                return x + 1
+            else:
+                return x + 2
+
+        try:
+            for i in range(100):
+                assert bad(1) == 3
+        except AssertionError:
+            pass
+        else:
+            assert 0, "should have failed"
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to