Tim Coote - 2010-08-21

Hullo
I'm using 1.5.0, so it's possible that the world has changed.
I'm parsing some email logs and wanted to use a common chunk of grammar for picking out the email addresses. However, when I used a naive piece of code, I found that I seem to have singleton instances of whatever the parse grammar class is called:

from pyparsing import Word, Literal, alphas, Group
add  = Word (alphas)
def fr ():
    print "from"
def to ():
    print "to"
msg =Group (Literal ("from=") + add("from").setParseAction (fr) + Literal ("to=") + add.setParseAction (to))
testdata = """from=a to=b"""
print msg.parseString (testdata).dump()

If I name the node for the 'from' address (as above), I get an output of:

from
to
[['from=', 'a', 'to=', 'b']]

However, if I don't name the node, the output looks like this:

to
to
[['from=', 'a', 'to=', 'b']]

Looking at pyparsing.py, I think that the implicit call to setResultsName causes the ParseResult (?) instance to be copied, so that the second call to setParseAction doesn't overwrite the first method call.  I *think* that all instances ought to be copies, otherwise this is quite an easy error to make, and non-trivial to pick out. Has the behaviour changed and am I right in thinking that the 'named' behaviour ought to happen for non-named nodes too?

feedback gratefully received.