You can subscribe to this list here.
2000 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
(1) |
Nov
(107) |
Dec
(67) |
---|---|---|---|---|---|---|---|---|---|---|---|---|
2001 |
Jan
(76) |
Feb
(125) |
Mar
(72) |
Apr
(13) |
May
(18) |
Jun
(12) |
Jul
(129) |
Aug
(47) |
Sep
(1) |
Oct
(36) |
Nov
(128) |
Dec
(124) |
2002 |
Jan
(59) |
Feb
|
Mar
(14) |
Apr
(14) |
May
(72) |
Jun
(9) |
Jul
(3) |
Aug
(5) |
Sep
(18) |
Oct
(65) |
Nov
(28) |
Dec
(12) |
2003 |
Jan
(10) |
Feb
(2) |
Mar
(4) |
Apr
(33) |
May
(21) |
Jun
(9) |
Jul
(29) |
Aug
(34) |
Sep
(4) |
Oct
(8) |
Nov
(15) |
Dec
(4) |
2004 |
Jan
(26) |
Feb
(12) |
Mar
(11) |
Apr
(9) |
May
(7) |
Jun
|
Jul
(5) |
Aug
|
Sep
(3) |
Oct
(7) |
Nov
(1) |
Dec
(10) |
2005 |
Jan
(2) |
Feb
(72) |
Mar
(16) |
Apr
(39) |
May
(48) |
Jun
(97) |
Jul
(57) |
Aug
(13) |
Sep
(16) |
Oct
(24) |
Nov
(100) |
Dec
(24) |
2006 |
Jan
(15) |
Feb
(34) |
Mar
(33) |
Apr
(31) |
May
(79) |
Jun
(64) |
Jul
(41) |
Aug
(64) |
Sep
(31) |
Oct
(46) |
Nov
(55) |
Dec
(37) |
2007 |
Jan
(32) |
Feb
(61) |
Mar
(11) |
Apr
(58) |
May
(46) |
Jun
(30) |
Jul
(94) |
Aug
(93) |
Sep
(86) |
Oct
(69) |
Nov
(125) |
Dec
(177) |
2008 |
Jan
(169) |
Feb
(97) |
Mar
(74) |
Apr
(113) |
May
(120) |
Jun
(334) |
Jul
(215) |
Aug
(237) |
Sep
(72) |
Oct
(189) |
Nov
(126) |
Dec
(160) |
2009 |
Jan
(180) |
Feb
(45) |
Mar
(98) |
Apr
(140) |
May
(151) |
Jun
(71) |
Jul
(107) |
Aug
(119) |
Sep
(73) |
Oct
(121) |
Nov
(14) |
Dec
(6) |
2010 |
Jan
(13) |
Feb
(9) |
Mar
(10) |
Apr
(64) |
May
(3) |
Jun
(16) |
Jul
(7) |
Aug
(23) |
Sep
(17) |
Oct
(37) |
Nov
(5) |
Dec
(8) |
2011 |
Jan
(10) |
Feb
(11) |
Mar
(77) |
Apr
(11) |
May
(2) |
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
From: <fwi...@us...> - 2008-08-09 14:25:28
|
Revision: 5121 http://jython.svn.sourceforge.net/jython/?rev=5121&view=rev Author: fwierzbicki Date: 2008-08-09 14:25:25 +0000 (Sat, 09 Aug 2008) Log Message: ----------- from: http://svn.python.org/projects/python/branches/release25-maint/Lib/test_genexps.py@51333 Added Paths: ----------- branches/asm/Lib/test/test_genexps.py Added: branches/asm/Lib/test/test_genexps.py =================================================================== --- branches/asm/Lib/test/test_genexps.py (rev 0) +++ branches/asm/Lib/test/test_genexps.py 2008-08-09 14:25:25 UTC (rev 5121) @@ -0,0 +1,280 @@ +doctests = """ + +Test simple loop with conditional + + >>> sum(i*i for i in range(100) if i&1 == 1) + 166650 + +Test simple nesting + + >>> list((i,j) for i in range(3) for j in range(4) ) + [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] + +Test nesting with the inner expression dependent on the outer + + >>> list((i,j) for i in range(4) for j in range(i) ) + [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)] + +Make sure the induction variable is not exposed + + >>> i = 20 + >>> sum(i*i for i in range(100)) + 328350 + >>> i + 20 + +Test first class + + >>> g = (i*i for i in range(4)) + >>> type(g) + <type 'generator'> + >>> list(g) + [0, 1, 4, 9] + +Test direct calls to next() + + >>> g = (i*i for i in range(3)) + >>> g.next() + 0 + >>> g.next() + 1 + >>> g.next() + 4 + >>> g.next() + Traceback (most recent call last): + File "<pyshell#21>", line 1, in -toplevel- + g.next() + StopIteration + +Does it stay stopped? + + >>> g.next() + Traceback (most recent call last): + File "<pyshell#21>", line 1, in -toplevel- + g.next() + StopIteration + >>> list(g) + [] + +Test running gen when defining function is out of scope + + >>> def f(n): + ... return (i*i for i in xrange(n)) + >>> list(f(10)) + [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] + + >>> def f(n): + ... return ((i,j) for i in xrange(3) for j in xrange(n)) + >>> list(f(4)) + [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] + >>> def f(n): + ... return ((i,j) for i in xrange(3) for j in xrange(4) if j in xrange(n)) + >>> list(f(4)) + [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] + >>> list(f(2)) + [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)] + +Verify that parenthesis are required in a statement + + >>> def f(n): + ... return i*i for i in xrange(n) + Traceback (most recent call last): + ... + SyntaxError: invalid syntax + +Verify that parenthesis are required when used as a keyword argument value + + >>> dict(a = i for i in xrange(10)) + Traceback (most recent call last): + ... + SyntaxError: invalid syntax + +Verify that parenthesis are required when used as a keyword argument value + + >>> dict(a = (i for i in xrange(10))) #doctest: +ELLIPSIS + {'a': <generator object at ...>} + +Verify early binding for the outermost for-expression + + >>> x=10 + >>> g = (i*i for i in range(x)) + >>> x = 5 + >>> list(g) + [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] + +Verify that the outermost for-expression makes an immediate check +for iterability + + >>> (i for i in 6) + Traceback (most recent call last): + File "<pyshell#4>", line 1, in -toplevel- + (i for i in 6) + TypeError: 'int' object is not iterable + +Verify late binding for the outermost if-expression + + >>> include = (2,4,6,8) + >>> g = (i*i for i in range(10) if i in include) + >>> include = (1,3,5,7,9) + >>> list(g) + [1, 9, 25, 49, 81] + +Verify late binding for the innermost for-expression + + >>> g = ((i,j) for i in range(3) for j in range(x)) + >>> x = 4 + >>> list(g) + [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] + +Verify re-use of tuples (a side benefit of using genexps over listcomps) + + >>> tupleids = map(id, ((i,i) for i in xrange(10))) + >>> int(max(tupleids) - min(tupleids)) + 0 + +Verify that syntax error's are raised for genexps used as lvalues + + >>> (y for y in (1,2)) = 10 + Traceback (most recent call last): + ... + SyntaxError: can't assign to generator expression (<doctest test.test_genexps.__test__.doctests[40]>, line 1) + + >>> (y for y in (1,2)) += 10 + Traceback (most recent call last): + ... + SyntaxError: augmented assignment to generator expression not possible (<doctest test.test_genexps.__test__.doctests[41]>, line 1) + + +########### Tests borrowed from or inspired by test_generators.py ############ + +Make a generator that acts like range() + + >>> yrange = lambda n: (i for i in xrange(n)) + >>> list(yrange(10)) + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + +Generators always return to the most recent caller: + + >>> def creator(): + ... r = yrange(5) + ... print "creator", r.next() + ... return r + >>> def caller(): + ... r = creator() + ... for i in r: + ... print "caller", i + >>> caller() + creator 0 + caller 1 + caller 2 + caller 3 + caller 4 + +Generators can call other generators: + + >>> def zrange(n): + ... for i in yrange(n): + ... yield i + >>> list(zrange(5)) + [0, 1, 2, 3, 4] + + +Verify that a gen exp cannot be resumed while it is actively running: + + >>> g = (me.next() for i in xrange(10)) + >>> me = g + >>> me.next() + Traceback (most recent call last): + File "<pyshell#30>", line 1, in -toplevel- + me.next() + File "<pyshell#28>", line 1, in <generator expression> + g = (me.next() for i in xrange(10)) + ValueError: generator already executing + +Verify exception propagation + + >>> g = (10 // i for i in (5, 0, 2)) + >>> g.next() + 2 + >>> g.next() + Traceback (most recent call last): + File "<pyshell#37>", line 1, in -toplevel- + g.next() + File "<pyshell#35>", line 1, in <generator expression> + g = (10 // i for i in (5, 0, 2)) + ZeroDivisionError: integer division or modulo by zero + >>> g.next() + Traceback (most recent call last): + File "<pyshell#38>", line 1, in -toplevel- + g.next() + StopIteration + +Make sure that None is a valid return value + + >>> list(None for i in xrange(10)) + [None, None, None, None, None, None, None, None, None, None] + +Check that generator attributes are present + + >>> g = (i*i for i in range(3)) + >>> expected = set(['gi_frame', 'gi_running', 'next']) + >>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected + True + + >>> print g.next.__doc__ + x.next() -> the next value, or raise StopIteration + >>> import types + >>> isinstance(g, types.GeneratorType) + True + +Check the __iter__ slot is defined to return self + + >>> iter(g) is g + True + +Verify that the running flag is set properly + + >>> g = (me.gi_running for i in (0,1)) + >>> me = g + >>> me.gi_running + 0 + >>> me.next() + 1 + >>> me.gi_running + 0 + +Verify that genexps are weakly referencable + + >>> import weakref + >>> g = (i*i for i in range(4)) + >>> wr = weakref.ref(g) + >>> wr() is g + True + >>> p = weakref.proxy(g) + >>> list(p) + [0, 1, 4, 9] + + +""" + + +__test__ = {'doctests' : doctests} + +def test_main(verbose=None): + import sys + from test import test_support + from test import test_genexps + test_support.run_doctest(test_genexps, verbose) + + # verify reference counting + if verbose and hasattr(sys, "gettotalrefcount"): + import gc + counts = [None] * 5 + for i in xrange(len(counts)): + test_support.run_doctest(test_genexps, verbose) + gc.collect() + counts[i] = sys.gettotalrefcount() + print counts + +if __name__ == "__main__": + test_main(verbose=True) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-09 14:00:32
|
Revision: 5120 http://jython.svn.sourceforge.net/jython/?rev=5120&view=rev Author: fwierzbicki Date: 2008-08-09 14:00:30 +0000 (Sat, 09 Aug 2008) Log Message: ----------- Surely a typo? Modified Paths: -------------- branches/asm/Lib/test/test_deque.py Modified: branches/asm/Lib/test/test_deque.py =================================================================== --- branches/asm/Lib/test/test_deque.py 2008-08-09 13:52:14 UTC (rev 5119) +++ branches/asm/Lib/test/test_deque.py 2008-08-09 14:00:30 UTC (rev 5120) @@ -476,7 +476,7 @@ self.assertEqual(str(p), str(d)) d = None if test_support.is_jython: - from test_weakref import extra_collect() + from test_weakref import extra_collect extra_collect() self.assertRaises(ReferenceError, str, p) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-09 13:52:16
|
Revision: 5119 http://jython.svn.sourceforge.net/jython/?rev=5119&view=rev Author: fwierzbicki Date: 2008-08-09 13:52:14 +0000 (Sat, 09 Aug 2008) Log Message: ----------- debug() is not really all that necessary now that these have been extracted from the .g files. Modified Paths: -------------- branches/asm/src/org/python/antlr/GrammarActions.java Modified: branches/asm/src/org/python/antlr/GrammarActions.java =================================================================== --- branches/asm/src/org/python/antlr/GrammarActions.java 2008-08-09 12:36:39 UTC (rev 5118) +++ branches/asm/src/org/python/antlr/GrammarActions.java 2008-08-09 13:52:14 UTC (rev 5119) @@ -92,11 +92,6 @@ public void setErrorHandler(ErrorHandler eh) { this.errorHandler = eh; } - void debug(String x) { - if (false) { - System.out.println(x); - } - } void throwGenExpNotSoleArg(PythonTree t) { throw new ParseException("Generator expression must be parenthesized if not sole argument", t); @@ -144,7 +139,6 @@ return errorHandler.errorStmt(t); } argumentsType a; - debug("Matched FunctionDef"); if (args != null) { a = args; } else { @@ -162,7 +156,6 @@ argumentsType makeArgumentsType(Token t, List params, Token snameToken, Token knameToken, List defaults) { - debug("Matched Arguments"); exprType[] p = (exprType[])params.toArray(new exprType[params.size()]); exprType[] d = (exprType[])defaults.toArray(new exprType[defaults.size()]); @@ -184,7 +177,6 @@ Object makeFloat(Token t) { - debug("makeFloat matched " + t.getText()); return Py.newFloat(Double.valueOf(t.getText())); } @@ -195,7 +187,6 @@ } Object makeInt(Token t) { - debug("Num matched " + t.getText()); String s = t.getText(); int radix = 10; if (s.startsWith("0x") || s.startsWith("0X")) { @@ -335,7 +326,6 @@ argumentsType makeArgumentsType(PythonTree t, List params, PythonTree snameToken, PythonTree knameToken, List defaults) { - debug("Matched Arguments"); exprType[] p = (exprType[])params.toArray(new exprType[params.size()]); exprType[] d = (exprType[])defaults.toArray(new exprType[defaults.size()]); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-09 12:36:42
|
Revision: 5118 http://jython.svn.sourceforge.net/jython/?rev=5118&view=rev Author: fwierzbicki Date: 2008-08-09 12:36:39 +0000 (Sat, 09 Aug 2008) Log Message: ----------- merge and add more compops. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-08-09 06:18:29 UTC (rev 5117) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-08-09 12:36:39 UTC (rev 5118) @@ -220,8 +220,11 @@ private boolean seenSingleOuterSuite = false; + private GrammarActions actions = new GrammarActions(); + public void setErrorHandler(ErrorHandler eh) { this.errorHandler = eh; + actions.setErrorHandler(eh); } private void debug(String message) { @@ -230,357 +233,6 @@ } } - private String makeFromText(List dots, String name) { - StringBuffer d = new StringBuffer(); - if (dots != null) { - for (int i=0;i<dots.size();i++) { - d.append("."); - } - } - if (name != null) { - d.append(name); - } - return d.toString(); - } - - private int makeLevel(List lev) { - if (lev == null) { - return 0; - } - return lev.size(); - } - - private aliasType[] makeStarAlias(Token t) { - return new aliasType[]{new aliasType(t, "*", null)}; - } - - private aliasType[] makeAliases(aliasType[] atypes) { - if (atypes == null) { - return new aliasType[0]; - } - return atypes; - } - - private exprType[] makeBases(exprType etype) { - if (etype != null) { - if (etype instanceof Tuple) { - return ((Tuple)etype).elts; - } - return new exprType[]{etype}; - } - return new exprType[0]; - } - - private String[] makeNames(List names) { - List<String> s = new ArrayList<String>(); - for(int i=0;i<names.size();i++) { - s.add(((Token)names.get(i)).getText()); - } - return s.toArray(new String[s.size()]); - } - - private void throwGenExpNotSoleArg(PythonTree t) { - throw new ParseException("Generator expression must be parenthesized if not sole argument", t); - } - - private exprType[] makeExprs(List exprs) { - return makeExprs(exprs, 0); - } - - private exprType[] makeExprs(List exprs, int start) { - if (exprs != null) { - List<exprType> result = new ArrayList<exprType>(); - for (int i=start; i<exprs.size(); i++) { - Object o = exprs.get(i); - if (o instanceof exprType) { - result.add((exprType)o); - } else if (o instanceof test_return) { - result.add((exprType)((test_return)o).tree); - } - } - return result.toArray(new exprType[result.size()]); - } - return new exprType[0]; - } - - private stmtType[] makeElses(List elseSuite, List elifs) { - stmtType[] o; - o = makeStmts(elseSuite); - if (elifs != null) { - ListIterator iter = elifs.listIterator(elifs.size()); - while (iter.hasPrevious()) { - If elif = (If)iter.previous(); - elif.orelse = o; - o = new stmtType[]{elif}; - } - } - return o; - } - - private stmtType[] makeStmts(List stmts) { - if (stmts != null) { - List<stmtType> result = new ArrayList<stmtType>(); - for (int i=0; i<stmts.size(); i++) { - Object o = stmts.get(i); - if (o instanceof stmtType) { - result.add((stmtType)o); - } else { - result.add((stmtType)((stmt_return)o).tree); - } - } - return (stmtType[])result.toArray(new stmtType[result.size()]); - } - return new stmtType[0]; - } - - private exprType makeDottedAttr(Token nameToken, List attrs) { - exprType current = new Name(nameToken, nameToken.getText(), expr_contextType.Load); - for (int i=attrs.size() - 1; i > -1; i--) { - Token t = ((PythonTree)attrs.get(i)).token; - current = new Attribute(t, current, t.getText(), - expr_contextType.Load); - } - return current; - } - - private While makeWhile(Token t, exprType test, List body, List orelse) { - stmtType[] o = makeStmts(orelse); - stmtType[] b = makeStmts(body); - return new While(t, test, b, o); - } - - private For makeFor(Token t, exprType target, exprType iter, List body, List orelse) { - stmtType[] o = makeStmts(orelse); - stmtType[] b = makeStmts(body); - return new For(t, target, iter, b, o); - } - - private stmtType makeTryExcept(Token t, List body, List handlers, List orelse, List finBody) { - stmtType[] b = (stmtType[])body.toArray(new stmtType[body.size()]); - excepthandlerType[] e = (excepthandlerType[])handlers.toArray(new excepthandlerType[handlers.size()]); - stmtType[] o; - if (orelse != null) { - o = (stmtType[])orelse.toArray(new stmtType[orelse.size()]); - } else { - o = new stmtType[0]; - } - - stmtType te = new TryExcept(t, b, e, o); - if (finBody == null) { - return te; - } - stmtType[] f = (stmtType[])finBody.toArray(new stmtType[finBody.size()]); - stmtType[] mainBody = new stmtType[]{te}; - return new TryFinally(t, mainBody, f); - } - - private TryFinally makeTryFinally(Token t, List body, List finBody) { - stmtType[] b = (stmtType[])body.toArray(new stmtType[body.size()]); - stmtType[] f = (stmtType[])finBody.toArray(new stmtType[finBody.size()]); - return new TryFinally(t, b, f); - } - - private FunctionDef makeFunctionDef(PythonTree t, PythonTree nameToken, argumentsType args, List funcStatements, List decorators) { - argumentsType a; - debug("Matched FunctionDef"); - if (args != null) { - a = args; - } else { - a = new argumentsType(t, new exprType[0], null, null, new exprType[0]); - } - stmtType[] s = (stmtType[])funcStatements.toArray(new stmtType[funcStatements.size()]); - exprType[] d; - if (decorators != null) { - d = (exprType[])decorators.toArray(new exprType[decorators.size()]); - } else { - d = new exprType[0]; - } - return new FunctionDef(t, nameToken.getText(), a, s, d); - } - - private exprType[] makeAssignTargets(exprType lhs, List rhs) { - exprType[] e = new exprType[rhs.size()]; - e[0] = lhs; - for(int i=0;i<rhs.size() - 1;i++) { - testlist_return r = (testlist_return)rhs.get(i); - e[i] = (exprType)r.getTree(); - } - return e; - } - - private exprType makeAssignValue(List rhs) { - exprType value = (exprType)rhs.get(rhs.size() -1); - - if (value instanceof Context) { - //XXX: for Tuples, etc -- will need to recursively set to expr_contextType.Load. - ((Context)value).setContext(expr_contextType.Load); - } - return value; - } - - private argumentsType makeArgumentsType(Token t, List params, Token snameToken, - Token knameToken, List defaults) { - debug("Matched Arguments"); - - exprType[] p; - if (params == null) { - p = new exprType[0]; - } else { - p = (exprType[])params.toArray(new exprType[params.size()]); - } - exprType[] d = (exprType[])defaults.toArray(new exprType[defaults.size()]); - String s; - String k; - if (snameToken == null) { - s = null; - } else { - s = snameToken.getText(); - } - if (knameToken == null) { - k = null; - } else { - k = knameToken.getText(); - } - return new argumentsType(t, p, s, k, d); - } - - exprType[] extractArgs(List args) { - if (args == null) { - return new exprType[0]; - } - return (exprType[])args.toArray(new exprType[args.size()]); - } - - keywordType[] makeKeywords(List args) { - List<keywordType> k = new ArrayList<keywordType>(); - if (args != null) { - for(int i=0;i<args.size();i++) { - exprType[] e = (exprType[])args.get(i); - Name arg = (Name)e[0]; - k.add(new keywordType(arg, arg.id, e[1])); - } - return k.toArray(new keywordType[k.size()]); - } - return new keywordType[0]; - } - - Object makeFloat(Token t) { - debug("makeFloat matched " + t.getText()); - return Py.newFloat(Double.valueOf(t.getText())); - } - - Object makeComplex(Token t) { - String s = t.getText(); - s = s.substring(0, s.length() - 1); - return Py.newImaginary(Double.valueOf(s)); - } - - Object makeInt(Token t) { - debug("Num matched " + t.getText()); - String s = t.getText(); - int radix = 10; - if (s.startsWith("0x") || s.startsWith("0X")) { - radix = 16; - s = s.substring(2, s.length()); - } else if (s.startsWith("0")) { - radix = 8; - } - if (s.endsWith("L") || s.endsWith("l")) { - s = s.substring(0, s.length()-1); - return Py.newLong(new BigInteger(s, radix)); - } - int ndigits = s.length(); - int i=0; - while (i < ndigits && s.charAt(i) == '0') - i++; - if ((ndigits - i) > 11) { - return Py.newLong(new BigInteger(s, radix)); - } - - long l = Long.valueOf(s, radix).longValue(); - if (l > 0xffffffffl || (l > Integer.MAX_VALUE)) { - return Py.newLong(new BigInteger(s, radix)); - } - return Py.newInteger((int) l); - } - - class StringPair { - private String s; - private boolean unicode; - - StringPair(String s, boolean unicode) { - this.s = s; - this.unicode = unicode; - } - String getString() { - return s; - } - - boolean isUnicode() { - return unicode; - } - } - - PyString extractStrings(List s) { - boolean ustring = false; - Token last = null; - StringBuffer sb = new StringBuffer(); - Iterator iter = s.iterator(); - while (iter.hasNext()) { - last = (Token)iter.next(); - StringPair sp = extractString(last); - if (sp.isUnicode()) { - ustring = true; - } - sb.append(sp.getString()); - } - if (ustring) { - return new PyUnicode(sb.toString()); - } - return new PyString(sb.toString()); - } - - StringPair extractString(Token t) { - String s = t.getText(); - char quoteChar = s.charAt(0); - int start=0; - boolean ustring = false; - if (quoteChar == 'u' || quoteChar == 'U') { - ustring = true; - start++; - } - quoteChar = s.charAt(start); - boolean raw = false; - if (quoteChar == 'r' || quoteChar == 'R') { - raw = true; - start++; - } - int quotes = 3; - if (s.length() - start == 2) { - quotes = 1; - } - if (s.charAt(start) != s.charAt(start+1)) { - quotes = 1; - } - - if (raw) { - return new StringPair(s.substring(quotes+start, s.length()-quotes), ustring); - } else { - StringBuffer sb = new StringBuffer(s.length()); - char[] ca = s.toCharArray(); - int n = ca.length-quotes; - int i=quotes+start; - int last_i=i; - return new StringPair(PyString.decode_UnicodeEscape(s, i, n, "strict", ustring), ustring); - //return decode_UnicodeEscape(s, i, n, "strict", ustring); - } - } - - Token extractStringToken(List s) { - return (Token)s.get(s.size() - 1); - } - - protected void mismatch(IntStream input, int ttype, BitSet follow) throws RecognitionException { if (errorHandler.isRecoverable()) { super.mismatch(input, ttype, follow); @@ -675,7 +327,7 @@ //file_input: (NEWLINE | stmt)* ENDMARKER file_input - : (NEWLINE | s+=stmt)+ -> ^(ModuleTok<Module>[$file_input.start, makeStmts($s)]) + : (NEWLINE | s+=stmt)+ -> ^(ModuleTok<Module>[$file_input.start, actions.makeStmts($s)]) | -> ^(ModuleTok<Module>[$file_input.start, new stmtType[0\]]) ; @@ -686,7 +338,7 @@ //not in CPython's Grammar file dotted_attr returns [exprType etype] : n1=NAME - ( (DOT n2+=dotted_attr)+ { $etype = makeDottedAttr($n1, $n2); } + ( (DOT n2+=dotted_attr)+ { $etype = actions.makeDottedAttr($n1, $n2); } | { $etype = new Name($NAME, $NAME.text, expr_contextType.Load); } ) ; @@ -737,8 +389,8 @@ : AT dotted_attr //XXX: ignoring the arglist and Call generation right now. ( LPAREN (arglist - {$etype = new Call($LPAREN, $dotted_attr.etype, makeExprs($arglist.args), - makeKeywords($arglist.keywords), $arglist.starargs, $arglist.kwargs);} + {$etype = new Call($LPAREN, $dotted_attr.etype, actions.makeExprs($arglist.args), + actions.makeKeywords($arglist.keywords), $arglist.starargs, $arglist.kwargs);} | {$etype = $dotted_attr.etype;} ) RPAREN @@ -754,8 +406,8 @@ //funcdef: [decorators] 'def' NAME parameters ':' suite funcdef : decorators? DEF NAME parameters COLON suite - -> ^(DEF<FunctionDef>[$DEF, $NAME.text, $parameters.args, makeStmts($suite.stmts), - makeExprs($decorators.etypes)]) + -> ^(DEF<FunctionDef>[$DEF, $NAME.text, $parameters.args, actions.makeStmts($suite.stmts), + actions.makeExprs($decorators.etypes)]) ; //parameters: '(' [varargslist] ')' @@ -797,11 +449,11 @@ | DOUBLESTAR kwargs=NAME )? )? - {$args = makeArgumentsType($varargslist.start, $d, $starargs, $kwargs, defaults);} + {$args = actions.makeArgumentsType($varargslist.start, $d, $starargs, $kwargs, defaults);} | STAR starargs=NAME (COMMA DOUBLESTAR kwargs=NAME)?{debug("parsed varargslist STARARGS");} - {$args = makeArgumentsType($varargslist.start, $d, $starargs, $kwargs, defaults);} + {$args = actions.makeArgumentsType($varargslist.start, $d, $starargs, $kwargs, defaults);} | DOUBLESTAR kwargs=NAME {debug("parsed varargslist KWS");} - {$args = makeArgumentsType($varargslist.start, $d, null, $kwargs, defaults);} + {$args = actions.makeArgumentsType($varargslist.start, $d, null, $kwargs, defaults);} ; //fpdef: NAME | '(' fplist ')' @@ -810,7 +462,7 @@ | (LPAREN fpdef[expr_contextType.Load] COMMA) => LPAREN fplist RPAREN -> fplist | LPAREN fplist RPAREN - -> ^(LPAREN<Tuple>[$fplist.start, makeExprs($fplist.etypes), expr_contextType.Store]) + -> ^(LPAREN<Tuple>[$fplist.start, actions.makeExprs($fplist.etypes), expr_contextType.Store]) ; //fplist: fpdef (',' fpdef)* [','] @@ -848,8 +500,8 @@ ) ( (augassign yield_expr -> ^(augassign $lhs yield_expr)) | (augassign rhs=testlist[expr_contextType.Load] -> ^(augassign $lhs $rhs)) - | ((at=ASSIGN t+=testlist[expr_contextType.Store])+ -> ^(AssignTok<Assign>[$at, makeAssignTargets((exprType)$lhs.tree, $t), makeAssignValue($t)])) - | ((ay=ASSIGN y+=yield_expr)+ -> ^(AssignTok<Assign>[$ay, makeAssignTargets((exprType)$lhs.tree, $y), makeAssignValue($y)])) + | ((at=ASSIGN t+=testlist[expr_contextType.Store])+ -> ^(AssignTok<Assign>[$at, actions.makeAssignTargets((exprType)$lhs.tree, $t), actions.makeAssignValue($t)])) + | ((ay=ASSIGN y+=yield_expr)+ -> ^(AssignTok<Assign>[$ay, actions.makeAssignTargets((exprType)$lhs.tree, $y), actions.makeAssignValue($y)])) | -> ExprTok<Expr>[$lhs.start, (exprType)$lhs.tree] ) ; @@ -874,9 +526,9 @@ // '>>' test [ (',' test)+ [','] ] ) print_stmt : PRINT ( t1=printlist - -> ^(PRINT<Print>[$PRINT, null, makeExprs($t1.elts), $t1.newline]) + -> ^(PRINT<Print>[$PRINT, null, actions.makeExprs($t1.elts), $t1.newline]) | RIGHTSHIFT t2=printlist2 - -> ^(PRINT<Print>[$PRINT, (exprType)$t2.elts.get(0), makeExprs($t2.elts, 1), $t2.newline]) + -> ^(PRINT<Print>[$PRINT, (exprType)$t2.elts.get(0), actions.makeExprs($t2.elts, 1), $t2.newline]) | -> ^(PRINT<Print>[$PRINT, null, new exprType[0\], false]) ) @@ -920,7 +572,7 @@ //del_stmt: 'del' exprlist del_stmt : DELETE exprlist2 - -> ^(DELETE<Delete>[$DELETE, makeExprs($exprlist2.etypes)]) + -> ^(DELETE<Delete>[$DELETE, actions.makeExprs($exprlist2.etypes)]) ; //pass_stmt: 'pass' @@ -976,11 +628,11 @@ // 'import' ('*' | '(' import_as_names ')' | import_as_names)) import_from: FROM (d+=DOT* dotted_name | d+=DOT+) IMPORT (STAR - -> ^(FROM<ImportFrom>[$FROM, makeFromText($d, $dotted_name.text), makeStarAlias($STAR), makeLevel($d)]) + -> ^(FROM<ImportFrom>[$FROM, actions.makeFromText($d, $dotted_name.text), actions.makeStarAlias($STAR), actions.makeLevel($d)]) | i1=import_as_names - -> ^(FROM<ImportFrom>[$FROM, makeFromText($d, $dotted_name.text), makeAliases($i1.atypes), makeLevel($d)]) + -> ^(FROM<ImportFrom>[$FROM, actions.makeFromText($d, $dotted_name.text), actions.makeAliases($i1.atypes), actions.makeLevel($d)]) | LPAREN i2=import_as_names RPAREN - -> ^(FROM<ImportFrom>[$FROM, makeFromText($d, $dotted_name.text), makeAliases($i2.atypes), makeLevel($d)]) + -> ^(FROM<ImportFrom>[$FROM, actions.makeFromText($d, $dotted_name.text), actions.makeAliases($i2.atypes), actions.makeLevel($d)]) ) ; @@ -1026,7 +678,7 @@ //global_stmt: 'global' NAME (',' NAME)* global_stmt : GLOBAL n+=NAME (COMMA n+=NAME)* - -> ^(GLOBAL<Global>[$GLOBAL, makeNames($n)]) + -> ^(GLOBAL<Global>[$GLOBAL, actions.makeNames($n)]) ; //exec_stmt: 'exec' expr ['in' test [',' test]] @@ -1056,12 +708,12 @@ //if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] if_stmt: IF test[expr_contextType.Load] COLON ifsuite=suite elifs+=elif_clause* (ORELSE COLON elsesuite=suite)? - -> ^(IF<If>[$IF, (exprType)$test.tree, makeStmts($ifsuite.stmts), makeElses($elsesuite.stmts, $elifs)]) + -> ^(IF<If>[$IF, (exprType)$test.tree, actions.makeStmts($ifsuite.stmts), actions.makeElses($elsesuite.stmts, $elifs)]) ; //not in CPython's Grammar file elif_clause : ELIF test[expr_contextType.Load] COLON suite - -> ^(ELIF<If>[$ELIF, (exprType)$test.tree, makeStmts($suite.stmts), new stmtType[0\]]) + -> ^(ELIF<If>[$ELIF, (exprType)$test.tree, actions.makeStmts($suite.stmts), new stmtType[0\]]) ; //while_stmt: 'while' test ':' suite ['else' ':' suite] @@ -1070,7 +722,7 @@ $while_stmt.tree = $stype; } : WHILE test[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? { - $stype = makeWhile($WHILE, (exprType)$test.tree, $s1.stmts, $s2.stmts); + $stype = actions.makeWhile($WHILE, (exprType)$test.tree, $s1.stmts, $s2.stmts); } ; @@ -1080,7 +732,7 @@ $for_stmt.tree = $stype; } : FOR exprlist[expr_contextType.Store] IN testlist[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? { - $stype = makeFor($FOR, $exprlist.etype, (exprType)$testlist.tree, $s2.stmts, $s2.stmts); + $stype = actions.makeFor($FOR, $exprlist.etype, (exprType)$testlist.tree, $s2.stmts, $s2.stmts); } ; @@ -1095,10 +747,10 @@ } : TRY COLON trysuite=suite ( e+=except_clause+ (ORELSE COLON elsesuite=suite)? (FINALLY COLON finalsuite=suite)? { - $stype = makeTryExcept($TRY, $trysuite.stmts, $e, $elsesuite.stmts, $finalsuite.stmts); + $stype = actions.makeTryExcept($TRY, $trysuite.stmts, $e, $elsesuite.stmts, $finalsuite.stmts); } | FINALLY COLON finalsuite=suite { - $stype = makeTryFinally($TRY, $trysuite.stmts, $finalsuite.stmts); + $stype = actions.makeTryFinally($TRY, $trysuite.stmts, $finalsuite.stmts); } ) ; @@ -1109,7 +761,7 @@ $with_stmt.tree = $stype; } :WITH test[expr_contextType.Load] (with_var)? COLON suite { - $stype = new With($WITH, (exprType)$test.tree, $with_var.etype, makeStmts($suite.stmts)); + $stype = new With($WITH, (exprType)$test.tree, $with_var.etype, actions.makeStmts($suite.stmts)); } ; @@ -1122,7 +774,7 @@ //except_clause: 'except' [test [',' test]] except_clause : EXCEPT (t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? COLON suite - -> ^(EXCEPT<excepthandlerType>[$EXCEPT, (exprType)$t1.tree, (exprType)$t2.tree, makeStmts($suite.stmts), $EXCEPT.getLine(), $EXCEPT.getCharPositionInLine()]) + -> ^(EXCEPT<excepthandlerType>[$EXCEPT, (exprType)$t1.tree, (exprType)$t2.tree, actions.makeStmts($suite.stmts), $EXCEPT.getLine(), $EXCEPT.getCharPositionInLine()]) ; //suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT @@ -1165,13 +817,57 @@ System.out.println("c.left:" + c.left); c.comparators = new exprType[]{(exprType)c.getChild(1)}; switch (c.getType()) { + case LESS: + c.ops = new cmpopType[]{cmpopType.Lt}; + break; + case GREATER: + c.ops = new cmpopType[]{cmpopType.Gt}; + break; + case EQUAL: + c.ops = new cmpopType[]{cmpopType.Eq}; + break; + case GREATEREQUAL: + c.ops = new cmpopType[]{cmpopType.GtE}; + break; + case LESSEQUAL: + c.ops = new cmpopType[]{cmpopType.LtE}; + break; + case ALT_NOTEQUAL: + c.ops = new cmpopType[]{cmpopType.Eq}; + break; + case NOTEQUAL: + c.ops = new cmpopType[]{cmpopType.NotEq}; + break; case IN: c.ops = new cmpopType[]{cmpopType.In}; break; + case NotIn: + c.ops = new cmpopType[]{cmpopType.NotIn}; + break; + case IS: + c.ops = new cmpopType[]{cmpopType.Is}; + break; + case IsNot: + c.ops = new cmpopType[]{cmpopType.IsNot}; + break; + default: + c.ops = new cmpopType[]{cmpopType.UNDEFINED}; } } } - : expr[ctype] (IN<Compare>^ expr[ctype])* +//comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' + : expr[ctype] (LESS<Compare>^ expr[ctype] + |GREATER<Compare>^ expr[ctype] + |EQUAL<Compare>^ expr[ctype] + |GREATEREQUAL<Compare>^ expr[ctype] + |LESSEQUAL<Compare>^ expr[ctype] + |ALT_NOTEQUAL<Compare>^ expr[ctype] + |NOTEQUAL<Compare>^ expr[ctype] + |IN<Compare>^ expr[ctype] + //|NotIn<Compare>^ exper[ctype] + |IS<Compare>^ expr[ctype] + //|IsNot<Compare>^ exper[ctype] + )* ; //comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' @@ -1290,12 +986,12 @@ | LCURLY (dictmaker)? RCURLY -> ^(Dict LCURLY ^(Elts dictmaker)?) | BACKQUOTE testlist[expr_contextType.Load] BACKQUOTE -> ^(Repr BACKQUOTE testlist) | NAME -> ^(NameTok<Name>[$NAME, $NAME.text, $expr::ctype]) - | INT -> ^(NumTok<Num>[$INT, makeInt($INT)]) - | LONGINT -> ^(NumTok<Num>[$LONGINT, makeInt($LONGINT)]) - | FLOAT -> ^(NumTok<Num>[$FLOAT, makeFloat($FLOAT)]) - | COMPLEX -> ^(NumTok<Num>[$COMPLEX, makeComplex($COMPLEX)]) + | INT -> ^(NumTok<Num>[$INT, actions.makeInt($INT)]) + | LONGINT -> ^(NumTok<Num>[$LONGINT, actions.makeInt($LONGINT)]) + | FLOAT -> ^(NumTok<Num>[$FLOAT, actions.makeFloat($FLOAT)]) + | COMPLEX -> ^(NumTok<Num>[$COMPLEX, actions.makeComplex($COMPLEX)]) | (S+=STRING)+ - -> ^(StrTok<Str>[extractStringToken($S), extractStrings($S)]) + -> ^(StrTok<Str>[actions.extractStringToken($S), actions.extractStrings($S)]) ; //listmaker: test ( list_for | (',' test)* [','] ) @@ -1306,7 +1002,7 @@ : t+=test[expr_contextType.Load] ( list_for -> ^(ListComp test list_for) | (options {greedy=true;}:COMMA t+=test[expr_contextType.Load])* { - $etype = new org.python.antlr.ast.List($listmaker.start, makeExprs($t), $expr::ctype); + $etype = new org.python.antlr.ast.List($listmaker.start, actions.makeExprs($t), $expr::ctype); } ) (COMMA)? ; @@ -1315,7 +1011,7 @@ testlist_gexp : t+=test[expr_contextType.Load] ( ((options {k=2;}: c1=COMMA t+=test[expr_contextType.Load])* (c2=COMMA)? - -> { $c1 != null || $c2 != null }? ^(TupleTok<Tuple>[$testlist_gexp.start, makeExprs($t), $expr::ctype]) + -> { $c1 != null || $c2 != null }? ^(TupleTok<Tuple>[$testlist_gexp.start, actions.makeExprs($t), $expr::ctype]) -> test ) | ( gen_for -> ^(GeneratorExp test gen_for) @@ -1329,7 +1025,7 @@ ; //trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME -trailer : LPAREN (arglist -> ^(LPAREN<Call>[$LPAREN, null, makeExprs($arglist.args), makeKeywords($arglist.keywords), $arglist.starargs, $arglist.kwargs]) +trailer : LPAREN (arglist -> ^(LPAREN<Call>[$LPAREN, null, actions.makeExprs($arglist.args), actions.makeKeywords($arglist.keywords), $arglist.starargs, $arglist.kwargs]) | -> ^(LPAREN<Call>[$LPAREN, null, new exprType[0\], new keywordType[0\], null, null]) ) RPAREN @@ -1387,7 +1083,7 @@ //exprlist: expr (',' expr)* [','] exprlist[expr_contextType ctype] returns [exprType etype] : (expr[expr_contextType.Load] COMMA) => e+=expr[ctype] (options {k=2;}: COMMA e+=expr[ctype])* (COMMA)? { - $etype = new Tuple($exprlist.start, makeExprs($e), ctype); + $etype = new Tuple($exprlist.start, actions.makeExprs($e), ctype); } | expr[ctype] { $etype = (exprType)$expr.tree; @@ -1404,7 +1100,7 @@ //testlist: test (',' test)* [','] testlist[expr_contextType ctype] returns [exprType etype] : (test[expr_contextType.Load] COMMA) => t+=test[ctype] (options {k=2;}: c1=COMMA t+=test[ctype])* (c2=COMMA)? { - $etype = new Tuple($testlist.start, makeExprs($t), ctype); + $etype = new Tuple($testlist.start, actions.makeExprs($t), ctype); } | test[ctype] { $etype = (exprType)$test.tree; @@ -1426,7 +1122,7 @@ $classdef.tree = $stype; } :CLASS NAME (LPAREN testlist[expr_contextType.Load]? RPAREN)? COLON suite { - $stype = new ClassDef($CLASS, $NAME.getText(), makeBases($testlist.etype), makeStmts($suite.stmts)); + $stype = new ClassDef($CLASS, $NAME.getText(), actions.makeBases($testlist.etype), actions.makeStmts($suite.stmts)); } ; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-08-09 06:18:31
|
Revision: 5117 http://jython.svn.sourceforge.net/jython/?rev=5117&view=rev Author: pjenvey Date: 2008-08-09 06:18:29 +0000 (Sat, 09 Aug 2008) Log Message: ----------- fix test_weakref, re-enable 2.5isms Modified Paths: -------------- branches/asm/Lib/test/test_deque.py Modified: branches/asm/Lib/test/test_deque.py =================================================================== --- branches/asm/Lib/test/test_deque.py 2008-08-09 06:17:43 UTC (rev 5116) +++ branches/asm/Lib/test/test_deque.py 2008-08-09 06:18:29 UTC (rev 5117) @@ -475,6 +475,9 @@ p = proxy(d) self.assertEqual(str(p), str(d)) d = None + if test_support.is_jython: + from test_weakref import extra_collect() + extra_collect() self.assertRaises(ReferenceError, str, p) def test_strange_subclass(self): @@ -563,7 +566,7 @@ >>> def roundrobin(*iterables): -... pending = deque([iter(i) for i in iterables]) +... pending = deque(iter(i) for i in iterables) ... while pending: ... task = pending.popleft() ... try: @@ -612,13 +615,6 @@ TestSubclassWithKwargs, ) - if test_support.is_jython: - # Jython doesn't weakref things immediately - del TestSubclass.test_weakref - - # Needs 2.5 seq_tests - del TestVariousIteratorArgs.test_constructor - test_support.run_unittest(*test_classes) # verify reference counting This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-08-09 06:17:46
|
Revision: 5116 http://jython.svn.sourceforge.net/jython/?rev=5116&view=rev Author: pjenvey Date: 2008-08-09 06:17:43 +0000 (Sat, 09 Aug 2008) Log Message: ----------- o re-integrate list_tests workaround and use stock 2.5 seq_tests o fix list.index handling of long start values and slice assignment of strings o raise MemoryErrors as per seq_tests.test_bigrepeat Modified Paths: -------------- branches/asm/Lib/test/list_tests.py branches/asm/src/org/python/core/PyList.java branches/asm/src/org/python/core/PyTuple.java Removed Paths: ------------- branches/asm/Lib/test/seq_tests.py Modified: branches/asm/Lib/test/list_tests.py =================================================================== --- branches/asm/Lib/test/list_tests.py 2008-08-09 06:12:17 UTC (rev 5115) +++ branches/asm/Lib/test/list_tests.py 2008-08-09 06:17:43 UTC (rev 5116) @@ -461,7 +461,11 @@ u += "eggs" self.assertEqual(u, self.type2test("spameggs")) - self.assertRaises(TypeError, u.__iadd__, None) + if not test_support.is_jython: + self.assertRaises(TypeError, u.__iadd__, None) + else: + import operator + self.assertRaises(TypeError, operator.__iadd__, u, None) def test_imul(self): u = self.type2test([0, 1]) @@ -512,7 +516,8 @@ a[::2] = tuple(range(5)) self.assertEqual(a, self.type2test([0, 1, 1, 3, 2, 5, 3, 7, 4, 9])) - def test_constructor_exception_handling(self): + # XXX: CPython specific, PyList doesn't len() during init + def _test_constructor_exception_handling(self): # Bug #1242657 class F(object): def __iter__(self): Deleted: branches/asm/Lib/test/seq_tests.py =================================================================== --- branches/asm/Lib/test/seq_tests.py 2008-08-09 06:12:17 UTC (rev 5115) +++ branches/asm/Lib/test/seq_tests.py 2008-08-09 06:17:43 UTC (rev 5116) @@ -1,207 +0,0 @@ -# From Python 2.4.4 -""" -Tests common to tuple, list and UserList.UserList -""" - -import unittest -from test import test_support - -class CommonTest(unittest.TestCase): - # The type to be tested - type2test = None - - def test_constructors(self): - l0 = [] - l1 = [0] - l2 = [0, 1] - - u = self.type2test() - u0 = self.type2test(l0) - u1 = self.type2test(l1) - u2 = self.type2test(l2) - - uu = self.type2test(u) - uu0 = self.type2test(u0) - uu1 = self.type2test(u1) - uu2 = self.type2test(u2) - - v = self.type2test(tuple(u)) - class OtherSeq: - def __init__(self, initseq): - self.__data = initseq - def __len__(self): - return len(self.__data) - def __getitem__(self, i): - return self.__data[i] - s = OtherSeq(u0) - v0 = self.type2test(s) - self.assertEqual(len(v0), len(s)) - - s = "this is also a sequence" - vv = self.type2test(s) - self.assertEqual(len(vv), len(s)) - - def test_truth(self): - self.assert_(not self.type2test()) - self.assert_(self.type2test([42])) - - def test_getitem(self): - u = self.type2test([0, 1, 2, 3, 4]) - for i in xrange(len(u)): - self.assertEqual(u[i], i) - self.assertEqual(u[long(i)], i) - for i in xrange(-len(u), -1): - self.assertEqual(u[i], len(u)+i) - self.assertEqual(u[long(i)], len(u)+i) - self.assertRaises(IndexError, u.__getitem__, -len(u)-1) - self.assertRaises(IndexError, u.__getitem__, len(u)) - self.assertRaises(ValueError, u.__getitem__, slice(0,10,0)) - - u = self.type2test() - self.assertRaises(IndexError, u.__getitem__, 0) - self.assertRaises(IndexError, u.__getitem__, -1) - - self.assertRaises(TypeError, u.__getitem__) - - a = self.type2test([10, 11]) - self.assertEqual(a[0], 10) - self.assertEqual(a[1], 11) - self.assertEqual(a[-2], 10) - self.assertEqual(a[-1], 11) - self.assertRaises(IndexError, a.__getitem__, -3) - self.assertRaises(IndexError, a.__getitem__, 3) - - def test_getslice(self): - l = [0, 1, 2, 3, 4] - u = self.type2test(l) - - self.assertEqual(u[0:0], self.type2test()) - self.assertEqual(u[1:2], self.type2test([1])) - self.assertEqual(u[-2:-1], self.type2test([3])) - self.assertEqual(u[-1000:1000], u) - self.assertEqual(u[1000:-1000], self.type2test([])) - self.assertEqual(u[:], u) - self.assertEqual(u[1:None], self.type2test([1, 2, 3, 4])) - self.assertEqual(u[None:3], self.type2test([0, 1, 2])) - - # Extended slices - self.assertEqual(u[::], u) - self.assertEqual(u[::2], self.type2test([0, 2, 4])) - self.assertEqual(u[1::2], self.type2test([1, 3])) - self.assertEqual(u[::-1], self.type2test([4, 3, 2, 1, 0])) - self.assertEqual(u[::-2], self.type2test([4, 2, 0])) - self.assertEqual(u[3::-2], self.type2test([3, 1])) - self.assertEqual(u[3:3:-2], self.type2test([])) - self.assertEqual(u[3:2:-2], self.type2test([3])) - self.assertEqual(u[3:1:-2], self.type2test([3])) - self.assertEqual(u[3:0:-2], self.type2test([3, 1])) - self.assertEqual(u[::-100], self.type2test([4])) - self.assertEqual(u[100:-100:], self.type2test([])) - self.assertEqual(u[-100:100:], u) - self.assertEqual(u[100:-100:-1], u[::-1]) - self.assertEqual(u[-100:100:-1], self.type2test([])) - self.assertEqual(u[-100L:100L:2L], self.type2test([0, 2, 4])) - - # Test extreme cases with long ints - a = self.type2test([0,1,2,3,4]) - self.assertEqual(a[ -pow(2,128L): 3 ], self.type2test([0,1,2])) - self.assertEqual(a[ 3: pow(2,145L) ], self.type2test([3,4])) - - self.assertRaises(TypeError, u.__getslice__) - - def test_contains(self): - u = self.type2test([0, 1, 2]) - for i in u: - self.assert_(i in u) - for i in min(u)-1, max(u)+1: - self.assert_(i not in u) - - self.assertRaises(TypeError, u.__contains__) - - def test_len(self): - self.assertEqual(len(self.type2test()), 0) - self.assertEqual(len(self.type2test([])), 0) - self.assertEqual(len(self.type2test([0])), 1) - self.assertEqual(len(self.type2test([0, 1, 2])), 3) - - def test_minmax(self): - u = self.type2test([0, 1, 2]) - self.assertEqual(min(u), 0) - self.assertEqual(max(u), 2) - - def test_addmul(self): - u1 = self.type2test([0]) - u2 = self.type2test([0, 1]) - self.assertEqual(u1, u1 + self.type2test()) - self.assertEqual(u1, self.type2test() + u1) - self.assertEqual(u1 + self.type2test([1]), u2) - self.assertEqual(self.type2test([-1]) + u1, self.type2test([-1, 0])) - self.assertEqual(self.type2test(), u2*0) - self.assertEqual(self.type2test(), 0*u2) - self.assertEqual(self.type2test(), u2*0L) - self.assertEqual(self.type2test(), 0L*u2) - self.assertEqual(u2, u2*1) - self.assertEqual(u2, 1*u2) - self.assertEqual(u2, u2*1L) - self.assertEqual(u2, 1L*u2) - self.assertEqual(u2+u2, u2*2) - self.assertEqual(u2+u2, 2*u2) - self.assertEqual(u2+u2, u2*2L) - self.assertEqual(u2+u2, 2L*u2) - self.assertEqual(u2+u2+u2, u2*3) - self.assertEqual(u2+u2+u2, 3*u2) - - class subclass(self.type2test): - pass - u3 = subclass([0, 1]) - self.assertEqual(u3, u3*1) - self.assert_(u3 is not u3*1) - - def test_iadd(self): - u = self.type2test([0, 1]) - u += self.type2test() - self.assertEqual(u, self.type2test([0, 1])) - u += self.type2test([2, 3]) - self.assertEqual(u, self.type2test([0, 1, 2, 3])) - u += self.type2test([4, 5]) - self.assertEqual(u, self.type2test([0, 1, 2, 3, 4, 5])) - - u = self.type2test("spam") - u += self.type2test("eggs") - self.assertEqual(u, self.type2test("spameggs")) - - def test_imul(self): - u = self.type2test([0, 1]) - u *= 3 - self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1])) - - def test_getitemoverwriteiter(self): - # Verify that __getitem__ overrides are not recognized by __iter__ - class T(self.type2test): - def __getitem__(self, key): - return str(key) + '!!!' - self.assertEqual(iter(T((1,2))).next(), 1) - - def test_repeat(self): - for m in xrange(4): - s = tuple(range(m)) - for n in xrange(-3, 5): - self.assertEqual(self.type2test(s*n), self.type2test(s)*n) - self.assertEqual(self.type2test(s)*(-4), self.type2test([])) - self.assertEqual(id(s), id(s*1)) - - def test_subscript(self): - a = self.type2test([10, 11]) - self.assertEqual(a.__getitem__(0L), 10) - self.assertEqual(a.__getitem__(1L), 11) - self.assertEqual(a.__getitem__(-2L), 10) - self.assertEqual(a.__getitem__(-1L), 11) - self.assertRaises(IndexError, a.__getitem__, -3) - self.assertRaises(IndexError, a.__getitem__, 3) - self.assertEqual(a.__getitem__(slice(0,1)), self.type2test([10])) - self.assertEqual(a.__getitem__(slice(1,2)), self.type2test([11])) - self.assertEqual(a.__getitem__(slice(0,2)), self.type2test([10, 11])) - self.assertEqual(a.__getitem__(slice(0,3)), self.type2test([10, 11])) - self.assertEqual(a.__getitem__(slice(3,5)), self.type2test([])) - self.assertRaises(ValueError, a.__getitem__, slice(0, 10, 0)) - self.assertRaises(TypeError, a.__getitem__, 'x') Modified: branches/asm/src/org/python/core/PyList.java =================================================================== --- branches/asm/src/org/python/core/PyList.java 2008-08-09 06:12:17 UTC (rev 5115) +++ branches/asm/src/org/python/core/PyList.java 2008-08-09 06:17:43 UTC (rev 5116) @@ -153,20 +153,17 @@ if(step == 1) { PyObject[] otherArray; PyObject[] array = getArray(); - if(value instanceof PySequenceList) { - PySequenceList seqList = (PySequenceList) value; - otherArray = seqList.getArray(); - if(otherArray == array) { - otherArray = otherArray.clone(); - } - list.replaceSubArray(start, stop, otherArray, 0, seqList.size()); + + int n = value.__len__(); + if (value instanceof PySequenceList) { + otherArray = ((PySequenceList)value).getArray(); } else { - int n = value.__len__(); - list.ensureCapacity(start + n); - for(int i = 0; i < n; i++) { - list.add(i + start, value.pyget(i)); - } + otherArray = Py.unpackSequence(value, value.__len__()); } + if (otherArray == array) { + otherArray = otherArray.clone(); + } + list.replaceSubArray(start, stop, otherArray, 0, n); } else if(step > 1) { int n = value.__len__(); for(int i = 0, j = 0; i < n; i++, j += step) { @@ -213,15 +210,21 @@ } protected PyObject repeat(int count) { - if(count < 0) { + if (count < 0) { count = 0; } - int l = size(); - PyObject[] newList = new PyObject[l * count]; + int size = size(); + int newSize = size * count; + if (count != 0 && newSize / count != size) { + throw Py.MemoryError(""); + } + + PyObject[] array = getArray(); + PyObject[] newArray = new PyObject[newSize]; for(int i = 0; i < count; i++) { - System.arraycopy(getArray(), 0, newList, i * l, l); + System.arraycopy(array, 0, newArray, i * size, size); } - return new PyList(newList); + return new PyList(newArray); } @ExposedMethod(type = MethodType.BINARY) @@ -264,16 +267,29 @@ return null; } int count = o.asIndex(Py.OverflowError); - int l = size(); - - int newSize = l * count; + + int size = size(); + if (size == 0 || count == 1) { + return this; + } + + if (count < 1) { + clear(); + return this; + } + + if (size > Integer.MAX_VALUE / count) { + throw Py.MemoryError(""); + } + + int newSize = size * count; list.setSize(newSize); PyObject[] array = getArray(); for (int i = 1; i < count; i++) { - System.arraycopy(array, 0, array, i * l, l); + System.arraycopy(array, 0, array, i * size, size); } gListAllocatedStatus = __len__(); - return this; + return this; } @Override @@ -486,24 +502,24 @@ } public int index(PyObject o, int start) { - return list_index(o, start, null); + return list_index(o, start, size()); } public int index(PyObject o, int start, int stop) { - return list_index(o, start, Py.newInteger(stop)); + return list_index(o, start, stop); } - @ExposedMethod(defaults = {"0", "null"}) - final int list_index(PyObject o, int start, PyObject stop) { - int iStop; - if(stop == null) { - iStop = size(); - } else { - iStop = stop.asInt(); - } - return _index(o, "list.index(x): x not in list", start, iStop); + @ExposedMethod(defaults = {"null", "null"}) + final int list_index(PyObject o, PyObject start, PyObject stop) { + int startInt = start == null ? 0 : PySlice.calculateSliceIndex(start); + int stopInt = stop == null ? size() : PySlice.calculateSliceIndex(stop); + return list_index(o, startInt, stopInt); } + final int list_index(PyObject o, int start, int stop) { + return _index(o, "list.index(x): x not in list", start, stop); + } + final int list_index(PyObject o, int start) { return _index(o, "list.index(x): x not in list", start, size()); } Modified: branches/asm/src/org/python/core/PyTuple.java =================================================================== --- branches/asm/src/org/python/core/PyTuple.java 2008-08-09 06:12:17 UTC (rev 5115) +++ branches/asm/src/org/python/core/PyTuple.java 2008-08-09 06:17:43 UTC (rev 5116) @@ -92,20 +92,26 @@ if (count < 0) { count = 0; } - if (size() == 0 || count == 1) { + int size = size(); + if (size == 0 || count == 1) { if (getType() == TYPE) { // Since tuples are immutable, we can return a shared copy in this case return this; } - if (size() == 0) { + if (size == 0) { return new PyTuple(); } } + + int newSize = size * count; + if (newSize / size != count) { + throw Py.MemoryError(""); + } + PyObject[] array = getArray(); - int l = size(); - PyObject[] newArray = new PyObject[l*count]; - for (int i=0; i<count; i++) { - System.arraycopy(array, 0, newArray, i*l, l); + PyObject[] newArray = new PyObject[newSize]; + for (int i = 0; i < count; i++) { + System.arraycopy(array, 0, newArray, i * size, size); } return new PyTuple(newArray); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-08-09 06:12:29
|
Revision: 5115 http://jython.svn.sourceforge.net/jython/?rev=5115&view=rev Author: pjenvey Date: 2008-08-09 06:12:17 +0000 (Sat, 09 Aug 2008) Log Message: ----------- from: http://svn.python.org/projects/python/branches/release25-maint/Lib/test/list_tests.py@52138 Modified Paths: -------------- branches/asm/Lib/test/list_tests.py Modified: branches/asm/Lib/test/list_tests.py =================================================================== --- branches/asm/Lib/test/list_tests.py 2008-08-09 05:36:17 UTC (rev 5114) +++ branches/asm/Lib/test/list_tests.py 2008-08-09 06:12:17 UTC (rev 5115) @@ -1,4 +1,3 @@ -# From Python 2.4.4 """ Tests common to list and UserList.UserList """ @@ -67,8 +66,7 @@ a = self.type2test(range(20)) self.assertRaises(ValueError, a.__setitem__, slice(0, 10, 0), [1,2,3]) self.assertRaises(TypeError, a.__setitem__, slice(0, 10), 1) - # XXX: Need Slice bounds checking (equiv to CPython's PySlice_GetIndicesEx) - #self.assertRaises(ValueError, a.__setitem__, slice(0, 10, 2), [1,2]) + self.assertRaises(ValueError, a.__setitem__, slice(0, 10, 2), [1,2]) self.assertRaises(TypeError, a.__getitem__, 'x', 1) a[slice(2,10,3)] = [1,2,3] self.assertEqual(a, self.type2test([0, 1, 1, 3, 4, 2, 6, 7, 3, @@ -271,8 +269,6 @@ self.assertRaises(TypeError, a.insert) def test_pop(self): - # XXX: no decimal module in 2.3 - #from decimal import Decimal a = self.type2test([-1, 0, 1]) a.pop() self.assertEqual(a, [-1, 0]) @@ -283,9 +279,7 @@ self.assertEqual(a, []) self.assertRaises(IndexError, a.pop) self.assertRaises(TypeError, a.pop, 42, 42) - #a = self.type2test([0, 10, 20, 30, 40]) - #self.assertEqual(a.pop(Decimal(2)), 20) - #self.assertRaises(IndexError, a.pop, Decimal(25)) + a = self.type2test([0, 10, 20, 30, 40]) def test_remove(self): a = self.type2test([0, 0, 1]) @@ -312,6 +306,26 @@ a = self.type2test([0, 1, 2, 3]) self.assertRaises(BadExc, a.remove, BadCmp()) + class BadCmp2: + def __eq__(self, other): + raise BadExc() + + d = self.type2test('abcdefghcij') + d.remove('c') + self.assertEqual(d, self.type2test('abdefghcij')) + d.remove('c') + self.assertEqual(d, self.type2test('abdefghij')) + self.assertRaises(ValueError, d.remove, 'c') + self.assertEqual(d, self.type2test('abdefghij')) + + # Handle comparison errors + d = self.type2test(['a', 'b', BadCmp2(), 'c']) + e = self.type2test(d) + self.assertRaises(BadExc, d.remove, 'c') + for x, y in zip(d, e): + # verify that original order and values are retained. + self.assert_(x is y) + def test_count(self): a = self.type2test([0, 1, 2])*3 self.assertEqual(a.count(0), 3) @@ -369,10 +383,8 @@ self.assertEqual(a.index(0, -3), 3) self.assertEqual(a.index(0, 3, 4), 3) self.assertEqual(a.index(0, -3, -2), 3) - # XXX: CPython takes start/stop as objects, and does bounds checking - # via _PyEval_SliceIndex - #self.assertEqual(a.index(0, -4*sys.maxint, 4*sys.maxint), 2) - #self.assertRaises(ValueError, a.index, 0, 4*sys.maxint,-4*sys.maxint) + self.assertEqual(a.index(0, -4*sys.maxint, 4*sys.maxint), 2) + self.assertRaises(ValueError, a.index, 0, 4*sys.maxint,-4*sys.maxint) self.assertRaises(ValueError, a.index, 2, 0, -10) a.remove(0) self.assertRaises(ValueError, a.index, 2, 0, 4) @@ -433,8 +445,7 @@ self.assertRaises(TypeError, z.sort, 42, 42, 42, 42) - # XXX: there are currently some slice bugs in Jython - def _test_slice(self): + def test_slice(self): u = self.type2test("spam") u[:2] = "h" self.assertEqual(u, list("ham")) @@ -450,13 +461,7 @@ u += "eggs" self.assertEqual(u, self.type2test("spameggs")) - # CPython specific; Jython/pypy test via the operator module - # instead - if not test_support.is_jython: - self.assertRaises(TypeError, u.__iadd__, None) - else: - import operator - self.assertRaises(TypeError, operator.__iadd__, u, None) + self.assertRaises(TypeError, u.__iadd__, None) def test_imul(self): u = self.type2test([0, 1]) @@ -507,8 +512,7 @@ a[::2] = tuple(range(5)) self.assertEqual(a, self.type2test([0, 1, 1, 3, 2, 5, 3, 7, 4, 9])) - # XXX: CPython specific, PyList doesn't len() during init - def _test_constructor_exception_handling(self): + def test_constructor_exception_handling(self): # Bug #1242657 class F(object): def __iter__(self): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-08-09 05:36:19
|
Revision: 5114 http://jython.svn.sourceforge.net/jython/?rev=5114&view=rev Author: zyasoft Date: 2008-08-09 05:36:17 +0000 (Sat, 09 Aug 2008) Log Message: ----------- Reordered code to test for raising exceptions thrown into a generator, fixing test cases in test_contextlib, test_generators, and test_with. Modified Paths: -------------- branches/asm/src/org/python/compiler/ClassFile.java branches/asm/src/org/python/compiler/CodeCompiler.java branches/asm/src/org/python/compiler/Module.java Modified: branches/asm/src/org/python/compiler/ClassFile.java =================================================================== --- branches/asm/src/org/python/compiler/ClassFile.java 2008-08-09 00:37:11 UTC (rev 5113) +++ branches/asm/src/org/python/compiler/ClassFile.java 2008-08-09 05:36:17 UTC (rev 5114) @@ -48,9 +48,7 @@ this.interfaces = new String[0]; this.access = access; - //XXX: can we do better than ASM for computing MAXS? cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); - methodVisitors = Collections.synchronizedList(new ArrayList()); fieldVisitors = Collections.synchronizedList(new ArrayList()); } Modified: branches/asm/src/org/python/compiler/CodeCompiler.java =================================================================== --- branches/asm/src/org/python/compiler/CodeCompiler.java 2008-08-09 00:37:11 UTC (rev 5113) +++ branches/asm/src/org/python/compiler/CodeCompiler.java 2008-08-09 05:36:17 UTC (rev 5114) @@ -552,7 +552,15 @@ throw new ParseException("'yield' outside function", node); } - saveLocals(); + loadFrame(); + code.invokevirtual("org/python/core/PyFrame", "getGeneratorInput", "()" + $obj); + code.dup(); + code.instanceof_("org/python/core/PyException"); + Label done = new Label(); + code.ifeq(done); + code.checkcast("java/lang/Throwable"); + code.athrow(); + code.label(done); if (node.value != null) { visit(node.value); @@ -560,6 +568,7 @@ getNone(); } setLastI(++yield_count); + saveLocals(); code.areturn(); Label restart = new Label(); @@ -568,10 +577,10 @@ restoreLocals(); loadFrame(); - code.invokevirtual("org/python/core/PyFrame", "getGeneratorInput", "()" + $obj); + code.invokevirtual("org/python/core/PyFrame", "checkGeneratorInput", "()" + $obj); code.dup(); code.instanceof_("org/python/core/PyException"); - Label done = new Label(); + done = new Label(); code.ifeq(done); code.checkcast("java/lang/Throwable"); code.athrow(); Modified: branches/asm/src/org/python/compiler/Module.java =================================================================== --- branches/asm/src/org/python/compiler/Module.java 2008-08-09 00:37:11 UTC (rev 5113) +++ branches/asm/src/org/python/compiler/Module.java 2008-08-09 05:36:17 UTC (rev 5114) @@ -437,19 +437,7 @@ if (scope.generator) { c.label(genswitch); - // throw any exception that was sent into this generator c.aload(1); - c.invokevirtual("org/python/core/PyFrame", "checkGeneratorInput", "()" + $obj); - c.dup(); - c.instanceof_("org/python/core/PyException"); - Label done = new Label(); - c.ifeq(done); - c.checkcast("java/lang/Throwable"); - c.athrow(); - c.label(done); - - c.pop(); - c.aload(1); c.getfield("org/python/core/PyFrame", "f_lasti", "I"); Label[] yields = new Label[compiler.yields.size()+1]; @@ -458,7 +446,6 @@ yields[i] = (Label) compiler.yields.elementAt(i-1); } c.tableswitch(0, yields.length - 1, start, yields); - // XXX: Generate an error } // !classdef only This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-08-09 00:37:14
|
Revision: 5113 http://jython.svn.sourceforge.net/jython/?rev=5113&view=rev Author: pjenvey Date: 2008-08-09 00:37:11 +0000 (Sat, 09 Aug 2008) Log Message: ----------- allow subclasses in getPyObjectByType Modified Paths: -------------- branches/asm/src/org/python/core/ArgParser.java Modified: branches/asm/src/org/python/core/ArgParser.java =================================================================== --- branches/asm/src/org/python/core/ArgParser.java 2008-08-08 23:54:06 UTC (rev 5112) +++ branches/asm/src/org/python/core/ArgParser.java 2008-08-09 00:37:11 UTC (rev 5113) @@ -205,7 +205,7 @@ */ public PyObject getPyObjectByType(int pos, PyType type) { PyObject arg = getRequiredArg(pos); - if (arg.getType() != type) { + if (!Py.isInstance(arg, type)) { throw Py.TypeError(String.format("argument %d must be %s, not %s", pos + 1, type.fastGetName(), arg.getType().fastGetName())); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-08 23:54:10
|
Revision: 5112 http://jython.svn.sourceforge.net/jython/?rev=5112&view=rev Author: fwierzbicki Date: 2008-08-08 23:54:06 +0000 (Fri, 08 Aug 2008) Log Message: ----------- New error nodes for tool ASTs. Added errorhandling methods to ErrorHandler. Modified Paths: -------------- branches/asm/grammar/Python.g branches/asm/grammar/PythonWalker.g branches/asm/src/org/python/antlr/ErrorHandler.java branches/asm/src/org/python/antlr/FailFastHandler.java branches/asm/src/org/python/antlr/GrammarActions.java branches/asm/src/org/python/antlr/ListErrorHandler.java Added Paths: ----------- branches/asm/src/org/python/antlr/ast/ErrorExpr.java branches/asm/src/org/python/antlr/ast/ErrorMod.java branches/asm/src/org/python/antlr/ast/ErrorSlice.java Modified: branches/asm/grammar/Python.g =================================================================== --- branches/asm/grammar/Python.g 2008-08-08 22:07:24 UTC (rev 5111) +++ branches/asm/grammar/Python.g 2008-08-08 23:54:06 UTC (rev 5112) @@ -192,6 +192,7 @@ public void setErrorHandler(ErrorHandler eh) { this.errorHandler = eh; + actions.setErrorHandler(eh); } private void debug(String message) { Modified: branches/asm/grammar/PythonWalker.g =================================================================== --- branches/asm/grammar/PythonWalker.g 2008-08-08 22:07:24 UTC (rev 5111) +++ branches/asm/grammar/PythonWalker.g 2008-08-08 23:54:06 UTC (rev 5112) @@ -95,6 +95,7 @@ public void setErrorHandler(ErrorHandler eh) { this.errorHandler = eh; + actions.setErrorHandler(eh); } public void debug(String message) { @@ -225,7 +226,7 @@ if ($Call == null) { decs.add($dotted_attr.etype); } else { - Call c; + exprType c; if ($Args == null) { c = actions.makeCall($Call, $dotted_attr.etype); } else { @@ -344,7 +345,7 @@ call_expr returns [exprType etype, PythonTree marker] : ^(Call (^(Args arglist))? test[expr_contextType.Load]) { - Call c; + exprType c; if ($Args == null) { c = actions.makeCall($test.marker, $test.etype); } else { @@ -676,7 +677,7 @@ if ($ORELSE != null) { o = $orelse.stypes; } - While w = actions.makeWhile($WHILE, $test.etype, $body.stypes, o); + stmtType w = actions.makeWhile($WHILE, $test.etype, $body.stypes, o); $stmts::statements.add(w); } ; @@ -687,7 +688,7 @@ if ($ORELSE != null) { o = $orelse.stypes; } - For f = actions.makeFor($FOR, $targ.etype, $iter.etype, $body.stypes, o); + stmtType f = actions.makeFor($FOR, $targ.etype, $iter.etype, $body.stypes, o); $stmts::statements.add(f); } ; Modified: branches/asm/src/org/python/antlr/ErrorHandler.java =================================================================== --- branches/asm/src/org/python/antlr/ErrorHandler.java 2008-08-08 22:07:24 UTC (rev 5111) +++ branches/asm/src/org/python/antlr/ErrorHandler.java 2008-08-08 23:54:06 UTC (rev 5112) @@ -4,10 +4,20 @@ import org.antlr.runtime.IntStream; import org.antlr.runtime.Lexer; import org.antlr.runtime.RecognitionException; +import org.python.antlr.ast.exprType; +import org.python.antlr.ast.modType; +import org.python.antlr.ast.sliceType; +import org.python.antlr.ast.stmtType; interface ErrorHandler { void reportError(BaseRecognizer br, RecognitionException re); void recover(BaseRecognizer br, IntStream input, RecognitionException re); void recover(Lexer lex, RecognitionException re); boolean isRecoverable(); + //exprType, modType, sliceType, stmtType + exprType errorExpr(PythonTree t); + modType errorMod(PythonTree t); + sliceType errorSlice(PythonTree t); + stmtType errorStmt(PythonTree t); + } Modified: branches/asm/src/org/python/antlr/FailFastHandler.java =================================================================== --- branches/asm/src/org/python/antlr/FailFastHandler.java 2008-08-08 22:07:24 UTC (rev 5111) +++ branches/asm/src/org/python/antlr/FailFastHandler.java 2008-08-08 23:54:06 UTC (rev 5112) @@ -4,6 +4,14 @@ import org.antlr.runtime.IntStream; import org.antlr.runtime.Lexer; import org.antlr.runtime.RecognitionException; +import org.python.antlr.ast.ErrorMod; +import org.python.antlr.ast.exprType; +import org.python.antlr.ast.ErrorExpr; +import org.python.antlr.ast.ErrorSlice; +import org.python.antlr.ast.ErrorStmt; +import org.python.antlr.ast.modType; +import org.python.antlr.ast.sliceType; +import org.python.antlr.ast.stmtType; public class FailFastHandler implements ErrorHandler { @@ -23,9 +31,26 @@ return false; } + public exprType errorExpr(PythonTree t) { + throw new ParseException("Bad Expr Node", t); + } + + public modType errorMod(PythonTree t) { + throw new ParseException("Bad Mod Node", t); + } + + public sliceType errorSlice(PythonTree t) { + throw new ParseException("Bad Slice Node", t); + } + + public stmtType errorStmt(PythonTree t) { + throw new ParseException("Bad Stmt Node", t); + } + private String message(BaseRecognizer br, RecognitionException re) { String hdr = br.getErrorHeader(re); String msg = br.getErrorMessage(re, br.getTokenNames()); return hdr+" "+msg; } + } Modified: branches/asm/src/org/python/antlr/GrammarActions.java =================================================================== --- branches/asm/src/org/python/antlr/GrammarActions.java 2008-08-08 22:07:24 UTC (rev 5111) +++ branches/asm/src/org/python/antlr/GrammarActions.java 2008-08-08 23:54:06 UTC (rev 5112) @@ -84,9 +84,14 @@ import java.util.Set; public class GrammarActions { + private ErrorHandler errorHandler = null; + public GrammarActions() { } + public void setErrorHandler(ErrorHandler eh) { + this.errorHandler = eh; + } void debug(String x) { if (false) { System.out.println(x); @@ -135,11 +140,9 @@ } stmtType makeFunctionDef(PythonTree t, PythonTree nameToken, argumentsType args, List funcStatements, List decorators) { - /* - if (nameToken == null || funcStatements == null || decorators == null) { - return new ErrorStmt(t); + if (nameToken == null) { + return errorHandler.errorStmt(t); } - */ argumentsType a; debug("Matched FunctionDef"); if (args != null) { @@ -322,11 +325,9 @@ } stmtType makeClassDef(PythonTree t, PythonTree nameToken, List bases, List body) { - /* - if (nameToken == null || bases == null || body == null) { - return new ErrorStmt(t); + if (nameToken == null) { + return errorHandler.errorStmt(t); } - */ exprType[] b = (exprType[])bases.toArray(new exprType[bases.size()]); stmtType[] s = (stmtType[])body.toArray(new stmtType[body.size()]); return new ClassDef(t, nameToken.getText(), b, s); @@ -378,7 +379,10 @@ return new TryFinally(t, b, f); } - If makeIf(PythonTree t, exprType test, List body, List orelse) { + stmtType makeIf(PythonTree t, exprType test, List body, List orelse) { + if (test == null) { + return errorHandler.errorStmt(t); + } stmtType[] o; if (orelse != null) { o = (stmtType[])orelse.toArray(new stmtType[orelse.size()]); @@ -395,7 +399,10 @@ } - While makeWhile(PythonTree t, exprType test, List body, List orelse) { + stmtType makeWhile(PythonTree t, exprType test, List body, List orelse) { + if (test == null) { + return errorHandler.errorStmt(t); + } stmtType[] o; if (orelse != null) { o = (stmtType[])orelse.toArray(new stmtType[orelse.size()]); @@ -406,7 +413,10 @@ return new While(t, test, b, o); } - For makeFor(PythonTree t, exprType target, exprType iter, List body, List orelse) { + stmtType makeFor(PythonTree t, exprType target, exprType iter, List body, List orelse) { + if (target == null || iter == null) { + return errorHandler.errorStmt(t); + } stmtType[] o; if (orelse != null) { o = (stmtType[])orelse.toArray(new stmtType[orelse.size()]); @@ -417,11 +427,14 @@ return new For(t, target, iter, b, o); } - Call makeCall(PythonTree t, exprType func) { + exprType makeCall(PythonTree t, exprType func) { return makeCall(t, func, null, null, null, null); } - Call makeCall(PythonTree t, exprType func, List args, List keywords, exprType starargs, exprType kwargs) { + exprType makeCall(PythonTree t, exprType func, List args, List keywords, exprType starargs, exprType kwargs) { + if (func == null) { + return errorHandler.errorExpr(t); + } exprType[] a; keywordType[] k; if (args == null) { Modified: branches/asm/src/org/python/antlr/ListErrorHandler.java =================================================================== --- branches/asm/src/org/python/antlr/ListErrorHandler.java 2008-08-08 22:07:24 UTC (rev 5111) +++ branches/asm/src/org/python/antlr/ListErrorHandler.java 2008-08-08 23:54:06 UTC (rev 5112) @@ -4,6 +4,14 @@ import org.antlr.runtime.IntStream; import org.antlr.runtime.Lexer; import org.antlr.runtime.RecognitionException; +import org.python.antlr.ast.ErrorMod; +import org.python.antlr.ast.exprType; +import org.python.antlr.ast.ErrorExpr; +import org.python.antlr.ast.ErrorSlice; +import org.python.antlr.ast.ErrorStmt; +import org.python.antlr.ast.modType; +import org.python.antlr.ast.sliceType; +import org.python.antlr.ast.stmtType; public class ListErrorHandler implements ErrorHandler { @@ -23,4 +31,20 @@ return true; } + public exprType errorExpr(PythonTree t) { + return new ErrorExpr(t); + } + + public modType errorMod(PythonTree t) { + return new ErrorMod(t); + } + + public sliceType errorSlice(PythonTree t) { + return new ErrorSlice(t); + } + + public stmtType errorStmt(PythonTree t) { + return new ErrorStmt(t); + } + } Added: branches/asm/src/org/python/antlr/ast/ErrorExpr.java =================================================================== --- branches/asm/src/org/python/antlr/ast/ErrorExpr.java (rev 0) +++ branches/asm/src/org/python/antlr/ast/ErrorExpr.java 2008-08-08 23:54:06 UTC (rev 5112) @@ -0,0 +1,28 @@ +package org.python.antlr.ast; +import org.python.antlr.PythonTree; + +public class ErrorExpr extends exprType { + + public static final String[] _fields = new String[] {}; + + public ErrorExpr(PythonTree tree) { + super(tree); + } + + public String toString() { + return "ErrorExpr"; + } + + public String toStringTree() { + return "ErrorExpr"; + } + + public int getLineno() { + return getLine(); + } + + public int getCol_offset() { + return getCharPositionInLine(); + } + +} Added: branches/asm/src/org/python/antlr/ast/ErrorMod.java =================================================================== --- branches/asm/src/org/python/antlr/ast/ErrorMod.java (rev 0) +++ branches/asm/src/org/python/antlr/ast/ErrorMod.java 2008-08-08 23:54:06 UTC (rev 5112) @@ -0,0 +1,28 @@ +package org.python.antlr.ast; +import org.python.antlr.PythonTree; + +public class ErrorMod extends modType { + + public static final String[] _fields = new String[] {}; + + public ErrorMod(PythonTree tree) { + super(tree); + } + + public String toString() { + return "ErrorMod"; + } + + public String toStringTree() { + return "ErrorMod"; + } + + public int getLineno() { + return getLine(); + } + + public int getCol_offset() { + return getCharPositionInLine(); + } + +} Added: branches/asm/src/org/python/antlr/ast/ErrorSlice.java =================================================================== --- branches/asm/src/org/python/antlr/ast/ErrorSlice.java (rev 0) +++ branches/asm/src/org/python/antlr/ast/ErrorSlice.java 2008-08-08 23:54:06 UTC (rev 5112) @@ -0,0 +1,32 @@ +package org.python.antlr.ast; +import org.python.antlr.PythonTree; +import org.antlr.runtime.CommonToken; +import org.antlr.runtime.Token; +import java.io.DataOutputStream; +import java.io.IOException; + +public class ErrorSlice extends sliceType { + + public static final String[] _fields = new String[] {}; + + public ErrorSlice(PythonTree tree) { + super(tree); + } + + public String toString() { + return "ErrorSlice"; + } + + public String toStringTree() { + return "ErrorSlice"; + } + + public int getLineno() { + return getLine(); + } + + public int getCol_offset() { + return getCharPositionInLine(); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-08-08 22:07:27
|
Revision: 5111 http://jython.svn.sourceforge.net/jython/?rev=5111&view=rev Author: pjenvey Date: 2008-08-08 22:07:24 +0000 (Fri, 08 Aug 2008) Log Message: ----------- o another fix to make 'currentThread() is currentThread()' from thread.start_new_thread threads. use a WeakHashMap because weakrefs of javainstances ref the wrapper, not the actual java object o custom toString for FunctionThread Modified Paths: -------------- branches/asm/Lib/threading.py branches/asm/src/org/python/core/FunctionThread.java Modified: branches/asm/Lib/threading.py =================================================================== --- branches/asm/Lib/threading.py 2008-08-08 19:30:38 UTC (rev 5110) +++ branches/asm/Lib/threading.py 2008-08-08 22:07:24 UTC (rev 5111) @@ -1,3 +1,4 @@ +from java.util import Collections, WeakHashMap from java.util.concurrent import Semaphore, CyclicBarrier from java.util.concurrent.locks import ReentrantLock from thread import _newFunctionThread @@ -160,6 +161,8 @@ class JavaThread(object): def __init__(self, thread): self._thread = thread + _jthread_to_pythread[thread] = self + _threads[thread.getId()] = self def __repr__(self): _thread = self._thread @@ -209,10 +212,12 @@ # relies on the fact that this is a CHM _threads = weakref.WeakValueDictionary() _active = _threads -_jthread_to_pythread = weakref.WeakKeyDictionary() +_jthread_to_pythread = Collections.synchronizedMap(WeakHashMap()) class Thread(JavaThread): def __init__(self, group=None, target=None, name=None, args=None, kwargs=None): + _thread = self._create_thread() + JavaThread.__init__(self, _thread) if args is None: args = () if kwargs is None: @@ -220,11 +225,8 @@ self._target = target self._args = args self._kwargs = kwargs - self._thread = _thread = self._create_thread() if name: self._thread.setName(name) - _jthread_to_pythread[_thread] = self - _threads[_thread.getId()] = self def _create_thread(self): return _newFunctionThread(self.__bootstrap, ()) @@ -314,10 +316,11 @@ return None def currentThread(): - try: - return _jthread_to_pythread[java.lang.Thread.currentThread()] - except KeyError: - return JavaThread(java.lang.Thread.currentThread()) + jthread = java.lang.Thread.currentThread() + pythread = _jthread_to_pythread[jthread] + if pythread is None: + pythread = JavaThread(jthread) + return pythread def activeCount(): return len(_threads) Modified: branches/asm/src/org/python/core/FunctionThread.java =================================================================== --- branches/asm/src/org/python/core/FunctionThread.java 2008-08-08 19:30:38 UTC (rev 5110) +++ branches/asm/src/org/python/core/FunctionThread.java 2008-08-08 22:07:24 UTC (rev 5111) @@ -28,4 +28,15 @@ Py.printException(exc); } } + + @Override + public String toString() { + ThreadGroup group = getThreadGroup(); + if (group != null) { + return String.format("FunctionThread[%s,%s,%s]", getName(), getPriority(), + group.getName()); + } else { + return String.format("FunctionThread[%s,%s]", getName(), getPriority()); + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-08 19:30:44
|
Revision: 5110 http://jython.svn.sourceforge.net/jython/?rev=5110&view=rev Author: fwierzbicki Date: 2008-08-08 19:30:38 +0000 (Fri, 08 Aug 2008) Log Message: ----------- Extracted the actions from the grammars into a single GrammarActions file. This will help if I ever get far enough on integrating the two grammars into one. Also started an ErrorStmt node to handle bogus statements in the walker. Modified Paths: -------------- branches/asm/grammar/Python.g branches/asm/grammar/PythonWalker.g Added Paths: ----------- branches/asm/src/org/python/antlr/GrammarActions.java branches/asm/src/org/python/antlr/ast/ErrorStmt.java Modified: branches/asm/grammar/Python.g =================================================================== --- branches/asm/grammar/Python.g 2008-08-08 19:24:38 UTC (rev 5109) +++ branches/asm/grammar/Python.g 2008-08-08 19:30:38 UTC (rev 5110) @@ -188,6 +188,8 @@ private boolean seenSingleOuterSuite = false; + private GrammarActions actions = new GrammarActions(); + public void setErrorHandler(ErrorHandler eh) { this.errorHandler = eh; } @@ -198,205 +200,6 @@ } } - private void throwGenExpNotSoleArg(PythonTree t) { - throw new ParseException("Generator expression must be parenthesized if not sole argument", t); - } - - private exprType[] makeExprs(List exprs) { - return makeExprs(exprs, 0); - } - - private exprType[] makeExprs(List exprs, int start) { - if (exprs != null) { - List<exprType> result = new ArrayList<exprType>(); - for (int i=start; i<exprs.size(); i++) { - exprType e = (exprType)exprs.get(i); - result.add(e); - } - return (exprType[])result.toArray(new exprType[result.size()]); - } - return new exprType[0]; - } - - private stmtType[] makeStmts(List stmts) { - if (stmts != null) { - List<stmtType> result = new ArrayList<stmtType>(); - for (int i=0; i<stmts.size(); i++) { - result.add((stmtType)stmts.get(i)); - } - return (stmtType[])result.toArray(new stmtType[result.size()]); - } - return new stmtType[0]; - } - - private exprType makeDottedAttr(Token nameToken, List attrs) { - exprType current = new Name(nameToken, nameToken.getText(), expr_contextType.Load); - for (int i=attrs.size() - 1; i > -1; i--) { - Token t = ((PythonTree)attrs.get(i)).token; - current = new Attribute(t, current, t.getText(), - expr_contextType.Load); - } - return current; - } - - private FunctionDef makeFunctionDef(PythonTree t, PythonTree nameToken, argumentsType args, List funcStatements, List decorators) { - argumentsType a; - debug("Matched FunctionDef"); - if (args != null) { - a = args; - } else { - a = new argumentsType(t, new exprType[0], null, null, new exprType[0]); - } - stmtType[] s = (stmtType[])funcStatements.toArray(new stmtType[funcStatements.size()]); - exprType[] d; - if (decorators != null) { - d = (exprType[])decorators.toArray(new exprType[decorators.size()]); - } else { - d = new exprType[0]; - } - return new FunctionDef(t, nameToken.getText(), a, s, d); - } - - private argumentsType makeArgumentsType(Token t, List params, Token snameToken, - Token knameToken, List defaults) { - debug("Matched Arguments"); - - exprType[] p = (exprType[])params.toArray(new exprType[params.size()]); - exprType[] d = (exprType[])defaults.toArray(new exprType[defaults.size()]); - String s; - String k; - if (snameToken == null) { - s = null; - } else { - s = snameToken.getText(); - } - if (knameToken == null) { - k = null; - } else { - k = knameToken.getText(); - } - return new argumentsType(t, p, s, k, d); - } - - - - Object makeFloat(Token t) { - debug("makeFloat matched " + t.getText()); - return Py.newFloat(Double.valueOf(t.getText())); - } - - Object makeComplex(Token t) { - String s = t.getText(); - s = s.substring(0, s.length() - 1); - return Py.newImaginary(Double.valueOf(s)); - } - - Object makeInt(Token t) { - debug("Num matched " + t.getText()); - String s = t.getText(); - int radix = 10; - if (s.startsWith("0x") || s.startsWith("0X")) { - radix = 16; - s = s.substring(2, s.length()); - } else if (s.startsWith("0")) { - radix = 8; - } - if (s.endsWith("L") || s.endsWith("l")) { - s = s.substring(0, s.length()-1); - return Py.newLong(new BigInteger(s, radix)); - } - int ndigits = s.length(); - int i=0; - while (i < ndigits && s.charAt(i) == '0') - i++; - if ((ndigits - i) > 11) { - return Py.newLong(new BigInteger(s, radix)); - } - - long l = Long.valueOf(s, radix).longValue(); - if (l > 0xffffffffl || (l > Integer.MAX_VALUE)) { - return Py.newLong(new BigInteger(s, radix)); - } - return Py.newInteger((int) l); - } - - class StringPair { - private String s; - private boolean unicode; - - StringPair(String s, boolean unicode) { - this.s = s; - this.unicode = unicode; - } - String getString() { - return s; - } - - boolean isUnicode() { - return unicode; - } - } - - PyString extractStrings(List s) { - boolean ustring = false; - Token last = null; - StringBuffer sb = new StringBuffer(); - Iterator iter = s.iterator(); - while (iter.hasNext()) { - last = (Token)iter.next(); - StringPair sp = extractString(last); - if (sp.isUnicode()) { - ustring = true; - } - sb.append(sp.getString()); - } - if (ustring) { - return new PyUnicode(sb.toString()); - } - return new PyString(sb.toString()); - } - - StringPair extractString(Token t) { - String s = t.getText(); - char quoteChar = s.charAt(0); - int start=0; - boolean ustring = false; - if (quoteChar == 'u' || quoteChar == 'U') { - ustring = true; - start++; - } - quoteChar = s.charAt(start); - boolean raw = false; - if (quoteChar == 'r' || quoteChar == 'R') { - raw = true; - start++; - } - int quotes = 3; - if (s.length() - start == 2) { - quotes = 1; - } - if (s.charAt(start) != s.charAt(start+1)) { - quotes = 1; - } - - if (raw) { - return new StringPair(s.substring(quotes+start, s.length()-quotes), ustring); - } else { - StringBuffer sb = new StringBuffer(s.length()); - char[] ca = s.toCharArray(); - int n = ca.length-quotes; - int i=quotes+start; - int last_i=i; - return new StringPair(PyString.decode_UnicodeEscape(s, i, n, "strict", ustring), ustring); - //return decode_UnicodeEscape(s, i, n, "strict", ustring); - } - } - - Token extractStringToken(List s) { - return (Token)s.get(s.size() - 1); - } - - protected void mismatch(IntStream input, int ttype, BitSet follow) throws RecognitionException { if (errorHandler.isRecoverable()) { super.mismatch(input, ttype, follow); @@ -989,12 +792,12 @@ | LCURLY (dictmaker)? RCURLY -> ^(Dict LCURLY ^(Elts dictmaker)?) | BACKQUOTE testlist[expr_contextType.Load] BACKQUOTE -> ^(Repr BACKQUOTE testlist) | NAME -> ^(NameTok NAME) - | INT -> ^(NumTok<Num>[$INT, makeInt($INT)]) - | LONGINT -> ^(NumTok<Num>[$LONGINT, makeInt($LONGINT)]) - | FLOAT -> ^(NumTok<Num>[$FLOAT, makeFloat($FLOAT)]) - | COMPLEX -> ^(NumTok<Num>[$COMPLEX, makeComplex($COMPLEX)]) + | INT -> ^(NumTok<Num>[$INT, actions.makeInt($INT)]) + | LONGINT -> ^(NumTok<Num>[$LONGINT, actions.makeInt($LONGINT)]) + | FLOAT -> ^(NumTok<Num>[$FLOAT, actions.makeFloat($FLOAT)]) + | COMPLEX -> ^(NumTok<Num>[$COMPLEX, actions.makeComplex($COMPLEX)]) | (S+=STRING)+ - -> ^(StrTok<Str>[extractStringToken($S), extractStrings($S)]) + -> ^(StrTok<Str>[actions.extractStringToken($S), actions.extractStrings($S)]) ; //listmaker: test ( list_for | (',' test)* [','] ) @@ -1080,11 +883,11 @@ )? )? { if ($a2 != null) { if ($a1.tree.getType() == GenFor) { - throwGenExpNotSoleArg($a1.tree); + actions.throwGenExpNotSoleArg($a1.tree); } for (int i=0;i<$a2.size();i++) { if (((PythonTree)$a2.get(i)).getType() == GenFor) { - throwGenExpNotSoleArg(((argument_return)$a2.get(i)).tree); + actions.throwGenExpNotSoleArg(((argument_return)$a2.get(i)).tree); } } } @@ -1101,7 +904,7 @@ : t1=test[expr_contextType.Load] ( (ASSIGN t2=test[expr_contextType.Load]) -> ^(Keyword ^(Arg $t1) ^(Value $t2)?) | gen_for { if (!first) { - throwGenExpNotSoleArg($gen_for.tree); + actions.throwGenExpNotSoleArg($gen_for.tree); } } -> ^(GenFor $t1 gen_for) Modified: branches/asm/grammar/PythonWalker.g =================================================================== --- branches/asm/grammar/PythonWalker.g 2008-08-08 19:24:38 UTC (rev 5109) +++ branches/asm/grammar/PythonWalker.g 2008-08-08 19:30:38 UTC (rev 5110) @@ -44,6 +44,7 @@ import org.python.antlr.ast.Delete; import org.python.antlr.ast.Dict; import org.python.antlr.ast.Ellipsis; +import org.python.antlr.ast.ErrorStmt; import org.python.antlr.ast.Exec; import org.python.antlr.ast.Expr; import org.python.antlr.ast.Expression; @@ -78,7 +79,6 @@ import org.python.antlr.ast.With; import org.python.antlr.ast.While; import org.python.antlr.ast.Yield; - import java.math.BigInteger; import java.util.Arrays; import java.util.Collections; @@ -91,12 +91,12 @@ @members { boolean debugOn = false; private ErrorHandler errorHandler; + private GrammarActions actions = new GrammarActions(); public void setErrorHandler(ErrorHandler eh) { this.errorHandler = eh; } - public void debug(String message) { if (debugOn) { System.out.println(message); @@ -111,7 +111,6 @@ throw e; } - String name = "Test"; //XXX: Not sure I need any below... @@ -121,177 +120,6 @@ boolean printResults = false; //CompilerFlags cflags = Py.getCompilerFlags(); - private modType makeMod(PythonTree t, List stmts) { - stmtType[] s; - if (stmts != null) { - s = (stmtType[])stmts.toArray(new stmtType[stmts.size()]); - } else { - s = new stmtType[0]; - } - return new Module(t, s); - } - - private modType makeExpression(PythonTree t, exprType e) { - return new Expression(t, e); - } - - private modType makeInteractive(PythonTree t, List stmts) { - stmtType[] s; - if (stmts == null) { - s = new stmtType[0]; - } else { - s = (stmtType[])stmts.toArray(new stmtType[stmts.size()]); - } - return new Interactive(t, s); - } - - private ClassDef makeClassDef(PythonTree t, PythonTree nameToken, List bases, List body) { - exprType[] b = (exprType[])bases.toArray(new exprType[bases.size()]); - stmtType[] s = (stmtType[])body.toArray(new stmtType[body.size()]); - return new ClassDef(t, nameToken.getText(), b, s); - } - - private FunctionDef makeFunctionDef(PythonTree t, PythonTree nameToken, argumentsType args, List funcStatements, List decorators) { - argumentsType a; - debug("Matched FunctionDef"); - if (args != null) { - a = args; - } else { - a = new argumentsType(t, new exprType[0], null, null, new exprType[0]); - } - stmtType[] s = (stmtType[])funcStatements.toArray(new stmtType[funcStatements.size()]); - exprType[] d; - if (decorators != null) { - d = (exprType[])decorators.toArray(new exprType[decorators.size()]); - } else { - d = new exprType[0]; - } - return new FunctionDef(t, nameToken.getText(), a, s, d); - } - - private argumentsType makeArgumentsType(PythonTree t, List params, PythonTree snameToken, - PythonTree knameToken, List defaults) { - debug("Matched Arguments"); - - exprType[] p = (exprType[])params.toArray(new exprType[params.size()]); - exprType[] d = (exprType[])defaults.toArray(new exprType[defaults.size()]); - String s; - String k; - if (snameToken == null) { - s = null; - } else { - s = snameToken.getText(); - } - if (knameToken == null) { - k = null; - } else { - k = knameToken.getText(); - } - return new argumentsType(t, p, s, k, d); - } - - private stmtType makeTryExcept(PythonTree t, List body, List handlers, List orelse, List finBody) { - stmtType[] b = (stmtType[])body.toArray(new stmtType[body.size()]); - excepthandlerType[] e = (excepthandlerType[])handlers.toArray(new excepthandlerType[handlers.size()]); - stmtType[] o; - if (orelse != null) { - o = (stmtType[])orelse.toArray(new stmtType[orelse.size()]); - } else { - o = new stmtType[0]; - } - - stmtType te = new TryExcept(t, b, e, o); - if (finBody == null) { - return te; - } - stmtType[] f = (stmtType[])finBody.toArray(new stmtType[finBody.size()]); - stmtType[] mainBody = new stmtType[]{te}; - return new TryFinally(t, mainBody, f); - } - - private TryFinally makeTryFinally(PythonTree t, List body, List finBody) { - stmtType[] b = (stmtType[])body.toArray(new stmtType[body.size()]); - stmtType[] f = (stmtType[])finBody.toArray(new stmtType[finBody.size()]); - return new TryFinally(t, b, f); - } - - private If makeIf(PythonTree t, exprType test, List body, List orelse) { - stmtType[] o; - if (orelse != null) { - o = (stmtType[])orelse.toArray(new stmtType[orelse.size()]); - } else { - o = new stmtType[0]; - } - stmtType[] b; - if (body != null) { - b = (stmtType[])body.toArray(new stmtType[body.size()]); - } else { - b = new stmtType[0]; - } - return new If(t, test, b, o); - } - - - private While makeWhile(PythonTree t, exprType test, List body, List orelse) { - stmtType[] o; - if (orelse != null) { - o = (stmtType[])orelse.toArray(new stmtType[orelse.size()]); - } else { - o = new stmtType[0]; - } - stmtType[] b = (stmtType[])body.toArray(new stmtType[body.size()]); - return new While(t, test, b, o); - } - - private For makeFor(PythonTree t, exprType target, exprType iter, List body, List orelse) { - stmtType[] o; - if (orelse != null) { - o = (stmtType[])orelse.toArray(new stmtType[orelse.size()]); - } else { - o = new stmtType[0]; - } - stmtType[] b = (stmtType[])body.toArray(new stmtType[body.size()]); - return new For(t, target, iter, b, o); - } - - private Call makeCall(PythonTree t, exprType func) { - return makeCall(t, func, null, null, null, null); - } - - private Call makeCall(PythonTree t, exprType func, List args, List keywords, exprType starargs, exprType kwargs) { - exprType[] a; - keywordType[] k; - if (args == null) { - a = new exprType[0]; - } else { - a = (exprType[])args.toArray(new exprType[args.size()]); - } - if (keywords == null) { - k = new keywordType[0]; - } else { - k = (keywordType[])keywords.toArray(new keywordType[keywords.size()]); - } - return new Call(t, func, a, k, starargs, kwargs); - } - - //FIXME: just calling __neg__ for now - can be better. Also does not parse expressions like - // --2 correctly (should give ^(USub -2) but gives 2). - private exprType negate(PythonTree t, exprType o) { - if (o instanceof Num) { - Num num = (Num)o; - if (num.n instanceof PyObject) { - num.n = ((PyObject)num.n).__neg__(); - } - return num; - } - return new UnaryOp(t, unaryopType.USub, o); - } - - private void checkAssign(exprType e) { - if (e instanceof Name && ((Name)e).id.equals("None")) { - throw new ParseException("assignment to None", e); - } - } } @rulecatch { @@ -303,24 +131,24 @@ expression returns [modType mod] - : ^(Expression test[expr_contextType.Load]) { $mod = makeExpression($Expression, $test.etype); } + : ^(Expression test[expr_contextType.Load]) { $mod = actions.makeExpression($Expression, $test.etype); } ; interactive returns [modType mod] - : ^(Interactive stmts?) { $mod = makeInteractive($Interactive, $stmts.stypes); } + : ^(Interactive stmts?) { $mod = actions.makeInteractive($Interactive, $stmts.stypes); } ; module returns [modType mod] : ^(Module - ( stmts {$mod = makeMod($Module, $stmts.stypes); } - | {$mod = makeMod($Module, null);} + ( stmts {$mod = actions.makeMod($Module, $stmts.stypes); } + | {$mod = actions.makeMod($Module, null);} ) ) ; funcdef : ^(DEF NAME ^(Arguments varargslist?) ^(Body stmts) ^(Decorators decorators?)) { - $stmts::statements.add(makeFunctionDef($DEF, $NAME, $varargslist.args, $stmts.stypes, $decorators.etypes)); + $stmts::statements.add(actions.makeFunctionDef($DEF, $NAME, $varargslist.args, $stmts.stypes, $decorators.etypes)); } ; @@ -330,19 +158,19 @@ List defaults = new ArrayList(); } : ^(Args defparameter[params, defaults]+) (^(StarArgs sname=NAME))? (^(KWArgs kname=NAME))? { - $args = makeArgumentsType($Args, params, $sname, $kname, defaults); + $args = actions.makeArgumentsType($Args, params, $sname, $kname, defaults); } | ^(StarArgs sname=NAME) (^(KWArgs kname=NAME))? { - $args = makeArgumentsType($StarArgs,params, $sname, $kname, defaults); + $args = actions.makeArgumentsType($StarArgs,params, $sname, $kname, defaults); } | ^(KWArgs NAME) { - $args = makeArgumentsType($KWArgs, params, null, $NAME, defaults); + $args = actions.makeArgumentsType($KWArgs, params, null, $NAME, defaults); } ; defparameter[List params, List defaults] : fpdef[expr_contextType.Param, null] (ASSIGN test[expr_contextType.Load])? { - checkAssign($fpdef.etype); + actions.checkAssign($fpdef.etype); params.add($fpdef.etype); if ($ASSIGN != null) { defaults.add($test.etype); @@ -399,9 +227,9 @@ } else { Call c; if ($Args == null) { - c = makeCall($Call, $dotted_attr.etype); + c = actions.makeCall($Call, $dotted_attr.etype); } else { - c = makeCall($Call, $dotted_attr.etype, $arglist.args, $arglist.keywords, $arglist.starargs, $arglist.kwargs); + c = actions.makeCall($Call, $dotted_attr.etype, $arglist.args, $arglist.keywords, $arglist.starargs, $arglist.kwargs); } c.setCharStopIndex($Call.getCharStopIndex()); decs.add(c); @@ -518,9 +346,9 @@ : ^(Call (^(Args arglist))? test[expr_contextType.Load]) { Call c; if ($Args == null) { - c = makeCall($test.marker, $test.etype); + c = actions.makeCall($test.marker, $test.etype); } else { - c = makeCall($test.marker, $test.etype, $arglist.args, $arglist.keywords, $arglist.starargs, $arglist.kwargs); + c = actions.makeCall($test.marker, $test.etype, $arglist.args, $arglist.keywords, $arglist.starargs, $arglist.kwargs); } c.setCharStopIndex($Call.getCharStopIndex()); $etype = c; @@ -538,7 +366,7 @@ target[List etypes] : ^(Target atom[expr_contextType.Store]) { - checkAssign($atom.etype); + actions.checkAssign($atom.etype); etypes.add($atom.etype); } ; @@ -848,7 +676,7 @@ if ($ORELSE != null) { o = $orelse.stypes; } - While w = makeWhile($WHILE, $test.etype, $body.stypes, o); + While w = actions.makeWhile($WHILE, $test.etype, $body.stypes, o); $stmts::statements.add(w); } ; @@ -859,7 +687,7 @@ if ($ORELSE != null) { o = $orelse.stypes; } - For f = makeFor($FOR, $targ.etype, $iter.etype, $body.stypes, o); + For f = actions.makeFor($FOR, $targ.etype, $iter.etype, $body.stypes, o); $stmts::statements.add(f); } ; @@ -877,11 +705,11 @@ if ($FINALLY != null) { f = $fin.stypes; } - stmtType te = makeTryExcept($TryExcept, $body.stypes, handlers, o, f); + stmtType te = actions.makeTryExcept($TryExcept, $body.stypes, handlers, o, f); $stmts::statements.add(te); } | ^(TryFinally ^(Body body=stmts) ^(FINALLY fin=stmts)) { - TryFinally tf = makeTryFinally($TryFinally, $body.stypes, $fin.stypes); + TryFinally tf = actions.makeTryFinally($TryFinally, $body.stypes, $fin.stypes); $stmts::statements.add(tf); } ; @@ -1184,7 +1012,7 @@ } | ^(USub tok=MINUS test[ctype]) { debug("USub matched " + $test.etype); - $etype = negate($tok, $test.etype); + $etype = actions.negate($tok, $test.etype); $marker = $tok; } | ^(UAdd tok=PLUS test[ctype]) { @@ -1309,7 +1137,7 @@ } else { b = new ArrayList(); } - $stmts::statements.add(makeClassDef($CLASS, $classname, b, $stmts.stypes)); + $stmts::statements.add(actions.makeClassDef($CLASS, $classname, b, $stmts.stypes)); } ; @@ -1388,7 +1216,7 @@ keyword[List kws] : ^(Keyword ^(Arg arg=test[expr_contextType.Load]) ^(Value val=test[expr_contextType.Load])) { - checkAssign($arg.etype); + actions.checkAssign($arg.etype); kws.add(new keywordType($Keyword, $arg.text, $val.etype)); } ; Added: branches/asm/src/org/python/antlr/GrammarActions.java =================================================================== --- branches/asm/src/org/python/antlr/GrammarActions.java (rev 0) +++ branches/asm/src/org/python/antlr/GrammarActions.java 2008-08-08 19:30:38 UTC (rev 5110) @@ -0,0 +1,458 @@ +package org.python.antlr; + +import org.antlr.runtime.CommonToken; +import org.antlr.runtime.Token; + +import org.python.core.Py; +import org.python.core.PyObject; +import org.python.core.PyString; +import org.python.core.PyUnicode; +import org.python.antlr.ParseException; +import org.python.antlr.ast.aliasType; +import org.python.antlr.ast.argumentsType; +import org.python.antlr.ast.boolopType; +import org.python.antlr.ast.comprehensionType; +import org.python.antlr.ast.cmpopType; +import org.python.antlr.ast.excepthandlerType; +import org.python.antlr.ast.exprType; +import org.python.antlr.ast.expr_contextType; +import org.python.antlr.ast.keywordType; +import org.python.antlr.ast.modType; +import org.python.antlr.ast.operatorType; +import org.python.antlr.ast.sliceType; +import org.python.antlr.ast.stmtType; +import org.python.antlr.ast.unaryopType; +import org.python.antlr.ast.Assert; +import org.python.antlr.ast.Assign; +import org.python.antlr.ast.Attribute; +import org.python.antlr.ast.AugAssign; +import org.python.antlr.ast.BinOp; +import org.python.antlr.ast.BoolOp; +import org.python.antlr.ast.Break; +import org.python.antlr.ast.Call; +import org.python.antlr.ast.ClassDef; +import org.python.antlr.ast.Compare; +import org.python.antlr.ast.Continue; +import org.python.antlr.ast.Delete; +import org.python.antlr.ast.Dict; +import org.python.antlr.ast.Ellipsis; +import org.python.antlr.ast.ErrorStmt; +import org.python.antlr.ast.Exec; +import org.python.antlr.ast.Expr; +import org.python.antlr.ast.Expression; +import org.python.antlr.ast.ExtSlice; +import org.python.antlr.ast.For; +import org.python.antlr.ast.FunctionDef; +import org.python.antlr.ast.GeneratorExp; +import org.python.antlr.ast.Global; +import org.python.antlr.ast.If; +import org.python.antlr.ast.IfExp; +import org.python.antlr.ast.Index; +import org.python.antlr.ast.Import; +import org.python.antlr.ast.ImportFrom; +import org.python.antlr.ast.Interactive; +import org.python.antlr.ast.Lambda; +import org.python.antlr.ast.ListComp; +import org.python.antlr.ast.Module; +import org.python.antlr.ast.Name; +import org.python.antlr.ast.Num; +import org.python.antlr.ast.Slice; +import org.python.antlr.ast.Subscript; +import org.python.antlr.ast.TryExcept; +import org.python.antlr.ast.TryFinally; +import org.python.antlr.ast.Tuple; +import org.python.antlr.ast.Pass; +import org.python.antlr.ast.Print; +import org.python.antlr.ast.Raise; +import org.python.antlr.ast.Repr; +import org.python.antlr.ast.Return; +import org.python.antlr.ast.Str; +import org.python.antlr.ast.UnaryOp; +import org.python.antlr.ast.With; +import org.python.antlr.ast.While; +import org.python.antlr.ast.Yield; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.Set; + +public class GrammarActions { + public GrammarActions() { + } + + void debug(String x) { + if (false) { + System.out.println(x); + } + } + + void throwGenExpNotSoleArg(PythonTree t) { + throw new ParseException("Generator expression must be parenthesized if not sole argument", t); + } + + exprType[] makeExprs(List exprs) { + return makeExprs(exprs, 0); + } + + exprType[] makeExprs(List exprs, int start) { + if (exprs != null) { + List<exprType> result = new ArrayList<exprType>(); + for (int i=start; i<exprs.size(); i++) { + exprType e = (exprType)exprs.get(i); + result.add(e); + } + return (exprType[])result.toArray(new exprType[result.size()]); + } + return new exprType[0]; + } + + stmtType[] makeStmts(List stmts) { + if (stmts != null) { + List<stmtType> result = new ArrayList<stmtType>(); + for (int i=0; i<stmts.size(); i++) { + result.add((stmtType)stmts.get(i)); + } + return (stmtType[])result.toArray(new stmtType[result.size()]); + } + return new stmtType[0]; + } + + exprType makeDottedAttr(Token nameToken, List attrs) { + exprType current = new Name(nameToken, nameToken.getText(), expr_contextType.Load); + for (int i=attrs.size() - 1; i > -1; i--) { + Token t = ((PythonTree)attrs.get(i)).token; + current = new Attribute(t, current, t.getText(), + expr_contextType.Load); + } + return current; + } + + stmtType makeFunctionDef(PythonTree t, PythonTree nameToken, argumentsType args, List funcStatements, List decorators) { + /* + if (nameToken == null || funcStatements == null || decorators == null) { + return new ErrorStmt(t); + } + */ + argumentsType a; + debug("Matched FunctionDef"); + if (args != null) { + a = args; + } else { + a = new argumentsType(t, new exprType[0], null, null, new exprType[0]); + } + stmtType[] s = (stmtType[])funcStatements.toArray(new stmtType[funcStatements.size()]); + exprType[] d; + if (decorators != null) { + d = (exprType[])decorators.toArray(new exprType[decorators.size()]); + } else { + d = new exprType[0]; + } + return new FunctionDef(t, nameToken.getText(), a, s, d); + } + + argumentsType makeArgumentsType(Token t, List params, Token snameToken, + Token knameToken, List defaults) { + debug("Matched Arguments"); + + exprType[] p = (exprType[])params.toArray(new exprType[params.size()]); + exprType[] d = (exprType[])defaults.toArray(new exprType[defaults.size()]); + String s; + String k; + if (snameToken == null) { + s = null; + } else { + s = snameToken.getText(); + } + if (knameToken == null) { + k = null; + } else { + k = knameToken.getText(); + } + return new argumentsType(t, p, s, k, d); + } + + + + Object makeFloat(Token t) { + debug("makeFloat matched " + t.getText()); + return Py.newFloat(Double.valueOf(t.getText())); + } + + Object makeComplex(Token t) { + String s = t.getText(); + s = s.substring(0, s.length() - 1); + return Py.newImaginary(Double.valueOf(s)); + } + + Object makeInt(Token t) { + debug("Num matched " + t.getText()); + String s = t.getText(); + int radix = 10; + if (s.startsWith("0x") || s.startsWith("0X")) { + radix = 16; + s = s.substring(2, s.length()); + } else if (s.startsWith("0")) { + radix = 8; + } + if (s.endsWith("L") || s.endsWith("l")) { + s = s.substring(0, s.length()-1); + return Py.newLong(new BigInteger(s, radix)); + } + int ndigits = s.length(); + int i=0; + while (i < ndigits && s.charAt(i) == '0') + i++; + if ((ndigits - i) > 11) { + return Py.newLong(new BigInteger(s, radix)); + } + + long l = Long.valueOf(s, radix).longValue(); + if (l > 0xffffffffl || (l > Integer.MAX_VALUE)) { + return Py.newLong(new BigInteger(s, radix)); + } + return Py.newInteger((int) l); + } + + class StringPair { + private String s; + private boolean unicode; + + StringPair(String s, boolean unicode) { + this.s = s; + this.unicode = unicode; + } + String getString() { + return s; + } + + boolean isUnicode() { + return unicode; + } + } + + PyString extractStrings(List s) { + boolean ustring = false; + Token last = null; + StringBuffer sb = new StringBuffer(); + Iterator iter = s.iterator(); + while (iter.hasNext()) { + last = (Token)iter.next(); + StringPair sp = extractString(last); + if (sp.isUnicode()) { + ustring = true; + } + sb.append(sp.getString()); + } + if (ustring) { + return new PyUnicode(sb.toString()); + } + return new PyString(sb.toString()); + } + + StringPair extractString(Token t) { + String s = t.getText(); + char quoteChar = s.charAt(0); + int start=0; + boolean ustring = false; + if (quoteChar == 'u' || quoteChar == 'U') { + ustring = true; + start++; + } + quoteChar = s.charAt(start); + boolean raw = false; + if (quoteChar == 'r' || quoteChar == 'R') { + raw = true; + start++; + } + int quotes = 3; + if (s.length() - start == 2) { + quotes = 1; + } + if (s.charAt(start) != s.charAt(start+1)) { + quotes = 1; + } + + if (raw) { + return new StringPair(s.substring(quotes+start, s.length()-quotes), ustring); + } else { + StringBuffer sb = new StringBuffer(s.length()); + char[] ca = s.toCharArray(); + int n = ca.length-quotes; + int i=quotes+start; + int last_i=i; + return new StringPair(PyString.decode_UnicodeEscape(s, i, n, "strict", ustring), ustring); + //return decode_UnicodeEscape(s, i, n, "strict", ustring); + } + } + + Token extractStringToken(List s) { + return (Token)s.get(s.size() - 1); + } + + //FROM Walker: + modType makeMod(PythonTree t, List stmts) { + stmtType[] s; + if (stmts != null) { + s = (stmtType[])stmts.toArray(new stmtType[stmts.size()]); + } else { + s = new stmtType[0]; + } + return new Module(t, s); + } + + modType makeExpression(PythonTree t, exprType e) { + return new Expression(t, e); + } + + modType makeInteractive(PythonTree t, List stmts) { + stmtType[] s; + if (stmts == null) { + s = new stmtType[0]; + } else { + s = (stmtType[])stmts.toArray(new stmtType[stmts.size()]); + } + return new Interactive(t, s); + } + + stmtType makeClassDef(PythonTree t, PythonTree nameToken, List bases, List body) { + /* + if (nameToken == null || bases == null || body == null) { + return new ErrorStmt(t); + } + */ + exprType[] b = (exprType[])bases.toArray(new exprType[bases.size()]); + stmtType[] s = (stmtType[])body.toArray(new stmtType[body.size()]); + return new ClassDef(t, nameToken.getText(), b, s); + } + + argumentsType makeArgumentsType(PythonTree t, List params, PythonTree snameToken, + PythonTree knameToken, List defaults) { + debug("Matched Arguments"); + + exprType[] p = (exprType[])params.toArray(new exprType[params.size()]); + exprType[] d = (exprType[])defaults.toArray(new exprType[defaults.size()]); + String s; + String k; + if (snameToken == null) { + s = null; + } else { + s = snameToken.getText(); + } + if (knameToken == null) { + k = null; + } else { + k = knameToken.getText(); + } + return new argumentsType(t, p, s, k, d); + } + + stmtType makeTryExcept(PythonTree t, List body, List handlers, List orelse, List finBody) { + stmtType[] b = (stmtType[])body.toArray(new stmtType[body.size()]); + excepthandlerType[] e = (excepthandlerType[])handlers.toArray(new excepthandlerType[handlers.size()]); + stmtType[] o; + if (orelse != null) { + o = (stmtType[])orelse.toArray(new stmtType[orelse.size()]); + } else { + o = new stmtType[0]; + } + + stmtType te = new TryExcept(t, b, e, o); + if (finBody == null) { + return te; + } + stmtType[] f = (stmtType[])finBody.toArray(new stmtType[finBody.size()]); + stmtType[] mainBody = new stmtType[]{te}; + return new TryFinally(t, mainBody, f); + } + + TryFinally makeTryFinally(PythonTree t, List body, List finBody) { + stmtType[] b = (stmtType[])body.toArray(new stmtType[body.size()]); + stmtType[] f = (stmtType[])finBody.toArray(new stmtType[finBody.size()]); + return new TryFinally(t, b, f); + } + + If makeIf(PythonTree t, exprType test, List body, List orelse) { + stmtType[] o; + if (orelse != null) { + o = (stmtType[])orelse.toArray(new stmtType[orelse.size()]); + } else { + o = new stmtType[0]; + } + stmtType[] b; + if (body != null) { + b = (stmtType[])body.toArray(new stmtType[body.size()]); + } else { + b = new stmtType[0]; + } + return new If(t, test, b, o); + } + + + While makeWhile(PythonTree t, exprType test, List body, List orelse) { + stmtType[] o; + if (orelse != null) { + o = (stmtType[])orelse.toArray(new stmtType[orelse.size()]); + } else { + o = new stmtType[0]; + } + stmtType[] b = (stmtType[])body.toArray(new stmtType[body.size()]); + return new While(t, test, b, o); + } + + For makeFor(PythonTree t, exprType target, exprType iter, List body, List orelse) { + stmtType[] o; + if (orelse != null) { + o = (stmtType[])orelse.toArray(new stmtType[orelse.size()]); + } else { + o = new stmtType[0]; + } + stmtType[] b = (stmtType[])body.toArray(new stmtType[body.size()]); + return new For(t, target, iter, b, o); + } + + Call makeCall(PythonTree t, exprType func) { + return makeCall(t, func, null, null, null, null); + } + + Call makeCall(PythonTree t, exprType func, List args, List keywords, exprType starargs, exprType kwargs) { + exprType[] a; + keywordType[] k; + if (args == null) { + a = new exprType[0]; + } else { + a = (exprType[])args.toArray(new exprType[args.size()]); + } + if (keywords == null) { + k = new keywordType[0]; + } else { + k = (keywordType[])keywords.toArray(new keywordType[keywords.size()]); + } + return new Call(t, func, a, k, starargs, kwargs); + } + + //FIXME: just calling __neg__ for now - can be better. Also does not parse expressions like + // --2 correctly (should give ^(USub -2) but gives 2). + exprType negate(PythonTree t, exprType o) { + if (o instanceof Num) { + Num num = (Num)o; + if (num.n instanceof PyObject) { + num.n = ((PyObject)num.n).__neg__(); + } + return num; + } + return new UnaryOp(t, unaryopType.USub, o); + } + + void checkAssign(exprType e) { + if (e instanceof Name && ((Name)e).id.equals("None")) { + throw new ParseException("assignment to None", e); + } + } +} Added: branches/asm/src/org/python/antlr/ast/ErrorStmt.java =================================================================== --- branches/asm/src/org/python/antlr/ast/ErrorStmt.java (rev 0) +++ branches/asm/src/org/python/antlr/ast/ErrorStmt.java 2008-08-08 19:30:38 UTC (rev 5110) @@ -0,0 +1,32 @@ +package org.python.antlr.ast; +import org.python.antlr.PythonTree; +import org.antlr.runtime.CommonToken; +import org.antlr.runtime.Token; +import java.io.DataOutputStream; +import java.io.IOException; + +public class ErrorStmt extends stmtType { + + public static final String[] _fields = new String[] {}; + + public ErrorStmt(PythonTree tree) { + super(tree); + } + + public String toString() { + return "ErrorStmt"; + } + + public String toStringTree() { + return "ErrorStmt"; + } + + public int getLineno() { + return getLine(); + } + + public int getCol_offset() { + return getCharPositionInLine(); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-08 19:24:44
|
Revision: 5109 http://jython.svn.sourceforge.net/jython/?rev=5109&view=rev Author: fwierzbicki Date: 2008-08-08 19:24:38 +0000 (Fri, 08 Aug 2008) Log Message: ----------- Remove debug string. Modified Paths: -------------- branches/asm/src/org/python/core/ParserFacade.java Modified: branches/asm/src/org/python/core/ParserFacade.java =================================================================== --- branches/asm/src/org/python/core/ParserFacade.java 2008-08-08 18:24:44 UTC (rev 5108) +++ branches/asm/src/org/python/core/ParserFacade.java 2008-08-08 19:24:38 UTC (rev 5109) @@ -187,7 +187,6 @@ } } catch (Exception e) { - System.out.println("valid sentence prob: " + e); return lexer.eofWhileNested; } return true; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-08 18:24:51
|
Revision: 5108 http://jython.svn.sourceforge.net/jython/?rev=5108&view=rev Author: fwierzbicki Date: 2008-08-08 18:24:44 +0000 (Fri, 08 Aug 2008) Log Message: ----------- Various fixes to improve Jython compatibility with CPython, especially when passing PyCF_DONT_IMPLY_DEDENT. Added PyCF_DONT_IMPLY_DEDENT to codeop.py, PyTableCode.java, and CompilerFlags.java to help test_codeop.py run. Began to integrate tests from test_jy_compile.py, which appears to have started as a copy of test_codeop.py, into test_codeop.py. Once test_jy_compile is fully integrated into test_codeop.py I should be able to delete test_jy_compile. Also added eval support to PythonPartial.g, since test_codeop exercises that functionality. Modified Paths: -------------- branches/asm/Lib/codeop.py branches/asm/Lib/test/test_codeop.py branches/asm/grammar/PythonPartial.g branches/asm/src/org/python/core/CompilerFlags.java branches/asm/src/org/python/core/ParserFacade.java branches/asm/src/org/python/core/Py.java branches/asm/src/org/python/core/PyTableCode.java Modified: branches/asm/Lib/codeop.py =================================================================== --- branches/asm/Lib/codeop.py 2008-08-08 17:56:53 UTC (rev 5107) +++ branches/asm/Lib/codeop.py 2008-08-08 18:24:44 UTC (rev 5108) @@ -58,6 +58,7 @@ # import internals, not guaranteed interface from org.python.core import Py,CompilerFlags +from org.python.core.PyTableCode import PyCF_DONT_IMPLY_DEDENT # public interface Modified: branches/asm/Lib/test/test_codeop.py =================================================================== --- branches/asm/Lib/test/test_codeop.py 2008-08-08 17:56:53 UTC (rev 5107) +++ branches/asm/Lib/test/test_codeop.py 2008-08-08 18:24:44 UTC (rev 5108) @@ -86,12 +86,22 @@ av("def x():\n\n pass\n") av("def x():\n pass\n \n") av("def x():\n pass\n \n") + ##next 4 added for Jython + av("\n\ndef x():\n pass\n") + av("def x():\n\n pass\n") # failed under Jython 2.1 + av("def x():\n pass\n \n") + av("def x():\n pass\n \n") + # this failed under 2.2.1 + av("def x():\n try: pass\n finally: [a for a in (1,2)]\n") + av("pass\n") av("3**3\n") av("if 9==3:\n pass\nelse:\n pass\n") av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n") + #next 2 added for Jython + av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n") av("#a\n#b\na = 3\n") av("#a\n\n \na=3\n") @@ -122,6 +132,7 @@ ai("if 9==3:\n pass\nelse:") ai("if 9==3:\n pass\nelse:\n") ai("if 9==3:\n pass\nelse:\n pass") + ai("if 1:") ai("if 1:\n") ai("if 1:\n pass\n if 1:\n pass\n else:") @@ -133,12 +144,12 @@ ai("def x():\n\n") ai("def x():\n pass") - ai("def x():\n pass\n ") - ai("def x():\n pass\n ") + #ai("def x():\n pass\n ") + #ai("def x():\n pass\n ") ai("\n\ndef x():\n pass") - ai("a = 9+ \\") - ai("a = 'a\\") + #ai("a = 9+ \\") + #ai("a = 'a\\") ai("a = '''xy") ai("","eval") @@ -146,8 +157,8 @@ ai("(","eval") ai("(\n\n\n","eval") ai("(9+","eval") - ai("9+ \\","eval") - ai("lambda z: \\","eval") + #ai("9+ \\","eval") + #ai("lambda z: \\","eval") def test_invalid(self): ai = self.assertInvalid @@ -168,14 +179,17 @@ ai("a = 'a\\ ") ai("a = 'a\\\n") - ai("a = 1","eval") - ai("a = (","eval") - ai("]","eval") - ai("())","eval") - ai("[}","eval") - ai("9+","eval") - ai("lambda z:","eval") - ai("a b","eval") + #XXX: eval is hopelessly permissive right now -- but the rest of this + # test_jy_compile is really important to me for flagging new bugs - + # so commenting out these for now. + #ai("a = 1","eval") + #ai("a = (","eval") + #ai("]","eval") + #ai("())","eval") + #ai("[}","eval") + #ai("9+","eval") + #ai("lambda z:","eval") + #ai("a b","eval") def test_filename(self): self.assertEquals(compile_command("a = 1\n", "abc").co_filename, Modified: branches/asm/grammar/PythonPartial.g =================================================================== --- branches/asm/grammar/PythonPartial.g 2008-08-08 17:56:53 UTC (rev 5107) +++ branches/asm/grammar/PythonPartial.g 2008-08-08 18:24:44 UTC (rev 5108) @@ -192,6 +192,10 @@ | compound_stmt NEWLINE? ; +//eval_input: testlist NEWLINE* ENDMARKER +eval_input : (NEWLINE)* testlist? (NEWLINE)* + ; + decorators: decorator+ ; Modified: branches/asm/src/org/python/core/CompilerFlags.java =================================================================== --- branches/asm/src/org/python/core/CompilerFlags.java 2008-08-08 17:56:53 UTC (rev 5107) +++ branches/asm/src/org/python/core/CompilerFlags.java 2008-08-08 18:24:44 UTC (rev 5108) @@ -7,6 +7,7 @@ public boolean division; public boolean generator_allowed = true; public boolean only_ast = false; + public boolean dont_imply_dedent = false; public boolean with_statement = false; public boolean absolute_import = false; @@ -33,12 +34,16 @@ if ((co_flags & org.python.core.PyTableCode.PyCF_ONLY_AST) != 0) { this.only_ast = true; } + if ((co_flags & org.python.core.PyTableCode.PyCF_DONT_IMPLY_DEDENT) != 0) { + this.dont_imply_dedent = true; + } } public String toString() { return String.format("CompilerFlags[division=%s nested_scopes=%s generators=%s " - + "with_statement=%s absolute_import=%s]", division, nested_scopes, generator_allowed, - with_statement, absolute_import); + + "with_statement=%s absolute_import=%s only_ast=%s " + + "dont_imply_dedent=%s]", division, nested_scopes, generator_allowed, + with_statement, absolute_import, only_ast, dont_imply_dedent); } } Modified: branches/asm/src/org/python/core/ParserFacade.java =================================================================== --- branches/asm/src/org/python/core/ParserFacade.java 2008-08-08 17:56:53 UTC (rev 5107) +++ branches/asm/src/org/python/core/ParserFacade.java 2008-08-08 18:24:44 UTC (rev 5108) @@ -98,7 +98,7 @@ String filename, CompilerFlags cflags) { BufferedInputStream bstream = new BufferedInputStream(stream); - //FIMXE: npe? + //FIXME: npe? BufferedReader bufreader = null; modType node = null; try { @@ -138,23 +138,28 @@ String filename, CompilerFlags cflags, boolean stdprompt) { + ByteArrayInputStream bi = new ByteArrayInputStream( + StringUtil.toBytes(string)); + BufferedInputStream bstream = bstream = new BufferedInputStream(bi); + //FIXME: npe? + BufferedReader bufreader = null; modType node = null; - //FIMXE: npe? - BufferedReader bufreader = null; try { if (kind.equals("single")) { - ByteArrayInputStream bi = new ByteArrayInputStream( - StringUtil.toBytes(string)); - BufferedInputStream bstream = bstream = new BufferedInputStream(bi); bufreader = prepBufreader(bstream, cflags, filename); InteractiveParser i = new InteractiveParser(bufreader, filename); node = i.parse(); + } else if (kind.equals("eval")) { + bufreader = prepBufreader(new LeadingSpaceSkippingStream(bstream), cflags, filename); + CharStream cs = new NoCloseReaderStream(bufreader); + ExpressionParser e = new ExpressionParser(cs, filename); + node = e.parse(); } else { throw Py.ValueError("parse kind must be eval, exec, " + "or single"); } } catch (Throwable t) { PyException p = fixParseError(bufreader, t, filename); - if (validPartialSentence(bufreader)) { + if (validPartialSentence(bufreader, kind)) { return null; } throw p; @@ -162,7 +167,7 @@ return node; } - private static boolean validPartialSentence(BufferedReader bufreader) { + private static boolean validPartialSentence(BufferedReader bufreader, String kind) { PythonPartialLexer lexer = null; try { bufreader.reset(); @@ -173,8 +178,16 @@ PythonPartialTokenSource indentedSource = new PythonPartialTokenSource(tokens); tokens = new CommonTokenStream(indentedSource); PythonPartialParser parser = new PythonPartialParser(tokens); - parser.single_input(); + if (kind.equals("single")) { + parser.single_input(); + } else if (kind.equals("eval")) { + parser.eval_input(); + } else { + return false; + } + } catch (Exception e) { + System.out.println("valid sentence prob: " + e); return lexer.eofWhileNested; } return true; Modified: branches/asm/src/org/python/core/Py.java =================================================================== --- branches/asm/src/org/python/core/Py.java 2008-08-08 17:56:53 UTC (rev 5107) +++ branches/asm/src/org/python/core/Py.java 2008-08-08 18:24:44 UTC (rev 5108) @@ -1682,7 +1682,13 @@ String filename, String type, CompilerFlags cflags) { - return Py.compile_flags(new ByteArrayInputStream(StringUtil.toBytes(data + "\n\n")), + byte[] bytes; + if (cflags.dont_imply_dedent) { + bytes = StringUtil.toBytes(data + "\n"); + } else { + bytes = StringUtil.toBytes(data + "\n\n"); + } + return Py.compile_flags(new ByteArrayInputStream(bytes), filename, type, cflags); @@ -1696,6 +1702,7 @@ if (node == null) { return Py.None; } + return Py.compile_flags(node, Py.getName(), filename, true, true, cflags); } Modified: branches/asm/src/org/python/core/PyTableCode.java =================================================================== --- branches/asm/src/org/python/core/PyTableCode.java 2008-08-08 17:56:53 UTC (rev 5107) +++ branches/asm/src/org/python/core/PyTableCode.java 2008-08-08 18:24:44 UTC (rev 5108) @@ -38,12 +38,13 @@ 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 this constant. - final public static int PyCF_ONLY_AST = 0x0400; + //XXX: I'm not positive that this is the right place for these constants. + 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_ONLY_AST|CO_NESTED|CO_GENERATOR_ALLOWED| - CO_FUTUREDIVISION|CO_FUTURE_ABSOLUTE_IMPORT| - CO_WITH_STATEMENT; + final public static int CO_ALL_FEATURES = PyCF_DONT_IMPLY_DEDENT|PyCF_ONLY_AST|CO_NESTED| + CO_GENERATOR_ALLOWED| CO_FUTUREDIVISION| + CO_FUTURE_ABSOLUTE_IMPORT|CO_WITH_STATEMENT; public PyTableCode(int argcount, String varnames[], String filename, String name, @@ -163,8 +164,8 @@ } } // nested scopes: setup env with closure - // this should only be done once, so let the frame take care of it - frame.setupEnv((PyTuple)closure); + // this should only be done once, so let the frame take care of it + frame.setupEnv((PyTuple)closure); ts.frame = frame; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-08 17:56:57
|
Revision: 5107 http://jython.svn.sourceforge.net/jython/?rev=5107&view=rev Author: fwierzbicki Date: 2008-08-08 17:56:53 +0000 (Fri, 08 Aug 2008) Log Message: ----------- from: http://svn.python.org/projects/python/branches/release25-maint/Lib/test/test_codeop.py@51333 Added Paths: ----------- branches/asm/Lib/test/test_codeop.py Added: branches/asm/Lib/test/test_codeop.py =================================================================== --- branches/asm/Lib/test/test_codeop.py (rev 0) +++ branches/asm/Lib/test/test_codeop.py 2008-08-08 17:56:53 UTC (rev 5107) @@ -0,0 +1,192 @@ +""" + Test cases for codeop.py + Nick Mathewson +""" +import unittest +from test.test_support import run_unittest, is_jython + +from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT + +if is_jython: + import sys + import cStringIO + + def unify_callables(d): + for n,v in d.items(): + if callable(v): + d[n] = callable + return d + +class CodeopTests(unittest.TestCase): + + def assertValid(self, str, symbol='single'): + '''succeed iff str is a valid piece of code''' + if is_jython: + code = compile_command(str, "<input>", symbol) + self.assert_(code) + if symbol == "single": + d,r = {},{} + saved_stdout = sys.stdout + sys.stdout = cStringIO.StringIO() + try: + exec code in d + exec compile(str,"<input>","single") in r + finally: + sys.stdout = saved_stdout + elif symbol == 'eval': + ctx = {'a': 2} + d = { 'value': eval(code,ctx) } + r = { 'value': eval(str,ctx) } + self.assertEquals(unify_callables(r),unify_callables(d)) + else: + expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT) + self.assertEquals( compile_command(str, "<input>", symbol), expected) + + def assertIncomplete(self, str, symbol='single'): + '''succeed iff str is the start of a valid piece of code''' + self.assertEquals( compile_command(str, symbol=symbol), None) + + def assertInvalid(self, str, symbol='single', is_syntax=1): + '''succeed iff str is the start of an invalid piece of code''' + try: + compile_command(str,symbol=symbol) + self.fail("No exception thrown for invalid code") + except SyntaxError: + self.assert_(is_syntax) + except OverflowError: + self.assert_(not is_syntax) + + def test_valid(self): + av = self.assertValid + + # special case + if not is_jython: + self.assertEquals(compile_command(""), + compile("pass", "<input>", 'single', + PyCF_DONT_IMPLY_DEDENT)) + self.assertEquals(compile_command("\n"), + compile("pass", "<input>", 'single', + PyCF_DONT_IMPLY_DEDENT)) + else: + av("") + av("\n") + + av("a = 1") + av("\na = 1") + av("a = 1\n") + av("a = 1\n\n") + av("\n\na = 1\n\n") + + av("def x():\n pass\n") + av("if 1:\n pass\n") + + av("\n\nif 1: pass\n") + av("\n\nif 1: pass\n\n") + + av("def x():\n\n pass\n") + av("def x():\n pass\n \n") + av("def x():\n pass\n \n") + + av("pass\n") + av("3**3\n") + + av("if 9==3:\n pass\nelse:\n pass\n") + av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n") + + av("#a\n#b\na = 3\n") + av("#a\n\n \na=3\n") + av("a=3\n\n") + av("a = 9+ \\\n3") + + av("3**3","eval") + av("(lambda z: \n z**3)","eval") + + av("9+ \\\n3","eval") + av("9+ \\\n3\n","eval") + + av("\n\na**3","eval") + av("\n \na**3","eval") + av("#a\n#b\na**3","eval") + + def test_incomplete(self): + ai = self.assertIncomplete + + ai("(a **") + ai("(a,b,") + ai("(a,b,(") + ai("(a,b,(") + ai("a = (") + ai("a = {") + ai("b + {") + + ai("if 9==3:\n pass\nelse:") + ai("if 9==3:\n pass\nelse:\n") + ai("if 9==3:\n pass\nelse:\n pass") + ai("if 1:") + ai("if 1:\n") + ai("if 1:\n pass\n if 1:\n pass\n else:") + ai("if 1:\n pass\n if 1:\n pass\n else:\n") + ai("if 1:\n pass\n if 1:\n pass\n else:\n pass") + + ai("def x():") + ai("def x():\n") + ai("def x():\n\n") + + ai("def x():\n pass") + ai("def x():\n pass\n ") + ai("def x():\n pass\n ") + ai("\n\ndef x():\n pass") + + ai("a = 9+ \\") + ai("a = 'a\\") + ai("a = '''xy") + + ai("","eval") + ai("\n","eval") + ai("(","eval") + ai("(\n\n\n","eval") + ai("(9+","eval") + ai("9+ \\","eval") + ai("lambda z: \\","eval") + + def test_invalid(self): + ai = self.assertInvalid + ai("a b") + + ai("a @") + ai("a b @") + ai("a ** @") + + ai("a = ") + ai("a = 9 +") + + ai("def x():\n\npass\n") + + ai("\n\n if 1: pass\n\npass") + + ai("a = 9+ \\\n") + ai("a = 'a\\ ") + ai("a = 'a\\\n") + + ai("a = 1","eval") + ai("a = (","eval") + ai("]","eval") + ai("())","eval") + ai("[}","eval") + ai("9+","eval") + ai("lambda z:","eval") + ai("a b","eval") + + def test_filename(self): + self.assertEquals(compile_command("a = 1\n", "abc").co_filename, + compile("a = 1\n", "abc", 'single').co_filename) + self.assertNotEquals(compile_command("a = 1\n", "abc").co_filename, + compile("a = 1\n", "def", 'single').co_filename) + + +def test_main(): + run_unittest(CodeopTests) + + +if __name__ == "__main__": + test_main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-08 17:42:10
|
Revision: 5106 http://jython.svn.sourceforge.net/jython/?rev=5106&view=rev Author: fwierzbicki Date: 2008-08-08 17:42:06 +0000 (Fri, 08 Aug 2008) Log Message: ----------- make jarjar run all of the time until I figure out why the conditionals are not working. Modified Paths: -------------- branches/asm/build.xml Modified: branches/asm/build.xml =================================================================== --- branches/asm/build.xml 2008-08-08 15:37:43 UTC (rev 5105) +++ branches/asm/build.xml 2008-08-08 17:42:06 UTC (rev 5106) @@ -510,7 +510,10 @@ includesfile="${jython.base.dir}/CoreExposed.includes"/> </target> - <target name="jarjar" depends="init,needed-check" if="jarjar.needed" unless="jarjar.notneeded"> + <!-- XXX: this should be conditional if="jarjar.needed" or unless="jarjar.notneeded" + but I haven't been able to get this to work. For now always do jarjar + --> + <target name="jarjar" depends="init,needed-check"> <taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask" classpath="extlibs/jarjar-0.7.jar"/> <jarjar destfile="${output.dir}/jarjar.jar"> <zipfileset src="extlibs/antlr-runtime-3.1b2.jar"/> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-08-08 15:37:54
|
Revision: 5105 http://jython.svn.sourceforge.net/jython/?rev=5105&view=rev Author: zyasoft Date: 2008-08-08 15:37:43 +0000 (Fri, 08 Aug 2008) Log Message: ----------- With statements now keep their own stack of exit functions in PyFrame so that they don't get stepped on by generators save/restore of local temporary registers. Fixes most problems in test_with, including JVM verification errors. (Remaining issues seem to be with generator exit issues, to be resolved separately.) Modified Paths: -------------- branches/asm/src/org/python/compiler/CodeCompiler.java branches/asm/src/org/python/compiler/ScopeInfo.java branches/asm/src/org/python/compiler/ScopesCompiler.java branches/asm/src/org/python/core/PyFrame.java Modified: branches/asm/src/org/python/compiler/CodeCompiler.java =================================================================== --- branches/asm/src/org/python/compiler/CodeCompiler.java 2008-08-07 21:18:44 UTC (rev 5104) +++ branches/asm/src/org/python/compiler/CodeCompiler.java 2008-08-08 15:37:43 UTC (rev 5105) @@ -137,6 +137,7 @@ public int bcfLevel = 0; int yield_count = 0; + int with_count = 0; public CodeCompiler(Module module, boolean print_results) { this.module = module; @@ -261,6 +262,15 @@ tbl = scope.tbl; optimizeGlobals = fast_locals&&!scope.exec&&!scope.from_import_star; + + if (scope.max_with_count > 0) { + // allocate for all the with-exits we will have in the frame; + // this allows yield and with to happily co-exist + loadFrame(); + code.iconst(scope.max_with_count); + code.anewarray("org/python/core/PyObject"); + code.putfield("org/python/core/PyFrame", "f_exits", $pyObjArr); + } Object exit = visit(node); @@ -2005,70 +2015,77 @@ if (!module.getFutures().withStatementSupported()) { throw new ParseException("'with' will become a reserved keyword in Python 2.6", node); } - - int mgr_tmp = code.getLocal("org/python/core/PyObject"); - int exit_tmp = code.getLocal("org/python/core/PyObject"); - int value_tmp = code.getLocal("org/python/core/PyObject"); - int exc_tmp = code.getLocal("java/lang/Throwable"); - + + int my_with_count = with_count; + with_count++; + Label label_body_start = new Label(); Label label_body_end = new Label(); Label label_catch = new Label(); Label label_finally = new Label(); Label label_end = new Label(); - + Method getattr = Method.getMethod("org.python.core.PyObject __getattr__ (String)"); Method call = Method.getMethod("org.python.core.PyObject __call__ ()"); Method call3 = Method.getMethod("org.python.core.PyObject __call__ (org.python.core.PyObject,org.python.core.PyObject,org.python.core.PyObject)"); - - setline(node); - + // mgr = (EXPR) visit(node.context_expr); + int mgr_tmp = code.getLocal("org/python/core/PyObject"); code.astore(mgr_tmp); - - // exit = mgr.__exit__ # Not calling it yet + + // exit = mgr.__exit__ # Not calling it yet, so storing in the frame + loadFrame(); + code.getfield("org/python/core/PyFrame", "f_exits", $pyObjArr); + code.iconst(my_with_count); code.aload(mgr_tmp); code.ldc("__exit__"); code.invokevirtual(Type.getType(PyObject.class).getInternalName(), getattr.getName(), getattr.getDescriptor()); - code.astore(exit_tmp); + code.aastore(); // value = mgr.__enter__() code.aload(mgr_tmp); + code.freeLocal(mgr_tmp); code.ldc("__enter__"); code.invokevirtual(Type.getType(PyObject.class).getInternalName(), getattr.getName(), getattr.getDescriptor()); code.invokevirtual(Type.getType(PyObject.class).getInternalName(), call.getName(), call.getDescriptor()); + int value_tmp = code.getLocal("org/python/core/PyObject"); code.astore(value_tmp); // exc = True # not necessary, since we don't exec finally if exception // try-catch block here code.trycatch(label_body_start, label_body_end, label_catch, "java/lang/Throwable"); - + // VAR = value # Only if "as VAR" is present code.label(label_body_start); if (node.optional_vars != null) { - set(node.optional_vars, value_tmp); + set(node.optional_vars, value_tmp); } + code.freeLocal(value_tmp); + // BLOCK suite(node.body); code.goto_(label_finally); code.label(label_body_end); - + // CATCH code.label(label_catch); - code.astore(exc_tmp); - - code.aload(exc_tmp); + loadFrame(); code.invokestatic("org/python/core/Py", "setException", "(" + $throwable + $pyFrame + ")" + $pyExc); - code.astore(exc_tmp); - - code.invokestatic("org/python/core/Py", "getThreadState", "()Lorg/python/core/ThreadState;"); + code.pop(); + + code.invokestatic("org/python/core/Py", "getThreadState", "()Lorg/python/core/ThreadState;"); code.getfield("org/python/core/ThreadState", "exception", $pyExc); int ts_tmp = storeTop(); + // # The exceptional case is handled here + // exc = False # implicit // if not exit(*sys.exc_info()): - code.aload(exit_tmp); + loadFrame(); + code.getfield("org/python/core/PyFrame", "f_exits", $pyObjArr); + code.iconst(my_with_count); + code.aaload(); code.aload(ts_tmp); code.getfield("org/python/core/PyException", "type", $pyObj); code.aload(ts_tmp); @@ -2079,10 +2096,13 @@ code.invokevirtual(Type.getType(PyObject.class).getInternalName(), call3.getName(), call3.getDescriptor()); code.invokevirtual("org/python/core/PyObject", "__nonzero__", "()Z"); code.ifne(label_end); - // raise + // raise + // # The exception is swallowed if exit() returns true code.invokestatic("org/python/core/Py", "makeException", "()Lorg/python/core/PyException;"); code.checkcast("java/lang/Throwable"); code.athrow(); + + code.freeLocal(ts_tmp); // FINALLY // ordinarily with a finally, we need to duplicate the code. that's not the case here @@ -2091,24 +2111,20 @@ // exit(None, None, None) code.label(label_finally); + + loadFrame(); + code.getfield("org/python/core/PyFrame", "f_exits", $pyObjArr); + code.iconst(my_with_count); + code.aaload(); getNone(); - int none_tmp = storeTop(); - - code.aload(exit_tmp); - code.aload(none_tmp); - code.aload(none_tmp); - code.aload(none_tmp); + code.dup(); + code.dup(); code.invokevirtual(Type.getType(PyObject.class).getInternalName(), call3.getName(), call3.getDescriptor()); code.pop(); code.label(label_end); + with_count--; - code.freeLocal(ts_tmp); - code.freeLocal(exc_tmp); - code.freeLocal(value_tmp); - code.freeLocal(exit_tmp); - code.freeLocal(mgr_tmp); - return null; } Modified: branches/asm/src/org/python/compiler/ScopeInfo.java =================================================================== --- branches/asm/src/org/python/compiler/ScopeInfo.java 2008-08-07 21:18:44 UTC (rev 5104) +++ branches/asm/src/org/python/compiler/ScopeInfo.java 2008-08-08 15:37:43 UTC (rev 5105) @@ -54,6 +54,7 @@ public boolean from_import_star; public boolean generator; public int yield_count; + public int max_with_count; public ArgListCompiler ac; Modified: branches/asm/src/org/python/compiler/ScopesCompiler.java =================================================================== --- branches/asm/src/org/python/compiler/ScopesCompiler.java 2008-08-07 21:18:44 UTC (rev 5104) +++ branches/asm/src/org/python/compiler/ScopesCompiler.java 2008-08-08 15:37:43 UTC (rev 5105) @@ -269,4 +269,11 @@ return null; } + public Object visitWith(With node) throws Exception { + cur.max_with_count++; + traverse(node); + + return null; + } + } Modified: branches/asm/src/org/python/core/PyFrame.java =================================================================== --- branches/asm/src/org/python/core/PyFrame.java 2008-08-07 21:18:44 UTC (rev 5104) +++ branches/asm/src/org/python/core/PyFrame.java 2008-08-08 15:37:43 UTC (rev 5105) @@ -41,6 +41,9 @@ private Object generatorInput = Py.None; + // with context exits - used by generated bytecode + public PyObject[] f_exits; + /** an interface to functions suitable for tracing, e.g. via * sys.settrace(). */ public TraceFunction tracefunc; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-08-07 21:18:46
|
Revision: 5104 http://jython.svn.sourceforge.net/jython/?rev=5104&view=rev Author: zyasoft Date: 2008-08-07 21:18:44 +0000 (Thu, 07 Aug 2008) Log Message: ----------- Replaces pkgutil.read_code with read_jython_code (diff args); this depends on the actual bytecode storage, so we have to have something specific here. get_loader special cases for the sys module, since Jython doesn't actually implement this as a module (and it appears to be a non-trivial decoupling to make it so with the current PySystemState). This fixes test_runpy. Modified Paths: -------------- branches/asm/Lib/pkgutil.py Modified: branches/asm/Lib/pkgutil.py =================================================================== --- branches/asm/Lib/pkgutil.py 2008-08-07 21:07:09 UTC (rev 5103) +++ branches/asm/Lib/pkgutil.py 2008-08-07 21:18:44 UTC (rev 5104) @@ -8,6 +8,7 @@ import imp import os.path from types import ModuleType +from org.python.core import imp as _imp, BytecodeLoader __all__ = [ 'get_importer', 'iter_importers', 'get_loader', 'find_loader', @@ -15,19 +16,15 @@ 'ImpImporter', 'ImpLoader', 'read_code', 'extend_path', ] -def read_code(stream): - # This helper is needed in order for the PEP 302 emulation to - # correctly handle compiled files - import marshal - magic = stream.read(4) - if magic != imp.get_magic(): - return None +# equivalent to CPythonLib's pkgutil.read_code except that we need +# diff args to pass into our underlying imp implementation, as +# accessed by _imp here - stream.read(4) # Skip timestamp - return marshal.load(stream) +def read_jython_code(fullname, file, filename): + data = _imp.unmarshalCode(filename, file, False) + return BytecodeLoader.makeCode(fullname + "$py", data, filename) - def simplegeneric(func): """Make a trivial single-dispatch generic function""" registry = {} @@ -276,7 +273,7 @@ elif mod_type==imp.PY_COMPILED: self._reopen() try: - self.code = read_code(self.file) + self.code = read_jython_code(fullname, self.file, self.filename) finally: self.file.close() elif mod_type==imp.PKG_DIRECTORY: @@ -451,6 +448,11 @@ if loader is not None: return loader fullname = module.__name__ + elif module_or_name == sys: + # Jython sys is not a real module; fake it here for now since + # making it a module requires a fair amount of decoupling from + # PySystemState + fullname = "sys" else: fullname = module_or_name return find_loader(fullname) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-08-07 21:07:11
|
Revision: 5103 http://jython.svn.sourceforge.net/jython/?rev=5103&view=rev Author: zyasoft Date: 2008-08-07 21:07:09 +0000 (Thu, 07 Aug 2008) Log Message: ----------- From http://svn.python.org/projects/python/branches/release25-maint/Lib/pkgutil.py@65582 Added Paths: ----------- branches/asm/Lib/pkgutil.py Added: branches/asm/Lib/pkgutil.py =================================================================== --- branches/asm/Lib/pkgutil.py (rev 0) +++ branches/asm/Lib/pkgutil.py 2008-08-07 21:07:09 UTC (rev 5103) @@ -0,0 +1,546 @@ +"""Utilities to support packages.""" + +# NOTE: This module must remain compatible with Python 2.3, as it is shared +# by setuptools for distribution with Python 2.3 and up. + +import os +import sys +import imp +import os.path +from types import ModuleType + +__all__ = [ + 'get_importer', 'iter_importers', 'get_loader', 'find_loader', + 'walk_packages', 'iter_modules', + 'ImpImporter', 'ImpLoader', 'read_code', 'extend_path', +] + +def read_code(stream): + # This helper is needed in order for the PEP 302 emulation to + # correctly handle compiled files + import marshal + + magic = stream.read(4) + if magic != imp.get_magic(): + return None + + stream.read(4) # Skip timestamp + return marshal.load(stream) + + +def simplegeneric(func): + """Make a trivial single-dispatch generic function""" + registry = {} + def wrapper(*args, **kw): + ob = args[0] + try: + cls = ob.__class__ + except AttributeError: + cls = type(ob) + try: + mro = cls.__mro__ + except AttributeError: + try: + class cls(cls, object): + pass + mro = cls.__mro__[1:] + except TypeError: + mro = object, # must be an ExtensionClass or some such :( + for t in mro: + if t in registry: + return registry[t](*args, **kw) + else: + return func(*args, **kw) + try: + wrapper.__name__ = func.__name__ + except (TypeError, AttributeError): + pass # Python 2.3 doesn't allow functions to be renamed + + def register(typ, func=None): + if func is None: + return lambda f: register(typ, f) + registry[typ] = func + return func + + wrapper.__dict__ = func.__dict__ + wrapper.__doc__ = func.__doc__ + wrapper.register = register + return wrapper + + +def walk_packages(path=None, prefix='', onerror=None): + """Yields (module_loader, name, ispkg) for all modules recursively + on path, or, if path is None, all accessible modules. + + 'path' should be either None or a list of paths to look for + modules in. + + 'prefix' is a string to output on the front of every module name + on output. + + Note that this function must import all *packages* (NOT all + modules!) on the given path, in order to access the __path__ + attribute to find submodules. + + 'onerror' is a function which gets called with one argument (the + name of the package which was being imported) if any exception + occurs while trying to import a package. If no onerror function is + supplied, ImportErrors are caught and ignored, while all other + exceptions are propagated, terminating the search. + + Examples: + + # list all modules python can access + walk_packages() + + # list all submodules of ctypes + walk_packages(ctypes.__path__, ctypes.__name__+'.') + """ + + def seen(p, m={}): + if p in m: + return True + m[p] = True + + for importer, name, ispkg in iter_modules(path, prefix): + yield importer, name, ispkg + + if ispkg: + try: + __import__(name) + except ImportError: + if onerror is not None: + onerror(name) + except Exception: + if onerror is not None: + onerror(name) + else: + raise + else: + path = getattr(sys.modules[name], '__path__', None) or [] + + # don't traverse path items we've seen before + path = [p for p in path if not seen(p)] + + for item in walk_packages(path, name+'.', onerror): + yield item + + +def iter_modules(path=None, prefix=''): + """Yields (module_loader, name, ispkg) for all submodules on path, + or, if path is None, all top-level modules on sys.path. + + 'path' should be either None or a list of paths to look for + modules in. + + 'prefix' is a string to output on the front of every module name + on output. + """ + + if path is None: + importers = iter_importers() + else: + importers = map(get_importer, path) + + yielded = {} + for i in importers: + for name, ispkg in iter_importer_modules(i, prefix): + if name not in yielded: + yielded[name] = 1 + yield i, name, ispkg + + +#@simplegeneric +def iter_importer_modules(importer, prefix=''): + if not hasattr(importer, 'iter_modules'): + return [] + return importer.iter_modules(prefix) + +iter_importer_modules = simplegeneric(iter_importer_modules) + + +class ImpImporter: + """PEP 302 Importer that wraps Python's "classic" import algorithm + + ImpImporter(dirname) produces a PEP 302 importer that searches that + directory. ImpImporter(None) produces a PEP 302 importer that searches + the current sys.path, plus any modules that are frozen or built-in. + + Note that ImpImporter does not currently support being used by placement + on sys.meta_path. + """ + + def __init__(self, path=None): + self.path = path + + def find_module(self, fullname, path=None): + # Note: we ignore 'path' argument since it is only used via meta_path + subname = fullname.split(".")[-1] + if subname != fullname and self.path is None: + return None + if self.path is None: + path = None + else: + path = [os.path.realpath(self.path)] + try: + file, filename, etc = imp.find_module(subname, path) + except ImportError: + return None + return ImpLoader(fullname, file, filename, etc) + + def iter_modules(self, prefix=''): + if self.path is None or not os.path.isdir(self.path): + return + + yielded = {} + import inspect + + filenames = os.listdir(self.path) + filenames.sort() # handle packages before same-named modules + + for fn in filenames: + modname = inspect.getmodulename(fn) + if modname=='__init__' or modname in yielded: + continue + + path = os.path.join(self.path, fn) + ispkg = False + + if not modname and os.path.isdir(path) and '.' not in fn: + modname = fn + for fn in os.listdir(path): + subname = inspect.getmodulename(fn) + if subname=='__init__': + ispkg = True + break + else: + continue # not a package + + if modname and '.' not in modname: + yielded[modname] = 1 + yield prefix + modname, ispkg + + +class ImpLoader: + """PEP 302 Loader that wraps Python's "classic" import algorithm + """ + code = source = None + + def __init__(self, fullname, file, filename, etc): + self.file = file + self.filename = filename + self.fullname = fullname + self.etc = etc + + def load_module(self, fullname): + self._reopen() + try: + mod = imp.load_module(fullname, self.file, self.filename, self.etc) + finally: + if self.file: + self.file.close() + # Note: we don't set __loader__ because we want the module to look + # normal; i.e. this is just a wrapper for standard import machinery + return mod + + def get_data(self, pathname): + return open(pathname, "rb").read() + + def _reopen(self): + if self.file and self.file.closed: + mod_type = self.etc[2] + if mod_type==imp.PY_SOURCE: + self.file = open(self.filename, 'rU') + elif mod_type in (imp.PY_COMPILED, imp.C_EXTENSION): + self.file = open(self.filename, 'rb') + + def _fix_name(self, fullname): + if fullname is None: + fullname = self.fullname + elif fullname != self.fullname: + raise ImportError("Loader for module %s cannot handle " + "module %s" % (self.fullname, fullname)) + return fullname + + def is_package(self, fullname): + fullname = self._fix_name(fullname) + return self.etc[2]==imp.PKG_DIRECTORY + + def get_code(self, fullname=None): + fullname = self._fix_name(fullname) + if self.code is None: + mod_type = self.etc[2] + if mod_type==imp.PY_SOURCE: + source = self.get_source(fullname) + self.code = compile(source, self.filename, 'exec') + elif mod_type==imp.PY_COMPILED: + self._reopen() + try: + self.code = read_code(self.file) + finally: + self.file.close() + elif mod_type==imp.PKG_DIRECTORY: + self.code = self._get_delegate().get_code() + return self.code + + def get_source(self, fullname=None): + fullname = self._fix_name(fullname) + if self.source is None: + mod_type = self.etc[2] + if mod_type==imp.PY_SOURCE: + self._reopen() + try: + self.source = self.file.read() + finally: + self.file.close() + elif mod_type==imp.PY_COMPILED: + if os.path.exists(self.filename[:-1]): + f = open(self.filename[:-1], 'rU') + self.source = f.read() + f.close() + elif mod_type==imp.PKG_DIRECTORY: + self.source = self._get_delegate().get_source() + return self.source + + + def _get_delegate(self): + return ImpImporter(self.filename).find_module('__init__') + + def get_filename(self, fullname=None): + fullname = self._fix_name(fullname) + mod_type = self.etc[2] + if self.etc[2]==imp.PKG_DIRECTORY: + return self._get_delegate().get_filename() + elif self.etc[2] in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION): + return self.filename + return None + + +try: + import zipimport + from zipimport import zipimporter + + def iter_zipimport_modules(importer, prefix=''): + dirlist = zipimport._zip_directory_cache[importer.archive].keys() + dirlist.sort() + _prefix = importer.prefix + plen = len(_prefix) + yielded = {} + import inspect + for fn in dirlist: + if not fn.startswith(_prefix): + continue + + fn = fn[plen:].split(os.sep) + + if len(fn)==2 and fn[1].startswith('__init__.py'): + if fn[0] not in yielded: + yielded[fn[0]] = 1 + yield fn[0], True + + if len(fn)!=1: + continue + + modname = inspect.getmodulename(fn[0]) + if modname=='__init__': + continue + + if modname and '.' not in modname and modname not in yielded: + yielded[modname] = 1 + yield prefix + modname, False + + iter_importer_modules.register(zipimporter, iter_zipimport_modules) + +except ImportError: + pass + + +def get_importer(path_item): + """Retrieve a PEP 302 importer for the given path item + + The returned importer is cached in sys.path_importer_cache + if it was newly created by a path hook. + + If there is no importer, a wrapper around the basic import + machinery is returned. This wrapper is never inserted into + the importer cache (None is inserted instead). + + The cache (or part of it) can be cleared manually if a + rescan of sys.path_hooks is necessary. + """ + try: + importer = sys.path_importer_cache[path_item] + except KeyError: + for path_hook in sys.path_hooks: + try: + importer = path_hook(path_item) + break + except ImportError: + pass + else: + importer = None + sys.path_importer_cache.setdefault(path_item, importer) + + if importer is None: + try: + importer = ImpImporter(path_item) + except ImportError: + importer = None + return importer + + +def iter_importers(fullname=""): + """Yield PEP 302 importers for the given module name + + If fullname contains a '.', the importers will be for the package + containing fullname, otherwise they will be importers for sys.meta_path, + sys.path, and Python's "classic" import machinery, in that order. If + the named module is in a package, that package is imported as a side + effect of invoking this function. + + Non PEP 302 mechanisms (e.g. the Windows registry) used by the + standard import machinery to find files in alternative locations + are partially supported, but are searched AFTER sys.path. Normally, + these locations are searched BEFORE sys.path, preventing sys.path + entries from shadowing them. + + For this to cause a visible difference in behaviour, there must + be a module or package name that is accessible via both sys.path + and one of the non PEP 302 file system mechanisms. In this case, + the emulation will find the former version, while the builtin + import mechanism will find the latter. + + Items of the following types can be affected by this discrepancy: + imp.C_EXTENSION, imp.PY_SOURCE, imp.PY_COMPILED, imp.PKG_DIRECTORY + """ + if fullname.startswith('.'): + raise ImportError("Relative module names not supported") + if '.' in fullname: + # Get the containing package's __path__ + pkg = '.'.join(fullname.split('.')[:-1]) + if pkg not in sys.modules: + __import__(pkg) + path = getattr(sys.modules[pkg], '__path__', None) or [] + else: + for importer in sys.meta_path: + yield importer + path = sys.path + for item in path: + yield get_importer(item) + if '.' not in fullname: + yield ImpImporter() + +def get_loader(module_or_name): + """Get a PEP 302 "loader" object for module_or_name + + If the module or package is accessible via the normal import + mechanism, a wrapper around the relevant part of that machinery + is returned. Returns None if the module cannot be found or imported. + If the named module is not already imported, its containing package + (if any) is imported, in order to establish the package __path__. + + This function uses iter_importers(), and is thus subject to the same + limitations regarding platform-specific special import locations such + as the Windows registry. + """ + if module_or_name in sys.modules: + module_or_name = sys.modules[module_or_name] + if isinstance(module_or_name, ModuleType): + module = module_or_name + loader = getattr(module, '__loader__', None) + if loader is not None: + return loader + fullname = module.__name__ + else: + fullname = module_or_name + return find_loader(fullname) + +def find_loader(fullname): + """Find a PEP 302 "loader" object for fullname + + If fullname contains dots, path must be the containing package's __path__. + Returns None if the module cannot be found or imported. This function uses + iter_importers(), and is thus subject to the same limitations regarding + platform-specific special import locations such as the Windows registry. + """ + for importer in iter_importers(fullname): + loader = importer.find_module(fullname) + if loader is not None: + return loader + + return None + + +def extend_path(path, name): + """Extend a package's path. + + Intended use is to place the following code in a package's __init__.py: + + from pkgutil import extend_path + __path__ = extend_path(__path__, __name__) + + This will add to the package's __path__ all subdirectories of + directories on sys.path named after the package. This is useful + if one wants to distribute different parts of a single logical + package as multiple directories. + + It also looks for *.pkg files beginning where * matches the name + argument. This feature is similar to *.pth files (see site.py), + except that it doesn't special-case lines starting with 'import'. + A *.pkg file is trusted at face value: apart from checking for + duplicates, all entries found in a *.pkg file are added to the + path, regardless of whether they are exist the filesystem. (This + is a feature.) + + If the input path is not a list (as is the case for frozen + packages) it is returned unchanged. The input path is not + modified; an extended copy is returned. Items are only appended + to the copy at the end. + + It is assumed that sys.path is a sequence. Items of sys.path that + are not (unicode or 8-bit) strings referring to existing + directories are ignored. Unicode items of sys.path that cause + errors when used as filenames may cause this function to raise an + exception (in line with os.path.isdir() behavior). + """ + + if not isinstance(path, list): + # This could happen e.g. when this is called from inside a + # frozen package. Return the path unchanged in that case. + return path + + pname = os.path.join(*name.split('.')) # Reconstitute as relative path + # Just in case os.extsep != '.' + sname = os.extsep.join(name.split('.')) + sname_pkg = sname + os.extsep + "pkg" + init_py = "__init__" + os.extsep + "py" + + path = path[:] # Start with a copy of the existing path + + for dir in sys.path: + if not isinstance(dir, basestring) or not os.path.isdir(dir): + continue + subdir = os.path.join(dir, pname) + # XXX This may still add duplicate entries to path on + # case-insensitive filesystems + initfile = os.path.join(subdir, init_py) + if subdir not in path and os.path.isfile(initfile): + path.append(subdir) + # XXX Is this the right thing for subpackages like zope.app? + # It looks for a file named "zope.app.pkg" + pkgfile = os.path.join(dir, sname_pkg) + if os.path.isfile(pkgfile): + try: + f = open(pkgfile) + except IOError, msg: + sys.stderr.write("Can't open %s: %s\n" % + (pkgfile, msg)) + else: + for line in f: + line = line.rstrip('\n') + if not line or line.startswith('#'): + continue + path.append(line) # Don't check for existence! + f.close() + + return path This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <nr...@us...> - 2008-08-07 17:11:21
|
Revision: 5102 http://jython.svn.sourceforge.net/jython/?rev=5102&view=rev Author: nriley Date: 2008-08-07 17:11:16 +0000 (Thu, 07 Aug 2008) Log Message: ----------- Don't attempt to test AWT if we have no display. Modified Paths: -------------- branches/asm/Lib/test/test_java_integration.py Modified: branches/asm/Lib/test/test_java_integration.py =================================================================== --- branches/asm/Lib/test/test_java_integration.py 2008-08-07 11:56:31 UTC (rev 5101) +++ branches/asm/Lib/test/test_java_integration.py 2008-08-07 17:11:16 UTC (rev 5102) @@ -4,9 +4,11 @@ import re from test import test_support -from java.awt import Dimension, Component, Rectangle, Button, Color +from java.awt import (Dimension, Component, Rectangle, Button, Color, + HeadlessException) from java.util import Vector, Hashtable -from java.io import FileOutputStream, FileWriter, OutputStreamWriter, UnsupportedEncodingException +from java.io import (FileOutputStream, FileWriter, OutputStreamWriter, + UnsupportedEncodingException) from java.lang import Runnable, Thread, ThreadGroup, System, Runtime, Math, Byte from javax.swing.tree import TreePath from java.math import BigDecimal @@ -325,8 +327,11 @@ class ButtonTest(unittest.TestCase): def test_setLabel(self): - b = Button() try: + b = Button() + except HeadlessException: + return # can't raise TestSkipped + try: b.setLabel = 4 except TypeError, e: self.failUnless("can't assign to this attribute in java instance: setLabel" in str(e)) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-07 11:56:34
|
Revision: 5101 http://jython.svn.sourceforge.net/jython/?rev=5101&view=rev Author: fwierzbicki Date: 2008-08-07 11:56:31 +0000 (Thu, 07 Aug 2008) Log Message: ----------- merge unified grammar with trunk. Modified Paths: -------------- trunk/sandbox/wierzbicki/backup/Python.g Modified: trunk/sandbox/wierzbicki/backup/Python.g =================================================================== --- trunk/sandbox/wierzbicki/backup/Python.g 2008-08-07 02:27:08 UTC (rev 5100) +++ trunk/sandbox/wierzbicki/backup/Python.g 2008-08-07 11:56:31 UTC (rev 5101) @@ -214,15 +214,15 @@ } @members { - //If you want to use antlr's default error recovery mechanisms change this - //and the same one in the lexer to true. - public boolean antlrErrorHandling = false; + boolean debugOn = false; - //XXX: only used for single_input -- seems kludgy. - public boolean inSingle = false; + private ErrorHandler errorHandler; + private boolean seenSingleOuterSuite = false; - boolean debugOn = false; + public void setErrorHandler(ErrorHandler eh) { + this.errorHandler = eh; + } private void debug(String message) { if (debugOn) { @@ -409,8 +409,8 @@ } private exprType makeAssignValue(List rhs) { - testlist_return r = (testlist_return)rhs.get(rhs.size() -1); - exprType value = (exprType)r.getTree(); + exprType value = (exprType)rhs.get(rhs.size() -1); + if (value instanceof Context) { //XXX: for Tuples, etc -- will need to recursively set to expr_contextType.Load. ((Context)value).setContext(expr_contextType.Load); @@ -453,12 +453,15 @@ keywordType[] makeKeywords(List args) { List<keywordType> k = new ArrayList<keywordType>(); - for(int i=0;i<args.size();i++) { - exprType[] e = (exprType[])args.get(i); - Name arg = (Name)e[0]; - k.add(new keywordType(arg, arg.id, e[1])); + if (args != null) { + for(int i=0;i<args.size();i++) { + exprType[] e = (exprType[])args.get(i); + Name arg = (Name)e[0]; + k.add(new keywordType(arg, arg.id, e[1])); + } + return k.toArray(new keywordType[k.size()]); } - return k.toArray(new keywordType[k.size()]); + return new keywordType[0]; } Object makeFloat(Token t) { @@ -574,23 +577,22 @@ } Token extractStringToken(List s) { - //XXX: really we want the *last* one. - return (Token)s.get(0); + return (Token)s.get(s.size() - 1); } protected void mismatch(IntStream input, int ttype, BitSet follow) throws RecognitionException { - if (antlrErrorHandling) { + if (errorHandler.isRecoverable()) { super.mismatch(input, ttype, follow); } else { throw new MismatchedTokenException(ttype, input); } } - protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) - throws RecognitionException - { - if (antlrErrorHandling) { + protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) + throws RecognitionException + { + if (errorHandler.isRecoverable()) { return super.recoverFromMismatchedToken(input, ttype, follow); } mismatch(input, ttype, follow); @@ -601,13 +603,9 @@ @rulecatch { catch (RecognitionException re) { - if (antlrErrorHandling) { - reportError(re); - recover(input,re); - retval.tree = (PythonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); - } else { - throw new ParseException(re); - } + errorHandler.reportError(this, re); + errorHandler.recover(this, input,re); + retval.tree = (PythonTree)adaptor.errorNode(input, retval.start, input.LT(-1), re); } } @@ -622,19 +620,23 @@ * 4] */ -//If you want to use antlr's default error recovery mechanisms change this -//and the same one in the parser to true. -public boolean antlrErrorHandling = false; +//If you want to use another error recovery mechanisms change this +//and the same one in the parser. +private ErrorHandler errorHandler; -//XXX: Hopefully we can remove inSingle when we get PyCF_DONT_IMPLY_DEDENT support. -public boolean inSingle = false; int implicitLineJoiningLevel = 0; int startPos=-1; + public void setErrorHandler(ErrorHandler eh) { + this.errorHandler = eh; + } + + /** + * Taken directly from antlr's Lexer.java -- needs to be re-integrated every time + * we upgrade from Antlr (need to consider a Lexer subclass, though the issue would + * remain). + */ public Token nextToken() { - if (antlrErrorHandling) { - return super.nextToken(); - } while (true) { state.token = null; state.channel = Token.DEFAULT_CHANNEL; @@ -654,16 +656,19 @@ continue; } return state.token; + } catch (NoViableAltException nva) { + errorHandler.reportError(this, nva); + errorHandler.recover(this, nva); // throw out current char and try again + } catch (RecognitionException re) { + errorHandler.reportError(this, re); + // match() routine has already called recover() } - catch (RecognitionException re) { - throw new ParseException(re); - } } } } //single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE -single_input : NEWLINE -> ^(Interactive) +single_input : NEWLINE? -> ^(Interactive) | simple_stmt -> ^(Interactive simple_stmt) | compound_stmt NEWLINE -> ^(Interactive compound_stmt) ; @@ -686,6 +691,44 @@ ) ; +//attr is here for Java compatibility. A Java foo.getIf() can be called from Jython as foo.if +// so we need to support any keyword as an attribute. + +attr + : NAME + | AND + | AS + | ASSERT + | BREAK + | CLASS + | CONTINUE + | DEF + | DELETE + | ELIF + | EXCEPT + | EXEC + | FINALLY + | FROM + | FOR + | GLOBAL + | IF + | IMPORT + | IN + | IS + | LAMBDA + | NOT + | OR + | ORELSE + | PASS + | PRINT + | RAISE + | RETURN + | TRY + | WHILE + | WITH + | YIELD + ; + //decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE decorator returns [exprType etype] @after { @@ -953,22 +996,19 @@ @after { $import_as_name.tree = $atype; } - : name=NAME (keyAS asname=NAME)? { + : name=NAME (AS asname=NAME)? { $atype = new aliasType($name, $name.text, $asname.text); } ; -//XXX: when does CPython Grammar match "dotted_name NAME NAME"? This may be a big -// problem because of the keyAS rule, which matches NAME (needed to allow -// 'as' to be a method name for Java integration). - +//XXX: when does CPython Grammar match "dotted_name NAME NAME"? //dotted_as_name: dotted_name [('as' | NAME) NAME] dotted_as_name returns [aliasType atype] @after { $dotted_as_name.tree = $atype; } - : dotted_name (keyAS NAME)? { + : dotted_name (AS NAME)? { $atype = new aliasType($NAME, $dotted_name.text, $NAME.text); } ; @@ -981,7 +1021,7 @@ ; //dotted_name: NAME ('.' NAME)* -dotted_name : NAME (DOT NAME)* +dotted_name : NAME (DOT attr)* ; //global_stmt: 'global' NAME (',' NAME)* @@ -994,7 +1034,7 @@ @after { $exec_stmt.tree = $stype; } - : keyEXEC expr[expr_contextType.Load] (IN t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? { + : EXEC expr[expr_contextType.Load] (IN t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? { $stype = new Exec($expr.start, (exprType)$expr.tree, (exprType)$t1.tree, (exprType)$t2.tree); } ; @@ -1075,7 +1115,7 @@ //with_var: ('as' | NAME) expr with_var returns [exprType etype] - : (keyAS | NAME) expr[expr_contextType.Load] { + : (AS | NAME) expr[expr_contextType.Load] { $etype = (exprType)$expr.tree; } ; @@ -1144,8 +1184,8 @@ | NOTEQUAL | IN | NOT IN -> NotIn - | 'is' - | 'is' NOT -> IsNot + | IS + | IS NOT -> IsNot ; //expr: xor_expr ('|' xor_expr)* @@ -1254,7 +1294,7 @@ | LONGINT -> ^(NumTok<Num>[$LONGINT, makeInt($LONGINT)]) | FLOAT -> ^(NumTok<Num>[$FLOAT, makeFloat($FLOAT)]) | COMPLEX -> ^(NumTok<Num>[$COMPLEX, makeComplex($COMPLEX)]) - | (S+=STRING)+ {debug("S+: " + $S);} + | (S+=STRING)+ -> ^(StrTok<Str>[extractStringToken($S), extractStrings($S)]) ; @@ -1294,7 +1334,7 @@ ) RPAREN | LBRACK s=subscriptlist RBRACK -> $s - | DOT^ NAME {debug("motched DOT^ NAME");} + | DOT^ attr {debug("motched DOT^ NAME");} ; //subscriptlist: subscript (',' subscript)* [','] @@ -1465,56 +1505,34 @@ //XXX: //testlist1: test (',' test)* -//These are all Python keywords that are not Java keywords -//This means that Jython needs to support these as NAMEs -//unlike CPython. For now I have only done this for 'as' -//and 'exec'. - -//keyAND : {input.LT(1).getText().equals("and")}? NAME ; -keyAS : {input.LT(1).getText().equals("as")}? NAME ; -//keyDEF : {input.LT(1).getText().equals("def")}? NAME ; -//keyDEL : {input.LT(1).getText().equals("del")}? NAME ; -//keyELIF : {input.LT(1).getText().equals("elif")}? NAME ; -//keyEXCEPT : {input.LT(1).getText().equals("except")}? NAME ; -keyEXEC : {input.LT(1).getText().equals("exec")}? NAME ; -//keyFROM : {input.LT(1).getText().equals("from")}? NAME ; -//keyGLOBAL : {input.LT(1).getText().equals("global")}? NAME ; -//keyIN : {input.LT(1).getText().equals("in")}? NAME ; -//keyIS : {input.LT(1).getText().equals("is")}? NAME ; -//keyLAMBDA : {input.LT(1).getText().equals("lambda")}? NAME ; -//keyNOT : {input.LT(1).getText().equals("not")}? NAME ; -//keyOR : {input.LT(1).getText().equals("or")}? NAME ; -//keyPASS : {input.LT(1).getText().equals("pass")}? NAME ; -//keyPRINT : {input.LT(1).getText().equals("print")}? NAME ; -//keyRAISE : {input.LT(1).getText().equals("raise")}? NAME ; -//keyWITH : {input.LT(1).getText().equals("with")}? NAME ; -//keyYIELD : {input.LT(1).getText().equals("yield")}? NAME ; - -DEF : 'def' ; +AS : 'as' ; +ASSERT : 'assert' ; +BREAK : 'break' ; CLASS : 'class' ; -PRINT : 'print' ; -BREAK : 'break' ; CONTINUE : 'continue' ; -RETURN : 'return' ; -RAISE : 'raise' ; -PASS : 'pass' ; -IMPORT : 'import' ; +DEF : 'def' ; +DELETE : 'del' ; +ELIF : 'elif' ; +EXCEPT : 'except' ; +EXEC : 'exec' ; +FINALLY : 'finally' ; FROM : 'from' ; FOR : 'for' ; +GLOBAL : 'global' ; +IF : 'if' ; +IMPORT : 'import' ; +IN : 'in' ; +IS : 'is' ; +LAMBDA : 'lambda' ; ORELSE : 'else' ; -ELIF : 'elif' ; -IN : 'in' ; -IF : 'if' ; +PASS : 'pass' ; +PRINT : 'print' ; +RAISE : 'raise' ; +RETURN : 'return' ; +TRY : 'try' ; WHILE : 'while' ; WITH : 'with' ; -LAMBDA : 'lambda' ; -GLOBAL : 'global' ; YIELD : 'yield' ; -ASSERT : 'assert' ; -FINALLY : 'finally' ; -DELETE : 'del' ; -EXCEPT : 'except' ; -TRY : 'try' ; LPAREN : '(' {implicitLineJoiningLevel++;} ; @@ -1655,7 +1673,12 @@ | '"""' (options {greedy=false;}:TRIQUOTE)* '"""' | '"' (ESC|~('\\'|'\n'|'"'))* '"' | '\'' (ESC|~('\\'|'\n'|'\''))* '\'' - ) + ) { + if (state.tokenStartLine != input.getLine()) { + state.tokenStartLine = input.getLine(); + state.tokenStartCharPositionInLine = -2; + } + } ; /** the two '"'? cause a warning -- is there a way to avoid that? */ @@ -1681,7 +1704,7 @@ */ CONTINUED_LINE : '\\' ('\r')? '\n' (' '|'\t')* { $channel=HIDDEN; } - ( nl=NEWLINE {emit(new ClassicToken(NEWLINE,nl.getText()));} + ( nl=NEWLINE {emit(new CommonToken(NEWLINE,nl.getText()));} | ) ; @@ -1693,12 +1716,11 @@ * Frank Wierzbicki added: Also ignore FORMFEEDS (\u000C). */ NEWLINE - : {inSingle}? => (('\u000C')?('\r')? '\n' ) - {if (implicitLineJoiningLevel>0 ) - $channel=HIDDEN; - } - | (('\u000C')?('\r')? '\n' )+ - {if ( startPos==0 || implicitLineJoiningLevel>0 ) +@init { + int newlines = 0; +} + : (('\u000C')?('\r')? '\n' {newlines++; } )+ { + if ( startPos==0 || implicitLineJoiningLevel>0 ) $channel=HIDDEN; } ; @@ -1715,24 +1737,42 @@ LEADING_WS @init { int spaces = 0; + int newlines = 0; } : {startPos==0}?=> ( {implicitLineJoiningLevel>0}? ( ' ' | '\t' )+ {$channel=HIDDEN;} - | ( ' ' { spaces++; } - | '\t' { spaces += 8; spaces -= (spaces \% 8); } - )+ - { - // make a string of n spaces where n is column number - 1 - char[] indentation = new char[spaces]; - for (int i=0; i<spaces; i++) { - indentation[i] = ' '; - } - String s = new String(indentation); - emit(new ClassicToken(LEADING_WS,new String(indentation))); - } - // kill trailing newline if present and then ignore - ( ('\r')? '\n' {if (state.token!=null) state.token.setChannel(HIDDEN); else $channel=HIDDEN;})* - // {state.token.setChannel(99); } + | ( ' ' { spaces++; } + | '\t' { spaces += 8; spaces -= (spaces \% 8); } + )+ + ( ('\r')? '\n' {newlines++; } + )* { + if (input.LA(1) != -1) { + // make a string of n spaces where n is column number - 1 + char[] indentation = new char[spaces]; + for (int i=0; i<spaces; i++) { + indentation[i] = ' '; + } + CommonToken c = new CommonToken(LEADING_WS,new String(indentation)); + c.setLine(input.getLine()); + c.setCharPositionInLine(input.getCharPositionInLine()); + emit(c); + // kill trailing newline if present and then ignore + if (newlines != 0) { + if (state.token!=null) { + state.token.setChannel(HIDDEN); + } else { + $channel=HIDDEN; + } + } + } else { + // make a string of n newlines + char[] nls = new char[newlines]; + for (int i=0; i<newlines; i++) { + nls[i] = '\n'; + } + emit(new CommonToken(NEWLINE,new String(nls))); + } + } ) ; @@ -1758,6 +1798,6 @@ $channel=HIDDEN; } : {startPos==0}?=> (' '|'\t')* '#' (~'\n')* '\n'+ - | {startPos>0}?=> '#' (~'\n')* // let NEWLINE handle \n unless char pos==0 for '#' + | '#' (~'\n')* // let NEWLINE handle \n unless char pos==0 for '#' ; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <le...@us...> - 2008-08-07 02:27:11
|
Revision: 5100 http://jython.svn.sourceforge.net/jython/?rev=5100&view=rev Author: leosoto Date: 2008-08-07 02:27:08 +0000 (Thu, 07 Aug 2008) Log Message: ----------- Experimental interpreter restarting support, aimed at webapp dev-server 'reloading', without having to resort to process forking. There is more info here: <http://codereview.appspot.com/2810>, where I posted the first attempt of this patch Modified Paths: -------------- branches/asm/Lib/threading.py branches/asm/src/org/python/core/FunctionThread.java branches/asm/src/org/python/core/PyTableCode.java branches/asm/src/org/python/modules/Setup.java branches/asm/src/org/python/modules/thread/thread.java branches/asm/src/org/python/util/jython.java Added Paths: ----------- branches/asm/src/org/python/modules/_systemrestart.java Modified: branches/asm/Lib/threading.py =================================================================== --- branches/asm/Lib/threading.py 2008-08-06 23:51:03 UTC (rev 5099) +++ branches/asm/Lib/threading.py 2008-08-07 02:27:08 UTC (rev 5100) @@ -1,6 +1,6 @@ from java.util.concurrent import Semaphore, CyclicBarrier from java.util.concurrent.locks import ReentrantLock -from org.python.core import FunctionThread +from thread import _newFunctionThread from thread import _local as local import java.lang.Thread import weakref @@ -227,7 +227,7 @@ _threads[_thread.getId()] = self def _create_thread(self): - return FunctionThread(self.__bootstrap, ()) + return _newFunctionThread(self.__bootstrap, ()) def run(self): if self._target: Modified: branches/asm/src/org/python/core/FunctionThread.java =================================================================== --- branches/asm/src/org/python/core/FunctionThread.java 2008-08-06 23:51:03 UTC (rev 5099) +++ branches/asm/src/org/python/core/FunctionThread.java 2008-08-07 02:27:08 UTC (rev 5100) @@ -1,31 +1,27 @@ package org.python.core; - + +import org.python.modules._systemrestart; + public class FunctionThread extends Thread { private final PyObject func; private final PyObject[] args; private final PySystemState systemState; - public FunctionThread(PyObject func, PyObject[] args) { - super(); + public FunctionThread(PyObject func, PyObject[] args, long stack_size, ThreadGroup group) { + super(group, null, "Thread", stack_size); this.func = func; this.args = args; this.systemState = Py.getSystemState(); } - - public FunctionThread(PyObject func, PyObject[] args, long stack_size) { - super(null, null, "Thread", stack_size); - this.func = func; - this.args = args; - this.systemState = Py.getSystemState(); - } public void run() { Py.setSystemState(systemState); try { func.__call__(args); } catch (PyException exc) { - if (Py.matchException(exc, Py.SystemExit)) { + if (Py.matchException(exc, Py.SystemExit) || + Py.matchException(exc, _systemrestart.SystemRestart)) { return; } Py.stderr.println("Unhandled exception in thread started by " + func); Modified: branches/asm/src/org/python/core/PyTableCode.java =================================================================== --- branches/asm/src/org/python/core/PyTableCode.java 2008-08-06 23:51:03 UTC (rev 5099) +++ branches/asm/src/org/python/core/PyTableCode.java 2008-08-07 02:27:08 UTC (rev 5100) @@ -1,6 +1,8 @@ // Copyright (c) Corporation for National Research Initiatives package org.python.core; +import org.python.modules._systemrestart; + /** * An implementation of PyCode where the actual executable content * is stored as a PyFunctionTable instance and an integer index. @@ -226,6 +228,12 @@ ts.exception = previous_exception; ts.frame = ts.frame.f_back; + + // Check for interruption, which is used for restarting the interpreter + // on Jython + if (Thread.currentThread().isInterrupted()) { + throw new PyException(_systemrestart.SystemRestart); + } return ret; } Modified: branches/asm/src/org/python/modules/Setup.java =================================================================== --- branches/asm/src/org/python/modules/Setup.java 2008-08-06 23:51:03 UTC (rev 5099) +++ branches/asm/src/org/python/modules/Setup.java 2008-08-07 02:27:08 UTC (rev 5100) @@ -54,6 +54,7 @@ "gc", "_hashlib", "_functools:org.python.modules._functools._functools", - "_csv:org.python.modules._csv._csv" + "_csv:org.python.modules._csv._csv", + "_systemrestart" }; } Added: branches/asm/src/org/python/modules/_systemrestart.java =================================================================== --- branches/asm/src/org/python/modules/_systemrestart.java (rev 0) +++ branches/asm/src/org/python/modules/_systemrestart.java 2008-08-07 02:27:08 UTC (rev 5100) @@ -0,0 +1,29 @@ +package org.python.modules; + +import org.python.core.ClassDictInit; +import org.python.core.Py; +import org.python.core.PyException; +import org.python.core.PyObject; +import org.python.core.PyStringMap; + +public class _systemrestart implements ClassDictInit { + /** + * Jython-specific exception for restarting the interpreter. Currently + * supported only by jython.java, when executing a file (i.e, + * non-interactive mode). + * + * WARNING: This is highly *experimental* and subject to change. + */ + public static PyObject SystemRestart; + + public static void classDictInit(PyObject dict) { + SystemRestart = Py.makeClass( + "_systemrestart.SystemRestart", Py.BaseException, + new PyStringMap() {{ + __setitem__("__doc__", + Py.newString("Request to restart the interpreter. " + + "(Jython-specific)")); + }}); + dict.__delitem__("classDictInit"); + } +} Modified: branches/asm/src/org/python/modules/thread/thread.java =================================================================== --- branches/asm/src/org/python/modules/thread/thread.java 2008-08-06 23:51:03 UTC (rev 5099) +++ branches/asm/src/org/python/modules/thread/thread.java 2008-08-07 02:27:08 UTC (rev 5100) @@ -8,13 +8,16 @@ 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.core.PyTuple; public class thread implements ClassDictInit { private static volatile long stack_size = 0; // XXX - can we figure out the current stack size? - + private static ThreadGroup group = new ThreadGroup("jython-threads"); + + public static PyString __doc__ = new PyString( "This module provides primitive operations to write multi-threaded "+ "programs.\n" + @@ -24,12 +27,13 @@ public static void classDictInit(PyObject dict) { dict.__setitem__("LockType", PyType.fromClass(PyLock.class)); dict.__setitem__("_local", PyLocal.TYPE); + dict.__setitem__("interruptAllThreads", null); } public static PyObject error = new PyString("thread.error"); public static void start_new_thread(PyObject func, PyTuple args) { - Thread pt = new FunctionThread(func, args.getArray(), stack_size); + Thread pt = _newFunctionThread(func, args); PyObject currentThread = func.__findattr__("im_self"); if (currentThread != null) { PyObject isDaemon = currentThread.__findattr__("isDaemon"); @@ -44,8 +48,36 @@ } } pt.start(); - } + } + /** + * Initializes a {@link FunctionThread}, using the configured stack_size and + * registering the thread in the @link {@link #group} of threads spawned by + * the thread module. + * + * Also used from the threading.py module. + */ + public static FunctionThread _newFunctionThread(PyObject func, PyTuple args) { + return new FunctionThread(func, args.getArray(), stack_size, group); + } + + /** + * Interrupts all running threads spawned by the thread module. + * + * This works in conjuntion with:<ul> + * <li>{@link PyTableCode#call(org.python.core.PyFrame, PyObject)}: + * checks for the interrupted status of the current thread and raise + * a SystemRestart exception if a interruption is detected.</li> + * <li>{@link FunctionThread#run()}: exits the current thread when a + * SystemRestart exception is not caught.</li> + * + * Thus, it is possible that this doesn't make all running threads to stop, + * if SystemRestart exception is caught. + */ + public static void interruptAllThreads() { + group.interrupt(); + } + public static PyLock allocate_lock() { return new PyLock(); } @@ -81,4 +113,4 @@ throw Py.TypeError("stack_size() takes at most 1 argument (" + args.length + "given)"); } } -} \ No newline at end of file +} Modified: branches/asm/src/org/python/util/jython.java =================================================================== --- branches/asm/src/org/python/util/jython.java 2008-08-06 23:51:03 UTC (rev 5099) +++ branches/asm/src/org/python/util/jython.java 2008-08-07 02:27:08 UTC (rev 5100) @@ -15,12 +15,13 @@ import org.python.core.PyException; import org.python.core.PyFile; import org.python.core.PyModule; -import org.python.core.PyObject; import org.python.core.PyString; import org.python.core.PyStringMap; import org.python.core.PySystemState; import org.python.core.imp; import org.python.core.util.RelativeFile; +import org.python.modules._systemrestart; +import org.python.modules.thread.thread; public class jython { @@ -59,6 +60,8 @@ "JYTHONPATH: '" + java.io.File.pathSeparator + "'-separated list of directories prefixed to the default module\n" + " search path. The result is sys.path."; + private static boolean shouldRestart; + public static void runJar(String filename) { // TBD: this is kind of gross because a local called `zipfile' just // magically shows up in the module's globals. Either `zipfile' @@ -102,6 +105,13 @@ } public static void main(String[] args) { + do { + shouldRestart = false; + run(args); + } while (shouldRestart); + } + + public static void run(String[] args) { // Parse the command line options CommandLineOptions opts = new CommandLineOptions(); if (!opts.parse(args)) { @@ -120,22 +130,8 @@ opts.properties, opts.argv); // Now create an interpreter - InteractiveConsole interp = null; - try { - String interpClass = PySystemState.registry.getProperty( - "python.console", - "org.python.util.InteractiveConsole"); - interp = (InteractiveConsole) - Class.forName(interpClass).newInstance(); - } catch (Exception e) { - interp = new InteractiveConsole(); - } + InteractiveConsole interp = newInterpreter(); - //System.err.println("interp"); - PyModule mod = imp.addModule("__main__"); - interp.setLocals(mod.__dict__); - //System.err.println("imp"); - for (int i = 0; i < opts.warnoptions.size(); i++) { String wopt = (String) opts.warnoptions.elementAt(i); PySystemState.warnoptions.append(new PyString(wopt)); @@ -224,10 +220,20 @@ interp.execfile(file, opts.filename); } } catch(Throwable t) { - Py.printException(t); - if (!opts.interactive) { - interp.cleanup(); - System.exit(-1); + if (t instanceof PyException && + Py.matchException((PyException)t, _systemrestart.SystemRestart)) { + // Stop current threads... + thread.interruptAllThreads(); + // ..reset the state... + Py.setSystemState(new PySystemState()); + // ...and start again + shouldRestart = true; + } else { + Py.printException(t); + if (!opts.interactive) { + interp.cleanup(); + System.exit(-1); + } } } } @@ -273,8 +279,31 @@ System.exit(0); } } + + /** + * @return a new python interpreter, instantiating the right + * InteractiveConsole subclass for the configured <tt>python.console</tt> + */ + private static InteractiveConsole newInterpreter() { + InteractiveConsole interp = null; + try { + String interpClass = PySystemState.registry.getProperty( + "python.console", + "org.python.util.InteractiveConsole"); + interp = (InteractiveConsole) + Class.forName(interpClass).newInstance(); + } catch (Exception e) { + interp = new InteractiveConsole(); } + //System.err.println("interp"); + PyModule mod = imp.addModule("__main__"); + interp.setLocals(mod.__dict__); + //System.err.println("imp"); + return interp; + } +} + class CommandLineOptions { public String filename; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-06 23:51:05
|
Revision: 5099 http://jython.svn.sourceforge.net/jython/?rev=5099&view=rev Author: fwierzbicki Date: 2008-08-06 23:51:03 +0000 (Wed, 06 Aug 2008) Log Message: ----------- Putting in fake SystemRandom that throws NotImplementedError on all accesses so the rest of test_random.py can run (and fail for better reasons). Modified Paths: -------------- branches/asm/Lib/random.py Modified: branches/asm/Lib/random.py =================================================================== --- branches/asm/Lib/random.py 2008-08-06 19:35:41 UTC (rev 5098) +++ branches/asm/Lib/random.py 2008-08-06 23:51:03 UTC (rev 5099) @@ -765,7 +765,27 @@ y = (y + a) % 256 or 1 z = (z + a) % 256 or 1 self.__whseed(x, y, z) +## --------------- Operating System Random Source ------------------ +class SystemRandom(Random): + """ + XXX: throw NotImplementedError for any attemt to use this for now. + """ + + def random(self): + self._notimplemented() + + def getrandbits(self, k): + self._notimplemented() + + def _stub(self, *args, **kwds): + self._notimplemented() + + def _notimplemented(self, *args, **kwds): + raise NotImplementedError('SystemRandom not implemented on Jython.') + getstate = setstate = _notimplemented + + ## -------------------- test program -------------------- def _test_generator(n, funccall): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-06 19:35:46
|
Revision: 5098 http://jython.svn.sourceforge.net/jython/?rev=5098&view=rev Author: fwierzbicki Date: 2008-08-06 19:35:41 +0000 (Wed, 06 Aug 2008) Log Message: ----------- Line and column position for from __future__ import braces Also fixed from __future__ import GIL Good thing there are unit tests for this :) Modified Paths: -------------- branches/asm/src/org/python/compiler/Future.java Modified: branches/asm/src/org/python/compiler/Future.java =================================================================== --- branches/asm/src/org/python/compiler/Future.java 2008-08-06 19:05:52 UTC (rev 5097) +++ branches/asm/src/org/python/compiler/Future.java 2008-08-06 19:35:41 UTC (rev 5098) @@ -44,10 +44,10 @@ continue; } if (feature.equals("braces")) { - throw new ParseException("not a chance"); + throw new ParseException("not a chance", cand); } if (feature.equals("GIL") || feature.equals("global_interpreter_lock")) { - throw new ParseException("Never going to happen!"); + throw new ParseException("Never going to happen!", cand); } throw new ParseException("future feature "+feature+ " is not defined",cand); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-08-06 19:05:55
|
Revision: 5097 http://jython.svn.sourceforge.net/jython/?rev=5097&view=rev Author: fwierzbicki Date: 2008-08-06 19:05:52 +0000 (Wed, 06 Aug 2008) Log Message: ----------- More thorough keyword testing. Modified Paths: -------------- branches/asm/Lib/test/test_java_integration.py Modified: branches/asm/Lib/test/test_java_integration.py =================================================================== --- branches/asm/Lib/test/test_java_integration.py 2008-08-06 18:45:50 UTC (rev 5096) +++ branches/asm/Lib/test/test_java_integration.py 2008-08-06 19:05:52 UTC (rev 5097) @@ -169,54 +169,151 @@ ht.put("a", fv) self.failUnless(fv is ht.get("a")) -class ReservedNamesTest(unittest.TestCase): - "Access to java names which are al reserved words" +class JavaReservedNamesTest(unittest.TestCase): + "Access to reserved words" - def test_in(self): + def test_system_in(self): s = System.in self.assert_("java.io.BufferedInputStream" in str(s)) - - def test_exec(self): + + def test_runtime_exec(self): e = Runtime.getRuntime().exec self.assert_(re.search("method .*exec", str(e)) is not None) - - def test_class(self): + + def test_byte_class(self): b = Byte(10) self.assert_("java.lang.Byte" in str(b.class)) +class Keywords(object): + pass + +Keywords.in = lambda self: "in" +Keywords.exec = lambda self: "exec" +Keywords.class = lambda self: "class" +Keywords.print = lambda self: "print" +Keywords.and = lambda self: "and" +Keywords.as = lambda self: "as" +Keywords.assert = lambda self: "assert" +Keywords.break = lambda self: "break" +Keywords.continue = lambda self: "continue" +Keywords.def = lambda self: "def" +Keywords.del = lambda self: "del" +Keywords.elif = lambda self: "elif" +Keywords.else = lambda self: "else" +Keywords.except = lambda self: "except" +Keywords.finally = lambda self: "finally" +Keywords.from = lambda self: "from" +Keywords.for = lambda self: "for" +Keywords.global = lambda self: "global" +Keywords.if = lambda self: "if" +Keywords.import = lambda self: "import" +Keywords.is = lambda self: "is" +Keywords.lambda = lambda self: "lambda" +Keywords.pass = lambda self: "pass" +Keywords.print = lambda self: "print" +Keywords.raise = lambda self: "raise" +Keywords.return = lambda self: "return" +Keywords.try = lambda self: "try" +Keywords.while = lambda self: "while" +Keywords.with = lambda self: "with" +Keywords.yield = lambda self: "yield" + +class PyReservedNamesTest(unittest.TestCase): + "Access to reserved words" + + def setUp(self): + self.kws = Keywords() + + def test_in(self): + self.assertEquals(self.kws.in(), "in") + + def test_exec(self): + self.assertEquals(self.kws.exec(), "exec") + + def test_class(self): + self.assertEquals(self.kws.class(), "class") + def test_print(self): - #XXX should find a better print test. - System.out.print("") + self.assertEquals(self.kws.print(), "print") - #TODO: - #and - #as - #assert - #break - #continue - #def - #del - #elif - #else - #except - #finally - #from - #for - #global - #if - #import - #is - #lambda - #pass - #print - #raise - #return - #try - #while - #with - #yield + def test_and(self): + self.assertEquals(self.kws.and(), "and") + def test_as(self): + self.assertEquals(self.kws.as(), "as") + def test_assert(self): + self.assertEquals(self.kws.assert(), "assert") + + def test_break(self): + self.assertEquals(self.kws.break(), "break") + + def test_continue(self): + self.assertEquals(self.kws.continue(), "continue") + + def test_def(self): + self.assertEquals(self.kws.def(), "def") + + def test_del(self): + self.assertEquals(self.kws.del(), "del") + + def test_elif(self): + self.assertEquals(self.kws.elif(), "elif") + + def test_else(self): + self.assertEquals(self.kws.else(), "else") + + def test_except(self): + self.assertEquals(self.kws.except(), "except") + + def test_finally(self): + self.assertEquals(self.kws.finally(), "finally") + + def test_from(self): + self.assertEquals(self.kws.from(), "from") + + def test_for(self): + self.assertEquals(self.kws.for(), "for") + + def test_global(self): + self.assertEquals(self.kws.global(), "global") + + def test_if(self): + self.assertEquals(self.kws.if(), "if") + + def test_import(self): + self.assertEquals(self.kws.import(), "import") + + def test_is(self): + self.assertEquals(self.kws.is(), "is") + + def test_lambda(self): + self.assertEquals(self.kws.lambda(), "lambda") + + def test_pass(self): + self.assertEquals(self.kws.pass(), "pass") + + def test_print(self): + self.assertEquals(self.kws.print(), "print") + + def test_raise(self): + self.assertEquals(self.kws.raise(), "raise") + + def test_return(self): + self.assertEquals(self.kws.return(), "return") + + def test_try(self): + self.assertEquals(self.kws.try(), "try") + + def test_while(self): + self.assertEquals(self.kws.while(), "while") + + def test_with(self): + self.assertEquals(self.kws.with(), "with") + + def test_yield(self): + self.assertEquals(self.kws.yield(), "yield") + class ImportTest(unittest.TestCase): def test_bad_input_exception(self): @@ -296,7 +393,8 @@ PyObjectCmpTest, IOTest, VectorTest, - ReservedNamesTest, + JavaReservedNamesTest, + PyReservedNamesTest, ImportTest, ButtonTest, ColorTest, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |