From 01e3201cab97d9af98a9c7090436ec05db56d4a2 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 12 Oct 2013 11:58:43 -0500 Subject: Load types from all cffi modules before declaring functions or macros This change loads all the types via cdef & then loops over the macros & functions and cdefs them. The advantage of this approach is that you can define the types in the right modules without worrying about import order. For example, if you need the BIO typedef in the asn1 module but it is defined in the bio module you can still import the modules alphabetically and expect that BIO will be properly declared. --- cryptography/bindings/openssl/api.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cryptography/bindings/openssl/api.py b/cryptography/bindings/openssl/api.py index 129605f3..f7e14c10 100644 --- a/cryptography/bindings/openssl/api.py +++ b/cryptography/bindings/openssl/api.py @@ -17,6 +17,8 @@ import sys import cffi +import six + from cryptography.primitives import interfaces @@ -41,16 +43,23 @@ class API(object): self.ffi = cffi.FFI() includes = [] functions = [] + macros = [] for name in self._modules: __import__("cryptography.bindings.openssl." + name) module = sys.modules["cryptography.bindings.openssl." + name] self.ffi.cdef(module.TYPES) - self.ffi.cdef(module.FUNCTIONS) - self.ffi.cdef(module.MACROS) + macros.append(module.MACROS) functions.append(module.FUNCTIONS) includes.append(module.INCLUDES) + # loop over the functions & macros after declaring all the types + # so we can set interdependent types in different files and still + # have them all defined before we parse the funcs & macros + for func, macro in six.moves.zip(functions, macros): + self.ffi.cdef(func) + self.ffi.cdef(macro) + # We include functions here so that if we got any of their definitions # wrong, the underlying C compiler will explode. In C you are allowed # to re-declare a function if it has the same signature. That is: -- cgit v1.2.3