[pypy-commit] cffi static-callback-embedding: Add a simple test (cpython 2.7 only)
arigo
pypy.commits at gmail.com
Fri Jan 1 05:11:11 EST 2016
Author: Armin Rigo <arigo at tunes.org>
Branch: static-callback-embedding
Changeset: r2502:ab417d35f58c
Date: 2016-01-01 11:10 +0100
http://bitbucket.org/cffi/cffi/changeset/ab417d35f58c/
Log: Add a simple test (cpython 2.7 only)
diff --git a/testing/embedding/__init__.py b/testing/embedding/__init__.py
new file mode 100644
diff --git a/testing/embedding/add1-test.c b/testing/embedding/add1-test.c
new file mode 100644
--- /dev/null
+++ b/testing/embedding/add1-test.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+extern int add1(int, int);
+
+
+int main(void)
+{
+ int x, y;
+ x = add1(40, 2);
+ y = add1(100, -5);
+ printf("got: %d %d\n", x, y);
+ return 0;
+}
diff --git a/testing/embedding/add1.py b/testing/embedding/add1.py
new file mode 100644
--- /dev/null
+++ b/testing/embedding/add1.py
@@ -0,0 +1,23 @@
+import cffi
+
+ffi = cffi.FFI()
+
+ffi.cdef("""
+ extern "Python" int add1(int, int);
+""", dllexport=True)
+
+ffi.embedding_init_code("""
+ print("preparing")
+
+ int(ord("A")) # check that built-ins are there
+
+ @ffi.def_extern()
+ def add1(x, y):
+ print "adding", x, "and", y
+ return x + y
+""")
+
+ffi.set_source("_add1_cffi", """
+""")
+
+ffi.compile()
diff --git a/testing/embedding/test_basic.py b/testing/embedding/test_basic.py
new file mode 100644
--- /dev/null
+++ b/testing/embedding/test_basic.py
@@ -0,0 +1,61 @@
+import sys, os
+import shutil, subprocess
+from testing.udir import udir
+
+local_dir = os.path.dirname(os.path.abspath(__file__))
+
+
+class EmbeddingTests:
+ _compiled_modules = set()
+
+ def get_path(self):
+ return str(udir.ensure('embedding', dir=True))
+
+ def _run(self, args, env=None):
+ print(args)
+ popen = subprocess.Popen(args, env=env, cwd=self.get_path())
+ err = popen.wait()
+ if err:
+ raise OSError("popen failed with exit code %r: %r" % (
+ err, args))
+
+ def prepare_module(self, name):
+ if name not in self._compiled_modules:
+ path = self.get_path()
+ filename = '%s.py' % name
+ env = os.environ.copy()
+ env['PYTHONPATH'] = os.path.dirname(os.path.dirname(local_dir))
+ self._run([sys.executable, os.path.join(local_dir, filename)],
+ env=env)
+ self._compiled_modules.add(name)
+
+ def compile(self, name, modules):
+ path = self.get_path()
+ filename = '%s.c' % name
+ shutil.copy(os.path.join(local_dir, filename), path)
+ self._run(['gcc', filename, '-o', name, '-L.'] +
+ ['%s.so' % modname for modname in modules] +
+ ['-lpython2.7'])
+
+ def execute(self, name):
+ path = self.get_path()
+ env = os.environ.copy()
+ env['LD_LIBRARY_PATH'] = path
+ popen = subprocess.Popen([name], cwd=path, stdout=subprocess.PIPE,
+ env=env)
+ 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):
+ 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")
More information about the pypy-commit
mailing list