Lines 1-68
Link Here
|
1 |
# Backport: PR #56: Actually check if the file exists rather than assume it doesn't |
|
|
2 |
# https://bitbucket.org/cffi/cffi/pull-request/56/ |
3 |
|
4 |
--- cffi/verifier.py.orig 2015-02-11 08:57:05 UTC |
5 |
+++ cffi/verifier.py |
6 |
@@ -1,7 +1,16 @@ |
7 |
-import sys, os, binascii, imp, shutil |
8 |
+import sys, os, binascii, imp, shutil, io |
9 |
from . import __version__ |
10 |
from . import ffiplatform |
11 |
|
12 |
+if sys.version_info >= (3,): |
13 |
+ NativeIO = io.StringIO |
14 |
+else: |
15 |
+ class NativeIO(io.BytesIO): |
16 |
+ def write(self, s): |
17 |
+ if isinstance(s, unicode): |
18 |
+ s = s.encode('ascii') |
19 |
+ super(NativeIO, self).write(s) |
20 |
+ |
21 |
|
22 |
class Verifier(object): |
23 |
|
24 |
@@ -118,19 +127,36 @@ class Verifier(object): |
25 |
self._vengine.collect_types() |
26 |
self._has_module = True |
27 |
|
28 |
- def _write_source(self, file=None): |
29 |
- must_close = (file is None) |
30 |
- if must_close: |
31 |
- _ensure_dir(self.sourcefilename) |
32 |
- file = open(self.sourcefilename, 'w') |
33 |
+ def _write_source_to(self, file): |
34 |
self._vengine._f = file |
35 |
try: |
36 |
self._vengine.write_source_to_f() |
37 |
finally: |
38 |
del self._vengine._f |
39 |
- if must_close: |
40 |
- file.close() |
41 |
- if must_close: |
42 |
+ |
43 |
+ def _write_source(self, file=None): |
44 |
+ if file is not None: |
45 |
+ self._write_source_to(file) |
46 |
+ else: |
47 |
+ # Write our source file to an in memory file. |
48 |
+ f = NativeIO() |
49 |
+ self._write_source_to(f) |
50 |
+ source_data = f.getvalue() |
51 |
+ |
52 |
+ # Determine if this matches the current file |
53 |
+ if os.path.exists(self.sourcefilename): |
54 |
+ with open(self.sourcefilename, "r") as fp: |
55 |
+ needs_written = not (fp.read() == source_data) |
56 |
+ else: |
57 |
+ needs_written = True |
58 |
+ |
59 |
+ # Actually write the file out if it doesn't match |
60 |
+ if needs_written: |
61 |
+ _ensure_dir(self.sourcefilename) |
62 |
+ with open(self.sourcefilename, "w") as fp: |
63 |
+ fp.write(source_data) |
64 |
+ |
65 |
+ # Set this flag |
66 |
self._has_source = True |
67 |
|
68 |
def _compile_module(self): |