Is it possible to make a custom REPL (for e.g. python) with two fifos?

Explanation of the problem

Suppose I have two fifos in and out. I have three terminals.

  1. In the first I want to run commands that output python code to in. For example echo "print(1+2)" > in echo "a = 3" > in echo "b = 4" > in // wait some time # usual comments break SO formatting echo "print(a*b)" > in
  2. The second just runs cat out. The result should be something like 3 // wait a little time <-- of course this is not real output 12
  3. The third should read in run the lines through python and print the result to out just like in a usual interactive python session.

Attempt at a solution / further remarks

  • Just running python in >out in the third terminal does not work. It terminates after the first line is written to in, the second terminal will only print the 3.
  • Writing a wrapper that tries to run lines from in through eval does not work, because I need to be able to parse lines like import sys, or the likes.
  • All in all, I really want the behaviour of the standard REPL of python, except that I do not want to be sitting at the keyboard and putting in lines, one by one. I want to be able to change the input and output channels.

Have you looked at the code module? You probably want to subclass code.InteractiveConsole and override its write method to write into your out FIFO, and have an outer loop that reads lines from the in FIFO and calls the push method with each line.

I haven't tested this but even if it doesn't work exactly like I've suggested your solution will no doubt nevertheless make use of the code module.

| improve this answer | |
  • Awesome, that will probably help me in finding a solution! Thanks for the pointer. – jmc Oct 3 '14 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.