[pypy-commit] pypy cffi-static-callback: update to cffi/4a750b4d4bfa
arigo
noreply at buildbot.pypy.org
Wed Nov 18 05:04:53 EST 2015
Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-static-callback
Changeset: r80750:f727b3445c49
Date: 2015-11-18 10:03 +0100
http://bitbucket.org/pypy/pypy/changeset/f727b3445c49/
Log: update to cffi/4a750b4d4bfa
diff --git a/lib_pypy/cffi/_cffi_include.h b/lib_pypy/cffi/_cffi_include.h
--- a/lib_pypy/cffi/_cffi_include.h
+++ b/lib_pypy/cffi/_cffi_include.h
@@ -147,7 +147,7 @@
#define _cffi_convert_array_from_object \
((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[24])
#define _cffi_call_python \
- ((void(*)(struct _cffi_callpy_s *, char *))_cffi_exports[25])
+ ((void(*)(struct _cffi_externpy_s *, char *))_cffi_exports[25])
#define _CFFI_NUM_EXPORTS 26
typedef struct _ctypedescr CTypeDescrObject;
@@ -206,7 +206,7 @@
/********** end CPython-specific section **********/
#else
_CFFI_UNUSED_FN
-static void (*_cffi_call_python)(struct _cffi_callpy_s *, char *);
+static void (*_cffi_call_python)(struct _cffi_externpy_s *, char *);
#endif
diff --git a/lib_pypy/cffi/cffi_opcode.py b/lib_pypy/cffi/cffi_opcode.py
--- a/lib_pypy/cffi/cffi_opcode.py
+++ b/lib_pypy/cffi/cffi_opcode.py
@@ -54,7 +54,7 @@
OP_DLOPEN_FUNC = 35
OP_DLOPEN_CONST = 37
OP_GLOBAL_VAR_F = 39
-OP_CALL_PYTHON = 41
+OP_EXTERN_PYTHON = 41
PRIM_VOID = 0
PRIM_BOOL = 1
diff --git a/lib_pypy/cffi/cparser.py b/lib_pypy/cffi/cparser.py
--- a/lib_pypy/cffi/cparser.py
+++ b/lib_pypy/cffi/cparser.py
@@ -29,7 +29,7 @@
_r_stdcall1 = re.compile(r"\b(__stdcall|WINAPI)\b")
_r_stdcall2 = re.compile(r"[(]\s*(__stdcall|WINAPI)\b")
_r_cdecl = re.compile(r"\b__cdecl\b")
-_r_cffi_call_python = re.compile(r"\bCFFI_CALL_PYTHON\b")
+_r_extern_python = re.compile(r'\bextern\s*"Python"\s*.')
_r_star_const_space = re.compile( # matches "* const "
r"[*]\s*((const|volatile|restrict)\b\s*)+")
@@ -81,6 +81,47 @@
parts.append(csource)
return ''.join(parts)
+def _preprocess_extern_python(csource):
+ # input: `extern "Python" int foo(int);` or
+ # `extern "Python" { int foo(int); }`
+ # output:
+ # void __cffi_extern_python_start;
+ # int foo(int);
+ # void __cffi_extern_python_stop;
+ parts = []
+ while True:
+ match = _r_extern_python.search(csource)
+ if not match:
+ break
+ endpos = match.end() - 1
+ #print
+ #print ''.join(parts)+csource
+ #print '=>'
+ parts.append(csource[:match.start()])
+ parts.append('void __cffi_extern_python_start; ')
+ if csource[endpos] == '{':
+ # grouping variant
+ closing = csource.find('}', endpos)
+ if closing < 0:
+ raise api.CDefError("'extern \"Python\" {': no '}' found")
+ if csource.find('{', endpos + 1, closing) >= 0:
+ raise NotImplementedError("cannot use { } inside a block "
+ "'extern \"Python\" { ... }'")
+ parts.append(csource[endpos+1:closing])
+ csource = csource[closing+1:]
+ else:
+ # non-grouping variant
+ semicolon = csource.find(';', endpos)
+ if semicolon < 0:
+ raise api.CDefError("'extern \"Python\": no ';' found")
+ parts.append(csource[endpos:semicolon+1])
+ csource = csource[semicolon+1:]
+ parts.append(' void __cffi_extern_python_stop;')
+ #print ''.join(parts)+csource
+ #print
+ parts.append(csource)
+ return ''.join(parts)
+
def _preprocess(csource):
# Remove comments. NOTE: this only work because the cdef() section
# should not contain any string literal!
@@ -105,8 +146,8 @@
csource = _r_stdcall1.sub(' volatile volatile const ', csource)
csource = _r_cdecl.sub(' ', csource)
#
- # Replace "CFFI_CALL_PYTHON" with "void CFFI_CALL_PYTHON;"
- csource = _r_cffi_call_python.sub('void CFFI_CALL_PYTHON;', csource)
+ # Replace `extern "Python"` with start/end markers
+ csource = _preprocess_extern_python(csource)
#
# Replace "[...]" with "[__dotdotdotarray__]"
csource = _r_partial_array.sub('[__dotdotdotarray__]', csource)
@@ -263,9 +304,8 @@
break
#
try:
- self._found_cffi_call_python = False
+ self._inside_extern_python = False
for decl in iterator:
- old_cffi_call_python = self._found_cffi_call_python
if isinstance(decl, pycparser.c_ast.Decl):
self._parse_decl(decl)
elif isinstance(decl, pycparser.c_ast.Typedef):
@@ -288,8 +328,6 @@
self._declare('typedef ' + decl.name, realtype, quals=quals)
else:
raise api.CDefError("unrecognized construct", decl)
- if old_cffi_call_python and self._found_cffi_call_python:
- raise api.CDefError("CFFI_CALL_PYTHON misplaced")
except api.FFIError as e:
msg = self._convert_pycparser_error(e, csource)
if msg:
@@ -338,9 +376,8 @@
def _declare_function(self, tp, quals, decl):
tp = self._get_type_pointer(tp, quals)
- if self._found_cffi_call_python:
- self._declare('call_python ' + decl.name, tp)
- self._found_cffi_call_python = False
+ if self._inside_extern_python:
+ self._declare('extern_python ' + decl.name, tp)
else:
self._declare('function ' + decl.name, tp)
@@ -378,15 +415,23 @@
_r_int_literal.match(decl.init.expr.value)):
self._add_integer_constant(decl.name,
'-' + decl.init.expr.value)
- elif tp is model.void_type and decl.name == 'CFFI_CALL_PYTHON':
- # hack: "CFFI_CALL_PYTHON" in the C source is replaced
- # with "void CFFI_CALL_PYTHON;", which when parsed arrives
- # at this point and sets this flag:
- self._found_cffi_call_python = True
- elif (quals & model.Q_CONST) and not tp.is_array_type:
- self._declare('constant ' + decl.name, tp, quals=quals)
+ elif (tp is model.void_type and
+ decl.name.startswith('__cffi_extern_python_')):
+ # hack: `extern "Python"` in the C source is replaced
+ # with "void __cffi_extern_python_start;" and
+ # "void __cffi_extern_python_stop;"
+ self._inside_extern_python = not self._inside_extern_python
+ assert self._inside_extern_python == (
+ decl.name == '__cffi_extern_python_start')
else:
- self._declare('variable ' + decl.name, tp, quals=quals)
+ if self._inside_extern_python:
+ raise api.CDefError(
+ "cannot declare constants or "
+ "variables with 'extern \"Python\"'")
+ if (quals & model.Q_CONST) and not tp.is_array_type:
+ self._declare('constant ' + decl.name, tp, quals=quals)
+ else:
+ self._declare('variable ' + decl.name, tp, quals=quals)
def parse_type(self, cdecl):
return self.parse_type_and_quals(cdecl)[0]
diff --git a/lib_pypy/cffi/parse_c_type.h b/lib_pypy/cffi/parse_c_type.h
--- a/lib_pypy/cffi/parse_c_type.h
+++ b/lib_pypy/cffi/parse_c_type.h
@@ -27,7 +27,7 @@
#define _CFFI_OP_DLOPEN_FUNC 35
#define _CFFI_OP_DLOPEN_CONST 37
#define _CFFI_OP_GLOBAL_VAR_F 39
-#define _CFFI_OP_CALL_PYTHON 41
+#define _CFFI_OP_EXTERN_PYTHON 41
#define _CFFI_PRIM_VOID 0
#define _CFFI_PRIM_BOOL 1
@@ -161,7 +161,7 @@
const char *error_message;
};
-struct _cffi_callpy_s {
+struct _cffi_externpy_s {
const char *name;
size_t size_of_result;
void *reserved1, *reserved2;
diff --git a/lib_pypy/cffi/recompiler.py b/lib_pypy/cffi/recompiler.py
--- a/lib_pypy/cffi/recompiler.py
+++ b/lib_pypy/cffi/recompiler.py
@@ -118,7 +118,7 @@
class Recompiler:
- _num_callpy = 0
+ _num_externpy = 0
def __init__(self, ffi, module_name, target_is_python=False):
self.ffi = ffi
@@ -358,8 +358,8 @@
prnt(' NULL, /* no includes */')
prnt(' %d, /* num_types */' % (len(self.cffi_types),))
flags = 0
- if self._num_callpy:
- flags |= 1 # set to mean "uses _cffi_call_python"
+ if self._num_externpy:
+ flags |= 1 # set to mean that we use extern "Python"
prnt(' %d, /* flags */' % flags)
prnt('};')
prnt()
@@ -370,10 +370,11 @@
prnt('PyMODINIT_FUNC')
prnt('_cffi_pypyinit_%s(const void *p[])' % (base_module_name,))
prnt('{')
- prnt(' if (((intptr_t)p[0]) >= 0x0A03) {')
- prnt(' _cffi_call_python = (void(*)(struct _cffi_callpy_s *, '
- 'char *))p[1];')
- prnt(' }')
+ if self._num_externpy:
+ prnt(' if (((intptr_t)p[0]) >= 0x0A03) {')
+ prnt(' _cffi_call_python = '
+ '(void(*)(struct _cffi_externpy_s *, char *))p[1];')
+ prnt(' }')
prnt(' p[0] = (const void *)%s;' % VERSION)
prnt(' p[1] = &_cffi_type_context;')
prnt('}')
@@ -1116,13 +1117,13 @@
GlobalExpr(name, '_cffi_var_%s' % name, CffiOp(op, type_index)))
# ----------
- # CFFI_CALL_PYTHON
+ # extern "Python"
- def _generate_cpy_call_python_collecttype(self, tp, name):
+ def _generate_cpy_extern_python_collecttype(self, tp, name):
assert isinstance(tp, model.FunctionPtrType)
self._do_collect_type(tp)
- def _generate_cpy_call_python_decl(self, tp, name):
+ def _generate_cpy_extern_python_decl(self, tp, name):
prnt = self._prnt
if isinstance(tp.result, model.VoidType):
size_of_result = '0'
@@ -1130,7 +1131,7 @@
context = 'result of %s' % name
size_of_result = '(int)sizeof(%s)' % (
tp.result.get_c_name('', context),)
- prnt('static struct _cffi_callpy_s _cffi_callpy__%s =' % name)
+ prnt('static struct _cffi_externpy_s _cffi_externpy__%s =' % name)
prnt(' { "%s", %s };' % (name, size_of_result))
prnt()
#
@@ -1166,23 +1167,23 @@
arg = '&' + arg
type = model.PointerType(type)
prnt(' *(%s)(p + %d) = %s;' % (type.get_c_name('*'), i*8, arg))
- prnt(' _cffi_call_python(&_cffi_callpy__%s, p);' % name)
+ prnt(' _cffi_call_python(&_cffi_externpy__%s, p);' % name)
if not isinstance(tp.result, model.VoidType):
prnt(' return *(%s)p;' % (tp.result.get_c_name('*'),))
prnt('}')
prnt()
- self._num_callpy += 1
+ self._num_externpy += 1
- def _generate_cpy_call_python_ctx(self, tp, name):
+ def _generate_cpy_extern_python_ctx(self, tp, name):
if self.target_is_python:
raise ffiplatform.VerificationError(
- "cannot use CFFI_CALL_PYTHON in the ABI mode")
+ "cannot use 'extern \"Python\"' in the ABI mode")
if tp.ellipsis:
- raise NotImplementedError("CFFI_CALL_PYTHON with a vararg function")
+ raise NotImplementedError("a vararg function is extern \"Python\"")
type_index = self._typesdict[tp]
- type_op = CffiOp(OP_CALL_PYTHON, type_index)
+ type_op = CffiOp(OP_EXTERN_PYTHON, type_index)
self._lsts["global"].append(
- GlobalExpr(name, '&_cffi_callpy__%s' % name, type_op, name))
+ GlobalExpr(name, '&_cffi_externpy__%s' % name, type_op, name))
# ----------
# emitting the opcodes for individual types
diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_parsing.py b/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_parsing.py
--- a/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_parsing.py
+++ b/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_parsing.py
@@ -407,16 +407,30 @@
"long(*)(), "
"short(%s*)(short))'>" % (stdcall, stdcall))
-def test_CFFI_CALL_PYTHON():
+def test_extern_python():
ffi = FFI()
ffi.cdef("""
+ int bok(int, int);
+ extern "Python" int foobar(int, int);
int baz(int, int);
- CFFI_CALL_PYTHON int foobar(int, int);
""")
- assert 'variable CFFI_CALL_PYTHON' not in ffi._parser._declarations
- assert 'function baz' in ffi._parser._declarations
- assert 'call_python baz' not in ffi._parser._declarations
- assert 'function foobar' not in ffi._parser._declarations
- assert 'call_python foobar' in ffi._parser._declarations
+ assert sorted(ffi._parser._declarations) == [
+ 'extern_python foobar', 'function baz', 'function bok']
+ assert (ffi._parser._declarations['function bok'] ==
+ ffi._parser._declarations['extern_python foobar'] ==
+ ffi._parser._declarations['function baz'])
+
+def test_extern_python_group():
+ ffi = FFI()
+ ffi.cdef("""
+ int bok(int);
+ extern "Python" {int foobar(int, int);int bzrrr(int);}
+ int baz(int, int);
+ """)
+ assert sorted(ffi._parser._declarations) == [
+ 'extern_python bzrrr', 'extern_python foobar',
+ 'function baz', 'function bok']
assert (ffi._parser._declarations['function baz'] ==
- ffi._parser._declarations['call_python foobar'])
+ ffi._parser._declarations['extern_python foobar'] !=
+ ffi._parser._declarations['function bok'] ==
+ ffi._parser._declarations['extern_python bzrrr'])
diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py
--- a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py
+++ b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py
@@ -1495,24 +1495,26 @@
def __exit__(self, *args):
sys.stderr = self.old_stderr
-def test_call_python_1():
+def test_extern_python_1():
ffi = FFI()
ffi.cdef("""
- CFFI_CALL_PYTHON int bar(int, int);
- CFFI_CALL_PYTHON void baz(int, int);
- CFFI_CALL_PYTHON int bok(void);
- CFFI_CALL_PYTHON void boz(void);
+ extern "Python" {
+ int bar(int, int);
+ void baz(int, int);
+ int bok(void);
+ void boz(void);
+ }
""")
- lib = verify(ffi, 'test_call_python_1', "")
+ lib = verify(ffi, 'test_extern_python_1', "")
assert ffi.typeof(lib.bar) == ffi.typeof("int(*)(int, int)")
with StdErrCapture() as f:
res = lib.bar(4, 5)
assert res == 0
assert f.getvalue() == (
- "CFFI_CALL_PYTHON: function bar() called, but no code was attached "
- "to it yet with ffi.call_python('bar'). Returning 0.\n")
+ "extern \"Python\": function bar() called, but no code was attached "
+ "to it yet with @ffi.def_extern(). Returning 0.\n")
- @ffi.call_python("bar")
+ @ffi.def_extern("bar")
def my_bar(x, y):
seen.append(("Bar", x, y))
return x * y
@@ -1522,7 +1524,7 @@
assert seen == [("Bar", 6, 7)]
assert res == 42
- @ffi.call_python()
+ @ffi.def_extern()
def baz(x, y):
seen.append(("Baz", x, y))
seen = []
@@ -1532,53 +1534,53 @@
assert type(seen[0][1]) is type(seen[0][2]) is int
assert baz == lib.baz
- @ffi.call_python(name="bok")
- def bok():
+ @ffi.def_extern(name="bok")
+ def bokk():
seen.append("Bok")
return 42
seen = []
- assert lib.bok() == bok() == 42
+ assert lib.bok() == bokk() == 42
assert seen == ["Bok", "Bok"]
- @ffi.call_python()
+ @ffi.def_extern()
def boz():
seen.append("Boz")
seen = []
assert lib.boz() is boz() is None
assert seen == ["Boz", "Boz"]
-def test_call_python_bogus_name():
+def test_extern_python_bogus_name():
ffi = FFI()
ffi.cdef("int abc;")
- lib = verify(ffi, 'test_call_python_bogus_name', "int abc;")
+ lib = verify(ffi, 'test_extern_python_bogus_name', "int abc;")
def fn():
pass
- py.test.raises(ffi.error, ffi.call_python("unknown_name"), fn)
- py.test.raises(ffi.error, ffi.call_python("abc"), fn)
+ py.test.raises(ffi.error, ffi.def_extern("unknown_name"), fn)
+ py.test.raises(ffi.error, ffi.def_extern("abc"), fn)
assert lib.abc == 0
- e = py.test.raises(ffi.error, ffi.call_python("abc"), fn)
- assert str(e.value) == ("ffi.call_python('abc'): name not found as a "
- "CFFI_CALL_PYTHON line from the cdef")
- e = py.test.raises(ffi.error, ffi.call_python(), fn)
- assert str(e.value) == ("ffi.call_python('fn'): name not found as a "
- "CFFI_CALL_PYTHON line from the cdef")
+ e = py.test.raises(ffi.error, ffi.def_extern("abc"), fn)
+ assert str(e.value) == ("ffi.def_extern('abc'): no 'extern \"Python\"' "
+ "function with this name")
+ e = py.test.raises(ffi.error, ffi.def_extern(), fn)
+ assert str(e.value) == ("ffi.def_extern('fn'): no 'extern \"Python\"' "
+ "function with this name")
#
- py.test.raises(TypeError, ffi.call_python(42), fn)
- py.test.raises((TypeError, AttributeError), ffi.call_python(), "foo")
+ py.test.raises(TypeError, ffi.def_extern(42), fn)
+ py.test.raises((TypeError, AttributeError), ffi.def_extern(), "foo")
class X:
pass
x = X()
x.__name__ = x
- py.test.raises(TypeError, ffi.call_python(), x)
+ py.test.raises(TypeError, ffi.def_extern(), x)
-def test_call_python_bogus_result_type():
+def test_extern_python_bogus_result_type():
ffi = FFI()
- ffi.cdef("CFFI_CALL_PYTHON void bar(int);")
- lib = verify(ffi, 'test_call_python_bogus_result_type', "")
+ ffi.cdef("""extern "Python" void bar(int);""")
+ lib = verify(ffi, 'test_extern_python_bogus_result_type', "")
#
def bar(n):
return n * 10
- bar1 = ffi.call_python()(bar)
+ bar1 = ffi.def_extern()(bar)
with StdErrCapture() as f:
res = bar1(321)
assert res is None
@@ -1587,39 +1589,39 @@
"Trying to convert the result back to C:\n"
"TypeError: callback with the return type 'void' must return None\n")
-def test_call_python_redefine():
+def test_extern_python_redefine():
ffi = FFI()
- ffi.cdef("CFFI_CALL_PYTHON int bar(int);")
- lib = verify(ffi, 'test_call_python_redefine', "")
+ ffi.cdef("""extern "Python" int bar(int);""")
+ lib = verify(ffi, 'test_extern_python_redefine', "")
#
- @ffi.call_python()
+ @ffi.def_extern()
def bar(n):
return n * 10
assert lib.bar(42) == 420
#
- @ffi.call_python()
+ @ffi.def_extern()
def bar(n):
return -n
assert lib.bar(42) == -42
-def test_call_python_struct():
+def test_extern_python_struct():
ffi = FFI()
ffi.cdef("""
struct foo_s { int a, b, c; };
- CFFI_CALL_PYTHON int bar(int, struct foo_s, int);
- CFFI_CALL_PYTHON struct foo_s baz(int, int);
- CFFI_CALL_PYTHON struct foo_s bok(void);
+ extern "Python" int bar(int, struct foo_s, int);
+ extern "Python" { struct foo_s baz(int, int);
+ struct foo_s bok(void); }
""")
- lib = verify(ffi, 'test_call_python_struct',
+ lib = verify(ffi, 'test_extern_python_struct',
"struct foo_s { int a, b, c; };")
#
- @ffi.call_python()
+ @ffi.def_extern()
def bar(x, s, z):
return x + s.a + s.b + s.c + z
res = lib.bar(1000, [1001, 1002, 1004], 1008)
assert res == 5015
#
- @ffi.call_python()
+ @ffi.def_extern()
def baz(x, y):
return [x + y, x - y, x * y]
res = lib.baz(1000, 42)
@@ -1627,22 +1629,22 @@
assert res.b == 958
assert res.c == 42000
#
- @ffi.call_python()
+ @ffi.def_extern()
def bok():
return [10, 20, 30]
res = lib.bok()
assert [res.a, res.b, res.c] == [10, 20, 30]
-def test_call_python_long_double():
+def test_extern_python_long_double():
ffi = FFI()
ffi.cdef("""
- CFFI_CALL_PYTHON int bar(int, long double, int);
- CFFI_CALL_PYTHON long double baz(int, int);
- CFFI_CALL_PYTHON long double bok(void);
+ extern "Python" int bar(int, long double, int);
+ extern "Python" long double baz(int, int);
+ extern "Python" long double bok(void);
""")
- lib = verify(ffi, 'test_call_python_long_double', "")
+ lib = verify(ffi, 'test_extern_python_long_double', "")
#
- @ffi.call_python()
+ @ffi.def_extern()
def bar(x, l, z):
seen.append((x, l, z))
return 6
@@ -1651,42 +1653,42 @@
expected = ffi.cast("long double", 3.5)
assert repr(seen) == repr([(10, expected, 20)])
#
- @ffi.call_python()
+ @ffi.def_extern()
def baz(x, z):
assert x == 10 and z == 20
return expected
res = lib.baz(10, 20)
assert repr(res) == repr(expected)
#
- @ffi.call_python()
+ @ffi.def_extern()
def bok():
return expected
res = lib.bok()
assert repr(res) == repr(expected)
-def test_call_python_signature():
+def test_extern_python_signature():
ffi = FFI()
- lib = verify(ffi, 'test_call_python_signature', "")
- py.test.raises(TypeError, ffi.call_python(425), None)
- py.test.raises(TypeError, ffi.call_python, 'a', 'b', 'c', 'd')
+ lib = verify(ffi, 'test_extern_python_signature', "")
+ py.test.raises(TypeError, ffi.def_extern(425), None)
+ py.test.raises(TypeError, ffi.def_extern, 'a', 'b', 'c', 'd')
-def test_call_python_errors():
+def test_extern_python_errors():
ffi = FFI()
ffi.cdef("""
- CFFI_CALL_PYTHON int bar(int);
+ extern "Python" int bar(int);
""")
- lib = verify(ffi, 'test_call_python_errors', "")
+ lib = verify(ffi, 'test_extern_python_errors', "")
seen = []
def oops(*args):
seen.append(args)
- @ffi.call_python(onerror=oops)
+ @ffi.def_extern(onerror=oops)
def bar(x):
return x + ""
assert bar(10) == 0
- @ffi.call_python(name="bar", onerror=oops, error=-66)
+ @ffi.def_extern(name="bar", onerror=oops, error=-66)
def bar2(x):
return x + ""
assert bar(10) == -66
@@ -1702,5 +1704,5 @@
assert tb.tb_frame.f_code.co_name == "bar2"
#
# a case where 'onerror' is not callable
- py.test.raises(TypeError, ffi.call_python(name='bar', onerror=42),
+ py.test.raises(TypeError, ffi.def_extern(name='bar', onerror=42),
lambda x: x)
More information about the pypy-commit
mailing list