Skip to content

Commit

Permalink
add check to CryptolCode's __call__
Browse files Browse the repository at this point in the history
  • Loading branch information
m-yac committed Nov 26, 2021
1 parent 36fac34 commit 1867938
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 5 additions & 3 deletions cryptol-remote-api/python/cryptol/cryptoltypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions cryptol-remote-api/python/tests/cryptol/test_quoting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down

0 comments on commit 1867938

Please sign in to comment.