mirror of
https://github.com/GaloisInc/cryptol.git
synced 2024-12-17 13:01:31 +03:00
ab39a74c98
The python side of the notebook needed to be updated to expect a .cabal-sandbox rather than cabal-dev sandbox. Also added a -fnotebook flag to the cabal file to build the Haskell side of it.
91 lines
2.3 KiB
Python
91 lines
2.3 KiB
Python
# -*- coding utf-8 -*-
|
|
"""
|
|
Module for controlling a Cryptol session from python.
|
|
"""
|
|
|
|
import pexpect
|
|
|
|
def interruptable(method):
|
|
"""
|
|
Decorator for methods that can be sent a KeyboardInterrupt
|
|
exception; passes a Ctrl-C to the underlying cryptol process
|
|
"""
|
|
def inner(self, *args):
|
|
result = None
|
|
try:
|
|
result = method(self, *args)
|
|
except KeyboardInterrupt:
|
|
self.cryptol.sendcontrol('c')
|
|
raise Cryptol2Interrupt()
|
|
return result
|
|
return inner
|
|
|
|
|
|
class Cryptol2Py:
|
|
|
|
def __init__(self):
|
|
self.cryptol = setup_cryptol_process()
|
|
self.cryptol.expect("<READY>")
|
|
|
|
def stop(self):
|
|
self.cryptol.sendcontrol('c')
|
|
|
|
@interruptable
|
|
def runInteractive(self, code):
|
|
out = []
|
|
for line in code.splitlines():
|
|
self.cryptol.sendline(line)
|
|
self.cryptol.expect("<DONE>", timeout=600)
|
|
out.append(self.cryptol.before)
|
|
return '\n'.join(out)
|
|
|
|
@interruptable
|
|
def addModuleFragment(self, code):
|
|
self.cryptol.sendline("<BEGINMOD>")
|
|
if code[-1] == '\n':
|
|
self.cryptol.send(code)
|
|
else:
|
|
self.cryptol.sendline(code)
|
|
self.cryptol.sendline("<ENDMOD>")
|
|
self.cryptol.expect("<DONE>")
|
|
return self.cryptol.before
|
|
|
|
@interruptable
|
|
def runAuto(self, code):
|
|
"""
|
|
Send a block of code to the cryptol process using the AUTO input
|
|
mode. This will attempt to automatically determine whether the
|
|
input is a module fragment or a sequence of interactive
|
|
commands.
|
|
"""
|
|
self.cryptol.sendline("<BEGINAUTO>")
|
|
if code[-1] == '\n':
|
|
self.cryptol.send(code)
|
|
else:
|
|
self.cryptol.sendline(code)
|
|
self.cryptol.sendline("<ENDAUTO>")
|
|
self.cryptol.expect("<DONE>", timeout=600)
|
|
return self.cryptol.before
|
|
|
|
def setup_cryptol_process():
|
|
"""
|
|
Static method that setups up an interactive cryptol process.
|
|
"""
|
|
cryptol = pexpect.spawn("../.cabal-sandbox/bin/cryptolnb")
|
|
cryptol.setecho(False)
|
|
return cryptol
|
|
|
|
class Cryptol2PyError:
|
|
pass
|
|
|
|
class Cryptol2Interrupt:
|
|
def _render_traceback_(self):
|
|
return ["<interrupted>"]
|
|
|
|
def test():
|
|
cry = Cryptol2Py()
|
|
return cry.runAuto("1+1")
|
|
|
|
if __name__ == "__main__":
|
|
print test()
|