Pathological regular expression

2009-04-09 Thread David Liang
Hi all,
I'm having a weird problem with a regular expression (tested in 2.6
and 3.0):

Basically, any of these:
_re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
_re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
_re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$')

followed by for example,
line = r'~/.[m]ozilla/firefox/*.default/chrome'
print(_re_comments.sub(r'\1', line))

...hangs the interpreter. For reference, if the first command had been
_re_comments = re.compile(r'^(([^z]+|\\.|"([^"\\]+|\\.)*")*)#.*$')

(off by one character z) it works fine, and all the equivalent
operations work in sed and awk. Am I missing something about Python
RE's?

-David
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pathological regular expression

2009-04-09 Thread David Liang
On Apr 9, 2:56 am, David Liang  wrote:
> Hi all,
> I'm having a weird problem with a regular expression (tested in 2.6
> and 3.0):
>
> Basically, any of these:
> _re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
> _re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
> _re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
>
> followed by for example,
> line = r'~/.[m]ozilla/firefox/*.default/chrome'
> print(_re_comments.sub(r'\1', line))
>
> ...hangs the interpreter. For reference, if the first command had been
> _re_comments = re.compile(r'^(([^z]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
>
> (off by one character z) it works fine, and all the equivalent
> operations work in sed and awk. Am I missing something about Python
> RE's?
>
> -David

The problem was the redundant +'s; the fixed RE is

_re_comments = re.compile(r'^(([^#"\\]|\\.|"([^"\\]|\\.)*")*)#.*')
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pathological regular expression

2009-04-09 Thread David Liang
On Apr 9, 2:56 am, David Liang  wrote:
> Hi all,
> I'm having a weird problem with a regular expression (tested in 2.6
> and 3.0):
>
> Basically, any of these:
> _re_comments = re.compile(r'^(([^\\]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
> _re_comments = re.compile(r'^(([^#]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
> _re_comments = re.compile(r'^(([^"]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
>
> followed by for example,
> line = r'~/.[m]ozilla/firefox/*.default/chrome'
> print(_re_comments.sub(r'\1', line))
>
> ...hangs the interpreter. For reference, if the first command had been
> _re_comments = re.compile(r'^(([^z]+|\\.|"([^"\\]+|\\.)*")*)#.*$')
>
> (off by one character z) it works fine, and all the equivalent
> operations work in sed and awk. Am I missing something about Python
> RE's?
>
> -David

The problem was the redundant +'s; the fixed RE is

_re_comments = re.compile(r'^(([^#"\\]|\\.|"([^"\\]|\\.)*")*)#.*')
--
http://mail.python.org/mailman/listinfo/python-list


Running an interactive subprocess with Popen

2009-04-09 Thread David Liang
Hello,
Sorry for the newbie question. How do I run a program that could
block, waiting for user input, using subprocess.Popen? For example,

from subprocess import *

def foo():
a = Popen(['python'] ...)

I want to be able to get input from the user and send input to the
subprocess, printing stdout and stderr as data becomes available, then
return once the subprocess exits. Is it possible to send to the
subprocess keyboard interrupts, EOF, and such?

I tried doing stdout=PIPE, stderr=PIPE, stdin=PIPE. I tried using
communicate(), but could only call it once; subsequent calls raised
"ValueError: I/O operation on closed file."
And both a.stdout.read() and a.stderr.read() blocked the program. Any
help would be much appreciated.

-David
--
http://mail.python.org/mailman/listinfo/python-list