[Patches] [ python-Patches-1607548 ] Optional Argument Syntax

SourceForge.net noreply at sourceforge.net
Wed Dec 20 23:13:18 CET 2006


Patches item #1607548, was opened at 2006-12-02 20:53
Message generated for change (Comment added) made by tonylownds
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1607548&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Core (C code)
Group: Python 3000
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Tony Lownds (tonylownds)
Assigned to: Nobody/Anonymous (nobody)
Summary: Optional Argument Syntax

Initial Comment:
This patch implements optional argument syntax for Python 3000. The patch still has issues; I am posting so that Collin Winters can add a link to the PEP.

The syntax implemented is roughly:

def f(arg:expr, (nested1:expr, nested2:expr)) -> expr:
  suite

The function object has a new attribute, func_annotations that maps from argument names to the result of the expression. The return annotation is stored with a key of 'return'.

Lambda's syntax doesn't support annotations.

This patch alters the MAKE_FUNCTION opcode. I have an implementation that built the func_annotations dictionary in bytecode as well but it was bigger and slower.


----------------------------------------------------------------------

>Comment By: Tony Lownds (tonylownds)
Date: 2006-12-20 22:13

Message:
Logged In: YES 
user_id=24100
Originator: YES

Changes:
1. Updated to apply cleanly
2. Fix to compile.c so that test_complex_args passes

Open implementation issues:
1. Neal's comments
2. test_scope fails
3. Output from Lib/compiler does not pass test_complex_args


File Added: opt_arg_ann.patch

----------------------------------------------------------------------

Comment By: Tony Lownds (tonylownds)
Date: 2006-12-20 18:04

Message:
Logged In: YES 
user_id=24100
Originator: YES

I'll work on code formatting and the error checking and other cleanup.
Open to other names than tname and
vname, I created those non-terminals in order to use the same code for
processing "def" and "lambda". Terminals 
are caps IIUC. 

I did add a test for the multi-paren situation. 2.5 had that bug too.

Re: no changes to ceval, I tried generating the func_annotations
dictionary using 
bytecodes. That doesn't change the ceval loop but was more code and was
slower. 
So there is a way to avoid ceval changes.

Re: deciding if lambda was going to require parens around the arguments,
I don't think there was any decision, and yes annotations would be easily
supportable.
Happy to change if there is support, it's backwards incompatible.

Re: return type syntax, I have only seen the -> syntax (vs a keyword 'as')
on Guido's blog.

Thanks for the comments!

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2006-12-20 09:25

Message:
Logged In: YES 
user_id=33168
Originator: NO

Nix this comment:  I would definitely prefer the annotations baked into
the code object so
there are no changes to ceval.  I see that Guido wants it the way it
currently is which makes sense for nested functions.  There should probably
be a test with nested functions even though it really shouldn't be
different.  The test will verify that.

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2006-12-20 08:38

Message:
Logged In: YES 
user_id=33168
Originator: NO

When regenerating the patch, can you also remove non-functional changes
such as removing unneeded parens and whitespace changes.  Also, please try
to keep the same formatting in the file wrt tabs and spaces and don't move
code around.  I know this is a pain and inconsistent.  I think I changed
ast.c to be all 4 space indents with spaces only.

In compiler_simple_arg(), don't you need to check if annotation is NULL
when returned from ast_for_expr?  Otherwise an undetected error would go
through, wouldn't it?

In compiler_complex_args(), don't you need to set the ast_error (or a
SystemError) if the switch isn't a tname, vname, or LPAR?  I don't like the
names tname and vname.  Also they seem inconsistent.  Aren't all the other
names all CAPS?

In hunk, @@ -602,51 +625,75 @@ remove the commented out code.  We
shouldn't use any // style comments either.
Can you improve the error msg for kwdefaults == NULL?  (Thanks for adding
it!)
Check annotation for NULL if returned from ast_for_expr?

BTW, the AST code in this area was tricky code which had some bugs.  Did
you test with adding extra parentheses and singleton tuples?

I'm not sure if Guido preferred syntax -> vs a keyword 'as' for the return
type.

In symtable.c remove the printfs.  They should probably be SystemErrors or
something.

I would definitely prefer the annotations baked into the code object so
there are no changes to ceval.

Did we decide if lambda was going to require parens around the arguments? 
If so, it could support annotations, right?  (No comment on the usefulness
of annotations for lambdas. :-)

In compiler_visit_argannotation, you should return the result from
PyList_Append and can remove the comment about checking for errors.  Also,
I believe the INCREF is not needed, it will be done by PyList_Append.
Same deal with returning result of compiler_visit_argannotations() (the
one with an s).

Need to check for PyList_New() returning NULL in
compiler_visit_annotations().
Lots more error checking needs to be added in this area.

Dammit, I really want to use Mondrian for these comments!  (Sorry Tony,
not your fault, I'm just having some bad memories at this point cause I
have to keep providing the references.)

This patch looks very complete in that it updates things like the compiler
package and the parsermodule.c.  Good job!  This is a great start.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2006-12-20 01:22

Message:
Logged In: YES 
user_id=6380
Originator: NO

Applying the patch fails, probably due to recent merge activities in the
p3yk branch. Can I inconvenience you with a request to regenerate the patch
from the branch head?

----------------------------------------------------------------------

Comment By: Jim Jewett (jimjjewett)
Date: 2006-12-11 17:29

Message:
Logged In: YES 
user_id=764593
Originator: NO

Could you rename it to "argument annotations"?  "optional argument" makes
me think of the current keyword arguments, that can be but don't have to be
passed.

-jJ


----------------------------------------------------------------------

Comment By: Tony Lownds (tonylownds)
Date: 2006-12-04 01:24

Message:
Logged In: YES 
user_id=24100
Originator: YES

This patch implements optional argument syntax for Python 3000. The patch
still has issues:
1. test_ast and test_scope fail.
2. Running the test suite after compiling the library with the compiler
package causes failures
3. no docs
4. C-code reference counts and error checking needs a review

The syntax implemented is roughly:

def f(arg:expr, (nested1:expr, nested2:expr)) -> expr:
suite

The function object has a new attribute, func_annotations that maps from
argument names to the result of the expression. The return annotation is
stored with a key of 'return'.

Lambda's syntax doesn't support annotations.

The ast format has changed for the builtin compiler and the compiler
package. A new token was added, '->' (called RARROW in token.h). token.py
lost ERRORTOKEN after re-generating, I don't know why. I added it back
manually.



----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1607548&group_id=5470


More information about the Patches mailing list