Author: Manuel Jacob
Branch: refactor-translator
Changeset: r61790:37d1bea138cf
Date: 2013-02-25 21:27 +0100
http://bitbucket.org/pypy/pypy/changeset/37d1bea138cf/
Log: Make Translation a factory method for TranslationDriver.
diff --git a/rpython/translator/interactive.py
b/rpython/translator/interactive.py
--- a/rpython/translator/interactive.py
+++ b/rpython/translator/interactive.py
@@ -1,75 +1,13 @@
-from rpython.translator.translator import TranslationContext
-from rpython.translator import driver
+from rpython.translator.driver import TranslationDriver
-DEFAULTS = {
- 'translation.verbose': True,
-}
-
-class Translation(object):
-
- def __init__(self, entry_point, argtypes=None, **kwds):
- self.driver = driver.TranslationDriver(overrides=DEFAULTS)
- self.config = self.driver.config
-
- self.entry_point = entry_point
- self.context = TranslationContext(config=self.config)
-
- policy = kwds.pop('policy', None)
- self.update_options(kwds)
- self.ensure_setup(argtypes, policy)
- # for t.view() to work just after construction
- graph = self.context.buildflowgraph(entry_point)
- self.context._prebuilt_graphs[entry_point] = graph
-
- def view(self):
- self.context.view()
-
- def viewcg(self):
- self.context.viewcg()
-
- def ensure_setup(self, argtypes=None, policy=None):
- standalone = argtypes is None
- if standalone:
- assert argtypes is None
- else:
- if argtypes is None:
- argtypes = []
- self.driver.setup(self.entry_point, argtypes, policy,
- empty_translator=self.context)
- self.ann_argtypes = argtypes
- self.ann_policy = policy
-
- def update_options(self, kwds):
- gc = kwds.pop('gc', None)
- if gc:
- self.config.translation.gc = gc
- self.config.translation.set(**kwds)
-
- def set_backend_extra_options(self, **extra_options):
- for name in extra_options:
- backend, option = name.split('_', 1)
- assert self.config.translation.backend == backend
- self.driver.set_backend_extra_options(extra_options)
-
- # backend independent
-
- def annotate(self, **kwds):
- return self.driver.annotate()
-
- # type system dependent
-
- def rtype(self):
- return self.driver.rtype()
-
- def backendopt(self):
- return self.driver.backendopt()
-
- # backend depedent
-
- def source(self):
- return self.driver.source()
-
- def compile(self):
- self.driver.compile()
- return self.driver.c_entryp
+def Translation(entry_point, argtypes=None, policy=None, **kwds):
+ driver = TranslationDriver(overrides={'translation.verbose': True})
+ driver.driver = driver
+ driver.config.translation.set(**kwds)
+ driver.setup(entry_point, argtypes, policy)
+ driver.context = driver.translator
+ # for t.view() to work just after construction
+ graph = driver.translator.buildflowgraph(entry_point)
+ driver.translator._prebuilt_graphs[entry_point] = graph
+ return driver
diff --git a/rpython/translator/test/test_driver.py
b/rpython/translator/test/test_driver.py
--- a/rpython/translator/test/test_driver.py
+++ b/rpython/translator/test/test_driver.py
@@ -1,7 +1,5 @@
-import py
-
from rpython.translator.driver import TranslationDriver
-import optparse
+from rpython.translator.interactive import Translation
def test_c_no_jit():
@@ -22,3 +20,96 @@
td = TranslationDriver({'backendopt.none': True})
names = ['annotate', 'rtype', 'database', 'source', 'compile']
assert [task.task_name for task in td.tasks] == names
+
+
+def test_simple_annotate():
+
+ def f(x,y):
+ return x+y
+
+ t = Translation(f, [int, int])
+ assert t.context is t.driver.translator
+ assert t.config is t.driver.config is t.context.config
+
+ s = t.annotate()
+ assert s.knowntype == int
+
+ t = Translation(f, [int, int])
+ s = t.annotate()
+ assert s.knowntype == int
+
+
+def test_simple_rtype():
+
+ def f(x,y):
+ return x+y
+
+ t = Translation(f, [int, int])
+ t.annotate()
+ t.rtype()
+
+ assert 'rtype' in t.driver.done
+
+def test_simple_backendopt():
+ def f(x, y):
+ return x,y
+
+ t = Translation(f, [int, int], backend='c')
+ t.backendopt()
+
+ assert 'backendopt' in t.driver.done
+
+def test_simple_source():
+ def f(x, y):
+ return x,y
+
+ t = Translation(f, [int, int], backend='c')
+ t.annotate()
+ t.source()
+ assert 'source' in t.driver.done
+
+ t = Translation(f, [int, int])
+ t.source()
+ assert 'source' in t.driver.done
+
+def test_disable_logic():
+ return # temporary skip
+
+ def f(x,y):
+ return x+y
+
+ t = Translation(f, [int, int])
+ t.disable(['backendopt'])
+ t.source()
+
+ assert 'backendopt' not in t.driver.done
+
+def test_simple_compile_c():
+ import ctypes
+
+ def f(x,y):
+ return x+y
+
+ t = Translation(f, [int, int])
+ t.source()
+ t.compile()
+
+ dll = ctypes.CDLL(str(t.driver.c_entryp))
+ f = dll.pypy_g_f
+ assert f(2, 3) == 5
+
+def test_simple_rtype_with_type_system():
+
+ def f(x,y):
+ return x+y
+
+ t = Translation(f, [int, int])
+ t.rtype()
+
+ t = Translation(f, [int, int], type_system='ootype')
+ t.rtype()
+ assert 'rtype' in t.driver.done
+
+ t = Translation(f, [int, int], backend='cli', type_system='ootype')
+ t.rtype()
+ assert 'rtype' in t.driver.done
diff --git a/rpython/translator/test/test_interactive.py
b/rpython/translator/test/test_interactive.py
deleted file mode 100644
--- a/rpython/translator/test/test_interactive.py
+++ /dev/null
@@ -1,94 +0,0 @@
-from rpython.translator.interactive import Translation
-import py
-
-def test_simple_annotate():
-
- def f(x,y):
- return x+y
-
- t = Translation(f, [int, int])
- assert t.context is t.driver.translator
- assert t.config is t.driver.config is t.context.config
-
- s = t.annotate()
- assert s.knowntype == int
-
- t = Translation(f, [int, int])
- s = t.annotate()
- assert s.knowntype == int
-
-
-def test_simple_rtype():
-
- def f(x,y):
- return x+y
-
- t = Translation(f, [int, int])
- t.annotate()
- t.rtype()
-
- assert 'rtype' in t.driver.done
-
-def test_simple_backendopt():
- def f(x, y):
- return x,y
-
- t = Translation(f, [int, int], backend='c')
- t.backendopt()
-
- assert 'backendopt' in t.driver.done
-
-def test_simple_source():
- def f(x, y):
- return x,y
-
- t = Translation(f, [int, int], backend='c')
- t.annotate()
- t.source()
- assert 'source' in t.driver.done
-
- t = Translation(f, [int, int])
- t.source()
- assert 'source' in t.driver.done
-
-def test_disable_logic():
- return # temporary skip
-
- def f(x,y):
- return x+y
-
- t = Translation(f, [int, int])
- t.disable(['backendopt'])
- t.source()
-
- assert 'backendopt' not in t.driver.done
-
-def test_simple_compile_c():
- import ctypes
-
- def f(x,y):
- return x+y
-
- t = Translation(f, [int, int])
- t.source()
- t.compile()
-
- dll = ctypes.CDLL(str(t.driver.c_entryp))
- f = dll.pypy_g_f
- assert f(2, 3) == 5
-
-def test_simple_rtype_with_type_system():
-
- def f(x,y):
- return x+y
-
- t = Translation(f, [int, int])
- t.rtype()
-
- t = Translation(f, [int, int], type_system='ootype')
- t.rtype()
- assert 'rtype' in t.driver.done
-
- t = Translation(f, [int, int], backend='cli', type_system='ootype')
- t.rtype()
- assert 'rtype' in t.driver.done
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit