#47 Wrong number of parameters passed to parseaction

open
nobody
None
5
2011-11-12
2011-11-12
No

I just lost a few hours searching for the reason of a weird error message I got after doing a couple of modifications to some of my parsing code. The following example code shows the problem:

from pyparsing import *

def buggyFunction(tokens):
1 + [] # type error

pattern = Word(alphas).setParseAction(buggyFunction)

pattern.parseString("hallo")

This yields: TypeError: buggyFunction() takes exactly 1 argument (0 given)
It should yield a Type error in the function! Especially for me as a beginner with pyparsing this is extremely confusing, since I was wondering if I did anything wrong with my grammar or whatever. It gets worse when the type error only happens in certain situations in the code (like an if statement). Then the code seems to work fine in some cases, but in other cases just no tokens will be passed.

The reason is the probably the _trim_arity function in pyparsing, which will actually use trial and error to find the parameter count of the function. Due to the unhandled TypeError exception in the parse callback, it will continue calling the function with the wrong parameter count. This should be easily rectified by constructing a wrapper using for example the inspection module which will give the correct parameter count.

Discussion

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks