From: <th...@us...> - 2009-03-29 22:11:45
|
Revision: 6115 http://jython.svn.sourceforge.net/jython/?rev=6115&view=rev Author: thobes Date: 2009-03-29 22:11:34 +0000 (Sun, 29 Mar 2009) Log Message: ----------- Patched the compiler paths for type stafety. The kind is now an enum (from builtin.compile and down) The compile methods now return PyCode (from builtin.compile and down) This simplifies the compiler paths, the check for if AST or code should be returned is now in only one place and the parser dispatch is simpler(less code) Modified Paths: -------------- trunk/jython/Lib/codeop.py trunk/jython/src/org/python/Version.java trunk/jython/src/org/python/antlr/BaseParser.java trunk/jython/src/org/python/antlr/ast/AstModule.java trunk/jython/src/org/python/compiler/Future.java trunk/jython/src/org/python/compiler/Module.java trunk/jython/src/org/python/compiler/pbc/Bytecode.java trunk/jython/src/org/python/core/CompilerFlags.java trunk/jython/src/org/python/core/ParserFacade.java trunk/jython/src/org/python/core/Py.java trunk/jython/src/org/python/core/PyBaseCode.java trunk/jython/src/org/python/core/PyBytecode.java trunk/jython/src/org/python/core/PyFrame.java trunk/jython/src/org/python/core/PyTableCode.java trunk/jython/src/org/python/core/__builtin__.java trunk/jython/src/org/python/core/imp.java trunk/jython/src/org/python/util/InteractiveInterpreter.java trunk/jython/src/org/python/util/PythonInterpreter.java trunk/jython/src/org/python/util/jython.java trunk/jython/tests/java/org/python/antlr/PythonTreeTester.java Added Paths: ----------- trunk/jython/src/org/python/core/CodeFlag.java trunk/jython/src/org/python/core/CompileMode.java trunk/jython/src/org/python/core/FutureFeature.java trunk/jython/src/org/python/core/Pragma.java trunk/jython/src/org/python/core/PragmaReceiver.java Removed Paths: ------------- trunk/jython/src/org/python/antlr/ExpressionParser.java trunk/jython/src/org/python/antlr/InteractiveParser.java trunk/jython/src/org/python/antlr/ModuleParser.java Modified: trunk/jython/Lib/codeop.py =================================================================== --- trunk/jython/Lib/codeop.py 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/Lib/codeop.py 2009-03-29 22:11:34 UTC (rev 6115) @@ -57,8 +57,8 @@ """ # import internals, not guaranteed interface -from org.python.core import Py,CompilerFlags -from org.python.core.PyTableCode import PyCF_DONT_IMPLY_DEDENT +from org.python.core import Py,CompilerFlags,CompileMode +from org.python.core.CompilerFlags import PyCF_DONT_IMPLY_DEDENT # public interface @@ -84,6 +84,7 @@ """ if symbol not in ['single','eval']: raise ValueError,"symbol arg must be either single or eval" + symbol = CompileMode.getMode(symbol) return Py.compile_command_flags(source,filename,symbol,Py.getCompilerFlags(),0) class Compile: @@ -95,6 +96,7 @@ self._cflags = CompilerFlags() def __call__(self, source, filename, symbol): + symbol = CompileMode.getMode(symbol) return Py.compile_flags(source, filename, symbol, self._cflags) class CommandCompiler: @@ -128,5 +130,6 @@ """ if symbol not in ['single','eval']: raise ValueError,"symbol arg must be either single or eval" + symbol = CompileMode.getMode(symbol) return Py.compile_command_flags(source,filename,symbol,self._cflags,0) Modified: trunk/jython/src/org/python/Version.java =================================================================== --- trunk/jython/src/org/python/Version.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/Version.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -3,8 +3,14 @@ import java.io.InputStream; import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.EnumSet; import java.util.Properties; +import java.util.Set; +import org.python.core.CodeFlag; + /** * Jython version information. * @@ -36,6 +42,10 @@ /** Short version of branch, e.g. asm. */ public static String SHORT_BRANCH; + /** The flags that are set by default in a code object. */ + private static final Collection<CodeFlag> defaultCodeFlags = Arrays.asList( + CodeFlag.CO_NESTED, CodeFlag.CO_GENERATOR_ALLOWED); + private static final String headURL = "$HeadURL$"; @@ -144,4 +154,8 @@ public static String getVersion() { return String.format("%.80s (%.80s) %.80s", PY_VERSION, getBuildInfo(), getVM()); } + + public static Set<CodeFlag> getDefaultCodeFlags() { + return EnumSet.copyOf(defaultCodeFlags); + } } Modified: trunk/jython/src/org/python/antlr/BaseParser.java =================================================================== --- trunk/jython/src/org/python/antlr/BaseParser.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/antlr/BaseParser.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -1,15 +1,31 @@ package org.python.antlr; import org.antlr.runtime.CharStream; +import org.antlr.runtime.CommonTokenStream; +import org.antlr.runtime.RecognitionException; import org.antlr.runtime.Token; +import org.python.antlr.base.mod; public class BaseParser { - protected CharStream charStream; - protected boolean partial; - protected String filename; - protected String encoding; + protected final CharStream charStream; + @Deprecated + protected final boolean partial; + protected final String filename; + protected final String encoding; protected ErrorHandler errorHandler = new FailFastHandler(); + + public BaseParser(CharStream stream, String filename, String encoding) { + this(stream, filename, encoding, false); + } + + @Deprecated + public BaseParser(CharStream stream, String filename, String encoding, boolean partial) { + this.charStream = stream; + this.filename = filename; + this.encoding = encoding; + this.partial = partial; + } public void setAntlrErrorHandler(ErrorHandler eh) { this.errorHandler = eh; @@ -25,4 +41,60 @@ return super.nextToken(); } } + + private CharStream charStream(boolean single) { + return charStream; + } + + private PythonParser setupParser(boolean single) { + PythonLexer lexer = new PyLexer(this.charStream(single)); + lexer.setErrorHandler(errorHandler); + CommonTokenStream tokens = new CommonTokenStream(lexer); + PythonTokenSource indentedSource = new PythonTokenSource(tokens, filename, single); + tokens = new CommonTokenStream(indentedSource); + PythonParser parser = new PythonParser(tokens, encoding); + parser.setErrorHandler(errorHandler); + parser.setTreeAdaptor(new PythonTreeAdaptor()); + return parser; + } + + public mod parseExpression() { + mod tree = null; + PythonParser parser = setupParser(false); + try { + PythonParser.eval_input_return r = parser.eval_input(); + tree = (mod)r.tree; + } catch (RecognitionException e) { + //XXX: this can't happen. Need to strip the throws from antlr + // generated code. + } + return tree; + } + + public mod parseInteractive() { + mod tree = null; + PythonParser parser = setupParser(true); + try { + PythonParser.single_input_return r = parser.single_input(); + tree = (mod)r.tree; + } catch (RecognitionException e) { + //I am only throwing ParseExceptions, but "throws RecognitionException" still gets + //into the generated code. + System.err.println("FIXME: pretty sure this can't happen -- but needs to be checked"); + } + return tree; + } + + public mod parseModule() { + mod tree = null; + PythonParser parser = setupParser(false); + try { + PythonParser.file_input_return r = parser.file_input(); + tree = (mod)r.tree; + } catch (RecognitionException e) { + //XXX: this can't happen. Need to strip the throws from antlr + // generated code. + } + return tree; + } } Deleted: trunk/jython/src/org/python/antlr/ExpressionParser.java =================================================================== --- trunk/jython/src/org/python/antlr/ExpressionParser.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/antlr/ExpressionParser.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -1,36 +0,0 @@ -package org.python.antlr; - -import org.antlr.runtime.CharStream; -import org.antlr.runtime.CommonTokenStream; -import org.antlr.runtime.RecognitionException; -import org.python.antlr.base.mod; - -public class ExpressionParser extends BaseParser { - - public ExpressionParser(CharStream cs, String filename, String encoding) { - this.charStream = cs; - this.filename = filename; - this.encoding = encoding; - } - - public mod parse() { - mod tree = null; - PythonLexer lexer = new PyLexer(this.charStream); - lexer.setErrorHandler(errorHandler); - CommonTokenStream tokens = new CommonTokenStream(lexer); - PythonTokenSource indentedSource = new PythonTokenSource(tokens, filename); - tokens = new CommonTokenStream(indentedSource); - PythonParser parser = new PythonParser(tokens, encoding); - parser.setErrorHandler(errorHandler); - parser.setTreeAdaptor(new PythonTreeAdaptor()); - - try { - PythonParser.eval_input_return r = parser.eval_input(); - tree = (mod)r.tree; - } catch (RecognitionException e) { - //XXX: this can't happen. Need to strip the throws from antlr - // generated code. - } - return tree; - } -} Deleted: trunk/jython/src/org/python/antlr/InteractiveParser.java =================================================================== --- trunk/jython/src/org/python/antlr/InteractiveParser.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/antlr/InteractiveParser.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -1,41 +0,0 @@ -package org.python.antlr; - -import java.io.BufferedReader; -import java.io.IOException; - -import org.antlr.runtime.CommonTokenStream; -import org.antlr.runtime.RecognitionException; -import org.python.antlr.base.mod; - -public class InteractiveParser extends BaseParser { - - private BufferedReader bufreader; - - public InteractiveParser(BufferedReader br, String filename, String encoding) { - this.bufreader = br; - this.filename = filename; - this.encoding = encoding; - } - - public mod parse() throws IOException { - mod tree = null; - PythonLexer lexer = new PyLexer(new NoCloseReaderStream(bufreader)); - lexer.setErrorHandler(errorHandler); - CommonTokenStream tokens = new CommonTokenStream(lexer); - PythonTokenSource indentedSource = new PythonTokenSource(tokens, filename, true); - tokens = new CommonTokenStream(indentedSource); - PythonParser parser = new PythonParser(tokens, encoding); - parser.setErrorHandler(errorHandler); - parser.setTreeAdaptor(new PythonTreeAdaptor()); - - try { - PythonParser.single_input_return r = parser.single_input(); - tree = (mod)r.tree; - } catch (RecognitionException e) { - //I am only throwing ParseExceptions, but "throws RecognitionException" still gets - //into the generated code. - System.err.println("FIXME: pretty sure this can't happen -- but needs to be checked"); - } - return tree; - } -} Deleted: trunk/jython/src/org/python/antlr/ModuleParser.java =================================================================== --- trunk/jython/src/org/python/antlr/ModuleParser.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/antlr/ModuleParser.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -1,39 +0,0 @@ -package org.python.antlr; - -import org.antlr.runtime.CharStream; -import org.antlr.runtime.CommonTokenStream; -import org.antlr.runtime.RecognitionException; -import org.python.antlr.base.mod; - -public class ModuleParser extends BaseParser { - public ModuleParser(CharStream cs, String filename, String encoding) { - this(cs, filename, encoding, false); - } - - public ModuleParser(CharStream cs, String filename, String encoding, boolean partial) { - this.charStream = cs; - this.filename = filename; - this.encoding = encoding; - this.partial = partial; - } - - public mod file_input() { - mod tree = null; - PythonLexer lexer = new PyLexer(this.charStream); - lexer.setErrorHandler(errorHandler); - CommonTokenStream tokens = new CommonTokenStream(lexer); - PythonTokenSource indentedSource = new PythonTokenSource(tokens, filename); - tokens = new CommonTokenStream(indentedSource); - PythonParser parser = new PythonParser(tokens, encoding); - parser.setErrorHandler(errorHandler); - parser.setTreeAdaptor(new PythonTreeAdaptor()); - try { - PythonParser.file_input_return r = parser.file_input(); - tree = (mod)r.tree; - } catch (RecognitionException e) { - //XXX: this can't happen. Need to strip the throws from antlr - // generated code. - } - return tree; - } -} Modified: trunk/jython/src/org/python/antlr/ast/AstModule.java =================================================================== --- trunk/jython/src/org/python/antlr/ast/AstModule.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/antlr/ast/AstModule.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -8,12 +8,12 @@ import org.python.antlr.op.*; import org.python.core.AstList; import org.python.core.ClassDictInit; +import org.python.core.CompilerFlags; import org.python.core.imp; import org.python.core.Py; import org.python.core.PyInteger; import org.python.core.PyObject; import org.python.core.PyString; -import org.python.core.PyTableCode; import org.python.core.PyType; import org.python.antlr.AST; @@ -27,7 +27,7 @@ dict.__setitem__("__doc__", Py.None); dict.__setitem__("__name__", new PyString("_ast")); dict.__setitem__("__version__", new PyString("62047")); - dict.__setitem__("PyCF_ONLY_AST", new PyInteger(PyTableCode.PyCF_ONLY_AST)); + dict.__setitem__("PyCF_ONLY_AST", new PyInteger(CompilerFlags.PyCF_ONLY_AST)); dict.__setitem__("astlist", AstList.TYPE); Modified: trunk/jython/src/org/python/compiler/Future.java =================================================================== --- trunk/jython/src/org/python/compiler/Future.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/compiler/Future.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -8,73 +8,65 @@ import org.python.antlr.ast.Interactive; import org.python.antlr.ast.Module; import org.python.antlr.ast.Str; +import org.python.antlr.ast.alias; import org.python.antlr.base.mod; import org.python.antlr.base.stmt; +import org.python.core.CodeFlag; +import org.python.core.FutureFeature; +import org.python.core.Pragma; +import org.python.core.PragmaReceiver; +import java.util.EnumSet; import java.util.List; +import java.util.Set; public class Future { + Set<FutureFeature> featureSet = EnumSet.noneOf(FutureFeature.class); + private final PragmaReceiver features = new PragmaReceiver() { - private boolean division = false; - private boolean with_statement = false; - private boolean absolute_import = false; + public void add(Pragma pragma) { + if (pragma instanceof FutureFeature) { + FutureFeature feature = (FutureFeature) pragma; + featureSet.add(feature); + } + } - private static final String FUTURE = "__future__"; + }; private boolean check(ImportFrom cand) throws Exception { - if (!cand.getInternalModule().equals(FUTURE)) + if (!cand.getInternalModule().equals(FutureFeature.MODULE_NAME)) return false; - int n = cand.getInternalNames().size(); - if (n == 0) { - throw new ParseException("future statement does not support import *", cand); + if (cand.getInternalNames().isEmpty()) { + throw new ParseException( + "future statement does not support import *", cand); } - for (int i = 0; i < n; i++) { - String feature = cand.getInternalNames().get(i).getInternalName(); - // *known* features - if (feature.equals("nested_scopes")) { - continue; + try { + for (alias feature : cand.getInternalNames()) { + // *known* features + FutureFeature.addFeature(feature.getInternalName(), features); } - if (feature.equals("division")) { - division = true; - continue; - } - if (feature.equals("generators")) { - continue; - } - if (feature.equals("with_statement")) { - with_statement = true; - continue; - } - if (feature.equals("absolute_import")) { - absolute_import = true; - continue; - } - if (feature.equals("braces")) { - throw new ParseException("not a chance", cand); - } - if (feature.equals("GIL") || feature.equals("global_interpreter_lock")) { - throw new ParseException("Never going to happen!", cand); - } - throw new ParseException("future feature " + feature + " is not defined", cand); + } catch (ParseException pe) { + throw new ParseException(pe.getMessage(), cand); } return true; } - public void preprocessFutures(mod node, - org.python.core.CompilerFlags cflags) - throws Exception - { + public void preprocessFutures(mod node, org.python.core.CompilerFlags cflags) + throws Exception { if (cflags != null) { - division = cflags.division; - with_statement = cflags.with_statement; - absolute_import = cflags.absolute_import; + if (cflags.isFlagSet(CodeFlag.CO_FUTURE_DIVISION)) + FutureFeature.division.addTo(features); + if (cflags.isFlagSet(CodeFlag.CO_FUTURE_WITH_STATEMENT)) + FutureFeature.with_statement.addTo(features); + if (cflags.isFlagSet(CodeFlag.CO_FUTURE_ABSOLUTE_IMPORT)) + FutureFeature.absolute_import.addTo(features); } int beg = 0; List<stmt> suite = null; if (node instanceof Module) { suite = ((Module) node).getInternalBody(); - if (suite.size() > 0 && suite.get(0) instanceof Expr && - ((Expr) suite.get(0)).getInternalValue() instanceof Str) { + if (suite.size() > 0 && suite.get(0) instanceof Expr + && ((Expr) suite.get(0)).getInternalValue() instanceof Str) { beg++; } } else if (node instanceof Interactive) { @@ -85,45 +77,36 @@ for (int i = beg; i < suite.size(); i++) { stmt s = suite.get(i); - if (!(s instanceof ImportFrom)) - break; + if (!(s instanceof ImportFrom)) break; s.from_future_checked = true; - if (!check((ImportFrom)s)) - break; + if (!check((ImportFrom) s)) break; } if (cflags != null) { - cflags.division = cflags.division || division; + for (FutureFeature feature : featureSet) { + feature.setFlag(cflags); + } } - if (cflags != null) { - cflags.with_statement = cflags.with_statement || with_statement; - } - if (cflags != null) { - cflags.absolute_import = cflags.absolute_import || absolute_import; - } } - public static void checkFromFuture(ImportFrom node) throws Exception { - if (node.from_future_checked) - return; - if (node.getInternalModule().equals(FUTURE)) { - throw new ParseException("from __future__ imports must occur " + - "at the beginning of the file",node); + if (node.from_future_checked) return; + if (node.getInternalModule().equals(FutureFeature.MODULE_NAME)) { + throw new ParseException("from __future__ imports must occur " + + "at the beginning of the file", node); } node.from_future_checked = true; } public boolean areDivisionOn() { - return division; + return featureSet.contains(FutureFeature.division); } public boolean withStatementSupported() { - return with_statement; + return featureSet.contains(FutureFeature.with_statement); } public boolean isAbsoluteImportOn() { - return absolute_import; + return featureSet.contains(FutureFeature.absolute_import); } - } Modified: trunk/jython/src/org/python/compiler/Module.java =================================================================== --- trunk/jython/src/org/python/compiler/Module.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/compiler/Module.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -12,6 +12,7 @@ import org.objectweb.asm.Label; import org.objectweb.asm.Opcodes; +import org.python.core.CodeFlag; import org.python.core.CompilerFlags; import org.python.core.Py; import org.python.core.PyException; @@ -506,17 +507,17 @@ code.jy_npurecell = scope.jy_npurecell; if (compiler.optimizeGlobals) { - code.moreflags |= org.python.core.PyTableCode.CO_OPTIMIZED; + code.moreflags |= org.python.core.CodeFlag.CO_OPTIMIZED.flag; } if (compiler.my_scope.generator) { - code.moreflags |= org.python.core.PyTableCode.CO_GENERATOR; + code.moreflags |= org.python.core.CodeFlag.CO_GENERATOR.flag; } if (cflags != null) { - if (cflags.generator_allowed) { - code.moreflags |= org.python.core.PyTableCode.CO_GENERATOR_ALLOWED; + if (cflags.isFlagSet(CodeFlag.CO_GENERATOR_ALLOWED)) { + code.moreflags |= org.python.core.CodeFlag.CO_GENERATOR_ALLOWED.flag; } - if (cflags.division) { - code.moreflags |= org.python.core.PyTableCode.CO_FUTUREDIVISION; + if (cflags.isFlagSet(CodeFlag.CO_FUTURE_DIVISION)) { + code.moreflags |= org.python.core.CodeFlag.CO_FUTURE_DIVISION.flag; } } Modified: trunk/jython/src/org/python/compiler/pbc/Bytecode.java =================================================================== --- trunk/jython/src/org/python/compiler/pbc/Bytecode.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/compiler/pbc/Bytecode.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -14,6 +14,8 @@ import java.util.List; import java.util.Map; import java.util.Set; + +import org.python.core.CodeFlag; import org.python.core.Py; import org.python.core.Opcode; import org.python.core.PyBaseCode; @@ -25,7 +27,7 @@ private int co_argcount = 0; private int co_stacksize = 0; - private int co_flags = PyBaseCode.CO_OPTIMIZED | PyBaseCode.CO_NEWLOCALS; // typical usage + private int co_flags = CodeFlag.CO_OPTIMIZED.flag | CodeFlag.CO_NEWLOCALS.flag; // typical usage // co_filename = '<generated code>' // co_name = '<lambda>' // co_firstlineno = 0 @@ -135,7 +137,7 @@ public void YIELD_VALUE() { stackchange(1, 1); - co_flags |= PyBaseCode.CO_GENERATOR; + co_flags |= CodeFlag.CO_GENERATOR.flag; emit(Opcode.YIELD_VALUE); } Added: trunk/jython/src/org/python/core/CodeFlag.java =================================================================== --- trunk/jython/src/org/python/core/CodeFlag.java (rev 0) +++ trunk/jython/src/org/python/core/CodeFlag.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -0,0 +1,106 @@ +package org.python.core; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; + +/** + * Represents flags that can be set on code objects. + * + * @author Tobias Ivarsson + */ +public enum CodeFlag { + /** + * Denotes that the code block uses fast locals. + */ + CO_OPTIMIZED(0x0001), + /** + * Denotes that a new dictionary should be created for the code block. + */ + CO_NEWLOCALS(0x0002), + /** + * The compiled code block has a varargs argument. + */ + CO_VARARGS(0x0004), + /** + * The compiled code block has a varkeyword argument. + */ + CO_VARKEYWORDS(0x0008), + /** + * The compiled code block is a generator code block. + */ + CO_GENERATOR(0x0020), + /** + * Denotes that nested scopes are enabled in the code block. + */ + CO_NESTED(0x0010), + /** + * Denotes that generators are enabled in the code block. + */ + CO_GENERATOR_ALLOWED(0x1000), + /** + * Standard division of integers returns float, truncating division needs to + * be enforced. + */ + CO_FUTURE_DIVISION(0x2000), + /** + * Absolute import. + */ + CO_FUTURE_ABSOLUTE_IMPORT(0x4000), + /** + * With statement. + */ + CO_FUTURE_WITH_STATEMENT(0x8000); + + public final int flag; + private static Iterable<CodeFlag> allFlags = Collections.unmodifiableList(Arrays.asList(values())); + + private CodeFlag(int flag) { + this.flag = flag; + } + + public boolean isFlagBitSetIn(int flags) { + return (flags & flag) != 0; + } + + static Iterable<CodeFlag> parse(final int flags) { + return new Iterable<CodeFlag>() { + + public Iterator<CodeFlag> iterator() { + return new Iterator<CodeFlag>() { + Iterator<CodeFlag> all = allFlags.iterator(); + CodeFlag next = null; + + public boolean hasNext() { + if (next != null) { + return true; + } + while (all.hasNext()) { + CodeFlag flag = all.next(); + if (flag.isFlagBitSetIn(flags)) { + next = flag; + return true; + } + } + return false; + } + + public CodeFlag next() { + if (hasNext()) try { + return next; + } finally { + next = null; + } + throw new IllegalStateException(); + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + }; + } + + }; + } +} Added: trunk/jython/src/org/python/core/CompileMode.java =================================================================== --- trunk/jython/src/org/python/core/CompileMode.java (rev 0) +++ trunk/jython/src/org/python/core/CompileMode.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -0,0 +1,33 @@ +package org.python.core; + +import org.python.antlr.BaseParser; +import org.python.antlr.base.mod; + +public enum CompileMode { + eval { + @Override + mod dispatch(BaseParser parser) { + return parser.parseExpression(); + } + }, + single { + @Override + mod dispatch(BaseParser parser) { + return parser.parseInteractive(); + } + }, + exec { + @Override + mod dispatch(BaseParser parser) { + return parser.parseModule(); + } + }; + abstract mod dispatch(BaseParser parser); + + public static CompileMode getMode(String mode) { + if (!mode.equals("exec") && !mode.equals("eval") && !mode.equals("single")) { + throw Py.ValueError("compile() arg 3 must be 'exec' or 'eval' or 'single'"); + } + return valueOf(mode); + } +} Modified: trunk/jython/src/org/python/core/CompilerFlags.java =================================================================== --- trunk/jython/src/org/python/core/CompilerFlags.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/core/CompilerFlags.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -1,79 +1,104 @@ -// At some future point this will also be extended - in conjunction with Py#compileFlags - to add -// support for a compiler factory that user code can choose in place of the normal compiler. +// At some future point this will also be extended - in conjunction with +// Py#compileFlags - to add +// support for a compiler factory that user code can choose in place of the +// normal compiler. // (Perhaps a better name might have been "CompilerOptions".) package org.python.core; -public class CompilerFlags { +import java.util.Set; - private int co_flags; +import org.python.Version; - public boolean optimized; - public boolean newlocals; - public boolean varargs; - public boolean varkeywords; - public boolean generator; - - public boolean nested_scopes = true; - public boolean division; - public boolean generator_allowed = true; - public boolean with_statement; - public boolean absolute_import; - +public class CompilerFlags { + // These flags don't mean anything to the code, only to the compiler + public static final int PyCF_SOURCE_IS_UTF8 = 0x0100; + public static final int PyCF_DONT_IMPLY_DEDENT = 0x0200; + public static final int PyCF_ONLY_AST = 0x0400; public boolean only_ast; public boolean dont_imply_dedent; public boolean source_is_utf8; public String encoding; + private final Set<CodeFlag> flags = Version.getDefaultCodeFlags(); - public CompilerFlags() {} + public CompilerFlags() { + } public CompilerFlags(int co_flags) { - this.co_flags = co_flags; - optimized = isEnabled(PyBaseCode.CO_OPTIMIZED); - newlocals = isEnabled(PyBaseCode.CO_NEWLOCALS); - varargs = isEnabled(PyBaseCode.CO_VARARGS); - varkeywords = isEnabled(PyBaseCode.CO_VARKEYWORDS); - generator = isEnabled(PyBaseCode.CO_GENERATOR); - nested_scopes = isEnabled(PyBaseCode.CO_NESTED); - division = isEnabled(PyBaseCode.CO_FUTUREDIVISION); - generator_allowed = isEnabled(PyBaseCode.CO_GENERATOR_ALLOWED); - absolute_import = isEnabled(PyBaseCode.CO_FUTURE_ABSOLUTE_IMPORT); - with_statement = isEnabled(PyBaseCode.CO_WITH_STATEMENT); - only_ast = isEnabled(PyBaseCode.PyCF_ONLY_AST); - dont_imply_dedent = isEnabled(PyBaseCode.PyCF_DONT_IMPLY_DEDENT); - source_is_utf8 = isEnabled(PyBaseCode.PyCF_SOURCE_IS_UTF8); + for (CodeFlag flag : CodeFlag.parse(co_flags)) { + setFlag(flag); + } + only_ast = isEnabled(co_flags, PyCF_ONLY_AST); + dont_imply_dedent = isEnabled(co_flags, PyCF_DONT_IMPLY_DEDENT); + source_is_utf8 = isEnabled(co_flags, PyCF_SOURCE_IS_UTF8); } - private boolean isEnabled(int codeConstant) { + private boolean isEnabled(int co_flags, int codeConstant) { return (co_flags & codeConstant) != 0; } + public int toBits() { + int bits = (only_ast ? PyCF_ONLY_AST : 0) + | (dont_imply_dedent ? PyCF_DONT_IMPLY_DEDENT : 0) + | (source_is_utf8 ? PyCF_SOURCE_IS_UTF8 : 0); + for (CodeFlag flag : flags) { + bits |= flag.flag; + } + return bits; + } + + public void setFlag(CodeFlag flag) { + flags.add(flag); + } + + public boolean isFlagSet(CodeFlag flag) { + return flags.contains(flag); + } + @Override public String toString() { - return String.format("CompilerFlags[division=%s nested_scopes=%s generators=%s " - + "with_statement=%s absolute_import=%s only_ast=%s " - + "dont_imply_dedent=%s source_is_utf8=%s]", division, nested_scopes, - generator_allowed, with_statement, absolute_import, only_ast, - dont_imply_dedent, source_is_utf8); + return String.format( + "CompilerFlags[division=%s nested_scopes=%s generators=%s " + + "with_statement=%s absolute_import=%s only_ast=%s " + + "dont_imply_dedent=%s source_is_utf8=%s]", + isFlagSet(CodeFlag.CO_FUTURE_DIVISION), + isFlagSet(CodeFlag.CO_NESTED), + isFlagSet(CodeFlag.CO_GENERATOR_ALLOWED), + isFlagSet(CodeFlag.CO_FUTURE_WITH_STATEMENT), + isFlagSet(CodeFlag.CO_FUTURE_ABSOLUTE_IMPORT), only_ast, + dont_imply_dedent, source_is_utf8); } - public int toBits() { - return (optimized ? PyBaseCode.CO_OPTIMIZED : 0) | - (newlocals ? PyBaseCode.CO_NEWLOCALS : 0) | - (varargs ? PyBaseCode.CO_VARARGS : 0) | - (varkeywords ? PyBaseCode.CO_VARKEYWORDS : 0) | - (generator ? PyBaseCode.CO_GENERATOR : 0) | - (nested_scopes ? PyBaseCode.CO_NESTED : 0) | - (division ? PyBaseCode.CO_FUTUREDIVISION : 0) | - (generator_allowed ? PyBaseCode.CO_GENERATOR_ALLOWED : 0) | - (absolute_import ? PyBaseCode.CO_FUTURE_ABSOLUTE_IMPORT : 0) | - (with_statement ? PyBaseCode.CO_WITH_STATEMENT : 0) | - (only_ast ? PyBaseCode.PyCF_ONLY_AST : 0) | - (dont_imply_dedent ? PyBaseCode.PyCF_DONT_IMPLY_DEDENT : 0) | - (source_is_utf8 ? PyBaseCode.PyCF_SOURCE_IS_UTF8 : 0); + public static CompilerFlags getCompilerFlags() { + return getCompilerFlags(0, null); } + private static final int CO_ALL_FEATURES = CompilerFlags.PyCF_DONT_IMPLY_DEDENT + | CompilerFlags.PyCF_ONLY_AST + | CompilerFlags.PyCF_SOURCE_IS_UTF8 + | CodeFlag.CO_NESTED.flag + | CodeFlag.CO_GENERATOR_ALLOWED.flag + | CodeFlag.CO_FUTURE_DIVISION.flag + | CodeFlag.CO_FUTURE_ABSOLUTE_IMPORT.flag + | CodeFlag.CO_FUTURE_WITH_STATEMENT.flag; + + public static CompilerFlags getCompilerFlags(int flags, PyFrame frame) { + if ((flags & ~CO_ALL_FEATURES) != 0) { + throw Py.ValueError("compile(): unrecognised flags"); + } + return getCompilerFlags(new CompilerFlags(flags), frame); + } + + public static CompilerFlags getCompilerFlags(CompilerFlags flags, + PyFrame frame) { + if (frame != null && frame.f_code != null) { + return frame.f_code.co_flags.combine(flags); + } else { + return flags; + } + } + // this will not strictly be an OR once we have other options, like a compiler factory // in that case, we would assume public CompilerFlags combine(CompilerFlags flags) { Added: trunk/jython/src/org/python/core/FutureFeature.java =================================================================== --- trunk/jython/src/org/python/core/FutureFeature.java (rev 0) +++ trunk/jython/src/org/python/core/FutureFeature.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -0,0 +1,101 @@ +package org.python.core; + +import org.python.antlr.ParseException; + +public enum FutureFeature implements Pragma { + /** + * Enables nested scopes. + */ + nested_scopes(CodeFlag.CO_NESTED), + /** + * Makes integer / integer division return float. + */ + division(CodeFlag.CO_FUTURE_DIVISION), + /** + * Enables generators. + */ + generators(CodeFlag.CO_GENERATOR_ALLOWED), + /** + * Enables absolute imports. + */ + absolute_import(CodeFlag.CO_FUTURE_ABSOLUTE_IMPORT), + /** + * Enables the with statement. + */ + with_statement(CodeFlag.CO_FUTURE_WITH_STATEMENT), + /** + * Use braces for block delimiters instead of indentation. + */ + braces { + @Override + public void addTo(PragmaReceiver features) { + throw new ParseException("not a chance"); + } + }, + /** + * Enable the Global Interpreter Lock in Jython. + */ + GIL { + @Override + public void addTo(PragmaReceiver features) { + throw new ParseException("Never going to happen!"); + } + }, + /** + * Enable the Global Interpreter Lock in Jython. + */ + global_interpreter_lock { + @Override + public void addTo(PragmaReceiver features) { + GIL.addTo(features); + } + }; + + public static final String MODULE_NAME = "__future__"; + public static final PragmaModule PRAGMA_MODULE = new PragmaModule( + MODULE_NAME) { + + @Override + public Pragma getPragma(String name) { + return getFeature(name); + } + + @Override + public Pragma getStarPragma() { + throw new ParseException("future feature * is not defined"); + } + }; + private final CodeFlag flag; + + private FutureFeature(CodeFlag flag) { + this.flag = flag; + } + + private FutureFeature() { + this(null); + } + + public void addTo(PragmaReceiver features) { + features.add(this); + } + + public static void addFeature(String featureName, PragmaReceiver features) { + getFeature(featureName).addTo(features); + } + + private static FutureFeature getFeature(String featureName) { + try { + return valueOf(featureName); + } catch (IllegalArgumentException ex) { + throw new ParseException("future feature " + featureName + + " is not defined"); + } + } + + public void setFlag(CompilerFlags cflags) { + if (flag != null) { + cflags.setFlag(flag); + } + } + +} Modified: trunk/jython/src/org/python/core/ParserFacade.java =================================================================== --- trunk/jython/src/org/python/core/ParserFacade.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/core/ParserFacade.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -23,10 +23,7 @@ import org.antlr.runtime.CommonTokenStream; import org.python.antlr.BaseParser; -import org.python.antlr.ExpressionParser; -import org.python.antlr.InteractiveParser; import org.python.antlr.ParseException; -import org.python.antlr.ModuleParser; import org.python.antlr.NoCloseReaderStream; import org.python.antlr.PythonTree; import org.python.antlr.PythonLexer; @@ -111,30 +108,23 @@ * PyIndentationErrors. */ private static mod parse(ExpectedEncodingBufferedReader reader, - String kind, + CompileMode kind, String filename, CompilerFlags cflags) throws Throwable { reader.mark(MARK_LIMIT); // We need the ability to move back on the // reader, for the benefit of fixParseError and // validPartialSentence - if (kind.equals("eval")) { + if (kind != null) { CharStream cs = new NoCloseReaderStream(reader); - ExpressionParser e = new ExpressionParser(cs, filename, cflags.encoding); - return e.parse(); - } else if (kind.equals("single")) { - InteractiveParser i = new InteractiveParser(reader, filename, cflags.encoding); - return i.parse(); - } else if (kind.equals("exec")) { - CharStream cs = new NoCloseReaderStream(reader); - ModuleParser g = new ModuleParser(cs, filename, cflags.encoding); - return g.file_input(); + BaseParser parser = new BaseParser(cs, filename, cflags.encoding); + return kind.dispatch(parser); } else { throw Py.ValueError("parse kind must be eval, exec, or single"); } } public static mod parse(InputStream stream, - String kind, + CompileMode kind, String filename, CompilerFlags cflags) { ExpectedEncodingBufferedReader bufReader = null; @@ -151,7 +141,7 @@ } public static mod parse(String string, - String kind, + CompileMode kind, String filename, CompilerFlags cflags) { ExpectedEncodingBufferedReader bufReader = null; @@ -166,7 +156,7 @@ } public static mod partialParse(String string, - String kind, + CompileMode kind, String filename, CompilerFlags cflags, boolean stdprompt) { @@ -186,7 +176,7 @@ } } - private static boolean validPartialSentence(BufferedReader bufreader, String kind, String filename) { + private static boolean validPartialSentence(BufferedReader bufreader, CompileMode kind, String filename) { PythonLexer lexer = null; try { bufreader.reset(); @@ -197,14 +187,16 @@ PythonTokenSource indentedSource = new PythonTokenSource(tokens, filename); tokens = new CommonTokenStream(indentedSource); PythonPartial parser = new PythonPartial(tokens); - if (kind.equals("single")) { + switch (kind) { + case single: parser.single_input(); - } else if (kind.equals("eval")) { + break; + case eval: parser.eval_input(); - } else { + break; + default: return false; } - } catch (Exception e) { return lexer.eofWhileNested; } Added: trunk/jython/src/org/python/core/Pragma.java =================================================================== --- trunk/jython/src/org/python/core/Pragma.java (rev 0) +++ trunk/jython/src/org/python/core/Pragma.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -0,0 +1,46 @@ +package org.python.core; + +import org.python.antlr.ParseException; + +public interface Pragma { + void addTo(PragmaReceiver receiver); + + public abstract class PragmaModule { + public final String name; + + protected PragmaModule(String name) { + this.name = name; + } + + public abstract Pragma getPragma(String name); + + public abstract Pragma getStarPragma(); + } + + public final class ForbiddenPragmaModule extends PragmaModule { + private final String message; + + public ForbiddenPragmaModule(String name) { + this(name, "pragma " + name + " is not allowed in this context."); + } + + public ForbiddenPragmaModule(String name, String message) { + super(name); + this.message = message; + } + + @Override + public Pragma getPragma(String name) { + throw new ParseException(message); + } + + @Override + public Pragma getStarPragma() { + throw new ParseException(message); + } + + public void addTo(PragmaReceiver receiver) { + throw new ParseException(message); + } + } +} Added: trunk/jython/src/org/python/core/PragmaReceiver.java =================================================================== --- trunk/jython/src/org/python/core/PragmaReceiver.java (rev 0) +++ trunk/jython/src/org/python/core/PragmaReceiver.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -0,0 +1,5 @@ +package org.python.core; + +public interface PragmaReceiver { + void add(Pragma pragma); +} Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/core/Py.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -1218,7 +1218,7 @@ String contents = null; if (o instanceof PyString) { if (o instanceof PyUnicode) { - flags |= PyBaseCode.PyCF_SOURCE_IS_UTF8; + flags |= CompilerFlags.PyCF_SOURCE_IS_UTF8; } contents = o.toString(); } else if (o instanceof PyFile) { @@ -1231,7 +1231,7 @@ throw Py.TypeError( "exec: argument 1 must be string, code or file object"); } - code = (PyCode)Py.compile_flags(contents, "<string>", "exec", + code = (PyCode)Py.compile_flags(contents, "<string>", CompileMode.exec, getCompilerFlags(flags, false)); } Py.runCode(code, locals, globals); @@ -1603,41 +1603,31 @@ } public static CompilerFlags getCompilerFlags() { - return getCompilerFlags(0, false); + return CompilerFlags.getCompilerFlags(); } public static CompilerFlags getCompilerFlags(int flags, boolean dont_inherit) { - CompilerFlags cflags = null; + final PyFrame frame; if (dont_inherit) { - cflags = new CompilerFlags(flags); + frame = null; } else { - PyFrame frame = Py.getFrame(); - if (frame != null && frame.f_code != null) { - cflags = frame.f_code.co_flags.combine(flags); - } else { - cflags = new CompilerFlags(flags); - } + frame = Py.getFrame(); } - return cflags; + return CompilerFlags.getCompilerFlags(flags, frame); } public static CompilerFlags getCompilerFlags(CompilerFlags flags, boolean dont_inherit) { - CompilerFlags cflags = null; + final PyFrame frame; if (dont_inherit) { - cflags = flags; + frame = null; } else { - PyFrame frame = Py.getFrame(); - if (frame != null && frame.f_code != null) { - cflags = frame.f_code.co_flags.combine(flags); - } else { - cflags = flags; - } + frame = Py.getFrame(); } - return cflags; + return CompilerFlags.getCompilerFlags(flags, frame); } // w/o compiler-flags - public static PyObject compile(InputStream istream, String filename, String kind) { + public static PyCode compile(InputStream istream, String filename, CompileMode kind) { return compile_flags(istream, filename, kind, new CompilerFlags()); } @@ -1655,13 +1645,10 @@ * @param cflags Compiler flags * @return Code object for the compiled module */ - public static PyObject compile_flags(mod node, String name, String filename, + public static PyCode compile_flags(mod node, String name, String filename, boolean linenumbers, boolean printResults, CompilerFlags cflags) { try { - if (cflags != null && cflags.only_ast) { - return Py.java2py(node); - } ByteArrayOutputStream ostream = new ByteArrayOutputStream(); Module.compile(node, ostream, name, filename, linenumbers, printResults, cflags); @@ -1673,17 +1660,17 @@ } } - public static PyObject compile_flags(mod node, String filename, - String kind, CompilerFlags cflags) { + public static PyCode compile_flags(mod node, String filename, + CompileMode kind, CompilerFlags cflags) { return Py.compile_flags(node, getName(), filename, true, - kind.equals("single"), cflags); + kind == CompileMode.single, cflags); } /** * Compiles python source code coming from a file or another external stream */ - public static PyObject compile_flags(InputStream istream, String filename, - String kind, CompilerFlags cflags) { + public static PyCode compile_flags(InputStream istream, String filename, + CompileMode kind, CompilerFlags cflags) { mod node = ParserFacade.parse(istream, kind, filename, cflags); return Py.compile_flags(node, filename, kind, cflags); } @@ -1694,8 +1681,8 @@ * DO NOT use this for PyString input. Use * {@link #compile_flags(byte[], String, String, CompilerFlags)} instead. */ - public static PyObject compile_flags(String data, String filename, - String kind, CompilerFlags cflags) { + public static PyCode compile_flags(String data, String filename, + CompileMode kind, CompilerFlags cflags) { if (data.contains("\0")) { throw Py.TypeError("compile() expected string without null bytes"); } @@ -1709,7 +1696,7 @@ } public static PyObject compile_command_flags(String string, String filename, - String kind, CompilerFlags cflags, boolean stdprompt) { + CompileMode kind, CompilerFlags cflags, boolean stdprompt) { mod node = ParserFacade.partialParse(string + "\n", kind, filename, cflags, stdprompt); if (node == null) { Modified: trunk/jython/src/org/python/core/PyBaseCode.java =================================================================== --- trunk/jython/src/org/python/core/PyBaseCode.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/core/PyBaseCode.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -17,30 +17,7 @@ public int co_nlocals; public boolean varargs, varkwargs; - final public static int CO_OPTIMIZED = 0x0001; - final public static int CO_NEWLOCALS = 0x0002; - final public static int CO_VARARGS = 0x0004; - final public static int CO_VARKEYWORDS = 0x0008; - final public static int CO_GENERATOR = 0x0020; - // these are defined in __future__.py - final public static int CO_NESTED = 0x0010; - final public static int CO_GENERATOR_ALLOWED = 0x0; - final public static int CO_FUTUREDIVISION = 0x2000; - final public static int CO_FUTURE_ABSOLUTE_IMPORT = 0x4000; - final public static int CO_WITH_STATEMENT = 0x8000; - - //XXX: I'm not positive that this is the right place for these constants. - final public static int PyCF_SOURCE_IS_UTF8 = 0x0100; - final public static int PyCF_DONT_IMPLY_DEDENT = 0x0200; - final public static int PyCF_ONLY_AST = 0x0400; - - final public static int CO_ALL_FEATURES = PyCF_DONT_IMPLY_DEDENT|PyCF_ONLY_AST| - PyCF_SOURCE_IS_UTF8|CO_NESTED| - CO_GENERATOR_ALLOWED| CO_FUTUREDIVISION| - CO_FUTURE_ABSOLUTE_IMPORT|CO_WITH_STATEMENT; - - public boolean hasFreevars() { return co_freevars != null && co_freevars.length > 0; } @@ -135,7 +112,7 @@ return call(Py.EmptyObjects, Py.NoKeywords, globals, defaults, closure); PyFrame frame = new PyFrame(this, globals); - if (co_flags.generator) { + if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) { return new PyGenerator(frame, closure); } return call(frame, closure); @@ -149,7 +126,7 @@ Py.NoKeywords, globals, defaults, closure); PyFrame frame = new PyFrame(this, globals); frame.f_fastlocals[0] = arg1; - if (co_flags.generator) { + if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) { return new PyGenerator(frame, closure); } return call(frame, closure); @@ -164,7 +141,7 @@ PyFrame frame = new PyFrame(this, globals); frame.f_fastlocals[0] = arg1; frame.f_fastlocals[1] = arg2; - if (co_flags.generator) { + if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) { return new PyGenerator(frame, closure); } return call(frame, closure); @@ -181,7 +158,7 @@ frame.f_fastlocals[0] = arg1; frame.f_fastlocals[1] = arg2; frame.f_fastlocals[2] = arg3; - if (co_flags.generator) { + if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) { return new PyGenerator(frame, closure); } return call(frame, closure); @@ -297,7 +274,7 @@ co_name, argcount)); } - if (co_flags.generator) { + if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) { return new PyGenerator(frame, closure); } return call(frame, closure); Modified: trunk/jython/src/org/python/core/PyBytecode.java =================================================================== --- trunk/jython/src/org/python/core/PyBytecode.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/core/PyBytecode.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -75,8 +75,8 @@ co_freevars = freevars; co_name = name; co_flags = new CompilerFlags(flags); - varargs = co_flags.varargs; - varkwargs = co_flags.varkeywords; + varargs = co_flags.isFlagSet(CodeFlag.CO_VARARGS); + varkwargs = co_flags.isFlagSet(CodeFlag.CO_VARKEYWORDS); co_stacksize = stacksize; co_consts = constants; @@ -420,7 +420,7 @@ PyObject b = stack.pop(); PyObject a = stack.pop(); - if (!co_flags.division) { + if (!co_flags.isFlagSet(CodeFlag.CO_FUTURE_DIVISION)) { stack.push(a._div(b)); } else { stack.push(a._truediv(b)); @@ -530,7 +530,7 @@ case Opcode.INPLACE_DIVIDE: { PyObject b = stack.pop(); PyObject a = stack.pop(); - if (!co_flags.division) { + if (!co_flags.isFlagSet(CodeFlag.CO_FUTURE_DIVISION)) { stack.push(a._idiv(b)); } else { stack.push(a._itruediv(b)); @@ -1248,7 +1248,7 @@ throw ts.exception; } - if (co_flags.generator && why == Why.RETURN && retval == Py.None) { + if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR) && why == Why.RETURN && retval == Py.None) { f.f_lasti = -1; } Modified: trunk/jython/src/org/python/core/PyFrame.java =================================================================== --- trunk/jython/src/org/python/core/PyFrame.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/core/PyFrame.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -62,7 +62,7 @@ // This needs work to be efficient with multiple interpreter states if (locals == null && code != null) { // ! f_fastlocals needed for arg passing too - if (code.co_flags.optimized || code.nargs > 0) { + if (code.co_flags.isFlagSet(CodeFlag.CO_OPTIMIZED) || code.nargs > 0) { if (code.co_nlocals > 0) { // internal: may change f_fastlocals = new PyObject[code.co_nlocals - code.jy_npurecell]; @@ -191,7 +191,7 @@ PyObject o = f_fastlocals[i]; if (o != null) f_locals.__setitem__(f_code.co_varnames[i], o); } - if (!f_code.co_flags.optimized) { + if (!f_code.co_flags.isFlagSet(CodeFlag.CO_OPTIMIZED)) { f_fastlocals = null; } } Modified: trunk/jython/src/org/python/core/PyTableCode.java =================================================================== --- trunk/jython/src/org/python/core/PyTableCode.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/core/PyTableCode.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -45,12 +45,12 @@ co_name = name; if (varargs) { co_argcount -= 1; - co_flags.varargs = true; + co_flags.setFlag(CodeFlag.CO_VARARGS); } this.varkwargs = varkwargs; if (varkwargs) { co_argcount -= 1; - co_flags.varkeywords = true; + co_flags.setFlag(CodeFlag.CO_VARKEYWORDS); } co_flags = new CompilerFlags(co_flags.toBits() | moreflags); this.funcs = funcs; Modified: trunk/jython/src/org/python/core/__builtin__.java =================================================================== --- trunk/jython/src/org/python/core/__builtin__.java 2009-03-29 20:07:31 UTC (rev 6114) +++ trunk/jython/src/org/python/core/__builtin__.java 2009-03-29 22:11:34 UTC (rev 6115) @@ -527,7 +527,7 @@ PyCode code; try { - code = (PyCode)Py.compile_flags(file, name, "exec", cflags); + code = (PyCode)Py.compile_flags(file, name, CompileMode.exec, cflags); } finally { try { file.close(); @@ -1526,45 +1526,41 @@ public static PyObject compile(PyObject source, String filename, String mode, int flags, boolean dont_inherit) { - if ((flags & ~PyBaseCode.CO_ALL_FEATURES) != 0) { - throw Py.ValueError("compile(): unrecognised flags"); - } - if (!mode.equals("exec") && !mode.equals("eval") && !mode.equals("single")) { - throw Py.ValueError("compile() arg 3 must be 'exec' or 'eval' or 'single'"); - } + CompilerFlags cflags = Py.getCompilerFlags(flags, dont_inherit); + CompileMode kind = CompileMode.getMode(mode); - mod ast = py2node(source); - if (ast != null) { - return Py.compile_flags(ast, filename, mode, Py.getCompilerFlags(flags, dont_inherit)); - } - - if (!(source instanceof PyString)) { - throw Py.TypeError("expected a readable buffer object"); - } - if (source instanceof PyUnicode) { - flags |= PyBaseCode.PyCF_SOURCE_IS_UTF8; - } - return Py.compile_flags(((PyString)source).toString(), filename, mode, - Py.getCompilerFlags(flags, dont_inherit)); + return compile(source, filename, kind, cflags, dont_inherit); } - public static PyObject compile(PyObject source, String filename, String mode, CompilerFlags flags, + public static PyObject compile(PyObject source, String filename, CompileMode kind, CompilerFlags cflags, boolean dont_inherit) { - if (!mode.equals("exec") && !mode.equals("eval") && !mode.equals("single")) { - throw Py.ValueError("compile() arg 3 must be 'exec' or 'eval' or 'single'"); - } - + cflags = Py.getCompilerFlags(cflags, dont_inherit); + mod ast = py2node(source); - if (ast != null) { - return Py.compile_flags(ast, filename, mode, Py.getCompilerFlags(flags, dont_inherit)); + if (ast == null) { + if (!(source instanceof PyString)) { + ... [truncated message content] |