summaryrefslogtreecommitdiff
path: root/cffi/verifier.py
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2012-08-13 19:07:45 +0200
committerArmin Rigo <arigo@tunes.org>2012-08-13 19:07:45 +0200
commit0d36b5bd4a78e0c6e0aa8d657d5131626e693329 (patch)
tree18ae63c82664a61a47e6dc247e44349e2983c3b6 /cffi/verifier.py
parentc740a695b7d08758a26807b4bad38d3e11e0e313 (diff)
downloadcffi-0d36b5bd4a78e0c6e0aa8d657d5131626e693329.tar.gz
Fix the logic: it was possible to reach the following bug (for which
I don't know how to write a small test): - in cffi: python setup.py install - in demo/bsdopendirtype: python setup.py install - rm -r demo/__pycache__ demo/build - in demo/bsdopendirtype: python setup.py install Then it would crash because it would not regenerate the C source but call the compiler anyway.
Diffstat (limited to 'cffi/verifier.py')
-rw-r--r--cffi/verifier.py37
1 files changed, 17 insertions, 20 deletions
diff --git a/cffi/verifier.py b/cffi/verifier.py
index 452598a..d8ad866 100644
--- a/cffi/verifier.py
+++ b/cffi/verifier.py
@@ -4,7 +4,6 @@ from . import ffiplatform
class Verifier(object):
- _status = '?'
def __init__(self, ffi, preamble, force_generic_engine=False, **kwds):
self.ffi = ffi
@@ -24,25 +23,24 @@ class Verifier(object):
suffix = _get_so_suffix()
self.sourcefilename = os.path.join(_TMPDIR, modulename + '.c')
self.modulefilename = os.path.join(_TMPDIR, modulename + suffix)
- self._status = 'init'
+ self._has_source = False
+ self._has_module = False
def write_source(self, file=None):
"""Write the C source code. It is produced in 'self.sourcefilename',
which can be tweaked beforehand."""
- if self._status == 'init':
- self._write_source(file)
- else:
+ if self._has_source and file is None:
raise ffiplatform.VerificationError("source code already written")
+ self._write_source(file)
def compile_module(self):
"""Write the C source code (if not done already) and compile it.
This produces a dynamic link library in 'self.modulefilename'."""
- if self._status == 'init':
- self._write_source()
- if self._status == 'source':
- self._compile_module()
- else:
+ if self._has_module:
raise ffiplatform.VerificationError("module already compiled")
+ if not self._has_source:
+ self._write_source()
+ self._compile_module()
def load_library(self):
"""Get a C module from this Verifier instance.
@@ -51,13 +49,10 @@ class Verifier(object):
operations to the C module. If necessary, the C code is written
and compiled first.
"""
- if self._status == 'init': # source code not written yet
+ if not self._has_module:
self._locate_module()
- if self._status == 'init':
- self._write_source()
- if self._status == 'source':
- self._compile_module()
- assert self._status == 'module'
+ if not self._has_module:
+ self.compile_module()
return self._load_library()
def get_module_name(self):
@@ -67,7 +62,7 @@ class Verifier(object):
return basename.split('.', 1)[0]
def get_extension(self):
- if self._status == 'init':
+ if not self._has_source:
self._write_source()
sourcename = self.sourcefilename
modname = self.get_module_name()
@@ -88,7 +83,7 @@ class Verifier(object):
f.close()
self.modulefilename = filename
self._vengine.collect_types()
- self._status = 'module'
+ self._has_module = True
def _write_source(self, file=None):
must_close = (file is None)
@@ -102,7 +97,8 @@ class Verifier(object):
del self._vengine._f
if must_close:
file.close()
- self._status = 'source'
+ if file is None:
+ self._has_source = True
def _compile_module(self):
# compile this C source
@@ -115,9 +111,10 @@ class Verifier(object):
if not same:
_ensure_dir(self.modulefilename)
shutil.move(outputfilename, self.modulefilename)
- self._status = 'module'
+ self._has_module = True
def _load_library(self):
+ assert self._has_module
return self._vengine.load_library()
# ____________________________________________________________