Skip to content

Commit

Permalink
Merge branch 'fix-versioning'
Browse files Browse the repository at this point in the history
  • Loading branch information
antocuni committed Jun 14, 2017
2 parents 9d020e0 + 098b9ff commit b21d4c4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions capnpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import sys
import pkg_resources
from capnpy.compiler.compiler import DynamicCompiler
from capnpy.compiler.distutils import capnpify
from capnpy.message import load, loads, load_all, dumps, dump

try:
__version__ = pkg_resources.get_distribution('capnpy').version
except Exception:
__version__ = 'unknown'

_compiler = DynamicCompiler(sys.path)
load_schema = _compiler.load_schema
parse_schema = _compiler.parse_schema
4 changes: 4 additions & 0 deletions capnpy/compiler/request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import py
from datetime import datetime
import capnpy
from capnpy import schema
from capnpy.type import Types

Expand Down Expand Up @@ -63,6 +64,7 @@ def emit(self, m):
m.w("from capnpy.util import float32_repr as _float32_repr")
m.w("from capnpy.util import float64_repr as _float64_repr")
m.w("from capnpy.util import extend_module_maybe as _extend_module_maybe")
m.w("from capnpy.util import check_version as _check_version")
#
if m.pyx:
m.w("from capnpy cimport _hash")
Expand All @@ -74,6 +76,8 @@ def emit(self, m):
# _compile_pyx for a detailed explanation
m.w('from %s import __compiler, __schema__' % m.tmpname)
#
m.w('__capnpy_version__ = {version!r}', version=capnpy.__version__)
m.w('_check_version(__capnpy_version__)')
self._declare_imports(m)
m.w("")
#
Expand Down
31 changes: 31 additions & 0 deletions capnpy/testing/compiler/test_standalone.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import py
import sys
import capnpy
from capnpy.testing.compiler.support import CompilerTest
from capnpy.compiler.compiler import StandaloneCompiler

Expand Down Expand Up @@ -126,3 +127,33 @@ def test_pickle_list(self):
#
for proto in (0, pickle.HIGHEST_PROTOCOL):
py.test.raises(TypeError, "pickle.dumps(f.ints, proto)")

def test_version(self, monkeypatch):
monkeypatch.setattr(capnpy, '__version__', 'fake 1.0')
self.compile("example.capnp", """
@0xbf5147cbbecf40c1;
struct Point {
x @0: Int64;
y @1: Int64;
}
""")
example = self.import_('example')
assert example.__capnpy_version__ == 'fake 1.0'

def test_version_check(self, monkeypatch):
monkeypatch.setattr(capnpy, '__version__', 'Fake 1.0')
self.compile("example.capnp", """
@0xbf5147cbbecf40c1;
struct Point {
x @0: Int64;
y @1: Int64;
}
""")
monkeypatch.setattr(capnpy, '__version__', 'Fake 2.0')
exc = py.test.raises(ImportError, "self.import_('example')")
expected = ('Version mismatch: the module has been compiled with capnpy '
'Fake 1.0, but the current version of capnpy is Fake 2.0. '
'Please recompile.')
assert str(exc.value) == expected


8 changes: 8 additions & 0 deletions capnpy/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import py
import imp
import capnpy
try:
from capnpy._util import setattr_builtin
except ImportError:
Expand Down Expand Up @@ -62,6 +63,13 @@ def extend_module_maybe(globals, filename=None, modname=None):
code = compile(src, str(extmod), 'exec')
exec code in globals

def check_version(version):
if version != capnpy.__version__:
msg = ('Version mismatch: the module has been compiled with capnpy '
'{v1}, but the current version of capnpy is {v2}. '
'Please recompile.').format(v1=version, v2=capnpy.__version__)
raise ImportError(msg)

def text_repr(s):
# abuse the python string repr algo: make sure that the string contains at
# least one single quote and one double quote (which we will remove
Expand Down

0 comments on commit b21d4c4

Please sign in to comment.