[Python-checkins] python/dist/src/Python compile.c, 2.309, 2.310 graminit.c, 2.35, 2.36

anthonybaxter at users.sourceforge.net anthonybaxter at users.sourceforge.net
Mon Aug 2 08:10:13 CEST 2004


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6086/Python

Modified Files:
	compile.c graminit.c 
Log Message:
PEP-0318, @decorator-style. In Guido's words:
"@ seems the syntax that everybody can hate equally"
Implementation by Mark Russell, from SF #979728.


Index: compile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
retrieving revision 2.309
retrieving revision 2.310
diff -C2 -d -r2.309 -r2.310
*** compile.c	17 Jul 2004 21:46:24 -0000	2.309
--- compile.c	2 Aug 2004 06:09:55 -0000	2.310
***************
*** 1877,1880 ****
--- 1877,1881 ----
  }
  
+ 
  static void
  com_dictmaker(struct compiling *c, node *n)
***************
*** 3964,3969 ****
  	}
  	else {
! 		REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
! 		n = CHILD(n, 2);
  		REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
  		n = CHILD(n, 1);
--- 3965,3971 ----
  	}
  	else {
! 		REQ(n, funcdef);
! 		/* funcdef: [decorators] 'def' NAME parameters ':' suite */
! 		n = RCHILD(n, -3);
  		REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
  		n = CHILD(n, 1);
***************
*** 4010,4022 ****
  
  static void
  com_funcdef(struct compiling *c, node *n)
  {
  	PyObject *co;
! 	int ndefs;
! 	REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
  	ndefs = com_argdefs(c, n);
  	if (ndefs < 0)
  		return;
! 	symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
  			     n->n_lineno);
  	co = (PyObject *)icompile(n, c);
--- 4012,4098 ----
  
  static void
+ com_decorator_name(struct compiling *c, node *n)
+ {
+ 	/* dotted_name: NAME ('.' NAME)* */
+ 	
+ 	int i, nch;
+ 	node *varname;
+ 
+ 	REQ(n, dotted_name);
+ 	nch = NCH(n);
+ 	assert(nch >= 1 && nch % 2 == 1);
+ 
+ 	varname = CHILD(n, 0);
+ 	REQ(varname, NAME);
+ 	com_addop_varname(c, VAR_LOAD, STR(varname));
+ 		
+ 	for (i = 1; i < nch; i += 2) {
+ 		node *attrname;
+ 		
+ 		REQ(CHILD(n, i), DOT);
+ 
+ 		attrname = CHILD(n, i + 1);
+ 		REQ(attrname, NAME);
+ 		com_addop_name(c, LOAD_ATTR, STR(attrname));
+ 	}
+ }
+ 
+ static void
+ com_decorator(struct compiling *c, node *n)
+ {
+ 	/* decorator: '@' dotted_name [ '(' [arglist] ')' ] */
+ 	int nch = NCH(n);
+ 	assert(nch >= 2);
+ 	REQ(CHILD(n, 0), AT);
+ 	com_decorator_name(c, CHILD(n, 1));
+ 
+ 	if (nch > 2) {
+ 		assert(nch == 4 || nch == 5);
+ 		REQ(CHILD(n, 2), LPAR);
+ 		REQ(CHILD(n, nch - 1), RPAR);
+ 		com_call_function(c, CHILD(n, 3));
+ 	}
+ }
+ 
+ static int
+ com_decorators(struct compiling *c, node *n)
+ {
+ 	int i, nch, ndecorators;
+ 	
+ 	/* decorator ([NEWLINE] decorator)* NEWLINE */
+ 	nch = NCH(n);
+ 	assert(nch >= 2);
+ 	REQ(CHILD(n, nch - 1), NEWLINE);
+ 
+ 	ndecorators = 0;
+ 	for (i = NCH(n) - 1; i >= 0; --i) {
+ 		node *ch = CHILD(n, i);
+ 		if (TYPE(ch) != NEWLINE) {
+ 			com_decorator(c, ch);
+ 			++ndecorators;
+ 		}
+ 	}
+ 
+ 	return ndecorators;
+ }
+ 
+ static void
  com_funcdef(struct compiling *c, node *n)
  {
  	PyObject *co;
! 	int ndefs, ndecorators;
! 	REQ(n, funcdef);
! 	/*          -6            -5   -4   -3         -2  -1
! 	   funcdef: [decorators] 'def' NAME parameters ':' suite */
! 
! 	if (NCH(n) == 6)
! 		ndecorators = com_decorators(c, CHILD(n, 0));
! 	else
! 		ndecorators = 0;
! 	
  	ndefs = com_argdefs(c, n);
  	if (ndefs < 0)
  		return;
! 	symtable_enter_scope(c->c_symtable, STR(RCHILD(n, -4)), TYPE(n),
  			     n->n_lineno);
  	co = (PyObject *)icompile(n, c);
***************
*** 4034,4038 ****
  			com_addoparg(c, MAKE_FUNCTION, ndefs);
  		com_pop(c, ndefs);
! 		com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
  		com_pop(c, 1);
  		Py_DECREF(co);
--- 4110,4119 ----
  			com_addoparg(c, MAKE_FUNCTION, ndefs);
  		com_pop(c, ndefs);
! 		while (ndecorators > 0) {
! 			com_addoparg(c, CALL_FUNCTION, 1);
! 			com_pop(c, 1);
! 			ndecorators--;
! 		}
! 		com_addop_varname(c, VAR_STORE, STR(RCHILD(n, -4)));
  		com_pop(c, 1);
  		Py_DECREF(co);
***************
*** 4113,4117 ****
  	
  	/* Definition nodes */
! 	
  	case funcdef:
  		com_funcdef(c, n);
--- 4194,4198 ----
  	
  	/* Definition nodes */
! 
  	case funcdef:
  		com_funcdef(c, n);
***************
*** 4378,4384 ****
  	PyObject *doc;
  	node *ch;
! 	REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
! 	c->c_name = STR(CHILD(n, 1));
! 	doc = get_docstring(c, CHILD(n, 4));
  	if (doc != NULL) {
  		(void) com_addconst(c, doc);
--- 4459,4467 ----
  	PyObject *doc;
  	node *ch;
! 	REQ(n, funcdef);
! 	/*          -6            -5   -4   -3         -2  -1
! 	   funcdef: [decorators] 'def' NAME parameters ':' suite */
! 	c->c_name = STR(RCHILD(n, -4));
! 	doc = get_docstring(c, RCHILD(n, -1));
  	if (doc != NULL) {
  		(void) com_addconst(c, doc);
***************
*** 4387,4396 ****
  	else
  		(void) com_addconst(c, Py_None); /* No docstring */
! 	ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
  	ch = CHILD(ch, 1); /* ')' | varargslist */
  	if (TYPE(ch) == varargslist)
  		com_arglist(c, ch);
  	c->c_infunction = 1;
! 	com_node(c, CHILD(n, 4));
  	c->c_infunction = 0;
  	com_strip_lnotab(c);
--- 4470,4479 ----
  	else
  		(void) com_addconst(c, Py_None); /* No docstring */
! 	ch = RCHILD(n, -3); /* parameters: '(' [varargslist] ')' */
  	ch = CHILD(ch, 1); /* ')' | varargslist */
  	if (TYPE(ch) == varargslist)
  		com_arglist(c, ch);
  	c->c_infunction = 1;
! 	com_node(c, RCHILD(n, -1));
  	c->c_infunction = 0;
  	com_strip_lnotab(c);
***************
*** 5588,5594 ****
  	switch (TYPE(n)) {
  	case funcdef: {
! 		char *func_name = STR(CHILD(n, 1));
  		symtable_add_def(st, func_name, DEF_LOCAL);
! 		symtable_default_args(st, CHILD(n, 2));
  		symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
  		symtable_funcdef(st, n);
--- 5671,5680 ----
  	switch (TYPE(n)) {
  	case funcdef: {
! 		char *func_name;
! 		if (NCH(n) == 6)
! 			symtable_node(st, CHILD(n, 0));
! 		func_name = STR(RCHILD(n, -4));
  		symtable_add_def(st, func_name, DEF_LOCAL);
! 		symtable_default_args(st, RCHILD(n, -3));
  		symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
  		symtable_funcdef(st, n);
***************
*** 5735,5738 ****
--- 5821,5835 ----
  	   rather innocuous.  Each case must double-check TYPE(n).
  	*/
+ 	case decorator:
+ 		if (TYPE(n) == decorator) {
+ 			/* decorator: '@' dotted_name [ '(' [arglist] ')' ] */
+ 			node *name, *varname;
+ 			name = CHILD(n, 1);
+ 			REQ(name, dotted_name);
+ 			varname = CHILD(name, 0);
+ 			REQ(varname, NAME);
+ 			symtable_add_use(st, STR(varname));
+ 		}
+ 		/* fall through */
  	case argument:
  		if (TYPE(n) == argument && NCH(n) == 3) {
***************
*** 5788,5792 ****
  			symtable_params(st, CHILD(n, 1));
  	} else
! 		symtable_params(st, CHILD(n, 2));
  	body = CHILD(n, NCH(n) - 1);
  	symtable_node(st, body);
--- 5885,5889 ----
  			symtable_params(st, CHILD(n, 1));
  	} else
! 		symtable_params(st, RCHILD(n, -3));
  	body = CHILD(n, NCH(n) - 1);
  	symtable_node(st, body);

Index: graminit.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/graminit.c,v
retrieving revision 2.35
retrieving revision 2.36
diff -C2 -d -r2.35 -r2.36
*** graminit.c	19 May 2004 08:20:16 -0000	2.35
--- graminit.c	2 Aug 2004 06:10:10 -0000	2.36
***************
*** 50,58 ****
  	{12, 2},
  };
! static arc arcs_3_2[1] = {
  	{13, 3},
  };
! static arc arcs_3_3[1] = {
  	{14, 4},
  };
  static arc arcs_3_4[1] = {
--- 50,60 ----
[...3619 lines suppressed...]
! 	{326, 0},
  	{1, "lambda"},
  	{312, 0},
! 	{313, 0},
! 	{314, 0},
! 	{317, 0},
  	{1, "class"},
! 	{321, 0},
  	{322, 0},
! 	{324, 0},
  	{325, 0},
  	{327, 0},
+ 	{329, 0},
  };
  grammar _PyParser_Grammar = {
! 	74,
  	dfas,
! 	{156, labels},
  	256
  };



More information about the Python-checkins mailing list