[Pyrex] Trailing comma in def argument list not accepted in Pyrex (with patch)

Jim Kleckner jek-gmane at kleckner.net
Sun Jun 17 22:35:21 UTC 2007


Note that Python permits an argument list to end with an empty
argument but Pyrex does not.  The argument list is a tuple and
tuples are permitted to end with a trailing comma.  In fact, a
tuple of one element must have a comma to syntactically
differentiate parenthesized expressions from tuples.

Would it make sense to permit this in Pyrex and Sagex?

I have a ton of code with trailing commas in def argument
lists...  Part of the reason is that adding or deleting an
argument definition can be done by adding or deleting a whole
line by itself in Python.  With Pyrex, you have to remember to
edit the previous line to add a comma.  e.g.:
def foo(
     arg1=None,
     arg2=None,
     arg3=None,
     arg4=None,  # In Pyrex, this causes a missing argument error
):
     pass


================ Example:
def foo(arg1, arg2,):
   return arg1 + arg2

================ Pyrex 0.9.5.1a output
/cygdrive/c/Jim/dev/runsim/finsim/playpyrex/hello.pyx:2:19: Missing 
argument name

================ Sagex output

Error converting Pyrex file to C:
------------------------------------------------------------
...

def foo(arg1, arg2,):
                   ^
------------------------------------------------------------

/cygdrive/c/Jim/dev/runsim/finsim/playpyrex/hello.pyx:2:19: Missing 
argument name

================ Patch discussion:

[I now use the sagex version of Pyrex because of the increased
speed and better error reporting.]

After perusing the code, it seems to me that the following code
change to Parsing.py would effect the change.  But someone would
need to try it out with all the test cases and someone more
knowledgeable should assess whether this breaks something else.

In the code below, simply change the use of c_arg_list_trailers
to c_arg_list_terminators.  Since trailers was defined for a
reason, this is probably not the whole story, but just the place
for the surgeon to place the scalpel...

And yes, I know, the patch is not in standard form.
But it is just one line...

================ Patch candidate:
c_arg_list_terminators = ('*', '**', '.', ')')
c_arg_list_trailers = ('.', '*', '**')

def p_c_arg_list(s, in_pyfunc, cmethod_flag = 0):
     args = []
     if s.sy not in c_arg_list_terminators:
         args.append(p_c_arg_decl(s, in_pyfunc, cmethod_flag))
         while s.sy == ',':
             s.next()
# JEK: The use of c_arg_list_trailers fails to recognize ')' as end of 
arg list.
# JEK:      if s.sy in c_arg_list_trailers:
             if s.sy in c_arg_list_terminators:
                 break
             args.append(p_c_arg_decl(s, in_pyfunc))
     return args




More information about the Pyrex mailing list