From c789c8204d6b7411eec1276ec79616c3a9b0a1b4 Mon Sep 17 00:00:00 2001
From: Donald Stufft <donald@stufft.io>
Date: Sun, 6 Oct 2013 17:09:16 -0400
Subject: [PATCH] Patch cffi to fix CFFI issue #110 for real

---
 src/nacl/_cffi_fix.py | 72 +++++++++++++++++++++++++++++++++++++++++++
 src/nacl/nacl.py      |  3 ++
 2 files changed, 75 insertions(+)
 create mode 100644 src/nacl/_cffi_fix.py

diff --git a/src/nacl/_cffi_fix.py b/src/nacl/_cffi_fix.py
new file mode 100644
index 00000000..c6ecc486
--- /dev/null
+++ b/src/nacl/_cffi_fix.py
@@ -0,0 +1,72 @@
+# Copyright 2013 Donald Stufft and individual contributors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+import imp
+import os.path
+import sys
+
+import cffi.vengine_cpy
+import cffi.vengine_gen
+
+
+def _get_so_suffixes():
+    suffixes = []
+    for suffix, mode, type in imp.get_suffixes():
+        if type == imp.C_EXTENSION:
+            suffixes.append(suffix)
+
+    if not suffixes:
+        # bah, no C_EXTENSION available.  Occurs on pypy without cpyext
+        if sys.platform == 'win32':
+            suffixes = [".pyd"]
+        else:
+            suffixes = [".so"]
+
+    return suffixes
+
+
+def vengine_cpy_find_module(self, module_name, path, so_suffix):
+    # We will ignore so_suffix and get it ourselves
+    so_suffixes = _get_so_suffixes()
+
+    try:
+        f, filename, descr = imp.find_module(module_name, path)
+    except ImportError:
+        return None
+    if f is not None:
+        f.close()
+
+    # Note that after a setuptools installation, there are both .py
+    # and .so files with the same basename.  The code here relies on
+    # imp.find_module() locating the .so in priority.
+    if descr[0] not in so_suffixes:
+        return None
+    return filename
+
+
+def vengine_gen_find_module(self, module_name, path, so_suffixes):
+    # We will ignore so_suffix and get it ourselves
+    so_suffixes = _get_so_suffixes()
+
+    for so_suffix in so_suffixes:
+        basename = module_name + so_suffix
+        if path is None:
+            path = sys.path
+        for dirname in path:
+            filename = os.path.join(dirname, basename)
+            if os.path.isfile(filename):
+                return filename
+
+
+cffi.vengine_cpy.VCPythonEngine.find_module = vengine_cpy_find_module
+cffi.vengine_gen.VGenericEngine.find_module = vengine_gen_find_module
diff --git a/src/nacl/nacl.py b/src/nacl/nacl.py
index 265b6615..35ee3010 100644
--- a/src/nacl/nacl.py
+++ b/src/nacl/nacl.py
@@ -19,6 +19,9 @@ from __future__ import division
 
 import functools
 
+# We need to patch cffi before importing it
+from nacl import _cffi_fix
+
 import cffi.verifier
 
 from cffi import FFI
-- 
GitLab