1990-11-18 13:27:39 -04:00
|
|
|
/* Compile an expression node to intermediate code */
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
/* XXX TO DO:
|
1997-01-17 17:04:03 -04:00
|
|
|
XXX add __doc__ attribute == co_doc to code object attributes?
|
|
|
|
XXX (it's currently the first item of the co_const tuple)
|
1990-12-20 11:06:42 -04:00
|
|
|
XXX Generate simple jump for break/return outside 'try...finally'
|
2001-02-01 18:48:12 -04:00
|
|
|
XXX Allow 'continue' inside finally clause of try-finally
|
1997-01-17 17:04:03 -04:00
|
|
|
XXX New opcode for loading the initial index for a for loop
|
1995-07-18 11:51:37 -03:00
|
|
|
XXX other JAR tricks?
|
1990-12-20 11:06:42 -04:00
|
|
|
*/
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
#include "Python.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
#include "node.h"
|
|
|
|
#include "token.h"
|
|
|
|
#include "graminit.h"
|
|
|
|
#include "compile.h"
|
2001-02-02 14:19:15 -04:00
|
|
|
#include "symtable.h"
|
1990-11-18 13:27:39 -04:00
|
|
|
#include "opcode.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "structmember.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
1991-04-04 06:42:56 -04:00
|
|
|
|
1997-05-07 14:46:13 -03:00
|
|
|
/* Three symbols from graminit.h are also defined in Python.h, with
|
|
|
|
Py_ prefixes to their names. Python.h can't include graminit.h
|
|
|
|
(which defines too many confusing symbols), but we can check here
|
|
|
|
that they haven't changed (which is very unlikely, but possible). */
|
|
|
|
#if Py_single_input != single_input
|
1997-05-20 19:18:48 -03:00
|
|
|
#error "single_input has changed -- update Py_single_input in Python.h"
|
1997-05-07 14:46:13 -03:00
|
|
|
#endif
|
|
|
|
#if Py_file_input != file_input
|
1997-05-20 19:18:48 -03:00
|
|
|
#error "file_input has changed -- update Py_file_input in Python.h"
|
1997-05-07 14:46:13 -03:00
|
|
|
#endif
|
|
|
|
#if Py_eval_input != eval_input
|
1997-05-20 19:18:48 -03:00
|
|
|
#error "eval_input has changed -- update Py_eval_input in Python.h"
|
1997-05-07 14:46:13 -03:00
|
|
|
#endif
|
|
|
|
|
1997-03-03 15:13:14 -04:00
|
|
|
int Py_OptimizeFlag = 0;
|
|
|
|
|
1996-07-30 13:49:37 -03:00
|
|
|
#define OP_DELETE 0
|
|
|
|
#define OP_ASSIGN 1
|
|
|
|
#define OP_APPLY 2
|
|
|
|
|
2001-01-18 23:21:30 -04:00
|
|
|
#define VAR_LOAD 0
|
|
|
|
#define VAR_STORE 1
|
|
|
|
#define VAR_DELETE 2
|
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
#define DEL_CLOSURE_ERROR \
|
2001-01-29 21:24:43 -04:00
|
|
|
"can not delete variable '%.400s' referenced in nested scope"
|
|
|
|
|
|
|
|
#define DUPLICATE_ARGUMENT \
|
|
|
|
"duplicate argument '%s' in function definition"
|
|
|
|
|
2001-02-27 00:23:34 -04:00
|
|
|
#define GLOBAL_AFTER_ASSIGN \
|
|
|
|
"name '%.400s' is assigned to before global declaration"
|
|
|
|
|
|
|
|
#define GLOBAL_AFTER_USE \
|
|
|
|
"name '%.400s' is used prior to global declaration"
|
|
|
|
|
2003-10-18 19:05:25 -03:00
|
|
|
#define PARAM_GLOBAL \
|
2002-01-28 20:53:41 -04:00
|
|
|
"name '%.400s' is a function parameter and declared global"
|
2001-02-27 00:23:34 -04:00
|
|
|
|
2001-02-27 21:58:08 -04:00
|
|
|
#define LATE_FUTURE \
|
|
|
|
"from __future__ imports must occur at the beginning of the file"
|
|
|
|
|
2001-03-23 10:08:38 -04:00
|
|
|
#define ASSIGN_DEBUG \
|
|
|
|
"can not assign to __debug__"
|
|
|
|
|
2001-01-18 23:21:30 -04:00
|
|
|
#define MANGLE_LEN 256
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
#define OFF(x) offsetof(PyCodeObject, x)
|
1990-12-20 11:06:42 -04:00
|
|
|
|
2001-09-20 17:46:19 -03:00
|
|
|
static PyMemberDef code_memberlist[] = {
|
1995-07-18 11:51:37 -03:00
|
|
|
{"co_argcount", T_INT, OFF(co_argcount), READONLY},
|
|
|
|
{"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
|
1997-01-17 17:04:03 -04:00
|
|
|
{"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
|
1995-07-18 11:51:37 -03:00
|
|
|
{"co_flags", T_INT, OFF(co_flags), READONLY},
|
1992-01-11 22:30:05 -04:00
|
|
|
{"co_code", T_OBJECT, OFF(co_code), READONLY},
|
|
|
|
{"co_consts", T_OBJECT, OFF(co_consts), READONLY},
|
|
|
|
{"co_names", T_OBJECT, OFF(co_names), READONLY},
|
1995-07-18 11:51:37 -03:00
|
|
|
{"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
{"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
|
|
|
|
{"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
|
1992-01-11 22:30:05 -04:00
|
|
|
{"co_filename", T_OBJECT, OFF(co_filename), READONLY},
|
1993-03-29 06:43:31 -04:00
|
|
|
{"co_name", T_OBJECT, OFF(co_name), READONLY},
|
1997-01-23 23:43:35 -04:00
|
|
|
{"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
|
|
|
|
{"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
|
1990-12-20 11:06:42 -04:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2004-08-12 14:56:29 -03:00
|
|
|
/* Helper for code_new: return a shallow copy of a tuple that is
|
|
|
|
guaranteed to contain exact strings, by converting string subclasses
|
|
|
|
to exact strings and complaining if a non-string is found. */
|
|
|
|
static PyObject*
|
|
|
|
validate_and_copy_tuple(PyObject *tup)
|
|
|
|
{
|
|
|
|
PyObject *newtuple;
|
|
|
|
PyObject *item;
|
|
|
|
int i, len;
|
|
|
|
|
|
|
|
len = PyTuple_GET_SIZE(tup);
|
|
|
|
newtuple = PyTuple_New(len);
|
|
|
|
if (newtuple == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
item = PyTuple_GET_ITEM(tup, i);
|
|
|
|
if (PyString_CheckExact(item)) {
|
|
|
|
Py_INCREF(item);
|
|
|
|
}
|
|
|
|
else if (!PyString_Check(item)) {
|
|
|
|
PyErr_Format(
|
|
|
|
PyExc_TypeError,
|
|
|
|
"name tuples must contain only "
|
|
|
|
"strings, not '%.500s'",
|
|
|
|
item->ob_type->tp_name);
|
|
|
|
Py_DECREF(newtuple);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
item = PyString_FromStringAndSize(
|
|
|
|
PyString_AS_STRING(item),
|
|
|
|
PyString_GET_SIZE(item));
|
|
|
|
if (item == NULL) {
|
|
|
|
Py_DECREF(newtuple);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(newtuple, i, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newtuple;
|
|
|
|
}
|
|
|
|
|
2002-06-14 17:41:17 -03:00
|
|
|
PyDoc_STRVAR(code_doc,
|
|
|
|
"code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
|
|
|
|
varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
|
|
|
|
\n\
|
|
|
|
Create a code object. Not for the faint of heart.");
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
|
|
|
{
|
|
|
|
int argcount;
|
|
|
|
int nlocals;
|
|
|
|
int stacksize;
|
|
|
|
int flags;
|
2004-08-12 15:16:43 -03:00
|
|
|
PyObject *co = NULL;
|
2002-06-14 17:41:17 -03:00
|
|
|
PyObject *code;
|
|
|
|
PyObject *consts;
|
2004-08-12 14:56:29 -03:00
|
|
|
PyObject *names, *ournames = NULL;
|
|
|
|
PyObject *varnames, *ourvarnames = NULL;
|
|
|
|
PyObject *freevars = NULL, *ourfreevars = NULL;
|
|
|
|
PyObject *cellvars = NULL, *ourcellvars = NULL;
|
2002-06-14 17:41:17 -03:00
|
|
|
PyObject *filename;
|
|
|
|
PyObject *name;
|
|
|
|
int firstlineno;
|
|
|
|
PyObject *lnotab;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
|
|
|
|
&argcount, &nlocals, &stacksize, &flags,
|
|
|
|
&code,
|
|
|
|
&PyTuple_Type, &consts,
|
|
|
|
&PyTuple_Type, &names,
|
|
|
|
&PyTuple_Type, &varnames,
|
|
|
|
&filename, &name,
|
|
|
|
&firstlineno, &lnotab,
|
|
|
|
&PyTuple_Type, &freevars,
|
|
|
|
&PyTuple_Type, &cellvars))
|
|
|
|
return NULL;
|
|
|
|
|
2004-08-12 14:56:29 -03:00
|
|
|
if (argcount < 0) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_ValueError,
|
|
|
|
"code: argcount must not be negative");
|
|
|
|
goto cleanup;
|
2002-06-14 17:41:17 -03:00
|
|
|
}
|
|
|
|
|
2004-08-12 14:56:29 -03:00
|
|
|
if (nlocals < 0) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_ValueError,
|
|
|
|
"code: nlocals must not be negative");
|
|
|
|
goto cleanup;
|
2003-09-16 01:27:52 -03:00
|
|
|
}
|
2003-09-15 18:43:16 -03:00
|
|
|
|
2004-08-12 14:56:29 -03:00
|
|
|
ournames = validate_and_copy_tuple(names);
|
|
|
|
if (ournames == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
ourvarnames = validate_and_copy_tuple(varnames);
|
|
|
|
if (ourvarnames == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
if (freevars)
|
|
|
|
ourfreevars = validate_and_copy_tuple(freevars);
|
|
|
|
else
|
|
|
|
ourfreevars = PyTuple_New(0);
|
|
|
|
if (ourfreevars == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
if (cellvars)
|
|
|
|
ourcellvars = validate_and_copy_tuple(cellvars);
|
|
|
|
else
|
|
|
|
ourcellvars = PyTuple_New(0);
|
|
|
|
if (ourcellvars == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
|
2003-09-15 18:43:16 -03:00
|
|
|
co = (PyObject *) PyCode_New(argcount, nlocals, stacksize, flags,
|
2004-08-12 14:56:29 -03:00
|
|
|
code, consts, ournames, ourvarnames,
|
|
|
|
ourfreevars, ourcellvars, filename,
|
|
|
|
name, firstlineno, lnotab);
|
|
|
|
cleanup:
|
|
|
|
Py_XDECREF(ournames);
|
|
|
|
Py_XDECREF(ourvarnames);
|
|
|
|
Py_XDECREF(ourfreevars);
|
|
|
|
Py_XDECREF(ourcellvars);
|
2003-09-15 18:43:16 -03:00
|
|
|
return co;
|
2002-06-14 17:41:17 -03:00
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
code_dealloc(PyCodeObject *co)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(co->co_code);
|
|
|
|
Py_XDECREF(co->co_consts);
|
|
|
|
Py_XDECREF(co->co_names);
|
1997-07-25 17:13:49 -03:00
|
|
|
Py_XDECREF(co->co_varnames);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
Py_XDECREF(co->co_freevars);
|
|
|
|
Py_XDECREF(co->co_cellvars);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(co->co_filename);
|
|
|
|
Py_XDECREF(co->co_name);
|
1997-07-25 17:13:49 -03:00
|
|
|
Py_XDECREF(co->co_lnotab);
|
2000-05-03 20:44:39 -03:00
|
|
|
PyObject_DEL(co);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
code_repr(PyCodeObject *co)
|
1992-09-03 17:50:59 -03:00
|
|
|
{
|
|
|
|
char buf[500];
|
|
|
|
int lineno = -1;
|
|
|
|
char *filename = "???";
|
1993-03-29 06:43:31 -04:00
|
|
|
char *name = "???";
|
1998-10-07 16:42:25 -03:00
|
|
|
|
2000-04-06 22:21:36 -03:00
|
|
|
if (co->co_firstlineno != 0)
|
|
|
|
lineno = co->co_firstlineno;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (co->co_filename && PyString_Check(co->co_filename))
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
filename = PyString_AS_STRING(co->co_filename);
|
1997-04-29 17:08:16 -03:00
|
|
|
if (co->co_name && PyString_Check(co->co_name))
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
name = PyString_AS_STRING(co->co_name);
|
2001-11-28 17:10:39 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
|
|
"<code object %.100s at %p, file \"%.300s\", line %d>",
|
|
|
|
name, co, filename, lineno);
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyString_FromString(buf);
|
1992-09-03 17:50:59 -03:00
|
|
|
}
|
|
|
|
|
1993-11-05 06:20:10 -04:00
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
code_compare(PyCodeObject *co, PyCodeObject *cp)
|
1993-11-05 06:20:10 -04:00
|
|
|
{
|
|
|
|
int cmp;
|
2000-04-10 13:20:31 -03:00
|
|
|
cmp = PyObject_Compare(co->co_name, cp->co_name);
|
|
|
|
if (cmp) return cmp;
|
1997-08-29 14:12:43 -03:00
|
|
|
cmp = co->co_argcount - cp->co_argcount;
|
2002-10-03 06:50:47 -03:00
|
|
|
if (cmp) return (cmp<0)?-1:1;
|
1997-08-29 14:12:43 -03:00
|
|
|
cmp = co->co_nlocals - cp->co_nlocals;
|
2002-10-03 06:50:47 -03:00
|
|
|
if (cmp) return (cmp<0)?-1:1;
|
1997-08-29 14:12:43 -03:00
|
|
|
cmp = co->co_flags - cp->co_flags;
|
2002-10-03 06:50:47 -03:00
|
|
|
if (cmp) return (cmp<0)?-1:1;
|
2004-10-23 21:10:06 -03:00
|
|
|
cmp = co->co_firstlineno - cp->co_firstlineno;
|
|
|
|
if (cmp) return (cmp<0)?-1:1;
|
1998-10-07 16:42:25 -03:00
|
|
|
cmp = PyObject_Compare(co->co_code, cp->co_code);
|
1993-11-05 06:20:10 -04:00
|
|
|
if (cmp) return cmp;
|
1997-04-29 17:08:16 -03:00
|
|
|
cmp = PyObject_Compare(co->co_consts, cp->co_consts);
|
1993-11-05 06:20:10 -04:00
|
|
|
if (cmp) return cmp;
|
1997-04-29 17:08:16 -03:00
|
|
|
cmp = PyObject_Compare(co->co_names, cp->co_names);
|
1995-07-18 11:51:37 -03:00
|
|
|
if (cmp) return cmp;
|
1997-04-29 17:08:16 -03:00
|
|
|
cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (cmp) return cmp;
|
|
|
|
cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
|
|
|
|
if (cmp) return cmp;
|
|
|
|
cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
|
1993-11-05 06:20:10 -04:00
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
2000-07-22 15:47:25 -03:00
|
|
|
code_hash(PyCodeObject *co)
|
1993-11-05 06:20:10 -04:00
|
|
|
{
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
long h, h0, h1, h2, h3, h4, h5, h6;
|
2000-04-10 13:20:31 -03:00
|
|
|
h0 = PyObject_Hash(co->co_name);
|
|
|
|
if (h0 == -1) return -1;
|
1998-10-07 16:42:25 -03:00
|
|
|
h1 = PyObject_Hash(co->co_code);
|
1993-11-05 06:20:10 -04:00
|
|
|
if (h1 == -1) return -1;
|
1997-04-29 17:08:16 -03:00
|
|
|
h2 = PyObject_Hash(co->co_consts);
|
1993-11-05 06:20:10 -04:00
|
|
|
if (h2 == -1) return -1;
|
1997-04-29 17:08:16 -03:00
|
|
|
h3 = PyObject_Hash(co->co_names);
|
1993-11-05 06:20:10 -04:00
|
|
|
if (h3 == -1) return -1;
|
1997-04-29 17:08:16 -03:00
|
|
|
h4 = PyObject_Hash(co->co_varnames);
|
1995-07-18 11:51:37 -03:00
|
|
|
if (h4 == -1) return -1;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
h5 = PyObject_Hash(co->co_freevars);
|
|
|
|
if (h5 == -1) return -1;
|
|
|
|
h6 = PyObject_Hash(co->co_cellvars);
|
|
|
|
if (h6 == -1) return -1;
|
|
|
|
h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
|
1995-07-18 11:51:37 -03:00
|
|
|
co->co_argcount ^ co->co_nlocals ^ co->co_flags;
|
1993-11-05 06:20:10 -04:00
|
|
|
if (h == -1) h = -2;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2001-03-01 02:09:34 -04:00
|
|
|
/* XXX code objects need to participate in GC? */
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyTypeObject PyCode_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-11-18 13:27:39 -04:00
|
|
|
0,
|
|
|
|
"code",
|
1997-04-29 17:08:16 -03:00
|
|
|
sizeof(PyCodeObject),
|
1990-11-18 13:27:39 -04:00
|
|
|
0,
|
2001-09-14 17:08:07 -03:00
|
|
|
(destructor)code_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
(cmpfunc)code_compare, /* tp_compare */
|
|
|
|
(reprfunc)code_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
(hashfunc)code_hash, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
2002-06-14 17:41:17 -03:00
|
|
|
code_doc, /* tp_doc */
|
2001-09-14 17:08:07 -03:00
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
code_memberlist, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
2002-06-14 17:41:17 -03:00
|
|
|
code_new, /* tp_new */
|
1990-11-18 13:27:39 -04:00
|
|
|
};
|
|
|
|
|
1997-04-09 16:24:53 -03:00
|
|
|
#define NAME_CHARS \
|
|
|
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
2001-01-18 20:24:06 -04:00
|
|
|
/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
|
|
|
|
|
|
|
|
static int
|
|
|
|
all_name_chars(unsigned char *s)
|
|
|
|
{
|
2001-02-09 11:06:42 -04:00
|
|
|
static char ok_name_char[256];
|
|
|
|
static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
|
|
|
|
|
|
|
|
if (ok_name_char[*name_chars] == 0) {
|
|
|
|
unsigned char *p;
|
|
|
|
for (p = name_chars; *p; p++)
|
|
|
|
ok_name_char[*p] = 1;
|
|
|
|
}
|
|
|
|
while (*s) {
|
|
|
|
if (ok_name_char[*s++] == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2001-01-18 20:24:06 -04:00
|
|
|
}
|
|
|
|
|
2004-08-12 14:56:29 -03:00
|
|
|
static void
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
intern_strings(PyObject *tuple)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
|
|
|
|
PyObject *v = PyTuple_GET_ITEM(tuple, i);
|
2004-08-12 14:56:29 -03:00
|
|
|
if (v == NULL || !PyString_CheckExact(v)) {
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
Py_FatalError("non-string found in code slot");
|
|
|
|
}
|
|
|
|
PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-22 15:44:21 -03:00
|
|
|
/* Begin: Peephole optimizations ----------------------------------------- */
|
|
|
|
|
2003-03-25 21:07:54 -04:00
|
|
|
#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
|
|
|
|
#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
|
2003-03-28 08:05:00 -04:00
|
|
|
#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP)
|
|
|
|
#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
|
2003-03-25 21:07:54 -04:00
|
|
|
#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
|
2004-03-21 11:12:00 -04:00
|
|
|
#define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
|
|
|
|
#define ISBASICBLOCK(blocks, start, bytes) (blocks[start]==blocks[start+bytes-1])
|
|
|
|
|
2004-09-22 15:44:21 -03:00
|
|
|
/* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
|
|
|
|
with LOAD_CONST (c1, c2, ... cn).
|
|
|
|
The consts table must still be in list form so that the
|
|
|
|
new constant (c1, c2, ... cn) can be appended.
|
|
|
|
Called with codestr pointing to the first LOAD_CONST.
|
2005-02-07 15:32:38 -04:00
|
|
|
Bails out with no change if one or more of the LOAD_CONSTs is missing.
|
|
|
|
Also works for BUILD_LIST when followed by an "in" or "not in" test.
|
|
|
|
*/
|
2004-09-22 15:44:21 -03:00
|
|
|
static int
|
|
|
|
tuple_of_constants(unsigned char *codestr, int n, PyObject *consts)
|
|
|
|
{
|
|
|
|
PyObject *newconst, *constant;
|
|
|
|
int i, arg, len_consts;
|
|
|
|
|
|
|
|
/* Pre-conditions */
|
|
|
|
assert(PyList_CheckExact(consts));
|
2005-02-07 15:32:38 -04:00
|
|
|
assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
|
2004-09-22 15:44:21 -03:00
|
|
|
assert(GETARG(codestr, (n*3)) == n);
|
|
|
|
for (i=0 ; i<n ; i++)
|
2004-10-30 05:55:08 -03:00
|
|
|
assert(codestr[i*3] == LOAD_CONST);
|
2004-09-22 15:44:21 -03:00
|
|
|
|
|
|
|
/* Buildup new tuple of constants */
|
|
|
|
newconst = PyTuple_New(n);
|
|
|
|
if (newconst == NULL)
|
|
|
|
return 0;
|
2004-10-26 05:59:14 -03:00
|
|
|
len_consts = PyList_GET_SIZE(consts);
|
2004-09-22 15:44:21 -03:00
|
|
|
for (i=0 ; i<n ; i++) {
|
|
|
|
arg = GETARG(codestr, (i*3));
|
2004-10-26 05:59:14 -03:00
|
|
|
assert(arg < len_consts);
|
2004-09-22 15:44:21 -03:00
|
|
|
constant = PyList_GET_ITEM(consts, arg);
|
|
|
|
Py_INCREF(constant);
|
|
|
|
PyTuple_SET_ITEM(newconst, i, constant);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append folded constant onto consts */
|
|
|
|
if (PyList_Append(consts, newconst)) {
|
|
|
|
Py_DECREF(newconst);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Py_DECREF(newconst);
|
|
|
|
|
|
|
|
/* Write NOPs over old LOAD_CONSTS and
|
|
|
|
add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
|
|
|
|
memset(codestr, NOP, n*3);
|
|
|
|
codestr[n*3] = LOAD_CONST;
|
|
|
|
SETARG(codestr, (n*3), len_consts);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-01-02 02:17:33 -04:00
|
|
|
/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
|
|
|
|
with LOAD_CONST binop(c1,c2)
|
|
|
|
The consts table must still be in list form so that the
|
|
|
|
new constant can be appended.
|
|
|
|
Called with codestr pointing to the first LOAD_CONST.
|
2005-01-26 08:50:05 -04:00
|
|
|
Abandons the transformation if the folding fails (i.e. 1+'a').
|
|
|
|
If the new constant is a sequence, only folds when the size
|
|
|
|
is below a threshold value. That keeps pyc files from
|
|
|
|
becoming large in the presence of code like: (None,)*1000.
|
|
|
|
*/
|
2005-01-02 02:17:33 -04:00
|
|
|
static int
|
|
|
|
fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
|
|
|
|
{
|
|
|
|
PyObject *newconst, *v, *w;
|
2005-01-26 08:50:05 -04:00
|
|
|
int len_consts, opcode, size;
|
2005-01-02 02:17:33 -04:00
|
|
|
|
|
|
|
/* Pre-conditions */
|
|
|
|
assert(PyList_CheckExact(consts));
|
|
|
|
assert(codestr[0] == LOAD_CONST);
|
|
|
|
assert(codestr[3] == LOAD_CONST);
|
|
|
|
|
|
|
|
/* Create new constant */
|
|
|
|
v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
|
|
|
|
w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
|
|
|
|
opcode = codestr[6];
|
|
|
|
switch (opcode) {
|
|
|
|
case BINARY_POWER:
|
|
|
|
newconst = PyNumber_Power(v, w, Py_None);
|
|
|
|
break;
|
|
|
|
case BINARY_MULTIPLY:
|
|
|
|
newconst = PyNumber_Multiply(v, w);
|
|
|
|
break;
|
|
|
|
case BINARY_DIVIDE:
|
2005-01-26 08:50:05 -04:00
|
|
|
/* Cannot fold this operation statically since
|
|
|
|
the result can depend on the run-time presence of the -Qnew flag */
|
2005-01-07 14:10:51 -04:00
|
|
|
return 0;
|
2005-01-02 02:17:33 -04:00
|
|
|
case BINARY_TRUE_DIVIDE:
|
|
|
|
newconst = PyNumber_TrueDivide(v, w);
|
|
|
|
break;
|
|
|
|
case BINARY_FLOOR_DIVIDE:
|
|
|
|
newconst = PyNumber_FloorDivide(v, w);
|
|
|
|
break;
|
|
|
|
case BINARY_MODULO:
|
|
|
|
newconst = PyNumber_Remainder(v, w);
|
|
|
|
break;
|
|
|
|
case BINARY_ADD:
|
|
|
|
newconst = PyNumber_Add(v, w);
|
|
|
|
break;
|
|
|
|
case BINARY_SUBTRACT:
|
|
|
|
newconst = PyNumber_Subtract(v, w);
|
|
|
|
break;
|
|
|
|
case BINARY_SUBSCR:
|
|
|
|
newconst = PyObject_GetItem(v, w);
|
|
|
|
break;
|
|
|
|
case BINARY_LSHIFT:
|
|
|
|
newconst = PyNumber_Lshift(v, w);
|
|
|
|
break;
|
|
|
|
case BINARY_RSHIFT:
|
|
|
|
newconst = PyNumber_Rshift(v, w);
|
|
|
|
break;
|
|
|
|
case BINARY_AND:
|
|
|
|
newconst = PyNumber_And(v, w);
|
|
|
|
break;
|
|
|
|
case BINARY_XOR:
|
|
|
|
newconst = PyNumber_Xor(v, w);
|
|
|
|
break;
|
|
|
|
case BINARY_OR:
|
|
|
|
newconst = PyNumber_Or(v, w);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Called with an unknown opcode */
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (newconst == NULL) {
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
|
|
|
}
|
2005-01-26 08:50:05 -04:00
|
|
|
size = PyObject_Size(newconst);
|
|
|
|
if (size == -1)
|
|
|
|
PyErr_Clear();
|
|
|
|
else if (size > 20) {
|
|
|
|
Py_DECREF(newconst);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-01-02 02:17:33 -04:00
|
|
|
|
|
|
|
/* Append folded constant into consts table */
|
|
|
|
len_consts = PyList_GET_SIZE(consts);
|
|
|
|
if (PyList_Append(consts, newconst)) {
|
|
|
|
Py_DECREF(newconst);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Py_DECREF(newconst);
|
|
|
|
|
|
|
|
/* Write NOP NOP NOP NOP LOAD_CONST newconst */
|
|
|
|
memset(codestr, NOP, 4);
|
|
|
|
codestr[4] = LOAD_CONST;
|
|
|
|
SETARG(codestr, 4, len_consts);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-02-20 08:41:32 -04:00
|
|
|
static int
|
|
|
|
fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
|
|
|
|
{
|
2005-02-23 09:37:55 -04:00
|
|
|
PyObject *newconst=NULL, *v;
|
2005-02-20 08:41:32 -04:00
|
|
|
int len_consts, opcode;
|
|
|
|
|
|
|
|
/* Pre-conditions */
|
|
|
|
assert(PyList_CheckExact(consts));
|
|
|
|
assert(codestr[0] == LOAD_CONST);
|
|
|
|
|
|
|
|
/* Create new constant */
|
|
|
|
v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
|
|
|
|
opcode = codestr[3];
|
|
|
|
switch (opcode) {
|
|
|
|
case UNARY_NEGATIVE:
|
2005-02-23 09:37:55 -04:00
|
|
|
/* Preserve the sign of -0.0 */
|
|
|
|
if (PyObject_IsTrue(v) == 1)
|
|
|
|
newconst = PyNumber_Negative(v);
|
2005-02-20 08:41:32 -04:00
|
|
|
break;
|
|
|
|
case UNARY_CONVERT:
|
|
|
|
newconst = PyObject_Repr(v);
|
|
|
|
break;
|
|
|
|
case UNARY_INVERT:
|
|
|
|
newconst = PyNumber_Invert(v);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Called with an unknown opcode */
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (newconst == NULL) {
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append folded constant into consts table */
|
|
|
|
len_consts = PyList_GET_SIZE(consts);
|
|
|
|
if (PyList_Append(consts, newconst)) {
|
|
|
|
Py_DECREF(newconst);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Py_DECREF(newconst);
|
|
|
|
|
|
|
|
/* Write NOP LOAD_CONST newconst */
|
|
|
|
codestr[0] = NOP;
|
|
|
|
codestr[1] = LOAD_CONST;
|
|
|
|
SETARG(codestr, 1, len_consts);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-03-21 11:12:00 -04:00
|
|
|
static unsigned int *
|
|
|
|
markblocks(unsigned char *code, int len)
|
|
|
|
{
|
|
|
|
unsigned int *blocks = PyMem_Malloc(len*sizeof(int));
|
2004-10-30 05:55:08 -03:00
|
|
|
int i,j, opcode, blockcnt = 0;
|
2004-03-21 11:12:00 -04:00
|
|
|
|
|
|
|
if (blocks == NULL)
|
|
|
|
return NULL;
|
|
|
|
memset(blocks, 0, len*sizeof(int));
|
2004-10-30 05:55:08 -03:00
|
|
|
|
|
|
|
/* Mark labels in the first pass */
|
2004-03-21 11:12:00 -04:00
|
|
|
for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
|
|
|
|
opcode = code[i];
|
|
|
|
switch (opcode) {
|
|
|
|
case FOR_ITER:
|
|
|
|
case JUMP_FORWARD:
|
|
|
|
case JUMP_IF_FALSE:
|
|
|
|
case JUMP_IF_TRUE:
|
|
|
|
case JUMP_ABSOLUTE:
|
|
|
|
case CONTINUE_LOOP:
|
|
|
|
case SETUP_LOOP:
|
|
|
|
case SETUP_EXCEPT:
|
|
|
|
case SETUP_FINALLY:
|
|
|
|
j = GETJUMPTGT(code, i);
|
2004-10-30 05:55:08 -03:00
|
|
|
blocks[j] = 1;
|
2004-03-21 11:12:00 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-10-30 05:55:08 -03:00
|
|
|
/* Build block numbers in the second pass */
|
|
|
|
for (i=0 ; i<len ; i++) {
|
|
|
|
blockcnt += blocks[i]; /* increment blockcnt over labels */
|
|
|
|
blocks[i] = blockcnt;
|
|
|
|
}
|
2004-03-21 11:12:00 -04:00
|
|
|
return blocks;
|
|
|
|
}
|
2003-03-25 21:07:54 -04:00
|
|
|
|
2004-09-22 15:44:21 -03:00
|
|
|
/* Perform basic peephole optimizations to components of a code object.
|
|
|
|
The consts object should still be in list form to allow new constants
|
|
|
|
to be appended.
|
|
|
|
|
|
|
|
To keep the optimizer simple, it bails out (does nothing) for code
|
|
|
|
containing extended arguments or that has a length over 32,700. That
|
|
|
|
allows us to avoid overflow and sign issues. Likewise, it bails when
|
|
|
|
the lineno table has complex encoding for gaps >= 255.
|
|
|
|
|
|
|
|
Optimizations are restricted to simple transformations occuring within a
|
|
|
|
single basic block. All transformations keep the code size the same or
|
|
|
|
smaller. For those that reduce size, the gaps are initially filled with
|
|
|
|
NOPs. Later those NOPs are removed and the jump addresses retargeted in
|
|
|
|
a single pass. Line numbering is adjusted accordingly. */
|
|
|
|
|
2003-03-25 21:07:54 -04:00
|
|
|
static PyObject *
|
2004-08-23 20:37:48 -03:00
|
|
|
optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *lineno_obj)
|
2003-03-25 21:07:54 -04:00
|
|
|
{
|
2004-08-23 20:37:48 -03:00
|
|
|
int i, j, codelen, nops, h, adj;
|
2003-03-25 21:07:54 -04:00
|
|
|
int tgt, tgttgt, opcode;
|
2004-08-23 20:37:48 -03:00
|
|
|
unsigned char *codestr = NULL;
|
|
|
|
unsigned char *lineno;
|
|
|
|
int *addrmap = NULL;
|
|
|
|
int new_line, cum_orig_line, last_line, tabsiz;
|
2004-10-26 05:59:14 -03:00
|
|
|
int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONST codes */
|
2004-10-30 05:55:08 -03:00
|
|
|
unsigned int *blocks = NULL;
|
2004-07-16 09:16:48 -03:00
|
|
|
char *name;
|
2003-03-25 21:07:54 -04:00
|
|
|
|
2004-10-30 05:55:08 -03:00
|
|
|
/* Bail out if an exception is set */
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
goto exitUnchanged;
|
|
|
|
|
2004-08-25 14:19:38 -03:00
|
|
|
/* Bypass optimization when the lineno table is too complex */
|
|
|
|
assert(PyString_Check(lineno_obj));
|
|
|
|
lineno = PyString_AS_STRING(lineno_obj);
|
|
|
|
tabsiz = PyString_GET_SIZE(lineno_obj);
|
|
|
|
if (memchr(lineno, 255, tabsiz) != NULL)
|
|
|
|
goto exitUnchanged;
|
|
|
|
|
2004-08-24 01:34:16 -03:00
|
|
|
/* Avoid situations where jump retargeting could overflow */
|
2004-09-28 14:22:12 -03:00
|
|
|
assert(PyString_Check(code));
|
2003-03-25 21:07:54 -04:00
|
|
|
codelen = PyString_Size(code);
|
2004-09-22 15:44:21 -03:00
|
|
|
if (codelen > 32700)
|
2004-08-24 01:34:16 -03:00
|
|
|
goto exitUnchanged;
|
|
|
|
|
|
|
|
/* Make a modifiable copy of the code string */
|
2003-03-25 21:07:54 -04:00
|
|
|
codestr = PyMem_Malloc(codelen);
|
2004-03-21 11:12:00 -04:00
|
|
|
if (codestr == NULL)
|
2003-03-25 21:07:54 -04:00
|
|
|
goto exitUnchanged;
|
|
|
|
codestr = memcpy(codestr, PyString_AS_STRING(code), codelen);
|
2004-08-06 16:46:34 -03:00
|
|
|
|
2005-02-21 16:03:14 -04:00
|
|
|
/* Verify that RETURN_VALUE terminates the codestring. This allows
|
|
|
|
the various transformation patterns to look ahead several
|
|
|
|
instructions without additional checks to make sure they are not
|
|
|
|
looking beyond the end of the code string.
|
|
|
|
*/
|
|
|
|
if (codestr[codelen-1] != RETURN_VALUE)
|
|
|
|
goto exitUnchanged;
|
|
|
|
|
2004-08-23 20:37:48 -03:00
|
|
|
/* Mapping to new jump targets after NOPs are removed */
|
|
|
|
addrmap = PyMem_Malloc(codelen * sizeof(int));
|
|
|
|
if (addrmap == NULL)
|
|
|
|
goto exitUnchanged;
|
|
|
|
|
2004-03-21 11:12:00 -04:00
|
|
|
blocks = markblocks(codestr, codelen);
|
2004-08-23 20:37:48 -03:00
|
|
|
if (blocks == NULL)
|
2004-03-21 11:12:00 -04:00
|
|
|
goto exitUnchanged;
|
2004-09-22 15:44:21 -03:00
|
|
|
assert(PyList_Check(consts));
|
2003-03-25 21:07:54 -04:00
|
|
|
|
2004-11-01 11:19:11 -04:00
|
|
|
for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
|
2003-03-25 21:07:54 -04:00
|
|
|
opcode = codestr[i];
|
2004-10-26 05:59:14 -03:00
|
|
|
|
|
|
|
lastlc = cumlc;
|
|
|
|
cumlc = 0;
|
|
|
|
|
2003-03-25 21:07:54 -04:00
|
|
|
switch (opcode) {
|
|
|
|
|
2004-06-24 06:25:39 -03:00
|
|
|
/* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with
|
2004-08-23 20:37:48 -03:00
|
|
|
with JUMP_IF_TRUE POP_TOP */
|
2004-06-21 13:31:15 -03:00
|
|
|
case UNARY_NOT:
|
|
|
|
if (codestr[i+1] != JUMP_IF_FALSE ||
|
|
|
|
codestr[i+4] != POP_TOP ||
|
|
|
|
!ISBASICBLOCK(blocks,i,5))
|
|
|
|
continue;
|
|
|
|
tgt = GETJUMPTGT(codestr, (i+1));
|
|
|
|
if (codestr[tgt] != POP_TOP)
|
|
|
|
continue;
|
2004-06-24 06:25:39 -03:00
|
|
|
j = GETARG(codestr, i+1) + 1;
|
|
|
|
codestr[i] = JUMP_IF_TRUE;
|
|
|
|
SETARG(codestr, i, j);
|
|
|
|
codestr[i+3] = POP_TOP;
|
|
|
|
codestr[i+4] = NOP;
|
2004-06-21 13:31:15 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* not a is b --> a is not b
|
|
|
|
not a in b --> a not in b
|
|
|
|
not a is not b --> a is b
|
2005-02-09 21:42:32 -04:00
|
|
|
not a not in b --> a in b
|
2005-02-06 18:05:42 -04:00
|
|
|
*/
|
2004-06-21 13:31:15 -03:00
|
|
|
case COMPARE_OP:
|
|
|
|
j = GETARG(codestr, i);
|
|
|
|
if (j < 6 || j > 9 ||
|
|
|
|
codestr[i+3] != UNARY_NOT ||
|
|
|
|
!ISBASICBLOCK(blocks,i,4))
|
2004-08-23 20:37:48 -03:00
|
|
|
continue;
|
2004-06-21 13:31:15 -03:00
|
|
|
SETARG(codestr, i, (j^1));
|
|
|
|
codestr[i+3] = NOP;
|
2004-07-17 02:00:52 -03:00
|
|
|
break;
|
|
|
|
|
2004-07-16 09:16:48 -03:00
|
|
|
/* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
|
|
|
|
case LOAD_NAME:
|
|
|
|
case LOAD_GLOBAL:
|
|
|
|
j = GETARG(codestr, i);
|
|
|
|
name = PyString_AsString(PyTuple_GET_ITEM(names, j));
|
|
|
|
if (name == NULL || strcmp(name, "None") != 0)
|
|
|
|
continue;
|
2004-09-22 15:44:21 -03:00
|
|
|
for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
|
|
|
|
if (PyList_GET_ITEM(consts, j) == Py_None) {
|
2004-07-16 09:16:48 -03:00
|
|
|
codestr[i] = LOAD_CONST;
|
|
|
|
SETARG(codestr, i, j);
|
2004-11-02 00:20:10 -04:00
|
|
|
cumlc = lastlc + 1;
|
2004-07-16 09:16:48 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-06-21 13:31:15 -03:00
|
|
|
break;
|
|
|
|
|
2004-08-23 20:37:48 -03:00
|
|
|
/* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP */
|
2003-03-25 21:07:54 -04:00
|
|
|
case LOAD_CONST:
|
2004-10-26 05:59:14 -03:00
|
|
|
cumlc = lastlc + 1;
|
2003-03-25 21:07:54 -04:00
|
|
|
j = GETARG(codestr, i);
|
|
|
|
if (codestr[i+3] != JUMP_IF_FALSE ||
|
|
|
|
codestr[i+6] != POP_TOP ||
|
2004-09-22 15:44:21 -03:00
|
|
|
!ISBASICBLOCK(blocks,i,7) ||
|
|
|
|
!PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
|
2003-03-25 21:07:54 -04:00
|
|
|
continue;
|
2004-08-23 20:37:48 -03:00
|
|
|
memset(codestr+i, NOP, 7);
|
2004-11-02 00:20:10 -04:00
|
|
|
cumlc = 0;
|
2003-03-25 21:07:54 -04:00
|
|
|
break;
|
|
|
|
|
2005-02-07 15:32:38 -04:00
|
|
|
/* Try to fold tuples of constants (includes a case for lists
|
|
|
|
which are only used for "in" and "not in" tests).
|
2004-09-22 15:44:21 -03:00
|
|
|
Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
|
2004-08-23 20:37:48 -03:00
|
|
|
Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
|
|
|
|
Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
|
2004-03-21 11:12:00 -04:00
|
|
|
case BUILD_TUPLE:
|
2005-02-07 15:32:38 -04:00
|
|
|
case BUILD_LIST:
|
2004-09-22 15:44:21 -03:00
|
|
|
j = GETARG(codestr, i);
|
|
|
|
h = i - 3 * j;
|
|
|
|
if (h >= 0 &&
|
2004-10-30 05:55:08 -03:00
|
|
|
j <= lastlc &&
|
2005-03-03 03:01:48 -04:00
|
|
|
((opcode == BUILD_TUPLE &&
|
|
|
|
ISBASICBLOCK(blocks, h, 3*(j+1))) ||
|
|
|
|
(opcode == BUILD_LIST &&
|
2005-02-07 15:32:38 -04:00
|
|
|
codestr[i+3]==COMPARE_OP &&
|
|
|
|
ISBASICBLOCK(blocks, h, 3*(j+2)) &&
|
2005-03-03 03:01:48 -04:00
|
|
|
(GETARG(codestr,i+3)==6 ||
|
|
|
|
GETARG(codestr,i+3)==7))) &&
|
|
|
|
tuple_of_constants(&codestr[h], j, consts)) {
|
2004-11-02 00:20:10 -04:00
|
|
|
assert(codestr[i] == LOAD_CONST);
|
|
|
|
cumlc = 1;
|
2004-09-22 15:44:21 -03:00
|
|
|
break;
|
|
|
|
}
|
2004-08-23 20:37:48 -03:00
|
|
|
if (codestr[i+3] != UNPACK_SEQUENCE ||
|
|
|
|
!ISBASICBLOCK(blocks,i,6) ||
|
|
|
|
j != GETARG(codestr, i+3))
|
2004-03-21 11:12:00 -04:00
|
|
|
continue;
|
2004-08-23 20:37:48 -03:00
|
|
|
if (j == 1) {
|
|
|
|
memset(codestr+i, NOP, 6);
|
|
|
|
} else if (j == 2) {
|
2004-03-21 11:12:00 -04:00
|
|
|
codestr[i] = ROT_TWO;
|
2004-08-23 20:37:48 -03:00
|
|
|
memset(codestr+i+1, NOP, 5);
|
|
|
|
} else if (j == 3) {
|
2004-03-21 11:12:00 -04:00
|
|
|
codestr[i] = ROT_THREE;
|
|
|
|
codestr[i+1] = ROT_TWO;
|
2004-08-23 20:37:48 -03:00
|
|
|
memset(codestr+i+2, NOP, 4);
|
2004-03-21 11:12:00 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-01-02 02:17:33 -04:00
|
|
|
/* Fold binary ops on constants.
|
|
|
|
LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
|
|
|
|
case BINARY_POWER:
|
|
|
|
case BINARY_MULTIPLY:
|
|
|
|
case BINARY_TRUE_DIVIDE:
|
|
|
|
case BINARY_FLOOR_DIVIDE:
|
|
|
|
case BINARY_MODULO:
|
|
|
|
case BINARY_ADD:
|
|
|
|
case BINARY_SUBTRACT:
|
|
|
|
case BINARY_SUBSCR:
|
|
|
|
case BINARY_LSHIFT:
|
|
|
|
case BINARY_RSHIFT:
|
|
|
|
case BINARY_AND:
|
|
|
|
case BINARY_XOR:
|
|
|
|
case BINARY_OR:
|
|
|
|
if (lastlc >= 2 &&
|
|
|
|
ISBASICBLOCK(blocks, i-6, 7) &&
|
|
|
|
fold_binops_on_constants(&codestr[i-6], consts)) {
|
|
|
|
i -= 2;
|
|
|
|
assert(codestr[i] == LOAD_CONST);
|
|
|
|
cumlc = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-02-20 08:41:32 -04:00
|
|
|
/* Fold unary ops on constants.
|
|
|
|
LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
|
|
|
|
case UNARY_NEGATIVE:
|
|
|
|
case UNARY_CONVERT:
|
|
|
|
case UNARY_INVERT:
|
|
|
|
if (lastlc >= 1 &&
|
|
|
|
ISBASICBLOCK(blocks, i-3, 4) &&
|
|
|
|
fold_unaryops_on_constants(&codestr[i-3], consts)) {
|
|
|
|
i -= 2;
|
|
|
|
assert(codestr[i] == LOAD_CONST);
|
|
|
|
cumlc = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-08-25 00:18:29 -03:00
|
|
|
/* Simplify conditional jump to conditional jump where the
|
|
|
|
result of the first test implies the success of a similar
|
|
|
|
test or the failure of the opposite test.
|
|
|
|
Arises in code like:
|
2004-10-30 05:55:08 -03:00
|
|
|
"if a and b:"
|
|
|
|
"if a or b:"
|
2004-08-25 00:18:29 -03:00
|
|
|
"a and b or c"
|
2004-10-30 18:08:59 -03:00
|
|
|
"(a and b) and c"
|
2004-08-25 00:18:29 -03:00
|
|
|
x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z
|
2004-08-25 12:15:56 -03:00
|
|
|
x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3
|
|
|
|
where y+3 is the instruction following the second test.
|
2004-08-25 00:18:29 -03:00
|
|
|
*/
|
|
|
|
case JUMP_IF_FALSE:
|
|
|
|
case JUMP_IF_TRUE:
|
|
|
|
tgt = GETJUMPTGT(codestr, i);
|
|
|
|
j = codestr[tgt];
|
|
|
|
if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) {
|
|
|
|
if (j == opcode) {
|
|
|
|
tgttgt = GETJUMPTGT(codestr, tgt) - i - 3;
|
|
|
|
SETARG(codestr, i, tgttgt);
|
|
|
|
} else {
|
|
|
|
tgt -= i;
|
|
|
|
SETARG(codestr, i, tgt);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Intentional fallthrough */
|
|
|
|
|
2003-03-25 21:07:54 -04:00
|
|
|
/* Replace jumps to unconditional jumps */
|
2003-04-15 07:35:07 -03:00
|
|
|
case FOR_ITER:
|
2003-03-25 21:07:54 -04:00
|
|
|
case JUMP_FORWARD:
|
|
|
|
case JUMP_ABSOLUTE:
|
|
|
|
case CONTINUE_LOOP:
|
|
|
|
case SETUP_LOOP:
|
|
|
|
case SETUP_EXCEPT:
|
|
|
|
case SETUP_FINALLY:
|
|
|
|
tgt = GETJUMPTGT(codestr, i);
|
2004-03-21 11:12:00 -04:00
|
|
|
if (!UNCONDITIONAL_JUMP(codestr[tgt]))
|
2003-03-25 21:07:54 -04:00
|
|
|
continue;
|
|
|
|
tgttgt = GETJUMPTGT(codestr, tgt);
|
|
|
|
if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
|
|
|
|
opcode = JUMP_ABSOLUTE;
|
2003-03-28 08:05:00 -04:00
|
|
|
if (!ABSOLUTE_JUMP(opcode))
|
2003-03-25 21:07:54 -04:00
|
|
|
tgttgt -= i + 3; /* Calc relative jump addr */
|
|
|
|
if (tgttgt < 0) /* No backward relative jumps */
|
|
|
|
continue;
|
|
|
|
codestr[i] = opcode;
|
|
|
|
SETARG(codestr, i, tgttgt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EXTENDED_ARG:
|
|
|
|
goto exitUnchanged;
|
2004-08-23 20:37:48 -03:00
|
|
|
|
|
|
|
/* Replace RETURN LOAD_CONST None RETURN with just RETURN */
|
|
|
|
case RETURN_VALUE:
|
2004-09-22 15:44:21 -03:00
|
|
|
if (i+4 >= codelen ||
|
|
|
|
codestr[i+4] != RETURN_VALUE ||
|
2004-08-23 20:37:48 -03:00
|
|
|
!ISBASICBLOCK(blocks,i,5))
|
|
|
|
continue;
|
|
|
|
memset(codestr+i+1, NOP, 4);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fixup linenotab */
|
2004-11-01 11:19:11 -04:00
|
|
|
for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
|
|
|
|
addrmap[i] = i - nops;
|
|
|
|
if (codestr[i] == NOP)
|
|
|
|
nops++;
|
|
|
|
}
|
2004-08-23 20:37:48 -03:00
|
|
|
cum_orig_line = 0;
|
|
|
|
last_line = 0;
|
|
|
|
for (i=0 ; i < tabsiz ; i+=2) {
|
|
|
|
cum_orig_line += lineno[i];
|
|
|
|
new_line = addrmap[cum_orig_line];
|
2004-08-25 14:19:38 -03:00
|
|
|
assert (new_line - last_line < 255);
|
2004-08-23 20:37:48 -03:00
|
|
|
lineno[i] =((unsigned char)(new_line - last_line));
|
|
|
|
last_line = new_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove NOPs and fixup jump targets */
|
|
|
|
for (i=0, h=0 ; i<codelen ; ) {
|
|
|
|
opcode = codestr[i];
|
|
|
|
switch (opcode) {
|
|
|
|
case NOP:
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case JUMP_ABSOLUTE:
|
|
|
|
case CONTINUE_LOOP:
|
|
|
|
j = addrmap[GETARG(codestr, i)];
|
|
|
|
SETARG(codestr, i, j);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FOR_ITER:
|
|
|
|
case JUMP_FORWARD:
|
|
|
|
case JUMP_IF_FALSE:
|
|
|
|
case JUMP_IF_TRUE:
|
|
|
|
case SETUP_LOOP:
|
|
|
|
case SETUP_EXCEPT:
|
|
|
|
case SETUP_FINALLY:
|
|
|
|
j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
|
|
|
|
SETARG(codestr, i, j);
|
|
|
|
break;
|
2003-03-25 21:07:54 -04:00
|
|
|
}
|
2004-08-23 20:37:48 -03:00
|
|
|
adj = CODESIZE(opcode);
|
|
|
|
while (adj--)
|
|
|
|
codestr[h++] = codestr[i++];
|
2003-03-25 21:07:54 -04:00
|
|
|
}
|
2004-08-24 01:34:16 -03:00
|
|
|
assert(h + nops == codelen);
|
2004-08-23 20:37:48 -03:00
|
|
|
|
|
|
|
code = PyString_FromStringAndSize((char *)codestr, h);
|
|
|
|
PyMem_Free(addrmap);
|
2003-03-25 21:07:54 -04:00
|
|
|
PyMem_Free(codestr);
|
2004-03-21 11:12:00 -04:00
|
|
|
PyMem_Free(blocks);
|
2003-03-25 21:07:54 -04:00
|
|
|
return code;
|
|
|
|
|
|
|
|
exitUnchanged:
|
2004-10-30 05:55:08 -03:00
|
|
|
if (blocks != NULL)
|
|
|
|
PyMem_Free(blocks);
|
2004-08-23 20:37:48 -03:00
|
|
|
if (addrmap != NULL)
|
|
|
|
PyMem_Free(addrmap);
|
|
|
|
if (codestr != NULL)
|
|
|
|
PyMem_Free(codestr);
|
2003-03-25 21:07:54 -04:00
|
|
|
Py_INCREF(code);
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2004-09-22 15:44:21 -03:00
|
|
|
/* End: Peephole optimizations ----------------------------------------- */
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyCodeObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
PyCode_New(int argcount, int nlocals, int stacksize, int flags,
|
|
|
|
PyObject *code, PyObject *consts, PyObject *names,
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
PyObject *varnames, PyObject *freevars, PyObject *cellvars,
|
|
|
|
PyObject *filename, PyObject *name, int firstlineno,
|
|
|
|
PyObject *lnotab)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyCodeObject *co;
|
1990-11-18 13:27:39 -04:00
|
|
|
int i;
|
|
|
|
/* Check argument types */
|
1995-07-18 11:51:37 -03:00
|
|
|
if (argcount < 0 || nlocals < 0 ||
|
1998-10-07 16:42:25 -03:00
|
|
|
code == NULL ||
|
1997-04-29 17:08:16 -03:00
|
|
|
consts == NULL || !PyTuple_Check(consts) ||
|
|
|
|
names == NULL || !PyTuple_Check(names) ||
|
|
|
|
varnames == NULL || !PyTuple_Check(varnames) ||
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
freevars == NULL || !PyTuple_Check(freevars) ||
|
|
|
|
cellvars == NULL || !PyTuple_Check(cellvars) ||
|
1997-04-29 17:08:16 -03:00
|
|
|
name == NULL || !PyString_Check(name) ||
|
|
|
|
filename == NULL || !PyString_Check(filename) ||
|
2001-11-09 18:02:48 -04:00
|
|
|
lnotab == NULL || !PyString_Check(lnotab) ||
|
|
|
|
!PyObject_CheckReadBuffer(code)) {
|
1998-10-07 16:42:25 -03:00
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
intern_strings(names);
|
|
|
|
intern_strings(varnames);
|
|
|
|
intern_strings(freevars);
|
|
|
|
intern_strings(cellvars);
|
1997-01-18 04:02:57 -04:00
|
|
|
/* Intern selected string constants */
|
1997-04-29 17:08:16 -03:00
|
|
|
for (i = PyTuple_Size(consts); --i >= 0; ) {
|
|
|
|
PyObject *v = PyTuple_GetItem(consts, i);
|
|
|
|
if (!PyString_Check(v))
|
1997-01-18 04:02:57 -04:00
|
|
|
continue;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
|
1997-01-18 04:02:57 -04:00
|
|
|
continue;
|
|
|
|
PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
co = PyObject_NEW(PyCodeObject, &PyCode_Type);
|
1990-11-18 13:27:39 -04:00
|
|
|
if (co != NULL) {
|
1995-07-18 11:51:37 -03:00
|
|
|
co->co_argcount = argcount;
|
|
|
|
co->co_nlocals = nlocals;
|
1997-01-17 17:04:03 -04:00
|
|
|
co->co_stacksize = stacksize;
|
1995-07-18 11:51:37 -03:00
|
|
|
co->co_flags = flags;
|
2004-08-18 02:22:06 -03:00
|
|
|
Py_INCREF(code);
|
|
|
|
co->co_code = code;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(consts);
|
1990-11-18 13:27:39 -04:00
|
|
|
co->co_consts = consts;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(names);
|
1990-11-18 13:27:39 -04:00
|
|
|
co->co_names = names;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(varnames);
|
1995-07-18 11:51:37 -03:00
|
|
|
co->co_varnames = varnames;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
Py_INCREF(freevars);
|
|
|
|
co->co_freevars = freevars;
|
|
|
|
Py_INCREF(cellvars);
|
|
|
|
co->co_cellvars = cellvars;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(filename);
|
1991-06-04 16:41:56 -03:00
|
|
|
co->co_filename = filename;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(name);
|
1993-03-29 06:43:31 -04:00
|
|
|
co->co_name = name;
|
1997-01-23 23:43:35 -04:00
|
|
|
co->co_firstlineno = firstlineno;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(lnotab);
|
1997-01-23 23:43:35 -04:00
|
|
|
co->co_lnotab = lnotab;
|
2003-02-05 19:13:00 -04:00
|
|
|
if (PyTuple_GET_SIZE(freevars) == 0 &&
|
|
|
|
PyTuple_GET_SIZE(cellvars) == 0)
|
|
|
|
co->co_flags |= CO_NOFREE;
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
return co;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Data structure used internally */
|
1991-07-27 18:34:52 -03:00
|
|
|
|
2001-01-18 23:21:30 -04:00
|
|
|
/* The compiler uses two passes to generate bytecodes. The first pass
|
|
|
|
builds the symbol table. The second pass generates the bytecode.
|
|
|
|
|
|
|
|
The first pass uses a single symtable struct. The second pass uses
|
|
|
|
a compiling struct for each code block. The compiling structs
|
|
|
|
share a reference to the symtable.
|
|
|
|
|
|
|
|
The two passes communicate via symtable_load_symbols() and via
|
|
|
|
is_local() and is_global(). The former initializes several slots
|
|
|
|
in the compiling struct: c_varnames, c_locals, c_nlocals,
|
|
|
|
c_argcount, c_globals, and c_flags.
|
|
|
|
*/
|
|
|
|
|
2001-06-09 06:26:21 -03:00
|
|
|
/* All about c_lnotab.
|
|
|
|
|
2002-08-15 11:59:02 -03:00
|
|
|
c_lnotab is an array of unsigned bytes disguised as a Python string. Since
|
|
|
|
version 2.3, SET_LINENO opcodes are never generated and bytecode offsets are
|
|
|
|
mapped to source code line #s via c_lnotab instead.
|
|
|
|
|
2001-06-09 06:26:21 -03:00
|
|
|
The array is conceptually a list of
|
|
|
|
(bytecode offset increment, line number increment)
|
|
|
|
pairs. The details are important and delicate, best illustrated by example:
|
|
|
|
|
|
|
|
byte code offset source code line number
|
|
|
|
0 1
|
|
|
|
6 2
|
|
|
|
50 7
|
|
|
|
350 307
|
|
|
|
361 308
|
|
|
|
|
|
|
|
The first trick is that these numbers aren't stored, only the increments
|
|
|
|
from one row to the next (this doesn't really work, but it's a start):
|
|
|
|
|
|
|
|
0, 1, 6, 1, 44, 5, 300, 300, 11, 1
|
|
|
|
|
|
|
|
The second trick is that an unsigned byte can't hold negative values, or
|
|
|
|
values larger than 255, so (a) there's a deep assumption that byte code
|
|
|
|
offsets and their corresponding line #s both increase monotonically, and (b)
|
|
|
|
if at least one column jumps by more than 255 from one row to the next, more
|
|
|
|
than one pair is written to the table. In case #b, there's no way to know
|
|
|
|
from looking at the table later how many were written. That's the delicate
|
|
|
|
part. A user of c_lnotab desiring to find the source line number
|
|
|
|
corresponding to a bytecode address A should do something like this
|
|
|
|
|
|
|
|
lineno = addr = 0
|
|
|
|
for addr_incr, line_incr in c_lnotab:
|
|
|
|
addr += addr_incr
|
|
|
|
if addr > A:
|
|
|
|
return lineno
|
|
|
|
lineno += line_incr
|
|
|
|
|
|
|
|
In order for this to work, when the addr field increments by more than 255,
|
|
|
|
the line # increment in each pair generated must be 0 until the remaining addr
|
|
|
|
increment is < 256. So, in the example above, com_set_lineno should not (as
|
|
|
|
was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
|
|
|
|
255, 0, 45, 255, 0, 45.
|
|
|
|
*/
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
struct compiling {
|
2000-09-08 13:31:24 -03:00
|
|
|
PyObject *c_code; /* string */
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *c_consts; /* list of objects */
|
2000-04-28 13:42:25 -03:00
|
|
|
PyObject *c_const_dict; /* inverse of c_consts */
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *c_names; /* list of strings (names) */
|
2000-04-28 13:42:25 -03:00
|
|
|
PyObject *c_name_dict; /* inverse of c_names */
|
2002-12-17 21:18:44 -04:00
|
|
|
PyObject *c_globals; /* dictionary (value=None or True) */
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *c_locals; /* dictionary (value=localID) */
|
|
|
|
PyObject *c_varnames; /* list (inverse of c_locals) */
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
PyObject *c_freevars; /* dictionary (value=None) */
|
2004-08-14 22:15:01 -03:00
|
|
|
PyObject *c_cellvars; /* dictionary */
|
1995-07-18 11:51:37 -03:00
|
|
|
int c_nlocals; /* index of next local */
|
|
|
|
int c_argcount; /* number of top-level arguments */
|
|
|
|
int c_flags; /* same as co_flags */
|
1990-11-18 13:27:39 -04:00
|
|
|
int c_nexti; /* index into c_code */
|
|
|
|
int c_errors; /* counts errors occurred */
|
1990-12-20 11:06:42 -04:00
|
|
|
int c_infunction; /* set when compiling a function */
|
1994-08-29 09:16:12 -03:00
|
|
|
int c_interactive; /* generating code for interactive command */
|
1990-12-20 11:06:42 -04:00
|
|
|
int c_loops; /* counts nested loops */
|
1991-07-27 18:34:52 -03:00
|
|
|
int c_begin; /* begin of current loop, for 'continue' */
|
1997-01-17 17:04:03 -04:00
|
|
|
int c_block[CO_MAXBLOCKS]; /* stack of block types */
|
1991-07-27 18:34:52 -03:00
|
|
|
int c_nblocks; /* current block stack level */
|
2002-12-11 10:04:59 -04:00
|
|
|
const char *c_filename; /* filename of current node */
|
1993-03-29 06:43:31 -04:00
|
|
|
char *c_name; /* name of object (e.g. function) */
|
1996-09-17 11:32:04 -03:00
|
|
|
int c_lineno; /* Current line number */
|
1997-01-17 17:04:03 -04:00
|
|
|
int c_stacklevel; /* Current stack level */
|
|
|
|
int c_maxstacklevel; /* Maximum stack level */
|
1997-01-23 23:43:35 -04:00
|
|
|
int c_firstlineno;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *c_lnotab; /* Table mapping address to line number */
|
2004-03-22 13:52:53 -04:00
|
|
|
int c_last_addr; /* last op addr seen and recorded in lnotab */
|
|
|
|
int c_last_line; /* last line seen and recorded in lnotab */
|
|
|
|
int c_lnotab_next; /* current length of lnotab */
|
|
|
|
int c_lnotab_last; /* start of last lnotab record added */
|
1996-08-24 03:21:31 -03:00
|
|
|
char *c_private; /* for private name mangling */
|
2000-08-12 15:09:51 -03:00
|
|
|
int c_tmpname; /* temporary local name counter */
|
2001-02-09 11:06:42 -04:00
|
|
|
int c_nested; /* Is block nested funcdef or lamdef? */
|
|
|
|
int c_closure; /* Is nested w/freevars? */
|
|
|
|
struct symtable *c_symtable; /* pointer to module symbol table */
|
2001-02-27 15:07:02 -04:00
|
|
|
PyFutureFeatures *c_future; /* pointer to module's __future__ */
|
2002-08-04 14:29:52 -03:00
|
|
|
char *c_encoding; /* source encoding (a borrowed reference) */
|
2001-01-18 23:21:30 -04:00
|
|
|
};
|
|
|
|
|
2001-04-14 14:55:09 -03:00
|
|
|
static int
|
|
|
|
is_free(int v)
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
{
|
|
|
|
if ((v & (USE | DEF_FREE))
|
|
|
|
&& !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
|
|
|
|
return 1;
|
|
|
|
if (v & DEF_FREE_CLASS)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
1991-07-27 18:34:52 -03:00
|
|
|
|
2001-02-02 16:01:10 -04:00
|
|
|
static void
|
|
|
|
com_error(struct compiling *c, PyObject *exc, char *msg)
|
|
|
|
{
|
2001-02-28 03:07:43 -04:00
|
|
|
PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
|
|
|
|
|
2001-02-02 16:01:10 -04:00
|
|
|
if (c == NULL) {
|
|
|
|
/* Error occurred via symtable call to
|
|
|
|
is_constant_false */
|
|
|
|
PyErr_SetString(exc, msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
c->c_errors++;
|
2001-02-28 03:07:43 -04:00
|
|
|
if (c->c_lineno < 1 || c->c_interactive) {
|
|
|
|
/* Unknown line number or interactive input */
|
2001-02-02 16:01:10 -04:00
|
|
|
PyErr_SetString(exc, msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
v = PyString_FromString(msg);
|
|
|
|
if (v == NULL)
|
|
|
|
return; /* MemoryError, too bad */
|
|
|
|
|
2001-02-28 13:47:12 -04:00
|
|
|
line = PyErr_ProgramText(c->c_filename, c->c_lineno);
|
2001-02-28 03:07:43 -04:00
|
|
|
if (line == NULL) {
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
line = Py_None;
|
|
|
|
}
|
2002-03-03 17:30:27 -04:00
|
|
|
if (exc == PyExc_SyntaxError) {
|
|
|
|
t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
|
|
|
|
Py_None, line);
|
|
|
|
if (t == NULL)
|
|
|
|
goto exit;
|
2003-10-12 16:09:37 -03:00
|
|
|
w = PyTuple_Pack(2, v, t);
|
2002-03-03 17:30:27 -04:00
|
|
|
if (w == NULL)
|
|
|
|
goto exit;
|
|
|
|
PyErr_SetObject(exc, w);
|
|
|
|
} else {
|
|
|
|
/* Make sure additional exceptions are printed with
|
|
|
|
file and line, also. */
|
|
|
|
PyErr_SetObject(exc, v);
|
|
|
|
PyErr_SyntaxLocation(c->c_filename, c->c_lineno);
|
|
|
|
}
|
2001-02-28 03:07:43 -04:00
|
|
|
exit:
|
|
|
|
Py_XDECREF(t);
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(w);
|
|
|
|
Py_XDECREF(line);
|
2001-02-02 16:01:10 -04:00
|
|
|
}
|
|
|
|
|
1991-07-27 18:34:52 -03:00
|
|
|
/* Interface to the block stack */
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
block_push(struct compiling *c, int type)
|
1991-07-27 18:34:52 -03:00
|
|
|
{
|
1997-01-17 17:04:03 -04:00
|
|
|
if (c->c_nblocks >= CO_MAXBLOCKS) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"too many statically nested blocks");
|
1991-07-27 18:34:52 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
c->c_block[c->c_nblocks++] = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
block_pop(struct compiling *c, int type)
|
1991-07-27 18:34:52 -03:00
|
|
|
{
|
|
|
|
if (c->c_nblocks > 0)
|
|
|
|
c->c_nblocks--;
|
|
|
|
if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError, "bad block pop");
|
1991-07-27 18:34:52 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-07-18 11:51:37 -03:00
|
|
|
/* Prototype forward declarations */
|
1991-07-27 18:34:52 -03:00
|
|
|
|
2002-12-11 10:04:59 -04:00
|
|
|
static int issue_warning(const char *, const char *, int);
|
|
|
|
static int com_init(struct compiling *, const char *);
|
2000-07-09 00:09:57 -03:00
|
|
|
static void com_free(struct compiling *);
|
|
|
|
static void com_push(struct compiling *, int);
|
|
|
|
static void com_pop(struct compiling *, int);
|
|
|
|
static void com_done(struct compiling *);
|
2001-01-18 23:21:30 -04:00
|
|
|
static void com_node(struct compiling *, node *);
|
|
|
|
static void com_factor(struct compiling *, node *);
|
2000-07-09 00:09:57 -03:00
|
|
|
static void com_addbyte(struct compiling *, int);
|
|
|
|
static void com_addint(struct compiling *, int);
|
|
|
|
static void com_addoparg(struct compiling *, int, int);
|
|
|
|
static void com_addfwref(struct compiling *, int, int *);
|
|
|
|
static void com_backpatch(struct compiling *, int);
|
|
|
|
static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
|
|
|
|
static int com_addconst(struct compiling *, PyObject *);
|
|
|
|
static int com_addname(struct compiling *, PyObject *);
|
|
|
|
static void com_addopname(struct compiling *, int, node *);
|
2004-05-19 05:20:33 -03:00
|
|
|
static void com_test(struct compiling *c, node *n);
|
2000-07-09 00:09:57 -03:00
|
|
|
static void com_list(struct compiling *, node *, int);
|
2000-08-12 15:09:51 -03:00
|
|
|
static void com_list_iter(struct compiling *, node *, node *, char *);
|
2004-05-19 05:20:33 -03:00
|
|
|
static void com_gen_iter(struct compiling *, node *, node *);
|
2000-07-09 00:09:57 -03:00
|
|
|
static int com_argdefs(struct compiling *, node *);
|
2000-08-24 17:11:32 -03:00
|
|
|
static void com_assign(struct compiling *, node *, int, node *);
|
|
|
|
static void com_assign_name(struct compiling *, node *, int);
|
2004-05-19 05:20:33 -03:00
|
|
|
static int com_make_closure(struct compiling *c, PyCodeObject *co);
|
|
|
|
|
2001-01-18 23:21:30 -04:00
|
|
|
static PyCodeObject *icompile(node *, struct compiling *);
|
2002-12-11 10:04:59 -04:00
|
|
|
static PyCodeObject *jcompile(node *, const char *, struct compiling *,
|
2001-03-01 18:59:14 -04:00
|
|
|
PyCompilerFlags *);
|
2001-08-17 15:39:25 -03:00
|
|
|
static PyObject *parsestrplus(struct compiling*, node *);
|
|
|
|
static PyObject *parsestr(struct compiling *, char *);
|
2000-08-24 17:11:32 -03:00
|
|
|
static node *get_rawdocstring(node *);
|
1990-11-18 13:27:39 -04:00
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
static int get_ref_type(struct compiling *, char *);
|
2001-01-18 23:21:30 -04:00
|
|
|
|
|
|
|
/* symtable operations */
|
2004-05-19 05:20:33 -03:00
|
|
|
static int symtable_lookup(struct symtable *st, char *name);
|
2003-07-15 17:23:26 -03:00
|
|
|
static struct symtable *symtable_build(node *, PyFutureFeatures *,
|
|
|
|
const char *filename);
|
2001-01-18 23:21:30 -04:00
|
|
|
static int symtable_load_symbols(struct compiling *);
|
2001-02-09 18:22:18 -04:00
|
|
|
static struct symtable *symtable_init(void);
|
2001-02-02 14:19:15 -04:00
|
|
|
static void symtable_enter_scope(struct symtable *, char *, int, int);
|
2001-01-18 23:21:30 -04:00
|
|
|
static int symtable_exit_scope(struct symtable *);
|
|
|
|
static int symtable_add_def(struct symtable *, char *, int);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
|
2001-01-18 23:21:30 -04:00
|
|
|
|
|
|
|
static void symtable_node(struct symtable *, node *);
|
|
|
|
static void symtable_funcdef(struct symtable *, node *);
|
|
|
|
static void symtable_default_args(struct symtable *, node *);
|
|
|
|
static void symtable_params(struct symtable *, node *);
|
|
|
|
static void symtable_params_fplist(struct symtable *, node *n);
|
|
|
|
static void symtable_global(struct symtable *, node *);
|
|
|
|
static void symtable_import(struct symtable *, node *);
|
2001-01-29 21:24:43 -04:00
|
|
|
static void symtable_assign(struct symtable *, node *, int);
|
2001-01-18 23:21:30 -04:00
|
|
|
static void symtable_list_comprehension(struct symtable *, node *);
|
2004-05-19 05:20:33 -03:00
|
|
|
static void symtable_generator_expression(struct symtable *, node *);
|
2003-05-21 14:34:50 -03:00
|
|
|
static void symtable_list_for(struct symtable *, node *);
|
2004-05-19 05:20:33 -03:00
|
|
|
static void symtable_gen_for(struct symtable *, node *, int);
|
|
|
|
static void symtable_gen_iter(struct symtable *, node *);
|
2001-01-18 23:21:30 -04:00
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
static int symtable_update_free_vars(struct symtable *);
|
|
|
|
static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
|
|
|
|
static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
|
|
|
|
|
2001-01-18 23:21:30 -04:00
|
|
|
/* helper */
|
|
|
|
static void
|
|
|
|
do_pad(int pad)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < pad; ++i)
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump(node *n, int pad, int depth)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (depth == 0)
|
|
|
|
return;
|
|
|
|
do_pad(pad);
|
|
|
|
fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
|
|
|
|
if (depth > 0)
|
|
|
|
depth--;
|
|
|
|
for (i = 0; i < NCH(n); ++i)
|
|
|
|
dump(CHILD(n, i), pad + 1, depth);
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static int
|
2002-12-11 10:04:59 -04:00
|
|
|
com_init(struct compiling *c, const char *filename)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2000-04-28 13:42:25 -03:00
|
|
|
memset((void *)c, '\0', sizeof(struct compiling));
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
|
|
|
|
1000)) == NULL)
|
2000-04-28 13:42:25 -03:00
|
|
|
goto fail;
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((c->c_consts = PyList_New(0)) == NULL)
|
2000-04-28 13:42:25 -03:00
|
|
|
goto fail;
|
|
|
|
if ((c->c_const_dict = PyDict_New()) == NULL)
|
|
|
|
goto fail;
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((c->c_names = PyList_New(0)) == NULL)
|
2000-04-28 13:42:25 -03:00
|
|
|
goto fail;
|
|
|
|
if ((c->c_name_dict = PyDict_New()) == NULL)
|
|
|
|
goto fail;
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((c->c_locals = PyDict_New()) == NULL)
|
2000-04-28 13:42:25 -03:00
|
|
|
goto fail;
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
|
|
|
|
1000)) == NULL)
|
2000-04-28 13:42:25 -03:00
|
|
|
goto fail;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
c->c_globals = NULL;
|
2001-01-18 23:21:30 -04:00
|
|
|
c->c_varnames = NULL;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
c->c_freevars = NULL;
|
|
|
|
c->c_cellvars = NULL;
|
1995-07-18 11:51:37 -03:00
|
|
|
c->c_nlocals = 0;
|
|
|
|
c->c_argcount = 0;
|
|
|
|
c->c_flags = 0;
|
1990-11-18 13:27:39 -04:00
|
|
|
c->c_nexti = 0;
|
|
|
|
c->c_errors = 0;
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_infunction = 0;
|
1994-08-29 09:16:12 -03:00
|
|
|
c->c_interactive = 0;
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_loops = 0;
|
1991-07-27 18:34:52 -03:00
|
|
|
c->c_begin = 0;
|
|
|
|
c->c_nblocks = 0;
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_filename = filename;
|
1993-03-29 06:43:31 -04:00
|
|
|
c->c_name = "?";
|
1996-09-17 11:32:04 -03:00
|
|
|
c->c_lineno = 0;
|
1997-01-17 17:04:03 -04:00
|
|
|
c->c_stacklevel = 0;
|
|
|
|
c->c_maxstacklevel = 0;
|
1997-01-23 23:43:35 -04:00
|
|
|
c->c_firstlineno = 0;
|
|
|
|
c->c_last_addr = 0;
|
|
|
|
c->c_last_line = 0;
|
2001-01-22 00:35:57 -04:00
|
|
|
c->c_lnotab_next = 0;
|
2004-03-22 13:52:53 -04:00
|
|
|
c->c_lnotab_last = 0;
|
2000-08-12 15:09:51 -03:00
|
|
|
c->c_tmpname = 0;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
c->c_nested = 0;
|
|
|
|
c->c_closure = 0;
|
2001-01-18 23:21:30 -04:00
|
|
|
c->c_symtable = NULL;
|
1990-11-18 13:27:39 -04:00
|
|
|
return 1;
|
|
|
|
|
2000-04-28 13:42:25 -03:00
|
|
|
fail:
|
|
|
|
com_free(c);
|
1990-11-18 13:27:39 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_free(struct compiling *c)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(c->c_code);
|
|
|
|
Py_XDECREF(c->c_consts);
|
2000-04-28 13:42:25 -03:00
|
|
|
Py_XDECREF(c->c_const_dict);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(c->c_names);
|
2000-04-28 13:42:25 -03:00
|
|
|
Py_XDECREF(c->c_name_dict);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(c->c_globals);
|
|
|
|
Py_XDECREF(c->c_locals);
|
|
|
|
Py_XDECREF(c->c_varnames);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
Py_XDECREF(c->c_freevars);
|
|
|
|
Py_XDECREF(c->c_cellvars);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(c->c_lnotab);
|
2001-02-27 21:58:08 -04:00
|
|
|
if (c->c_future)
|
2002-08-04 03:28:21 -03:00
|
|
|
PyObject_FREE((void *)c->c_future);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
1997-01-17 17:04:03 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_push(struct compiling *c, int n)
|
1997-01-17 17:04:03 -04:00
|
|
|
{
|
|
|
|
c->c_stacklevel += n;
|
2001-10-17 10:22:22 -03:00
|
|
|
if (c->c_stacklevel > c->c_maxstacklevel) {
|
1997-01-17 17:04:03 -04:00
|
|
|
c->c_maxstacklevel = c->c_stacklevel;
|
2001-10-17 10:22:22 -03:00
|
|
|
/*
|
|
|
|
fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n",
|
|
|
|
c->c_filename, c->c_name, c->c_lineno,
|
|
|
|
c->c_nexti, c->c_stacklevel, n);
|
|
|
|
*/
|
|
|
|
}
|
1997-01-17 17:04:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_pop(struct compiling *c, int n)
|
1997-01-17 17:04:03 -04:00
|
|
|
{
|
2001-10-17 10:22:22 -03:00
|
|
|
if (c->c_stacklevel < n)
|
1997-01-17 17:04:03 -04:00
|
|
|
c->c_stacklevel = 0;
|
|
|
|
else
|
|
|
|
c->c_stacklevel -= n;
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_done(struct compiling *c)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
if (c->c_code != NULL)
|
1997-04-29 17:08:16 -03:00
|
|
|
_PyString_Resize(&c->c_code, c->c_nexti);
|
1997-01-23 23:43:35 -04:00
|
|
|
if (c->c_lnotab != NULL)
|
1997-04-29 17:08:16 -03:00
|
|
|
_PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
2001-10-17 10:22:22 -03:00
|
|
|
static int
|
|
|
|
com_check_size(PyObject **s, int offset)
|
|
|
|
{
|
|
|
|
int len = PyString_GET_SIZE(*s);
|
|
|
|
if (offset >= len)
|
|
|
|
return _PyString_Resize(s, len * 2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addbyte(struct compiling *c, int byte)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1995-07-18 11:51:37 -03:00
|
|
|
/*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
|
2001-01-18 23:21:30 -04:00
|
|
|
assert(byte >= 0 && byte <= 255);
|
2002-01-01 15:59:11 -04:00
|
|
|
assert(c->c_code != 0);
|
2001-10-17 10:22:22 -03:00
|
|
|
if (com_check_size(&c->c_code, c->c_nexti)) {
|
|
|
|
c->c_errors++;
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
}
|
2001-10-17 10:22:22 -03:00
|
|
|
PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte;
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addint(struct compiling *c, int x)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1990-11-18 13:35:03 -04:00
|
|
|
com_addbyte(c, x & 0xff);
|
|
|
|
com_addbyte(c, x >> 8); /* XXX x should be positive */
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
1997-01-23 23:43:35 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_add_lnotab(struct compiling *c, int addr, int line)
|
1997-01-23 23:43:35 -04:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
if (c->c_lnotab == NULL)
|
|
|
|
return;
|
2001-10-17 10:22:22 -03:00
|
|
|
if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) {
|
|
|
|
c->c_errors++;
|
|
|
|
return;
|
1997-01-23 23:43:35 -04:00
|
|
|
}
|
2001-10-17 10:22:22 -03:00
|
|
|
p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next;
|
1997-01-23 23:43:35 -04:00
|
|
|
*p++ = addr;
|
|
|
|
*p++ = line;
|
|
|
|
c->c_lnotab_next += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_set_lineno(struct compiling *c, int lineno)
|
1997-01-23 23:43:35 -04:00
|
|
|
{
|
|
|
|
c->c_lineno = lineno;
|
|
|
|
if (c->c_firstlineno == 0) {
|
|
|
|
c->c_firstlineno = c->c_last_line = lineno;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int incr_addr = c->c_nexti - c->c_last_addr;
|
|
|
|
int incr_line = lineno - c->c_last_line;
|
2004-03-22 13:52:53 -04:00
|
|
|
c->c_lnotab_last = c->c_lnotab_next;
|
2001-06-09 06:26:21 -03:00
|
|
|
while (incr_addr > 255) {
|
|
|
|
com_add_lnotab(c, 255, 0);
|
|
|
|
incr_addr -= 255;
|
|
|
|
}
|
|
|
|
while (incr_line > 255) {
|
|
|
|
com_add_lnotab(c, incr_addr, 255);
|
|
|
|
incr_line -=255;
|
|
|
|
incr_addr = 0;
|
1997-01-23 23:43:35 -04:00
|
|
|
}
|
2001-06-09 06:26:21 -03:00
|
|
|
if (incr_addr > 0 || incr_line > 0)
|
|
|
|
com_add_lnotab(c, incr_addr, incr_line);
|
1997-01-23 23:43:35 -04:00
|
|
|
c->c_last_addr = c->c_nexti;
|
|
|
|
c->c_last_line = lineno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-22 13:52:53 -04:00
|
|
|
static void
|
|
|
|
com_strip_lnotab(struct compiling *c)
|
|
|
|
{
|
|
|
|
/* strip the last lnotab entry if no opcode were emitted.
|
|
|
|
* This prevents a line number to be generated on a final
|
|
|
|
* pass, like in the following example:
|
|
|
|
*
|
|
|
|
* if a:
|
|
|
|
* print 5
|
|
|
|
* else:
|
|
|
|
* pass
|
|
|
|
*
|
|
|
|
* Without the fix, a line trace event would be generated
|
|
|
|
* on the pass even if a is true (because of the implicit
|
|
|
|
* return).
|
|
|
|
*/
|
|
|
|
if (c->c_nexti == c->c_last_addr && c->c_lnotab_last > 0) {
|
|
|
|
c->c_lnotab_next = c->c_lnotab_last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addoparg(struct compiling *c, int op, int arg)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2000-08-23 21:32:09 -03:00
|
|
|
int extended_arg = arg >> 16;
|
|
|
|
if (extended_arg){
|
|
|
|
com_addbyte(c, EXTENDED_ARG);
|
|
|
|
com_addint(c, extended_arg);
|
|
|
|
arg &= 0xffff;
|
|
|
|
}
|
1997-03-03 15:13:14 -04:00
|
|
|
com_addbyte(c, op);
|
|
|
|
com_addint(c, arg);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addfwref(struct compiling *c, int op, int *p_anchor)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
/* Compile a forward reference for backpatching */
|
|
|
|
int here;
|
|
|
|
int anchor;
|
|
|
|
com_addbyte(c, op);
|
|
|
|
here = c->c_nexti;
|
|
|
|
anchor = *p_anchor;
|
|
|
|
*p_anchor = here;
|
|
|
|
com_addint(c, anchor == 0 ? 0 : here - anchor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_backpatch(struct compiling *c, int anchor)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2001-10-17 10:22:22 -03:00
|
|
|
unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code);
|
1990-11-18 13:27:39 -04:00
|
|
|
int target = c->c_nexti;
|
|
|
|
int dist;
|
|
|
|
int prev;
|
|
|
|
for (;;) {
|
|
|
|
/* Make the JUMP instruction at anchor point to target */
|
1990-11-18 13:35:03 -04:00
|
|
|
prev = code[anchor] + (code[anchor+1] << 8);
|
|
|
|
dist = target - (anchor+2);
|
|
|
|
code[anchor] = dist & 0xff;
|
2000-08-23 21:32:09 -03:00
|
|
|
dist >>= 8;
|
|
|
|
code[anchor+1] = dist;
|
|
|
|
dist >>= 8;
|
|
|
|
if (dist) {
|
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"com_backpatch: offset too large");
|
|
|
|
break;
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
if (!prev)
|
|
|
|
break;
|
|
|
|
anchor -= prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1991-12-31 09:13:35 -04:00
|
|
|
/* Handle literals and names uniformly */
|
1990-11-18 13:27:39 -04:00
|
|
|
|
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2000-04-28 13:42:25 -03:00
|
|
|
PyObject *w, *t, *np=NULL;
|
|
|
|
long n;
|
2001-01-18 23:21:30 -04:00
|
|
|
|
2003-10-12 16:09:37 -03:00
|
|
|
t = PyTuple_Pack(2, v, v->ob_type);
|
2000-04-28 13:42:25 -03:00
|
|
|
if (t == NULL)
|
|
|
|
goto fail;
|
|
|
|
w = PyDict_GetItem(dict, t);
|
|
|
|
if (w != NULL) {
|
|
|
|
n = PyInt_AsLong(w);
|
|
|
|
} else {
|
|
|
|
n = PyList_Size(list);
|
|
|
|
np = PyInt_FromLong(n);
|
|
|
|
if (np == NULL)
|
|
|
|
goto fail;
|
|
|
|
if (PyList_Append(list, v) != 0)
|
|
|
|
goto fail;
|
|
|
|
if (PyDict_SetItem(dict, t, np) != 0)
|
|
|
|
goto fail;
|
|
|
|
Py_DECREF(np);
|
1990-11-18 13:35:03 -04:00
|
|
|
}
|
2000-04-28 13:42:25 -03:00
|
|
|
Py_DECREF(t);
|
1990-11-18 13:35:03 -04:00
|
|
|
return n;
|
2000-04-28 13:42:25 -03:00
|
|
|
fail:
|
|
|
|
Py_XDECREF(np);
|
|
|
|
Py_XDECREF(t);
|
|
|
|
c->c_errors++;
|
|
|
|
return 0;
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addconst(struct compiling *c, PyObject *v)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2000-04-28 13:42:25 -03:00
|
|
|
return com_add(c, c->c_consts, c->c_const_dict, v);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addname(struct compiling *c, PyObject *v)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2000-04-28 13:42:25 -03:00
|
|
|
return com_add(c, c->c_names, c->c_name_dict, v);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
2002-06-20 19:23:15 -03:00
|
|
|
int
|
|
|
|
_Py_Mangle(char *p, char *name, char *buffer, size_t maxlen)
|
1996-08-24 03:21:31 -03:00
|
|
|
{
|
1996-08-24 04:54:27 -03:00
|
|
|
/* Name mangling: __private becomes _classname__private.
|
1996-08-24 03:21:31 -03:00
|
|
|
This is independent from how the name is used. */
|
2000-06-28 19:07:35 -03:00
|
|
|
size_t nlen, plen;
|
2001-01-18 23:21:30 -04:00
|
|
|
if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
|
|
|
|
return 0;
|
1996-08-24 03:21:31 -03:00
|
|
|
nlen = strlen(name);
|
1996-08-24 04:54:27 -03:00
|
|
|
if (nlen+2 >= maxlen)
|
1996-08-24 03:21:31 -03:00
|
|
|
return 0; /* Don't mangle __extremely_long_names */
|
|
|
|
if (name[nlen-1] == '_' && name[nlen-2] == '_')
|
|
|
|
return 0; /* Don't mangle __whatever__ */
|
|
|
|
/* Strip leading underscores from class name */
|
|
|
|
while (*p == '_')
|
|
|
|
p++;
|
|
|
|
if (*p == '\0')
|
|
|
|
return 0; /* Don't mangle if class is just underscores */
|
|
|
|
plen = strlen(p);
|
|
|
|
if (plen + nlen >= maxlen)
|
1996-08-24 04:54:27 -03:00
|
|
|
plen = maxlen-nlen-2; /* Truncate class name if too long */
|
|
|
|
/* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
|
1996-08-24 03:21:31 -03:00
|
|
|
buffer[0] = '_';
|
|
|
|
strncpy(buffer+1, p, plen);
|
1996-08-24 04:54:27 -03:00
|
|
|
strcpy(buffer+1+plen, name);
|
1996-08-24 03:21:31 -03:00
|
|
|
return 1;
|
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_name(struct compiling *c, int op, char *name)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1990-11-18 13:27:39 -04:00
|
|
|
int i;
|
2001-01-18 23:21:30 -04:00
|
|
|
char buffer[MANGLE_LEN];
|
2001-01-29 21:24:43 -04:00
|
|
|
|
2002-06-20 19:23:15 -03:00
|
|
|
if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer)))
|
1996-08-24 03:21:31 -03:00
|
|
|
name = buffer;
|
1997-01-18 04:02:57 -04:00
|
|
|
if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
|
1990-11-18 13:27:39 -04:00
|
|
|
c->c_errors++;
|
|
|
|
i = 255;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = com_addname(c, v);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addoparg(c, op, i);
|
|
|
|
}
|
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
#define NAME_LOCAL 0
|
|
|
|
#define NAME_GLOBAL 1
|
|
|
|
#define NAME_DEFAULT 2
|
|
|
|
#define NAME_CLOSURE 3
|
|
|
|
|
|
|
|
static int
|
|
|
|
com_lookup_arg(PyObject *dict, PyObject *name)
|
|
|
|
{
|
|
|
|
PyObject *v = PyDict_GetItem(dict, name);
|
|
|
|
if (v == NULL)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return PyInt_AS_LONG(v);
|
|
|
|
}
|
|
|
|
|
2002-08-15 23:13:49 -03:00
|
|
|
static int
|
|
|
|
none_assignment_check(struct compiling *c, char *name, int assigning)
|
|
|
|
{
|
|
|
|
if (name[0] == 'N' && strcmp(name, "None") == 0) {
|
|
|
|
char *msg;
|
|
|
|
if (assigning)
|
2003-01-16 11:39:07 -04:00
|
|
|
msg = "assignment to None";
|
2002-08-15 23:13:49 -03:00
|
|
|
else
|
|
|
|
msg = "deleting None";
|
2004-07-17 18:46:25 -03:00
|
|
|
com_error(c, PyExc_SyntaxError, msg);
|
|
|
|
return -1;
|
2002-08-15 23:13:49 -03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-18 23:21:30 -04:00
|
|
|
static void
|
|
|
|
com_addop_varname(struct compiling *c, int kind, char *name)
|
|
|
|
{
|
|
|
|
PyObject *v;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
int i, reftype;
|
|
|
|
int scope = NAME_DEFAULT;
|
2001-01-18 23:21:30 -04:00
|
|
|
int op = STOP_CODE;
|
|
|
|
char buffer[MANGLE_LEN];
|
|
|
|
|
2002-08-15 23:13:49 -03:00
|
|
|
if (kind != VAR_LOAD &&
|
|
|
|
none_assignment_check(c, name, kind == VAR_STORE))
|
|
|
|
{
|
|
|
|
i = 255;
|
|
|
|
goto done;
|
|
|
|
}
|
2002-06-20 19:23:15 -03:00
|
|
|
if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer)))
|
2001-01-18 23:21:30 -04:00
|
|
|
name = buffer;
|
|
|
|
if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
|
|
|
|
c->c_errors++;
|
|
|
|
i = 255;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
reftype = get_ref_type(c, name);
|
|
|
|
switch (reftype) {
|
|
|
|
case LOCAL:
|
2001-02-09 18:22:18 -04:00
|
|
|
if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
scope = NAME_LOCAL;
|
|
|
|
break;
|
|
|
|
case GLOBAL_EXPLICIT:
|
|
|
|
scope = NAME_GLOBAL;
|
|
|
|
break;
|
|
|
|
case GLOBAL_IMPLICIT:
|
|
|
|
if (c->c_flags & CO_OPTIMIZED)
|
|
|
|
scope = NAME_GLOBAL;
|
|
|
|
break;
|
|
|
|
case FREE:
|
|
|
|
case CELL:
|
|
|
|
scope = NAME_CLOSURE;
|
|
|
|
break;
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
i = com_addname(c, v);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (scope == NAME_LOCAL)
|
|
|
|
i = com_lookup_arg(c->c_locals, v);
|
|
|
|
else if (reftype == FREE)
|
|
|
|
i = com_lookup_arg(c->c_freevars, v);
|
|
|
|
else if (reftype == CELL)
|
|
|
|
i = com_lookup_arg(c->c_cellvars, v);
|
|
|
|
if (i == -1) {
|
|
|
|
c->c_errors++; /* XXX no exception set */
|
|
|
|
i = 255;
|
|
|
|
goto done;
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
Py_DECREF(v);
|
|
|
|
|
|
|
|
switch (kind) {
|
|
|
|
case VAR_LOAD:
|
|
|
|
switch (scope) {
|
|
|
|
case NAME_LOCAL:
|
|
|
|
op = LOAD_FAST;
|
|
|
|
break;
|
|
|
|
case NAME_GLOBAL:
|
|
|
|
op = LOAD_GLOBAL;
|
|
|
|
break;
|
|
|
|
case NAME_DEFAULT:
|
|
|
|
op = LOAD_NAME;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
break;
|
|
|
|
case NAME_CLOSURE:
|
|
|
|
op = LOAD_DEREF;
|
|
|
|
break;
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VAR_STORE:
|
|
|
|
switch (scope) {
|
|
|
|
case NAME_LOCAL:
|
|
|
|
op = STORE_FAST;
|
|
|
|
break;
|
|
|
|
case NAME_GLOBAL:
|
|
|
|
op = STORE_GLOBAL;
|
|
|
|
break;
|
|
|
|
case NAME_DEFAULT:
|
|
|
|
op = STORE_NAME;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
break;
|
|
|
|
case NAME_CLOSURE:
|
|
|
|
op = STORE_DEREF;
|
|
|
|
break;
|
1991-12-10 09:53:51 -04:00
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
break;
|
|
|
|
case VAR_DELETE:
|
|
|
|
switch (scope) {
|
|
|
|
case NAME_LOCAL:
|
|
|
|
op = DELETE_FAST;
|
|
|
|
break;
|
|
|
|
case NAME_GLOBAL:
|
|
|
|
op = DELETE_GLOBAL;
|
|
|
|
break;
|
|
|
|
case NAME_DEFAULT:
|
|
|
|
op = DELETE_NAME;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
break;
|
|
|
|
case NAME_CLOSURE: {
|
2001-01-29 21:24:43 -04:00
|
|
|
char buf[500];
|
2001-11-28 17:10:39 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
|
|
DEL_CLOSURE_ERROR, name);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
com_error(c, PyExc_SyntaxError, buf);
|
|
|
|
i = 255;
|
|
|
|
break;
|
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
break;
|
1991-12-10 09:53:51 -04:00
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
done:
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addoparg(c, op, i);
|
|
|
|
}
|
|
|
|
|
1995-01-07 08:41:23 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_addopname(struct compiling *c, int op, node *n)
|
1995-01-07 08:41:23 -04:00
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char buffer[1000];
|
|
|
|
/* XXX it is possible to write this code without the 1000
|
|
|
|
chars on the total length of dotted names, I just can't be
|
|
|
|
bothered right now */
|
|
|
|
if (TYPE(n) == STAR)
|
|
|
|
name = "*";
|
|
|
|
else if (TYPE(n) == dotted_name) {
|
|
|
|
char *p = buffer;
|
|
|
|
int i;
|
|
|
|
name = buffer;
|
|
|
|
for (i = 0; i < NCH(n); i += 2) {
|
|
|
|
char *s = STR(CHILD(n, i));
|
|
|
|
if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_MemoryError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"dotted_name too long");
|
1995-02-17 11:04:57 -04:00
|
|
|
name = NULL;
|
1995-01-07 08:41:23 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p != buffer)
|
|
|
|
*p++ = '.';
|
|
|
|
strcpy(p, s);
|
|
|
|
p = strchr(p, '\0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
REQ(n, NAME);
|
|
|
|
name = STR(n);
|
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_name(c, op, name);
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
2002-08-15 22:57:32 -03:00
|
|
|
parsenumber(struct compiling *c, char *s)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1991-12-10 09:53:51 -04:00
|
|
|
char *end;
|
1990-11-18 13:27:39 -04:00
|
|
|
long x;
|
1997-02-14 18:58:07 -04:00
|
|
|
double dx;
|
1996-01-11 21:13:16 -04:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
|
|
|
int imflag;
|
|
|
|
#endif
|
|
|
|
|
1991-04-04 06:42:56 -04:00
|
|
|
errno = 0;
|
1991-12-10 09:53:51 -04:00
|
|
|
end = s + strlen(s) - 1;
|
1996-01-11 21:13:16 -04:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
1996-01-26 16:53:56 -04:00
|
|
|
imflag = *end == 'j' || *end == 'J';
|
1996-01-11 21:13:16 -04:00
|
|
|
#endif
|
1992-03-27 13:28:26 -04:00
|
|
|
if (*end == 'l' || *end == 'L')
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyLong_FromString(s, (char **)0, 0);
|
2002-08-11 01:24:12 -03:00
|
|
|
if (s[0] == '0') {
|
1997-04-29 17:08:16 -03:00
|
|
|
x = (long) PyOS_strtoul(s, &end, 0);
|
2002-08-11 01:24:12 -03:00
|
|
|
if (x < 0 && errno == 0) {
|
2003-11-29 19:52:13 -04:00
|
|
|
return PyLong_FromString(s, (char **)0, 0);
|
2002-08-11 01:24:12 -03:00
|
|
|
}
|
|
|
|
}
|
1992-01-19 12:33:51 -04:00
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
x = PyOS_strtol(s, &end, 0);
|
1991-04-04 06:42:56 -04:00
|
|
|
if (*end == '\0') {
|
2001-08-27 16:45:25 -03:00
|
|
|
if (errno != 0)
|
|
|
|
return PyLong_FromString(s, (char **)0, 0);
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyInt_FromLong(x);
|
1991-04-04 06:42:56 -04:00
|
|
|
}
|
1994-08-29 09:16:12 -03:00
|
|
|
/* XXX Huge floats may silently fail */
|
1996-01-11 21:13:16 -04:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
|
|
|
if (imflag) {
|
2002-08-15 22:57:32 -03:00
|
|
|
Py_complex z;
|
|
|
|
z.real = 0.;
|
1997-02-14 18:58:07 -04:00
|
|
|
PyFPE_START_PROTECT("atof", return 0)
|
2004-06-08 15:52:54 -03:00
|
|
|
z.imag = PyOS_ascii_atof(s);
|
2002-08-15 22:57:32 -03:00
|
|
|
PyFPE_END_PROTECT(z)
|
|
|
|
return PyComplex_FromCComplex(z);
|
1996-01-11 21:13:16 -04:00
|
|
|
}
|
1997-10-08 12:23:55 -03:00
|
|
|
else
|
1996-01-11 21:13:16 -04:00
|
|
|
#endif
|
1997-10-08 12:23:55 -03:00
|
|
|
{
|
1997-02-14 18:58:07 -04:00
|
|
|
PyFPE_START_PROTECT("atof", return 0)
|
2004-06-08 15:52:54 -03:00
|
|
|
dx = PyOS_ascii_atof(s);
|
1997-03-14 00:32:50 -04:00
|
|
|
PyFPE_END_PROTECT(dx)
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyFloat_FromDouble(dx);
|
1997-02-14 18:58:07 -04:00
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
2002-08-04 14:29:52 -03:00
|
|
|
static PyObject *
|
|
|
|
decode_utf8(char **sPtr, char *end, char* encoding)
|
|
|
|
{
|
2002-08-07 09:33:18 -03:00
|
|
|
#ifndef Py_USING_UNICODE
|
2002-08-07 12:18:57 -03:00
|
|
|
Py_FatalError("decode_utf8 should not be called in this build.");
|
|
|
|
return NULL;
|
2002-08-07 09:33:18 -03:00
|
|
|
#else
|
2002-08-04 14:29:52 -03:00
|
|
|
PyObject *u, *v;
|
|
|
|
char *s, *t;
|
|
|
|
t = s = *sPtr;
|
|
|
|
/* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
|
|
|
|
while (s < end && (*s & 0x80)) s++;
|
|
|
|
*sPtr = s;
|
|
|
|
u = PyUnicode_DecodeUTF8(t, s - t, NULL);
|
|
|
|
if (u == NULL)
|
|
|
|
return NULL;
|
|
|
|
v = PyUnicode_AsEncodedString(u, encoding, NULL);
|
|
|
|
Py_DECREF(u);
|
|
|
|
return v;
|
2002-08-07 09:33:18 -03:00
|
|
|
#endif
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
2002-12-31 14:17:44 -04:00
|
|
|
/* compiler.transformer.Transformer.decode_literal depends on what
|
|
|
|
might seem like minor details of this function -- changes here
|
|
|
|
must be reflected there. */
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
2002-08-15 22:57:32 -03:00
|
|
|
parsestr(struct compiling *c, char *s)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
2000-06-28 19:07:35 -03:00
|
|
|
size_t len;
|
2002-08-14 04:46:28 -03:00
|
|
|
int quote = *s;
|
2000-03-10 19:01:36 -04:00
|
|
|
int rawmode = 0;
|
2002-08-15 22:57:32 -03:00
|
|
|
char* encoding = ((c == NULL) ? NULL : c->c_encoding);
|
2002-08-04 14:29:52 -03:00
|
|
|
int need_encoding;
|
2000-03-10 19:01:36 -04:00
|
|
|
int unicode = 0;
|
2002-05-28 15:47:29 -03:00
|
|
|
|
2000-03-10 19:01:36 -04:00
|
|
|
if (isalpha(quote) || quote == '_') {
|
|
|
|
if (quote == 'u' || quote == 'U') {
|
|
|
|
quote = *++s;
|
|
|
|
unicode = 1;
|
|
|
|
}
|
|
|
|
if (quote == 'r' || quote == 'R') {
|
|
|
|
quote = *++s;
|
|
|
|
rawmode = 1;
|
|
|
|
}
|
|
|
|
}
|
1993-10-26 12:19:44 -03:00
|
|
|
if (quote != '\'' && quote != '\"') {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_BadInternalCall();
|
1990-11-18 13:27:39 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
len = strlen(s);
|
2000-06-28 19:07:35 -03:00
|
|
|
if (len > INT_MAX) {
|
2002-08-15 22:57:32 -03:00
|
|
|
com_error(c, PyExc_OverflowError,
|
2002-03-03 17:30:27 -04:00
|
|
|
"string to parse is too long");
|
2000-06-28 19:07:35 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1993-10-26 12:19:44 -03:00
|
|
|
if (s[--len] != quote) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_BadInternalCall();
|
1990-11-18 13:27:39 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-29 09:16:12 -03:00
|
|
|
if (len >= 4 && s[0] == quote && s[1] == quote) {
|
|
|
|
s += 2;
|
|
|
|
len -= 2;
|
|
|
|
if (s[--len] != quote || s[--len] != quote) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_BadInternalCall();
|
1994-08-29 09:16:12 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2000-05-01 14:54:56 -03:00
|
|
|
if (unicode || Py_UnicodeFlag) {
|
2002-08-04 14:29:52 -03:00
|
|
|
PyObject *u, *w;
|
2002-11-21 16:13:40 -04:00
|
|
|
char *buf;
|
|
|
|
char *p;
|
|
|
|
char *end;
|
2002-08-04 14:29:52 -03:00
|
|
|
if (encoding == NULL) {
|
|
|
|
buf = s;
|
|
|
|
u = NULL;
|
|
|
|
} else if (strcmp(encoding, "iso-8859-1") == 0) {
|
|
|
|
buf = s;
|
|
|
|
u = NULL;
|
|
|
|
} else {
|
|
|
|
/* "\XX" may become "\u005c\uHHLL" (12 bytes) */
|
|
|
|
u = PyString_FromStringAndSize((char *)NULL, len * 4);
|
|
|
|
if (u == NULL)
|
|
|
|
return NULL;
|
|
|
|
p = buf = PyString_AsString(u);
|
|
|
|
end = s + len;
|
|
|
|
while (s < end) {
|
|
|
|
if (*s == '\\') {
|
|
|
|
*p++ = *s++;
|
|
|
|
if (*s & 0x80) {
|
|
|
|
strcpy(p, "u005c");
|
|
|
|
p += 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*s & 0x80) { /* XXX inefficient */
|
|
|
|
char *r;
|
|
|
|
int rn, i;
|
|
|
|
w = decode_utf8(&s, end, "utf-16-be");
|
|
|
|
if (w == NULL) {
|
|
|
|
Py_DECREF(u);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
r = PyString_AsString(w);
|
|
|
|
rn = PyString_Size(w);
|
|
|
|
assert(rn % 2 == 0);
|
|
|
|
for (i = 0; i < rn; i += 2) {
|
|
|
|
sprintf(p, "\\u%02x%02x",
|
|
|
|
r[i + 0] & 0xFF,
|
|
|
|
r[i + 1] & 0xFF);
|
|
|
|
p += 6;
|
|
|
|
}
|
|
|
|
Py_DECREF(w);
|
|
|
|
} else {
|
|
|
|
*p++ = *s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
len = p - buf;
|
|
|
|
}
|
2000-03-10 19:01:36 -04:00
|
|
|
if (rawmode)
|
2002-08-04 14:29:52 -03:00
|
|
|
v = PyUnicode_DecodeRawUnicodeEscape(buf, len, NULL);
|
2000-03-10 19:01:36 -04:00
|
|
|
else
|
2002-08-04 14:29:52 -03:00
|
|
|
v = PyUnicode_DecodeUnicodeEscape(buf, len, NULL);
|
|
|
|
Py_XDECREF(u);
|
2002-03-03 17:30:27 -04:00
|
|
|
if (v == NULL)
|
2002-08-15 22:57:32 -03:00
|
|
|
PyErr_SyntaxLocation(c->c_filename, c->c_lineno);
|
2002-03-03 17:30:27 -04:00
|
|
|
return v;
|
|
|
|
|
2000-03-10 19:01:36 -04:00
|
|
|
}
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2002-08-04 14:29:52 -03:00
|
|
|
need_encoding = (encoding != NULL &&
|
|
|
|
strcmp(encoding, "utf-8") != 0 &&
|
|
|
|
strcmp(encoding, "iso-8859-1") != 0);
|
|
|
|
if (rawmode || strchr(s, '\\') == NULL) {
|
|
|
|
if (need_encoding) {
|
2002-08-07 09:33:18 -03:00
|
|
|
#ifndef Py_USING_UNICODE
|
|
|
|
/* This should not happen - we never see any other
|
|
|
|
encoding. */
|
2002-08-07 12:18:57 -03:00
|
|
|
Py_FatalError("cannot deal with encodings in this build.");
|
2002-08-07 09:33:18 -03:00
|
|
|
#else
|
2002-08-04 14:29:52 -03:00
|
|
|
PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL);
|
|
|
|
if (u == NULL)
|
|
|
|
return NULL;
|
|
|
|
v = PyUnicode_AsEncodedString(u, encoding, NULL);
|
|
|
|
Py_DECREF(u);
|
|
|
|
return v;
|
2002-08-07 09:33:18 -03:00
|
|
|
#endif
|
2002-08-04 14:29:52 -03:00
|
|
|
} else {
|
|
|
|
return PyString_FromStringAndSize(s, len);
|
|
|
|
}
|
|
|
|
}
|
2002-08-14 04:46:28 -03:00
|
|
|
|
|
|
|
v = PyString_DecodeEscape(s, len, NULL, unicode,
|
|
|
|
need_encoding ? encoding : NULL);
|
2000-09-02 17:11:27 -03:00
|
|
|
if (v == NULL)
|
2002-08-15 22:57:32 -03:00
|
|
|
PyErr_SyntaxLocation(c->c_filename, c->c_lineno);
|
1990-11-18 13:27:39 -04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
2001-08-17 15:39:25 -03:00
|
|
|
parsestrplus(struct compiling* c, node *n)
|
1995-01-07 08:41:23 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1995-01-07 08:41:23 -04:00
|
|
|
int i;
|
|
|
|
REQ(CHILD(n, 0), STRING);
|
2001-08-17 15:39:25 -03:00
|
|
|
if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
|
1995-01-07 08:41:23 -04:00
|
|
|
/* String literal concatenation */
|
2000-04-13 11:10:44 -03:00
|
|
|
for (i = 1; i < NCH(n); i++) {
|
|
|
|
PyObject *s;
|
2001-08-17 15:39:25 -03:00
|
|
|
s = parsestr(c, STR(CHILD(n, i)));
|
2000-04-13 11:10:44 -03:00
|
|
|
if (s == NULL)
|
|
|
|
goto onError;
|
|
|
|
if (PyString_Check(v) && PyString_Check(s)) {
|
|
|
|
PyString_ConcatAndDel(&v, s);
|
|
|
|
if (v == NULL)
|
|
|
|
goto onError;
|
|
|
|
}
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2000-04-13 11:10:44 -03:00
|
|
|
else {
|
|
|
|
PyObject *temp;
|
|
|
|
temp = PyUnicode_Concat(v, s);
|
|
|
|
Py_DECREF(s);
|
|
|
|
if (temp == NULL)
|
|
|
|
goto onError;
|
|
|
|
Py_DECREF(v);
|
|
|
|
v = temp;
|
|
|
|
}
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
2000-04-13 11:10:44 -03:00
|
|
|
|
|
|
|
onError:
|
|
|
|
Py_XDECREF(v);
|
|
|
|
return NULL;
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-08-12 15:09:51 -03:00
|
|
|
com_list_for(struct compiling *c, node *n, node *e, char *t)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2000-08-12 15:09:51 -03:00
|
|
|
int anchor = 0;
|
|
|
|
int save_begin = c->c_begin;
|
|
|
|
|
2004-05-19 05:20:33 -03:00
|
|
|
/* list_for: for v in expr [list_iter] */
|
2000-08-12 15:09:51 -03:00
|
|
|
com_node(c, CHILD(n, 3)); /* expr */
|
2001-04-20 16:13:02 -03:00
|
|
|
com_addbyte(c, GET_ITER);
|
2000-08-12 15:09:51 -03:00
|
|
|
c->c_begin = c->c_nexti;
|
2001-04-20 16:13:02 -03:00
|
|
|
com_addfwref(c, FOR_ITER, &anchor);
|
2000-08-12 15:09:51 -03:00
|
|
|
com_push(c, 1);
|
2000-08-24 17:11:32 -03:00
|
|
|
com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
|
2000-08-12 15:09:51 -03:00
|
|
|
c->c_loops++;
|
|
|
|
com_list_iter(c, n, e, t);
|
|
|
|
c->c_loops--;
|
|
|
|
com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
|
|
|
|
c->c_begin = save_begin;
|
|
|
|
com_backpatch(c, anchor);
|
2001-04-20 16:13:02 -03:00
|
|
|
com_pop(c, 1); /* FOR_ITER has popped this */
|
2000-08-12 15:09:51 -03:00
|
|
|
}
|
|
|
|
|
2004-05-19 05:20:33 -03:00
|
|
|
static void
|
|
|
|
com_gen_for(struct compiling *c, node *n, node *t, int is_outmost)
|
|
|
|
{
|
|
|
|
int break_anchor = 0;
|
|
|
|
int anchor = 0;
|
|
|
|
int save_begin = c->c_begin;
|
|
|
|
|
|
|
|
REQ(n, gen_for);
|
|
|
|
/* gen_for: for v in test [gen_iter] */
|
|
|
|
|
|
|
|
com_addfwref(c, SETUP_LOOP, &break_anchor);
|
|
|
|
block_push(c, SETUP_LOOP);
|
|
|
|
|
|
|
|
if (is_outmost) {
|
|
|
|
com_addop_varname(c, VAR_LOAD, "[outmost-iterable]");
|
|
|
|
com_push(c, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
com_node(c, CHILD(n, 3));
|
|
|
|
com_addbyte(c, GET_ITER);
|
|
|
|
}
|
|
|
|
|
|
|
|
c->c_begin = c->c_nexti;
|
|
|
|
com_set_lineno(c, c->c_last_line);
|
|
|
|
com_addfwref(c, FOR_ITER, &anchor);
|
|
|
|
com_push(c, 1);
|
|
|
|
com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
|
|
|
|
|
|
|
|
if (NCH(n) == 5)
|
|
|
|
com_gen_iter(c, CHILD(n, 4), t);
|
|
|
|
else {
|
|
|
|
com_test(c, t);
|
|
|
|
com_addbyte(c, YIELD_VALUE);
|
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
|
|
|
|
c->c_begin = save_begin;
|
|
|
|
|
|
|
|
com_backpatch(c, anchor);
|
|
|
|
com_pop(c, 1); /* FOR_ITER has popped this */
|
|
|
|
com_addbyte(c, POP_BLOCK);
|
|
|
|
block_pop(c, SETUP_LOOP);
|
|
|
|
com_backpatch(c, break_anchor);
|
|
|
|
}
|
|
|
|
|
2000-08-12 15:09:51 -03:00
|
|
|
static void
|
|
|
|
com_list_if(struct compiling *c, node *n, node *e, char *t)
|
|
|
|
{
|
|
|
|
int anchor = 0;
|
|
|
|
int a = 0;
|
|
|
|
/* list_iter: 'if' test [list_iter] */
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &a);
|
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
com_pop(c, 1);
|
|
|
|
com_list_iter(c, n, e, t);
|
|
|
|
com_addfwref(c, JUMP_FORWARD, &anchor);
|
|
|
|
com_backpatch(c, a);
|
|
|
|
/* We jump here with an extra entry which we now pop */
|
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
com_backpatch(c, anchor);
|
|
|
|
}
|
|
|
|
|
2004-05-19 05:20:33 -03:00
|
|
|
static void
|
|
|
|
com_gen_if(struct compiling *c, node *n, node *t)
|
|
|
|
{
|
|
|
|
/* gen_if: 'if' test [gen_iter] */
|
|
|
|
int anchor = 0;
|
|
|
|
int a=0;
|
|
|
|
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &a);
|
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
com_pop(c, 1);
|
|
|
|
|
|
|
|
if (NCH(n) == 3)
|
|
|
|
com_gen_iter(c, CHILD(n, 2), t);
|
|
|
|
else {
|
|
|
|
com_test(c, t);
|
|
|
|
com_addbyte(c, YIELD_VALUE);
|
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
|
|
|
com_addfwref(c, JUMP_FORWARD, &anchor);
|
|
|
|
com_backpatch(c, a);
|
|
|
|
/* We jump here with an extra entry which we now pop */
|
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
com_backpatch(c, anchor);
|
|
|
|
}
|
|
|
|
|
2000-08-12 15:09:51 -03:00
|
|
|
static void
|
|
|
|
com_list_iter(struct compiling *c,
|
|
|
|
node *p, /* parent of list_iter node */
|
|
|
|
node *e, /* element expression node */
|
|
|
|
char *t /* name of result list temp local */)
|
|
|
|
{
|
|
|
|
/* list_iter is the last child in a listmaker, list_for, or list_if */
|
|
|
|
node *n = CHILD(p, NCH(p)-1);
|
|
|
|
if (TYPE(n) == list_iter) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
switch (TYPE(n)) {
|
|
|
|
case list_for:
|
|
|
|
com_list_for(c, n, e, t);
|
|
|
|
break;
|
|
|
|
case list_if:
|
|
|
|
com_list_if(c, n, e, t);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"invalid list_iter node type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_varname(c, VAR_LOAD, t);
|
2000-08-12 15:09:51 -03:00
|
|
|
com_push(c, 1);
|
|
|
|
com_node(c, e);
|
2004-03-07 03:31:06 -04:00
|
|
|
com_addbyte(c, LIST_APPEND);
|
2000-08-12 15:09:51 -03:00
|
|
|
com_pop(c, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-19 05:20:33 -03:00
|
|
|
static void
|
|
|
|
com_gen_iter(struct compiling *c, node *n, node *t)
|
|
|
|
{
|
|
|
|
/* gen_iter: gen_for | gen_if */
|
|
|
|
node *ch;
|
|
|
|
REQ(n, gen_iter);
|
|
|
|
|
|
|
|
ch = CHILD(n, 0);
|
|
|
|
|
|
|
|
switch (TYPE(ch)) {
|
|
|
|
case gen_for:
|
|
|
|
com_gen_for(c, ch, t, 0);
|
|
|
|
break;
|
|
|
|
case gen_if:
|
|
|
|
com_gen_if(c, ch, t);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"invalid gen_iter node type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-12 15:09:51 -03:00
|
|
|
static void
|
|
|
|
com_list_comprehension(struct compiling *c, node *n)
|
|
|
|
{
|
2000-08-21 23:43:07 -03:00
|
|
|
/* listmaker: test list_for */
|
2001-11-28 17:10:39 -04:00
|
|
|
char tmpname[30];
|
2003-05-21 14:34:50 -03:00
|
|
|
|
|
|
|
REQ(n, listmaker);
|
2001-11-28 17:10:39 -04:00
|
|
|
PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname);
|
2000-08-12 15:09:51 -03:00
|
|
|
com_addoparg(c, BUILD_LIST, 0);
|
|
|
|
com_addbyte(c, DUP_TOP); /* leave the result on the stack */
|
|
|
|
com_push(c, 2);
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_varname(c, VAR_STORE, tmpname);
|
2000-08-12 15:09:51 -03:00
|
|
|
com_pop(c, 1);
|
2000-08-21 23:43:07 -03:00
|
|
|
com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_varname(c, VAR_DELETE, tmpname);
|
2000-08-12 15:09:51 -03:00
|
|
|
--c->c_tmpname;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_listmaker(struct compiling *c, node *n)
|
|
|
|
{
|
2000-08-21 23:43:07 -03:00
|
|
|
/* listmaker: test ( list_for | (',' test)* [','] ) */
|
|
|
|
if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
|
2000-08-12 15:09:51 -03:00
|
|
|
com_list_comprehension(c, n);
|
|
|
|
else {
|
|
|
|
int len = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < NCH(n); i += 2, len++)
|
|
|
|
com_node(c, CHILD(n, i));
|
|
|
|
com_addoparg(c, BUILD_LIST, len);
|
|
|
|
com_pop(c, len-1);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
2004-05-19 05:20:33 -03:00
|
|
|
static void
|
|
|
|
com_generator_expression(struct compiling *c, node *n)
|
|
|
|
{
|
|
|
|
/* testlist_gexp: test gen_for */
|
|
|
|
/* argument: test gen_for */
|
|
|
|
PyCodeObject *co;
|
|
|
|
|
|
|
|
REQ(CHILD(n, 0), test);
|
|
|
|
REQ(CHILD(n, 1), gen_for);
|
|
|
|
|
|
|
|
symtable_enter_scope(c->c_symtable, "<genexpr>", TYPE(n),
|
|
|
|
n->n_lineno);
|
|
|
|
co = icompile(n, c);
|
|
|
|
symtable_exit_scope(c->c_symtable);
|
|
|
|
|
|
|
|
if (co == NULL)
|
|
|
|
c->c_errors++;
|
|
|
|
else {
|
|
|
|
int closure = com_make_closure(c, co);
|
|
|
|
int i = com_addconst(c, (PyObject *)co);
|
|
|
|
|
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
|
|
|
com_push(c, 1);
|
|
|
|
if (closure)
|
|
|
|
com_addoparg(c, MAKE_CLOSURE, 0);
|
|
|
|
else
|
|
|
|
com_addoparg(c, MAKE_FUNCTION, 0);
|
|
|
|
|
|
|
|
com_test(c, CHILD(CHILD(n, 1), 3));
|
|
|
|
com_addbyte(c, GET_ITER);
|
|
|
|
com_addoparg(c, CALL_FUNCTION, 1);
|
|
|
|
com_pop(c, 1);
|
|
|
|
|
|
|
|
Py_DECREF(co);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_testlist_gexp(struct compiling *c, node *n)
|
|
|
|
{
|
|
|
|
/* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
|
|
|
|
if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
|
|
|
|
com_generator_expression(c, n);
|
|
|
|
else com_list(c, n, 0);
|
|
|
|
}
|
|
|
|
|
2004-08-02 03:10:11 -03:00
|
|
|
|
1991-07-27 18:34:52 -03:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_dictmaker(struct compiling *c, node *n)
|
1991-07-27 18:34:52 -03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
/* dictmaker: test ':' test (',' test ':' value)* [','] */
|
|
|
|
for (i = 0; i+2 < NCH(n); i += 4) {
|
|
|
|
/* We must arrange things just right for STORE_SUBSCR.
|
|
|
|
It wants the stack to look like (value) (dict) (key) */
|
|
|
|
com_addbyte(c, DUP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1991-07-27 18:34:52 -03:00
|
|
|
com_node(c, CHILD(n, i)); /* key */
|
2002-12-16 09:54:02 -04:00
|
|
|
com_node(c, CHILD(n, i+2)); /* value */
|
|
|
|
com_addbyte(c, ROT_THREE);
|
1991-07-27 18:34:52 -03:00
|
|
|
com_addbyte(c, STORE_SUBSCR);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 3);
|
1991-07-27 18:34:52 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_atom(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
node *ch;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1990-11-18 13:27:39 -04:00
|
|
|
int i;
|
|
|
|
REQ(n, atom);
|
|
|
|
ch = CHILD(n, 0);
|
|
|
|
switch (TYPE(ch)) {
|
|
|
|
case LPAR:
|
1997-01-17 17:04:03 -04:00
|
|
|
if (TYPE(CHILD(n, 1)) == RPAR) {
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addoparg(c, BUILD_TUPLE, 0);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
else
|
2004-05-19 05:20:33 -03:00
|
|
|
com_testlist_gexp(c, CHILD(n, 1));
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
2000-08-12 15:09:51 -03:00
|
|
|
case LSQB: /* '[' [listmaker] ']' */
|
1997-01-17 17:04:03 -04:00
|
|
|
if (TYPE(CHILD(n, 1)) == RSQB) {
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addoparg(c, BUILD_LIST, 0);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
else
|
2000-08-12 15:09:51 -03:00
|
|
|
com_listmaker(c, CHILD(n, 1));
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
1991-07-27 18:34:52 -03:00
|
|
|
case LBRACE: /* '{' [dictmaker] '}' */
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addoparg(c, BUILD_MAP, 0);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
2000-08-12 15:09:51 -03:00
|
|
|
if (TYPE(CHILD(n, 1)) == dictmaker)
|
1991-07-27 18:34:52 -03:00
|
|
|
com_dictmaker(c, CHILD(n, 1));
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case BACKQUOTE:
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, UNARY_CONVERT);
|
|
|
|
break;
|
|
|
|
case NUMBER:
|
1996-09-17 11:32:04 -03:00
|
|
|
if ((v = parsenumber(c, STR(ch))) == NULL) {
|
1990-11-18 13:27:39 -04:00
|
|
|
i = 255;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = com_addconst(c, v);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case STRING:
|
2001-08-17 15:39:25 -03:00
|
|
|
v = parsestrplus(c, n);
|
1994-08-29 09:16:12 -03:00
|
|
|
if (v == NULL) {
|
1990-11-18 13:27:39 -04:00
|
|
|
c->c_errors++;
|
|
|
|
i = 255;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = com_addconst(c, v);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case NAME:
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_varname(c, VAR_LOAD, STR(ch));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"com_atom: unexpected node type");
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_slice(struct compiling *c, node *n, int op)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
com_addbyte(c, op);
|
|
|
|
}
|
|
|
|
else if (NCH(n) == 2) {
|
|
|
|
if (TYPE(CHILD(n, 0)) != COLON) {
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
com_addbyte(c, op+1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, op+2);
|
|
|
|
}
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
com_node(c, CHILD(n, 2));
|
|
|
|
com_addbyte(c, op+3);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 2);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-24 17:11:32 -03:00
|
|
|
static void
|
|
|
|
com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
|
|
|
|
{
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
com_addbyte(c, DUP_TOP);
|
|
|
|
com_push(c, 1);
|
|
|
|
com_addbyte(c, SLICE);
|
|
|
|
com_node(c, augn);
|
|
|
|
com_addbyte(c, opcode);
|
|
|
|
com_pop(c, 1);
|
|
|
|
com_addbyte(c, ROT_TWO);
|
|
|
|
com_addbyte(c, STORE_SLICE);
|
|
|
|
com_pop(c, 2);
|
|
|
|
} else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
com_addoparg(c, DUP_TOPX, 2);
|
|
|
|
com_push(c, 2);
|
|
|
|
com_addbyte(c, SLICE+1);
|
|
|
|
com_pop(c, 1);
|
|
|
|
com_node(c, augn);
|
|
|
|
com_addbyte(c, opcode);
|
|
|
|
com_pop(c, 1);
|
|
|
|
com_addbyte(c, ROT_THREE);
|
|
|
|
com_addbyte(c, STORE_SLICE+1);
|
|
|
|
com_pop(c, 3);
|
|
|
|
} else if (NCH(n) == 2) {
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addoparg(c, DUP_TOPX, 2);
|
|
|
|
com_push(c, 2);
|
|
|
|
com_addbyte(c, SLICE+2);
|
|
|
|
com_pop(c, 1);
|
|
|
|
com_node(c, augn);
|
|
|
|
com_addbyte(c, opcode);
|
|
|
|
com_pop(c, 1);
|
|
|
|
com_addbyte(c, ROT_THREE);
|
|
|
|
com_addbyte(c, STORE_SLICE+2);
|
|
|
|
com_pop(c, 3);
|
|
|
|
} else {
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
com_node(c, CHILD(n, 2));
|
|
|
|
com_addoparg(c, DUP_TOPX, 3);
|
|
|
|
com_push(c, 3);
|
|
|
|
com_addbyte(c, SLICE+3);
|
|
|
|
com_pop(c, 2);
|
|
|
|
com_node(c, augn);
|
|
|
|
com_addbyte(c, opcode);
|
|
|
|
com_pop(c, 1);
|
|
|
|
com_addbyte(c, ROT_FOUR);
|
|
|
|
com_addbyte(c, STORE_SLICE+3);
|
|
|
|
com_pop(c, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-01-06 18:56:52 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_argument(struct compiling *c, node *n, PyObject **pkeywords)
|
1995-07-07 19:53:21 -03:00
|
|
|
{
|
|
|
|
node *m;
|
2004-05-19 05:20:33 -03:00
|
|
|
REQ(n, argument); /* [test '='] test [gen_for]; really [keyword '='] test */
|
1995-07-07 19:53:21 -03:00
|
|
|
if (NCH(n) == 1) {
|
1997-01-06 18:56:52 -04:00
|
|
|
if (*pkeywords != NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
2000-08-15 12:49:44 -03:00
|
|
|
"non-keyword arg after keyword arg");
|
1995-07-07 19:53:21 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
}
|
1997-01-06 18:56:52 -04:00
|
|
|
return;
|
1995-07-07 19:53:21 -03:00
|
|
|
}
|
2004-05-19 05:20:33 -03:00
|
|
|
if (NCH(n) == 2) {
|
|
|
|
com_generator_expression(c, n);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1995-07-07 19:53:21 -03:00
|
|
|
m = n;
|
|
|
|
do {
|
|
|
|
m = CHILD(m, 0);
|
|
|
|
} while (NCH(m) == 1);
|
|
|
|
if (TYPE(m) != NAME) {
|
2001-02-18 00:45:10 -04:00
|
|
|
/* f(lambda x: x[0] = 3) ends up getting parsed with
|
|
|
|
* LHS test = lambda x: x[0], and RHS test = 3.
|
|
|
|
* SF bug 132313 points out that complaining about a keyword
|
|
|
|
* then is very confusing.
|
|
|
|
*/
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
2001-02-18 00:45:10 -04:00
|
|
|
TYPE(m) == lambdef ?
|
|
|
|
"lambda cannot contain assignment" :
|
|
|
|
"keyword can't be an expression");
|
1995-07-07 19:53:21 -03:00
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v = PyString_InternFromString(STR(m));
|
2002-08-15 23:24:56 -03:00
|
|
|
(void) none_assignment_check(c, STR(m), 1);
|
1997-01-06 18:56:52 -04:00
|
|
|
if (v != NULL && *pkeywords == NULL)
|
1997-04-29 17:08:16 -03:00
|
|
|
*pkeywords = PyDict_New();
|
2001-02-27 21:58:08 -04:00
|
|
|
if (v == NULL)
|
1995-07-07 19:53:21 -03:00
|
|
|
c->c_errors++;
|
2001-02-27 21:58:08 -04:00
|
|
|
else if (*pkeywords == NULL) {
|
|
|
|
c->c_errors++;
|
|
|
|
Py_DECREF(v);
|
|
|
|
} else {
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyDict_GetItem(*pkeywords, v) != NULL)
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
1997-01-06 18:56:52 -04:00
|
|
|
"duplicate keyword argument");
|
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyDict_SetItem(*pkeywords, v, v) != 0)
|
1997-01-06 18:56:52 -04:00
|
|
|
c->c_errors++;
|
1995-07-07 19:53:21 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, v));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1995-07-07 19:53:21 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
com_node(c, CHILD(n, 2));
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_call_function(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
if (TYPE(n) == RPAR) {
|
1995-07-07 19:53:21 -03:00
|
|
|
com_addoparg(c, CALL_FUNCTION, 0);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *keywords = NULL;
|
1997-01-06 18:56:52 -04:00
|
|
|
int i, na, nk;
|
1998-12-10 12:56:22 -04:00
|
|
|
int lineno = n->n_lineno;
|
2000-03-28 19:49:17 -04:00
|
|
|
int star_flag = 0;
|
|
|
|
int starstar_flag = 0;
|
|
|
|
int opcode;
|
1995-07-07 19:53:21 -03:00
|
|
|
REQ(n, arglist);
|
|
|
|
na = 0;
|
|
|
|
nk = 0;
|
|
|
|
for (i = 0; i < NCH(n); i += 2) {
|
1998-12-10 12:56:22 -04:00
|
|
|
node *ch = CHILD(n, i);
|
2000-03-28 19:49:17 -04:00
|
|
|
if (TYPE(ch) == STAR ||
|
|
|
|
TYPE(ch) == DOUBLESTAR)
|
|
|
|
break;
|
1998-12-10 12:56:22 -04:00
|
|
|
if (ch->n_lineno != lineno) {
|
|
|
|
lineno = ch->n_lineno;
|
2002-08-15 11:59:02 -03:00
|
|
|
com_set_lineno(c, lineno);
|
1998-12-10 12:56:22 -04:00
|
|
|
}
|
|
|
|
com_argument(c, ch, &keywords);
|
1997-01-06 18:56:52 -04:00
|
|
|
if (keywords == NULL)
|
1995-07-07 19:53:21 -03:00
|
|
|
na++;
|
|
|
|
else
|
|
|
|
nk++;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(keywords);
|
2000-03-28 19:49:17 -04:00
|
|
|
while (i < NCH(n)) {
|
|
|
|
node *tok = CHILD(n, i);
|
|
|
|
node *ch = CHILD(n, i+1);
|
|
|
|
i += 3;
|
|
|
|
switch (TYPE(tok)) {
|
|
|
|
case STAR: star_flag = 1; break;
|
|
|
|
case DOUBLESTAR: starstar_flag = 1; break;
|
|
|
|
}
|
|
|
|
com_node(c, ch);
|
|
|
|
}
|
1995-07-07 19:53:21 -03:00
|
|
|
if (na > 255 || nk > 255) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"more than 255 arguments");
|
1995-07-07 19:53:21 -03:00
|
|
|
}
|
2000-03-28 19:49:17 -04:00
|
|
|
if (star_flag || starstar_flag)
|
2000-03-28 20:10:44 -04:00
|
|
|
opcode = CALL_FUNCTION_VAR - 1 +
|
2000-03-28 19:49:17 -04:00
|
|
|
star_flag + (starstar_flag << 1);
|
|
|
|
else
|
|
|
|
opcode = CALL_FUNCTION;
|
|
|
|
com_addoparg(c, opcode, na | (nk << 8));
|
|
|
|
com_pop(c, na + 2*nk + star_flag + starstar_flag);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_select_member(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
com_addopname(c, LOAD_ATTR, n);
|
|
|
|
}
|
|
|
|
|
1996-07-30 13:49:37 -03:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_sliceobj(struct compiling *c, node *n)
|
1996-07-30 13:49:37 -03:00
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
int ns=2; /* number of slice arguments */
|
|
|
|
node *ch;
|
|
|
|
|
|
|
|
/* first argument */
|
|
|
|
if (TYPE(CHILD(n,i)) == COLON) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1996-07-30 13:49:37 -03:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
com_node(c, CHILD(n,i));
|
|
|
|
i++;
|
|
|
|
REQ(CHILD(n,i),COLON);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
/* second argument */
|
|
|
|
if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
|
|
|
|
com_node(c, CHILD(n,i));
|
|
|
|
i++;
|
|
|
|
}
|
1997-01-17 17:04:03 -04:00
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1996-07-30 13:49:37 -03:00
|
|
|
/* remaining arguments */
|
|
|
|
for (; i < NCH(n); i++) {
|
|
|
|
ns++;
|
|
|
|
ch=CHILD(n,i);
|
|
|
|
REQ(ch, sliceop);
|
|
|
|
if (NCH(ch) == 1) {
|
|
|
|
/* right argument of ':' missing */
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1996-07-30 13:49:37 -03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
com_node(c, CHILD(ch,1));
|
|
|
|
}
|
|
|
|
com_addoparg(c, BUILD_SLICE, ns);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1 + (ns == 3));
|
1996-07-30 13:49:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_subscript(struct compiling *c, node *n)
|
1996-07-30 13:49:37 -03:00
|
|
|
{
|
|
|
|
node *ch;
|
|
|
|
REQ(n, subscript);
|
|
|
|
ch = CHILD(n,0);
|
|
|
|
/* check for rubber index */
|
1997-01-17 17:04:03 -04:00
|
|
|
if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
|
1996-10-11 13:25:41 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1996-07-30 13:49:37 -03:00
|
|
|
else {
|
|
|
|
/* check for slice */
|
|
|
|
if ((TYPE(ch) == COLON || NCH(n) > 1))
|
|
|
|
com_sliceobj(c, n);
|
|
|
|
else {
|
|
|
|
REQ(ch, test);
|
|
|
|
com_node(c, ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-08-24 17:11:32 -03:00
|
|
|
com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
|
1996-07-30 13:49:37 -03:00
|
|
|
{
|
|
|
|
int i, op;
|
|
|
|
REQ(n, subscriptlist);
|
|
|
|
/* Check to make backward compatible slice behavior for '[i:j]' */
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
node *sub = CHILD(n, 0); /* subscript */
|
2000-08-05 18:37:50 -03:00
|
|
|
/* 'Basic' slice, should have exactly one colon. */
|
|
|
|
if ((TYPE(CHILD(sub, 0)) == COLON
|
|
|
|
|| (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
|
|
|
|
&& (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
|
|
|
|
{
|
2000-08-24 17:11:32 -03:00
|
|
|
switch (assigning) {
|
|
|
|
case OP_DELETE:
|
|
|
|
op = DELETE_SLICE;
|
|
|
|
break;
|
|
|
|
case OP_ASSIGN:
|
|
|
|
op = STORE_SLICE;
|
|
|
|
break;
|
|
|
|
case OP_APPLY:
|
1996-07-30 13:49:37 -03:00
|
|
|
op = SLICE;
|
2000-08-24 17:11:32 -03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
com_augassign_slice(c, sub, assigning, augn);
|
|
|
|
return;
|
|
|
|
}
|
1996-07-30 13:49:37 -03:00
|
|
|
com_slice(c, sub, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
if (op == STORE_SLICE)
|
|
|
|
com_pop(c, 2);
|
|
|
|
else if (op == DELETE_SLICE)
|
|
|
|
com_pop(c, 1);
|
1996-07-30 13:49:37 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Else normal subscriptlist. Compile each subscript. */
|
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_subscript(c, CHILD(n, i));
|
|
|
|
/* Put multiple subscripts into a tuple */
|
1997-01-17 17:04:03 -04:00
|
|
|
if (NCH(n) > 1) {
|
|
|
|
i = (NCH(n)+1) / 2;
|
|
|
|
com_addoparg(c, BUILD_TUPLE, i);
|
|
|
|
com_pop(c, i-1);
|
|
|
|
}
|
2000-08-24 17:11:32 -03:00
|
|
|
switch (assigning) {
|
|
|
|
case OP_DELETE:
|
|
|
|
op = DELETE_SUBSCR;
|
|
|
|
i = 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case OP_ASSIGN:
|
1997-01-17 17:04:03 -04:00
|
|
|
op = STORE_SUBSCR;
|
|
|
|
i = 3;
|
2000-08-24 17:11:32 -03:00
|
|
|
break;
|
|
|
|
case OP_APPLY:
|
|
|
|
op = BINARY_SUBSCR;
|
|
|
|
i = 1;
|
|
|
|
break;
|
1997-01-17 17:04:03 -04:00
|
|
|
}
|
2000-08-24 17:11:32 -03:00
|
|
|
if (assigning > OP_APPLY) {
|
|
|
|
com_addoparg(c, DUP_TOPX, 2);
|
|
|
|
com_push(c, 2);
|
|
|
|
com_addbyte(c, BINARY_SUBSCR);
|
|
|
|
com_pop(c, 1);
|
|
|
|
com_node(c, augn);
|
|
|
|
com_addbyte(c, assigning);
|
|
|
|
com_pop(c, 1);
|
|
|
|
com_addbyte(c, ROT_THREE);
|
1997-01-17 17:04:03 -04:00
|
|
|
}
|
1996-07-30 13:49:37 -03:00
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, i);
|
1996-07-30 13:49:37 -03:00
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_apply_trailer(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, trailer);
|
|
|
|
switch (TYPE(CHILD(n, 0))) {
|
|
|
|
case LPAR:
|
|
|
|
com_call_function(c, CHILD(n, 1));
|
|
|
|
break;
|
|
|
|
case DOT:
|
|
|
|
com_select_member(c, CHILD(n, 1));
|
|
|
|
break;
|
|
|
|
case LSQB:
|
2000-08-24 17:11:32 -03:00
|
|
|
com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_apply_trailer: unknown trailer type");
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-01-11 21:13:16 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_power(struct compiling *c, node *n)
|
1996-01-11 21:13:16 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
REQ(n, power);
|
|
|
|
com_atom(c, CHILD(n, 0));
|
|
|
|
for (i = 1; i < NCH(n); i++) {
|
|
|
|
if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
|
|
|
|
com_factor(c, CHILD(n, i+1));
|
|
|
|
com_addbyte(c, BINARY_POWER);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1996-01-11 21:13:16 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
com_apply_trailer(c, CHILD(n, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-11 22:54:38 -03:00
|
|
|
static void
|
|
|
|
com_invert_constant(struct compiling *c, node *n)
|
|
|
|
{
|
|
|
|
/* Compute the inverse of int and longs and use them directly,
|
|
|
|
but be prepared to generate code for all other
|
|
|
|
possibilities (invalid numbers, floats, complex).
|
|
|
|
*/
|
|
|
|
PyObject *num, *inv = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
REQ(n, NUMBER);
|
|
|
|
num = parsenumber(c, STR(n));
|
|
|
|
if (num == NULL)
|
|
|
|
i = 255;
|
|
|
|
else {
|
|
|
|
inv = PyNumber_Invert(num);
|
|
|
|
if (inv == NULL) {
|
|
|
|
PyErr_Clear();
|
|
|
|
i = com_addconst(c, num);
|
|
|
|
} else {
|
|
|
|
i = com_addconst(c, inv);
|
|
|
|
Py_DECREF(inv);
|
|
|
|
}
|
|
|
|
Py_DECREF(num);
|
|
|
|
}
|
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
|
|
|
com_push(c, 1);
|
|
|
|
if (num != NULL && inv == NULL)
|
|
|
|
com_addbyte(c, UNARY_INVERT);
|
|
|
|
}
|
|
|
|
|
2001-09-07 05:45:55 -03:00
|
|
|
static int
|
|
|
|
is_float_zero(const char *p)
|
|
|
|
{
|
|
|
|
int found_radix_point = 0;
|
|
|
|
int ch;
|
|
|
|
while ((ch = Py_CHARMASK(*p++)) != '\0') {
|
|
|
|
switch (ch) {
|
|
|
|
case '0':
|
|
|
|
/* no reason to believe it's not 0 -- continue */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e': case 'E': case 'j': case 'J':
|
|
|
|
/* If this was a hex constant, we already would have
|
|
|
|
returned 0 due to the 'x' or 'X', so 'e' or 'E'
|
|
|
|
must be an exponent marker, and we haven't yet
|
|
|
|
seen a non-zero digit, and it doesn't matter what
|
|
|
|
the exponent is then. For 'j' or 'J' similarly,
|
|
|
|
except that this is an imaginary 0 then. */
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case '.':
|
|
|
|
found_radix_point = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found_radix_point;
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_factor(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2001-08-11 22:54:38 -03:00
|
|
|
int childtype = TYPE(CHILD(n, 0));
|
2001-09-07 05:45:55 -03:00
|
|
|
node *pfactor, *ppower, *patom, *pnum;
|
1990-11-18 13:27:39 -04:00
|
|
|
REQ(n, factor);
|
2001-08-11 22:54:38 -03:00
|
|
|
/* If the unary +, -, or ~ operator is applied to a constant,
|
2001-09-07 05:45:55 -03:00
|
|
|
don't generate a UNARY_xxx opcode. Just store the
|
2001-08-11 22:54:38 -03:00
|
|
|
approriate value as a constant. If the value is negative,
|
|
|
|
extend the string containing the constant and insert a
|
2001-09-07 05:45:55 -03:00
|
|
|
negative in the 0th position -- unless we're doing unary minus
|
|
|
|
of a floating zero! In that case the sign is significant, but
|
|
|
|
the const dict can't distinguish +0.0 from -0.0.
|
2001-08-11 22:54:38 -03:00
|
|
|
*/
|
|
|
|
if ((childtype == PLUS || childtype == MINUS || childtype == TILDE)
|
2001-08-30 15:53:25 -03:00
|
|
|
&& NCH(n) == 2
|
2001-09-07 05:45:55 -03:00
|
|
|
&& TYPE((pfactor = CHILD(n, 1))) == factor
|
|
|
|
&& NCH(pfactor) == 1
|
|
|
|
&& TYPE((ppower = CHILD(pfactor, 0))) == power
|
|
|
|
&& NCH(ppower) == 1
|
|
|
|
&& TYPE((patom = CHILD(ppower, 0))) == atom
|
|
|
|
&& TYPE((pnum = CHILD(patom, 0))) == NUMBER
|
2003-02-12 12:57:47 -04:00
|
|
|
&& !(childtype == MINUS &&
|
|
|
|
(STR(pnum)[0] == '0' || is_float_zero(STR(pnum))))) {
|
2001-08-11 22:54:38 -03:00
|
|
|
if (childtype == TILDE) {
|
2001-09-07 05:45:55 -03:00
|
|
|
com_invert_constant(c, pnum);
|
2001-08-11 22:54:38 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (childtype == MINUS) {
|
2002-08-04 03:28:21 -03:00
|
|
|
char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
|
2001-08-11 22:54:38 -03:00
|
|
|
if (s == NULL) {
|
|
|
|
com_error(c, PyExc_MemoryError, "");
|
|
|
|
com_addbyte(c, 255);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s[0] = '-';
|
2001-09-07 05:45:55 -03:00
|
|
|
strcpy(s + 1, STR(pnum));
|
2002-08-04 03:28:21 -03:00
|
|
|
PyObject_FREE(STR(pnum));
|
2001-09-07 05:45:55 -03:00
|
|
|
STR(pnum) = s;
|
2001-08-11 22:54:38 -03:00
|
|
|
}
|
2001-09-07 05:45:55 -03:00
|
|
|
com_atom(c, patom);
|
2001-08-11 22:54:38 -03:00
|
|
|
}
|
|
|
|
else if (childtype == PLUS) {
|
1990-11-18 13:27:39 -04:00
|
|
|
com_factor(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, UNARY_POSITIVE);
|
|
|
|
}
|
2001-08-11 22:54:38 -03:00
|
|
|
else if (childtype == MINUS) {
|
1990-11-18 13:27:39 -04:00
|
|
|
com_factor(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, UNARY_NEGATIVE);
|
|
|
|
}
|
2001-08-11 22:54:38 -03:00
|
|
|
else if (childtype == TILDE) {
|
1991-10-24 11:59:31 -03:00
|
|
|
com_factor(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, UNARY_INVERT);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
else {
|
1996-01-11 21:13:16 -04:00
|
|
|
com_power(c, CHILD(n, 0));
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_term(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int op;
|
|
|
|
REQ(n, term);
|
|
|
|
com_factor(c, CHILD(n, 0));
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_factor(c, CHILD(n, i));
|
|
|
|
switch (TYPE(CHILD(n, i-1))) {
|
|
|
|
case STAR:
|
|
|
|
op = BINARY_MULTIPLY;
|
|
|
|
break;
|
|
|
|
case SLASH:
|
2001-08-08 02:00:18 -03:00
|
|
|
if (c->c_flags & CO_FUTURE_DIVISION)
|
|
|
|
op = BINARY_TRUE_DIVIDE;
|
|
|
|
else
|
|
|
|
op = BINARY_DIVIDE;
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case PERCENT:
|
|
|
|
op = BINARY_MODULO;
|
|
|
|
break;
|
2001-08-08 02:00:18 -03:00
|
|
|
case DOUBLESLASH:
|
|
|
|
op = BINARY_FLOOR_DIVIDE;
|
|
|
|
break;
|
1990-11-18 13:27:39 -04:00
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
2001-08-08 02:00:18 -03:00
|
|
|
"com_term: operator not *, /, // or %");
|
1990-11-18 13:27:39 -04:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_arith_expr(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int op;
|
1991-10-24 11:59:31 -03:00
|
|
|
REQ(n, arith_expr);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_term(c, CHILD(n, 0));
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_term(c, CHILD(n, i));
|
|
|
|
switch (TYPE(CHILD(n, i-1))) {
|
|
|
|
case PLUS:
|
|
|
|
op = BINARY_ADD;
|
|
|
|
break;
|
|
|
|
case MINUS:
|
|
|
|
op = BINARY_SUBTRACT;
|
|
|
|
break;
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_arith_expr: operator not + or -");
|
1991-10-24 11:59:31 -03:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_shift_expr(struct compiling *c, node *n)
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int op;
|
|
|
|
REQ(n, shift_expr);
|
|
|
|
com_arith_expr(c, CHILD(n, 0));
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_arith_expr(c, CHILD(n, i));
|
|
|
|
switch (TYPE(CHILD(n, i-1))) {
|
|
|
|
case LEFTSHIFT:
|
|
|
|
op = BINARY_LSHIFT;
|
|
|
|
break;
|
|
|
|
case RIGHTSHIFT:
|
|
|
|
op = BINARY_RSHIFT;
|
|
|
|
break;
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_shift_expr: operator not << or >>");
|
1991-10-24 11:59:31 -03:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_and_expr(struct compiling *c, node *n)
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int op;
|
|
|
|
REQ(n, and_expr);
|
|
|
|
com_shift_expr(c, CHILD(n, 0));
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_shift_expr(c, CHILD(n, i));
|
|
|
|
if (TYPE(CHILD(n, i-1)) == AMPER) {
|
|
|
|
op = BINARY_AND;
|
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_and_expr: operator not &");
|
1991-10-24 11:59:31 -03:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_xor_expr(struct compiling *c, node *n)
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int op;
|
|
|
|
REQ(n, xor_expr);
|
|
|
|
com_and_expr(c, CHILD(n, 0));
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_and_expr(c, CHILD(n, i));
|
|
|
|
if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
|
|
|
|
op = BINARY_XOR;
|
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_xor_expr: operator not ^");
|
1991-10-24 11:59:31 -03:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_expr(struct compiling *c, node *n)
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int op;
|
|
|
|
REQ(n, expr);
|
|
|
|
com_xor_expr(c, CHILD(n, 0));
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_xor_expr(c, CHILD(n, i));
|
|
|
|
if (TYPE(CHILD(n, i-1)) == VBAR) {
|
|
|
|
op = BINARY_OR;
|
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_expr: expr operator not |");
|
1990-11-18 13:27:39 -04:00
|
|
|
op = 255;
|
|
|
|
}
|
|
|
|
com_addbyte(c, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum cmp_op
|
2000-07-22 15:47:25 -03:00
|
|
|
cmp_type(node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, comp_op);
|
2003-05-12 16:16:52 -03:00
|
|
|
/* comp_op: '<' | '>' | '>=' | '<=' | '<>' | '!=' | '=='
|
1990-11-18 13:27:39 -04:00
|
|
|
| 'in' | 'not' 'in' | 'is' | 'is' not' */
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
switch (TYPE(n)) {
|
2002-01-01 15:59:11 -04:00
|
|
|
case LESS: return PyCmp_LT;
|
|
|
|
case GREATER: return PyCmp_GT;
|
2003-05-12 16:16:52 -03:00
|
|
|
case EQEQUAL: return PyCmp_EQ;
|
2002-01-01 15:59:11 -04:00
|
|
|
case LESSEQUAL: return PyCmp_LE;
|
|
|
|
case GREATEREQUAL: return PyCmp_GE;
|
|
|
|
case NOTEQUAL: return PyCmp_NE; /* <> or != */
|
|
|
|
case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN;
|
|
|
|
if (strcmp(STR(n), "is") == 0) return PyCmp_IS;
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (NCH(n) == 2) {
|
|
|
|
switch (TYPE(CHILD(n, 0))) {
|
|
|
|
case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
|
2002-01-01 15:59:11 -04:00
|
|
|
return PyCmp_NOT_IN;
|
1990-11-18 13:27:39 -04:00
|
|
|
if (strcmp(STR(CHILD(n, 0)), "is") == 0)
|
2002-01-01 15:59:11 -04:00
|
|
|
return PyCmp_IS_NOT;
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
2002-01-01 15:59:11 -04:00
|
|
|
return PyCmp_BAD;
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_comparison(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
enum cmp_op op;
|
|
|
|
int anchor;
|
|
|
|
REQ(n, comparison); /* comparison: expr (comp_op expr)* */
|
|
|
|
com_expr(c, CHILD(n, 0));
|
|
|
|
if (NCH(n) == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
The following code is generated for all but the last
|
|
|
|
comparison in a chain:
|
|
|
|
|
|
|
|
label: on stack: opcode: jump to:
|
|
|
|
|
|
|
|
a <code to load b>
|
|
|
|
a, b DUP_TOP
|
|
|
|
a, b, b ROT_THREE
|
|
|
|
b, a, b COMPARE_OP
|
|
|
|
b, 0-or-1 JUMP_IF_FALSE L1
|
|
|
|
b, 1 POP_TOP
|
|
|
|
b
|
|
|
|
|
|
|
|
We are now ready to repeat this sequence for the next
|
|
|
|
comparison in the chain.
|
|
|
|
|
|
|
|
For the last we generate:
|
|
|
|
|
|
|
|
b <code to load c>
|
|
|
|
b, c COMPARE_OP
|
|
|
|
0-or-1
|
|
|
|
|
|
|
|
If there were any jumps to L1 (i.e., there was more than one
|
|
|
|
comparison), we generate:
|
|
|
|
|
|
|
|
0-or-1 JUMP_FORWARD L2
|
|
|
|
L1: b, 0 ROT_TWO
|
|
|
|
0, b POP_TOP
|
|
|
|
0
|
1997-01-17 17:04:03 -04:00
|
|
|
L2: 0-or-1
|
1990-11-18 13:27:39 -04:00
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
anchor = 0;
|
|
|
|
|
|
|
|
for (i = 2; i < NCH(n); i += 2) {
|
|
|
|
com_expr(c, CHILD(n, i));
|
|
|
|
if (i+2 < NCH(n)) {
|
|
|
|
com_addbyte(c, DUP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, ROT_THREE);
|
|
|
|
}
|
|
|
|
op = cmp_type(CHILD(n, i-1));
|
2002-01-01 15:59:11 -04:00
|
|
|
if (op == PyCmp_BAD) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"com_comparison: unknown comparison op");
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
com_addoparg(c, COMPARE_OP, op);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
if (i+2 < NCH(n)) {
|
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &anchor);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anchor) {
|
|
|
|
int anchor2 = 0;
|
|
|
|
com_addfwref(c, JUMP_FORWARD, &anchor2);
|
|
|
|
com_backpatch(c, anchor);
|
|
|
|
com_addbyte(c, ROT_TWO);
|
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
com_backpatch(c, anchor2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_not_test(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, not_test); /* 'not' not_test | comparison */
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
com_comparison(c, CHILD(n, 0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
com_not_test(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, UNARY_NOT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_and_test(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int anchor;
|
|
|
|
REQ(n, and_test); /* not_test ('and' not_test)* */
|
|
|
|
anchor = 0;
|
|
|
|
i = 0;
|
|
|
|
for (;;) {
|
|
|
|
com_not_test(c, CHILD(n, i));
|
|
|
|
if ((i += 2) >= NCH(n))
|
|
|
|
break;
|
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &anchor);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
if (anchor)
|
|
|
|
com_backpatch(c, anchor);
|
|
|
|
}
|
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
static int
|
|
|
|
com_make_closure(struct compiling *c, PyCodeObject *co)
|
|
|
|
{
|
2001-12-13 15:51:56 -04:00
|
|
|
int i, free = PyCode_GetNumFree(co);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (free == 0)
|
|
|
|
return 0;
|
|
|
|
for (i = 0; i < free; ++i) {
|
|
|
|
/* Bypass com_addop_varname because it will generate
|
|
|
|
LOAD_DEREF but LOAD_CLOSURE is needed.
|
|
|
|
*/
|
|
|
|
PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
|
|
|
|
int arg, reftype;
|
|
|
|
|
|
|
|
/* Special case: If a class contains a method with a
|
|
|
|
free variable that has the same name as a method,
|
|
|
|
the name will be considered free *and* local in the
|
|
|
|
class. It should be handled by the closure, as
|
|
|
|
well as by the normal name loookup logic.
|
|
|
|
*/
|
|
|
|
reftype = get_ref_type(c, PyString_AS_STRING(name));
|
|
|
|
if (reftype == CELL)
|
|
|
|
arg = com_lookup_arg(c->c_cellvars, name);
|
|
|
|
else /* (reftype == FREE) */
|
|
|
|
arg = com_lookup_arg(c->c_freevars, name);
|
|
|
|
if (arg == -1) {
|
2001-03-01 02:09:34 -04:00
|
|
|
fprintf(stderr, "lookup %s in %s %d %d\n"
|
|
|
|
"freevars of %s: %s\n",
|
|
|
|
PyObject_REPR(name),
|
|
|
|
c->c_name,
|
|
|
|
reftype, arg,
|
|
|
|
PyString_AS_STRING(co->co_name),
|
|
|
|
PyObject_REPR(co->co_freevars));
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
Py_FatalError("com_make_closure()");
|
|
|
|
}
|
|
|
|
com_addoparg(c, LOAD_CLOSURE, arg);
|
|
|
|
|
|
|
|
}
|
|
|
|
com_push(c, free);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_test(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-01-17 17:04:03 -04:00
|
|
|
REQ(n, test); /* and_test ('or' and_test)* | lambdef */
|
1993-11-30 10:57:42 -04:00
|
|
|
if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
|
2001-10-17 10:22:22 -03:00
|
|
|
PyCodeObject *co;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
int i, closure;
|
1995-07-18 11:51:37 -03:00
|
|
|
int ndefs = com_argdefs(c, CHILD(n, 0));
|
2001-02-02 14:19:15 -04:00
|
|
|
symtable_enter_scope(c->c_symtable, "lambda", lambdef,
|
|
|
|
n->n_lineno);
|
2001-10-17 10:22:22 -03:00
|
|
|
co = icompile(CHILD(n, 0), c);
|
2001-02-09 18:22:18 -04:00
|
|
|
if (co == NULL) {
|
|
|
|
c->c_errors++;
|
|
|
|
return;
|
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
symtable_exit_scope(c->c_symtable);
|
2001-10-17 10:22:22 -03:00
|
|
|
i = com_addconst(c, (PyObject *)co);
|
|
|
|
closure = com_make_closure(c, co);
|
1993-11-30 10:57:42 -04:00
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
2001-10-17 10:22:22 -03:00
|
|
|
if (closure) {
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
com_addoparg(c, MAKE_CLOSURE, ndefs);
|
2001-12-13 15:51:56 -04:00
|
|
|
com_pop(c, PyCode_GetNumFree(co));
|
2001-10-17 10:22:22 -03:00
|
|
|
} else
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
com_addoparg(c, MAKE_FUNCTION, ndefs);
|
2001-10-17 10:22:22 -03:00
|
|
|
Py_DECREF(co);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, ndefs);
|
1993-11-30 10:57:42 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int anchor = 0;
|
|
|
|
int i = 0;
|
|
|
|
for (;;) {
|
|
|
|
com_and_test(c, CHILD(n, i));
|
|
|
|
if ((i += 2) >= NCH(n))
|
|
|
|
break;
|
|
|
|
com_addfwref(c, JUMP_IF_TRUE, &anchor);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1993-11-30 10:57:42 -04:00
|
|
|
}
|
|
|
|
if (anchor)
|
|
|
|
com_backpatch(c, anchor);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_list(struct compiling *c, node *n, int toplevel)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
/* exprlist: expr (',' expr)* [',']; likewise for testlist */
|
1991-12-16 09:05:10 -04:00
|
|
|
if (NCH(n) == 1 && !toplevel) {
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
len = (NCH(n) + 1) / 2;
|
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_node(c, CHILD(n, i));
|
|
|
|
com_addoparg(c, BUILD_TUPLE, len);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, len-1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Begin of assignment compilation */
|
|
|
|
|
2000-08-24 17:11:32 -03:00
|
|
|
|
|
|
|
static void
|
|
|
|
com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
|
|
|
|
{
|
|
|
|
com_addbyte(c, DUP_TOP);
|
|
|
|
com_push(c, 1);
|
|
|
|
com_addopname(c, LOAD_ATTR, n);
|
|
|
|
com_node(c, augn);
|
|
|
|
com_addbyte(c, opcode);
|
|
|
|
com_pop(c, 1);
|
|
|
|
com_addbyte(c, ROT_TWO);
|
|
|
|
com_addopname(c, STORE_ATTR, n);
|
|
|
|
com_pop(c, 2);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_assign_attr(struct compiling *c, node *n, int assigning)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2002-08-15 23:13:49 -03:00
|
|
|
if (none_assignment_check(c, STR(n), assigning))
|
|
|
|
return;
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, assigning ? 2 : 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-08-24 17:11:32 -03:00
|
|
|
com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, trailer);
|
|
|
|
switch (TYPE(CHILD(n, 0))) {
|
|
|
|
case LPAR: /* '(' [exprlist] ')' */
|
2002-05-31 11:08:29 -03:00
|
|
|
if (assigning == OP_DELETE)
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"can't delete function call");
|
|
|
|
else
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"can't assign to function call");
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case DOT: /* '.' NAME */
|
2000-08-24 17:11:32 -03:00
|
|
|
if (assigning > OP_APPLY)
|
|
|
|
com_augassign_attr(c, CHILD(n, 1), assigning, augn);
|
|
|
|
else
|
|
|
|
com_assign_attr(c, CHILD(n, 1), assigning);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
1996-07-30 13:49:37 -03:00
|
|
|
case LSQB: /* '[' subscriptlist ']' */
|
2000-08-24 17:11:32 -03:00
|
|
|
com_subscriptlist(c, CHILD(n, 1), assigning, augn);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError, "unknown trailer type");
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-08-11 19:15:52 -03:00
|
|
|
com_assign_sequence(struct compiling *c, node *n, int assigning)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
2004-05-19 05:20:33 -03:00
|
|
|
if (TYPE(n) != testlist && TYPE(n) != testlist_gexp &&
|
|
|
|
TYPE(n) != listmaker)
|
1990-11-18 13:27:39 -04:00
|
|
|
REQ(n, exprlist);
|
1997-01-17 17:04:03 -04:00
|
|
|
if (assigning) {
|
|
|
|
i = (NCH(n)+1)/2;
|
2000-08-11 19:15:52 -03:00
|
|
|
com_addoparg(c, UNPACK_SEQUENCE, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, i-1);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
2000-08-24 17:11:32 -03:00
|
|
|
com_assign(c, CHILD(n, i), assigning, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
|
|
|
|
{
|
|
|
|
REQ(n, NAME);
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_varname(c, VAR_LOAD, STR(n));
|
2000-08-24 17:11:32 -03:00
|
|
|
com_push(c, 1);
|
|
|
|
com_node(c, augn);
|
|
|
|
com_addbyte(c, opcode);
|
|
|
|
com_pop(c, 1);
|
|
|
|
com_assign_name(c, n, OP_ASSIGN);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_assign_name(struct compiling *c, node *n, int assigning)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, NAME);
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
|
1997-01-17 17:04:03 -04:00
|
|
|
if (assigning)
|
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-08-24 17:11:32 -03:00
|
|
|
com_assign(struct compiling *c, node *n, int assigning, node *augn)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
/* Loop to avoid trivial recursion */
|
|
|
|
for (;;) {
|
|
|
|
switch (TYPE(n)) {
|
1990-11-18 13:35:03 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
case exprlist:
|
|
|
|
case testlist:
|
2002-05-24 12:47:06 -03:00
|
|
|
case testlist1:
|
2004-05-19 05:20:33 -03:00
|
|
|
case testlist_gexp:
|
1990-11-18 13:27:39 -04:00
|
|
|
if (NCH(n) > 1) {
|
2004-05-19 05:20:33 -03:00
|
|
|
if (TYPE(CHILD(n, 1)) == gen_for) {
|
2004-09-29 18:47:10 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
2004-05-19 05:20:33 -03:00
|
|
|
"assign to generator expression not possible");
|
|
|
|
return;
|
|
|
|
}
|
2000-08-24 17:11:32 -03:00
|
|
|
if (assigning > OP_APPLY) {
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
2004-09-29 18:47:10 -03:00
|
|
|
"augmented assign to generator expression not possible");
|
2000-08-24 17:11:32 -03:00
|
|
|
return;
|
|
|
|
}
|
2000-08-11 19:15:52 -03:00
|
|
|
com_assign_sequence(c, n, assigning);
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
break;
|
1990-11-18 13:35:03 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
case test:
|
|
|
|
case and_test:
|
|
|
|
case not_test:
|
|
|
|
case comparison:
|
|
|
|
case expr:
|
1991-10-24 11:59:31 -03:00
|
|
|
case xor_expr:
|
|
|
|
case and_expr:
|
|
|
|
case shift_expr:
|
|
|
|
case arith_expr:
|
1990-11-18 13:27:39 -04:00
|
|
|
case term:
|
1996-01-11 21:13:16 -04:00
|
|
|
case factor:
|
1990-11-18 13:27:39 -04:00
|
|
|
if (NCH(n) > 1) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"can't assign to operator");
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
break;
|
1990-11-18 13:35:03 -04:00
|
|
|
|
2001-02-09 18:22:18 -04:00
|
|
|
case power: /* atom trailer* ('**' power)*
|
|
|
|
('+'|'-'|'~') factor | atom trailer* */
|
1996-01-11 21:13:16 -04:00
|
|
|
if (TYPE(CHILD(n, 0)) != atom) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"can't assign to operator");
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
}
|
1996-01-11 21:13:16 -04:00
|
|
|
if (NCH(n) > 1) { /* trailer or exponent present */
|
1990-11-18 13:27:39 -04:00
|
|
|
int i;
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
for (i = 1; i+1 < NCH(n); i++) {
|
1996-01-11 21:13:16 -04:00
|
|
|
if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"can't assign to operator");
|
1996-01-11 21:13:16 -04:00
|
|
|
return;
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
com_apply_trailer(c, CHILD(n, i));
|
|
|
|
} /* NB i is still alive */
|
|
|
|
com_assign_trailer(c,
|
2000-08-24 17:11:32 -03:00
|
|
|
CHILD(n, i), assigning, augn);
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
break;
|
1990-11-18 13:35:03 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
case atom:
|
|
|
|
switch (TYPE(CHILD(n, 0))) {
|
|
|
|
case LPAR:
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
if (TYPE(n) == RPAR) {
|
|
|
|
/* XXX Should allow () = () ??? */
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"can't assign to ()");
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
}
|
2000-08-24 17:11:32 -03:00
|
|
|
if (assigning > OP_APPLY) {
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
2004-09-29 20:54:08 -03:00
|
|
|
"augmented assign to tuple literal or generator expression not possible");
|
2000-08-24 17:11:32 -03:00
|
|
|
return;
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case LSQB:
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
if (TYPE(n) == RSQB) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"can't assign to []");
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
}
|
2000-08-24 17:11:32 -03:00
|
|
|
if (assigning > OP_APPLY) {
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
2004-09-29 20:54:08 -03:00
|
|
|
"augmented assign to list literal or comprehension not possible");
|
2001-01-18 23:21:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (NCH(n) > 1
|
|
|
|
&& TYPE(CHILD(n, 1)) == list_for) {
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
"can't assign to list comprehension");
|
2000-08-24 17:11:32 -03:00
|
|
|
return;
|
|
|
|
}
|
2000-08-11 19:15:52 -03:00
|
|
|
com_assign_sequence(c, n, assigning);
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
case NAME:
|
2000-08-24 17:11:32 -03:00
|
|
|
if (assigning > OP_APPLY)
|
|
|
|
com_augassign_name(c, CHILD(n, 0),
|
|
|
|
assigning, augn);
|
|
|
|
else
|
|
|
|
com_assign_name(c, CHILD(n, 0),
|
|
|
|
assigning);
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"can't assign to literal");
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
1996-08-08 15:51:04 -03:00
|
|
|
|
|
|
|
case lambdef:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"can't assign to lambda");
|
1996-08-08 15:51:04 -03:00
|
|
|
return;
|
1990-11-18 13:35:03 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"com_assign: bad node");
|
1990-11-18 13:27:39 -04:00
|
|
|
return;
|
1990-11-18 13:35:03 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1997-03-11 14:42:21 -04:00
|
|
|
|
2000-08-24 17:11:32 -03:00
|
|
|
static void
|
|
|
|
com_augassign(struct compiling *c, node *n)
|
|
|
|
{
|
|
|
|
int opcode;
|
|
|
|
|
|
|
|
switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
|
|
|
|
case '+': opcode = INPLACE_ADD; break;
|
|
|
|
case '-': opcode = INPLACE_SUBTRACT; break;
|
2001-08-08 02:00:18 -03:00
|
|
|
case '/':
|
|
|
|
if (STR(CHILD(CHILD(n, 1), 0))[1] == '/')
|
|
|
|
opcode = INPLACE_FLOOR_DIVIDE;
|
|
|
|
else if (c->c_flags & CO_FUTURE_DIVISION)
|
|
|
|
opcode = INPLACE_TRUE_DIVIDE;
|
|
|
|
else
|
|
|
|
opcode = INPLACE_DIVIDE;
|
|
|
|
break;
|
2000-08-24 17:11:32 -03:00
|
|
|
case '%': opcode = INPLACE_MODULO; break;
|
|
|
|
case '<': opcode = INPLACE_LSHIFT; break;
|
|
|
|
case '>': opcode = INPLACE_RSHIFT; break;
|
|
|
|
case '&': opcode = INPLACE_AND; break;
|
|
|
|
case '^': opcode = INPLACE_XOR; break;
|
|
|
|
case '|': opcode = INPLACE_OR; break;
|
|
|
|
case '*':
|
|
|
|
if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
|
|
|
|
opcode = INPLACE_POWER;
|
|
|
|
else
|
|
|
|
opcode = INPLACE_MULTIPLY;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
com_error(c, PyExc_SystemError, "com_augassign: bad operator");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_expr_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2000-08-24 17:11:32 -03:00
|
|
|
REQ(n, expr_stmt);
|
|
|
|
/* testlist (('=' testlist)* | augassign testlist) */
|
1997-01-17 17:04:03 -04:00
|
|
|
/* Forget it if we have just a doc string here */
|
1997-04-05 23:41:40 -04:00
|
|
|
if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
|
1997-01-17 17:04:03 -04:00
|
|
|
return;
|
2000-08-24 17:11:32 -03:00
|
|
|
if (NCH(n) == 1) {
|
|
|
|
com_node(c, CHILD(n, NCH(n)-1));
|
1994-08-29 09:16:12 -03:00
|
|
|
if (c->c_interactive)
|
|
|
|
com_addbyte(c, PRINT_EXPR);
|
|
|
|
else
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
2000-08-24 17:11:32 -03:00
|
|
|
else if (TYPE(CHILD(n,1)) == augassign)
|
|
|
|
com_augassign(c, n);
|
1990-11-18 13:27:39 -04:00
|
|
|
else {
|
|
|
|
int i;
|
2000-08-24 17:11:32 -03:00
|
|
|
com_node(c, CHILD(n, NCH(n)-1));
|
1991-07-27 18:34:52 -03:00
|
|
|
for (i = 0; i < NCH(n)-2; i+=2) {
|
1997-01-17 17:04:03 -04:00
|
|
|
if (i+2 < NCH(n)-2) {
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, DUP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
2000-08-24 17:11:32 -03:00
|
|
|
com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-04-02 01:24:36 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_assert_stmt(struct compiling *c, node *n)
|
1997-04-02 01:24:36 -04:00
|
|
|
{
|
2002-04-25 22:58:53 -03:00
|
|
|
int a = 0;
|
1997-04-02 01:24:36 -04:00
|
|
|
int i;
|
|
|
|
REQ(n, assert_stmt); /* 'assert' test [',' test] */
|
2002-04-25 22:58:53 -03:00
|
|
|
if (Py_OptimizeFlag)
|
|
|
|
return;
|
|
|
|
/* Generate code like
|
1997-04-02 01:24:36 -04:00
|
|
|
|
2002-04-25 22:58:53 -03:00
|
|
|
if not <test>:
|
1997-04-02 01:24:36 -04:00
|
|
|
raise AssertionError [, <message>]
|
|
|
|
|
|
|
|
where <message> is the second test, if present.
|
|
|
|
*/
|
|
|
|
com_node(c, CHILD(n, 1));
|
2002-04-25 22:58:53 -03:00
|
|
|
com_addfwref(c, JUMP_IF_TRUE, &a);
|
1997-04-02 01:24:36 -04:00
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
com_pop(c, 1);
|
|
|
|
/* Raise that exception! */
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_name(c, LOAD_GLOBAL, "AssertionError");
|
1997-04-02 01:24:36 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
i = NCH(n)/2; /* Either 2 or 4 */
|
|
|
|
if (i > 1)
|
|
|
|
com_node(c, CHILD(n, 3));
|
|
|
|
com_addoparg(c, RAISE_VARARGS, i);
|
|
|
|
com_pop(c, i);
|
|
|
|
/* The interpreter does not fall through */
|
2002-04-25 22:58:53 -03:00
|
|
|
/* Jump ends up here */
|
1997-04-02 01:24:36 -04:00
|
|
|
com_backpatch(c, a);
|
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_print_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2000-08-21 12:38:56 -03:00
|
|
|
int i = 1;
|
|
|
|
node* stream = NULL;
|
|
|
|
|
1991-07-27 18:34:52 -03:00
|
|
|
REQ(n, print_stmt); /* 'print' (test ',')* [test] */
|
2000-08-21 12:38:56 -03:00
|
|
|
|
|
|
|
/* are we using the extended print form? */
|
|
|
|
if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
|
|
|
|
stream = CHILD(n, 2);
|
2000-08-21 14:07:20 -03:00
|
|
|
com_node(c, stream);
|
|
|
|
/* stack: [...] => [... stream] */
|
|
|
|
com_push(c, 1);
|
2000-08-21 12:38:56 -03:00
|
|
|
if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
|
|
|
|
i = 4;
|
|
|
|
else
|
|
|
|
i = 3;
|
|
|
|
}
|
|
|
|
for (; i < NCH(n); i += 2) {
|
|
|
|
if (stream != NULL) {
|
2000-08-21 14:07:20 -03:00
|
|
|
com_addbyte(c, DUP_TOP);
|
|
|
|
/* stack: [stream] => [stream stream] */
|
|
|
|
com_push(c, 1);
|
2000-08-21 12:38:56 -03:00
|
|
|
com_node(c, CHILD(n, i));
|
2000-08-21 14:07:20 -03:00
|
|
|
/* stack: [stream stream] => [stream stream obj] */
|
|
|
|
com_addbyte(c, ROT_TWO);
|
|
|
|
/* stack: [stream stream obj] => [stream obj stream] */
|
2000-08-21 12:38:56 -03:00
|
|
|
com_addbyte(c, PRINT_ITEM_TO);
|
2000-08-21 14:07:20 -03:00
|
|
|
/* stack: [stream obj stream] => [stream] */
|
2000-08-21 12:38:56 -03:00
|
|
|
com_pop(c, 2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
com_node(c, CHILD(n, i));
|
2000-08-21 14:07:20 -03:00
|
|
|
/* stack: [...] => [... obj] */
|
2000-08-21 12:38:56 -03:00
|
|
|
com_addbyte(c, PRINT_ITEM);
|
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
|
2000-08-21 14:07:20 -03:00
|
|
|
if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
|
|
|
|
if (stream != NULL) {
|
|
|
|
/* must pop the extra stream object off the stack */
|
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
/* stack: [... stream] => [...] */
|
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2000-08-21 12:38:56 -03:00
|
|
|
if (stream != NULL) {
|
2000-08-21 14:07:20 -03:00
|
|
|
/* this consumes the last stream object on stack */
|
2000-08-21 12:38:56 -03:00
|
|
|
com_addbyte(c, PRINT_NEWLINE_TO);
|
2000-08-21 14:07:20 -03:00
|
|
|
/* stack: [... stream] => [...] */
|
2000-08-21 12:38:56 -03:00
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
com_addbyte(c, PRINT_NEWLINE);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_return_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1991-07-27 18:34:52 -03:00
|
|
|
REQ(n, return_stmt); /* 'return' [testlist] */
|
1990-12-20 11:06:42 -04:00
|
|
|
if (!c->c_infunction) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError, "'return' outside function");
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
2001-06-18 19:08:13 -03:00
|
|
|
if (c->c_flags & CO_GENERATOR) {
|
|
|
|
if (NCH(n) > 1) {
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"'return' with argument inside generator");
|
|
|
|
}
|
|
|
|
}
|
2001-06-23 03:19:16 -03:00
|
|
|
if (NCH(n) < 2) {
|
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
|
|
|
com_push(c, 1);
|
1997-01-17 17:04:03 -04:00
|
|
|
}
|
2001-06-23 03:19:16 -03:00
|
|
|
else
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, RETURN_VALUE);
|
2001-06-18 19:08:13 -03:00
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
com_yield_stmt(struct compiling *c, node *n)
|
|
|
|
{
|
2001-06-22 23:07:08 -03:00
|
|
|
int i;
|
2001-06-18 19:08:13 -03:00
|
|
|
REQ(n, yield_stmt); /* 'yield' testlist */
|
|
|
|
if (!c->c_infunction) {
|
|
|
|
com_error(c, PyExc_SyntaxError, "'yield' outside function");
|
|
|
|
}
|
2001-06-22 23:07:08 -03:00
|
|
|
|
|
|
|
for (i = 0; i < c->c_nblocks; ++i) {
|
|
|
|
if (c->c_block[i] == SETUP_FINALLY) {
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"'yield' not allowed in a 'try' block "
|
|
|
|
"with a 'finally' clause");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2001-06-18 19:08:13 -03:00
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addbyte(c, YIELD_VALUE);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_raise_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-01-17 17:04:03 -04:00
|
|
|
int i;
|
1998-04-09 18:39:57 -03:00
|
|
|
REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
|
|
|
|
if (NCH(n) > 1) {
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
if (NCH(n) > 3) {
|
|
|
|
com_node(c, CHILD(n, 3));
|
|
|
|
if (NCH(n) > 5)
|
|
|
|
com_node(c, CHILD(n, 5));
|
|
|
|
}
|
1995-07-07 19:53:21 -03:00
|
|
|
}
|
1997-01-17 17:04:03 -04:00
|
|
|
i = NCH(n)/2;
|
|
|
|
com_addoparg(c, RAISE_VARARGS, i);
|
|
|
|
com_pop(c, i);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
2000-08-17 19:55:00 -03:00
|
|
|
static void
|
|
|
|
com_from_import(struct compiling *c, node *n)
|
|
|
|
{
|
|
|
|
com_addopname(c, IMPORT_FROM, CHILD(n, 0));
|
|
|
|
com_push(c, 1);
|
|
|
|
if (NCH(n) > 1) {
|
|
|
|
if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
|
|
|
|
com_error(c, PyExc_SyntaxError, "invalid syntax");
|
|
|
|
return;
|
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
|
2000-08-17 19:55:00 -03:00
|
|
|
} else
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
|
2000-08-17 19:55:00 -03:00
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_import_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2004-08-31 07:07:13 -03:00
|
|
|
node *nn;
|
1990-11-18 13:27:39 -04:00
|
|
|
int i;
|
|
|
|
REQ(n, import_stmt);
|
2004-08-31 07:07:13 -03:00
|
|
|
n = CHILD(n, 0);
|
|
|
|
/* import_stmt: import_name | import_from */
|
|
|
|
if (TYPE(n) == import_from) {
|
|
|
|
/* 'from' dotted_name 'import' ('*' |
|
|
|
|
'(' import_as_names ')' | import_as_names) */
|
2000-11-27 18:22:36 -04:00
|
|
|
PyObject *tup;
|
1995-01-07 08:41:23 -04:00
|
|
|
REQ(CHILD(n, 1), dotted_name);
|
2004-08-31 07:07:13 -03:00
|
|
|
nn = CHILD(n, 3 + (TYPE(CHILD(n, 3)) == LPAR));
|
|
|
|
if (TYPE(nn) == STAR)
|
2000-08-27 17:31:27 -03:00
|
|
|
tup = Py_BuildValue("(s)", "*");
|
2004-08-31 07:07:13 -03:00
|
|
|
else {
|
|
|
|
if (TYPE(CHILD(nn, NCH(nn) - 1)) == COMMA &&
|
|
|
|
TYPE(CHILD(n, 3)) != LPAR) {
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"trailing comma not allowed "
|
|
|
|
"without surrounding parentheses");
|
|
|
|
return;
|
2000-08-27 17:31:27 -03:00
|
|
|
}
|
2004-08-31 07:07:13 -03:00
|
|
|
REQ(nn, import_as_names);
|
|
|
|
tup = PyTuple_New((NCH(nn) + 1) / 2);
|
2004-11-07 10:04:00 -04:00
|
|
|
for (i = 0; i < NCH(nn); i += 2) {
|
|
|
|
PyObject *s = PyString_FromString(
|
|
|
|
STR(CHILD(CHILD(nn, i), 0)));
|
|
|
|
if (s == NULL) {
|
|
|
|
Py_CLEAR(tup);
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
PyTuple_SET_ITEM(tup, i / 2, s);
|
|
|
|
}
|
|
|
|
if (tup == NULL) {
|
|
|
|
/* Assume that failue above was MemoryError */
|
|
|
|
com_error(c, PyExc_MemoryError, "");
|
|
|
|
return;
|
|
|
|
}
|
2000-08-27 17:31:27 -03:00
|
|
|
}
|
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
|
2000-11-27 18:22:36 -04:00
|
|
|
Py_DECREF(tup);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
2000-08-27 17:31:27 -03:00
|
|
|
com_addopname(c, IMPORT_NAME, CHILD(n, 1));
|
2004-08-31 07:07:13 -03:00
|
|
|
if (TYPE(nn) == STAR)
|
2000-08-17 19:55:00 -03:00
|
|
|
com_addbyte(c, IMPORT_STAR);
|
|
|
|
else {
|
2004-08-31 07:07:13 -03:00
|
|
|
for (i = 0; i < NCH(nn); i += 2)
|
|
|
|
com_from_import(c, CHILD(nn, i));
|
2000-08-17 19:55:00 -03:00
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
}
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
else {
|
2004-08-31 07:07:13 -03:00
|
|
|
/* 'import' dotted_as_names */
|
|
|
|
nn = CHILD(n, 1);
|
|
|
|
REQ(nn, dotted_as_names);
|
|
|
|
for (i = 0; i < NCH(nn); i += 2) {
|
|
|
|
node *subn = CHILD(nn, i);
|
2000-08-17 19:55:00 -03:00
|
|
|
REQ(subn, dotted_as_name);
|
2000-08-27 17:31:27 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
2000-08-27 17:31:27 -03:00
|
|
|
com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
|
2000-08-17 19:55:00 -03:00
|
|
|
if (NCH(subn) > 1) {
|
2000-08-27 17:16:32 -03:00
|
|
|
int j;
|
|
|
|
if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
|
2000-08-17 19:55:00 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"invalid syntax");
|
|
|
|
return;
|
|
|
|
}
|
2000-08-27 17:16:32 -03:00
|
|
|
for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
|
|
|
|
com_addopname(c, LOAD_ATTR,
|
2001-01-18 23:21:30 -04:00
|
|
|
CHILD(CHILD(subn, 0),
|
|
|
|
j));
|
|
|
|
com_addop_varname(c, VAR_STORE,
|
|
|
|
STR(CHILD(subn, 2)));
|
2000-08-17 19:55:00 -03:00
|
|
|
} else
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_varname(c, VAR_STORE,
|
|
|
|
STR(CHILD(CHILD(subn, 0),
|
|
|
|
0)));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-10-18 14:06:59 -03:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_exec_stmt(struct compiling *c, node *n)
|
1993-10-18 14:06:59 -03:00
|
|
|
{
|
|
|
|
REQ(n, exec_stmt);
|
|
|
|
/* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
|
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
if (NCH(n) >= 4)
|
|
|
|
com_node(c, CHILD(n, 3));
|
1997-01-17 17:04:03 -04:00
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1993-10-18 14:06:59 -03:00
|
|
|
if (NCH(n) >= 6)
|
|
|
|
com_node(c, CHILD(n, 5));
|
1997-01-17 17:04:03 -04:00
|
|
|
else {
|
1993-10-18 14:06:59 -03:00
|
|
|
com_addbyte(c, DUP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1993-10-18 14:06:59 -03:00
|
|
|
com_addbyte(c, EXEC_STMT);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 3);
|
1993-10-18 14:06:59 -03:00
|
|
|
}
|
|
|
|
|
1997-03-11 14:42:21 -04:00
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
is_constant_false(struct compiling *c, node *n)
|
1997-03-11 14:42:21 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1997-03-11 14:42:21 -04:00
|
|
|
int i;
|
2001-01-18 23:21:30 -04:00
|
|
|
/* argument c will be NULL when called from symtable_node() */
|
1997-03-11 14:42:21 -04:00
|
|
|
|
|
|
|
/* Label to avoid tail recursion */
|
|
|
|
next:
|
|
|
|
switch (TYPE(n)) {
|
|
|
|
|
|
|
|
case suite:
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
/* Fall through */
|
|
|
|
case file_input:
|
|
|
|
for (i = 0; i < NCH(n); i++) {
|
|
|
|
node *ch = CHILD(n, i);
|
|
|
|
if (TYPE(ch) == stmt) {
|
|
|
|
n = ch;
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case stmt:
|
|
|
|
case simple_stmt:
|
|
|
|
case small_stmt:
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
|
|
|
|
|
|
|
case expr_stmt:
|
|
|
|
case testlist:
|
2002-05-24 12:47:06 -03:00
|
|
|
case testlist1:
|
1997-03-11 14:42:21 -04:00
|
|
|
case test:
|
|
|
|
case and_test:
|
|
|
|
case not_test:
|
|
|
|
case comparison:
|
|
|
|
case expr:
|
|
|
|
case xor_expr:
|
|
|
|
case and_expr:
|
|
|
|
case shift_expr:
|
|
|
|
case arith_expr:
|
|
|
|
case term:
|
|
|
|
case factor:
|
|
|
|
case power:
|
|
|
|
case atom:
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NAME:
|
|
|
|
if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NUMBER:
|
|
|
|
v = parsenumber(c, STR(n));
|
|
|
|
if (v == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_Clear();
|
1997-03-11 14:42:21 -04:00
|
|
|
break;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
i = PyObject_IsTrue(v);
|
|
|
|
Py_DECREF(v);
|
1997-03-11 14:42:21 -04:00
|
|
|
return i == 0;
|
|
|
|
|
|
|
|
case STRING:
|
2001-08-17 15:39:25 -03:00
|
|
|
v = parsestr(c, STR(n));
|
1997-03-11 14:42:21 -04:00
|
|
|
if (v == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_Clear();
|
1997-03-11 14:42:21 -04:00
|
|
|
break;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
i = PyObject_IsTrue(v);
|
|
|
|
Py_DECREF(v);
|
1997-03-11 14:42:21 -04:00
|
|
|
return i == 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-06-27 22:52:22 -03:00
|
|
|
|
|
|
|
/* Look under n for a return stmt with an expression.
|
|
|
|
* This hack is used to find illegal returns under "if 0:" blocks in
|
|
|
|
* functions already known to be generators (as determined by the symtable
|
|
|
|
* pass).
|
|
|
|
* Return the offending return node if found, else NULL.
|
|
|
|
*/
|
|
|
|
static node *
|
|
|
|
look_for_offending_return(node *n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NCH(n); ++i) {
|
|
|
|
node *kid = CHILD(n, i);
|
|
|
|
|
|
|
|
switch (TYPE(kid)) {
|
|
|
|
case classdef:
|
|
|
|
case funcdef:
|
|
|
|
case lambdef:
|
|
|
|
/* Stuff in nested functions & classes doesn't
|
|
|
|
affect the code block we started in. */
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
case return_stmt:
|
|
|
|
if (NCH(kid) > 1)
|
|
|
|
return kid;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: {
|
|
|
|
node *bad = look_for_offending_return(kid);
|
|
|
|
if (bad != NULL)
|
|
|
|
return bad;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_if_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int anchor = 0;
|
|
|
|
REQ(n, if_stmt);
|
|
|
|
/*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
|
|
|
|
for (i = 0; i+3 < NCH(n); i+=4) {
|
|
|
|
int a = 0;
|
1990-12-20 11:06:42 -04:00
|
|
|
node *ch = CHILD(n, i+1);
|
2001-06-27 22:52:22 -03:00
|
|
|
if (is_constant_false(c, ch)) {
|
|
|
|
/* We're going to skip this block. However, if this
|
|
|
|
is a generator, we have to check the dead code
|
|
|
|
anyway to make sure there aren't any return stmts
|
|
|
|
with expressions, in the same scope. */
|
|
|
|
if (c->c_flags & CO_GENERATOR) {
|
|
|
|
node *p = look_for_offending_return(n);
|
|
|
|
if (p != NULL) {
|
|
|
|
int savelineno = c->c_lineno;
|
|
|
|
c->c_lineno = p->n_lineno;
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"'return' with argument "
|
|
|
|
"inside generator");
|
|
|
|
c->c_lineno = savelineno;
|
|
|
|
}
|
|
|
|
}
|
1997-03-11 14:42:21 -04:00
|
|
|
continue;
|
2001-06-27 22:52:22 -03:00
|
|
|
}
|
1990-12-20 11:06:42 -04:00
|
|
|
if (i > 0)
|
2002-08-15 11:59:02 -03:00
|
|
|
com_set_lineno(c, ch->n_lineno);
|
1997-03-11 14:42:21 -04:00
|
|
|
com_node(c, ch);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &a);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, i+3));
|
|
|
|
com_addfwref(c, JUMP_FORWARD, &anchor);
|
|
|
|
com_backpatch(c, a);
|
1997-01-17 17:04:03 -04:00
|
|
|
/* We jump here with an extra entry which we now pop */
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
}
|
|
|
|
if (i+2 < NCH(n))
|
|
|
|
com_node(c, CHILD(n, i+2));
|
1997-03-11 14:42:21 -04:00
|
|
|
if (anchor)
|
|
|
|
com_backpatch(c, anchor);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_while_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int break_anchor = 0;
|
|
|
|
int anchor = 0;
|
1991-07-27 18:34:52 -03:00
|
|
|
int save_begin = c->c_begin;
|
1990-11-18 13:27:39 -04:00
|
|
|
REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
|
|
|
|
com_addfwref(c, SETUP_LOOP, &break_anchor);
|
1991-07-27 18:34:52 -03:00
|
|
|
block_push(c, SETUP_LOOP);
|
|
|
|
c->c_begin = c->c_nexti;
|
2002-08-15 11:59:02 -03:00
|
|
|
com_set_lineno(c, n->n_lineno);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 1));
|
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &anchor);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_loops++;
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 3));
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_loops--;
|
1991-07-27 18:34:52 -03:00
|
|
|
com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
|
|
|
|
c->c_begin = save_begin;
|
1990-11-18 13:27:39 -04:00
|
|
|
com_backpatch(c, anchor);
|
1997-01-17 17:04:03 -04:00
|
|
|
/* We jump here with one entry more on the stack */
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, POP_TOP);
|
|
|
|
com_addbyte(c, POP_BLOCK);
|
1991-07-27 18:34:52 -03:00
|
|
|
block_pop(c, SETUP_LOOP);
|
1990-11-18 13:27:39 -04:00
|
|
|
if (NCH(n) > 4)
|
|
|
|
com_node(c, CHILD(n, 6));
|
|
|
|
com_backpatch(c, break_anchor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_for_stmt(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int break_anchor = 0;
|
|
|
|
int anchor = 0;
|
1991-07-27 18:34:52 -03:00
|
|
|
int save_begin = c->c_begin;
|
1990-11-18 13:27:39 -04:00
|
|
|
REQ(n, for_stmt);
|
|
|
|
/* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
|
|
|
|
com_addfwref(c, SETUP_LOOP, &break_anchor);
|
1991-07-27 18:34:52 -03:00
|
|
|
block_push(c, SETUP_LOOP);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 3));
|
2001-04-20 16:13:02 -03:00
|
|
|
com_addbyte(c, GET_ITER);
|
1991-07-27 18:34:52 -03:00
|
|
|
c->c_begin = c->c_nexti;
|
2003-04-29 14:07:36 -03:00
|
|
|
com_set_lineno(c, c->c_last_line);
|
2001-04-20 16:13:02 -03:00
|
|
|
com_addfwref(c, FOR_ITER, &anchor);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
2000-08-24 17:11:32 -03:00
|
|
|
com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_loops++;
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 5));
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_loops--;
|
1991-07-27 18:34:52 -03:00
|
|
|
com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
|
|
|
|
c->c_begin = save_begin;
|
1990-11-18 13:27:39 -04:00
|
|
|
com_backpatch(c, anchor);
|
2001-04-20 16:13:02 -03:00
|
|
|
com_pop(c, 1); /* FOR_ITER has popped this */
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, POP_BLOCK);
|
1991-07-27 18:34:52 -03:00
|
|
|
block_pop(c, SETUP_LOOP);
|
1990-11-18 13:27:39 -04:00
|
|
|
if (NCH(n) > 8)
|
|
|
|
com_node(c, CHILD(n, 8));
|
|
|
|
com_backpatch(c, break_anchor);
|
|
|
|
}
|
|
|
|
|
1994-08-29 09:16:12 -03:00
|
|
|
/* Code generated for "try: S finally: Sf" is as follows:
|
1990-11-18 13:35:03 -04:00
|
|
|
|
|
|
|
SETUP_FINALLY L
|
|
|
|
<code for S>
|
|
|
|
POP_BLOCK
|
|
|
|
LOAD_CONST <nil>
|
|
|
|
L: <code for Sf>
|
|
|
|
END_FINALLY
|
|
|
|
|
|
|
|
The special instructions use the block stack. Each block
|
|
|
|
stack entry contains the instruction that created it (here
|
|
|
|
SETUP_FINALLY), the level of the value stack at the time the
|
|
|
|
block stack entry was created, and a label (here L).
|
|
|
|
|
|
|
|
SETUP_FINALLY:
|
|
|
|
Pushes the current value stack level and the label
|
|
|
|
onto the block stack.
|
|
|
|
POP_BLOCK:
|
|
|
|
Pops en entry from the block stack, and pops the value
|
|
|
|
stack until its level is the same as indicated on the
|
|
|
|
block stack. (The label is ignored.)
|
|
|
|
END_FINALLY:
|
1990-12-20 11:06:42 -04:00
|
|
|
Pops a variable number of entries from the *value* stack
|
|
|
|
and re-raises the exception they specify. The number of
|
|
|
|
entries popped depends on the (pseudo) exception type.
|
1990-11-18 13:35:03 -04:00
|
|
|
|
|
|
|
The block stack is unwound when an exception is raised:
|
|
|
|
when a SETUP_FINALLY entry is found, the exception is pushed
|
|
|
|
onto the value stack (and the exception condition is cleared),
|
|
|
|
and the interpreter jumps to the label gotten from the block
|
|
|
|
stack.
|
|
|
|
|
|
|
|
Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
|
1990-12-20 11:06:42 -04:00
|
|
|
(The contents of the value stack is shown in [], with the top
|
|
|
|
at the right; 'tb' is trace-back info, 'val' the exception's
|
|
|
|
associated value, and 'exc' the exception.)
|
1990-11-18 13:35:03 -04:00
|
|
|
|
|
|
|
Value stack Label Instruction Argument
|
|
|
|
[] SETUP_EXCEPT L1
|
|
|
|
[] <code for S>
|
|
|
|
[] POP_BLOCK
|
|
|
|
[] JUMP_FORWARD L0
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
[tb, val, exc] L1: DUP )
|
|
|
|
[tb, val, exc, exc] <evaluate E1> )
|
|
|
|
[tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
|
|
|
|
[tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
|
|
|
|
[tb, val, exc, 1] POP )
|
|
|
|
[tb, val, exc] POP
|
|
|
|
[tb, val] <assign to V1> (or POP if no V1)
|
|
|
|
[tb] POP
|
1990-11-18 13:35:03 -04:00
|
|
|
[] <code for S1>
|
|
|
|
JUMP_FORWARD L0
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
[tb, val, exc, 0] L2: POP
|
|
|
|
[tb, val, exc] DUP
|
1990-11-18 13:35:03 -04:00
|
|
|
.............................etc.......................
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
[tb, val, exc, 0] Ln+1: POP
|
|
|
|
[tb, val, exc] END_FINALLY # re-raise exception
|
1990-11-18 13:35:03 -04:00
|
|
|
|
|
|
|
[] L0: <next statement>
|
|
|
|
|
|
|
|
Of course, parts are not generated if Vi or Ei is not present.
|
|
|
|
*/
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_try_except(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int except_anchor = 0;
|
1994-08-29 09:16:12 -03:00
|
|
|
int end_anchor = 0;
|
|
|
|
int else_anchor = 0;
|
|
|
|
int i;
|
|
|
|
node *ch;
|
1992-04-05 11:24:50 -03:00
|
|
|
|
1994-08-29 09:16:12 -03:00
|
|
|
com_addfwref(c, SETUP_EXCEPT, &except_anchor);
|
|
|
|
block_push(c, SETUP_EXCEPT);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 2));
|
1994-08-29 09:16:12 -03:00
|
|
|
com_addbyte(c, POP_BLOCK);
|
|
|
|
block_pop(c, SETUP_EXCEPT);
|
|
|
|
com_addfwref(c, JUMP_FORWARD, &else_anchor);
|
|
|
|
com_backpatch(c, except_anchor);
|
|
|
|
for (i = 3;
|
|
|
|
i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
|
|
|
|
i += 3) {
|
1997-01-17 17:04:03 -04:00
|
|
|
/* except_clause: 'except' [expr [',' var]] */
|
1994-08-29 09:16:12 -03:00
|
|
|
if (except_anchor == 0) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"default 'except:' must be last");
|
1994-08-29 09:16:12 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
except_anchor = 0;
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 3); /* tb, val, exc pushed by exception */
|
2002-08-15 11:59:02 -03:00
|
|
|
com_set_lineno(c, ch->n_lineno);
|
1994-08-29 09:16:12 -03:00
|
|
|
if (NCH(ch) > 1) {
|
|
|
|
com_addbyte(c, DUP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1994-08-29 09:16:12 -03:00
|
|
|
com_node(c, CHILD(ch, 1));
|
2002-01-01 15:59:11 -04:00
|
|
|
com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1994-08-29 09:16:12 -03:00
|
|
|
com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1994-08-29 09:16:12 -03:00
|
|
|
}
|
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1994-08-29 09:16:12 -03:00
|
|
|
if (NCH(ch) > 3)
|
2000-08-24 17:11:32 -03:00
|
|
|
com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
|
1997-01-17 17:04:03 -04:00
|
|
|
else {
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
1994-08-29 09:16:12 -03:00
|
|
|
com_addbyte(c, POP_TOP);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1994-08-29 09:16:12 -03:00
|
|
|
com_node(c, CHILD(n, i+2));
|
|
|
|
com_addfwref(c, JUMP_FORWARD, &end_anchor);
|
|
|
|
if (except_anchor) {
|
|
|
|
com_backpatch(c, except_anchor);
|
1997-01-17 17:04:03 -04:00
|
|
|
/* We come in with [tb, val, exc, 0] on the
|
|
|
|
stack; one pop and it's the same as
|
|
|
|
expected at the start of the loop */
|
1990-12-20 11:06:42 -04:00
|
|
|
com_addbyte(c, POP_TOP);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
1997-01-17 17:04:03 -04:00
|
|
|
/* We actually come in here with [tb, val, exc] but the
|
|
|
|
END_FINALLY will zap those and jump around.
|
|
|
|
The c_stacklevel does not reflect them so we need not pop
|
|
|
|
anything. */
|
1994-08-29 09:16:12 -03:00
|
|
|
com_addbyte(c, END_FINALLY);
|
|
|
|
com_backpatch(c, else_anchor);
|
|
|
|
if (i < NCH(n))
|
|
|
|
com_node(c, CHILD(n, i+2));
|
|
|
|
com_backpatch(c, end_anchor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_try_finally(struct compiling *c, node *n)
|
1994-08-29 09:16:12 -03:00
|
|
|
{
|
|
|
|
int finally_anchor = 0;
|
|
|
|
node *ch;
|
|
|
|
|
|
|
|
com_addfwref(c, SETUP_FINALLY, &finally_anchor);
|
|
|
|
block_push(c, SETUP_FINALLY);
|
|
|
|
com_node(c, CHILD(n, 2));
|
|
|
|
com_addbyte(c, POP_BLOCK);
|
|
|
|
block_pop(c, SETUP_FINALLY);
|
|
|
|
block_push(c, END_FINALLY);
|
1997-04-29 17:08:16 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
1997-01-17 17:04:03 -04:00
|
|
|
/* While the generated code pushes only one item,
|
|
|
|
the try-finally handling can enter here with
|
|
|
|
up to three items. OK, here are the details:
|
|
|
|
3 for an exception, 2 for RETURN, 1 for BREAK. */
|
|
|
|
com_push(c, 3);
|
1994-08-29 09:16:12 -03:00
|
|
|
com_backpatch(c, finally_anchor);
|
|
|
|
ch = CHILD(n, NCH(n)-1);
|
2002-08-15 11:59:02 -03:00
|
|
|
com_set_lineno(c, ch->n_lineno);
|
1994-08-29 09:16:12 -03:00
|
|
|
com_node(c, ch);
|
|
|
|
com_addbyte(c, END_FINALLY);
|
|
|
|
block_pop(c, END_FINALLY);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 3); /* Matches the com_push above */
|
1994-08-29 09:16:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_try_stmt(struct compiling *c, node *n)
|
1994-08-29 09:16:12 -03:00
|
|
|
{
|
|
|
|
REQ(n, try_stmt);
|
|
|
|
/* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
|
|
|
|
| 'try' ':' suite 'finally' ':' suite */
|
|
|
|
if (TYPE(CHILD(n, 3)) != except_clause)
|
|
|
|
com_try_finally(c, n);
|
|
|
|
else
|
|
|
|
com_try_except(c, n);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
1997-01-17 17:04:03 -04:00
|
|
|
static node *
|
2000-07-22 15:47:25 -03:00
|
|
|
get_rawdocstring(node *n)
|
1995-01-07 08:41:23 -04:00
|
|
|
{
|
1995-01-25 20:40:09 -04:00
|
|
|
int i;
|
|
|
|
|
1997-01-17 17:04:03 -04:00
|
|
|
/* Label to avoid tail recursion */
|
|
|
|
next:
|
1995-01-07 08:41:23 -04:00
|
|
|
switch (TYPE(n)) {
|
|
|
|
|
|
|
|
case suite:
|
1997-01-17 17:04:03 -04:00
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
1997-01-17 17:04:03 -04:00
|
|
|
/* Fall through */
|
1995-01-25 20:40:09 -04:00
|
|
|
case file_input:
|
|
|
|
for (i = 0; i < NCH(n); i++) {
|
|
|
|
node *ch = CHILD(n, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
if (TYPE(ch) == stmt) {
|
|
|
|
n = ch;
|
|
|
|
goto next;
|
|
|
|
}
|
1995-01-25 20:40:09 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1995-01-07 08:41:23 -04:00
|
|
|
case stmt:
|
|
|
|
case simple_stmt:
|
|
|
|
case small_stmt:
|
1997-01-17 17:04:03 -04:00
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
1995-01-07 08:41:23 -04:00
|
|
|
|
|
|
|
case expr_stmt:
|
|
|
|
case testlist:
|
2002-05-24 12:47:06 -03:00
|
|
|
case testlist1:
|
1995-01-07 08:41:23 -04:00
|
|
|
case test:
|
|
|
|
case and_test:
|
|
|
|
case not_test:
|
|
|
|
case comparison:
|
|
|
|
case expr:
|
|
|
|
case xor_expr:
|
|
|
|
case and_expr:
|
|
|
|
case shift_expr:
|
|
|
|
case arith_expr:
|
|
|
|
case term:
|
|
|
|
case factor:
|
1996-01-11 21:13:16 -04:00
|
|
|
case power:
|
1997-01-17 17:04:03 -04:00
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto next;
|
|
|
|
}
|
1995-01-07 08:41:23 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case atom:
|
|
|
|
if (TYPE(CHILD(n, 0)) == STRING)
|
1997-01-17 17:04:03 -04:00
|
|
|
return n;
|
1995-01-07 08:41:23 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
2001-08-17 15:39:25 -03:00
|
|
|
get_docstring(struct compiling *c, node *n)
|
1997-01-17 17:04:03 -04:00
|
|
|
{
|
1999-01-28 11:08:09 -04:00
|
|
|
/* Don't generate doc-strings if run with -OO */
|
|
|
|
if (Py_OptimizeFlag > 1)
|
|
|
|
return NULL;
|
1997-01-17 17:04:03 -04:00
|
|
|
n = get_rawdocstring(n);
|
|
|
|
if (n == NULL)
|
|
|
|
return NULL;
|
2001-08-17 15:39:25 -03:00
|
|
|
return parsestrplus(c, n);
|
1997-01-17 17:04:03 -04:00
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_suite(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, suite);
|
|
|
|
/* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
|
|
|
|
if (NCH(n) == 1) {
|
|
|
|
com_node(c, CHILD(n, 0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
2001-02-09 18:22:18 -04:00
|
|
|
for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
|
1990-11-18 13:27:39 -04:00
|
|
|
node *ch = CHILD(n, i);
|
|
|
|
if (TYPE(ch) == stmt)
|
|
|
|
com_node(c, ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1992-03-27 13:28:26 -04:00
|
|
|
/* ARGSUSED */
|
1991-07-27 18:34:52 -03:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_continue_stmt(struct compiling *c, node *n)
|
1991-07-27 18:34:52 -03:00
|
|
|
{
|
|
|
|
int i = c->c_nblocks;
|
|
|
|
if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
|
|
|
|
com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
|
|
|
|
}
|
2000-09-08 13:31:24 -03:00
|
|
|
else if (i <= 0) {
|
|
|
|
/* at the outer level */
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"'continue' not properly in loop");
|
|
|
|
}
|
1991-07-27 18:34:52 -03:00
|
|
|
else {
|
2000-09-08 13:31:24 -03:00
|
|
|
int j;
|
2001-02-01 18:48:12 -04:00
|
|
|
for (j = i-1; j >= 0; --j) {
|
2000-09-08 13:31:24 -03:00
|
|
|
if (c->c_block[j] == SETUP_LOOP)
|
|
|
|
break;
|
|
|
|
}
|
2001-02-01 18:48:12 -04:00
|
|
|
if (j >= 0) {
|
2000-09-08 13:31:24 -03:00
|
|
|
/* there is a loop, but something interferes */
|
2001-02-01 18:48:12 -04:00
|
|
|
for (; i > j; --i) {
|
|
|
|
if (c->c_block[i] == SETUP_EXCEPT ||
|
|
|
|
c->c_block[i] == SETUP_FINALLY) {
|
|
|
|
com_addoparg(c, CONTINUE_LOOP,
|
|
|
|
c->c_begin);
|
2000-09-08 13:31:24 -03:00
|
|
|
return;
|
|
|
|
}
|
2001-02-01 18:48:12 -04:00
|
|
|
if (c->c_block[i] == END_FINALLY) {
|
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"'continue' not supported inside 'finally' clause");
|
|
|
|
return;
|
|
|
|
}
|
2000-09-08 13:31:24 -03:00
|
|
|
}
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"'continue' not properly in loop");
|
1991-07-27 18:34:52 -03:00
|
|
|
}
|
|
|
|
/* XXX Could allow it inside a 'finally' clause
|
|
|
|
XXX if we could pop the exception still on the stack */
|
|
|
|
}
|
|
|
|
|
2003-08-28 11:42:14 -03:00
|
|
|
/* Return the number of default values in the argument list.
|
|
|
|
|
|
|
|
If a non-default argument follows a default argument, set an
|
|
|
|
exception and return -1.
|
|
|
|
*/
|
|
|
|
|
1994-08-29 09:16:12 -03:00
|
|
|
static int
|
2000-07-22 15:47:25 -03:00
|
|
|
com_argdefs(struct compiling *c, node *n)
|
1994-08-29 09:16:12 -03:00
|
|
|
{
|
2003-08-28 11:42:14 -03:00
|
|
|
int i, nch, ndefs;
|
1994-08-29 09:16:12 -03:00
|
|
|
if (TYPE(n) == lambdef) {
|
|
|
|
/* lambdef: 'lambda' [varargslist] ':' test */
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
}
|
|
|
|
else {
|
2004-08-02 03:10:11 -03:00
|
|
|
REQ(n, funcdef);
|
|
|
|
/* funcdef: [decorators] 'def' NAME parameters ':' suite */
|
|
|
|
n = RCHILD(n, -3);
|
1994-08-29 09:16:12 -03:00
|
|
|
REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
}
|
|
|
|
if (TYPE(n) != varargslist)
|
1995-07-18 11:51:37 -03:00
|
|
|
return 0;
|
1994-08-29 09:16:12 -03:00
|
|
|
/* varargslist:
|
1995-07-18 11:51:37 -03:00
|
|
|
(fpdef ['=' test] ',')* '*' ....... |
|
1994-08-29 09:16:12 -03:00
|
|
|
fpdef ['=' test] (',' fpdef ['=' test])* [','] */
|
|
|
|
nch = NCH(n);
|
|
|
|
ndefs = 0;
|
|
|
|
for (i = 0; i < nch; i++) {
|
|
|
|
int t;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (TYPE(CHILD(n, i)) == STAR ||
|
|
|
|
TYPE(CHILD(n, i)) == DOUBLESTAR)
|
1995-07-07 19:53:21 -03:00
|
|
|
break;
|
1994-08-29 09:16:12 -03:00
|
|
|
i++;
|
|
|
|
if (i >= nch)
|
1995-09-18 18:44:04 -03:00
|
|
|
t = RPAR; /* Anything except EQUAL or COMMA */
|
|
|
|
else
|
|
|
|
t = TYPE(CHILD(n, i));
|
1994-08-29 09:16:12 -03:00
|
|
|
if (t == EQUAL) {
|
|
|
|
i++;
|
|
|
|
ndefs++;
|
|
|
|
com_node(c, CHILD(n, i));
|
|
|
|
i++;
|
|
|
|
if (i >= nch)
|
|
|
|
break;
|
|
|
|
t = TYPE(CHILD(n, i));
|
|
|
|
}
|
|
|
|
else {
|
1998-10-02 10:41:54 -03:00
|
|
|
/* Treat "(a=1, b)" as an error */
|
2003-08-28 11:42:14 -03:00
|
|
|
if (ndefs) {
|
1998-10-02 10:41:54 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
1998-10-02 11:06:56 -03:00
|
|
|
"non-default argument follows default argument");
|
2003-08-28 11:42:14 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1994-08-29 09:16:12 -03:00
|
|
|
}
|
|
|
|
if (t != COMMA)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ndefs;
|
|
|
|
}
|
|
|
|
|
2004-08-02 03:10:11 -03:00
|
|
|
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));
|
2004-08-02 08:08:15 -03:00
|
|
|
com_push(c, 1);
|
2004-08-02 03:10:11 -03:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2004-08-17 14:29:16 -03:00
|
|
|
/* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
|
2004-08-02 03:10:11 -03:00
|
|
|
int nch = NCH(n);
|
2004-08-17 14:29:16 -03:00
|
|
|
assert(nch >= 3);
|
2004-08-02 03:10:11 -03:00
|
|
|
REQ(CHILD(n, 0), AT);
|
2004-08-17 14:29:16 -03:00
|
|
|
REQ(RCHILD(n, -1), NEWLINE);
|
2004-08-02 03:10:11 -03:00
|
|
|
com_decorator_name(c, CHILD(n, 1));
|
|
|
|
|
2004-08-17 14:29:16 -03:00
|
|
|
if (nch > 3) {
|
|
|
|
assert(nch == 5 || nch == 6);
|
2004-08-02 03:10:11 -03:00
|
|
|
REQ(CHILD(n, 2), LPAR);
|
2004-08-17 14:29:16 -03:00
|
|
|
REQ(RCHILD(n, -2), RPAR);
|
2004-08-02 03:10:11 -03:00
|
|
|
com_call_function(c, CHILD(n, 3));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
com_decorators(struct compiling *c, node *n)
|
|
|
|
{
|
2004-08-17 14:29:16 -03:00
|
|
|
int i, nch;
|
2004-08-02 03:10:11 -03:00
|
|
|
|
2004-08-17 14:29:16 -03:00
|
|
|
/* decorator+ */
|
2004-08-02 03:10:11 -03:00
|
|
|
nch = NCH(n);
|
2004-08-17 14:29:16 -03:00
|
|
|
assert(nch >= 1);
|
|
|
|
|
|
|
|
for (i = 0; i < nch; ++i) {
|
2004-08-02 03:10:11 -03:00
|
|
|
node *ch = CHILD(n, i);
|
2004-08-17 14:29:16 -03:00
|
|
|
REQ(ch, decorator);
|
|
|
|
|
|
|
|
com_decorator(c, ch);
|
2004-08-02 03:10:11 -03:00
|
|
|
}
|
|
|
|
|
2004-08-17 14:29:16 -03:00
|
|
|
return nch;
|
2004-08-02 03:10:11 -03:00
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_funcdef(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
PyObject *co;
|
2004-08-02 03:10:11 -03:00
|
|
|
int ndefs, ndecorators;
|
2004-08-17 14:29:16 -03:00
|
|
|
|
2004-08-02 03:10:11 -03:00
|
|
|
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;
|
2004-08-17 14:29:16 -03:00
|
|
|
|
2001-01-18 23:21:30 -04:00
|
|
|
ndefs = com_argdefs(c, n);
|
2003-08-28 11:42:14 -03:00
|
|
|
if (ndefs < 0)
|
|
|
|
return;
|
2004-08-02 03:10:11 -03:00
|
|
|
symtable_enter_scope(c->c_symtable, STR(RCHILD(n, -4)), TYPE(n),
|
2001-02-02 14:19:15 -04:00
|
|
|
n->n_lineno);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
co = (PyObject *)icompile(n, c);
|
2001-01-18 23:21:30 -04:00
|
|
|
symtable_exit_scope(c->c_symtable);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (co == NULL)
|
1990-11-18 13:27:39 -04:00
|
|
|
c->c_errors++;
|
|
|
|
else {
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
int closure = com_make_closure(c, (PyCodeObject *)co);
|
|
|
|
int i = com_addconst(c, co);
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (closure)
|
|
|
|
com_addoparg(c, MAKE_CLOSURE, ndefs);
|
|
|
|
else
|
|
|
|
com_addoparg(c, MAKE_FUNCTION, ndefs);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, ndefs);
|
2004-08-17 14:29:16 -03:00
|
|
|
|
2004-08-02 03:10:11 -03:00
|
|
|
while (ndecorators > 0) {
|
|
|
|
com_addoparg(c, CALL_FUNCTION, 1);
|
|
|
|
com_pop(c, 1);
|
2004-08-17 14:29:16 -03:00
|
|
|
--ndecorators;
|
2004-08-02 03:10:11 -03:00
|
|
|
}
|
2004-08-17 14:29:16 -03:00
|
|
|
|
2004-08-02 03:10:11 -03:00
|
|
|
com_addop_varname(c, VAR_STORE, STR(RCHILD(n, -4)));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
Py_DECREF(co);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:35:03 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_bases(struct compiling *c, node *n)
|
1991-12-10 09:53:51 -04:00
|
|
|
{
|
1992-03-27 13:28:26 -04:00
|
|
|
int i;
|
1991-12-10 09:53:51 -04:00
|
|
|
REQ(n, testlist);
|
|
|
|
/* testlist: test (',' test)* [','] */
|
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_node(c, CHILD(n, i));
|
1997-01-17 17:04:03 -04:00
|
|
|
i = (NCH(n)+1) / 2;
|
|
|
|
com_addoparg(c, BUILD_TUPLE, i);
|
|
|
|
com_pop(c, i-1);
|
1991-12-10 09:53:51 -04:00
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_classdef(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1993-05-19 11:50:45 -03:00
|
|
|
int i;
|
2001-10-17 10:22:22 -03:00
|
|
|
PyObject *v;
|
|
|
|
PyCodeObject *co;
|
2001-01-18 23:21:30 -04:00
|
|
|
char *name;
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
REQ(n, classdef);
|
2005-03-05 02:47:57 -04:00
|
|
|
/* classdef: class NAME ['(' [testlist] ')'] ':' suite */
|
1997-01-18 04:02:57 -04:00
|
|
|
if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
|
1993-05-19 11:50:45 -03:00
|
|
|
c->c_errors++;
|
|
|
|
return;
|
1991-12-10 09:53:51 -04:00
|
|
|
}
|
1993-05-19 11:50:45 -03:00
|
|
|
/* Push the class name on the stack */
|
|
|
|
i = com_addconst(c, v);
|
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1993-05-19 11:50:45 -03:00
|
|
|
/* Push the tuple of base classes on the stack */
|
2005-03-05 02:47:57 -04:00
|
|
|
if (TYPE(CHILD(n, 2)) != LPAR ||
|
|
|
|
TYPE(CHILD(n, 3)) == RPAR) {
|
1993-05-19 11:50:45 -03:00
|
|
|
com_addoparg(c, BUILD_TUPLE, 0);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
|
|
|
}
|
1993-05-19 11:50:45 -03:00
|
|
|
else
|
|
|
|
com_bases(c, CHILD(n, 3));
|
2001-01-18 23:21:30 -04:00
|
|
|
name = STR(CHILD(n, 1));
|
2001-02-02 14:19:15 -04:00
|
|
|
symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
|
2001-10-17 10:22:22 -03:00
|
|
|
co = icompile(n, c);
|
2001-01-18 23:21:30 -04:00
|
|
|
symtable_exit_scope(c->c_symtable);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (co == NULL)
|
1990-11-18 13:35:03 -04:00
|
|
|
c->c_errors++;
|
|
|
|
else {
|
2001-10-17 10:22:22 -03:00
|
|
|
int closure = com_make_closure(c, co);
|
|
|
|
i = com_addconst(c, (PyObject *)co);
|
1990-11-18 13:35:03 -04:00
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
2001-10-17 10:22:22 -03:00
|
|
|
if (closure) {
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
com_addoparg(c, MAKE_CLOSURE, 0);
|
2001-12-13 15:51:56 -04:00
|
|
|
com_pop(c, PyCode_GetNumFree(co));
|
2001-10-17 10:22:22 -03:00
|
|
|
} else
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
com_addoparg(c, MAKE_FUNCTION, 0);
|
1995-07-18 11:51:37 -03:00
|
|
|
com_addoparg(c, CALL_FUNCTION, 0);
|
1990-11-18 13:35:03 -04:00
|
|
|
com_addbyte(c, BUILD_CLASS);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 2);
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
|
2001-10-17 10:22:22 -03:00
|
|
|
com_pop(c, 1);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
Py_DECREF(co);
|
1990-11-18 13:35:03 -04:00
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_node(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2001-01-18 23:21:30 -04:00
|
|
|
loop:
|
2001-02-09 18:22:18 -04:00
|
|
|
if (c->c_errors)
|
|
|
|
return;
|
1990-11-18 13:27:39 -04:00
|
|
|
switch (TYPE(n)) {
|
|
|
|
|
|
|
|
/* Definition nodes */
|
2004-08-02 03:10:11 -03:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
case funcdef:
|
|
|
|
com_funcdef(c, n);
|
|
|
|
break;
|
|
|
|
case classdef:
|
|
|
|
com_classdef(c, n);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Trivial parse tree nodes */
|
|
|
|
|
|
|
|
case stmt:
|
1991-07-27 18:34:52 -03:00
|
|
|
case small_stmt:
|
1990-11-18 13:27:39 -04:00
|
|
|
case flow_stmt:
|
2001-01-18 23:21:30 -04:00
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto loop;
|
1990-12-20 11:06:42 -04:00
|
|
|
|
|
|
|
case simple_stmt:
|
1991-07-27 18:34:52 -03:00
|
|
|
/* small_stmt (';' small_stmt)* [';'] NEWLINE */
|
2002-08-15 11:59:02 -03:00
|
|
|
com_set_lineno(c, n->n_lineno);
|
1991-07-27 18:34:52 -03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < NCH(n)-1; i += 2)
|
|
|
|
com_node(c, CHILD(n, i));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
case compound_stmt:
|
2002-08-15 11:59:02 -03:00
|
|
|
com_set_lineno(c, n->n_lineno);
|
2001-01-18 23:21:30 -04:00
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto loop;
|
1990-11-18 13:27:39 -04:00
|
|
|
|
|
|
|
/* Statement nodes */
|
|
|
|
|
|
|
|
case expr_stmt:
|
|
|
|
com_expr_stmt(c, n);
|
|
|
|
break;
|
|
|
|
case print_stmt:
|
|
|
|
com_print_stmt(c, n);
|
|
|
|
break;
|
1991-07-27 18:34:52 -03:00
|
|
|
case del_stmt: /* 'del' exprlist */
|
2000-08-24 17:11:32 -03:00
|
|
|
com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case pass_stmt:
|
|
|
|
break;
|
|
|
|
case break_stmt:
|
1990-12-20 11:06:42 -04:00
|
|
|
if (c->c_loops == 0) {
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SyntaxError,
|
|
|
|
"'break' outside loop");
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
com_addbyte(c, BREAK_LOOP);
|
|
|
|
break;
|
1991-07-27 18:34:52 -03:00
|
|
|
case continue_stmt:
|
|
|
|
com_continue_stmt(c, n);
|
|
|
|
break;
|
1990-11-18 13:27:39 -04:00
|
|
|
case return_stmt:
|
|
|
|
com_return_stmt(c, n);
|
|
|
|
break;
|
2001-06-18 19:08:13 -03:00
|
|
|
case yield_stmt:
|
|
|
|
com_yield_stmt(c, n);
|
|
|
|
break;
|
1990-11-18 13:27:39 -04:00
|
|
|
case raise_stmt:
|
|
|
|
com_raise_stmt(c, n);
|
|
|
|
break;
|
|
|
|
case import_stmt:
|
|
|
|
com_import_stmt(c, n);
|
|
|
|
break;
|
1991-12-10 09:53:51 -04:00
|
|
|
case global_stmt:
|
|
|
|
break;
|
1993-10-18 14:06:59 -03:00
|
|
|
case exec_stmt:
|
|
|
|
com_exec_stmt(c, n);
|
|
|
|
break;
|
1997-04-02 01:24:36 -04:00
|
|
|
case assert_stmt:
|
|
|
|
com_assert_stmt(c, n);
|
|
|
|
break;
|
1990-11-18 13:27:39 -04:00
|
|
|
case if_stmt:
|
|
|
|
com_if_stmt(c, n);
|
|
|
|
break;
|
|
|
|
case while_stmt:
|
|
|
|
com_while_stmt(c, n);
|
|
|
|
break;
|
|
|
|
case for_stmt:
|
|
|
|
com_for_stmt(c, n);
|
|
|
|
break;
|
|
|
|
case try_stmt:
|
|
|
|
com_try_stmt(c, n);
|
|
|
|
break;
|
|
|
|
case suite:
|
|
|
|
com_suite(c, n);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Expression nodes */
|
|
|
|
|
|
|
|
case testlist:
|
2002-05-24 12:47:06 -03:00
|
|
|
case testlist1:
|
2001-10-15 12:44:05 -03:00
|
|
|
case testlist_safe:
|
1991-12-16 09:05:10 -04:00
|
|
|
com_list(c, n, 0);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case test:
|
|
|
|
com_test(c, n);
|
|
|
|
break;
|
|
|
|
case and_test:
|
|
|
|
com_and_test(c, n);
|
|
|
|
break;
|
|
|
|
case not_test:
|
|
|
|
com_not_test(c, n);
|
|
|
|
break;
|
|
|
|
case comparison:
|
|
|
|
com_comparison(c, n);
|
|
|
|
break;
|
|
|
|
case exprlist:
|
1991-12-16 09:05:10 -04:00
|
|
|
com_list(c, n, 0);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
case expr:
|
|
|
|
com_expr(c, n);
|
|
|
|
break;
|
1991-10-24 11:59:31 -03:00
|
|
|
case xor_expr:
|
|
|
|
com_xor_expr(c, n);
|
|
|
|
break;
|
|
|
|
case and_expr:
|
|
|
|
com_and_expr(c, n);
|
|
|
|
break;
|
|
|
|
case shift_expr:
|
|
|
|
com_shift_expr(c, n);
|
|
|
|
break;
|
|
|
|
case arith_expr:
|
|
|
|
com_arith_expr(c, n);
|
|
|
|
break;
|
1990-11-18 13:27:39 -04:00
|
|
|
case term:
|
|
|
|
com_term(c, n);
|
|
|
|
break;
|
|
|
|
case factor:
|
|
|
|
com_factor(c, n);
|
|
|
|
break;
|
1996-01-11 21:13:16 -04:00
|
|
|
case power:
|
|
|
|
com_power(c, n);
|
|
|
|
break;
|
1990-11-18 13:27:39 -04:00
|
|
|
case atom:
|
|
|
|
com_atom(c, n);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
|
|
|
"com_node: unexpected node type");
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-09 00:09:57 -03:00
|
|
|
static void com_fplist(struct compiling *, node *);
|
1990-11-18 13:27:39 -04:00
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_fpdef(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
|
|
|
|
if (TYPE(CHILD(n, 0)) == LPAR)
|
|
|
|
com_fplist(c, CHILD(n, 1));
|
1997-01-17 17:04:03 -04:00
|
|
|
else {
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_fplist(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1991-12-31 09:13:35 -04:00
|
|
|
REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
|
1990-11-18 13:27:39 -04:00
|
|
|
if (NCH(n) == 1) {
|
|
|
|
com_fpdef(c, CHILD(n, 0));
|
|
|
|
}
|
|
|
|
else {
|
1997-01-17 17:04:03 -04:00
|
|
|
int i = (NCH(n)+1)/2;
|
2000-08-11 19:15:52 -03:00
|
|
|
com_addoparg(c, UNPACK_SEQUENCE, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, i-1);
|
1990-11-18 13:27:39 -04:00
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
com_fpdef(c, CHILD(n, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1992-01-14 14:30:26 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_arglist(struct compiling *c, node *n)
|
1992-01-14 14:30:26 -04:00
|
|
|
{
|
2002-12-23 12:51:42 -04:00
|
|
|
int nch, i, narg;
|
1995-07-18 11:51:37 -03:00
|
|
|
int complex = 0;
|
2002-12-23 12:51:42 -04:00
|
|
|
char nbuf[30];
|
1992-01-14 14:30:26 -04:00
|
|
|
REQ(n, varargslist);
|
1992-01-19 12:33:51 -04:00
|
|
|
/* varargslist:
|
1995-07-18 11:51:37 -03:00
|
|
|
(fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
|
2002-12-23 12:51:42 -04:00
|
|
|
nch = NCH(n);
|
|
|
|
/* Enter all arguments in table of locals */
|
|
|
|
for (i = 0, narg = 0; i < nch; i++) {
|
|
|
|
node *ch = CHILD(n, i);
|
|
|
|
node *fp;
|
|
|
|
if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
|
1992-01-14 14:30:26 -04:00
|
|
|
break;
|
2002-12-23 12:51:42 -04:00
|
|
|
REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
|
|
|
|
fp = CHILD(ch, 0);
|
|
|
|
if (TYPE(fp) != NAME) {
|
|
|
|
PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
|
|
|
|
complex = 1;
|
2002-12-23 12:35:23 -04:00
|
|
|
}
|
2002-12-23 12:51:42 -04:00
|
|
|
narg++;
|
|
|
|
/* all name updates handled by symtable */
|
|
|
|
if (++i >= nch)
|
|
|
|
break;
|
|
|
|
ch = CHILD(n, i);
|
|
|
|
if (TYPE(ch) == EQUAL)
|
|
|
|
i += 2;
|
|
|
|
else
|
|
|
|
REQ(ch, COMMA);
|
|
|
|
}
|
1995-07-18 11:51:37 -03:00
|
|
|
if (complex) {
|
|
|
|
/* Generate code for complex arguments only after
|
|
|
|
having counted the simple arguments */
|
|
|
|
int ilocal = 0;
|
2002-12-23 12:51:42 -04:00
|
|
|
for (i = 0; i < nch; i++) {
|
1995-07-18 11:51:37 -03:00
|
|
|
node *ch = CHILD(n, i);
|
|
|
|
node *fp;
|
1996-01-11 21:13:16 -04:00
|
|
|
if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
|
1994-08-29 09:16:12 -03:00
|
|
|
break;
|
1995-07-18 11:51:37 -03:00
|
|
|
REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
|
|
|
|
fp = CHILD(ch, 0);
|
|
|
|
if (TYPE(fp) != NAME) {
|
|
|
|
com_addoparg(c, LOAD_FAST, ilocal);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1995-07-18 11:51:37 -03:00
|
|
|
com_fpdef(c, ch);
|
|
|
|
}
|
|
|
|
ilocal++;
|
2002-12-23 12:51:42 -04:00
|
|
|
if (++i >= nch)
|
1995-07-18 11:51:37 -03:00
|
|
|
break;
|
|
|
|
ch = CHILD(n, i);
|
|
|
|
if (TYPE(ch) == EQUAL)
|
|
|
|
i += 2;
|
|
|
|
else
|
|
|
|
REQ(ch, COMMA);
|
1994-08-29 09:16:12 -03:00
|
|
|
}
|
|
|
|
}
|
1992-01-14 14:30:26 -04:00
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
com_file_input(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *doc;
|
1990-11-18 13:27:39 -04:00
|
|
|
REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
|
2001-08-17 15:39:25 -03:00
|
|
|
doc = get_docstring(c, n);
|
1995-01-07 08:41:23 -04:00
|
|
|
if (doc != NULL) {
|
|
|
|
int i = com_addconst(c, doc);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(doc);
|
1995-01-07 08:41:23 -04:00
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_name(c, STORE_NAME, "__doc__");
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
for (i = 0; i < NCH(n); i++) {
|
|
|
|
node *ch = CHILD(n, i);
|
|
|
|
if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
|
|
|
|
com_node(c, ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Top-level compile-node interface */
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
compile_funcdef(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *doc;
|
1990-11-18 13:27:39 -04:00
|
|
|
node *ch;
|
2004-08-02 03:10:11 -03:00
|
|
|
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));
|
1995-01-07 08:41:23 -04:00
|
|
|
if (doc != NULL) {
|
|
|
|
(void) com_addconst(c, doc);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(doc);
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
1995-07-18 11:51:37 -03:00
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
(void) com_addconst(c, Py_None); /* No docstring */
|
2004-08-02 03:10:11 -03:00
|
|
|
ch = RCHILD(n, -3); /* parameters: '(' [varargslist] ')' */
|
1992-01-14 14:30:26 -04:00
|
|
|
ch = CHILD(ch, 1); /* ')' | varargslist */
|
1995-07-18 11:51:37 -03:00
|
|
|
if (TYPE(ch) == varargslist)
|
1992-01-14 14:30:26 -04:00
|
|
|
com_arglist(c, ch);
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_infunction = 1;
|
2004-08-02 03:10:11 -03:00
|
|
|
com_node(c, RCHILD(n, -1));
|
1990-12-20 11:06:42 -04:00
|
|
|
c->c_infunction = 0;
|
2004-03-22 13:52:53 -04:00
|
|
|
com_strip_lnotab(c);
|
2002-08-30 10:09:51 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
|
|
|
com_push(c, 1);
|
|
|
|
com_addbyte(c, RETURN_VALUE);
|
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
|
1993-10-26 14:58:25 -03:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
compile_lambdef(struct compiling *c, node *n)
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
1993-11-30 09:40:46 -04:00
|
|
|
node *ch;
|
1995-07-18 11:51:37 -03:00
|
|
|
REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
|
1994-11-10 18:40:34 -04:00
|
|
|
c->c_name = "<lambda>";
|
1993-11-30 09:40:46 -04:00
|
|
|
|
|
|
|
ch = CHILD(n, 1);
|
1997-04-29 17:08:16 -03:00
|
|
|
(void) com_addconst(c, Py_None); /* No docstring */
|
1995-07-18 11:51:37 -03:00
|
|
|
if (TYPE(ch) == varargslist) {
|
1993-11-30 09:40:46 -04:00
|
|
|
com_arglist(c, ch);
|
1995-07-18 11:51:37 -03:00
|
|
|
ch = CHILD(n, 3);
|
1993-11-30 09:40:46 -04:00
|
|
|
}
|
1995-07-18 11:51:37 -03:00
|
|
|
else
|
|
|
|
ch = CHILD(n, 2);
|
|
|
|
com_node(c, ch);
|
1993-10-26 14:58:25 -03:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
1995-01-07 08:41:23 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
compile_classdef(struct compiling *c, node *n)
|
1995-01-07 08:41:23 -04:00
|
|
|
{
|
|
|
|
node *ch;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *doc;
|
1995-01-07 08:41:23 -04:00
|
|
|
REQ(n, classdef);
|
|
|
|
/* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
|
|
|
|
c->c_name = STR(CHILD(n, 1));
|
1996-08-24 03:21:31 -03:00
|
|
|
c->c_private = c->c_name;
|
2002-01-15 17:06:07 -04:00
|
|
|
/* Initialize local __module__ from global __name__ */
|
|
|
|
com_addop_name(c, LOAD_GLOBAL, "__name__");
|
|
|
|
com_addop_name(c, STORE_NAME, "__module__");
|
1995-01-07 08:41:23 -04:00
|
|
|
ch = CHILD(n, NCH(n)-1); /* The suite */
|
2001-08-17 15:39:25 -03:00
|
|
|
doc = get_docstring(c, ch);
|
1995-01-07 08:41:23 -04:00
|
|
|
if (doc != NULL) {
|
|
|
|
int i = com_addconst(c, doc);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(doc);
|
1995-01-07 08:41:23 -04:00
|
|
|
com_addoparg(c, LOAD_CONST, i);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
2001-01-18 23:21:30 -04:00
|
|
|
com_addop_name(c, STORE_NAME, "__doc__");
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
(void) com_addconst(c, Py_None);
|
1995-01-07 08:41:23 -04:00
|
|
|
com_node(c, ch);
|
2004-03-22 13:52:53 -04:00
|
|
|
com_strip_lnotab(c);
|
1995-01-07 08:41:23 -04:00
|
|
|
com_addbyte(c, LOAD_LOCALS);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_push(c, 1);
|
1995-01-07 08:41:23 -04:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1995-01-07 08:41:23 -04:00
|
|
|
}
|
|
|
|
|
2004-05-19 05:20:33 -03:00
|
|
|
static void
|
|
|
|
compile_generator_expression(struct compiling *c, node *n)
|
|
|
|
{
|
|
|
|
/* testlist_gexp: test gen_for */
|
|
|
|
/* argument: test gen_for */
|
|
|
|
REQ(CHILD(n, 0), test);
|
|
|
|
REQ(CHILD(n, 1), gen_for);
|
|
|
|
|
|
|
|
c->c_name = "<generator expression>";
|
|
|
|
com_gen_for(c, CHILD(n, 1), CHILD(n, 0), 1);
|
|
|
|
|
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
|
|
|
com_push(c, 1);
|
|
|
|
com_addbyte(c, RETURN_VALUE);
|
|
|
|
com_pop(c, 1);
|
|
|
|
}
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
compile_node(struct compiling *c, node *n)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
2002-08-15 11:59:02 -03:00
|
|
|
com_set_lineno(c, n->n_lineno);
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
switch (TYPE(n)) {
|
|
|
|
|
1991-01-21 12:09:22 -04:00
|
|
|
case single_input: /* One interactive command */
|
1990-11-18 13:27:39 -04:00
|
|
|
/* NEWLINE | simple_stmt | compound_stmt NEWLINE */
|
1994-08-29 09:16:12 -03:00
|
|
|
c->c_interactive++;
|
1990-11-18 13:27:39 -04:00
|
|
|
n = CHILD(n, 0);
|
|
|
|
if (TYPE(n) != NEWLINE)
|
|
|
|
com_node(c, n);
|
2004-03-22 13:52:53 -04:00
|
|
|
com_strip_lnotab(c);
|
2002-08-30 10:09:51 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
|
|
|
com_push(c, 1);
|
|
|
|
com_addbyte(c, RETURN_VALUE);
|
|
|
|
com_pop(c, 1);
|
1994-08-29 09:16:12 -03:00
|
|
|
c->c_interactive--;
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
|
1991-01-21 12:09:22 -04:00
|
|
|
case file_input: /* A whole file, or built-in function exec() */
|
1990-11-18 13:27:39 -04:00
|
|
|
com_file_input(c, n);
|
2004-03-22 13:52:53 -04:00
|
|
|
com_strip_lnotab(c);
|
2002-08-30 10:09:51 -03:00
|
|
|
com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
|
|
|
|
com_push(c, 1);
|
|
|
|
com_addbyte(c, RETURN_VALUE);
|
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
|
1993-11-30 09:40:46 -04:00
|
|
|
case eval_input: /* Built-in function input() */
|
1990-11-18 13:27:39 -04:00
|
|
|
com_node(c, CHILD(n, 0));
|
1990-12-20 11:06:42 -04:00
|
|
|
com_addbyte(c, RETURN_VALUE);
|
1997-01-17 17:04:03 -04:00
|
|
|
com_pop(c, 1);
|
1990-11-18 13:27:39 -04:00
|
|
|
break;
|
|
|
|
|
1993-11-30 09:40:46 -04:00
|
|
|
case lambdef: /* anonymous function definition */
|
|
|
|
compile_lambdef(c, n);
|
|
|
|
break;
|
|
|
|
|
1991-01-21 12:09:22 -04:00
|
|
|
case funcdef: /* A function definition */
|
1990-11-18 13:27:39 -04:00
|
|
|
compile_funcdef(c, n);
|
|
|
|
break;
|
|
|
|
|
1991-01-21 12:09:22 -04:00
|
|
|
case classdef: /* A class definition */
|
1995-01-07 08:41:23 -04:00
|
|
|
compile_classdef(c, n);
|
1990-11-18 13:35:03 -04:00
|
|
|
break;
|
|
|
|
|
2004-05-19 05:20:33 -03:00
|
|
|
case testlist_gexp: /* A generator expression */
|
|
|
|
case argument: /* A generator expression */
|
|
|
|
compile_generator_expression(c, n);
|
|
|
|
break;
|
|
|
|
|
1990-11-18 13:27:39 -04:00
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
com_error(c, PyExc_SystemError,
|
1996-09-17 11:32:04 -03:00
|
|
|
"compile_node: unexpected node type");
|
1990-11-18 13:27:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
static PyObject *
|
|
|
|
dict_keys_inorder(PyObject *dict, int offset)
|
|
|
|
{
|
|
|
|
PyObject *tuple, *k, *v;
|
|
|
|
int i, pos = 0, size = PyDict_Size(dict);
|
|
|
|
|
|
|
|
tuple = PyTuple_New(size);
|
|
|
|
if (tuple == NULL)
|
|
|
|
return NULL;
|
|
|
|
while (PyDict_Next(dict, &pos, &k, &v)) {
|
|
|
|
i = PyInt_AS_LONG(v);
|
|
|
|
Py_INCREF(k);
|
2001-03-19 20:25:43 -04:00
|
|
|
assert((i - offset) < size);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
PyTuple_SET_ITEM(tuple, i - offset, k);
|
|
|
|
}
|
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyCodeObject *
|
2002-12-11 10:04:59 -04:00
|
|
|
PyNode_Compile(node *n, const char *filename)
|
1996-08-24 03:21:31 -03:00
|
|
|
{
|
2001-03-01 18:59:14 -04:00
|
|
|
return PyNode_CompileFlags(n, filename, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyCodeObject *
|
2002-12-11 10:04:59 -04:00
|
|
|
PyNode_CompileFlags(node *n, const char *filename, PyCompilerFlags *flags)
|
2001-03-01 18:59:14 -04:00
|
|
|
{
|
|
|
|
return jcompile(n, filename, NULL, flags);
|
1996-08-24 03:21:31 -03:00
|
|
|
}
|
|
|
|
|
2001-02-02 14:19:15 -04:00
|
|
|
struct symtable *
|
2002-12-11 10:04:59 -04:00
|
|
|
PyNode_CompileSymtable(node *n, const char *filename)
|
2001-02-02 14:19:15 -04:00
|
|
|
{
|
|
|
|
struct symtable *st;
|
2001-03-21 15:01:33 -04:00
|
|
|
PyFutureFeatures *ff;
|
2001-02-02 14:19:15 -04:00
|
|
|
|
2001-03-21 15:01:33 -04:00
|
|
|
ff = PyNode_Future(n, filename);
|
|
|
|
if (ff == NULL)
|
|
|
|
return NULL;
|
2003-07-15 17:23:26 -03:00
|
|
|
st = symtable_build(n, ff, filename);
|
2001-11-04 15:26:58 -04:00
|
|
|
if (st == NULL) {
|
2002-08-04 03:28:21 -03:00
|
|
|
PyObject_FREE((void *)ff);
|
2001-02-02 14:19:15 -04:00
|
|
|
return NULL;
|
2001-11-04 15:26:58 -04:00
|
|
|
}
|
2001-02-02 14:19:15 -04:00
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyCodeObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
icompile(node *n, struct compiling *base)
|
1996-08-24 03:21:31 -03:00
|
|
|
{
|
2001-03-01 18:59:14 -04:00
|
|
|
return jcompile(n, base->c_filename, base, NULL);
|
1996-08-24 03:21:31 -03:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyCodeObject *
|
2002-12-11 10:04:59 -04:00
|
|
|
jcompile(node *n, const char *filename, struct compiling *base,
|
2001-03-01 18:59:14 -04:00
|
|
|
PyCompilerFlags *flags)
|
1990-11-18 13:27:39 -04:00
|
|
|
{
|
|
|
|
struct compiling sc;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyCodeObject *co;
|
1993-11-30 09:40:46 -04:00
|
|
|
if (!com_init(&sc, filename))
|
1990-11-18 13:27:39 -04:00
|
|
|
return NULL;
|
2003-02-10 04:21:10 -04:00
|
|
|
if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
|
|
|
|
sc.c_encoding = "utf-8";
|
|
|
|
} else if (TYPE(n) == encoding_decl) {
|
2002-08-04 14:29:52 -03:00
|
|
|
sc.c_encoding = STR(n);
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
} else {
|
|
|
|
sc.c_encoding = NULL;
|
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
if (base) {
|
1996-08-24 03:21:31 -03:00
|
|
|
sc.c_private = base->c_private;
|
2001-01-18 23:21:30 -04:00
|
|
|
sc.c_symtable = base->c_symtable;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
/* c_symtable still points to parent's symbols */
|
|
|
|
if (base->c_nested
|
2001-02-09 18:22:18 -04:00
|
|
|
|| (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
sc.c_nested = 1;
|
2001-10-17 10:22:22 -03:00
|
|
|
sc.c_flags |= base->c_flags & PyCF_MASK;
|
2002-08-04 14:29:52 -03:00
|
|
|
if (base->c_encoding != NULL) {
|
|
|
|
assert(sc.c_encoding == NULL);
|
|
|
|
sc.c_encoding = base->c_encoding;
|
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
} else {
|
1996-08-24 03:21:31 -03:00
|
|
|
sc.c_private = NULL;
|
2001-02-27 15:07:02 -04:00
|
|
|
sc.c_future = PyNode_Future(n, filename);
|
2001-03-01 18:59:14 -04:00
|
|
|
if (sc.c_future == NULL) {
|
|
|
|
com_free(&sc);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-08-14 17:01:59 -03:00
|
|
|
if (flags) {
|
|
|
|
int merged = sc.c_future->ff_features |
|
|
|
|
flags->cf_flags;
|
|
|
|
sc.c_future->ff_features = merged;
|
|
|
|
flags->cf_flags = merged;
|
|
|
|
}
|
2003-07-15 17:23:26 -03:00
|
|
|
sc.c_symtable = symtable_build(n, sc.c_future, sc.c_filename);
|
|
|
|
if (sc.c_symtable == NULL) {
|
2001-01-18 23:21:30 -04:00
|
|
|
com_free(&sc);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-07-15 17:23:26 -03:00
|
|
|
/* reset symbol table for second pass */
|
|
|
|
sc.c_symtable->st_nscopes = 1;
|
|
|
|
sc.c_symtable->st_pass = 2;
|
1995-07-18 11:51:37 -03:00
|
|
|
}
|
1993-03-29 06:43:31 -04:00
|
|
|
co = NULL;
|
2001-02-09 18:22:18 -04:00
|
|
|
if (symtable_load_symbols(&sc) < 0) {
|
|
|
|
sc.c_errors++;
|
2001-01-18 23:21:30 -04:00
|
|
|
goto exit;
|
2001-02-09 18:22:18 -04:00
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
compile_node(&sc, n);
|
|
|
|
com_done(&sc);
|
1993-03-29 06:43:31 -04:00
|
|
|
if (sc.c_errors == 0) {
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
PyObject *consts, *names, *varnames, *filename, *name,
|
2004-08-18 02:22:06 -03:00
|
|
|
*freevars, *cellvars, *code;
|
1997-04-29 17:08:16 -03:00
|
|
|
names = PyList_AsTuple(sc.c_names);
|
|
|
|
varnames = PyList_AsTuple(sc.c_varnames);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
cellvars = dict_keys_inorder(sc.c_cellvars, 0);
|
|
|
|
freevars = dict_keys_inorder(sc.c_freevars,
|
|
|
|
PyTuple_GET_SIZE(cellvars));
|
2004-08-04 07:26:08 -03:00
|
|
|
filename = PyString_InternFromString(sc.c_filename);
|
1997-01-18 04:02:57 -04:00
|
|
|
name = PyString_InternFromString(sc.c_name);
|
2004-09-22 15:44:21 -03:00
|
|
|
code = optimize_code(sc.c_code, sc.c_consts, names, sc.c_lnotab);
|
|
|
|
consts = PyList_AsTuple(sc.c_consts);
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyErr_Occurred())
|
|
|
|
co = PyCode_New(sc.c_argcount,
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
sc.c_nlocals,
|
|
|
|
sc.c_maxstacklevel,
|
|
|
|
sc.c_flags,
|
2004-08-18 02:22:06 -03:00
|
|
|
code,
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
consts,
|
|
|
|
names,
|
|
|
|
varnames,
|
|
|
|
freevars,
|
|
|
|
cellvars,
|
|
|
|
filename,
|
|
|
|
name,
|
|
|
|
sc.c_firstlineno,
|
|
|
|
sc.c_lnotab);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(consts);
|
|
|
|
Py_XDECREF(names);
|
|
|
|
Py_XDECREF(varnames);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
Py_XDECREF(freevars);
|
|
|
|
Py_XDECREF(cellvars);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(filename);
|
|
|
|
Py_XDECREF(name);
|
2004-08-18 02:22:06 -03:00
|
|
|
Py_XDECREF(code);
|
1991-06-04 16:41:56 -03:00
|
|
|
}
|
2000-08-07 16:22:43 -03:00
|
|
|
else if (!PyErr_Occurred()) {
|
|
|
|
/* This could happen if someone called PyErr_Clear() after an
|
|
|
|
error was reported above. That's not supposed to happen,
|
|
|
|
but I just plugged one case and I'm not sure there can't be
|
|
|
|
others. In that case, raise SystemError so that at least
|
|
|
|
it gets reported instead dumping core. */
|
|
|
|
PyErr_SetString(PyExc_SystemError, "lost syntax error");
|
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
exit:
|
2001-02-23 13:55:27 -04:00
|
|
|
if (base == NULL) {
|
2001-02-02 14:19:15 -04:00
|
|
|
PySymtable_Free(sc.c_symtable);
|
2001-02-23 13:55:27 -04:00
|
|
|
sc.c_symtable = NULL;
|
|
|
|
}
|
1990-11-18 13:27:39 -04:00
|
|
|
com_free(&sc);
|
|
|
|
return co;
|
|
|
|
}
|
1997-01-23 23:43:35 -04:00
|
|
|
|
|
|
|
int
|
2000-07-22 15:47:25 -03:00
|
|
|
PyCode_Addr2Line(PyCodeObject *co, int addrq)
|
1997-01-23 23:43:35 -04:00
|
|
|
{
|
|
|
|
int size = PyString_Size(co->co_lnotab) / 2;
|
1999-09-15 19:48:09 -03:00
|
|
|
unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
|
1997-01-23 23:43:35 -04:00
|
|
|
int line = co->co_firstlineno;
|
|
|
|
int addr = 0;
|
|
|
|
while (--size >= 0) {
|
|
|
|
addr += *p++;
|
|
|
|
if (addr > addrq)
|
|
|
|
break;
|
|
|
|
line += *p++;
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
/* The test for LOCAL must come before the test for FREE in order to
|
|
|
|
handle classes where name is both local and free. The local var is
|
|
|
|
a method and the free var is a free var referenced within a method.
|
|
|
|
*/
|
2001-01-18 23:21:30 -04:00
|
|
|
|
|
|
|
static int
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
get_ref_type(struct compiling *c, char *name)
|
2001-01-18 23:21:30 -04:00
|
|
|
{
|
2001-08-11 18:51:24 -03:00
|
|
|
char buf[350];
|
2001-01-18 23:21:30 -04:00
|
|
|
PyObject *v;
|
2001-08-11 18:51:24 -03:00
|
|
|
|
|
|
|
if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
|
|
|
|
return CELL;
|
|
|
|
if (PyDict_GetItemString(c->c_locals, name) != NULL)
|
|
|
|
return LOCAL;
|
|
|
|
if (PyDict_GetItemString(c->c_freevars, name) != NULL)
|
|
|
|
return FREE;
|
|
|
|
v = PyDict_GetItemString(c->c_globals, name);
|
|
|
|
if (v) {
|
|
|
|
if (v == Py_None)
|
|
|
|
return GLOBAL_EXPLICIT;
|
|
|
|
else {
|
|
|
|
return GLOBAL_IMPLICIT;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
}
|
|
|
|
}
|
2001-11-28 07:47:00 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
2001-08-11 18:51:24 -03:00
|
|
|
"unknown scope for %.100s in %.100s(%s) "
|
|
|
|
"in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
|
|
|
|
name, c->c_name,
|
|
|
|
PyObject_REPR(c->c_symtable->st_cur->ste_id),
|
|
|
|
c->c_filename,
|
|
|
|
PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
|
|
|
|
PyObject_REPR(c->c_locals),
|
|
|
|
PyObject_REPR(c->c_globals)
|
|
|
|
);
|
|
|
|
|
|
|
|
Py_FatalError(buf);
|
|
|
|
return -1;
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
|
2001-03-01 23:30:41 -04:00
|
|
|
/* Helper functions to issue warnings */
|
2001-02-28 18:08:12 -04:00
|
|
|
|
2001-02-28 18:54:51 -04:00
|
|
|
static int
|
2002-12-11 10:04:59 -04:00
|
|
|
issue_warning(const char *msg, const char *filename, int lineno)
|
2001-02-28 18:08:12 -04:00
|
|
|
{
|
2003-07-15 17:23:26 -03:00
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
/* This can happen because symtable_node continues
|
|
|
|
processing even after raising a SyntaxError.
|
|
|
|
Calling PyErr_WarnExplicit now would clobber the
|
|
|
|
pending exception; instead we fail and let that
|
|
|
|
exception propagate.
|
|
|
|
*/
|
|
|
|
return -1;
|
|
|
|
}
|
2001-03-01 23:30:41 -04:00
|
|
|
if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
|
|
|
|
lineno, NULL, NULL) < 0) {
|
2001-02-28 18:08:12 -04:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
|
|
|
|
PyErr_SetString(PyExc_SyntaxError, msg);
|
2001-03-01 23:30:41 -04:00
|
|
|
PyErr_SyntaxLocation(filename, lineno);
|
2001-02-28 18:08:12 -04:00
|
|
|
}
|
2001-03-01 23:30:41 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
symtable_warn(struct symtable *st, char *msg)
|
|
|
|
{
|
|
|
|
if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
|
2001-02-28 18:08:12 -04:00
|
|
|
st->st_errors++;
|
2001-02-28 18:54:51 -04:00
|
|
|
return -1;
|
2001-02-28 18:08:12 -04:00
|
|
|
}
|
2001-02-28 18:54:51 -04:00
|
|
|
return 0;
|
2001-02-28 18:08:12 -04:00
|
|
|
}
|
|
|
|
|
2001-02-28 03:07:43 -04:00
|
|
|
/* Helper function for setting lineno and filename */
|
|
|
|
|
2003-07-15 17:23:26 -03:00
|
|
|
static struct symtable *
|
|
|
|
symtable_build(node *n, PyFutureFeatures *ff, const char *filename)
|
2001-01-18 23:21:30 -04:00
|
|
|
{
|
2003-07-15 17:23:26 -03:00
|
|
|
struct symtable *st;
|
|
|
|
|
|
|
|
st = symtable_init();
|
|
|
|
if (st == NULL)
|
|
|
|
return NULL;
|
|
|
|
st->st_future = ff;
|
|
|
|
st->st_filename = filename;
|
|
|
|
symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
|
|
|
|
if (st->st_errors > 0)
|
|
|
|
goto fail;
|
|
|
|
symtable_node(st, n);
|
|
|
|
if (st->st_errors > 0)
|
|
|
|
goto fail;
|
|
|
|
return st;
|
|
|
|
fail:
|
|
|
|
if (!PyErr_Occurred()) {
|
|
|
|
/* This could happen because after a syntax error is
|
|
|
|
detected, the symbol-table-building continues for
|
|
|
|
a while, and PyErr_Clear() might erroneously be
|
|
|
|
called during that process. One such case has been
|
|
|
|
fixed, but there might be more (now or later).
|
|
|
|
*/
|
|
|
|
PyErr_SetString(PyExc_SystemError, "lost exception");
|
|
|
|
}
|
|
|
|
st->st_future = NULL;
|
|
|
|
st->st_filename = NULL;
|
|
|
|
PySymtable_Free(st);
|
|
|
|
return NULL;
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-02-27 00:23:34 -04:00
|
|
|
symtable_init_compiling_symbols(struct compiling *c)
|
2001-01-18 23:21:30 -04:00
|
|
|
{
|
2001-02-27 00:23:34 -04:00
|
|
|
PyObject *varnames;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
|
2001-02-27 00:23:34 -04:00
|
|
|
varnames = c->c_symtable->st_cur->ste_varnames;
|
2001-01-18 23:21:30 -04:00
|
|
|
if (varnames == NULL) {
|
|
|
|
varnames = PyList_New(0);
|
|
|
|
if (varnames == NULL)
|
|
|
|
return -1;
|
2001-02-27 00:23:34 -04:00
|
|
|
c->c_symtable->st_cur->ste_varnames = varnames;
|
2001-02-09 18:22:18 -04:00
|
|
|
Py_INCREF(varnames);
|
2001-01-18 23:21:30 -04:00
|
|
|
} else
|
|
|
|
Py_INCREF(varnames);
|
|
|
|
c->c_varnames = varnames;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
|
|
|
|
c->c_globals = PyDict_New();
|
|
|
|
if (c->c_globals == NULL)
|
2001-02-27 00:23:34 -04:00
|
|
|
return -1;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
c->c_freevars = PyDict_New();
|
|
|
|
if (c->c_freevars == NULL)
|
2001-02-27 00:23:34 -04:00
|
|
|
return -1;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
c->c_cellvars = PyDict_New();
|
|
|
|
if (c->c_cellvars == NULL)
|
2001-02-27 00:23:34 -04:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct symbol_info {
|
|
|
|
int si_nlocals;
|
|
|
|
int si_ncells;
|
|
|
|
int si_nfrees;
|
|
|
|
int si_nimplicit;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
symtable_init_info(struct symbol_info *si)
|
|
|
|
{
|
|
|
|
si->si_nlocals = 0;
|
|
|
|
si->si_ncells = 0;
|
|
|
|
si->si_nfrees = 0;
|
|
|
|
si->si_nimplicit = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-04-26 23:29:40 -03:00
|
|
|
symtable_resolve_free(struct compiling *c, PyObject *name, int flags,
|
2001-02-27 00:23:34 -04:00
|
|
|
struct symbol_info *si)
|
|
|
|
{
|
|
|
|
PyObject *dict, *v;
|
|
|
|
|
|
|
|
/* Seperate logic for DEF_FREE. If it occurs in a function,
|
|
|
|
it indicates a local that we must allocate storage for (a
|
|
|
|
cell var). If it occurs in a class, then the class has a
|
|
|
|
method and a free variable with the same name.
|
|
|
|
*/
|
|
|
|
if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
|
2001-04-26 23:29:40 -03:00
|
|
|
/* If it isn't declared locally, it can't be a cell. */
|
|
|
|
if (!(flags & (DEF_LOCAL | DEF_PARAM)))
|
|
|
|
return 0;
|
2001-02-27 00:23:34 -04:00
|
|
|
v = PyInt_FromLong(si->si_ncells++);
|
|
|
|
dict = c->c_cellvars;
|
|
|
|
} else {
|
2001-04-26 23:29:40 -03:00
|
|
|
/* If it is free anyway, then there is no need to do
|
|
|
|
anything here.
|
|
|
|
*/
|
|
|
|
if (is_free(flags ^ DEF_FREE_CLASS)
|
2001-05-08 01:12:34 -03:00
|
|
|
|| (flags == DEF_FREE_CLASS))
|
2001-04-26 23:29:40 -03:00
|
|
|
return 0;
|
2001-02-27 00:23:34 -04:00
|
|
|
v = PyInt_FromLong(si->si_nfrees++);
|
|
|
|
dict = c->c_freevars;
|
|
|
|
}
|
|
|
|
if (v == NULL)
|
|
|
|
return -1;
|
|
|
|
if (PyDict_SetItem(dict, name, v) < 0) {
|
|
|
|
Py_DECREF(v);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(v);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-03-21 12:43:47 -04:00
|
|
|
/* If a variable is a cell and an argument, make sure that appears in
|
|
|
|
co_cellvars before any variable to its right in varnames.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
symtable_cellvar_offsets(PyObject **cellvars, int argcount,
|
|
|
|
PyObject *varnames, int flags)
|
|
|
|
{
|
2003-03-24 13:22:24 -04:00
|
|
|
PyObject *v = NULL;
|
|
|
|
PyObject *w, *d, *list = NULL;
|
2001-03-21 12:43:47 -04:00
|
|
|
int i, pos;
|
|
|
|
|
|
|
|
if (flags & CO_VARARGS)
|
|
|
|
argcount++;
|
|
|
|
if (flags & CO_VARKEYWORDS)
|
|
|
|
argcount++;
|
|
|
|
for (i = argcount; --i >= 0; ) {
|
|
|
|
v = PyList_GET_ITEM(varnames, i);
|
|
|
|
if (PyDict_GetItem(*cellvars, v)) {
|
|
|
|
if (list == NULL) {
|
|
|
|
list = PyList_New(1);
|
|
|
|
if (list == NULL)
|
|
|
|
return -1;
|
|
|
|
PyList_SET_ITEM(list, 0, v);
|
|
|
|
Py_INCREF(v);
|
2003-05-03 07:53:08 -03:00
|
|
|
} else {
|
|
|
|
if (PyList_Insert(list, 0, v) < 0) {
|
|
|
|
Py_DECREF(list);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2001-03-21 12:43:47 -04:00
|
|
|
}
|
|
|
|
}
|
2003-05-22 12:47:02 -03:00
|
|
|
if (list == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2001-03-21 12:43:47 -04:00
|
|
|
/* There are cellvars that are also arguments. Create a dict
|
|
|
|
to replace cellvars and put the args at the front.
|
|
|
|
*/
|
|
|
|
d = PyDict_New();
|
2003-05-03 07:53:08 -03:00
|
|
|
if (d == NULL)
|
|
|
|
return -1;
|
2001-03-21 12:43:47 -04:00
|
|
|
for (i = PyList_GET_SIZE(list); --i >= 0; ) {
|
|
|
|
v = PyInt_FromLong(i);
|
|
|
|
if (v == NULL)
|
|
|
|
goto fail;
|
|
|
|
if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
|
|
|
|
goto fail;
|
|
|
|
if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
|
|
|
|
goto fail;
|
2003-03-24 13:22:24 -04:00
|
|
|
Py_DECREF(v);
|
2001-03-21 12:43:47 -04:00
|
|
|
}
|
|
|
|
pos = 0;
|
|
|
|
i = PyList_GET_SIZE(list);
|
|
|
|
Py_DECREF(list);
|
|
|
|
while (PyDict_Next(*cellvars, &pos, &v, &w)) {
|
|
|
|
w = PyInt_FromLong(i++); /* don't care about the old key */
|
2003-05-03 07:53:08 -03:00
|
|
|
if (w == NULL)
|
|
|
|
goto fail;
|
2001-03-21 12:43:47 -04:00
|
|
|
if (PyDict_SetItem(d, v, w) < 0) {
|
|
|
|
Py_DECREF(w);
|
2003-03-24 13:22:24 -04:00
|
|
|
v = NULL;
|
2001-03-21 12:43:47 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
Py_DECREF(w);
|
|
|
|
}
|
|
|
|
Py_DECREF(*cellvars);
|
|
|
|
*cellvars = d;
|
|
|
|
return 1;
|
|
|
|
fail:
|
|
|
|
Py_DECREF(d);
|
2003-03-24 13:22:24 -04:00
|
|
|
Py_XDECREF(v);
|
2001-03-21 12:43:47 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-02-27 00:23:34 -04:00
|
|
|
static int
|
|
|
|
symtable_freevar_offsets(PyObject *freevars, int offset)
|
|
|
|
{
|
|
|
|
PyObject *name, *v;
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
/* The cell vars are the first elements of the closure,
|
|
|
|
followed by the free vars. Update the offsets in
|
|
|
|
c_freevars to account for number of cellvars. */
|
|
|
|
pos = 0;
|
|
|
|
while (PyDict_Next(freevars, &pos, &name, &v)) {
|
|
|
|
int i = PyInt_AS_LONG(v) + offset;
|
|
|
|
PyObject *o = PyInt_FromLong(i);
|
|
|
|
if (o == NULL)
|
|
|
|
return -1;
|
|
|
|
if (PyDict_SetItem(freevars, name, o) < 0) {
|
|
|
|
Py_DECREF(o);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(o);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-28 18:54:51 -04:00
|
|
|
static int
|
|
|
|
symtable_check_unoptimized(struct compiling *c,
|
|
|
|
PySymtableEntryObject *ste,
|
|
|
|
struct symbol_info *si)
|
|
|
|
{
|
|
|
|
char buf[300];
|
|
|
|
|
|
|
|
if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
|
|
|
|
|| (ste->ste_nested && si->si_nimplicit)))
|
|
|
|
return 0;
|
|
|
|
|
2001-03-21 23:51:05 -04:00
|
|
|
#define ILLEGAL_CONTAINS "contains a nested function with free variables"
|
|
|
|
|
|
|
|
#define ILLEGAL_IS "is a nested function"
|
|
|
|
|
2001-02-28 18:54:51 -04:00
|
|
|
#define ILLEGAL_IMPORT_STAR \
|
2001-03-21 23:51:05 -04:00
|
|
|
"import * is not allowed in function '%.100s' because it %s"
|
2001-02-28 18:54:51 -04:00
|
|
|
|
|
|
|
#define ILLEGAL_BARE_EXEC \
|
2001-03-21 23:51:05 -04:00
|
|
|
"unqualified exec is not allowed in function '%.100s' it %s"
|
2001-02-28 18:54:51 -04:00
|
|
|
|
|
|
|
#define ILLEGAL_EXEC_AND_IMPORT_STAR \
|
2002-01-28 20:56:37 -04:00
|
|
|
"function '%.100s' uses import * and bare exec, which are illegal " \
|
2001-03-21 23:51:05 -04:00
|
|
|
"because it %s"
|
2001-02-28 18:54:51 -04:00
|
|
|
|
|
|
|
/* XXX perhaps the linenos for these opt-breaking statements
|
|
|
|
should be stored so the exception can point to them. */
|
|
|
|
|
2001-03-21 23:51:05 -04:00
|
|
|
if (ste->ste_child_free) {
|
|
|
|
if (ste->ste_optimized == OPT_IMPORT_STAR)
|
2001-11-28 17:10:39 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
|
|
ILLEGAL_IMPORT_STAR,
|
|
|
|
PyString_AS_STRING(ste->ste_name),
|
|
|
|
ILLEGAL_CONTAINS);
|
2001-03-21 23:51:05 -04:00
|
|
|
else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
|
2001-11-28 17:10:39 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
|
|
ILLEGAL_BARE_EXEC,
|
|
|
|
PyString_AS_STRING(ste->ste_name),
|
|
|
|
ILLEGAL_CONTAINS);
|
2001-03-21 23:51:05 -04:00
|
|
|
else {
|
2001-11-28 17:10:39 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
|
|
ILLEGAL_EXEC_AND_IMPORT_STAR,
|
|
|
|
PyString_AS_STRING(ste->ste_name),
|
|
|
|
ILLEGAL_CONTAINS);
|
2001-03-21 23:51:05 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ste->ste_optimized == OPT_IMPORT_STAR)
|
2001-11-28 17:10:39 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
|
|
ILLEGAL_IMPORT_STAR,
|
|
|
|
PyString_AS_STRING(ste->ste_name),
|
|
|
|
ILLEGAL_IS);
|
2001-03-21 23:51:05 -04:00
|
|
|
else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
|
2001-11-28 17:10:39 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
|
|
ILLEGAL_BARE_EXEC,
|
|
|
|
PyString_AS_STRING(ste->ste_name),
|
|
|
|
ILLEGAL_IS);
|
2001-03-21 23:51:05 -04:00
|
|
|
else {
|
2001-11-28 17:10:39 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
|
|
ILLEGAL_EXEC_AND_IMPORT_STAR,
|
|
|
|
PyString_AS_STRING(ste->ste_name),
|
|
|
|
ILLEGAL_IS);
|
2001-03-21 23:51:05 -04:00
|
|
|
}
|
2001-02-28 18:54:51 -04:00
|
|
|
}
|
2001-03-21 23:51:05 -04:00
|
|
|
|
2001-08-11 18:51:24 -03:00
|
|
|
PyErr_SetString(PyExc_SyntaxError, buf);
|
|
|
|
PyErr_SyntaxLocation(c->c_symtable->st_filename,
|
|
|
|
ste->ste_opt_lineno);
|
|
|
|
return -1;
|
2001-02-28 18:54:51 -04:00
|
|
|
}
|
|
|
|
|
2001-02-27 00:23:34 -04:00
|
|
|
static int
|
|
|
|
symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
|
|
|
|
struct symbol_info *si)
|
|
|
|
{
|
2001-08-10 18:41:33 -03:00
|
|
|
if (c->c_future)
|
|
|
|
c->c_flags |= c->c_future->ff_features;
|
2001-06-18 19:08:13 -03:00
|
|
|
if (ste->ste_generator)
|
|
|
|
c->c_flags |= CO_GENERATOR;
|
2001-02-27 00:23:34 -04:00
|
|
|
if (ste->ste_type != TYPE_MODULE)
|
|
|
|
c->c_flags |= CO_NEWLOCALS;
|
|
|
|
if (ste->ste_type == TYPE_FUNCTION) {
|
|
|
|
c->c_nlocals = si->si_nlocals;
|
|
|
|
if (ste->ste_optimized == 0)
|
|
|
|
c->c_flags |= CO_OPTIMIZED;
|
2001-02-28 18:54:51 -04:00
|
|
|
else if (ste->ste_optimized != OPT_EXEC)
|
|
|
|
return symtable_check_unoptimized(c, ste, si);
|
2001-02-27 00:23:34 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-22 01:26:44 -03:00
|
|
|
static int
|
|
|
|
symtable_error(struct symtable *st, int lineno)
|
|
|
|
{
|
|
|
|
if (lineno == 0)
|
|
|
|
lineno = st->st_cur->ste_lineno;
|
|
|
|
PyErr_SyntaxLocation(st->st_filename, lineno);
|
|
|
|
st->st_errors++;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-02-27 00:23:34 -04:00
|
|
|
static int
|
|
|
|
symtable_load_symbols(struct compiling *c)
|
|
|
|
{
|
|
|
|
struct symtable *st = c->c_symtable;
|
|
|
|
PySymtableEntryObject *ste = st->st_cur;
|
|
|
|
PyObject *name, *varnames, *v;
|
|
|
|
int i, flags, pos;
|
|
|
|
struct symbol_info si;
|
|
|
|
|
|
|
|
v = NULL;
|
|
|
|
|
|
|
|
if (symtable_init_compiling_symbols(c) < 0)
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
goto fail;
|
2001-02-27 00:23:34 -04:00
|
|
|
symtable_init_info(&si);
|
|
|
|
varnames = st->st_cur->ste_varnames;
|
|
|
|
si.si_nlocals = PyList_GET_SIZE(varnames);
|
|
|
|
c->c_argcount = si.si_nlocals;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
|
2001-02-27 00:23:34 -04:00
|
|
|
for (i = 0; i < si.si_nlocals; ++i) {
|
2001-01-18 23:21:30 -04:00
|
|
|
v = PyInt_FromLong(i);
|
2003-05-03 07:53:08 -03:00
|
|
|
if (v == NULL)
|
|
|
|
goto fail;
|
2001-01-18 23:21:30 -04:00
|
|
|
if (PyDict_SetItem(c->c_locals,
|
|
|
|
PyList_GET_ITEM(varnames, i), v) < 0)
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
goto fail;
|
|
|
|
Py_DECREF(v);
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX The cases below define the rules for whether a name is
|
|
|
|
local or global. The logic could probably be clearer. */
|
|
|
|
pos = 0;
|
2001-02-09 18:22:18 -04:00
|
|
|
while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
|
|
|
|
flags = PyInt_AS_LONG(v);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
|
2001-02-09 18:22:18 -04:00
|
|
|
if (flags & DEF_FREE_GLOBAL)
|
2001-02-27 00:23:34 -04:00
|
|
|
/* undo the original DEF_FREE */
|
|
|
|
flags &= ~(DEF_FREE | DEF_FREE_CLASS);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
|
2001-03-19 20:25:43 -04:00
|
|
|
/* Deal with names that need two actions:
|
2001-08-11 18:51:24 -03:00
|
|
|
1. Cell variables that are also locals.
|
2001-03-19 20:25:43 -04:00
|
|
|
2. Free variables in methods that are also class
|
|
|
|
variables or declared global.
|
|
|
|
*/
|
2001-08-11 18:51:24 -03:00
|
|
|
if (flags & (DEF_FREE | DEF_FREE_CLASS))
|
2001-04-26 23:29:40 -03:00
|
|
|
symtable_resolve_free(c, name, flags, &si);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
|
2001-02-09 18:22:18 -04:00
|
|
|
if (flags & DEF_STAR) {
|
2001-01-18 23:21:30 -04:00
|
|
|
c->c_argcount--;
|
|
|
|
c->c_flags |= CO_VARARGS;
|
2001-02-09 18:22:18 -04:00
|
|
|
} else if (flags & DEF_DOUBLESTAR) {
|
2001-01-18 23:21:30 -04:00
|
|
|
c->c_argcount--;
|
|
|
|
c->c_flags |= CO_VARKEYWORDS;
|
2001-02-09 18:22:18 -04:00
|
|
|
} else if (flags & DEF_INTUPLE)
|
2001-01-18 23:21:30 -04:00
|
|
|
c->c_argcount--;
|
2001-02-09 18:22:18 -04:00
|
|
|
else if (flags & DEF_GLOBAL) {
|
2001-02-27 00:23:34 -04:00
|
|
|
if (flags & DEF_PARAM) {
|
2003-10-18 19:05:25 -03:00
|
|
|
PyErr_Format(PyExc_SyntaxError, PARAM_GLOBAL,
|
2001-02-02 16:01:10 -04:00
|
|
|
PyString_AS_STRING(name));
|
2003-09-22 01:26:44 -03:00
|
|
|
symtable_error(st, 0);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
goto fail;
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
|
|
|
|
goto fail;
|
2001-02-09 18:22:18 -04:00
|
|
|
} else if (flags & DEF_FREE_GLOBAL) {
|
2001-02-27 00:23:34 -04:00
|
|
|
si.si_nimplicit++;
|
2002-12-17 21:18:44 -04:00
|
|
|
if (PyDict_SetItem(c->c_globals, name, Py_True) < 0)
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
goto fail;
|
2001-02-09 18:22:18 -04:00
|
|
|
} else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
|
2001-02-27 00:23:34 -04:00
|
|
|
v = PyInt_FromLong(si.si_nlocals++);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (v == NULL)
|
|
|
|
goto fail;
|
2001-01-18 23:21:30 -04:00
|
|
|
if (PyDict_SetItem(c->c_locals, name, v) < 0)
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
goto fail;
|
|
|
|
Py_DECREF(v);
|
2001-02-09 18:22:18 -04:00
|
|
|
if (ste->ste_type != TYPE_CLASS)
|
2001-01-18 23:21:30 -04:00
|
|
|
if (PyList_Append(c->c_varnames, name) < 0)
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
goto fail;
|
2001-02-09 18:22:18 -04:00
|
|
|
} else if (is_free(flags)) {
|
2001-08-11 18:51:24 -03:00
|
|
|
if (ste->ste_nested) {
|
2001-02-27 00:23:34 -04:00
|
|
|
v = PyInt_FromLong(si.si_nfrees++);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (v == NULL)
|
|
|
|
goto fail;
|
2001-02-09 18:22:18 -04:00
|
|
|
if (PyDict_SetItem(c->c_freevars, name, v) < 0)
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
goto fail;
|
|
|
|
Py_DECREF(v);
|
|
|
|
} else {
|
2001-02-27 00:23:34 -04:00
|
|
|
si.si_nimplicit++;
|
2001-08-11 18:51:24 -03:00
|
|
|
if (PyDict_SetItem(c->c_globals, name,
|
2002-12-17 21:18:44 -04:00
|
|
|
Py_True) < 0)
|
2001-08-11 18:51:24 -03:00
|
|
|
goto fail;
|
|
|
|
if (st->st_nscopes != 1) {
|
|
|
|
v = PyInt_FromLong(flags);
|
2003-05-03 07:53:08 -03:00
|
|
|
if (v == NULL)
|
|
|
|
goto fail;
|
2001-08-11 18:51:24 -03:00
|
|
|
if (PyDict_SetItem(st->st_global,
|
|
|
|
name, v))
|
|
|
|
goto fail;
|
|
|
|
Py_DECREF(v);
|
|
|
|
}
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
}
|
2001-04-26 23:29:40 -03:00
|
|
|
assert(PyDict_Size(c->c_freevars) == si.si_nfrees);
|
|
|
|
|
2001-03-21 12:43:47 -04:00
|
|
|
if (si.si_ncells > 1) { /* one cell is always in order */
|
|
|
|
if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount,
|
|
|
|
c->c_varnames, c->c_flags) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2001-02-27 00:23:34 -04:00
|
|
|
if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
|
|
|
|
return -1;
|
|
|
|
return symtable_update_flags(c, ste, &si);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
fail:
|
|
|
|
/* is this always the right thing to do? */
|
|
|
|
Py_XDECREF(v);
|
|
|
|
return -1;
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct symtable *
|
2001-02-09 18:22:18 -04:00
|
|
|
symtable_init()
|
2001-01-18 23:21:30 -04:00
|
|
|
{
|
|
|
|
struct symtable *st;
|
|
|
|
|
2002-08-04 03:28:21 -03:00
|
|
|
st = (struct symtable *)PyObject_MALLOC(sizeof(struct symtable));
|
2001-01-18 23:21:30 -04:00
|
|
|
if (st == NULL)
|
|
|
|
return NULL;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
st->st_pass = 1;
|
2001-08-10 22:06:35 -03:00
|
|
|
|
2001-02-02 16:01:10 -04:00
|
|
|
st->st_filename = NULL;
|
2003-05-03 07:53:08 -03:00
|
|
|
st->st_symbols = NULL;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if ((st->st_stack = PyList_New(0)) == NULL)
|
2001-01-18 23:21:30 -04:00
|
|
|
goto fail;
|
|
|
|
if ((st->st_symbols = PyDict_New()) == NULL)
|
|
|
|
goto fail;
|
|
|
|
st->st_cur = NULL;
|
2001-02-02 14:19:15 -04:00
|
|
|
st->st_nscopes = 0;
|
2001-01-18 23:21:30 -04:00
|
|
|
st->st_errors = 0;
|
|
|
|
st->st_private = NULL;
|
|
|
|
return st;
|
|
|
|
fail:
|
2001-02-02 14:19:15 -04:00
|
|
|
PySymtable_Free(st);
|
2001-01-18 23:21:30 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-02-02 14:19:15 -04:00
|
|
|
void
|
|
|
|
PySymtable_Free(struct symtable *st)
|
2001-01-18 23:21:30 -04:00
|
|
|
{
|
|
|
|
Py_XDECREF(st->st_symbols);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
Py_XDECREF(st->st_stack);
|
2001-02-23 13:55:27 -04:00
|
|
|
Py_XDECREF(st->st_cur);
|
2002-08-04 03:28:21 -03:00
|
|
|
PyObject_FREE((void *)st);
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
/* When the compiler exits a scope, it must should update the scope's
|
|
|
|
free variable information with the list of free variables in its
|
|
|
|
children.
|
|
|
|
|
|
|
|
Variables that are free in children and defined in the current
|
|
|
|
scope are cellvars.
|
|
|
|
|
2001-02-09 18:22:18 -04:00
|
|
|
If the scope being exited is defined at the top-level (ste_nested is
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
false), free variables in children that are not defined here are
|
|
|
|
implicit globals.
|
|
|
|
|
|
|
|
*/
|
2001-01-18 23:21:30 -04:00
|
|
|
|
|
|
|
static int
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
symtable_update_free_vars(struct symtable *st)
|
|
|
|
{
|
2001-02-12 12:01:03 -04:00
|
|
|
int i, j, def;
|
|
|
|
PyObject *o, *name, *list = NULL;
|
2001-02-09 18:22:18 -04:00
|
|
|
PySymtableEntryObject *child, *ste = st->st_cur;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
|
2001-02-09 18:22:18 -04:00
|
|
|
if (ste->ste_type == TYPE_CLASS)
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
def = DEF_FREE_CLASS;
|
|
|
|
else
|
|
|
|
def = DEF_FREE;
|
2001-02-09 18:22:18 -04:00
|
|
|
for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
int pos = 0;
|
|
|
|
|
2003-05-22 13:22:33 -03:00
|
|
|
if (list && PyList_SetSlice(list, 0,
|
|
|
|
PyList_GET_SIZE(list), 0) < 0)
|
2003-05-03 07:53:08 -03:00
|
|
|
return -1;
|
2001-02-23 14:22:59 -04:00
|
|
|
child = (PySymtableEntryObject *)
|
2001-02-09 18:22:18 -04:00
|
|
|
PyList_GET_ITEM(ste->ste_children, i);
|
|
|
|
while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
|
2001-03-01 02:09:34 -04:00
|
|
|
int flags = PyInt_AS_LONG(o);
|
|
|
|
if (!(is_free(flags)))
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
continue; /* avoids indentation */
|
2001-02-12 12:01:03 -04:00
|
|
|
if (list == NULL) {
|
|
|
|
list = PyList_New(0);
|
|
|
|
if (list == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
2001-02-09 18:22:18 -04:00
|
|
|
ste->ste_child_free = 1;
|
2001-02-12 12:01:03 -04:00
|
|
|
if (PyList_Append(list, name) < 0) {
|
|
|
|
Py_DECREF(list);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
|
2001-03-01 02:09:34 -04:00
|
|
|
PyObject *v;
|
2001-02-12 12:01:03 -04:00
|
|
|
name = PyList_GET_ITEM(list, j);
|
2001-03-01 02:09:34 -04:00
|
|
|
v = PyDict_GetItem(ste->ste_symbols, name);
|
|
|
|
/* If a name N is declared global in scope A and
|
|
|
|
referenced in scope B contained (perhaps
|
|
|
|
indirectly) in A and there are no scopes
|
|
|
|
with bindings for N between B and A, then N
|
2001-05-08 01:12:34 -03:00
|
|
|
is global in B. Unless A is a class scope,
|
|
|
|
because class scopes are not considered for
|
|
|
|
nested scopes.
|
2001-03-01 02:09:34 -04:00
|
|
|
*/
|
2001-03-19 20:25:43 -04:00
|
|
|
if (v && (ste->ste_type != TYPE_CLASS)) {
|
2001-03-01 02:09:34 -04:00
|
|
|
int flags = PyInt_AS_LONG(v);
|
|
|
|
if (flags & DEF_GLOBAL) {
|
|
|
|
symtable_undo_free(st, child->ste_id,
|
|
|
|
name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2001-02-09 18:22:18 -04:00
|
|
|
if (ste->ste_nested) {
|
|
|
|
if (symtable_add_def_o(st, ste->ste_symbols,
|
2001-02-12 12:01:03 -04:00
|
|
|
name, def) < 0) {
|
|
|
|
Py_DECREF(list);
|
|
|
|
return -1;
|
|
|
|
}
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
} else {
|
2001-02-09 18:22:18 -04:00
|
|
|
if (symtable_check_global(st, child->ste_id,
|
2001-02-12 12:01:03 -04:00
|
|
|
name) < 0) {
|
|
|
|
Py_DECREF(list);
|
|
|
|
return -1;
|
|
|
|
}
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-02-12 12:01:03 -04:00
|
|
|
|
|
|
|
Py_XDECREF(list);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the current scope is a non-nested class or if name is not
|
|
|
|
defined in the current, non-nested scope, then it is an implicit
|
|
|
|
global in all nested scopes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
|
2001-01-18 23:21:30 -04:00
|
|
|
{
|
|
|
|
PyObject *o;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
int v;
|
2001-02-09 18:22:18 -04:00
|
|
|
PySymtableEntryObject *ste = st->st_cur;
|
2001-03-01 02:09:34 -04:00
|
|
|
|
2001-02-09 18:22:18 -04:00
|
|
|
if (ste->ste_type == TYPE_CLASS)
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
return symtable_undo_free(st, child, name);
|
2001-02-09 18:22:18 -04:00
|
|
|
o = PyDict_GetItem(ste->ste_symbols, name);
|
2001-01-18 23:21:30 -04:00
|
|
|
if (o == NULL)
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
return symtable_undo_free(st, child, name);
|
|
|
|
v = PyInt_AS_LONG(o);
|
2001-03-01 02:09:34 -04:00
|
|
|
|
2001-01-29 21:24:43 -04:00
|
|
|
if (is_free(v) || (v & DEF_GLOBAL))
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
return symtable_undo_free(st, child, name);
|
|
|
|
else
|
2001-02-09 18:22:18 -04:00
|
|
|
return symtable_add_def_o(st, ste->ste_symbols,
|
|
|
|
name, DEF_FREE);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
symtable_undo_free(struct symtable *st, PyObject *id,
|
|
|
|
PyObject *name)
|
|
|
|
{
|
|
|
|
int i, v, x;
|
2001-02-09 18:22:18 -04:00
|
|
|
PyObject *info;
|
|
|
|
PySymtableEntryObject *ste;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
|
2001-02-09 18:22:18 -04:00
|
|
|
ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
|
|
|
|
if (ste == NULL)
|
2001-01-18 23:21:30 -04:00
|
|
|
return -1;
|
2001-03-01 02:09:34 -04:00
|
|
|
|
2001-02-09 18:22:18 -04:00
|
|
|
info = PyDict_GetItem(ste->ste_symbols, name);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (info == NULL)
|
|
|
|
return 0;
|
|
|
|
v = PyInt_AS_LONG(info);
|
|
|
|
if (is_free(v)) {
|
2001-02-09 18:22:18 -04:00
|
|
|
if (symtable_add_def_o(st, ste->ste_symbols, name,
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
DEF_FREE_GLOBAL) < 0)
|
|
|
|
return -1;
|
|
|
|
} else
|
|
|
|
/* If the name is defined here or declared global,
|
|
|
|
then the recursion stops. */
|
|
|
|
return 0;
|
|
|
|
|
2001-02-09 18:22:18 -04:00
|
|
|
for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
|
|
|
|
PySymtableEntryObject *child;
|
2001-02-23 14:22:59 -04:00
|
|
|
child = (PySymtableEntryObject *)
|
2001-02-09 18:22:18 -04:00
|
|
|
PyList_GET_ITEM(ste->ste_children, i);
|
|
|
|
x = symtable_undo_free(st, child->ste_id, name);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (x < 0)
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-27 00:23:34 -04:00
|
|
|
/* symtable_enter_scope() gets a reference via PySymtableEntry_New().
|
|
|
|
This reference is released when the scope is exited, via the DECREF
|
|
|
|
in symtable_exit_scope().
|
|
|
|
*/
|
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
static int
|
|
|
|
symtable_exit_scope(struct symtable *st)
|
|
|
|
{
|
|
|
|
int end;
|
|
|
|
|
2001-02-28 18:54:51 -04:00
|
|
|
if (st->st_pass == 1)
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
symtable_update_free_vars(st);
|
2001-02-09 18:22:18 -04:00
|
|
|
Py_DECREF(st->st_cur);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
end = PyList_GET_SIZE(st->st_stack) - 1;
|
2001-02-09 18:22:18 -04:00
|
|
|
st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
|
|
|
|
end);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (PySequence_DelItem(st->st_stack, end) < 0)
|
2001-01-18 23:21:30 -04:00
|
|
|
return -1;
|
2001-02-09 18:22:18 -04:00
|
|
|
return 0;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
}
|
|
|
|
|
2001-02-02 14:19:15 -04:00
|
|
|
static void
|
|
|
|
symtable_enter_scope(struct symtable *st, char *name, int type,
|
|
|
|
int lineno)
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
{
|
2001-02-09 18:22:18 -04:00
|
|
|
PySymtableEntryObject *prev = NULL;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
|
|
|
|
if (st->st_cur) {
|
2001-02-09 18:22:18 -04:00
|
|
|
prev = st->st_cur;
|
|
|
|
if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
|
2001-02-02 14:19:15 -04:00
|
|
|
st->st_errors++;
|
|
|
|
return;
|
|
|
|
}
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
}
|
2001-02-23 14:22:59 -04:00
|
|
|
st->st_cur = (PySymtableEntryObject *)
|
2001-02-09 18:22:18 -04:00
|
|
|
PySymtableEntry_New(st, name, type, lineno);
|
2003-05-03 07:53:08 -03:00
|
|
|
if (st->st_cur == NULL) {
|
|
|
|
st->st_errors++;
|
|
|
|
return;
|
|
|
|
}
|
2001-02-09 18:22:18 -04:00
|
|
|
if (strcmp(name, TOP) == 0)
|
|
|
|
st->st_global = st->st_cur->ste_symbols;
|
2001-02-23 13:55:27 -04:00
|
|
|
if (prev && st->st_pass == 1) {
|
2001-02-09 18:22:18 -04:00
|
|
|
if (PyList_Append(prev->ste_children,
|
|
|
|
(PyObject *)st->st_cur) < 0)
|
2001-02-02 14:19:15 -04:00
|
|
|
st->st_errors++;
|
2001-02-23 13:55:27 -04:00
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
|
2001-02-27 00:23:34 -04:00
|
|
|
static int
|
|
|
|
symtable_lookup(struct symtable *st, char *name)
|
|
|
|
{
|
|
|
|
char buffer[MANGLE_LEN];
|
|
|
|
PyObject *v;
|
|
|
|
int flags;
|
|
|
|
|
2002-06-20 19:23:15 -03:00
|
|
|
if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer)))
|
2001-02-27 00:23:34 -04:00
|
|
|
name = buffer;
|
|
|
|
v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
|
|
|
|
if (v == NULL) {
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = PyInt_AS_LONG(v);
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2001-01-18 23:21:30 -04:00
|
|
|
static int
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
symtable_add_def(struct symtable *st, char *name, int flag)
|
2001-01-18 23:21:30 -04:00
|
|
|
{
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
PyObject *s;
|
2001-01-18 23:21:30 -04:00
|
|
|
char buffer[MANGLE_LEN];
|
2001-02-27 21:58:08 -04:00
|
|
|
int ret;
|
2001-01-18 23:21:30 -04:00
|
|
|
|
2002-08-15 23:48:11 -03:00
|
|
|
/* Warn about None, except inside a tuple (where the assignment
|
|
|
|
code already issues a warning). */
|
|
|
|
if ((flag & DEF_PARAM) && !(flag & DEF_INTUPLE) &&
|
|
|
|
*name == 'N' && strcmp(name, "None") == 0)
|
|
|
|
{
|
2004-07-17 18:46:25 -03:00
|
|
|
PyErr_SetString(PyExc_SyntaxError,
|
|
|
|
"Invalid syntax. Assignment to None.");
|
|
|
|
symtable_error(st, 0);
|
|
|
|
return -1;
|
2002-08-15 23:48:11 -03:00
|
|
|
}
|
2002-06-20 19:23:15 -03:00
|
|
|
if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer)))
|
2001-01-18 23:21:30 -04:00
|
|
|
name = buffer;
|
|
|
|
if ((s = PyString_InternFromString(name)) == NULL)
|
|
|
|
return -1;
|
2001-02-27 21:58:08 -04:00
|
|
|
ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
|
|
|
|
Py_DECREF(s);
|
|
|
|
return ret;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Must only be called with mangled names */
|
|
|
|
|
|
|
|
static int
|
|
|
|
symtable_add_def_o(struct symtable *st, PyObject *dict,
|
|
|
|
PyObject *name, int flag)
|
|
|
|
{
|
|
|
|
PyObject *o;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
if ((o = PyDict_GetItem(dict, name))) {
|
2001-01-18 23:21:30 -04:00
|
|
|
val = PyInt_AS_LONG(o);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
|
2001-01-29 21:24:43 -04:00
|
|
|
PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
|
2001-02-01 16:20:45 -04:00
|
|
|
PyString_AsString(name));
|
2003-09-22 01:26:44 -03:00
|
|
|
return symtable_error(st, 0);
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
val |= flag;
|
2001-01-18 23:21:30 -04:00
|
|
|
} else
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
val = flag;
|
2001-01-18 23:21:30 -04:00
|
|
|
o = PyInt_FromLong(val);
|
2003-05-03 07:53:08 -03:00
|
|
|
if (o == NULL)
|
|
|
|
return -1;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (PyDict_SetItem(dict, name, o) < 0) {
|
2001-01-18 23:21:30 -04:00
|
|
|
Py_DECREF(o);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(o);
|
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (flag & DEF_PARAM) {
|
2001-02-09 18:22:18 -04:00
|
|
|
if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
|
2001-01-18 23:21:30 -04:00
|
|
|
return -1;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
} else if (flag & DEF_GLOBAL) {
|
|
|
|
/* XXX need to update DEF_GLOBAL for other flags too;
|
|
|
|
perhaps only DEF_FREE_GLOBAL */
|
|
|
|
if ((o = PyDict_GetItem(st->st_global, name))) {
|
2001-01-18 23:21:30 -04:00
|
|
|
val = PyInt_AS_LONG(o);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
val |= flag;
|
2001-01-18 23:21:30 -04:00
|
|
|
} else
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
val = flag;
|
2001-01-18 23:21:30 -04:00
|
|
|
o = PyInt_FromLong(val);
|
2003-05-03 07:53:08 -03:00
|
|
|
if (o == NULL)
|
|
|
|
return -1;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (PyDict_SetItem(st->st_global, name, o) < 0) {
|
2001-01-18 23:21:30 -04:00
|
|
|
Py_DECREF(o);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(o);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
#define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
|
2001-01-18 23:21:30 -04:00
|
|
|
|
2001-06-27 22:52:22 -03:00
|
|
|
/* Look for a yield stmt under n. Return 1 if found, else 0.
|
|
|
|
This hack is used to look inside "if 0:" blocks (which are normally
|
|
|
|
ignored) in case those are the only places a yield occurs (so that this
|
|
|
|
function is a generator). */
|
2001-06-26 00:36:28 -03:00
|
|
|
static int
|
|
|
|
look_for_yield(node *n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NCH(n); ++i) {
|
|
|
|
node *kid = CHILD(n, i);
|
|
|
|
|
|
|
|
switch (TYPE(kid)) {
|
|
|
|
|
|
|
|
case classdef:
|
|
|
|
case funcdef:
|
2001-06-27 22:52:22 -03:00
|
|
|
case lambdef:
|
2001-06-26 00:36:28 -03:00
|
|
|
/* Stuff in nested functions and classes can't make
|
|
|
|
the parent a generator. */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case yield_stmt:
|
2004-05-19 05:20:33 -03:00
|
|
|
return GENERATOR;
|
2001-06-26 00:36:28 -03:00
|
|
|
|
|
|
|
default:
|
|
|
|
if (look_for_yield(kid))
|
2004-05-19 05:20:33 -03:00
|
|
|
return GENERATOR;
|
2001-06-26 00:36:28 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-18 23:21:30 -04:00
|
|
|
static void
|
|
|
|
symtable_node(struct symtable *st, node *n)
|
|
|
|
{
|
2001-10-18 13:15:10 -03:00
|
|
|
int i;
|
2001-01-18 23:21:30 -04:00
|
|
|
|
|
|
|
loop:
|
|
|
|
switch (TYPE(n)) {
|
|
|
|
case funcdef: {
|
2004-08-02 03:10:11 -03:00
|
|
|
char *func_name;
|
|
|
|
if (NCH(n) == 6)
|
|
|
|
symtable_node(st, CHILD(n, 0));
|
|
|
|
func_name = STR(RCHILD(n, -4));
|
2001-01-18 23:21:30 -04:00
|
|
|
symtable_add_def(st, func_name, DEF_LOCAL);
|
2004-08-02 03:10:11 -03:00
|
|
|
symtable_default_args(st, RCHILD(n, -3));
|
2001-02-02 14:19:15 -04:00
|
|
|
symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
|
2001-01-18 23:21:30 -04:00
|
|
|
symtable_funcdef(st, n);
|
|
|
|
symtable_exit_scope(st);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case lambdef:
|
|
|
|
if (NCH(n) == 4)
|
|
|
|
symtable_default_args(st, CHILD(n, 1));
|
2001-02-02 14:19:15 -04:00
|
|
|
symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
|
2001-01-18 23:21:30 -04:00
|
|
|
symtable_funcdef(st, n);
|
|
|
|
symtable_exit_scope(st);
|
|
|
|
break;
|
|
|
|
case classdef: {
|
|
|
|
char *tmp, *class_name = STR(CHILD(n, 1));
|
|
|
|
symtable_add_def(st, class_name, DEF_LOCAL);
|
|
|
|
if (TYPE(CHILD(n, 2)) == LPAR) {
|
|
|
|
node *bases = CHILD(n, 3);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < NCH(bases); i += 2) {
|
|
|
|
symtable_node(st, CHILD(bases, i));
|
|
|
|
}
|
|
|
|
}
|
2001-02-02 14:19:15 -04:00
|
|
|
symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
|
2001-01-18 23:21:30 -04:00
|
|
|
tmp = st->st_private;
|
|
|
|
st->st_private = class_name;
|
|
|
|
symtable_node(st, CHILD(n, NCH(n) - 1));
|
|
|
|
st->st_private = tmp;
|
|
|
|
symtable_exit_scope(st);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case if_stmt:
|
|
|
|
for (i = 0; i + 3 < NCH(n); i += 4) {
|
2001-06-26 00:36:28 -03:00
|
|
|
if (is_constant_false(NULL, (CHILD(n, i + 1)))) {
|
|
|
|
if (st->st_cur->ste_generator == 0)
|
|
|
|
st->st_cur->ste_generator =
|
|
|
|
look_for_yield(CHILD(n, i+3));
|
2001-01-18 23:21:30 -04:00
|
|
|
continue;
|
2001-06-26 00:36:28 -03:00
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
symtable_node(st, CHILD(n, i + 1));
|
|
|
|
symtable_node(st, CHILD(n, i + 3));
|
|
|
|
}
|
|
|
|
if (i + 2 < NCH(n))
|
|
|
|
symtable_node(st, CHILD(n, i + 2));
|
|
|
|
break;
|
|
|
|
case global_stmt:
|
|
|
|
symtable_global(st, n);
|
|
|
|
break;
|
|
|
|
case import_stmt:
|
|
|
|
symtable_import(st, n);
|
|
|
|
break;
|
2001-02-01 16:20:45 -04:00
|
|
|
case exec_stmt: {
|
2001-02-27 00:23:34 -04:00
|
|
|
st->st_cur->ste_optimized |= OPT_EXEC;
|
2001-01-18 23:21:30 -04:00
|
|
|
symtable_node(st, CHILD(n, 1));
|
|
|
|
if (NCH(n) > 2)
|
|
|
|
symtable_node(st, CHILD(n, 3));
|
2001-03-21 23:57:58 -04:00
|
|
|
else {
|
2001-02-27 00:23:34 -04:00
|
|
|
st->st_cur->ste_optimized |= OPT_BARE_EXEC;
|
2001-03-21 23:57:58 -04:00
|
|
|
st->st_cur->ste_opt_lineno = n->n_lineno;
|
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
if (NCH(n) > 4)
|
|
|
|
symtable_node(st, CHILD(n, 5));
|
|
|
|
break;
|
2001-02-01 16:20:45 -04:00
|
|
|
|
|
|
|
}
|
2001-02-19 11:50:51 -04:00
|
|
|
case assert_stmt:
|
|
|
|
if (Py_OptimizeFlag)
|
|
|
|
return;
|
|
|
|
if (NCH(n) == 2) {
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
goto loop;
|
|
|
|
} else {
|
|
|
|
symtable_node(st, CHILD(n, 1));
|
|
|
|
n = CHILD(n, 3);
|
|
|
|
goto loop;
|
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
case except_clause:
|
|
|
|
if (NCH(n) == 4)
|
2001-01-29 21:24:43 -04:00
|
|
|
symtable_assign(st, CHILD(n, 3), 0);
|
2001-01-18 23:21:30 -04:00
|
|
|
if (NCH(n) > 1) {
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case del_stmt:
|
2001-01-29 21:24:43 -04:00
|
|
|
symtable_assign(st, CHILD(n, 1), 0);
|
2001-01-18 23:21:30 -04:00
|
|
|
break;
|
2001-06-18 19:08:13 -03:00
|
|
|
case yield_stmt:
|
|
|
|
st->st_cur->ste_generator = 1;
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
goto loop;
|
2001-01-18 23:21:30 -04:00
|
|
|
case expr_stmt:
|
|
|
|
if (NCH(n) == 1)
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
else {
|
|
|
|
if (TYPE(CHILD(n, 1)) == augassign) {
|
2001-01-29 21:24:43 -04:00
|
|
|
symtable_assign(st, CHILD(n, 0), 0);
|
2001-01-18 23:21:30 -04:00
|
|
|
symtable_node(st, CHILD(n, 2));
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < NCH(n) - 2; i += 2)
|
2001-01-29 21:24:43 -04:00
|
|
|
symtable_assign(st, CHILD(n, i), 0);
|
2001-01-18 23:21:30 -04:00
|
|
|
n = CHILD(n, NCH(n) - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto loop;
|
2001-10-18 13:15:10 -03:00
|
|
|
case list_iter:
|
2003-05-21 14:34:50 -03:00
|
|
|
/* only occurs when there are multiple for loops
|
|
|
|
in a list comprehension */
|
2001-10-18 13:15:10 -03:00
|
|
|
n = CHILD(n, 0);
|
2003-05-21 14:34:50 -03:00
|
|
|
if (TYPE(n) == list_for)
|
|
|
|
symtable_list_for(st, n);
|
|
|
|
else {
|
2001-10-18 13:15:10 -03:00
|
|
|
REQ(n, list_if);
|
|
|
|
symtable_node(st, CHILD(n, 1));
|
|
|
|
if (NCH(n) == 3) {
|
|
|
|
n = CHILD(n, 2);
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case for_stmt:
|
|
|
|
symtable_assign(st, CHILD(n, 1), 0);
|
|
|
|
for (i = 3; i < NCH(n); ++i)
|
|
|
|
if (TYPE(CHILD(n, i)) >= single_input)
|
|
|
|
symtable_node(st, CHILD(n, i));
|
|
|
|
break;
|
2004-05-19 05:20:33 -03:00
|
|
|
case arglist:
|
|
|
|
if (NCH(n) > 1)
|
|
|
|
for (i = 0; i < NCH(n); ++i) {
|
|
|
|
node *ch = CHILD(n, i);
|
|
|
|
if (TYPE(ch) == argument && NCH(ch) == 2 &&
|
|
|
|
TYPE(CHILD(ch, 1)) == gen_for) {
|
|
|
|
PyErr_SetString(PyExc_SyntaxError,
|
|
|
|
"invalid syntax");
|
|
|
|
symtable_error(st, n->n_lineno);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2001-10-18 13:15:10 -03:00
|
|
|
/* The remaining cases fall through to default except in
|
|
|
|
special circumstances. This requires the individual cases
|
|
|
|
to be coded with great care, even though they look like
|
|
|
|
rather innocuous. Each case must double-check TYPE(n).
|
|
|
|
*/
|
2004-08-02 03:10:11 -03:00
|
|
|
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 */
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
case argument:
|
2001-10-18 13:15:10 -03:00
|
|
|
if (TYPE(n) == argument && NCH(n) == 3) {
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
n = CHILD(n, 2);
|
|
|
|
goto loop;
|
|
|
|
}
|
2004-05-19 05:20:33 -03:00
|
|
|
else if (TYPE(n) == argument && NCH(n) == 2 &&
|
|
|
|
TYPE(CHILD(n, 1)) == gen_for) {
|
|
|
|
symtable_generator_expression(st, n);
|
|
|
|
break;
|
|
|
|
}
|
2001-10-18 13:15:10 -03:00
|
|
|
/* fall through */
|
2001-01-18 23:21:30 -04:00
|
|
|
case listmaker:
|
|
|
|
if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
|
2003-05-21 14:34:50 -03:00
|
|
|
symtable_list_comprehension(st, n);
|
2001-10-18 13:15:10 -03:00
|
|
|
break;
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
2001-10-18 13:15:10 -03:00
|
|
|
/* fall through */
|
2004-05-19 05:20:33 -03:00
|
|
|
case testlist_gexp:
|
|
|
|
if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) {
|
|
|
|
symtable_generator_expression(st, n);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
|
2001-01-18 23:21:30 -04:00
|
|
|
case atom:
|
|
|
|
if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
|
|
|
|
symtable_add_use(st, STR(CHILD(n, 0)));
|
|
|
|
break;
|
|
|
|
}
|
2001-10-18 13:15:10 -03:00
|
|
|
/* fall through */
|
2001-01-18 23:21:30 -04:00
|
|
|
default:
|
2001-10-18 13:15:10 -03:00
|
|
|
/* Walk over every non-token child with a special case
|
|
|
|
for one child.
|
|
|
|
*/
|
2001-01-18 23:21:30 -04:00
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto loop;
|
|
|
|
}
|
2001-10-18 13:15:10 -03:00
|
|
|
for (i = 0; i < NCH(n); ++i)
|
2001-01-18 23:21:30 -04:00
|
|
|
if (TYPE(CHILD(n, i)) >= single_input)
|
|
|
|
symtable_node(st, CHILD(n, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
symtable_funcdef(struct symtable *st, node *n)
|
|
|
|
{
|
|
|
|
node *body;
|
|
|
|
|
|
|
|
if (TYPE(n) == lambdef) {
|
|
|
|
if (NCH(n) == 4)
|
|
|
|
symtable_params(st, CHILD(n, 1));
|
|
|
|
} else
|
2004-08-02 03:10:11 -03:00
|
|
|
symtable_params(st, RCHILD(n, -3));
|
2001-01-18 23:21:30 -04:00
|
|
|
body = CHILD(n, NCH(n) - 1);
|
|
|
|
symtable_node(st, body);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The next two functions parse the argument tuple.
|
2002-08-15 23:48:11 -03:00
|
|
|
symtable_default_args() checks for names in the default arguments,
|
2001-01-18 23:21:30 -04:00
|
|
|
which are references in the defining scope. symtable_params()
|
|
|
|
parses the parameter names, which are defined in the function's
|
|
|
|
body.
|
|
|
|
|
|
|
|
varargslist:
|
|
|
|
(fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
|
|
|
|
| fpdef ['=' test] (',' fpdef ['=' test])* [',']
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
symtable_default_args(struct symtable *st, node *n)
|
|
|
|
{
|
|
|
|
node *c;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (TYPE(n) == parameters) {
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
if (TYPE(n) == RPAR)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
REQ(n, varargslist);
|
|
|
|
for (i = 0; i < NCH(n); i += 2) {
|
|
|
|
c = CHILD(n, i);
|
|
|
|
if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
|
|
|
|
symtable_node(st, CHILD(n, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
symtable_params(struct symtable *st, node *n)
|
|
|
|
{
|
2001-01-25 13:01:49 -04:00
|
|
|
int i, complex = -1, ext = 0;
|
2002-12-23 12:51:42 -04:00
|
|
|
node *c = NULL;
|
2001-01-18 23:21:30 -04:00
|
|
|
|
|
|
|
if (TYPE(n) == parameters) {
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
if (TYPE(n) == RPAR)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
REQ(n, varargslist);
|
|
|
|
for (i = 0; i < NCH(n); i += 2) {
|
|
|
|
c = CHILD(n, i);
|
|
|
|
if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
|
|
|
|
ext = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (TYPE(c) == test) {
|
|
|
|
continue;
|
|
|
|
}
|
2002-12-23 12:51:42 -04:00
|
|
|
if (TYPE(CHILD(c, 0)) == NAME)
|
2001-01-18 23:21:30 -04:00
|
|
|
symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
|
2002-12-23 12:51:42 -04:00
|
|
|
else {
|
2001-11-28 17:10:39 -04:00
|
|
|
char nbuf[30];
|
|
|
|
PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
|
2001-01-18 23:21:30 -04:00
|
|
|
symtable_add_def(st, nbuf, DEF_PARAM);
|
2001-01-25 13:01:49 -04:00
|
|
|
complex = i;
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ext) {
|
|
|
|
c = CHILD(n, i);
|
|
|
|
if (TYPE(c) == STAR) {
|
|
|
|
i++;
|
|
|
|
symtable_add_def(st, STR(CHILD(n, i)),
|
|
|
|
DEF_PARAM | DEF_STAR);
|
|
|
|
i += 2;
|
2001-01-22 20:50:52 -04:00
|
|
|
if (i >= NCH(n))
|
2001-01-25 13:01:49 -04:00
|
|
|
c = NULL;
|
|
|
|
else
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
c = CHILD(n, i);
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
2001-01-25 13:01:49 -04:00
|
|
|
if (c && TYPE(c) == DOUBLESTAR) {
|
2001-01-18 23:21:30 -04:00
|
|
|
i++;
|
|
|
|
symtable_add_def(st, STR(CHILD(n, i)),
|
|
|
|
DEF_PARAM | DEF_DOUBLESTAR);
|
|
|
|
}
|
|
|
|
}
|
2001-01-25 13:01:49 -04:00
|
|
|
if (complex >= 0) {
|
|
|
|
int j;
|
|
|
|
for (j = 0; j <= complex; j++) {
|
|
|
|
c = CHILD(n, j);
|
|
|
|
if (TYPE(c) == COMMA)
|
2001-02-19 19:52:49 -04:00
|
|
|
c = CHILD(n, ++j);
|
|
|
|
else if (TYPE(c) == EQUAL)
|
|
|
|
c = CHILD(n, j += 3);
|
2001-01-25 13:01:49 -04:00
|
|
|
if (TYPE(CHILD(c, 0)) == LPAR)
|
|
|
|
symtable_params_fplist(st, CHILD(c, 1));
|
|
|
|
}
|
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
symtable_params_fplist(struct symtable *st, node *n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
node *c;
|
|
|
|
|
|
|
|
REQ(n, fplist);
|
|
|
|
for (i = 0; i < NCH(n); i += 2) {
|
|
|
|
c = CHILD(n, i);
|
|
|
|
REQ(c, fpdef);
|
|
|
|
if (NCH(c) == 1)
|
|
|
|
symtable_add_def(st, STR(CHILD(c, 0)),
|
|
|
|
DEF_PARAM | DEF_INTUPLE);
|
|
|
|
else
|
|
|
|
symtable_params_fplist(st, CHILD(c, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
symtable_global(struct symtable *st, node *n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2001-03-01 18:59:14 -04:00
|
|
|
/* XXX It might be helpful to warn about module-level global
|
|
|
|
statements, but it's hard to tell the difference between
|
|
|
|
module-level and a string passed to exec.
|
|
|
|
*/
|
2001-02-28 19:44:45 -04:00
|
|
|
|
2001-02-27 00:23:34 -04:00
|
|
|
for (i = 1; i < NCH(n); i += 2) {
|
|
|
|
char *name = STR(CHILD(n, i));
|
2001-02-27 01:15:57 -04:00
|
|
|
int flags;
|
|
|
|
|
|
|
|
flags = symtable_lookup(st, name);
|
2001-02-27 21:58:08 -04:00
|
|
|
if (flags < 0)
|
|
|
|
continue;
|
2001-02-27 01:15:57 -04:00
|
|
|
if (flags && flags != DEF_GLOBAL) {
|
|
|
|
char buf[500];
|
|
|
|
if (flags & DEF_PARAM) {
|
2003-10-18 19:05:25 -03:00
|
|
|
PyErr_Format(PyExc_SyntaxError, PARAM_GLOBAL,
|
2001-02-27 21:58:08 -04:00
|
|
|
name);
|
2003-09-22 01:26:44 -03:00
|
|
|
symtable_error(st, 0);
|
2001-02-27 01:15:57 -04:00
|
|
|
return;
|
2001-02-28 17:55:38 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (flags & DEF_LOCAL)
|
2001-11-28 17:10:39 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
|
|
GLOBAL_AFTER_ASSIGN,
|
|
|
|
name);
|
2001-02-28 17:55:38 -04:00
|
|
|
else
|
2001-11-28 17:10:39 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
|
|
GLOBAL_AFTER_USE, name);
|
2001-02-28 18:08:12 -04:00
|
|
|
symtable_warn(st, buf);
|
2001-02-27 01:15:57 -04:00
|
|
|
}
|
|
|
|
}
|
2001-02-27 00:23:34 -04:00
|
|
|
symtable_add_def(st, name, DEF_GLOBAL);
|
|
|
|
}
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
symtable_list_comprehension(struct symtable *st, node *n)
|
|
|
|
{
|
2003-05-21 14:34:50 -03:00
|
|
|
/* listmaker: test list_for */
|
2001-11-28 17:10:39 -04:00
|
|
|
char tmpname[30];
|
2001-01-18 23:21:30 -04:00
|
|
|
|
2003-05-21 14:34:50 -03:00
|
|
|
REQ(n, listmaker);
|
|
|
|
PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
|
|
|
|
++st->st_cur->ste_tmpname);
|
2001-01-18 23:21:30 -04:00
|
|
|
symtable_add_def(st, tmpname, DEF_LOCAL);
|
2003-05-21 14:34:50 -03:00
|
|
|
symtable_list_for(st, CHILD(n, 1));
|
|
|
|
symtable_node(st, CHILD(n, 0));
|
|
|
|
--st->st_cur->ste_tmpname;
|
|
|
|
}
|
|
|
|
|
2004-05-19 05:20:33 -03:00
|
|
|
static void
|
|
|
|
symtable_generator_expression(struct symtable *st, node *n)
|
|
|
|
{
|
|
|
|
/* testlist_gexp: test gen_for */
|
|
|
|
REQ(CHILD(n, 0), test);
|
|
|
|
REQ(CHILD(n, 1), gen_for);
|
|
|
|
|
|
|
|
symtable_enter_scope(st, "<genexpr>", TYPE(n), n->n_lineno);
|
|
|
|
st->st_cur->ste_generator = GENERATOR_EXPRESSION;
|
|
|
|
|
|
|
|
symtable_add_def(st, "[outmost-iterable]", DEF_PARAM);
|
|
|
|
|
|
|
|
symtable_gen_for(st, CHILD(n, 1), 1);
|
|
|
|
symtable_node(st, CHILD(n, 0));
|
|
|
|
symtable_exit_scope(st);
|
|
|
|
|
|
|
|
/* for outmost iterable precomputation */
|
|
|
|
symtable_node(st, CHILD(CHILD(n, 1), 3));
|
|
|
|
}
|
|
|
|
|
2003-05-21 14:34:50 -03:00
|
|
|
static void
|
|
|
|
symtable_list_for(struct symtable *st, node *n)
|
|
|
|
{
|
|
|
|
REQ(n, list_for);
|
|
|
|
/* list_for: for v in expr [list_iter] */
|
2001-01-29 21:24:43 -04:00
|
|
|
symtable_assign(st, CHILD(n, 1), 0);
|
2001-01-18 23:21:30 -04:00
|
|
|
symtable_node(st, CHILD(n, 3));
|
|
|
|
if (NCH(n) == 5)
|
|
|
|
symtable_node(st, CHILD(n, 4));
|
|
|
|
}
|
|
|
|
|
2004-05-19 05:20:33 -03:00
|
|
|
static void
|
|
|
|
symtable_gen_for(struct symtable *st, node *n, int is_outmost)
|
|
|
|
{
|
|
|
|
REQ(n, gen_for);
|
|
|
|
|
|
|
|
/* gen_for: for v in test [gen_iter] */
|
|
|
|
symtable_assign(st, CHILD(n, 1), 0);
|
|
|
|
if (is_outmost)
|
|
|
|
symtable_add_use(st, "[outmost-iterable]");
|
|
|
|
else
|
|
|
|
symtable_node(st, CHILD(n, 3));
|
|
|
|
|
|
|
|
if (NCH(n) == 5)
|
|
|
|
symtable_gen_iter(st, CHILD(n, 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
symtable_gen_iter(struct symtable *st, node *n)
|
|
|
|
{
|
|
|
|
REQ(n, gen_iter);
|
|
|
|
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
if (TYPE(n) == gen_for)
|
|
|
|
symtable_gen_for(st, n, 0);
|
|
|
|
else {
|
|
|
|
REQ(n, gen_if);
|
|
|
|
symtable_node(st, CHILD(n, 1));
|
|
|
|
|
|
|
|
if (NCH(n) == 3)
|
|
|
|
symtable_gen_iter(st, CHILD(n, 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-18 23:21:30 -04:00
|
|
|
static void
|
|
|
|
symtable_import(struct symtable *st, node *n)
|
|
|
|
{
|
2004-08-31 07:07:13 -03:00
|
|
|
node *nn;
|
2001-01-18 23:21:30 -04:00
|
|
|
int i;
|
2004-08-31 07:07:13 -03:00
|
|
|
/* import_stmt: import_name | import_from */
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
if (TYPE(n) == import_from) {
|
|
|
|
/* import_from: 'from' dotted_name 'import' ('*' |
|
|
|
|
| '(' import_as_names ')' | import_as_names) */
|
2001-02-27 21:58:08 -04:00
|
|
|
node *dotname = CHILD(n, 1);
|
2004-08-31 07:07:13 -03:00
|
|
|
REQ(dotname, dotted_name);
|
2001-02-27 21:58:08 -04:00
|
|
|
if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
|
|
|
|
/* check for bogus imports */
|
|
|
|
if (n->n_lineno >= st->st_future->ff_last_lineno) {
|
|
|
|
PyErr_SetString(PyExc_SyntaxError,
|
|
|
|
LATE_FUTURE);
|
2003-09-22 01:26:44 -03:00
|
|
|
symtable_error(st, n->n_lineno);
|
2001-02-27 21:58:08 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2004-08-31 07:07:13 -03:00
|
|
|
nn = CHILD(n, 3 + (TYPE(CHILD(n, 3)) == LPAR));
|
|
|
|
if (TYPE(nn) == STAR) {
|
2001-08-06 16:45:40 -03:00
|
|
|
if (st->st_cur->ste_type != TYPE_MODULE) {
|
2001-08-06 17:34:25 -03:00
|
|
|
if (symtable_warn(st,
|
|
|
|
"import * only allowed at module level") < 0)
|
|
|
|
return;
|
2001-08-06 16:45:40 -03:00
|
|
|
}
|
2001-02-27 00:23:34 -04:00
|
|
|
st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
|
2001-03-21 23:57:58 -04:00
|
|
|
st->st_cur->ste_opt_lineno = n->n_lineno;
|
2001-01-18 23:21:30 -04:00
|
|
|
} else {
|
2004-08-31 07:07:13 -03:00
|
|
|
REQ(nn, import_as_names);
|
|
|
|
for (i = 0; i < NCH(nn); i += 2) {
|
|
|
|
node *c = CHILD(nn, i);
|
2001-01-18 23:21:30 -04:00
|
|
|
if (NCH(c) > 1) /* import as */
|
2001-01-29 21:24:43 -04:00
|
|
|
symtable_assign(st, CHILD(c, 2),
|
|
|
|
DEF_IMPORT);
|
2001-01-18 23:21:30 -04:00
|
|
|
else
|
2001-01-29 21:24:43 -04:00
|
|
|
symtable_assign(st, CHILD(c, 0),
|
|
|
|
DEF_IMPORT);
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
}
|
2004-08-31 07:07:13 -03:00
|
|
|
} else {
|
|
|
|
/* 'import' dotted_as_names */
|
|
|
|
nn = CHILD(n, 1);
|
|
|
|
REQ(nn, dotted_as_names);
|
|
|
|
for (i = 0; i < NCH(nn); i += 2)
|
|
|
|
symtable_assign(st, CHILD(nn, i), DEF_IMPORT);
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-18 13:15:10 -03:00
|
|
|
/* The third argument to symatble_assign() is a flag to be passed to
|
|
|
|
symtable_add_def() if it is eventually called. The flag is useful
|
|
|
|
to specify the particular type of assignment that should be
|
|
|
|
recorded, e.g. an assignment caused by import.
|
|
|
|
*/
|
|
|
|
|
2001-01-18 23:21:30 -04:00
|
|
|
static void
|
2001-10-18 13:15:10 -03:00
|
|
|
symtable_assign(struct symtable *st, node *n, int def_flag)
|
2001-01-18 23:21:30 -04:00
|
|
|
{
|
|
|
|
node *tmp;
|
|
|
|
int i;
|
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
loop:
|
|
|
|
switch (TYPE(n)) {
|
|
|
|
case lambdef:
|
2001-01-29 21:24:43 -04:00
|
|
|
/* invalid assignment, e.g. lambda x:x=2. The next
|
|
|
|
pass will catch this error. */
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
return;
|
|
|
|
case power:
|
|
|
|
if (NCH(n) > 2) {
|
|
|
|
for (i = 2; i < NCH(n); ++i)
|
|
|
|
if (TYPE(CHILD(n, i)) != DOUBLESTAR)
|
|
|
|
symtable_node(st, CHILD(n, i));
|
|
|
|
}
|
|
|
|
if (NCH(n) > 1) {
|
|
|
|
symtable_node(st, CHILD(n, 0));
|
|
|
|
symtable_node(st, CHILD(n, 1));
|
|
|
|
} else {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case listmaker:
|
2001-03-19 16:38:06 -04:00
|
|
|
if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
|
|
|
|
/* XXX This is an error, but the next pass
|
|
|
|
will catch it. */
|
|
|
|
return;
|
|
|
|
} else {
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
2001-10-18 13:15:10 -03:00
|
|
|
symtable_assign(st, CHILD(n, i), def_flag);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
}
|
|
|
|
return;
|
2004-05-19 05:20:33 -03:00
|
|
|
case testlist_gexp:
|
|
|
|
if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) {
|
|
|
|
/* XXX This is an error, but the next pass
|
|
|
|
will catch it. */
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
|
|
symtable_assign(st, CHILD(n, i), def_flag);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
case exprlist:
|
|
|
|
case testlist:
|
2002-05-24 12:47:06 -03:00
|
|
|
case testlist1:
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < NCH(n); i += 2)
|
2001-10-18 13:15:10 -03:00
|
|
|
symtable_assign(st, CHILD(n, i), def_flag);
|
2001-01-18 23:21:30 -04:00
|
|
|
return;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
}
|
|
|
|
case atom:
|
|
|
|
tmp = CHILD(n, 0);
|
|
|
|
if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
|
|
|
|
n = CHILD(n, 1);
|
|
|
|
goto loop;
|
2001-03-23 10:08:38 -04:00
|
|
|
} else if (TYPE(tmp) == NAME) {
|
2001-11-09 15:50:08 -04:00
|
|
|
if (strcmp(STR(tmp), "__debug__") == 0) {
|
|
|
|
PyErr_SetString(PyExc_SyntaxError,
|
|
|
|
ASSIGN_DEBUG);
|
2003-09-22 01:26:44 -03:00
|
|
|
symtable_error(st, n->n_lineno);
|
|
|
|
return;
|
2001-11-09 15:50:08 -04:00
|
|
|
}
|
2001-10-18 13:15:10 -03:00
|
|
|
symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag);
|
2001-03-23 10:08:38 -04:00
|
|
|
}
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
return;
|
|
|
|
case dotted_as_name:
|
|
|
|
if (NCH(n) == 3)
|
|
|
|
symtable_add_def(st, STR(CHILD(n, 2)),
|
2001-10-18 13:15:10 -03:00
|
|
|
DEF_LOCAL | def_flag);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
else
|
|
|
|
symtable_add_def(st,
|
|
|
|
STR(CHILD(CHILD(n,
|
|
|
|
0), 0)),
|
2001-10-18 13:15:10 -03:00
|
|
|
DEF_LOCAL | def_flag);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
return;
|
|
|
|
case dotted_name:
|
2001-10-18 13:15:10 -03:00
|
|
|
symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
return;
|
|
|
|
case NAME:
|
2001-10-18 13:15:10 -03:00
|
|
|
symtable_add_def(st, STR(n), DEF_LOCAL | def_flag);
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
if (NCH(n) == 0)
|
2001-01-18 23:21:30 -04:00
|
|
|
return;
|
2001-02-19 11:33:10 -04:00
|
|
|
if (NCH(n) == 1) {
|
|
|
|
n = CHILD(n, 0);
|
|
|
|
goto loop;
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
2001-02-19 11:33:10 -04:00
|
|
|
/* Should only occur for errors like x + 1 = 1,
|
|
|
|
which will be caught in the next pass. */
|
|
|
|
for (i = 0; i < NCH(n); ++i)
|
|
|
|
if (TYPE(CHILD(n, i)) >= single_input)
|
2001-10-18 13:15:10 -03:00
|
|
|
symtable_assign(st, CHILD(n, i), def_flag);
|
2001-01-18 23:21:30 -04:00
|
|
|
}
|
|
|
|
}
|