[pypy-commit] creflect default: In-progress: change the outer layers
arigo
noreply at buildbot.pypy.org
Wed Sep 17 19:25:02 CEST 2014
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r48:f76ddeee4479
Date: 2014-09-17 19:25 +0200
http://bitbucket.org/cffi/creflect/changeset/f76ddeee4479/
Log: In-progress: change the outer layers
diff --git a/bin/creflect b/bin/creflect
--- a/bin/creflect
+++ b/bin/creflect
@@ -1,5 +1,5 @@
#! /usr/bin/env python
import sys
-import creflect
-sys.exit(creflect.main(sys.argv[1:]))
+from creflect.cmdline import main
+sys.exit(main(sys.argv[1:]))
diff --git a/creflect/__init__.py b/creflect/__init__.py
--- a/creflect/__init__.py
+++ b/creflect/__init__.py
@@ -1,3 +1,4 @@
-from cmdline import main
+from .driver import expand_cdef, CDefError
-__version__ = (1, 0)
+__version__ = "1.0.0"
+__version_info__ = (1, 0, 0)
diff --git a/creflect/codegen.py b/creflect/codegen.py
--- a/creflect/codegen.py
+++ b/creflect/codegen.py
@@ -120,6 +120,7 @@
def generate(self, funcname):
outerblock = CodeBlock(self)
+ outerblock.add_include('stdio.h')
outerblock.writeline('int %s(char *r)' % (funcname,))
funcblock = CodeBlock(self)
diff --git a/creflect/driver.py b/creflect/driver.py
--- a/creflect/driver.py
+++ b/creflect/driver.py
@@ -2,6 +2,11 @@
from cparser import CSource, CDefError
+def expand_cdef(input, reflection_func_name, first_lineno=1):
+ csource = CSource(input, first_lineno)
+ return transform_cdef(csource, reflection_func_name)
+
+
def get_preprocessor_directive(line):
if line.startswith('#'):
line = line[1:].split()
@@ -51,24 +56,19 @@
''' % (reflection_func_name,))
-def output_main(outputf):
- outputf.write(r'''
+DEBUG_TEMPLATE = r'''
/* debugging only */
#include <stdio.h>
#include <stdlib.h>
-static void __creflect_report(char *cmd, void **args, int nargs) {
- int i;
- printf("%s", cmd);
- for (i = 0; i < nargs; i++) {
- printf(" %ld", (long)args[i]);
- args[i] = (void *)0;
- }
- printf("\n");
- if (cmd[0] == 'E')
- exit(1);
+#define _CREFLECT_MAIN %s
+int main(void) {
+ int size = _CREFLECT_MAIN((char *)0);
+ char *result = malloc(size);
+ int err = _CREFLECT_MAIN(result);
+ printf("%%s", result);
+ return (err != 0);
}
-int main(void) { /* for debugging only: compile and run as an executable */
- __CREFLECT_PREV(__creflect_report);
- return 0;
-}
-''')
+'''
+
+def get_debug_code(reflection_func_name):
+ return DEBUG_TEMPLATE % (reflection_func_name,)
diff --git a/test/test_driver.py b/test/test_driver.py
--- a/test/test_driver.py
+++ b/test/test_driver.py
@@ -1,9 +1,37 @@
import os
from StringIO import StringIO
-from creflect import driver
+from creflect.driver import expand_cdef, get_debug_code
from .udir import udir
-def test_driver():
+def compile_and_run(real_code, generated_code, funcname):
+ f = open(str(udir.join(funcname + '.c')), 'w')
+ print >> f, '/* real code */'
+ print >> f, real_code
+ print >> f, '/* generated code */'
+ print >> f, generated_code
+ print >> f, get_debug_code(funcname)
+ f.close()
+ #
+ err = os.system(
+ "cd '%s' && gcc -fPIC -Werror -Wall %s.c -o %s"
+ % (str(udir), funcname, funcname))
+ assert err == 0
+ #
+ g = os.popen("cd '%s' && %s" % (str(udir), funcname), 'r')
+ data = g.read()
+ err = g.close()
+ assert err is None
+ return data
+
+
+def test_expand_cdef():
+ real_code = "typedef short a_t[42];\n"
+ gen = expand_cdef("typedef long a_t[20];", "test_expand_cdef")
+ data = compile_and_run(real_code, gen, "test_expand_cdef")
+ assert data == real_code
+
+
+def XXXtest_driver():
f = StringIO("""
typedef unsigned int a_t[5];
More information about the pypy-commit
mailing list