? compiler/filesystem.attributes ? core/filesystem.attributes ? parser/filesystem.attributes Index: compiler/CodeCompiler.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/CodeCompiler.java,v retrieving revision 2.14 diff -u -5 -r2.14 CodeCompiler.java --- compiler/CodeCompiler.java 2001/05/15 18:53:34 2.14 +++ compiler/CodeCompiler.java 2001/05/20 15:33:53 @@ -183,20 +183,20 @@ throw new ParseException(msg,node); } public void parse(SimpleNode node, Code code, boolean fast_locals, String className, - boolean classBody, ScopeInfo scope) + boolean classBody, ScopeInfo scope,CompilerFlags cflags) throws Exception { this.fast_locals = fast_locals; this.className = className; this.code = code; if (scope == null) { futures = new Future(); - futures.preprocessFutures(node,null); + futures.preprocessFutures(node,cflags); new ScopesCompiler(this).parse(node); scope = node.scope; } my_scope = scope; @@ -779,19 +779,18 @@ code.invokestatic(mrefs.exec); return null; } public int assert1, assert2; - public Object assert_stmt(SimpleNode node) throws Exception { // ?? pending __debug__ should be treated as temp global + public Object assert_stmt(SimpleNode node) throws Exception { setline(node); Label end_of_assert = code.getLabel(); /* First do an if __debug__: */ - SimpleNode debugName = new SimpleNode(PythonGrammarTreeConstants.JJTNAME); - debugName.setInfo("__debug__"); - debugName.visit(this); - + loadFrame(); + emitGetGlobal("__debug__"); + if (mrefs.nonzero == 0) { mrefs.nonzero = code.pool.Methodref("org/python/core/PyObject", "__nonzero__", "()Z"); } code.invokevirtual(mrefs.nonzero); @@ -2250,10 +2249,20 @@ int getglobal, getlocal1, getlocal2; int setglobal, setlocal1, setlocal2; int delglobal, dellocal1, dellocal2; int getderef,setderef; + void emitGetGlobal(String name) throws Exception { + code.ldc(name); + if (mrefs.getglobal == 0) { + mrefs.getglobal = code.pool.Methodref( + "org/python/core/PyFrame", "getglobal", + "(Ljava/lang/String;)Lorg/python/core/PyObject;"); + } + code.invokevirtual(mrefs.getglobal); + } + public Object Name(SimpleNode node) throws Exception { String name; if (fast_locals) name = (String)node.getInfo(); else @@ -2272,17 +2281,11 @@ if (syminf != null) { int flags = syminf.flags; if (!my_scope.nested_scopes) flags &= ~ScopeInfo.FREE; if ((flags&ScopeInfo.GLOBAL) !=0 || optimizeGlobals&&(flags&(ScopeInfo.BOUND|ScopeInfo.CELL|ScopeInfo.FREE))==0) { - code.ldc(name); - if (mrefs.getglobal == 0) { - mrefs.getglobal = code.pool.Methodref( - "org/python/core/PyFrame", "getglobal", - "(Ljava/lang/String;)Lorg/python/core/PyObject;"); - } - code.invokevirtual(mrefs.getglobal); + emitGetGlobal(name); return null; } if (fast_locals) { if ((flags&ScopeInfo.CELL) != 0) { code.iconst(syminf.env_index); Index: compiler/CompilerFlags.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/CompilerFlags.java,v retrieving revision 2.1 diff -u -5 -r2.1 CompilerFlags.java --- compiler/CompilerFlags.java 2001/03/08 23:43:51 2.1 +++ compiler/CompilerFlags.java 2001/05/20 15:33:53 @@ -1,8 +1,14 @@ package org.python.compiler; 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: compiler/Module.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/Module.java,v retrieving revision 2.6 diff -u -5 -r2.6 Module.java --- compiler/Module.java 2001/05/15 18:53:34 2.6 +++ compiler/Module.java 2001/05/20 15:33:53 @@ -194,11 +194,11 @@ String fname; // for nested scopes public String[] cellvars; public String[] freevars; - public int xxx_npurecell; + public int jy_npurecell; public int moreflags; public PyCodeConstant() { ; } @@ -234,11 +234,11 @@ c.iconst(id); if (cellvars != null) CodeCompiler.makeStrings(c, cellvars, cellvars.length); else c.aconst_null(); if (freevars != null) CodeCompiler.makeStrings(c, freevars, freevars.length); else c.aconst_null(); - c.iconst(xxx_npurecell); + c.iconst(jy_npurecell); c.iconst(moreflags); int mref_newCode = c.pool.Methodref( "org/python/core/Py", @@ -357,15 +357,25 @@ return nameArray; } private int to_cell; + + public PyCodeConstant PyCode(SimpleNode tree, String name, + boolean fast_locals, String className, + boolean classBody, boolean printResults, + int firstlineno, ScopeInfo scope) + throws Exception + { + return PyCode(tree,name,fast_locals,className,classBody,printResults,firstlineno,scope,null); + } + public PyCodeConstant PyCode(SimpleNode tree, String name, boolean fast_locals, String className, boolean classBody, boolean printResults, - int firstlineno, ScopeInfo scope) + int firstlineno, ScopeInfo scope,CompilerFlags cflags) throws Exception { PyCodeConstant code = new PyCodeConstant(); ArgListCompiler ac = (scope != null)?scope.ac:null; @@ -399,41 +409,45 @@ ac.init_code.jjtAddChild(tree, ac.init_code.getNumChildren()); tree = ac.init_code; } if (scope != null) { - int nparamcell = scope.xxx_paramcells.size(); + int nparamcell = scope.jy_paramcells.size(); if (nparamcell > 0) { if (to_cell == 0) { to_cell = classfile.pool.Methodref("org/python/core/PyFrame","to_cell","(II)V"); } Hashtable tbl = scope.tbl; - Vector paramcells = scope.xxx_paramcells; + Vector paramcells = scope.jy_paramcells; for (int i = 0; i < nparamcell; i++) { c.aload(1); SymInfo syminf = (SymInfo)tbl.get(paramcells.elementAt(i)); c.iconst(syminf.locals_index); c.iconst(syminf.env_index); c.invokevirtual(to_cell); } } } - compiler.parse(tree, c, fast_locals, className, classBody, scope); + compiler.parse(tree, c, fast_locals, className, classBody, scope,cflags); // !classdef only if (!classBody) code.names = toNameAr(compiler.names,false); if (scope != null) { code.cellvars = toNameAr(scope.cellvars,true); code.freevars = toNameAr(scope.freevars,true); - code.xxx_npurecell = scope.xxx_npurecell; - if (compiler.optimizeGlobals) { - code.moreflags |= org.python.core.PyTableCode.CO_OPTIMIZED; - } + code.jy_npurecell = scope.jy_npurecell; } - + + if (compiler.optimizeGlobals) { + code.moreflags |= org.python.core.PyTableCode.CO_OPTIMIZED; + } + if (compiler.my_scope.nested_scopes) { + code.moreflags |= org.python.core.PyTableCode.CO_NESTED; + } + code.module = this; code.name = code.fname; return code; } @@ -551,18 +565,18 @@ } public static void compile(SimpleNode node, OutputStream ostream, String name, String filename, boolean linenumbers, boolean printResults, - boolean setFile) + boolean setFile,CompilerFlags cflags) throws Exception { Module module = new Module(name, filename, linenumbers); module.setFile = setFile; //Add __doc__ if it exists //Add __file__ for filename (if it exists?) - Constant main = module.PyCode(node, "?",false, null, false, printResults, 0,null); + Constant main = module.PyCode(node, "?",false, null, false, printResults, 0,null,cflags); module.mainCode = main; module.write(ostream); } } Index: compiler/ScopeInfo.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/ScopeInfo.java,v retrieving revision 2.3 diff -u -5 -r2.3 ScopeInfo.java --- compiler/ScopeInfo.java 2001/05/15 18:53:34 2.3 +++ compiler/ScopeInfo.java 2001/05/20 15:33:53 @@ -101,13 +101,13 @@ public Hashtable inner_free = new Hashtable(); public Vector cellvars = new Vector(); - public Vector xxx_paramcells = new Vector(); + public Vector jy_paramcells = new Vector(); - public int xxx_npurecell; // ?? xxx + public int jy_npurecell; public int cell; public void cook(ScopeInfo up,CompilationContext ctxt) throws Exception { if (up == null) return; // top level => nop @@ -128,11 +128,11 @@ int flags = info.flags; if (func) { if ((flags&NGLOBAL) == 0 && (flags&BOUND) != 0) { // not func global and bound ? if (nested_scopes) { info.flags |= CELL; - if ((info.flags&PARAM) != 0) xxx_paramcells.addElement(name); + if ((info.flags&PARAM) != 0) jy_paramcells.addElement(name); cellvars.addElement(name); info.env_index = cell++; if ((flags&PARAM) == 0) purecells.addElement(name); continue; } @@ -160,11 +160,11 @@ info.flags |= FREE; some_free = true; if (nested) up.inner_free.put(name,PRESENT); } } - if ((xxx_npurecell = purecells.size()) > 0) { + if ((jy_npurecell = purecells.size()) > 0) { int sz = purecells.size(); for (int i = 0; i < sz; i++) { names.addElement(purecells.elementAt(i)); } } Index: compiler/ScopesCompiler.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/ScopesCompiler.java,v retrieving revision 2.3 diff -u -5 -r2.3 ScopesCompiler.java --- compiler/ScopesCompiler.java 2001/05/15 18:53:34 2.3 +++ compiler/ScopesCompiler.java 2001/05/20 15:33:53 @@ -6,13 +6,13 @@ import java.util.*; public class ScopesCompiler extends Visitor implements ScopeConstants { private CompilationContext code_compiler; - + private boolean nested_scopes = false; - + private Stack scopes; private ScopeInfo cur = null; private int mode; private static final int GET=0; @@ -21,18 +21,18 @@ private static final int AUGSET=4; private int level = 0; private int func_level = 0; - public ScopesCompiler(CompilationContext code_compiler) { // ?? polish + public ScopesCompiler(CompilationContext code_compiler) { this.code_compiler = code_compiler; scopes = new Stack(); mode = GET; - String nested_scopes_opt = org.python.core.PySystemState.registry.getProperty("python.xfutures.nested_scopes"); - if (nested_scopes_opt != null && nested_scopes_opt.equals("on")) nested_scopes = true; else + /*String nested_scopes_opt = org.python.core.PySystemState.registry.getProperty("python.xfutures.nested_scopes"); + if (nested_scopes_opt != null && nested_scopes_opt.equals("on")) nested_scopes = true; else*/ nested_scopes = code_compiler.getFutures().areNestedScopesOn(); - // System.err.println("nested-scopes: "+nested_scopes); + // System.err.println("nested-scopes: "+nested_scopes); } public Object set(SimpleNode node) throws Exception { return modal(node,SET); } @@ -269,11 +269,11 @@ stmt(node); return null; } public Object for_stmt(SimpleNode node) throws Exception { - if (mode != GET) illassign(node); + if (mode != GET) illassign(node); set(node.getChild(0)); node.getChild(1).visit(this); node.getChild(2).visit(this); if (node.getNumChildren()>3) node.getChild(3).visit(this); return null; @@ -578,11 +578,14 @@ return null; } public Object Name(SimpleNode node) throws Exception { String name = (String)node.getInfo(); - if ( mode != GET) cur.addBound(name); + if ( mode != GET) { + if (name.equals("__debug__")) code_compiler.error("can not assign to __debug__",false,node); + cur.addBound(name); + } else cur.addUsed(name); return null; } public Object String(SimpleNode node) throws Exception { Index: core/Py.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/Py.java,v retrieving revision 2.43 diff -u -5 -r2.43 Py.java --- core/Py.java 2001/05/15 18:53:35 2.43 +++ core/Py.java 2001/05/20 15:33:53 @@ -430,34 +430,34 @@ public static PyInteger newBoolean(boolean t) { return t ? Py.One : Py.Zero; } - // nested scopes: String[] cellvars,String[] freevars,int xxx_npurecell & int moreflags + // nested scopes: String[] cellvars,String[] freevars,int npurecell & int moreflags public static PyCode newCode(int argcount, String varnames[], String filename, String name, boolean args, boolean keywords, PyFunctionTable funcs, int func_id, - String[] cellvars,String[] freevars,int xxx_npurecell,int moreflags) + String[] cellvars,String[] freevars,int npurecell,int moreflags) { return new PyTableCode(argcount, varnames, filename, name, 0, args, keywords, funcs, - func_id, cellvars, freevars, xxx_npurecell,moreflags); + func_id, cellvars, freevars, npurecell,moreflags); } public static PyCode newCode(int argcount, String varnames[], String filename, String name, int firstlineno, boolean args, boolean keywords, PyFunctionTable funcs, int func_id, - String[] cellvars,String[] freevars,int xxx_npurecell,int moreflags) + String[] cellvars,String[] freevars,int npurecell,int moreflags) { return new PyTableCode(argcount, varnames, filename, name, firstlineno, args, keywords, - funcs, func_id, cellvars, freevars, xxx_npurecell,moreflags); + funcs, func_id, cellvars, freevars, npurecell,moreflags); } // -- public static PyCode newCode(int argcount, String varnames[], @@ -1107,11 +1107,11 @@ return; contents = fp.read().toString(); } else throw Py.TypeError( "exec: argument 1 must be string, code or file object"); - code = __builtin__.compile(contents, "", "exec"); + code = Py.compile_flags(contents, "", "exec",Py.getCompilerFlags()); } Py.runCode(code, locals, globals); } private static ThreadStateMapping threadStateMapping = null; @@ -1439,10 +1439,22 @@ String name = "org.python.pycode._pyx"+nameindex; nameindex += 1; return name; } + + public static org.python.compiler.CompilerFlags getCompilerFlags() { + org.python.compiler.CompilerFlags cflags = null; + PyFrame frame = Py.getFrame(); + if (frame!=null && frame.f_code != null) { + cflags = new org.python.compiler.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); } public static PyCode compile(SimpleNode node, String name, @@ -1454,34 +1466,55 @@ public static PyCode compile(SimpleNode node, String name, String filename, boolean linenumbers, 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,org.python.compiler.CompilerFlags cflags) + { try { ByteArrayOutputStream ostream = new ByteArrayOutputStream(); org.python.compiler.Module.compile(node, ostream, name, filename, linenumbers, printResults, - false); + false,cflags); saveClassFile(name, ostream); return BytecodeLoader.makeCode(name, ostream.toByteArray()); } catch (Throwable t) { throw parser.fixParseError(null, t, filename); } } - public static PyCode compile(InputStream istream, String filename, - String type) + public static PyCode compile_flags(InputStream istream, String filename, + String type,org.python.compiler.CompilerFlags cflags) { SimpleNode node = parser.parse(istream, type, filename); boolean printResults = false; if (type.equals("single")) printResults = true; - return Py.compile(node, getName(), filename, true, printResults); + return Py.compile_flags(node, getName(), filename, true, printResults,cflags); } - + + public static PyCode compile_flags(String data, String filename, String type,org.python.compiler.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) { PyTuple tup = (PyTuple)o; //System.err.println("unpack tuple"); if (tup.__len__() == length) Index: core/PyFrame.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyFrame.java,v retrieving revision 2.8 diff -u -5 -r2.8 PyFrame.java --- core/PyFrame.java 2001/05/15 18:53:35 2.8 +++ core/PyFrame.java 2001/05/20 15:33:53 @@ -38,11 +38,11 @@ f_globals = globals; f_builtins = builtins; // This needs work to be efficient with multiple interpreter states if (locals == null && code != null) { if ((code.co_flags&PyTableCode.CO_OPTIMIZED)!=0) { - if (code.co_nlocals>0) f_fastlocals = new PyObject[code.co_nlocals-code.xxx_npurecell]; // internal: may change + if (code.co_nlocals>0) f_fastlocals = new PyObject[code.co_nlocals-code.jy_npurecell]; // internal: may change } else f_locals = new PyStringMap(); } if (code != null) { // reserve space for env int env_sz = 0; if (code.co_freevars != null) env_sz += (f_nfreevars = code.co_freevars.length); @@ -118,13 +118,13 @@ if (f_fastlocals != null) { for (i=0; i", "eval"); + code = Py.compile_flags(((PyString)o).toString(), + "", "eval",null); // eval does not inherit co_nested else throw Py.TypeError( "eval: argument 1 must be string or code object"); } return Py.runCode(code, locals, globals); @@ -276,20 +277,27 @@ } public static void execfile(String name, PyObject globals, PyObject locals) { + execfile_flags(name,globals,locals,Py.getCompilerFlags()); + } + + + public static void execfile_flags(String name, PyObject globals, + PyObject locals,org.python.compiler.CompilerFlags cflags) + { java.io.FileInputStream file; try { file = new java.io.FileInputStream(name); } catch (java.io.FileNotFoundException e) { throw Py.IOError(e); } PyCode code; try { - code = Py.compile(file, name, "exec"); + code = Py.compile_flags(file, name, "exec",cflags); } finally { try { file.close(); } catch (java.io.IOException e) { throw Py.IOError(e); @@ -299,10 +307,11 @@ } public static void execfile(String name, PyObject globals) { execfile(name, globals, globals); } + public static void execfile(String name) { execfile(name, null, null); } public static PyObject filter(PyObject f, PyString s) { Index: core/imp.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/imp.java,v retrieving revision 2.41 diff -u -5 -r2.41 imp.java --- core/imp.java 2001/03/14 13:47:53 2.41 +++ core/imp.java 2001/05/20 15:33:54 @@ -123,11 +123,11 @@ node = parser.parse(fp, "exec", filename); } finally { fp.close(); } org.python.compiler.Module.compile(node, ofp, name+"$py", - filename, true, false, true); + filename, true, false, true,null); if (outFilename != null) { File classFile = new File(outFilename); try { FileOutputStream fop = new FileOutputStream(classFile); Index: modules/codeop.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/modules/codeop.java,v retrieving revision 2.3 diff -u -5 -r2.3 codeop.java --- modules/codeop.java 2001/03/13 20:27:18 2.3 +++ modules/codeop.java 2001/05/20 15:33:54 @@ -1,15 +1,21 @@ // Copyright © Corporation for National Research Initiatives package org.python.modules; import org.python.core.*; -public class codeop { +public class codeop implements ClassDictInit { public static PyString __doc__ = new PyString( "Utility to compile possibly incomplete Python source code.\n" ); + public static void classDictInit(PyObject dict) + { + dict.__delitem__("compile_command_flags"); + dict.__delitem__("classInitDict"); + } + public static PyList __all__ = new PyList(new PyString[] { new PyString("compile_command") }); public static PyObject compile_command(String string) { @@ -21,13 +27,19 @@ } public static PyObject compile_command(String string, String filename, String kind) { + return compile_command_flags(string,filename,kind,null); + } + + public static PyObject compile_command_flags(String string,String filename,String kind,org.python.compiler.CompilerFlags cflags) + { org.python.parser.SimpleNode node = parser.partialParse(string+"\n", kind, filename); if (node == null) return Py.None; - return Py.compile(node, Py.getName(), filename, true, true); + return Py.compile_flags(node, Py.getName(), filename, true, true,cflags); } + } Index: util/InteractiveInterpreter.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/util/InteractiveInterpreter.java,v retrieving revision 2.3 diff -u -5 -r2.3 InteractiveInterpreter.java --- util/InteractiveInterpreter.java 2000/12/04 21:18:59 2.3 +++ util/InteractiveInterpreter.java 2001/05/20 15:33:54 @@ -5,13 +5,15 @@ // Based on CPython-1.5.2's code module public class InteractiveInterpreter extends PythonInterpreter { public InteractiveInterpreter() { super(); + cflags = new org.python.compiler.CompilerFlags(); } public InteractiveInterpreter(PyObject locals) { super(locals); + cflags = new org.python.compiler.CompilerFlags(); } /** * Compile and run some source in the interpreter. * @@ -43,12 +45,12 @@ } public boolean runsource(String source, String filename, String symbol) { PyObject code; try { - code = org.python.modules.codeop.compile_command( - source, filename, symbol); + code = org.python.modules.codeop.compile_command_flags( + source, filename, symbol,cflags); } catch (PyException exc) { if (Py.matchException(exc, Py.SyntaxError)) { // Case 1 showexception(exc); return false; Index: util/PythonInterpreter.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/util/PythonInterpreter.java,v retrieving revision 2.6 diff -u -5 -r2.6 PythonInterpreter.java --- util/PythonInterpreter.java 2001/01/15 18:27:38 2.6 +++ util/PythonInterpreter.java 2001/05/20 15:33:54 @@ -14,10 +14,12 @@ public class PythonInterpreter { PyModule module; protected PySystemState systemState; PyObject locals; + protected org.python.compiler.CompilerFlags cflags = null; + /** * Initialize the jython runtime. This method should only be * called once, and should be call before any other python objects * are created (included a PythonInterpreter). * @@ -130,11 +132,11 @@ * * @param s the string to execute */ public void exec(String s) { setState(); - Py.exec(new PyString(s), locals, locals); + Py.exec(Py.compile_flags(s, "", "exec",cflags), locals, locals); } /** * Execute a Python code object in the local namespace * @@ -150,20 +152,20 @@ * * @param s the name of the file to execute */ public void execfile(String s) { setState(); - __builtin__.execfile(s, locals); + __builtin__.execfile_flags(s, locals, locals, cflags); } public void execfile(java.io.InputStream s) { execfile(s, ""); } public void execfile(java.io.InputStream s, String name) { setState(); - Py.runCode(Py.compile(s, name, "exec"), locals, locals); + Py.runCode(Py.compile_flags(s, name, "exec",cflags), locals, locals); } // Getting and setting the locals dictionary public PyObject getLocals() { return locals;