From 1867938921d353c0be5ec272cfad276b76ccc779 Mon Sep 17 00:00:00 2001 From: Matthew Yacavone Date: Fri, 26 Nov 2021 13:57:18 -0800 Subject: [PATCH] add check to CryptolCode's __call__ --- cryptol-remote-api/python/cryptol/cryptoltypes.py | 8 +++++--- cryptol-remote-api/python/tests/cryptol/test_quoting.py | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cryptol-remote-api/python/cryptol/cryptoltypes.py b/cryptol-remote-api/python/cryptol/cryptoltypes.py index d2ef5145a..a5a7540b7 100644 --- a/cryptol-remote-api/python/cryptol/cryptoltypes.py +++ b/cryptol-remote-api/python/cryptol/cryptoltypes.py @@ -56,7 +56,10 @@ def __to_cryptol__(self, ty : CryptolType) -> Any: ... class CryptolCode(metaclass=ABCMeta): def __call__(self, *others : CryptolJSON) -> CryptolCode: - return CryptolApplication(self, *others) + if all(hasattr(other, '__to_cryptol__') for other in others): + return CryptolApplication(self, *others) + else: + raise ValueError("Argument to __call__ on CryptolCode is not CryptolJSON") @abstractmethod def __to_cryptol__(self, ty : CryptolType) -> Any: ... @@ -100,8 +103,7 @@ def __eq__(self, other : Any) -> bool: return isinstance(other, CryptolApplication) and self._rator == other._rator and self._rands == other._rands def __repr__(self) -> str: - args = [self._rator, *(arg for arg in self._rands)] - return f'CryptolApplication({", ".join(repr(x) for x in args)})' + return f'CryptolApplication({", ".join(repr(x) for x in [self._rator, *self._rands])})' class CryptolArrowKind: diff --git a/cryptol-remote-api/python/tests/cryptol/test_quoting.py b/cryptol-remote-api/python/tests/cryptol/test_quoting.py index 741220953..64508c1a1 100644 --- a/cryptol-remote-api/python/tests/cryptol/test_quoting.py +++ b/cryptol-remote-api/python/tests/cryptol/test_quoting.py @@ -33,11 +33,15 @@ def test_quoting(self): self.assertEqual(cry_f('id {5:#x}'), cry('id "0x5"')) self.assertEqual(cry_f('id {BV(4,5)}'), cry('id 0x5')) + # Only here to check backwards compatability, the above syntax is preferred y = cry('g')(cry_f('{x}')) z = cry('h')(cry_f('{y}')) self.assertEqual(str(z), str(cry('(h) ((g) (0x01))'))) self.assertEqual(cry_eval(z), [x,x]) self.assertEqual(str(cry('id')(cry_f('{BV(size=7, value=1)}'))), str(cry('(id) (1 : [7])'))) + # This is why this syntax is not preferred + with self.assertRaises(ValueError): + cry('g')(x) # x is not CryptolJSON if __name__ == "__main__":