Author: Armin Rigo <[email protected]>
Branch: wchar_t
Changeset: r608:2375bc677da9
Date: 2012-07-09 16:26 +0200
http://bitbucket.org/cffi/cffi/changeset/2375bc677da9/
Log: Basic unicode support is complete.
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -2111,6 +2111,18 @@
value = (unsigned char)PyString_AS_STRING(ob)[0];
}
}
+#ifdef HAVE_WCHAR_H
+ else if (PyUnicode_Check(ob)) {
+ wchar_t ordinal;
+ if (_my_PyUnicode_AsSingleWideChar(ob, &ordinal) < 0) {
+ PyErr_Format(PyExc_TypeError,
+ "cannot cast unicode of length %zd to ctype '%s'",
+ PyUnicode_GET_SIZE(ob), ct->ct_name);
+ return NULL;
+ }
+ value = (long)ordinal;
+ }
+#endif
else {
value = _my_PyLong_AsUnsignedLongLong(ob, 0);
if (value == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
@@ -2504,11 +2516,11 @@
td->ct_length = ptypes->align;
td->ct_extra = ffitype;
td->ct_flags = ptypes->flags;
- if (td->ct_flags & CT_PRIMITIVE_SIGNED) {
+ if (td->ct_flags & (CT_PRIMITIVE_SIGNED | CT_PRIMITIVE_CHAR)) {
if (td->ct_size <= sizeof(long))
td->ct_flags |= CT_PRIMITIVE_FITS_LONG;
}
- else if (td->ct_flags & (CT_PRIMITIVE_UNSIGNED | CT_PRIMITIVE_CHAR)) {
+ else if (td->ct_flags & CT_PRIMITIVE_UNSIGNED) {
if (td->ct_size < sizeof(long))
td->ct_flags |= CT_PRIMITIVE_FITS_LONG;
}
@@ -2738,8 +2750,10 @@
if (!(ftype->ct_flags & (CT_PRIMITIVE_SIGNED |
CT_PRIMITIVE_UNSIGNED |
CT_PRIMITIVE_CHAR)) ||
+#ifdef HAVE_WCHAR_H
((ftype->ct_flags & CT_PRIMITIVE_CHAR)
- && ftype->ct_size > sizeof(char)) ||
+ && ftype->ct_size == sizeof(wchar_t)) ||
+#endif
fbitsize == 0 ||
fbitsize > 8 * ftype->ct_size) {
PyErr_Format(PyExc_TypeError, "invalid bit field '%s'",
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -1281,6 +1281,7 @@
def test_wchar():
BWChar = new_primitive_type("wchar_t")
+ BInt = new_primitive_type("int")
pyuni4 = {1: True, 2: False}[len(u'\U00012345')]
wchar4 = {2: False, 4: True}[sizeof(BWChar)]
assert str(cast(BWChar, 0x45)) == "<cdata 'wchar_t' u'E'>"
@@ -1350,6 +1351,24 @@
assert str(w) == repr(w)
assert unicode(w) == u'\u1234'
assert int(w) == 0x1234
+ w = cast(BWChar, u'\u1234')
+ assert repr(w) == "<cdata 'wchar_t' u'\u1234'>"
+ assert str(w) == repr(w)
+ assert unicode(w) == u'\u1234'
+ assert int(w) == 0x1234
+ w = cast(BInt, u'\u1234')
+ assert repr(w) == "<cdata 'int' 4660>"
+ if wchar4:
+ w = cast(BWChar, u'\U00012345')
+ assert repr(w) == "<cdata 'wchar_t' u'\U00012345'>"
+ assert str(w) == repr(w)
+ assert unicode(w) == u'\U00012345'
+ assert int(w) == 0x12345
+ w = cast(BInt, u'\U00012345')
+ assert repr(w) == "<cdata 'int' 74565>"
+ py.test.raises(TypeError, cast, BInt, u'')
+ py.test.raises(TypeError, cast, BInt, u'XX')
+ assert int(cast(BInt, u'a')) == ord('a')
#
a = newp(BWCharArray, u'hello - world')
p = cast(BWCharP, a)
@@ -1367,7 +1386,6 @@
assert str(q) == repr(q)
py.test.raises(RuntimeError, unicode, q)
#
- BInt = new_primitive_type("int")
def cb(p):
assert repr(p).startswith("<cdata 'wchar_t *' 0x")
return len(unicode(p))
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit