1

As a continuation from my previous question on building interactive option menus in pexpect, print statements within an interact input filter do not get sent to stdout until after the interact is complete.

The docs don't seem to contain an equivalent method to send_user from expect, is there any workaround to send output to the user, not to the spawned child from within pexpect's interact method?

bash-4.1$ cat testInputFilter.py
import pexpect

def input_filter(s):
    if s == b'\003':
        print('you pushed ctrl+c')
        return b'\r: r u going to kill me? press ctrl-d to exit!\r'
    elif s == b'\004':
        print('you pushed ctrl+d')
        return b'\r: ok, bye; exit\r'
    else:
        return s

proc = pexpect.spawn('bash --norc')
proc.interact(input_filter=input_filter)
proc.expect(pexpect.EOF)
bash-4.1$ ~/python/python36/bin/python3.6 testInputFilter.py | tee inputTest.txt
bash-4.1$
bash-4.1$ : r u going to kill me? press ctrl-d to exit!
bash-4.1$
bash-4.1$ : ok, bye; exit
exit
you pushed ctrl+c
you pushed ctrl+d
bash-4.1$
pynexj
  • 19,215
  • 5
  • 38
  • 56
Chris
  • 119
  • 1
  • 10
  • 1
    What if you don't `| tee inputTest.txt`? –  Mar 02 '18 at 13:22
  • Well that does it, and I feel stupid, but I find it odd as shouldn't tee redirect to both stdout and to a file? Isn't that the whole purpose? I guess I'll just have to use the logger instead. – Chris Mar 02 '18 at 13:25

2 Answers2

2

Don't really know why print()'ed data is not automatically flushed when interacting with the spawned child but you can flush it explicitly so it would show up realtime:

print('something')
sys.stdout.flush()

or just use (only for python3)

# .raw is not buffered
sys.stdout.buffer.raw.write('something')
pynexj
  • 19,215
  • 5
  • 38
  • 56
0

I found that printing to the screen gave a weird offset based on what had previously been printed out by the spawned process and that the spawned processes next output was also offset a weird amount. What I ended up doing was:

    sys.stdout.write(f'\n\rsomething\r\n')
    sys.stdout.flush()

This prints my something at the beginning of a new line, then starts the spawned processes output at the beginning of the next line.

shortcipher3
  • 1,292
  • 9
  • 22