Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r1199:4fb209f49094
Date: 2013-03-27 20:51 +0100
http://bitbucket.org/cffi/cffi/changeset/4fb209f49094/

Log:    Issue #67 Copy the enum's values on the library object returned by
        dlopen().

diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -358,6 +358,7 @@
         if path is None:
             raise OSError("library not found: %r" % (name,))
         backendlib = backend.load_library(path, flags)
+    copied_enums = []
     #
     def make_accessor(name):
         key = 'function ' + name
@@ -379,6 +380,17 @@
                 lambda self, value: write_variable(BType, name, value)))
             return
         #
+        if not copied_enums:
+            for key, tp in ffi._parser._declarations.iteritems():
+                if not key.startswith('enum '):
+                    continue
+                for enumname, enumval in zip(tp.enumerators, tp.enumvalues):
+                    if enumname not in library.__dict__:
+                        library.__dict__[enumname] = enumval
+            copied_enums.append(True)
+            if name in library.__dict__:
+                return
+        #
         raise AttributeError(name)
     #
     class FFILibrary(object):
diff --git a/testing/test_function.py b/testing/test_function.py
--- a/testing/test_function.py
+++ b/testing/test_function.py
@@ -333,3 +333,12 @@
         with open(filename, 'rb') as f:
             res = f.read()
         assert res == b'[hello from custom file][some more output]'
+
+    def test_constants_on_lib(self):
+        ffi = FFI(backend=self.Backend())
+        ffi.cdef("""enum foo_e { AA, BB, CC=5, DD };""")
+        lib = ffi.dlopen(None)
+        assert lib.AA == 0
+        assert lib.BB == 1
+        assert lib.CC == 5
+        assert lib.DD == 6
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to