Commits

Armin Rigo  committed 012670e

Add 'relements' to enum ctypes.

  • Participants
  • Parent commits 8194696

Comments (0)

Files changed (2)

File c/_cffi_backend.c

     return nosuchattr("elements");
 }
 
+static PyObject *ctypeget_relements(CTypeDescrObject *ct, void *context)
+{
+    if (ct->ct_flags & CT_IS_ENUM) {
+        PyObject *res = PyTuple_GetItem(ct->ct_stuff, 0);
+        if (res) res = PyDict_Copy(res);
+        return res;
+    }
+    return nosuchattr("relements");
+}
+
 static PyGetSetDef ctypedescr_getsets[] = {
     {"kind", (getter)ctypeget_kind, NULL, "kind"},
     {"cname", (getter)ctypeget_cname, NULL, "C name"},
     {"ellipsis", (getter)ctypeget_ellipsis, NULL, "function has '...'"},
     {"abi", (getter)ctypeget_abi, NULL, "function ABI"},
     {"elements", (getter)ctypeget_elements, NULL, "enum elements"},
+    {"relements", (getter)ctypeget_relements, NULL, "enum elements, reverse"},
     {NULL}                        /* sentinel */
 };
 
     # 'elements' is not the real dict, but merely a copy
     BEnum.elements[2] = '??'
     assert BEnum.elements == {-20: 'ab', 0: 'def', 1: 'c'}
+    #
+    BEnum = new_enum_type("bar", ('ab', 'cd'), (5, 5))
+    assert BEnum.elements == {5: 'ab'}
+    assert BEnum.relements == {'ab': 5, 'cd': 5}
 
 def test_cast_to_enum():
     BEnum = new_enum_type("foo", ('def', 'c', 'ab'), (0, 1, -20))