[pypy-commit] pypy cffi-static-callback-embedding: Forgot to "hg add" these new tests
arigo
pypy.commits at gmail.com
Fri Jan 15 10:01:14 EST 2016
Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-static-callback-embedding
Changeset: r81802:0129e05ba29e
Date: 2016-01-15 15:14 +0100
http://bitbucket.org/pypy/pypy/changeset/0129e05ba29e/
Log: Forgot to "hg add" these new tests
diff --git a/pypy/module/test_lib_pypy/cffi_tests/embedding/__init__.py b/pypy/module/test_lib_pypy/cffi_tests/embedding/__init__.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/cffi_tests/embedding/__init__.py
@@ -0,0 +1,1 @@
+# Generated by pypy/tool/import_cffi.py
diff --git a/pypy/module/test_lib_pypy/cffi_tests/embedding/add1.py b/pypy/module/test_lib_pypy/cffi_tests/embedding/add1.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/cffi_tests/embedding/add1.py
@@ -0,0 +1,34 @@
+# Generated by pypy/tool/import_cffi.py
+import cffi
+
+ffi = cffi.FFI()
+
+ffi.embedding_api("""
+ int add1(int, int);
+""")
+
+ffi.embedding_init_code(r"""
+ import sys, time
+ sys.stdout.write("preparing")
+ for i in range(3):
+ sys.stdout.flush()
+ time.sleep(0.02)
+ sys.stdout.write(".")
+ sys.stdout.write("\n")
+
+ from _add1_cffi import ffi
+
+ int(ord("A")) # check that built-ins are there
+
+ @ffi.def_extern()
+ def add1(x, y):
+ sys.stdout.write("adding %d and %d\n" % (x, y))
+ sys.stdout.flush()
+ return x + y
+""")
+
+ffi.set_source("_add1_cffi", """
+""")
+
+fn = ffi.compile(verbose=True)
+print('FILENAME: %s' % (fn,))
diff --git a/pypy/module/test_lib_pypy/cffi_tests/embedding/add2.py b/pypy/module/test_lib_pypy/cffi_tests/embedding/add2.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/cffi_tests/embedding/add2.py
@@ -0,0 +1,30 @@
+# Generated by pypy/tool/import_cffi.py
+import cffi
+
+ffi = cffi.FFI()
+
+ffi.embedding_api("""
+ int add2(int, int, int);
+""")
+
+ffi.embedding_init_code(r"""
+ import sys
+ sys.stdout.write("prepADD2\n")
+
+ assert '_add2_cffi' in sys.modules
+ m = sys.modules['_add2_cffi']
+ import _add2_cffi
+ ffi = _add2_cffi.ffi
+
+ @ffi.def_extern()
+ def add2(x, y, z):
+ sys.stdout.write("adding %d and %d and %d\n" % (x, y, z))
+ sys.stdout.flush()
+ return x + y + z
+""")
+
+ffi.set_source("_add2_cffi", """
+""")
+
+fn = ffi.compile(verbose=True)
+print('FILENAME: %s' % (fn,))
diff --git a/pypy/module/test_lib_pypy/cffi_tests/embedding/add3.py b/pypy/module/test_lib_pypy/cffi_tests/embedding/add3.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/cffi_tests/embedding/add3.py
@@ -0,0 +1,25 @@
+# Generated by pypy/tool/import_cffi.py
+import cffi
+
+ffi = cffi.FFI()
+
+ffi.embedding_api("""
+ int add3(int, int, int, int);
+""")
+
+ffi.embedding_init_code(r"""
+ from _add3_cffi import ffi
+ import sys
+
+ @ffi.def_extern()
+ def add3(x, y, z, t):
+ sys.stdout.write("adding %d, %d, %d, %d\n" % (x, y, z, t))
+ sys.stdout.flush()
+ return x + y + z + t
+""")
+
+ffi.set_source("_add3_cffi", """
+""")
+
+fn = ffi.compile(verbose=True)
+print('FILENAME: %s' % (fn,))
diff --git a/pypy/module/test_lib_pypy/cffi_tests/embedding/add_recursive.py b/pypy/module/test_lib_pypy/cffi_tests/embedding/add_recursive.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/cffi_tests/embedding/add_recursive.py
@@ -0,0 +1,34 @@
+# Generated by pypy/tool/import_cffi.py
+import cffi
+
+ffi = cffi.FFI()
+
+ffi.embedding_api("""
+ int (*my_callback)(int);
+ int add_rec(int, int);
+""")
+
+ffi.embedding_init_code(r"""
+ from _add_recursive_cffi import ffi, lib
+ import sys
+ print("preparing REC")
+ sys.stdout.flush()
+
+ @ffi.def_extern()
+ def add_rec(x, y):
+ print("adding %d and %d" % (x, y))
+ sys.stdout.flush()
+ return x + y
+
+ x = lib.my_callback(400)
+ print('<<< %d >>>' % (x,))
+""")
+
+ffi.set_source("_add_recursive_cffi", """
+/* use CFFI_DLLEXPORT: on windows, it expands to __declspec(dllexport),
+ which is needed to export a variable from a dll */
+CFFI_DLLEXPORT int (*my_callback)(int);
+""")
+
+fn = ffi.compile(verbose=True)
+print('FILENAME: %s' % (fn,))
diff --git a/pypy/module/test_lib_pypy/cffi_tests/embedding/perf.py b/pypy/module/test_lib_pypy/cffi_tests/embedding/perf.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/cffi_tests/embedding/perf.py
@@ -0,0 +1,22 @@
+# Generated by pypy/tool/import_cffi.py
+import cffi
+
+ffi = cffi.FFI()
+
+ffi.embedding_api("""
+ int add1(int, int);
+""")
+
+ffi.embedding_init_code(r"""
+ from _perf_cffi import ffi
+
+ @ffi.def_extern()
+ def add1(x, y):
+ return x + y
+""")
+
+ffi.set_source("_perf_cffi", """
+""")
+
+fn = ffi.compile(verbose=True)
+print('FILENAME: %s' % (fn,))
diff --git a/pypy/module/test_lib_pypy/cffi_tests/embedding/test_basic.py b/pypy/module/test_lib_pypy/cffi_tests/embedding/test_basic.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/cffi_tests/embedding/test_basic.py
@@ -0,0 +1,151 @@
+# Generated by pypy/tool/import_cffi.py
+import py
+import sys, os, re
+import shutil, subprocess, time
+from pypy.module.test_lib_pypy.cffi_tests.udir import udir
+import cffi
+
+if hasattr(sys, 'gettotalrefcount'):
+ py.test.skip("tried hard and failed to have these tests run "
+ "in a debug-mode python")
+
+
+local_dir = os.path.dirname(os.path.abspath(__file__))
+_link_error = '?'
+
+def check_lib_python_found(tmpdir):
+ global _link_error
+ if _link_error == '?':
+ ffi = cffi.FFI()
+ kwds = {}
+ ffi._apply_embedding_fix(kwds)
+ ffi.set_source("_test_lib_python_found", "", **kwds)
+ try:
+ ffi.compile(tmpdir=tmpdir)
+ except cffi.VerificationError as e:
+ _link_error = e
+ else:
+ _link_error = None
+ if _link_error:
+ py.test.skip(str(_link_error))
+
+
+class EmbeddingTests:
+ _compiled_modules = {}
+
+ def setup_method(self, meth):
+ check_lib_python_found(str(udir.ensure('embedding', dir=1)))
+ self._path = udir.join('embedding', meth.__name__)
+ if sys.platform == "win32":
+ self._compiled_modules.clear() # workaround
+
+ def get_path(self):
+ return str(self._path.ensure(dir=1))
+
+ def _run(self, args, env=None):
+ print(args)
+ popen = subprocess.Popen(args, env=env, cwd=self.get_path(),
+ stdout=subprocess.PIPE,
+ universal_newlines=True)
+ output = popen.stdout.read()
+ err = popen.wait()
+ if err:
+ raise OSError("popen failed with exit code %r: %r" % (
+ err, args))
+ print(output.rstrip())
+ return output
+
+ def prepare_module(self, name):
+ if name not in self._compiled_modules:
+ path = self.get_path()
+ filename = '%s.py' % name
+ # NOTE: if you have an .egg globally installed with an older
+ # version of cffi, this will not work, because sys.path ends
+ # up with the .egg before the PYTHONPATH entries. I didn't
+ # find a solution to that: we could hack sys.path inside the
+ # script run here, but we can't hack it in the same way in
+ # execute().
+ env = os.environ.copy()
+ env['PYTHONPATH'] = os.path.dirname(os.path.dirname(local_dir))
+ output = self._run([sys.executable, os.path.join(local_dir, filename)],
+ env=env)
+ match = re.compile(r"\bFILENAME: (.+)").search(output)
+ assert match
+ dynamic_lib_name = match.group(1)
+ if sys.platform == 'win32':
+ assert dynamic_lib_name.endswith('_cffi.dll')
+ else:
+ assert dynamic_lib_name.endswith('_cffi.so')
+ self._compiled_modules[name] = dynamic_lib_name
+ return self._compiled_modules[name]
+
+ def compile(self, name, modules, opt=False, threads=False, defines={}):
+ path = self.get_path()
+ filename = '%s.c' % name
+ shutil.copy(os.path.join(local_dir, filename), path)
+ shutil.copy(os.path.join(local_dir, 'thread-test.h'), path)
+ import distutils.ccompiler
+ curdir = os.getcwd()
+ try:
+ os.chdir(self.get_path())
+ c = distutils.ccompiler.new_compiler()
+ print('compiling %s with %r' % (name, modules))
+ extra_preargs = []
+ if sys.platform == 'win32':
+ libfiles = []
+ for m in modules:
+ m = os.path.basename(m)
+ assert m.endswith('.dll')
+ libfiles.append('Release\\%s.lib' % m[:-4])
+ modules = libfiles
+ elif threads:
+ extra_preargs.append('-pthread')
+ objects = c.compile([filename], macros=sorted(defines.items()), debug=True)
+ c.link_executable(objects + modules, name, extra_preargs=extra_preargs)
+ finally:
+ os.chdir(curdir)
+
+ def execute(self, name):
+ path = self.get_path()
+ env = os.environ.copy()
+ env['PYTHONPATH'] = os.path.dirname(os.path.dirname(local_dir))
+ libpath = env.get('LD_LIBRARY_PATH')
+ if libpath:
+ libpath = path + ':' + libpath
+ else:
+ libpath = path
+ env['LD_LIBRARY_PATH'] = libpath
+ print('running %r in %r' % (name, path))
+ executable_name = name
+ if sys.platform == 'win32':
+ executable_name = os.path.join(path, executable_name + '.exe')
+ popen = subprocess.Popen([executable_name], cwd=path, env=env,
+ stdout=subprocess.PIPE,
+ universal_newlines=True)
+ result = popen.stdout.read()
+ err = popen.wait()
+ if err:
+ raise OSError("%r failed with exit code %r" % (name, err))
+ return result
+
+
+class TestBasic(EmbeddingTests):
+ def test_basic(self):
+ add1_cffi = self.prepare_module('add1')
+ self.compile('add1-test', [add1_cffi])
+ output = self.execute('add1-test')
+ assert output == ("preparing...\n"
+ "adding 40 and 2\n"
+ "adding 100 and -5\n"
+ "got: 42 95\n")
+
+ def test_two_modules(self):
+ add1_cffi = self.prepare_module('add1')
+ add2_cffi = self.prepare_module('add2')
+ self.compile('add2-test', [add1_cffi, add2_cffi])
+ output = self.execute('add2-test')
+ assert output == ("preparing...\n"
+ "adding 40 and 2\n"
+ "prepADD2\n"
+ "adding 100 and -5 and -20\n"
+ "got: 42 75\n")
diff --git a/pypy/module/test_lib_pypy/cffi_tests/embedding/test_performance.py b/pypy/module/test_lib_pypy/cffi_tests/embedding/test_performance.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/cffi_tests/embedding/test_performance.py
@@ -0,0 +1,53 @@
+# Generated by pypy/tool/import_cffi.py
+import sys
+from pypy.module.test_lib_pypy.cffi_tests.embedding.test_basic import EmbeddingTests
+
+if sys.platform == 'win32':
+ import py
+ py.test.skip("written with POSIX functions")
+
+
+class TestPerformance(EmbeddingTests):
+ def test_perf_single_threaded(self):
+ perf_cffi = self.prepare_module('perf')
+ self.compile('perf-test', [perf_cffi], opt=True)
+ output = self.execute('perf-test')
+ print('='*79)
+ print(output.rstrip())
+ print('='*79)
+
+ def test_perf_in_1_thread(self):
+ perf_cffi = self.prepare_module('perf')
+ self.compile('perf-test', [perf_cffi], opt=True, threads=True,
+ defines={'PTEST_USE_THREAD': '1'})
+ output = self.execute('perf-test')
+ print('='*79)
+ print(output.rstrip())
+ print('='*79)
+
+ def test_perf_in_2_threads(self):
+ perf_cffi = self.prepare_module('perf')
+ self.compile('perf-test', [perf_cffi], opt=True, threads=True,
+ defines={'PTEST_USE_THREAD': '2'})
+ output = self.execute('perf-test')
+ print('='*79)
+ print(output.rstrip())
+ print('='*79)
+
+ def test_perf_in_4_threads(self):
+ perf_cffi = self.prepare_module('perf')
+ self.compile('perf-test', [perf_cffi], opt=True, threads=True,
+ defines={'PTEST_USE_THREAD': '4'})
+ output = self.execute('perf-test')
+ print('='*79)
+ print(output.rstrip())
+ print('='*79)
+
+ def test_perf_in_8_threads(self):
+ perf_cffi = self.prepare_module('perf')
+ self.compile('perf-test', [perf_cffi], opt=True, threads=True,
+ defines={'PTEST_USE_THREAD': '8'})
+ output = self.execute('perf-test')
+ print('='*79)
+ print(output.rstrip())
+ print('='*79)
diff --git a/pypy/module/test_lib_pypy/cffi_tests/embedding/test_recursive.py b/pypy/module/test_lib_pypy/cffi_tests/embedding/test_recursive.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/cffi_tests/embedding/test_recursive.py
@@ -0,0 +1,16 @@
+# Generated by pypy/tool/import_cffi.py
+from pypy.module.test_lib_pypy.cffi_tests.embedding.test_basic import EmbeddingTests
+
+
+class TestRecursive(EmbeddingTests):
+ def test_recursive(self):
+ add_recursive_cffi = self.prepare_module('add_recursive')
+ self.compile('add_recursive-test', [add_recursive_cffi])
+ output = self.execute('add_recursive-test')
+ assert output == ("preparing REC\n"
+ "some_callback(400)\n"
+ "adding 400 and 9\n"
+ "<<< 409 >>>\n"
+ "adding 40 and 2\n"
+ "adding 100 and -5\n"
+ "got: 42 95\n")
diff --git a/pypy/module/test_lib_pypy/cffi_tests/embedding/test_thread.py b/pypy/module/test_lib_pypy/cffi_tests/embedding/test_thread.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/cffi_tests/embedding/test_thread.py
@@ -0,0 +1,62 @@
+# Generated by pypy/tool/import_cffi.py
+from pypy.module.test_lib_pypy.cffi_tests.embedding.test_basic import EmbeddingTests
+
+
+class TestThread(EmbeddingTests):
+ def test_first_calls_in_parallel(self):
+ add1_cffi = self.prepare_module('add1')
+ self.compile('thread1-test', [add1_cffi], threads=True)
+ for i in range(50):
+ output = self.execute('thread1-test')
+ assert output == ("starting\n"
+ "preparing...\n" +
+ "adding 40 and 2\n" * 10 +
+ "done\n")
+
+ def _take_out(self, text, content):
+ assert content in text
+ i = text.index(content)
+ return text[:i] + text[i+len(content):]
+
+ def test_init_different_modules_in_different_threads(self):
+ add1_cffi = self.prepare_module('add1')
+ add2_cffi = self.prepare_module('add2')
+ self.compile('thread2-test', [add1_cffi, add2_cffi], threads=True)
+ output = self.execute('thread2-test')
+ output = self._take_out(output, "preparing")
+ output = self._take_out(output, ".")
+ output = self._take_out(output, ".")
+ # at least the 3rd dot should be after everything from ADD2
+ assert output == ("starting\n"
+ "prepADD2\n"
+ "adding 1000 and 200 and 30\n"
+ ".\n"
+ "adding 40 and 2\n"
+ "done\n")
+
+ def test_alt_issue(self):
+ add1_cffi = self.prepare_module('add1')
+ add2_cffi = self.prepare_module('add2')
+ self.compile('thread2-test', [add1_cffi, add2_cffi],
+ threads=True, defines={'T2TEST_AGAIN_ADD1': '1'})
+ output = self.execute('thread2-test')
+ output = self._take_out(output, "adding 40 and 2\n")
+ assert output == ("starting\n"
+ "preparing...\n"
+ "adding -1 and -1\n"
+ "prepADD2\n"
+ "adding 1000 and 200 and 30\n"
+ "done\n")
+
+ def test_load_in_parallel_more(self):
+ add2_cffi = self.prepare_module('add2')
+ add3_cffi = self.prepare_module('add3')
+ self.compile('thread3-test', [add2_cffi, add3_cffi], threads=True)
+ for i in range(150):
+ output = self.execute('thread3-test')
+ for j in range(10):
+ output = self._take_out(output, "adding 40 and 2 and 100\n")
+ output = self._take_out(output, "adding 1000, 200, 30, 4\n")
+ assert output == ("starting\n"
+ "prepADD2\n"
+ "done\n")
diff --git a/pypy/module/test_lib_pypy/cffi_tests/embedding/test_tlocal.py b/pypy/module/test_lib_pypy/cffi_tests/embedding/test_tlocal.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/cffi_tests/embedding/test_tlocal.py
@@ -0,0 +1,11 @@
+# Generated by pypy/tool/import_cffi.py
+from pypy.module.test_lib_pypy.cffi_tests.embedding.test_basic import EmbeddingTests
+
+
+class TestThreadLocal(EmbeddingTests):
+ def test_thread_local(self):
+ tlocal_cffi = self.prepare_module('tlocal')
+ self.compile('tlocal-test', [tlocal_cffi], threads=True)
+ for i in range(10):
+ output = self.execute('tlocal-test')
+ assert output == "done\n"
diff --git a/pypy/module/test_lib_pypy/cffi_tests/embedding/tlocal.py b/pypy/module/test_lib_pypy/cffi_tests/embedding/tlocal.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/cffi_tests/embedding/tlocal.py
@@ -0,0 +1,34 @@
+# Generated by pypy/tool/import_cffi.py
+import cffi
+
+ffi = cffi.FFI()
+
+ffi.embedding_api("""
+ int add1(int, int);
+""")
+
+ffi.embedding_init_code(r"""
+ from _tlocal_cffi import ffi
+ import itertools
+ try:
+ import thread
+ g_seen = itertools.count().next
+ except ImportError:
+ import _thread as thread # py3
+ g_seen = itertools.count().__next__
+ tloc = thread._local()
+
+ @ffi.def_extern()
+ def add1(x, y):
+ try:
+ num = tloc.num
+ except AttributeError:
+ num = tloc.num = g_seen() * 1000
+ return x + y + num
+""")
+
+ffi.set_source("_tlocal_cffi", """
+""")
+
+fn = ffi.compile(verbose=True)
+print('FILENAME: %s' % (fn,))
More information about the pypy-commit
mailing list