From: Samuele P. <ped...@us...> - 2001-05-27 18:49:18
|
Update of /cvsroot/jython/jython/org/python/core In directory usw-pr-cvs1:/tmp/cvs-serv24200/core Modified Files: Py.java PyFrame.java PyTableCode.java __builtin__.java imp.java Added Files: CompilerFlags.java Log Message: ** nested scopes (2) ** completes nested scopes support in interpreter: now nested scopes semantic settings are inherited trough exec, execfile, etc ... plus: assert now correctly get __debug__ as a global --- NEW FILE --- package org.python.core; public class CompilerFlags extends Object { public CompilerFlags() {} public CompilerFlags(int co_flags) { if ((co_flags&org.python.core.PyTableCode.CO_NESTED) != 0) nested_scopes = true; } public boolean nested_scopes; } Index: Py.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/Py.java,v retrieving revision 2.43 retrieving revision 2.44 diff -C2 -r2.43 -r2.44 *** Py.java 2001/05/15 18:53:35 2.43 --- Py.java 2001/05/27 18:49:15 2.44 *************** *** 433,437 **** } ! // nested scopes: String[] cellvars,String[] freevars,int xxx_npurecell & int moreflags public static PyCode newCode(int argcount, String varnames[], --- 433,437 ---- } ! // nested scopes: String[] cellvars,String[] freevars,int npurecell & int moreflags public static PyCode newCode(int argcount, String varnames[], *************** *** 439,447 **** boolean args, boolean keywords, PyFunctionTable funcs, int func_id, ! String[] cellvars,String[] freevars,int xxx_npurecell,int moreflags) { return new PyTableCode(argcount, varnames, filename, name, 0, args, keywords, funcs, ! func_id, cellvars, freevars, xxx_npurecell,moreflags); } --- 439,447 ---- boolean args, boolean keywords, PyFunctionTable funcs, int func_id, ! String[] cellvars,String[] freevars,int npurecell,int moreflags) { return new PyTableCode(argcount, varnames, filename, name, 0, args, keywords, funcs, ! func_id, cellvars, freevars, npurecell,moreflags); } *************** *** 451,460 **** boolean args, boolean keywords, PyFunctionTable funcs, int func_id, ! String[] cellvars,String[] freevars,int xxx_npurecell,int moreflags) { return new PyTableCode(argcount, varnames, filename, name, firstlineno, args, keywords, ! funcs, func_id, cellvars, freevars, xxx_npurecell,moreflags); } --- 451,460 ---- boolean args, boolean keywords, PyFunctionTable funcs, int func_id, ! String[] cellvars,String[] freevars,int npurecell,int moreflags) { return new PyTableCode(argcount, varnames, filename, name, firstlineno, args, keywords, ! funcs, func_id, cellvars, freevars, npurecell,moreflags); } *************** *** 1110,1114 **** throw Py.TypeError( "exec: argument 1 must be string, code or file object"); ! code = __builtin__.compile(contents, "<string>", "exec"); } Py.runCode(code, locals, globals); --- 1110,1114 ---- throw Py.TypeError( "exec: argument 1 must be string, code or file object"); ! code = Py.compile_flags(contents, "<string>", "exec",Py.getCompilerFlags()); } Py.runCode(code, locals, globals); *************** *** 1442,1445 **** --- 1442,1457 ---- } + + public static CompilerFlags getCompilerFlags() { + CompilerFlags cflags = null; + PyFrame frame = Py.getFrame(); + if (frame!=null && frame.f_code != null) { + cflags = new CompilerFlags(frame.f_code.co_flags); + } + return cflags; + } + + // w/o compiler-flags + public static PyCode compile(SimpleNode node, String filename) { return compile(node, getName(), filename); *************** *** 1457,1465 **** boolean printResults) { try { ByteArrayOutputStream ostream = new ByteArrayOutputStream(); org.python.compiler.Module.compile(node, ostream, name, filename, linenumbers, printResults, ! false); saveClassFile(name, ostream); --- 1469,1493 ---- boolean printResults) { + return compile_flags(node,name,filename,linenumbers,printResults,null); + } + + public static PyCode compile(InputStream istream, String filename, + String type) + { + return compile_flags(istream,filename,type,null); + } + + // with compiler-flags + + public static PyCode compile_flags(SimpleNode node, String name, + String filename, + boolean linenumbers, + boolean printResults,CompilerFlags cflags) + { try { ByteArrayOutputStream ostream = new ByteArrayOutputStream(); org.python.compiler.Module.compile(node, ostream, name, filename, linenumbers, printResults, ! false,cflags); saveClassFile(name, ostream); *************** *** 1471,1476 **** } ! public static PyCode compile(InputStream istream, String filename, ! String type) { SimpleNode node = parser.parse(istream, type, filename); --- 1499,1504 ---- } ! public static PyCode compile_flags(InputStream istream, String filename, ! String type,CompilerFlags cflags) { SimpleNode node = parser.parse(istream, type, filename); *************** *** 1478,1484 **** if (type.equals("single")) printResults = true; ! return Py.compile(node, getName(), filename, true, printResults); } ! public static PyObject[] unpackSequence(PyObject o, int length) { if (o instanceof PyTuple) { --- 1506,1517 ---- if (type.equals("single")) printResults = true; ! return Py.compile_flags(node, getName(), filename, true, printResults,cflags); } ! ! public static PyCode compile_flags(String data, String filename, String type,CompilerFlags cflags) { ! return Py.compile_flags(new java.io.StringBufferInputStream(data+"\n\n"), ! filename, type,cflags); ! } ! public static PyObject[] unpackSequence(PyObject o, int length) { if (o instanceof PyTuple) { Index: PyFrame.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyFrame.java,v retrieving revision 2.9 retrieving revision 2.10 diff -C2 -r2.9 -r2.10 *** PyFrame.java 2001/05/22 12:38:03 2.9 --- PyFrame.java 2001/05/27 18:49:15 2.10 *************** *** 41,45 **** if (locals == null && code != null) { if ((code.co_flags&PyTableCode.CO_OPTIMIZED)!=0 || code.nargs > 0) { // ! f_fastlocals needed for arg passing too ! if (code.co_nlocals>0) f_fastlocals = new PyObject[code.co_nlocals-code.xxx_npurecell]; // internal: may change } else f_locals = new PyStringMap(); } --- 41,45 ---- if (locals == null && code != null) { if ((code.co_flags&PyTableCode.CO_OPTIMIZED)!=0 || code.nargs > 0) { // ! f_fastlocals needed for arg passing too ! if (code.co_nlocals>0) f_fastlocals = new PyObject[code.co_nlocals-code.jy_npurecell]; // internal: may change } else f_locals = new PyStringMap(); } Index: PyTableCode.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyTableCode.java,v retrieving revision 2.13 retrieving revision 2.14 diff -C2 -r2.13 -r2.14 *** PyTableCode.java 2001/05/15 18:53:35 2.13 --- PyTableCode.java 2001/05/27 18:49:15 2.14 *************** *** 14,18 **** public String co_varnames[]; public String co_cellvars[]; ! public int xxx_npurecell; // internal: may change public String co_freevars[]; public String co_filename; --- 14,18 ---- public String co_varnames[]; public String co_cellvars[]; ! public int jy_npurecell; // internal: jython specific public String co_freevars[]; public String co_filename; *************** *** 44,48 **** boolean args, boolean keywords, PyFunctionTable funcs, int func_id, ! String[] cellvars,String[] freevars,int xxx_npurecell,int moreflags) // may change { co_argcount = nargs = argcount; --- 44,48 ---- boolean args, boolean keywords, PyFunctionTable funcs, int func_id, ! String[] cellvars,String[] freevars,int npurecell,int moreflags) // may change { co_argcount = nargs = argcount; *************** *** 53,57 **** co_cellvars = cellvars; co_freevars = freevars; ! this.xxx_npurecell = xxx_npurecell; this.args = args; co_name = name; --- 53,57 ---- co_cellvars = cellvars; co_freevars = freevars; ! this.jy_npurecell = npurecell; this.args = args; co_name = name; Index: __builtin__.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/__builtin__.java,v retrieving revision 2.32 retrieving revision 2.33 diff -C2 -r2.32 -r2.33 *** __builtin__.java 2001/04/25 19:04:32 2.32 --- __builtin__.java 2001/05/27 18:49:15 2.33 *************** *** 131,134 **** --- 131,136 ---- dict.__setitem__("id", new BuiltinFunctions("id", 11, 1)); dict.__setitem__("__import__", new ImportFunction()); + + dict.__delitem__("execfile_flags"); // -execfile_flags } *************** *** 209,215 **** } ! public static PyCode compile(String data, String filename, String type) { ! return Py.compile(new java.io.StringBufferInputStream(data+"\n\n"), ! filename, type); } --- 211,216 ---- } ! public static PyCode compile(String data, String filename, String type) { ! return Py.compile_flags(data,filename,type,Py.getCompilerFlags()); } *************** *** 259,264 **** else { if (o instanceof PyString) ! code = __builtin__.compile(((PyString)o).toString(), ! "<string>", "eval"); else throw Py.TypeError( --- 260,265 ---- else { if (o instanceof PyString) ! code = Py.compile_flags(((PyString)o).toString(), ! "<string>", "eval",null); // eval does not inherit co_nested else throw Py.TypeError( *************** *** 279,282 **** --- 280,290 ---- PyObject locals) { + execfile_flags(name,globals,locals,Py.getCompilerFlags()); + } + + + public static void execfile_flags(String name, PyObject globals, + PyObject locals,CompilerFlags cflags) + { java.io.FileInputStream file; try { *************** *** 288,292 **** try { ! code = Py.compile(file, name, "exec"); } finally { try { --- 296,300 ---- try { ! code = Py.compile_flags(file, name, "exec",cflags); } finally { try { *************** *** 302,305 **** --- 310,314 ---- execfile(name, globals, globals); } + public static void execfile(String name) { execfile(name, null, null); Index: imp.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/imp.java,v retrieving revision 2.41 retrieving revision 2.42 diff -C2 -r2.41 -r2.42 *** imp.java 2001/03/14 13:47:53 2.41 --- imp.java 2001/05/27 18:49:15 2.42 *************** *** 126,130 **** } org.python.compiler.Module.compile(node, ofp, name+"$py", ! filename, true, false, true); if (outFilename != null) { --- 126,130 ---- } org.python.compiler.Module.compile(node, ofp, name+"$py", ! filename, true, false, true,null); if (outFilename != null) { |