1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
from test import test_support
from jythonlib import bytecodetools as tools
from java.util.concurrent import Callable
from org.python.core import Options
import glob
import os.path
import sys
import tempfile
import unittest
class BytecodeCallbackTest(unittest.TestCase):
def setUp(self):
self.count=0
tools.clear()
def assert_callback(self, name, byte, klass):
self.count+=1
self.assertEqual(name, klass.name)
def test_register(self):
tools.register(self.assert_callback)
eval("42+1")
def test_unregister(self):
self.count=0
tools.register(self.assert_callback)
eval("42+1")
self.assertEqual(self.count, 1)
tools.unregister(self.assert_callback)
eval("42+1")
self.assertEqual(self.count, 1)
def faulty_callback(self, name, byte, klass):
raise Exception("test exception")
def faulty_callback2(self, name, byte, klass, bogus):
return
def test_faulty_callback(self):
tools.register(self.faulty_callback)
tools.register(self.assert_callback)
tools.register(self.faulty_callback2)
self.count=0
try:
# Suppress the warning otherwise produced
from org.python.core import Py
from java.util.logging import Level
level = Py.setLoggingLevel(Level.SEVERE)
eval("42+1")
finally:
self.assertTrue(tools.unregister(self.faulty_callback))
self.assertTrue(tools.unregister(self.faulty_callback2))
self.assertTrue(tools.unregister(self.assert_callback))
Py.setLoggingLevel(level)
self.assertEqual(self.count, 1)
class ProxyDebugDirectoryTest(unittest.TestCase):
"""ProxyDebugDirectory used to be the only way to save proxied classes"""
def setUp(self):
tmp = tempfile.mkdtemp()
# Ensure Unicode since derived file paths are used in Java calls
if isinstance(tmp, bytes):
tmp = tmp.decode(sys.getfilesystemencoding())
self.tmpdir = tmp
def tearDown(self):
test_support.rmtree(self.tmpdir)
def test_set_debug_directory(self):
"""Verify that proxy debug directory can be set at runtime"""
self.assertIs(Options.proxyDebugDirectory, None)
Options.proxyDebugDirectory = self.tmpdir
class C(Callable):
def call(self):
return 47
self.assertEqual(C().call(), 47)
proxy_dir = os.path.join(self.tmpdir, "org", "python", "proxies")
# If test script is run outside of regrtest, the first path is used;
# otherwise the second
proxy_classes = glob.glob(os.path.join(proxy_dir, "*.class")) + \
glob.glob(os.path.join(proxy_dir, "test", "*.class"))
self.assertEqual(len(proxy_classes), 1, "Only one proxy class is generated")
self.assertRegexpMatches(
proxy_classes[0],
r'\$C\$\d+.class$')
def test_main():
test_support.run_unittest(
BytecodeCallbackTest,
ProxyDebugDirectoryTest
)
if __name__ == '__main__':
test_main()
|