[pypy-commit] cffi cffi-1.0: Accept unicode literals for type specifications too
arigo
noreply at buildbot.pypy.org
Fri May 8 20:47:45 CEST 2015
Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1937:8e9329205207
Date: 2015-05-08 20:48 +0200
http://bitbucket.org/cffi/cffi/changeset/8e9329205207/
Log: Accept unicode literals for type specifications too
diff --git a/_cffi1/ffi_obj.c b/_cffi1/ffi_obj.c
--- a/_cffi1/ffi_obj.c
+++ b/_cffi1/ffi_obj.c
@@ -158,6 +158,17 @@
else if ((accept & ACCEPT_CDATA) && CData_Check(arg)) {
return ((CDataObject *)arg)->c_type;
}
+#if PY_MAJOR_VERSION < 3
+ else if (PyUnicode_Check(arg)) {
+ CTypeDescrObject *result;
+ arg = PyUnicode_AsASCIIString(arg);
+ if (arg == NULL)
+ return NULL;
+ result = _ffi_type(ffi, arg, accept);
+ Py_DECREF(arg);
+ return result;
+ }
+#endif
else {
const char *m1 = (accept & ACCEPT_STRING) ? "string" : "";
const char *m2 = (accept & ACCEPT_CTYPE) ? "ctype object" : "";
diff --git a/_cffi1/test_recompiler.py b/_cffi1/test_recompiler.py
--- a/_cffi1/test_recompiler.py
+++ b/_cffi1/test_recompiler.py
@@ -281,6 +281,7 @@
#
assert ffi.offsetof("struct foo_s", "a") == 0
assert ffi.offsetof("struct foo_s", "b") == 4
+ assert ffi.offsetof(u"struct foo_s", u"b") == 4
#
py.test.raises(TypeError, ffi.addressof, p)
assert ffi.addressof(p[0]) == p
@@ -606,3 +607,22 @@
p = lib.ff7()
assert ffi.cast("int *", p)[0] == 42
assert lib.ff7b(p) == 42
+
+def test_unicode_libraries():
+ try:
+ unicode
+ except NameError:
+ py.test.skip("for python 2.x")
+ #
+ import math
+ lib_m = "m"
+ if sys.platform == 'win32':
+ #there is a small chance this fails on Mingw via environ $CC
+ import distutils.ccompiler
+ if distutils.ccompiler.get_default_compiler() == 'msvc':
+ lib_m = 'msvcrt'
+ ffi = FFI()
+ ffi.cdef(unicode("float sin(double); double cos(double);"))
+ lib = verify(ffi, 'test_math_sin_unicode', unicode('#include <math.h>'),
+ libraries=[unicode(lib_m)])
+ assert lib.cos(1.43) == math.cos(1.43)
diff --git a/_cffi1/test_unicode_literals.py b/_cffi1/test_unicode_literals.py
--- a/_cffi1/test_unicode_literals.py
+++ b/_cffi1/test_unicode_literals.py
@@ -7,16 +7,7 @@
#
#
#
-import sys, math
from _cffi_backend import FFI
-from _cffi1 import recompiler
-
-lib_m = "m"
-if sys.platform == 'win32':
- #there is a small chance this fails on Mingw via environ $CC
- import distutils.ccompiler
- if distutils.ccompiler.get_default_compiler() == 'msvc':
- lib_m = 'msvcrt'
def test_cast():
@@ -45,43 +36,8 @@
assert ffi.getctype("int**") == "int * *" # unicode literal
assert type(ffi.getctype("int**")) is str
-def test_cdef():
- ffi = FFI()
- ffi.cdef("typedef int foo_t[50];") # unicode literal
-
-def test_offsetof():
- ffi = FFI()
- ffi.cdef("typedef struct { int x, y; } foo_t;")
- assert ffi.offsetof("foo_t", "y") == 4 # unicode literal
-
-def test_enum():
- ffi = FFI()
- ffi.cdef("enum foo_e { AA, BB, CC };") # unicode literal
- x = ffi.cast("enum foo_e", 1)
- assert int(ffi.cast("int", x)) == 1
-
-def test_dlopen():
- ffi = FFI()
- ffi.cdef("double sin(double x);")
- m = ffi.dlopen(lib_m) # unicode literal
- x = m.sin(1.23)
- assert x == math.sin(1.23)
-
-def test_verify():
- ffi = FFI()
- ffi.cdef("double test_verify_1(double x);") # unicode literal
- lib = ffi.verify("double test_verify_1(double x) { return x * 42.0; }")
- assert lib.test_verify_1(-1.5) == -63.0
-
def test_callback():
ffi = FFI()
cb = ffi.callback("int(int)", # unicode literal
lambda x: x + 42)
assert cb(5) == 47
-
-def test_math_sin_unicode():
- ffi = FFI()
- ffi.cdef("float sin(double); double cos(double);")
- lib = recompiler.verify(ffi, 'test_math_sin_unicode', '#include <math.h>',
- libraries=[lib_m])
- assert lib.cos(1.43) == math.cos(1.43)
More information about the pypy-commit
mailing list