summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2012-08-12 18:08:43 +0200
committerArmin Rigo <arigo@tunes.org>2012-08-12 18:08:43 +0200
commit49bdc02a566011e48a122afff8b6c76ae2ad41eb (patch)
tree698334dc72c6427484432e7838b0a0a3e7e1263d
parent1da5d3c37168e73bbdf3ed97d7c3aad63adb7b38 (diff)
downloadcffi-49bdc02a566011e48a122afff8b6c76ae2ad41eb.tar.gz
More regularily in casts to floats.
-rw-r--r--c/_cffi_backend.c10
-rw-r--r--c/test_c.py7
2 files changed, 16 insertions, 1 deletions
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
index ce10a5d..357bd32 100644
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -2305,6 +2305,16 @@ static PyObject *b_cast(PyObject *self, PyObject *args)
}
value = (unsigned char)PyString_AS_STRING(io)[0];
}
+#if HAVE_WCHAR_H
+ else if (PyUnicode_Check(io)) {
+ wchar_t ordinal;
+ if (_my_PyUnicode_AsSingleWideChar(io, &ordinal) < 0) {
+ Py_DECREF(io);
+ goto cannot_cast;
+ }
+ value = (long)ordinal;
+ }
+#endif
else if ((ct->ct_flags & CT_IS_LONGDOUBLE) &&
CData_Check(io) &&
(((CDataObject *)io)->c_type->ct_flags & CT_IS_LONGDOUBLE)) {
diff --git a/c/test_c.py b/c/test_c.py
index 2d2df6a..12cf3f8 100644
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -82,6 +82,8 @@ def test_integer_types():
assert int(cast(p, max + 1)) == min
py.test.raises(TypeError, cast, p, None)
assert long(cast(p, min - 1)) == max
+ assert int(cast(p, b'\x08')) == 8
+ assert int(cast(p, u'\x08')) == 8
for name in ['char', 'short', 'int', 'long', 'long long']:
p = new_primitive_type('unsigned ' + name)
size = sizeof(p)
@@ -91,6 +93,8 @@ def test_integer_types():
assert int(cast(p, -1)) == max
assert int(cast(p, max + 1)) == 0
assert long(cast(p, -1)) == max
+ assert int(cast(p, b'\xFE')) == 254
+ assert int(cast(p, u'\xFE')) == 254
def test_no_float_on_int_types():
p = new_primitive_type('long')
@@ -122,7 +126,8 @@ def test_float_types():
assert cast(p, -1.1) != cast(p, -1.1)
assert repr(float(cast(p, -0.0))) == '-0.0'
- assert float(cast(p, '\x09')) == 9.0
+ assert float(cast(p, b'\x09')) == 9.0
+ assert float(cast(p, u'\x09')) == 9.0
assert float(cast(p, True)) == 1.0
py.test.raises(TypeError, cast, p, None)