Dealing with errors in interactive subprocess running python interpreter that freeze the process

2018-08-01 Thread cseberino
I can run python3 interactively in a subprocess w/ Popen but
if I sent it text, that throws an exception, the process freezes
instead of just printing the exception like the normal interpreter..
why? how fix?  Here is my code below.

(I suspect when there is an exception, there is NO output to stdin so that
the problem is the line below that tries to read from stdin never finishes.
Maybe I need a different readline that can "survive" when there is no output 
and won't block?)



import subprocess
 
interpreter = subprocess.Popen(['python3', '-i'],
   stdin  = subprocess.PIPE,
   stdout = subprocess.PIPE,
   stderr = subprocess.PIPE)
 
while True:
exp = input(">>> ").encode() + b"\n"
interpreter.stdin.write(exp)
interpreter.stdin.flush()
print(interpreter.stdout.readline().strip())
interpreter.stdin.close()
interpreter.terminate()
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Dealing with errors in interactive subprocess running python interpreter that freeze the process

2018-08-02 Thread cseberino


> As others have mentioned, separate threads for the individual pipes
> may help, or if you need to go that far there are specialised
> libraries, I believe (pexpect is one, but from what I know it's fairly
> Unix-specific, so I'm not very familiar with it).

I'm on Linux so pexpect is a possibility.

> Sorry, but there's no "simple" answer here for you (although you may
> well be able to get something that works well enough for your specific
> needs - but it's not obvious from your snippet of code what you're
> trying to achieve).

To send and receive text from a subprocess..even when there are exceptions.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Dealing with errors in interactive subprocess running python interpreter that freeze the process

2018-08-02 Thread cseberino


> -I think the Python interpreter actually sends its output to stderr, so to 
> capture it you'd probably want it to go to the same place as stdout, so use 
> stderr = subprocess.STDOUT

Yes that captured the error messages!  Thanks! 

> -You're only reading 1 line out output for each thing, so if 1 command 
> creates multiple lines of output then you won't be showing them all.

Yes.  That is the one remaining problem.  I tried replacing the readline with 
read and readlines and both froze/blocked.

cs
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Dealing with errors in interactive subprocess running python interpreter that freeze the process

2018-08-02 Thread cseberino


> subprocess is not meant for interaction through the pipes.  That is why, 
> I have been told, IDLE uses a socket for interaction.  Multiprocess is 
> apparently better suited for interaction without resorting to a socket.

So use normal socket on localhost for this?  Don't you still need subprocess
to launch the interpreter in a separate process?

cs
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Dealing with errors in interactive subprocess running python interpreter that freeze the process

2018-08-02 Thread cseberino


> Another possibility: If the ONLY thing you're doing with stdout/stderr
> is passing them through to the screen, simply don't change them. Let
> them remain bound to the console. You can have a pipe for stdin
> without also having pipes for the others. But that won't work if you
> intend to do any sort of parsing on the returned output.


I must parse.
-- 
https://mail.python.org/mailman/listinfo/python-list


Why this regex for string literals can't handle escaped quotes?.... '"(\\.|[^"])*"'

2018-08-09 Thread cseberino
Why this regex for string literals 
can't handle escaped quotes? '"(\\.|[^"])*"'

See this...

>>> string_re = '"(\\.|[^"])*"'

>>> re.match(string_re, '""')
<_sre.SRE_Match object; span=(0, 6), match='""'>

>>> re.match(string_re, '"aa\"aa"')
<_sre.SRE_Match object; span=(0, 4), match='"aa"'>

How make the last match be the entire string '"aa\"aa"' ?

cs
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why this regex for string literals can't handle escaped quotes?.... '"(\\.|[^"])*"'

2018-08-09 Thread cseberino
Wow.  Thanks.  That cleared everything up.

cs
-- 
https://mail.python.org/mailman/listinfo/python-list