summaryrefslogtreecommitdiff
path: root/cffi/verifier.py
diff options
context:
space:
mode:
authorDonald Stufft <donald@stufft.io>2015-01-29 12:09:10 -0500
committerDonald Stufft <donald@stufft.io>2015-01-29 12:09:10 -0500
commitbda9e04147c6e13d4455cba5c20ed4d43ed2b1b9 (patch)
tree41682c023e692e638ff4861afc374366d140df67 /cffi/verifier.py
parent85b4bcf0c4258607a8ab557b73af66f654733a3a (diff)
downloadcffi-bda9e04147c6e13d4455cba5c20ed4d43ed2b1b9.tar.gz
Actually test the file contents instead of the filename
Diffstat (limited to 'cffi/verifier.py')
-rw-r--r--cffi/verifier.py43
1 files changed, 32 insertions, 11 deletions
diff --git a/cffi/verifier.py b/cffi/verifier.py
index f07be22..b759b2e 100644
--- a/cffi/verifier.py
+++ b/cffi/verifier.py
@@ -1,4 +1,4 @@
-import sys, os, binascii, shutil
+import sys, os, binascii, shutil, io
from . import __version_verifier_modules__
from . import ffiplatform
@@ -13,6 +13,12 @@ else:
if type == imp.C_EXTENSION]
+if sys.version_info >= (3,):
+ NativeIO = io.StringIO
+else:
+ NativeIO = io.BytesIO
+
+
class Verifier(object):
def __init__(self, ffi, preamble, tmpdir=None, modulename=None,
@@ -48,12 +54,9 @@ class Verifier(object):
self.sourcefilename = os.path.join(self.tmpdir, modulename + source_extension)
self.modulefilename = os.path.join(self.tmpdir, modulename + suffix)
self.ext_package = ext_package
+ self._has_source = False
self._has_module = False
- @property
- def _has_source(self):
- return os.path.exists(self.sourcefilename)
-
def write_source(self, file=None):
"""Write the C source code. It is produced in 'self.sourcefilename',
which can be tweaked beforehand."""
@@ -148,17 +151,35 @@ class Verifier(object):
self._has_module = True
def _write_source(self, file=None):
- must_close = (file is None)
- if must_close:
- _ensure_dir(self.sourcefilename)
- file = open(self.sourcefilename, 'w')
- self._vengine._f = file
+ # Write our source file to an in memory file.
+ self._vengine._f = NativeIO()
try:
self._vengine.write_source_to_f()
finally:
+ source_data = self._vengine._f.getvalue()
del self._vengine._f
+
+ # Determine if this matches the current file
+ if file is None and os.path.exists(self.sourcefilename):
+ with open(self.sourcefilename, "r") as fp:
+ needs_written = not (fp.read() == source_data)
+ else:
+ needs_written = True
+
+ # Actually write the file out if it doesn't match
+ must_close = (file is None)
+ if needs_written:
if must_close:
- file.close()
+ _ensure_dir(self.sourcefilename)
+ file = open(self.sourcefilename, "w")
+ try:
+ file.write(source_data)
+ finally:
+ if must_close:
+ file.close()
+
+ if must_close:
+ self._has_source = True
def _compile_module(self):
# compile this C source