[pypy-commit] creflect default: Enums
arigo
noreply at buildbot.pypy.org
Thu Dec 11 13:41:20 CET 2014
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r196:6009daa6e243
Date: 2014-12-11 12:41 +0000
http://bitbucket.org/cffi/creflect/changeset/6009daa6e243/
Log: Enums
diff --git a/zeffir/builder.c b/zeffir/builder.c
--- a/zeffir/builder.c
+++ b/zeffir/builder.c
@@ -742,6 +742,10 @@
_crx_type_t *inttype,
const char *enumvalues[], int nvalues)
{
+ PyObject *l_dict;
+ PyObject *d1, *d2;
+ int i;
+
assert(inttype->ct_flags & (CT_PRIMITIVE_SIGNED | CT_PRIMITIVE_UNSIGNED));
assert(ct->ct_flags & CT_IS_ENUM);
if (ct->ct_size >= 0) {
@@ -750,10 +754,39 @@
return;
}
- //...
+ d1 = PyDict_New();
+ if (d1 == NULL)
+ goto error;
+
+ d2 = PyDict_New();
+ if (d2 == NULL)
+ goto error;
+
+ l_dict = ((zeffir_builder_t *)cb)->l_dict;
+
+ for (i = 0; i < nvalues; i++) {
+ PyObject *x, *n = PyString_FromString(enumvalues[i]);
+ if (n == NULL)
+ goto error;
+
+ x = PyDict_GetItem(l_dict, n);
+ if (x == NULL || !(PyInt_Check(x) || PyLong_Check(x)))
+ continue;
+
+ if (PyDict_SetItem(d2, n, x) < 0 || PyDict_SetItem(d1, x, n) < 0)
+ goto error;
+ }
+
+ ct->ct_stuff = PyTuple_Pack(2, d1, d2);
+ if (ct->ct_stuff == NULL)
+ goto error;
ct->ct_flags = inttype->ct_flags | CT_IS_ENUM;
ct->ct_size = inttype->ct_size;
+
+ error:
+ Py_XDECREF(d1);
+ Py_XDECREF(d2);
}
static void zef_define_type(_crx_builder_t *cb, const char *name,
@@ -812,7 +845,7 @@
assert(ct->ct_flags & CT_PRIMITIVE_ANY);
- PyObject *x = PyInt_FromLong(value->as_int);
+ PyObject *x = convert_to_object((char *)value, ct);
if (x == NULL)
return;
diff --git a/zeffir/test/const.crx b/zeffir/test/const.crx
new file mode 100644
--- /dev/null
+++ b/zeffir/test/const.crx
@@ -0,0 +1,10 @@
+#define MY_LL_CONST -0x01234567890abcdefLL
+#define MY_DBL_CONST -1.23
+
+
+// CREFLECT: start
+
+#define MY_LL_CONST ...
+static const double MY_DBL_CONST;
+
+// CREFLECT: end
diff --git a/zeffir/test/test_const.py b/zeffir/test/test_const.py
new file mode 100644
--- /dev/null
+++ b/zeffir/test/test_const.py
@@ -0,0 +1,8 @@
+import py
+import support
+
+
+def test_constants():
+ ffi, lib = support.compile_and_open('const')
+ assert lib.MY_DBL_CONST == -1.23
+ assert lib.MY_LL_CONST == -0x01234567890abcdef
diff --git a/zeffir/test/test_enum.py b/zeffir/test/test_enum.py
--- a/zeffir/test/test_enum.py
+++ b/zeffir/test/test_enum.py
@@ -8,3 +8,9 @@
assert lib.CC == 2
n = int(ffi.cast('enum foo_e', -5))
assert n == 2 ** 32 - 5 # unsigned
+
+def test_elements():
+ ffi, lib = support.compile_and_open('enum')
+ foo_e = ffi.typeof("enum foo_e")
+ assert foo_e.elements == {'AA': 0, 'CC': 2}
+ assert foo_e.relements == {0: 'AA', 2: 'CC'}
More information about the pypy-commit
mailing list