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: <pj...@us...> - 2008-10-11 23:17:03
|
Revision: 5375 http://jython.svn.sourceforge.net/jython/?rev=5375&view=rev Author: pjenvey Date: 2008-10-11 23:16:54 +0000 (Sat, 11 Oct 2008) Log Message: ----------- fix unicodedata.lookup to return unicode instead of the codepoint int Modified Paths: -------------- trunk/jython/Lib/unicodedata.py Modified: trunk/jython/Lib/unicodedata.py =================================================================== --- trunk/jython/Lib/unicodedata.py 2008-10-11 21:29:29 UTC (rev 5374) +++ trunk/jython/Lib/unicodedata.py 2008-10-11 23:16:54 UTC (rev 5375) @@ -56,7 +56,7 @@ elif name.find('Last') >= 0: _segments.append((start, (start, codepoint), data)) else: - _names[name] = codepoint + _names[name] = unichr(codepoint) _codepoints[codepoint] = data def init_east_asian_width(path): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-10-11 21:29:38
|
Revision: 5374 http://jython.svn.sourceforge.net/jython/?rev=5374&view=rev Author: fwierzbicki Date: 2008-10-11 21:29:29 +0000 (Sat, 11 Oct 2008) Log Message: ----------- A very basic start to supporting pep 328 style relative imports like from . import foo This patch just passes the "level" (that is number of dots) information from the compile to the import machinary without actually using it, and forbids from . import * As Python 2.5 does. Modified Paths: -------------- trunk/jython/src/org/python/compiler/CodeCompiler.java trunk/jython/src/org/python/core/imp.java Modified: trunk/jython/src/org/python/compiler/CodeCompiler.java =================================================================== --- trunk/jython/src/org/python/compiler/CodeCompiler.java 2008-10-11 20:54:47 UTC (rev 5373) +++ trunk/jython/src/org/python/compiler/CodeCompiler.java 2008-10-11 21:29:29 UTC (rev 5374) @@ -768,6 +768,9 @@ code.ldc(node.module); //Note: parser does not allow node.names.length == 0 if (node.names.length == 1 && node.names[0].name.equals("*")) { + if (node.level > 0) { + throw new ParseException("'import *' not allowed with 'from .'", node); + } if (my_scope.func_level > 0) { module.error("import * only allowed at module level", false, node); @@ -799,7 +802,8 @@ makeStrings(code, fromNames, fromNames.length); loadFrame(); - code.invokestatic("org/python/core/imp", "importFrom", "(" + $str + $strArr + $pyFrame + ")" + $pyObjArr); + code.iconst(node.level); + code.invokestatic("org/python/core/imp", "importFrom", "(" + $str + $strArr + $pyFrame + "I" + ")" + $pyObjArr); int tmp = storeTop(); for (int i = 0; i < node.names.length; i++) { code.aload(tmp); Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2008-10-11 20:54:47 UTC (rev 5373) +++ trunk/jython/src/org/python/core/imp.java 2008-10-11 21:29:29 UTC (rev 5374) @@ -753,12 +753,30 @@ } /** + * replaced by importFrom with level param. Kept for backwards compatibility. + * @deprecated use importFrom with level param. + */ + public static PyObject[] importFrom(String mod, String[] names, + PyFrame frame) { + return importFromAs(mod, names, null, frame, 0); + } + + /** * Called from jython generated code when a statement like "from spam.eggs * import foo, bar" is executed. */ public static PyObject[] importFrom(String mod, String[] names, + PyFrame frame, int level) { + return importFromAs(mod, names, null, frame, level); + } + + /** + * replaced by importFromAs with level param. Kept for backwards compatibility. + * @deprecated use importFromAs with level param. + */ + public static PyObject[] importFromAs(String mod, String[] names, PyFrame frame) { - return importFromAs(mod, names, null, frame); + return importFromAs(mod, names, null, frame, 0); } /** @@ -766,7 +784,7 @@ * import foo as spam" is executed. */ public static PyObject[] importFromAs(String mod, String[] names, - String[] asnames, PyFrame frame) { + String[] asnames, PyFrame frame, int level) { PyObject[] pyNames = new PyObject[names.length]; for (int i = 0; i < names.length; i++) { pyNames[i] = Py.newString(names[i]); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-10-11 20:54:55
|
Revision: 5373 http://jython.svn.sourceforge.net/jython/?rev=5373&view=rev Author: fwierzbicki Date: 2008-10-11 20:54:47 +0000 (Sat, 11 Oct 2008) Log Message: ----------- Throw a Python exception for two internal compiler errors that where just printing a less useful error message and going on to an NPE. Modified Paths: -------------- trunk/jython/src/org/python/compiler/CodeCompiler.java Modified: trunk/jython/src/org/python/compiler/CodeCompiler.java =================================================================== --- trunk/jython/src/org/python/compiler/CodeCompiler.java 2008-10-11 06:42:35 UTC (rev 5372) +++ trunk/jython/src/org/python/compiler/CodeCompiler.java 2008-10-11 20:54:47 UTC (rev 5373) @@ -1966,7 +1966,7 @@ code.invokevirtual("org/python/core/PyFrame", "setlocal", "(" + $str + $pyObj + ")V"); } else { if (syminf == null) { - System.err.println("internal compiler error: "+node); + throw new ParseException("internal compiler error", node); } if ((syminf.flags&ScopeInfo.CELL) != 0) { code.iconst(syminf.env_index); @@ -1991,7 +1991,7 @@ code.invokevirtual("org/python/core/PyFrame", "dellocal", "(" + $str + ")V"); } else { if (syminf == null) { - System.err.println("internal compiler error: "+node); + throw new ParseException("internal compiler error", node); } if ((syminf.flags&ScopeInfo.CELL) != 0) { module.error("can not delete variable '"+name+ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-10-11 06:42:42
|
Revision: 5372 http://jython.svn.sourceforge.net/jython/?rev=5372&view=rev Author: pjenvey Date: 2008-10-11 06:42:35 +0000 (Sat, 11 Oct 2008) Log Message: ----------- fix solid_base potentially resolving the wrong type when called before the mro was initialized. caused best_base to blowup with a TypeError Modified Paths: -------------- trunk/jython/Lib/test/test_class_jy.py trunk/jython/src/org/python/core/PyType.java Modified: trunk/jython/Lib/test/test_class_jy.py =================================================================== --- trunk/jython/Lib/test/test_class_jy.py 2008-10-11 05:08:23 UTC (rev 5371) +++ trunk/jython/Lib/test/test_class_jy.py 2008-10-11 06:42:35 UTC (rev 5372) @@ -152,7 +152,17 @@ keys.sort() self.assertEqual(str(keys), "['__doc__', '__module__', 'moo']") + def test_metaclass_and_slotted_base(self): + class Meta(type): + pass + class SlottedBase(object): + __slots__ = 'foo' + # A regression up until 2.5a3: Defining Bar would cause a + # TypeError "mro() returned base with unsuitable layout ('Bar')" + class Bar(SlottedBase): + __metaclass__ = Meta + class ClassNamelessModuleTestCase(unittest.TestCase): def setUp(self): @@ -298,6 +308,7 @@ class Bar(object): self.assertEqual(__module__, module_name) + class ClassMetaclassRepr(unittest.TestCase): """Verifies #1131 is fixed""" def test_repr_with_metaclass(self): Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2008-10-11 05:08:23 UTC (rev 5371) +++ trunk/jython/src/org/python/core/PyType.java 2008-10-11 06:42:35 UTC (rev 5372) @@ -47,7 +47,7 @@ private PyObject dict; /** __mro__, the method resolution. order */ - private PyObject[] mro = new PyObject[0]; + private PyObject[] mro = null; /** __flags__, the type's options. */ private long tp_flags; @@ -645,7 +645,7 @@ @ExposedGet(name = "__mro__") public PyTuple getMro() { - return new PyTuple(mro); + return mro == null ? Py.EmptyTuple : new PyTuple(mro); } @ExposedGet(name = "__flags__") @@ -929,11 +929,24 @@ public boolean isSubType(PyType supertype) { PyObject[] mro = this.mro; - for (int i = 0; i < mro.length; i++) { - if (mro[i] == supertype) + if (mro != null) { + for (PyObject base : mro) { + if (base == supertype) { + return true; + } + } + return false; + } + + // we're not completely initilized yet; follow tp_base + PyType type = this; + do { + if (type == supertype) { return true; - } - return false; + } + type = type.base; + } while (type != null); + return supertype == PyObject.TYPE; } /** @@ -945,6 +958,9 @@ */ public PyObject lookup(String name) { PyObject[] mro = this.mro; + if (mro == null) { + return null; + } for (int i = 0; i < mro.length; i++) { PyObject dict = mro[i].fastGetDict(); if (dict != null) { @@ -958,6 +974,9 @@ public PyObject lookup_where(String name, PyObject[] where) { PyObject[] mro = this.mro; + if (mro == null) { + return null; + } for (int i = 0; i < mro.length; i++) { PyObject t = mro[i]; PyObject dict = t.fastGetDict(); @@ -974,6 +993,9 @@ public PyObject super_lookup(PyType ref, String name) { PyObject[] mro = this.mro; + if (mro == null) { + return null; + } int i; for (i = 0; i < mro.length; i++) { if (mro[i] == ref) @@ -1235,6 +1257,9 @@ protected void __rawdir__(PyDictionary accum) { PyObject[] mro = this.mro; + if (mro == null) { + return; + } for (int i = 0; i < mro.length; i++) { mro[i].addKeys(accum, "__dict__"); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-10-11 05:08:32
|
Revision: 5371 http://jython.svn.sourceforge.net/jython/?rev=5371&view=rev Author: pjenvey Date: 2008-10-11 05:08:23 +0000 (Sat, 11 Oct 2008) Log Message: ----------- disable test_compiler as we don't intend to support the parser module compiler relies on, with _ast being the way forward Modified Paths: -------------- trunk/jython/Lib/test/regrtest.py Modified: trunk/jython/Lib/test/regrtest.py =================================================================== --- trunk/jython/Lib/test/regrtest.py 2008-10-11 03:37:39 UTC (rev 5370) +++ trunk/jython/Lib/test/regrtest.py 2008-10-11 05:08:23 UTC (rev 5371) @@ -1475,6 +1475,7 @@ test_codecmaps_jp test_codecmaps_kr test_codecmaps_tw + test_compiler test_dis test_eof test_frozen This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-10-11 03:37:49
|
Revision: 5370 http://jython.svn.sourceforge.net/jython/?rev=5370&view=rev Author: zyasoft Date: 2008-10-11 03:37:39 +0000 (Sat, 11 Oct 2008) Log Message: ----------- Patch from #1075, thanks Matt Boersma! Modified Paths: -------------- trunk/jython/Lib/os.py Modified: trunk/jython/Lib/os.py =================================================================== --- trunk/jython/Lib/os.py 2008-10-11 02:38:55 UTC (rev 5369) +++ trunk/jython/Lib/os.py 2008-10-11 03:37:39 UTC (rev 5370) @@ -205,15 +205,15 @@ if i < 0 or i > 9: raise IndexError(i) return getattr(self, stat_result._stat_members[i][0]) - + def __setitem__(self, x, value): raise TypeError("object doesn't support item assignment") - + def __setattr__(self, name, value): if name in [x[0] for x in stat_result._stat_members]: raise TypeError(name) raise AttributeError("readonly attribute") - + def __len__(self): return 10 @@ -303,14 +303,14 @@ # if making a /x/y/z/., java.io.File#mkdirs inexplicably fails. So we need # to force it - + # need to use _path instead of path, because param is hiding # os.path module in namespace! head, tail = _path.split(sys_path) if tail == curdir: if File(_path.join(head)).mkdirs(): return - + raise OSError(0, "couldn't make directories", path) def remove(path): @@ -362,7 +362,12 @@ """rmdir(path) Remove a directory.""" - if not File(sys.getPath(path)).delete(): + f = File(sys.getPath(path)) + if not f.exists(): + raise OSError(errno.ENOENT, errno.strerror(errno.ENOENT), path) + elif not f.isDirectory(): + raise OSError(errno.ENOTDIR, errno.strerror(errno.ENOTDIR), path) + elif not f.delete(): raise OSError(0, "couldn't delete directory", path) #XXX: copied from CPython 2.5.1 @@ -392,7 +397,7 @@ def strerror(code): """strerror(code) -> string - + Translate an error code to a message string. """ if not isinstance(code, (int, long)): @@ -404,7 +409,7 @@ def access(path, mode): """access(path, mode) -> True if granted, False otherwise - + Use the real uid/gid to test for access to a path. Note that most operations will use the effective uid/gid, therefore this routine can be used in a suid/sgid environment to test if the invoking user has the @@ -458,7 +463,7 @@ def lstat(path): """lstat(path) -> stat result - + Like stat(path), but do not follow symbolic links. """ abs_path = sys.getPath(path) @@ -545,7 +550,7 @@ """ftruncate(fd, length) Truncate a file to a specified length. - """ + """ rawio = FileDescriptors.get(fd) try: rawio.truncate(length) @@ -647,7 +652,7 @@ if _name == 'posix' and _native_posix: def link(src, dst): """link(src, dst) - + Create a hard link to a file. """ _posix.link(sys.getPath(src), sys.getPath(dst)) @@ -661,7 +666,7 @@ def readlink(path): """readlink(path) -> path - + Return a string representing the path to which the symbolic link points. """ @@ -863,7 +868,7 @@ def putenv(key, value): """putenv(key, value) - + Change or add an environment variable. """ environ[key] = value This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-10-11 02:39:07
|
Revision: 5369 http://jython.svn.sourceforge.net/jython/?rev=5369&view=rev Author: zyasoft Date: 2008-10-11 02:38:55 +0000 (Sat, 11 Oct 2008) Log Message: ----------- Make "extended" builtin functions in __builtin__ (that is, functions like max which use a class like MaxFunction for their definition) copyable by copy.copy and return the correct type. This fixes test_copy, along with enabling reflexive structures to be compared, since we don't prohibit or otherwise detect them in the copying anyway. Modified Paths: -------------- trunk/jython/Lib/test/test_copy.py trunk/jython/src/org/python/core/__builtin__.java Modified: trunk/jython/Lib/test/test_copy.py =================================================================== --- trunk/jython/Lib/test/test_copy.py 2008-10-11 02:24:42 UTC (rev 5368) +++ trunk/jython/Lib/test/test_copy.py 2008-10-11 02:38:55 UTC (rev 5369) @@ -268,11 +268,12 @@ self.assert_(x is not y) self.assert_(x[0] is not y[0]) + # modified for Jython def test_deepcopy_reflexive_list(self): x = [] x.append(x) y = copy.deepcopy(x) - self.assertRaises(RuntimeError, cmp, y, x) + self.assertEqual(y, x) self.assert_(y is not x) self.assert_(y[0] is y) self.assertEqual(len(y), 1) @@ -284,11 +285,12 @@ self.assert_(x is not y) self.assert_(x[0] is not y[0]) + # modified for Jython def test_deepcopy_reflexive_tuple(self): x = ([],) x[0].append(x) y = copy.deepcopy(x) - self.assertRaises(RuntimeError, cmp, y, x) + self.assertEqual(y, x) self.assert_(y is not x) self.assert_(y[0] is not x[0]) self.assert_(y[0][0] is y) @@ -300,11 +302,12 @@ self.assert_(x is not y) self.assert_(x["foo"] is not y["foo"]) + # modified for Jython def test_deepcopy_reflexive_dict(self): x = {} x['foo'] = x y = copy.deepcopy(x) - self.assertRaises(RuntimeError, cmp, y, x) + self.assertEqual(y, x) self.assert_(y is not x) self.assert_(y['foo'] is y) self.assertEqual(len(y), 1) Modified: trunk/jython/src/org/python/core/__builtin__.java =================================================================== --- trunk/jython/src/org/python/core/__builtin__.java 2008-10-11 02:24:42 UTC (rev 5368) +++ trunk/jython/src/org/python/core/__builtin__.java 2008-10-11 02:38:55 UTC (rev 5369) @@ -8,6 +8,8 @@ import org.python.antlr.ast.modType; import org.python.core.util.RelativeFile; import org.python.expose.ExposedGet; +import org.python.expose.ExposedNew; +import org.python.expose.ExposedType; class BuiltinFunctions extends PyBuiltinFunctionSet { @@ -371,23 +373,23 @@ dict.__setitem__("iter", new BuiltinFunctions("iter", 27, 1, 2)); dict.__setitem__("locals", new BuiltinFunctions("locals", 28, 0)); dict.__setitem__("map", new BuiltinFunctions("map", 29, 2, -1)); - dict.__setitem__("max", new MaxFunction()); - dict.__setitem__("min", new MinFunction()); + dict.__setitem__("max", MaxFunction.INSTANCE); + dict.__setitem__("min", MinFunction.INSTANCE); dict.__setitem__("oct", new BuiltinFunctions("oct", 32, 1)); dict.__setitem__("pow", new BuiltinFunctions("pow", 33, 2, 3)); dict.__setitem__("raw_input", new BuiltinFunctions("raw_input", 34, 0, 1)); dict.__setitem__("reduce", new BuiltinFunctions("reduce", 35, 2, 3)); dict.__setitem__("reload", new BuiltinFunctions("reload", 36, 1)); dict.__setitem__("repr", new BuiltinFunctions("repr", 37, 1)); - dict.__setitem__("round", new RoundFunction()); + dict.__setitem__("round", RoundFunction.INSTANCE); dict.__setitem__("setattr", new BuiltinFunctions("setattr", 39, 3)); dict.__setitem__("vars", new BuiltinFunctions("vars", 41, 0, 1)); dict.__setitem__("zip", new BuiltinFunctions("zip", 43, 0, -1)); dict.__setitem__("reversed", new BuiltinFunctions("reversed", 45, 1)); - dict.__setitem__("__import__", new ImportFunction()); - dict.__setitem__("sorted", new SortedFunction()); - dict.__setitem__("all", new AllFunction()); - dict.__setitem__("any", new AnyFunction()); + dict.__setitem__("__import__", ImportFunction.INSTANCE); + dict.__setitem__("sorted", SortedFunction.INSTANCE); + dict.__setitem__("all", AllFunction.INSTANCE); + dict.__setitem__("any", AnyFunction.INSTANCE); } public static PyObject abs(PyObject o) { @@ -1227,8 +1229,25 @@ } } -class ImportFunction extends PyObject { +// simulates a PyBuiltinFunction for functions not using the PyBuiltinFunctionSet approach of above +abstract class ExtendedBuiltinFunction extends PyObject { + public static final PyType TYPE = PyType.fromClass(PyBuiltinFunction.class); + @ExposedGet(name = "__class__") + public PyType getType() { + return TYPE; + } +} + +class ImportFunction extends ExtendedBuiltinFunction { + static final ImportFunction INSTANCE = new ImportFunction(); + + private ImportFunction() {} + @ExposedNew + public static PyObject __new__(PyObject[] args, String[] keyword) { + return INSTANCE; + } + @ExposedGet(name = "__doc__") public PyObject getDoc() { return new PyString("__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\n" + @@ -1276,8 +1295,16 @@ } -class SortedFunction extends PyObject { +class SortedFunction extends ExtendedBuiltinFunction { + static final SortedFunction INSTANCE = new SortedFunction(); + private SortedFunction() {} + + @ExposedNew + public static PyObject __new__(PyObject[] args, String[] keyword) { + return INSTANCE; + } + @ExposedGet(name = "__doc__") @Override public PyObject getDoc() { @@ -1317,8 +1344,16 @@ } } -class AllFunction extends PyObject { +class AllFunction extends ExtendedBuiltinFunction { + static final AllFunction INSTANCE = new AllFunction(); + private AllFunction() {} + + @ExposedNew + public static PyObject __new__(PyObject[] args, String[] keyword) { + return INSTANCE; + } + @ExposedGet(name = "__doc__") @Override public PyObject getDoc() { @@ -1348,8 +1383,16 @@ } } -class AnyFunction extends PyObject { +class AnyFunction extends ExtendedBuiltinFunction { + static final AnyFunction INSTANCE = new AnyFunction(); + private AnyFunction() {} + + @ExposedNew + public static PyObject __new__(PyObject[] args, String[] keyword) { + return INSTANCE; + } + @ExposedGet(name = "__doc__") @Override public PyObject getDoc() { @@ -1379,10 +1422,16 @@ } } +class MaxFunction extends ExtendedBuiltinFunction { + static final MaxFunction INSTANCE = new MaxFunction(); + + private MaxFunction() {} + + @ExposedNew + public static PyObject __new__(PyObject[] args, String[] keyword) { + return INSTANCE; + } - -class MaxFunction extends PyObject { - @ExposedGet(name = "__doc__") @Override public PyObject getDoc() { @@ -1418,7 +1467,7 @@ else { return max(args[0], key); } - } + } @Override public String toString() { @@ -1446,10 +1495,20 @@ } return max; } + } -class MinFunction extends PyObject { - +class MinFunction extends ExtendedBuiltinFunction { + static final MinFunction INSTANCE = new MinFunction(); + + private MinFunction() {} + + @ExposedNew + public static PyObject __new__(PyObject[] args, String[] keyword) { + return INSTANCE; + } + + @ExposedGet(name = "__doc__") @Override public PyObject getDoc() { @@ -1515,9 +1574,15 @@ } } - -class RoundFunction extends PyObject { - +class RoundFunction extends ExtendedBuiltinFunction { + static final RoundFunction INSTANCE = new RoundFunction(); + private RoundFunction() {} + + @ExposedNew + public static PyObject __new__(PyObject[] args, String[] keyword) { + return INSTANCE; + } + @ExposedGet(name = "__doc__") @Override public PyObject getDoc() { @@ -1526,8 +1591,13 @@ "Round a number to a given precision in decimal digits (default 0 digits).\n" + "This always returns a floating point number. Precision may be negative."); } - + @Override + public String toString() { + return "<built-in function round>"; + } + + @Override public PyObject __call__(PyObject args[], String kwds[]) { ArgParser ap = new ArgParser("round", args, kwds, new String[]{"number", "ndigits"}, 0); PyObject number = ap.getPyObject(0); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-10-11 02:24:53
|
Revision: 5368 http://jython.svn.sourceforge.net/jython/?rev=5368&view=rev Author: zyasoft Date: 2008-10-11 02:24:42 +0000 (Sat, 11 Oct 2008) Log Message: ----------- From http://svn.python.org/projects/python/branches/release25-maint/Lib/test/test_copy.py@42573 Added Paths: ----------- trunk/jython/Lib/test/test_copy.py Added: trunk/jython/Lib/test/test_copy.py =================================================================== --- trunk/jython/Lib/test/test_copy.py (rev 0) +++ trunk/jython/Lib/test/test_copy.py 2008-10-11 02:24:42 UTC (rev 5368) @@ -0,0 +1,591 @@ +"""Unit tests for the copy module.""" + +import sys +import copy +import copy_reg + +import unittest +from test import test_support + +class TestCopy(unittest.TestCase): + + # Attempt full line coverage of copy.py from top to bottom + + def test_exceptions(self): + self.assert_(copy.Error is copy.error) + self.assert_(issubclass(copy.Error, Exception)) + + # The copy() method + + def test_copy_basic(self): + x = 42 + y = copy.copy(x) + self.assertEqual(x, y) + + def test_copy_copy(self): + class C(object): + def __init__(self, foo): + self.foo = foo + def __copy__(self): + return C(self.foo) + x = C(42) + y = copy.copy(x) + self.assertEqual(y.__class__, x.__class__) + self.assertEqual(y.foo, x.foo) + + def test_copy_registry(self): + class C(object): + def __new__(cls, foo): + obj = object.__new__(cls) + obj.foo = foo + return obj + def pickle_C(obj): + return (C, (obj.foo,)) + x = C(42) + self.assertRaises(TypeError, copy.copy, x) + copy_reg.pickle(C, pickle_C, C) + y = copy.copy(x) + + def test_copy_reduce_ex(self): + class C(object): + def __reduce_ex__(self, proto): + return "" + def __reduce__(self): + raise test_support.TestFailed, "shouldn't call this" + x = C() + y = copy.copy(x) + self.assert_(y is x) + + def test_copy_reduce(self): + class C(object): + def __reduce__(self): + return "" + x = C() + y = copy.copy(x) + self.assert_(y is x) + + def test_copy_cant(self): + class C(object): + def __getattribute__(self, name): + if name.startswith("__reduce"): + raise AttributeError, name + return object.__getattribute__(self, name) + x = C() + self.assertRaises(copy.Error, copy.copy, x) + + # Type-specific _copy_xxx() methods + + def test_copy_atomic(self): + class Classic: + pass + class NewStyle(object): + pass + def f(): + pass + tests = [None, 42, 2L**100, 3.14, True, False, 1j, + "hello", u"hello\u1234", f.func_code, + NewStyle, xrange(10), Classic, max] + for x in tests: + self.assert_(copy.copy(x) is x, repr(x)) + + def test_copy_list(self): + x = [1, 2, 3] + self.assertEqual(copy.copy(x), x) + + def test_copy_tuple(self): + x = (1, 2, 3) + self.assertEqual(copy.copy(x), x) + + def test_copy_dict(self): + x = {"foo": 1, "bar": 2} + self.assertEqual(copy.copy(x), x) + + def test_copy_inst_vanilla(self): + class C: + def __init__(self, foo): + self.foo = foo + def __cmp__(self, other): + return cmp(self.foo, other.foo) + x = C(42) + self.assertEqual(copy.copy(x), x) + + def test_copy_inst_copy(self): + class C: + def __init__(self, foo): + self.foo = foo + def __copy__(self): + return C(self.foo) + def __cmp__(self, other): + return cmp(self.foo, other.foo) + x = C(42) + self.assertEqual(copy.copy(x), x) + + def test_copy_inst_getinitargs(self): + class C: + def __init__(self, foo): + self.foo = foo + def __getinitargs__(self): + return (self.foo,) + def __cmp__(self, other): + return cmp(self.foo, other.foo) + x = C(42) + self.assertEqual(copy.copy(x), x) + + def test_copy_inst_getstate(self): + class C: + def __init__(self, foo): + self.foo = foo + def __getstate__(self): + return {"foo": self.foo} + def __cmp__(self, other): + return cmp(self.foo, other.foo) + x = C(42) + self.assertEqual(copy.copy(x), x) + + def test_copy_inst_setstate(self): + class C: + def __init__(self, foo): + self.foo = foo + def __setstate__(self, state): + self.foo = state["foo"] + def __cmp__(self, other): + return cmp(self.foo, other.foo) + x = C(42) + self.assertEqual(copy.copy(x), x) + + def test_copy_inst_getstate_setstate(self): + class C: + def __init__(self, foo): + self.foo = foo + def __getstate__(self): + return self.foo + def __setstate__(self, state): + self.foo = state + def __cmp__(self, other): + return cmp(self.foo, other.foo) + x = C(42) + self.assertEqual(copy.copy(x), x) + + # The deepcopy() method + + def test_deepcopy_basic(self): + x = 42 + y = copy.deepcopy(x) + self.assertEqual(y, x) + + def test_deepcopy_memo(self): + # Tests of reflexive objects are under type-specific sections below. + # This tests only repetitions of objects. + x = [] + x = [x, x] + y = copy.deepcopy(x) + self.assertEqual(y, x) + self.assert_(y is not x) + self.assert_(y[0] is not x[0]) + self.assert_(y[0] is y[1]) + + def test_deepcopy_issubclass(self): + # XXX Note: there's no way to test the TypeError coming out of + # issubclass() -- this can only happen when an extension + # module defines a "type" that doesn't formally inherit from + # type. + class Meta(type): + pass + class C: + __metaclass__ = Meta + self.assertEqual(copy.deepcopy(C), C) + + def test_deepcopy_deepcopy(self): + class C(object): + def __init__(self, foo): + self.foo = foo + def __deepcopy__(self, memo=None): + return C(self.foo) + x = C(42) + y = copy.deepcopy(x) + self.assertEqual(y.__class__, x.__class__) + self.assertEqual(y.foo, x.foo) + + def test_deepcopy_registry(self): + class C(object): + def __new__(cls, foo): + obj = object.__new__(cls) + obj.foo = foo + return obj + def pickle_C(obj): + return (C, (obj.foo,)) + x = C(42) + self.assertRaises(TypeError, copy.deepcopy, x) + copy_reg.pickle(C, pickle_C, C) + y = copy.deepcopy(x) + + def test_deepcopy_reduce_ex(self): + class C(object): + def __reduce_ex__(self, proto): + return "" + def __reduce__(self): + raise test_support.TestFailed, "shouldn't call this" + x = C() + y = copy.deepcopy(x) + self.assert_(y is x) + + def test_deepcopy_reduce(self): + class C(object): + def __reduce__(self): + return "" + x = C() + y = copy.deepcopy(x) + self.assert_(y is x) + + def test_deepcopy_cant(self): + class C(object): + def __getattribute__(self, name): + if name.startswith("__reduce"): + raise AttributeError, name + return object.__getattribute__(self, name) + x = C() + self.assertRaises(copy.Error, copy.deepcopy, x) + + # Type-specific _deepcopy_xxx() methods + + def test_deepcopy_atomic(self): + class Classic: + pass + class NewStyle(object): + pass + def f(): + pass + tests = [None, 42, 2L**100, 3.14, True, False, 1j, + "hello", u"hello\u1234", f.func_code, + NewStyle, xrange(10), Classic, max] + for x in tests: + self.assert_(copy.deepcopy(x) is x, repr(x)) + + def test_deepcopy_list(self): + x = [[1, 2], 3] + y = copy.deepcopy(x) + self.assertEqual(y, x) + self.assert_(x is not y) + self.assert_(x[0] is not y[0]) + + def test_deepcopy_reflexive_list(self): + x = [] + x.append(x) + y = copy.deepcopy(x) + self.assertRaises(RuntimeError, cmp, y, x) + self.assert_(y is not x) + self.assert_(y[0] is y) + self.assertEqual(len(y), 1) + + def test_deepcopy_tuple(self): + x = ([1, 2], 3) + y = copy.deepcopy(x) + self.assertEqual(y, x) + self.assert_(x is not y) + self.assert_(x[0] is not y[0]) + + def test_deepcopy_reflexive_tuple(self): + x = ([],) + x[0].append(x) + y = copy.deepcopy(x) + self.assertRaises(RuntimeError, cmp, y, x) + self.assert_(y is not x) + self.assert_(y[0] is not x[0]) + self.assert_(y[0][0] is y) + + def test_deepcopy_dict(self): + x = {"foo": [1, 2], "bar": 3} + y = copy.deepcopy(x) + self.assertEqual(y, x) + self.assert_(x is not y) + self.assert_(x["foo"] is not y["foo"]) + + def test_deepcopy_reflexive_dict(self): + x = {} + x['foo'] = x + y = copy.deepcopy(x) + self.assertRaises(RuntimeError, cmp, y, x) + self.assert_(y is not x) + self.assert_(y['foo'] is y) + self.assertEqual(len(y), 1) + + def test_deepcopy_keepalive(self): + memo = {} + x = 42 + y = copy.deepcopy(x, memo) + self.assert_(memo[id(x)] is x) + + def test_deepcopy_inst_vanilla(self): + class C: + def __init__(self, foo): + self.foo = foo + def __cmp__(self, other): + return cmp(self.foo, other.foo) + x = C([42]) + y = copy.deepcopy(x) + self.assertEqual(y, x) + self.assert_(y.foo is not x.foo) + + def test_deepcopy_inst_deepcopy(self): + class C: + def __init__(self, foo): + self.foo = foo + def __deepcopy__(self, memo): + return C(copy.deepcopy(self.foo, memo)) + def __cmp__(self, other): + return cmp(self.foo, other.foo) + x = C([42]) + y = copy.deepcopy(x) + self.assertEqual(y, x) + self.assert_(y is not x) + self.assert_(y.foo is not x.foo) + + def test_deepcopy_inst_getinitargs(self): + class C: + def __init__(self, foo): + self.foo = foo + def __getinitargs__(self): + return (self.foo,) + def __cmp__(self, other): + return cmp(self.foo, other.foo) + x = C([42]) + y = copy.deepcopy(x) + self.assertEqual(y, x) + self.assert_(y is not x) + self.assert_(y.foo is not x.foo) + + def test_deepcopy_inst_getstate(self): + class C: + def __init__(self, foo): + self.foo = foo + def __getstate__(self): + return {"foo": self.foo} + def __cmp__(self, other): + return cmp(self.foo, other.foo) + x = C([42]) + y = copy.deepcopy(x) + self.assertEqual(y, x) + self.assert_(y is not x) + self.assert_(y.foo is not x.foo) + + def test_deepcopy_inst_setstate(self): + class C: + def __init__(self, foo): + self.foo = foo + def __setstate__(self, state): + self.foo = state["foo"] + def __cmp__(self, other): + return cmp(self.foo, other.foo) + x = C([42]) + y = copy.deepcopy(x) + self.assertEqual(y, x) + self.assert_(y is not x) + self.assert_(y.foo is not x.foo) + + def test_deepcopy_inst_getstate_setstate(self): + class C: + def __init__(self, foo): + self.foo = foo + def __getstate__(self): + return self.foo + def __setstate__(self, state): + self.foo = state + def __cmp__(self, other): + return cmp(self.foo, other.foo) + x = C([42]) + y = copy.deepcopy(x) + self.assertEqual(y, x) + self.assert_(y is not x) + self.assert_(y.foo is not x.foo) + + def test_deepcopy_reflexive_inst(self): + class C: + pass + x = C() + x.foo = x + y = copy.deepcopy(x) + self.assert_(y is not x) + self.assert_(y.foo is y) + + # _reconstruct() + + def test_reconstruct_string(self): + class C(object): + def __reduce__(self): + return "" + x = C() + y = copy.copy(x) + self.assert_(y is x) + y = copy.deepcopy(x) + self.assert_(y is x) + + def test_reconstruct_nostate(self): + class C(object): + def __reduce__(self): + return (C, ()) + x = C() + x.foo = 42 + y = copy.copy(x) + self.assert_(y.__class__ is x.__class__) + y = copy.deepcopy(x) + self.assert_(y.__class__ is x.__class__) + + def test_reconstruct_state(self): + class C(object): + def __reduce__(self): + return (C, (), self.__dict__) + def __cmp__(self, other): + return cmp(self.__dict__, other.__dict__) + x = C() + x.foo = [42] + y = copy.copy(x) + self.assertEqual(y, x) + y = copy.deepcopy(x) + self.assertEqual(y, x) + self.assert_(y.foo is not x.foo) + + def test_reconstruct_state_setstate(self): + class C(object): + def __reduce__(self): + return (C, (), self.__dict__) + def __setstate__(self, state): + self.__dict__.update(state) + def __cmp__(self, other): + return cmp(self.__dict__, other.__dict__) + x = C() + x.foo = [42] + y = copy.copy(x) + self.assertEqual(y, x) + y = copy.deepcopy(x) + self.assertEqual(y, x) + self.assert_(y.foo is not x.foo) + + def test_reconstruct_reflexive(self): + class C(object): + pass + x = C() + x.foo = x + y = copy.deepcopy(x) + self.assert_(y is not x) + self.assert_(y.foo is y) + + # Additions for Python 2.3 and pickle protocol 2 + + def test_reduce_4tuple(self): + class C(list): + def __reduce__(self): + return (C, (), self.__dict__, iter(self)) + def __cmp__(self, other): + return (cmp(list(self), list(other)) or + cmp(self.__dict__, other.__dict__)) + x = C([[1, 2], 3]) + y = copy.copy(x) + self.assertEqual(x, y) + self.assert_(x is not y) + self.assert_(x[0] is y[0]) + y = copy.deepcopy(x) + self.assertEqual(x, y) + self.assert_(x is not y) + self.assert_(x[0] is not y[0]) + + def test_reduce_5tuple(self): + class C(dict): + def __reduce__(self): + return (C, (), self.__dict__, None, self.iteritems()) + def __cmp__(self, other): + return (cmp(dict(self), list(dict)) or + cmp(self.__dict__, other.__dict__)) + x = C([("foo", [1, 2]), ("bar", 3)]) + y = copy.copy(x) + self.assertEqual(x, y) + self.assert_(x is not y) + self.assert_(x["foo"] is y["foo"]) + y = copy.deepcopy(x) + self.assertEqual(x, y) + self.assert_(x is not y) + self.assert_(x["foo"] is not y["foo"]) + + def test_copy_slots(self): + class C(object): + __slots__ = ["foo"] + x = C() + x.foo = [42] + y = copy.copy(x) + self.assert_(x.foo is y.foo) + + def test_deepcopy_slots(self): + class C(object): + __slots__ = ["foo"] + x = C() + x.foo = [42] + y = copy.deepcopy(x) + self.assertEqual(x.foo, y.foo) + self.assert_(x.foo is not y.foo) + + def test_copy_list_subclass(self): + class C(list): + pass + x = C([[1, 2], 3]) + x.foo = [4, 5] + y = copy.copy(x) + self.assertEqual(list(x), list(y)) + self.assertEqual(x.foo, y.foo) + self.assert_(x[0] is y[0]) + self.assert_(x.foo is y.foo) + + def test_deepcopy_list_subclass(self): + class C(list): + pass + x = C([[1, 2], 3]) + x.foo = [4, 5] + y = copy.deepcopy(x) + self.assertEqual(list(x), list(y)) + self.assertEqual(x.foo, y.foo) + self.assert_(x[0] is not y[0]) + self.assert_(x.foo is not y.foo) + + def test_copy_tuple_subclass(self): + class C(tuple): + pass + x = C([1, 2, 3]) + self.assertEqual(tuple(x), (1, 2, 3)) + y = copy.copy(x) + self.assertEqual(tuple(y), (1, 2, 3)) + + def test_deepcopy_tuple_subclass(self): + class C(tuple): + pass + x = C([[1, 2], 3]) + self.assertEqual(tuple(x), ([1, 2], 3)) + y = copy.deepcopy(x) + self.assertEqual(tuple(y), ([1, 2], 3)) + self.assert_(x is not y) + self.assert_(x[0] is not y[0]) + + def test_getstate_exc(self): + class EvilState(object): + def __getstate__(self): + raise ValueError, "ain't got no stickin' state" + self.assertRaises(ValueError, copy.copy, EvilState()) + + def test_copy_function(self): + self.assertEqual(copy.copy(global_foo), global_foo) + def foo(x, y): return x+y + self.assertEqual(copy.copy(foo), foo) + bar = lambda: None + self.assertEqual(copy.copy(bar), bar) + + def test_deepcopy_function(self): + self.assertEqual(copy.deepcopy(global_foo), global_foo) + def foo(x, y): return x+y + self.assertEqual(copy.deepcopy(foo), foo) + bar = lambda: None + self.assertEqual(copy.deepcopy(bar), bar) + +def global_foo(x, y): return x+y + +def test_main(): + test_support.run_unittest(TestCopy) + +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-10-10 18:13:10
|
Revision: 5367 http://jython.svn.sourceforge.net/jython/?rev=5367&view=rev Author: fwierzbicki Date: 2008-10-10 18:12:58 +0000 (Fri, 10 Oct 2008) Log Message: ----------- Cleaning up dead code. Modified Paths: -------------- trunk/jython/src/org/python/antlr/ExpressionParser.java trunk/jython/src/org/python/antlr/InteractiveParser.java Modified: trunk/jython/src/org/python/antlr/ExpressionParser.java =================================================================== --- trunk/jython/src/org/python/antlr/ExpressionParser.java 2008-10-09 23:23:05 UTC (rev 5366) +++ trunk/jython/src/org/python/antlr/ExpressionParser.java 2008-10-10 18:12:58 UTC (rev 5367) @@ -35,11 +35,6 @@ try { PythonParser.eval_input_return r = parser.eval_input(); tree = (modType)r.tree; - //CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree); - //nodes.setTokenStream(tokens); - //PythonWalker walker = new PythonWalker(nodes); - //walker.setErrorHandler(errorHandler); - //tree = (modType)walker.expression(); } catch (RecognitionException e) { //XXX: this can't happen. Need to strip the throws from antlr // generated code. Modified: trunk/jython/src/org/python/antlr/InteractiveParser.java =================================================================== --- trunk/jython/src/org/python/antlr/InteractiveParser.java 2008-10-09 23:23:05 UTC (rev 5366) +++ trunk/jython/src/org/python/antlr/InteractiveParser.java 2008-10-10 18:12:58 UTC (rev 5367) @@ -40,11 +40,6 @@ try { PythonParser.single_input_return r = parser.single_input(); tree = (modType)r.tree; - //CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree); - //nodes.setTokenStream(tokens); - //PythonWalker walker = new PythonWalker(nodes); - //walker.setErrorHandler(errorHandler); - //tree = (modType)walker.interactive(); } catch (RecognitionException e) { //I am only throwing ParseExceptions, but "throws RecognitionException" still gets //into the generated code. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-10-09 23:23:13
|
Revision: 5366 http://jython.svn.sourceforge.net/jython/?rev=5366&view=rev Author: pjenvey Date: 2008-10-09 23:23:05 +0000 (Thu, 09 Oct 2008) Log Message: ----------- fix lambdas within decorator args causing an NPE. we weren't initializing their scopes Modified Paths: -------------- trunk/jython/src/org/python/compiler/ScopesCompiler.java Modified: trunk/jython/src/org/python/compiler/ScopesCompiler.java =================================================================== --- trunk/jython/src/org/python/compiler/ScopesCompiler.java 2008-10-06 22:08:13 UTC (rev 5365) +++ trunk/jython/src/org/python/compiler/ScopesCompiler.java 2008-10-09 23:23:05 UTC (rev 5366) @@ -106,6 +106,11 @@ visit(defaults[i]); } + exprType[] decs = node.decorators; + for (int i = decs.length - 1; i >= 0; i--) { + visit(decs[i]); + } + beginScope(node.name, FUNCSCOPE, node, ac); int n = ac.names.size(); for (int i = 0; i < n; i++) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-10-06 22:08:18
|
Revision: 5365 http://jython.svn.sourceforge.net/jython/?rev=5365&view=rev Author: zyasoft Date: 2008-10-06 22:08:13 +0000 (Mon, 06 Oct 2008) Log Message: ----------- Similar mass removal of tests as in test_trace, these all are related to some fundamental differences in our VM, especially around exception handling. Modified Paths: -------------- trunk/jython/Lib/test/test_profilehooks.py Modified: trunk/jython/Lib/test/test_profilehooks.py =================================================================== --- trunk/jython/Lib/test/test_profilehooks.py 2008-10-06 22:00:46 UTC (rev 5364) +++ trunk/jython/Lib/test/test_profilehooks.py 2008-10-06 22:08:13 UTC (rev 5365) @@ -1,3 +1,9 @@ +# see test_trace.py for the reasoning behind why we need to disable so +# many of these tests; in general we need to come up with a more +# stable mechanism for this that's not so dependent on our compilation +# process, but that is tough. Regardless, we want some testing of this +# functionality, so this will have to suffice for now. + import pprint import sys import unittest @@ -358,6 +364,20 @@ def test_main(): + if test_support.is_jython: + del ProfileHookTestCase.test_distant_exception + del ProfileHookTestCase.test_exception + del ProfileHookTestCase.test_exception_in_except_clause + del ProfileHookTestCase.test_exception_propogation + del ProfileHookTestCase.test_nested_exception + del ProfileHookTestCase.test_raise + del ProfileHookTestCase.test_raise_reraise + del ProfileHookTestCase.test_raise_twice + del ProfileHookTestCase.test_stop_iteration + + del ProfileSimulatorTestCase.test_basic_exception + del ProfileSimulatorTestCase.test_distant_exception + test_support.run_unittest( ProfileHookTestCase, ProfileSimulatorTestCase This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-10-06 22:00:57
|
Revision: 5364 http://jython.svn.sourceforge.net/jython/?rev=5364&view=rev Author: zyasoft Date: 2008-10-06 22:00:46 +0000 (Mon, 06 Oct 2008) Log Message: ----------- From http://svn.python.org/projects/python/branches/release25-maint/Lib/test/test_profilehooks.py@35570 Added Paths: ----------- trunk/jython/Lib/test/test_profilehooks.py Added: trunk/jython/Lib/test/test_profilehooks.py =================================================================== --- trunk/jython/Lib/test/test_profilehooks.py (rev 0) +++ trunk/jython/Lib/test/test_profilehooks.py 2008-10-06 22:00:46 UTC (rev 5364) @@ -0,0 +1,368 @@ +import pprint +import sys +import unittest + +from test import test_support + + +class HookWatcher: + def __init__(self): + self.frames = [] + self.events = [] + + def callback(self, frame, event, arg): + if (event == "call" + or event == "return" + or event == "exception"): + self.add_event(event, frame) + + def add_event(self, event, frame=None): + """Add an event to the log.""" + if frame is None: + frame = sys._getframe(1) + + try: + frameno = self.frames.index(frame) + except ValueError: + frameno = len(self.frames) + self.frames.append(frame) + + self.events.append((frameno, event, ident(frame))) + + def get_events(self): + """Remove calls to add_event().""" + disallowed = [ident(self.add_event.im_func), ident(ident)] + self.frames = None + + return [item for item in self.events if item[2] not in disallowed] + + +class ProfileSimulator(HookWatcher): + def __init__(self, testcase): + self.testcase = testcase + self.stack = [] + HookWatcher.__init__(self) + + def callback(self, frame, event, arg): + # Callback registered with sys.setprofile()/sys.settrace() + self.dispatch[event](self, frame) + + def trace_call(self, frame): + self.add_event('call', frame) + self.stack.append(frame) + + def trace_return(self, frame): + self.add_event('return', frame) + self.stack.pop() + + def trace_exception(self, frame): + self.testcase.fail( + "the profiler should never receive exception events") + + def trace_pass(self, frame): + pass + + dispatch = { + 'call': trace_call, + 'exception': trace_exception, + 'return': trace_return, + 'c_call': trace_pass, + 'c_return': trace_pass, + 'c_exception': trace_pass, + } + + +class TestCaseBase(unittest.TestCase): + def check_events(self, callable, expected): + events = capture_events(callable, self.new_watcher()) + if events != expected: + self.fail("Expected events:\n%s\nReceived events:\n%s" + % (pprint.pformat(expected), pprint.pformat(events))) + + +class ProfileHookTestCase(TestCaseBase): + def new_watcher(self): + return HookWatcher() + + def test_simple(self): + def f(p): + pass + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'return', f_ident), + ]) + + def test_exception(self): + def f(p): + 1/0 + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'return', f_ident), + ]) + + def test_caught_exception(self): + def f(p): + try: 1/0 + except: pass + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'return', f_ident), + ]) + + def test_caught_nested_exception(self): + def f(p): + try: 1/0 + except: pass + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'return', f_ident), + ]) + + def test_nested_exception(self): + def f(p): + 1/0 + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + # This isn't what I expected: + # (0, 'exception', protect_ident), + # I expected this again: + (1, 'return', f_ident), + ]) + + def test_exception_in_except_clause(self): + def f(p): + 1/0 + def g(p): + try: + f(p) + except: + try: f(p) + except: pass + f_ident = ident(f) + g_ident = ident(g) + self.check_events(g, [(1, 'call', g_ident), + (2, 'call', f_ident), + (2, 'return', f_ident), + (3, 'call', f_ident), + (3, 'return', f_ident), + (1, 'return', g_ident), + ]) + + def test_exception_propogation(self): + def f(p): + 1/0 + def g(p): + try: f(p) + finally: p.add_event("falling through") + f_ident = ident(f) + g_ident = ident(g) + self.check_events(g, [(1, 'call', g_ident), + (2, 'call', f_ident), + (2, 'return', f_ident), + (1, 'falling through', g_ident), + (1, 'return', g_ident), + ]) + + def test_raise_twice(self): + def f(p): + try: 1/0 + except: 1/0 + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'return', f_ident), + ]) + + def test_raise_reraise(self): + def f(p): + try: 1/0 + except: raise + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'return', f_ident), + ]) + + def test_raise(self): + def f(p): + raise Exception() + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'return', f_ident), + ]) + + def test_distant_exception(self): + def f(): + 1/0 + def g(): + f() + def h(): + g() + def i(): + h() + def j(p): + i() + f_ident = ident(f) + g_ident = ident(g) + h_ident = ident(h) + i_ident = ident(i) + j_ident = ident(j) + self.check_events(j, [(1, 'call', j_ident), + (2, 'call', i_ident), + (3, 'call', h_ident), + (4, 'call', g_ident), + (5, 'call', f_ident), + (5, 'return', f_ident), + (4, 'return', g_ident), + (3, 'return', h_ident), + (2, 'return', i_ident), + (1, 'return', j_ident), + ]) + + def test_generator(self): + def f(): + for i in range(2): + yield i + def g(p): + for i in f(): + pass + f_ident = ident(f) + g_ident = ident(g) + self.check_events(g, [(1, 'call', g_ident), + # call the iterator twice to generate values + (2, 'call', f_ident), + (2, 'return', f_ident), + (2, 'call', f_ident), + (2, 'return', f_ident), + # once more; returns end-of-iteration with + # actually raising an exception + (2, 'call', f_ident), + (2, 'return', f_ident), + (1, 'return', g_ident), + ]) + + def test_stop_iteration(self): + def f(): + for i in range(2): + yield i + raise StopIteration + def g(p): + for i in f(): + pass + f_ident = ident(f) + g_ident = ident(g) + self.check_events(g, [(1, 'call', g_ident), + # call the iterator twice to generate values + (2, 'call', f_ident), + (2, 'return', f_ident), + (2, 'call', f_ident), + (2, 'return', f_ident), + # once more to hit the raise: + (2, 'call', f_ident), + (2, 'return', f_ident), + (1, 'return', g_ident), + ]) + + +class ProfileSimulatorTestCase(TestCaseBase): + def new_watcher(self): + return ProfileSimulator(self) + + def test_simple(self): + def f(p): + pass + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'return', f_ident), + ]) + + def test_basic_exception(self): + def f(p): + 1/0 + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'return', f_ident), + ]) + + def test_caught_exception(self): + def f(p): + try: 1/0 + except: pass + f_ident = ident(f) + self.check_events(f, [(1, 'call', f_ident), + (1, 'return', f_ident), + ]) + + def test_distant_exception(self): + def f(): + 1/0 + def g(): + f() + def h(): + g() + def i(): + h() + def j(p): + i() + f_ident = ident(f) + g_ident = ident(g) + h_ident = ident(h) + i_ident = ident(i) + j_ident = ident(j) + self.check_events(j, [(1, 'call', j_ident), + (2, 'call', i_ident), + (3, 'call', h_ident), + (4, 'call', g_ident), + (5, 'call', f_ident), + (5, 'return', f_ident), + (4, 'return', g_ident), + (3, 'return', h_ident), + (2, 'return', i_ident), + (1, 'return', j_ident), + ]) + + +def ident(function): + if hasattr(function, "f_code"): + code = function.f_code + else: + code = function.func_code + return code.co_firstlineno, code.co_name + + +def protect(f, p): + try: f(p) + except: pass + +protect_ident = ident(protect) + + +def capture_events(callable, p=None): + try: + sys.setprofile() + except TypeError: + pass + else: + raise test_support.TestFailed( + 'sys.setprofile() did not raise TypeError') + + if p is None: + p = HookWatcher() + sys.setprofile(p.callback) + protect(callable, p) + sys.setprofile(None) + return p.get_events()[1:-1] + + +def show_events(callable): + import pprint + pprint.pprint(capture_events(callable)) + + +def test_main(): + test_support.run_unittest( + ProfileHookTestCase, + ProfileSimulatorTestCase + ) + + +if __name__ == "__main__": + test_main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-10-06 21:48:12
|
Revision: 5363 http://jython.svn.sourceforge.net/jython/?rev=5363&view=rev Author: zyasoft Date: 2008-10-06 21:48:03 +0000 (Mon, 06 Oct 2008) Log Message: ----------- Removed test cases not applicable to Jython. Although it's not possible to implement jump when compiled to Java bytecode, it's possible that we can better align our trace with what is done in CPython, so this should be revisited at some point. Modified Paths: -------------- trunk/jython/Lib/test/test_trace.py Modified: trunk/jython/Lib/test/test_trace.py =================================================================== --- trunk/jython/Lib/test/test_trace.py 2008-10-06 20:42:04 UTC (rev 5362) +++ trunk/jython/Lib/test/test_trace.py 2008-10-06 21:48:03 UTC (rev 5363) @@ -1,3 +1,21 @@ +# Tracing in Jython works somewhat similarly to CPython, with the following exceptions: +# +# - No support for jump. Can't do that without dynamic bytecode +# rewriting OR at least until we support a Python bytecode +# interpreter +# +# - Some differences in how we compile, which results in different +# trace orders. This is of course an implementation detail. For +# example, we do not bother to dead-code while 0: loops because the +# JVM does a good job of that (and more aggressively than CPython +# for example, it should be to see comparable things like while +# False). +# +# - Related to this, we record f.setlineno in too many places in our +# code and not at all in the try, since this doesn't correspond to a +# bytecode on the JVM. The latter records events separately. +# + # Testing the line trace facility. from test import test_support @@ -715,11 +733,25 @@ no_jump_without_trace_function() def test_main(): - test_support.run_unittest( - TraceTestCase, - RaisingTraceFuncTestCase, - JumpTestCase - ) + tests = [TraceTestCase, + RaisingTraceFuncTestCase] + if not test_support.is_jython: + tests.append(JumpTestCase) + else: + del TraceTestCase.test_02_arigo + del TraceTestCase.test_05_no_pop_tops + del TraceTestCase.test_07_raise + del TraceTestCase.test_09_settrace_and_raise + del TraceTestCase.test_10_ireturn + del TraceTestCase.test_11_tightloop + del TraceTestCase.test_12_tighterloop + del TraceTestCase.test_13_genexp + del TraceTestCase.test_14_onliner_if + del TraceTestCase.test_15_loops + + print tests + test_support.run_unittest(*tests) + if __name__ == "__main__": test_main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-10-06 20:45:36
|
Revision: 5362 http://jython.svn.sourceforge.net/jython/?rev=5362&view=rev Author: zyasoft Date: 2008-10-06 20:42:04 +0000 (Mon, 06 Oct 2008) Log Message: ----------- From http://svn.python.org/projects/python/branches/release25-maint/Lib/test/test_trace.py@60582 Added Paths: ----------- trunk/jython/Lib/test/test_trace.py Added: trunk/jython/Lib/test/test_trace.py =================================================================== --- trunk/jython/Lib/test/test_trace.py (rev 0) +++ trunk/jython/Lib/test/test_trace.py 2008-10-06 20:42:04 UTC (rev 5362) @@ -0,0 +1,725 @@ +# Testing the line trace facility. + +from test import test_support +import unittest +import sys +import difflib + +# A very basic example. If this fails, we're in deep trouble. +def basic(): + return 1 + +basic.events = [(0, 'call'), + (1, 'line'), + (1, 'return')] + +# Many of the tests below are tricky because they involve pass statements. +# If there is implicit control flow around a pass statement (in an except +# clause or else caluse) under what conditions do you set a line number +# following that clause? + + +# The entire "while 0:" statement is optimized away. No code +# exists for it, so the line numbers skip directly from "del x" +# to "x = 1". +def arigo_example(): + x = 1 + del x + while 0: + pass + x = 1 + +arigo_example.events = [(0, 'call'), + (1, 'line'), + (2, 'line'), + (5, 'line'), + (5, 'return')] + +# check that lines consisting of just one instruction get traced: +def one_instr_line(): + x = 1 + del x + x = 1 + +one_instr_line.events = [(0, 'call'), + (1, 'line'), + (2, 'line'), + (3, 'line'), + (3, 'return')] + +def no_pop_tops(): # 0 + x = 1 # 1 + for a in range(2): # 2 + if a: # 3 + x = 1 # 4 + else: # 5 + x = 1 # 6 + +no_pop_tops.events = [(0, 'call'), + (1, 'line'), + (2, 'line'), + (3, 'line'), + (6, 'line'), + (2, 'line'), + (3, 'line'), + (4, 'line'), + (2, 'line'), + (2, 'return')] + +def no_pop_blocks(): + y = 1 + while not y: + bla + x = 1 + +no_pop_blocks.events = [(0, 'call'), + (1, 'line'), + (2, 'line'), + (4, 'line'), + (4, 'return')] + +def called(): # line -3 + x = 1 + +def call(): # line 0 + called() + +call.events = [(0, 'call'), + (1, 'line'), + (-3, 'call'), + (-2, 'line'), + (-2, 'return'), + (1, 'return')] + +def raises(): + raise Exception + +def test_raise(): + try: + raises() + except Exception, exc: + x = 1 + +test_raise.events = [(0, 'call'), + (1, 'line'), + (2, 'line'), + (-3, 'call'), + (-2, 'line'), + (-2, 'exception'), + (-2, 'return'), + (2, 'exception'), + (3, 'line'), + (4, 'line'), + (4, 'return')] + +def _settrace_and_return(tracefunc): + sys.settrace(tracefunc) + sys._getframe().f_back.f_trace = tracefunc +def settrace_and_return(tracefunc): + _settrace_and_return(tracefunc) + +settrace_and_return.events = [(1, 'return')] + +def _settrace_and_raise(tracefunc): + sys.settrace(tracefunc) + sys._getframe().f_back.f_trace = tracefunc + raise RuntimeError +def settrace_and_raise(tracefunc): + try: + _settrace_and_raise(tracefunc) + except RuntimeError, exc: + pass + +settrace_and_raise.events = [(2, 'exception'), + (3, 'line'), + (4, 'line'), + (4, 'return')] + +# implicit return example +# This test is interesting because of the else: pass +# part of the code. The code generate for the true +# part of the if contains a jump past the else branch. +# The compiler then generates an implicit "return None" +# Internally, the compiler visits the pass statement +# and stores its line number for use on the next instruction. +# The next instruction is the implicit return None. +def ireturn_example(): + a = 5 + b = 5 + if a == b: + b = a+1 + else: + pass + +ireturn_example.events = [(0, 'call'), + (1, 'line'), + (2, 'line'), + (3, 'line'), + (4, 'line'), + (6, 'line'), + (6, 'return')] + +# Tight loop with while(1) example (SF #765624) +def tightloop_example(): + items = range(0, 3) + try: + i = 0 + while 1: + b = items[i]; i+=1 + except IndexError: + pass + +tightloop_example.events = [(0, 'call'), + (1, 'line'), + (2, 'line'), + (3, 'line'), + (4, 'line'), + (5, 'line'), + (5, 'line'), + (5, 'line'), + (5, 'line'), + (5, 'exception'), + (6, 'line'), + (7, 'line'), + (7, 'return')] + +def tighterloop_example(): + items = range(1, 4) + try: + i = 0 + while 1: i = items[i] + except IndexError: + pass + +tighterloop_example.events = [(0, 'call'), + (1, 'line'), + (2, 'line'), + (3, 'line'), + (4, 'line'), + (4, 'line'), + (4, 'line'), + (4, 'line'), + (4, 'exception'), + (5, 'line'), + (6, 'line'), + (6, 'return')] + +def generator_function(): + try: + yield True + "continued" + finally: + "finally" +def generator_example(): + # any() will leave the generator before its end + x = any(generator_function()) + + # the following lines were not traced + for x in range(10): + y = x + +generator_example.events = ([(0, 'call'), + (2, 'line'), + (-6, 'call'), + (-5, 'line'), + (-4, 'line'), + (-4, 'return'), + (-4, 'call'), + (-4, 'exception'), + (-1, 'line'), + (-1, 'return')] + + [(5, 'line'), (6, 'line')] * 10 + + [(5, 'line'), (5, 'return')]) + + +class Tracer: + def __init__(self): + self.events = [] + def trace(self, frame, event, arg): + self.events.append((frame.f_lineno, event)) + return self.trace + def traceWithGenexp(self, frame, event, arg): + (o for o in [1]) + self.events.append((frame.f_lineno, event)) + return self.trace + +class TraceTestCase(unittest.TestCase): + def compare_events(self, line_offset, events, expected_events): + events = [(l - line_offset, e) for (l, e) in events] + if events != expected_events: + self.fail( + "events did not match expectation:\n" + + "\n".join(difflib.ndiff([str(x) for x in expected_events], + [str(x) for x in events]))) + + def run_and_compare(self, func, events): + tracer = Tracer() + sys.settrace(tracer.trace) + func() + sys.settrace(None) + self.compare_events(func.func_code.co_firstlineno, + tracer.events, events) + + def run_test(self, func): + self.run_and_compare(func, func.events) + + def run_test2(self, func): + tracer = Tracer() + func(tracer.trace) + sys.settrace(None) + self.compare_events(func.func_code.co_firstlineno, + tracer.events, func.events) + + def test_01_basic(self): + self.run_test(basic) + def test_02_arigo(self): + self.run_test(arigo_example) + def test_03_one_instr(self): + self.run_test(one_instr_line) + def test_04_no_pop_blocks(self): + self.run_test(no_pop_blocks) + def test_05_no_pop_tops(self): + self.run_test(no_pop_tops) + def test_06_call(self): + self.run_test(call) + def test_07_raise(self): + self.run_test(test_raise) + + def test_08_settrace_and_return(self): + self.run_test2(settrace_and_return) + def test_09_settrace_and_raise(self): + self.run_test2(settrace_and_raise) + def test_10_ireturn(self): + self.run_test(ireturn_example) + def test_11_tightloop(self): + self.run_test(tightloop_example) + def test_12_tighterloop(self): + self.run_test(tighterloop_example) + + def test_13_genexp(self): + self.run_test(generator_example) + # issue1265: if the trace function contains a generator, + # and if the traced function contains another generator + # that is not completely exhausted, the trace stopped. + # Worse: the 'finally' clause was not invoked. + tracer = Tracer() + sys.settrace(tracer.traceWithGenexp) + generator_example() + sys.settrace(None) + self.compare_events(generator_example.func_code.co_firstlineno, + tracer.events, generator_example.events) + + def test_14_onliner_if(self): + def onliners(): + if True: False + else: True + return 0 + self.run_and_compare( + onliners, + [(0, 'call'), + (1, 'line'), + (3, 'line'), + (3, 'return')]) + + def test_15_loops(self): + # issue1750076: "while" expression is skipped by debugger + def for_example(): + for x in range(2): + pass + self.run_and_compare( + for_example, + [(0, 'call'), + (1, 'line'), + (2, 'line'), + (1, 'line'), + (2, 'line'), + (1, 'line'), + (1, 'return')]) + + def while_example(): + # While expression should be traced on every loop + x = 2 + while x > 0: + x -= 1 + self.run_and_compare( + while_example, + [(0, 'call'), + (2, 'line'), + (3, 'line'), + (4, 'line'), + (3, 'line'), + (4, 'line'), + (3, 'line'), + (3, 'return')]) + + def test_16_blank_lines(self): + exec("def f():\n" + "\n" * 256 + " pass") + self.run_and_compare( + f, + [(0, 'call'), + (257, 'line'), + (257, 'return')]) + + +class RaisingTraceFuncTestCase(unittest.TestCase): + def trace(self, frame, event, arg): + """A trace function that raises an exception in response to a + specific trace event.""" + if event == self.raiseOnEvent: + raise ValueError # just something that isn't RuntimeError + else: + return self.trace + + def f(self): + """The function to trace; raises an exception if that's the case + we're testing, so that the 'exception' trace event fires.""" + if self.raiseOnEvent == 'exception': + x = 0 + y = 1/x + else: + return 1 + + def run_test_for_event(self, event): + """Tests that an exception raised in response to the given event is + handled OK.""" + self.raiseOnEvent = event + try: + for i in xrange(sys.getrecursionlimit() + 1): + sys.settrace(self.trace) + try: + self.f() + except ValueError: + pass + else: + self.fail("exception not thrown!") + except RuntimeError: + self.fail("recursion counter not reset") + + # Test the handling of exceptions raised by each kind of trace event. + def test_call(self): + self.run_test_for_event('call') + def test_line(self): + self.run_test_for_event('line') + def test_return(self): + self.run_test_for_event('return') + def test_exception(self): + self.run_test_for_event('exception') + + def test_trash_stack(self): + def f(): + for i in range(5): + print i # line tracing will raise an exception at this line + + def g(frame, why, extra): + if (why == 'line' and + frame.f_lineno == f.func_code.co_firstlineno + 2): + raise RuntimeError, "i am crashing" + return g + + sys.settrace(g) + try: + f() + except RuntimeError: + # the test is really that this doesn't segfault: + import gc + gc.collect() + else: + self.fail("exception not propagated") + + +# 'Jump' tests: assigning to frame.f_lineno within a trace function +# moves the execution position - it's how debuggers implement a Jump +# command (aka. "Set next statement"). + +class JumpTracer: + """Defines a trace function that jumps from one place to another, + with the source and destination lines of the jump being defined by + the 'jump' property of the function under test.""" + + def __init__(self, function): + self.function = function + self.jumpFrom = function.jump[0] + self.jumpTo = function.jump[1] + self.done = False + + def trace(self, frame, event, arg): + if not self.done and frame.f_code == self.function.func_code: + firstLine = frame.f_code.co_firstlineno + if frame.f_lineno == firstLine + self.jumpFrom: + # Cope with non-integer self.jumpTo (because of + # no_jump_to_non_integers below). + try: + frame.f_lineno = firstLine + self.jumpTo + except TypeError: + frame.f_lineno = self.jumpTo + self.done = True + return self.trace + +# The first set of 'jump' tests are for things that are allowed: + +def jump_simple_forwards(output): + output.append(1) + output.append(2) + output.append(3) + +jump_simple_forwards.jump = (1, 3) +jump_simple_forwards.output = [3] + +def jump_simple_backwards(output): + output.append(1) + output.append(2) + +jump_simple_backwards.jump = (2, 1) +jump_simple_backwards.output = [1, 1, 2] + +def jump_out_of_block_forwards(output): + for i in 1, 2: + output.append(2) + for j in [3]: # Also tests jumping over a block + output.append(4) + output.append(5) + +jump_out_of_block_forwards.jump = (3, 5) +jump_out_of_block_forwards.output = [2, 5] + +def jump_out_of_block_backwards(output): + output.append(1) + for i in [1]: + output.append(3) + for j in [2]: # Also tests jumping over a block + output.append(5) + output.append(6) + output.append(7) + +jump_out_of_block_backwards.jump = (6, 1) +jump_out_of_block_backwards.output = [1, 3, 5, 1, 3, 5, 6, 7] + +def jump_to_codeless_line(output): + output.append(1) + # Jumping to this line should skip to the next one. + output.append(3) + +jump_to_codeless_line.jump = (1, 2) +jump_to_codeless_line.output = [3] + +def jump_to_same_line(output): + output.append(1) + output.append(2) + output.append(3) + +jump_to_same_line.jump = (2, 2) +jump_to_same_line.output = [1, 2, 3] + +# Tests jumping within a finally block, and over one. +def jump_in_nested_finally(output): + try: + output.append(2) + finally: + output.append(4) + try: + output.append(6) + finally: + output.append(8) + output.append(9) + +jump_in_nested_finally.jump = (4, 9) +jump_in_nested_finally.output = [2, 9] + +# The second set of 'jump' tests are for things that are not allowed: + +def no_jump_too_far_forwards(output): + try: + output.append(2) + output.append(3) + except ValueError, e: + output.append('after' in str(e)) + +no_jump_too_far_forwards.jump = (3, 6) +no_jump_too_far_forwards.output = [2, True] + +def no_jump_too_far_backwards(output): + try: + output.append(2) + output.append(3) + except ValueError, e: + output.append('before' in str(e)) + +no_jump_too_far_backwards.jump = (3, -1) +no_jump_too_far_backwards.output = [2, True] + +# Test each kind of 'except' line. +def no_jump_to_except_1(output): + try: + output.append(2) + except: + e = sys.exc_info()[1] + output.append('except' in str(e)) + +no_jump_to_except_1.jump = (2, 3) +no_jump_to_except_1.output = [True] + +def no_jump_to_except_2(output): + try: + output.append(2) + except ValueError: + e = sys.exc_info()[1] + output.append('except' in str(e)) + +no_jump_to_except_2.jump = (2, 3) +no_jump_to_except_2.output = [True] + +def no_jump_to_except_3(output): + try: + output.append(2) + except ValueError, e: + output.append('except' in str(e)) + +no_jump_to_except_3.jump = (2, 3) +no_jump_to_except_3.output = [True] + +def no_jump_to_except_4(output): + try: + output.append(2) + except (ValueError, RuntimeError), e: + output.append('except' in str(e)) + +no_jump_to_except_4.jump = (2, 3) +no_jump_to_except_4.output = [True] + +def no_jump_forwards_into_block(output): + try: + output.append(2) + for i in 1, 2: + output.append(4) + except ValueError, e: + output.append('into' in str(e)) + +no_jump_forwards_into_block.jump = (2, 4) +no_jump_forwards_into_block.output = [True] + +def no_jump_backwards_into_block(output): + try: + for i in 1, 2: + output.append(3) + output.append(4) + except ValueError, e: + output.append('into' in str(e)) + +no_jump_backwards_into_block.jump = (4, 3) +no_jump_backwards_into_block.output = [3, 3, True] + +def no_jump_into_finally_block(output): + try: + try: + output.append(3) + x = 1 + finally: + output.append(6) + except ValueError, e: + output.append('finally' in str(e)) + +no_jump_into_finally_block.jump = (4, 6) +no_jump_into_finally_block.output = [3, 6, True] # The 'finally' still runs + +def no_jump_out_of_finally_block(output): + try: + try: + output.append(3) + finally: + output.append(5) + output.append(6) + except ValueError, e: + output.append('finally' in str(e)) + +no_jump_out_of_finally_block.jump = (5, 1) +no_jump_out_of_finally_block.output = [3, True] + +# This verifies the line-numbers-must-be-integers rule. +def no_jump_to_non_integers(output): + try: + output.append(2) + except ValueError, e: + output.append('integer' in str(e)) + +no_jump_to_non_integers.jump = (2, "Spam") +no_jump_to_non_integers.output = [True] + +# This verifies that you can't set f_lineno via _getframe or similar +# trickery. +def no_jump_without_trace_function(): + try: + previous_frame = sys._getframe().f_back + previous_frame.f_lineno = previous_frame.f_lineno + except ValueError, e: + # This is the exception we wanted; make sure the error message + # talks about trace functions. + if 'trace' not in str(e): + raise + else: + # Something's wrong - the expected exception wasn't raised. + raise RuntimeError, "Trace-function-less jump failed to fail" + + +class JumpTestCase(unittest.TestCase): + def compare_jump_output(self, expected, received): + if received != expected: + self.fail( "Outputs don't match:\n" + + "Expected: " + repr(expected) + "\n" + + "Received: " + repr(received)) + + def run_test(self, func): + tracer = JumpTracer(func) + sys.settrace(tracer.trace) + output = [] + func(output) + sys.settrace(None) + self.compare_jump_output(func.output, output) + + def test_01_jump_simple_forwards(self): + self.run_test(jump_simple_forwards) + def test_02_jump_simple_backwards(self): + self.run_test(jump_simple_backwards) + def test_03_jump_out_of_block_forwards(self): + self.run_test(jump_out_of_block_forwards) + def test_04_jump_out_of_block_backwards(self): + self.run_test(jump_out_of_block_backwards) + def test_05_jump_to_codeless_line(self): + self.run_test(jump_to_codeless_line) + def test_06_jump_to_same_line(self): + self.run_test(jump_to_same_line) + def test_07_jump_in_nested_finally(self): + self.run_test(jump_in_nested_finally) + def test_08_no_jump_too_far_forwards(self): + self.run_test(no_jump_too_far_forwards) + def test_09_no_jump_too_far_backwards(self): + self.run_test(no_jump_too_far_backwards) + def test_10_no_jump_to_except_1(self): + self.run_test(no_jump_to_except_1) + def test_11_no_jump_to_except_2(self): + self.run_test(no_jump_to_except_2) + def test_12_no_jump_to_except_3(self): + self.run_test(no_jump_to_except_3) + def test_13_no_jump_to_except_4(self): + self.run_test(no_jump_to_except_4) + def test_14_no_jump_forwards_into_block(self): + self.run_test(no_jump_forwards_into_block) + def test_15_no_jump_backwards_into_block(self): + self.run_test(no_jump_backwards_into_block) + def test_16_no_jump_into_finally_block(self): + self.run_test(no_jump_into_finally_block) + def test_17_no_jump_out_of_finally_block(self): + self.run_test(no_jump_out_of_finally_block) + def test_18_no_jump_to_non_integers(self): + self.run_test(no_jump_to_non_integers) + def test_19_no_jump_without_trace_function(self): + no_jump_without_trace_function() + +def test_main(): + test_support.run_unittest( + TraceTestCase, + RaisingTraceFuncTestCase, + JumpTestCase + ) + +if __name__ == "__main__": + test_main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-10-06 19:00:39
|
Revision: 5361 http://jython.svn.sourceforge.net/jython/?rev=5361&view=rev Author: zyasoft Date: 2008-10-06 18:57:58 +0000 (Mon, 06 Oct 2008) Log Message: ----------- Fixed test_quopri by encoding space or tab before newline and resetting the column count upon a newline Fixed #1144 so that -mMODULE works and remaining args are passed in sys.argv Modified Paths: -------------- trunk/jython/src/org/python/modules/binascii.java trunk/jython/src/org/python/util/jython.java Modified: trunk/jython/src/org/python/modules/binascii.java =================================================================== --- trunk/jython/src/org/python/modules/binascii.java 2008-10-06 03:12:34 UTC (rev 5360) +++ trunk/jython/src/org/python/modules/binascii.java 2008-10-06 18:57:58 UTC (rev 5361) @@ -984,12 +984,25 @@ int count = 0; for (int i=0, m=s.length(); i<m; i++) { char c = s.charAt(i); - if (('!' <= c && c <= '<') + + // RFC 1521 requires that the line ending in a space or tab must have + // that trailing character encoded. + if (lineEnding(s, lineEnd, i)) { + count = 0; + sb.append(lineEnd); + if (lineEnd.length() > 1) i++; + } + else if ((c == '\t' || c == ' ' ) && endOfLine(s, lineEnd, i + 1)) { + count += 3; + qpEscape(sb, c); + } + else if (('!' <= c && c <= '<') || ('>' <= c && c <= '^') || ('`' <= c && c <= '~') || (c == '_' && !header) || (c == '\n' || c == '\r' && istext)) { - if (count == 75) { +// if (count == 75 && i < s.length() - 1) { + if (count == 75 && !endOfLine(s, lineEnd, i + 1)) { sb.append("=").append(lineEnd); count = 0; } @@ -1023,7 +1036,16 @@ } return sb.toString(); } - + + private static boolean endOfLine(String s, String lineEnd, int i) { + return (s.length() == i || lineEnding(s, lineEnd, i)); + } + + private static boolean lineEnding(String s, String lineEnd, int i) { + return + (s.length() > i && s.substring(i).startsWith(lineEnd)); + } + /* public static void main(String[] args) { String l = b2a_uu("Hello"); Modified: trunk/jython/src/org/python/util/jython.java =================================================================== --- trunk/jython/src/org/python/util/jython.java 2008-10-06 03:12:34 UTC (rev 5360) +++ trunk/jython/src/org/python/util/jython.java 2008-10-06 18:57:58 UTC (rev 5361) @@ -7,6 +7,7 @@ import java.io.InputStream; import java.nio.charset.Charset; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -439,7 +440,10 @@ } else if (arg.startsWith("-m")) { runModule = true; - if ((index + 1) < args.length) { + if (arg.length() > 2) { + moduleName = arg.substring(2); + } + else if ((index + 1) < args.length) { moduleName = args[++index]; } else { System.err.println("Argument expected for the -m option"); @@ -450,6 +454,15 @@ if (!fixInteractive) { interactive = false; } + + index++; + int n = args.length-index+1; + argv = new String[n]; + argv[0] = moduleName; + for (int i = 1; index < args.length; i++, index++) { + argv[i] = args[index]; + } + return true; } else { String opt = args[index]; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-10-06 03:13:20
|
Revision: 5360 http://jython.svn.sourceforge.net/jython/?rev=5360&view=rev Author: zyasoft Date: 2008-10-06 03:12:34 +0000 (Mon, 06 Oct 2008) Log Message: ----------- Removed older version of random.py in favor of CPythonLib's; added _random.getrandbits (in PyRandom); modified test_random so that it doesn't test implementation specific aspects of the random generator, since we actually use java.util.Random, not the Mersenne Twister. Modified Paths: -------------- trunk/jython/Lib/test/test_random.py trunk/jython/src/org/python/modules/random/PyRandom.java Removed Paths: ------------- trunk/jython/Lib/random.py Deleted: trunk/jython/Lib/random.py =================================================================== --- trunk/jython/Lib/random.py 2008-10-06 03:02:29 UTC (rev 5359) +++ trunk/jython/Lib/random.py 2008-10-06 03:12:34 UTC (rev 5360) @@ -1,880 +0,0 @@ -"""Random variable generators. - - integers - -------- - uniform within range - - sequences - --------- - pick random element - pick random sample - generate random permutation - - distributions on the real line: - ------------------------------ - uniform - normal (Gaussian) - lognormal - negative exponential - gamma - beta - pareto - Weibull - - distributions on the circle (angles 0 to 2pi) - --------------------------------------------- - circular uniform - von Mises - -General notes on the underlying Mersenne Twister core generator: - -* The period is 2**19937-1. -* It is one of the most extensively tested generators in existence -* Without a direct way to compute N steps forward, the - semantics of jumpahead(n) are weakened to simply jump - to another distant state and rely on the large period - to avoid overlapping sequences. -* The random() method is implemented in C, executes in - a single Python step, and is, therefore, threadsafe. - -""" -import time -from types import BuiltinMethodType as _BuiltinMethodType -from math import log as _log, exp as _exp, pi as _pi, e as _e -from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin -from math import floor as _floor -from os import urandom as _urandom -from binascii import hexlify as _hexlify - - -__all__ = ["Random","seed","random","uniform","randint","choice","sample", - "randrange","shuffle","normalvariate","lognormvariate", - "cunifvariate","expovariate","vonmisesvariate","gammavariate", - "stdgamma","gauss","betavariate","paretovariate","weibullvariate", - "getstate","setstate","jumpahead", "WichmannHill"] - -NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0) -TWOPI = 2.0*_pi -LOG4 = _log(4.0) -SG_MAGICCONST = 1.0 + _log(4.5) -BPF = 53 # Number of bits in a float -RECIP_BPF = 2**-BPF - -# Translated by Guido van Rossum from C source provided by -# Adrian Baddeley. Adapted by Raymond Hettinger for use with -# the Mersenne Twister core generator. - -import _random - -class Random(_random.Random): - """Random number generator base class used by bound module functions. - - Used to instantiate instances of Random to get generators that don't - share state. Especially useful for multi-threaded programs, creating - a different instance of Random for each thread, and using the jumpahead() - method to ensure that the generated sequences seen by each thread don't - overlap. - - Class Random can also be subclassed if you want to use a different basic - generator of your own devising: in that case, override the following - methods: random(), seed(), getstate(), setstate() and jumpahead(). - - """ - - VERSION = 2 # used by getstate/setstate - - def __init__(self, x=None): - """Initialize an instance. - - Optional argument x controls seeding, as for Random.seed(). - """ - - self.seed(x) - self.gauss_next = None - - def seed(self, a=None): - """Initialize internal state from hashable object. - - None or no argument seeds from current time. - - If a is not None or an int or long, hash(a) is used instead. - """ - - if a is None: - a = long(time.time() * 256) # use fractional seconds - super(Random, self).seed(a) - self.gauss_next = None - - def getstate(self): - """Return internal state; can be passed to setstate() later.""" - return self.VERSION, super(Random, self).getstate(), self.gauss_next - - def setstate(self, state): - """Restore internal state from object returned by getstate().""" - version = state[0] - if version == 2: - version, internalstate, self.gauss_next = state - super(Random, self).setstate(internalstate) - else: - raise ValueError("state with version %s passed to " - "Random.setstate() of version %s" % - (version, self.VERSION)) - -## ---- Methods below this point do not need to be overridden when -## ---- subclassing for the purpose of using a different core generator. - -## -------------------- pickle support ------------------- - - def __getstate__(self): # for pickle - return self.getstate() - - def __setstate__(self, state): # for pickle - self.setstate(state) - - def __reduce__(self): - return self.__class__, (), self.getstate() - -## -------------------- integer methods ------------------- - - def randrange(self, start, stop=None, step=1, int=int, default=None, - maxwidth=1L<<BPF, _BuiltinMethod=_BuiltinMethodType): - """Choose a random item from range(start, stop[, step]). - - This fixes the problem with randint() which includes the - endpoint; in Python this is usually not what you want. - Do not supply the 'int', 'default', and 'maxwidth' arguments. - """ - - # This code is a bit messy to make it fast for the - # common case while still doing adequate error checking. - istart = int(start) - if istart != start: - raise ValueError, "non-integer arg 1 for randrange()" - if stop is default: - if istart > 0: - if istart >= maxwidth and type(self.random) is _BuiltinMethod: - return self._randbelow(istart) - return int(self.random() * istart) - raise ValueError, "empty range for randrange()" - - # stop argument supplied. - istop = int(stop) - if istop != stop: - raise ValueError, "non-integer stop for randrange()" - width = istop - istart - if step == 1 and width > 0: - # Note that - # int(istart + self.random()*(istop - istart)) - # instead would be incorrect. For example, consider istart - # = -2 and istop = 0. Then the guts would be in - # -2.0 to 0.0 exclusive on both ends (ignoring that random() - # might return 0.0), and because int() truncates toward 0, the - # final result would be -1 or 0 (instead of -2 or -1). - # istart + int(self.random()*(istop - istart)) - # would also be incorrect, for a subtler reason: the RHS - # can return a long, and then randrange() would also return - # a long, but we're supposed to return an int (for backward - # compatibility). - if width >= maxwidth and type(self.random) is _BuiltinMethod: - return int(istart + self._randbelow(width)) - return int(istart + int(self.random()*width)) - if step == 1: - raise ValueError, "empty range for randrange()" - - # Non-unit step argument supplied. - istep = int(step) - if istep != step: - raise ValueError, "non-integer step for randrange()" - if istep > 0: - n = (width + istep - 1) / istep - elif istep < 0: - n = (width + istep + 1) / istep - else: - raise ValueError, "zero step for randrange()" - - if n <= 0: - raise ValueError, "empty range for randrange()" - - if n >= maxwidth and type(self.random) is _BuiltinMethod: - return istart + self._randbelow(n) - return istart + istep*int(self.random() * n) - - def randint(self, a, b): - """Return random integer in range [a, b], including both end points. - """ - - return self.randrange(a, b+1) - - def _randbelow(self, n, bpf=BPF, maxwidth=1L<<BPF, - long=long, _log=_log, int=int): - """Return a random int in the range [0,n) - - Handles the case where n has more bits than returned - by a single call to the underlying generator. - """ - - # k is a sometimes over but never under estimate of the bits in n - k = int(1.00001 + _log(n-1, 2)) # 2**k > n-1 >= 2**(k-2) - - random = self.random - r = n - while r >= n: - # In Py2.4, this section becomes: r = self.getrandbits(k) - r = long(random() * maxwidth) - bits = bpf - while bits < k: - r = (r << bpf) | (long(random() * maxwidth)) - bits += bpf - r >>= (bits - k) - return r - -## -------------------- sequence methods ------------------- - - def choice(self, seq): - """Choose a random element from a non-empty sequence.""" - return seq[int(self.random() * len(seq))] - - def shuffle(self, x, random=None, int=int): - """x, random=random.random -> shuffle list x in place; return None. - - Optional arg random is a 0-argument function returning a random - float in [0.0, 1.0); by default, the standard random.random. - - Note that for even rather small len(x), the total number of - permutations of x is larger than the period of most random number - generators; this implies that "most" permutations of a long - sequence can never be generated. - """ - - if random is None: - random = self.random - for i in xrange(len(x)-1, 0, -1): - # pick an element in x[:i+1] with which to exchange x[i] - j = int(random() * (i+1)) - x[i], x[j] = x[j], x[i] - - def sample(self, population, k): - """Chooses k unique random elements from a population sequence. - - Returns a new list containing elements from the population while - leaving the original population unchanged. The resulting list is - in selection order so that all sub-slices will also be valid random - samples. This allows raffle winners (the sample) to be partitioned - into grand prize and second place winners (the subslices). - - Members of the population need not be hashable or unique. If the - population contains repeats, then each occurrence is a possible - selection in the sample. - - To choose a sample in a range of integers, use xrange as an argument. - This is especially fast and space efficient for sampling from a - large population: sample(xrange(10000000), 60) - """ - - # Sampling without replacement entails tracking either potential - # selections (the pool) in a list or previous selections in a - # dictionary. - - # When the number of selections is small compared to the population, - # then tracking selections is efficient, requiring only a small - # dictionary and an occasional reselection. For a larger number of - # selections, the pool tracking method is preferred since the list takes - # less space than the dictionary and it doesn't suffer from frequent - # reselections. - - n = len(population) - if not 0 <= k <= n: - raise ValueError, "sample larger than population" - random = self.random - _int = int - result = [None] * k - if n < 6 * k: # if n len list takes less space than a k len dict - pool = list(population) - for i in xrange(k): # invariant: non-selected at [0,n-i) - j = _int(random() * (n-i)) - result[i] = pool[j] - pool[j] = pool[n-i-1] # move non-selected item into vacancy - else: - try: - n > 0 and (population[0], population[n//2], population[n-1]) - except (TypeError, KeyError): # handle sets and dictionaries - population = tuple(population) - selected = {} - for i in xrange(k): - j = _int(random() * n) - while j in selected: - j = _int(random() * n) - result[i] = selected[j] = population[j] - return result - -## -------------------- real-valued distributions ------------------- - -## -------------------- uniform distribution ------------------- - - def uniform(self, a, b): - """Get a random number in the range [a, b).""" - return a + (b-a) * self.random() - -## -------------------- normal distribution -------------------- - - def normalvariate(self, mu, sigma): - """Normal distribution. - - mu is the mean, and sigma is the standard deviation. - - """ - # mu = mean, sigma = standard deviation - - # Uses Kinderman and Monahan method. Reference: Kinderman, - # A.J. and Monahan, J.F., "Computer generation of random - # variables using the ratio of uniform deviates", ACM Trans - # Math Software, 3, (1977), pp257-260. - - random = self.random - while True: - u1 = random() - u2 = 1.0 - random() - z = NV_MAGICCONST*(u1-0.5)/u2 - zz = z*z/4.0 - if zz <= -_log(u2): - break - return mu + z*sigma - -## -------------------- lognormal distribution -------------------- - - def lognormvariate(self, mu, sigma): - """Log normal distribution. - - If you take the natural logarithm of this distribution, you'll get a - normal distribution with mean mu and standard deviation sigma. - mu can have any value, and sigma must be greater than zero. - - """ - return _exp(self.normalvariate(mu, sigma)) - -## -------------------- circular uniform -------------------- - - def cunifvariate(self, mean, arc): - """Circular uniform distribution. - - mean is the mean angle, and arc is the range of the distribution, - centered around the mean angle. Both values must be expressed in - radians. Returned values range between mean - arc/2 and - mean + arc/2 and are normalized to between 0 and pi. - - Deprecated in version 2.3. Use: - (mean + arc * (Random.random() - 0.5)) % Math.pi - - """ - # mean: mean angle (in radians between 0 and pi) - # arc: range of distribution (in radians between 0 and pi) - import warnings - warnings.warn("The cunifvariate function is deprecated; Use (mean " - "+ arc * (Random.random() - 0.5)) % Math.pi instead.", - DeprecationWarning, 2) - - return (mean + arc * (self.random() - 0.5)) % _pi - -## -------------------- exponential distribution -------------------- - - def expovariate(self, lambd): - """Exponential distribution. - - lambd is 1.0 divided by the desired mean. (The parameter would be - called "lambda", but that is a reserved word in Python.) Returned - values range from 0 to positive infinity. - - """ - # lambd: rate lambd = 1/mean - # ('lambda' is a Python reserved word) - - random = self.random - u = random() - while u <= 1e-7: - u = random() - return -_log(u)/lambd - -## -------------------- von Mises distribution -------------------- - - def vonmisesvariate(self, mu, kappa): - """Circular data distribution. - - mu is the mean angle, expressed in radians between 0 and 2*pi, and - kappa is the concentration parameter, which must be greater than or - equal to zero. If kappa is equal to zero, this distribution reduces - to a uniform random angle over the range 0 to 2*pi. - - """ - # mu: mean angle (in radians between 0 and 2*pi) - # kappa: concentration parameter kappa (>= 0) - # if kappa = 0 generate uniform random angle - - # Based upon an algorithm published in: Fisher, N.I., - # "Statistical Analysis of Circular Data", Cambridge - # University Press, 1993. - - # Thanks to Magnus Kessler for a correction to the - # implementation of step 4. - - random = self.random - if kappa <= 1e-6: - return TWOPI * random() - - a = 1.0 + _sqrt(1.0 + 4.0 * kappa * kappa) - b = (a - _sqrt(2.0 * a))/(2.0 * kappa) - r = (1.0 + b * b)/(2.0 * b) - - while True: - u1 = random() - - z = _cos(_pi * u1) - f = (1.0 + r * z)/(r + z) - c = kappa * (r - f) - - u2 = random() - - if not (u2 >= c * (2.0 - c) and u2 > c * _exp(1.0 - c)): - break - - u3 = random() - if u3 > 0.5: - theta = (mu % TWOPI) + _acos(f) - else: - theta = (mu % TWOPI) - _acos(f) - - return theta - -## -------------------- gamma distribution -------------------- - - def gammavariate(self, alpha, beta): - """Gamma distribution. Not the gamma function! - - Conditions on the parameters are alpha > 0 and beta > 0. - - """ - - # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2 - - # Warning: a few older sources define the gamma distribution in terms - # of alpha > -1.0 - if alpha <= 0.0 or beta <= 0.0: - raise ValueError, 'gammavariate: alpha and beta must be > 0.0' - - random = self.random - if alpha > 1.0: - - # Uses R.C.H. Cheng, "The generation of Gamma - # variables with non-integral shape parameters", - # Applied Statistics, (1977), 26, No. 1, p71-74 - - ainv = _sqrt(2.0 * alpha - 1.0) - bbb = alpha - LOG4 - ccc = alpha + ainv - - while True: - u1 = random() - if not 1e-7 < u1 < .9999999: - continue - u2 = 1.0 - random() - v = _log(u1/(1.0-u1))/ainv - x = alpha*_exp(v) - z = u1*u1*u2 - r = bbb+ccc*v-x - if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z): - return x * beta - - elif alpha == 1.0: - # expovariate(1) - u = random() - while u <= 1e-7: - u = random() - return -_log(u) * beta - - else: # alpha is between 0 and 1 (exclusive) - - # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle - - while True: - u = random() - b = (_e + alpha)/_e - p = b*u - if p <= 1.0: - x = pow(p, 1.0/alpha) - else: - # p > 1 - x = -_log((b-p)/alpha) - u1 = random() - if not (((p <= 1.0) and (u1 > _exp(-x))) or - ((p > 1) and (u1 > pow(x, alpha - 1.0)))): - break - return x * beta - - - def stdgamma(self, alpha, ainv, bbb, ccc): - # This method was (and shall remain) undocumented. - # This method is deprecated - # for the following reasons: - # 1. Returns same as .gammavariate(alpha, 1.0) - # 2. Requires caller to provide 3 extra arguments - # that are functions of alpha anyway - # 3. Can't be used for alpha < 0.5 - - # ainv = sqrt(2 * alpha - 1) - # bbb = alpha - log(4) - # ccc = alpha + ainv - import warnings - warnings.warn("The stdgamma function is deprecated; " - "use gammavariate() instead.", - DeprecationWarning, 2) - return self.gammavariate(alpha, 1.0) - - - -## -------------------- Gauss (faster alternative) -------------------- - - def gauss(self, mu, sigma): - """Gaussian distribution. - - mu is the mean, and sigma is the standard deviation. This is - slightly faster than the normalvariate() function. - - Not thread-safe without a lock around calls. - - """ - - # When x and y are two variables from [0, 1), uniformly - # distributed, then - # - # cos(2*pi*x)*sqrt(-2*log(1-y)) - # sin(2*pi*x)*sqrt(-2*log(1-y)) - # - # are two *independent* variables with normal distribution - # (mu = 0, sigma = 1). - # (Lambert Meertens) - # (corrected version; bug discovered by Mike Miller, fixed by LM) - - # Multithreading note: When two threads call this function - # simultaneously, it is possible that they will receive the - # same return value. The window is very small though. To - # avoid this, you have to use a lock around all calls. (I - # didn't want to slow this down in the serial case by using a - # lock here.) - - random = self.random - z = self.gauss_next - self.gauss_next = None - if z is None: - x2pi = random() * TWOPI - g2rad = _sqrt(-2.0 * _log(1.0 - random())) - z = _cos(x2pi) * g2rad - self.gauss_next = _sin(x2pi) * g2rad - - return mu + z*sigma - -## -------------------- beta -------------------- -## See -## http://sourceforge.net/bugs/?func=detailbug&bug_id=130030&group_id=5470 -## for Ivan Frohne's insightful analysis of why the original implementation: -## -## def betavariate(self, alpha, beta): -## # Discrete Event Simulation in C, pp 87-88. -## -## y = self.expovariate(alpha) -## z = self.expovariate(1.0/beta) -## return z/(y+z) -## -## was dead wrong, and how it probably got that way. - - def betavariate(self, alpha, beta): - """Beta distribution. - - Conditions on the parameters are alpha > -1 and beta} > -1. - Returned values range between 0 and 1. - - """ - - # This version due to Janne Sinkkonen, and matches all the std - # texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution"). - y = self.gammavariate(alpha, 1.) - if y == 0: - return 0.0 - else: - return y / (y + self.gammavariate(beta, 1.)) - -## -------------------- Pareto -------------------- - - def paretovariate(self, alpha): - """Pareto distribution. alpha is the shape parameter.""" - # Jain, pg. 495 - - u = 1.0 - self.random() - return 1.0 / pow(u, 1.0/alpha) - -## -------------------- Weibull -------------------- - - def weibullvariate(self, alpha, beta): - """Weibull distribution. - - alpha is the scale parameter and beta is the shape parameter. - - """ - # Jain, pg. 499; bug fix courtesy Bill Arms - - u = 1.0 - self.random() - return alpha * pow(-_log(u), 1.0/beta) - -## -------------------- Wichmann-Hill ------------------- - -class WichmannHill(Random): - - VERSION = 1 # used by getstate/setstate - - def seed(self, a=None): - """Initialize internal state from hashable object. - - None or no argument seeds from current time. - - If a is not None or an int or long, hash(a) is used instead. - - If a is an int or long, a is used directly. Distinct values between - 0 and 27814431486575L inclusive are guaranteed to yield distinct - internal states (this guarantee is specific to the default - Wichmann-Hill generator). - """ - - if a is None: - # Initialize from current time - import time - a = long(time.time() * 256) - - if not isinstance(a, (int, long)): - a = hash(a) - - a, x = divmod(a, 30268) - a, y = divmod(a, 30306) - a, z = divmod(a, 30322) - self._seed = int(x)+1, int(y)+1, int(z)+1 - - self.gauss_next = None - - def random(self): - """Get the next random number in the range [0.0, 1.0).""" - - # Wichman-Hill random number generator. - # - # Wichmann, B. A. & Hill, I. D. (1982) - # Algorithm AS 183: - # An efficient and portable pseudo-random number generator - # Applied Statistics 31 (1982) 188-190 - # - # see also: - # Correction to Algorithm AS 183 - # Applied Statistics 33 (1984) 123 - # - # McLeod, A. I. (1985) - # A remark on Algorithm AS 183 - # Applied Statistics 34 (1985),198-200 - - # This part is thread-unsafe: - # BEGIN CRITICAL SECTION - x, y, z = self._seed - x = (171 * x) % 30269 - y = (172 * y) % 30307 - z = (170 * z) % 30323 - self._seed = x, y, z - # END CRITICAL SECTION - - # Note: on a platform using IEEE-754 double arithmetic, this can - # never return 0.0 (asserted by Tim; proof too long for a comment). - return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0 - - def getstate(self): - """Return internal state; can be passed to setstate() later.""" - return self.VERSION, self._seed, self.gauss_next - - def setstate(self, state): - """Restore internal state from object returned by getstate().""" - version = state[0] - if version == 1: - version, self._seed, self.gauss_next = state - else: - raise ValueError("state with version %s passed to " - "Random.setstate() of version %s" % - (version, self.VERSION)) - - def jumpahead(self, n): - """Act as if n calls to random() were made, but quickly. - - n is an int, greater than or equal to 0. - - Example use: If you have 2 threads and know that each will - consume no more than a million random numbers, create two Random - objects r1 and r2, then do - r2.setstate(r1.getstate()) - r2.jumpahead(1000000) - Then r1 and r2 will use guaranteed-disjoint segments of the full - period. - """ - - if not n >= 0: - raise ValueError("n must be >= 0") - x, y, z = self._seed - x = int(x * pow(171, n, 30269)) % 30269 - y = int(y * pow(172, n, 30307)) % 30307 - z = int(z * pow(170, n, 30323)) % 30323 - self._seed = x, y, z - - def __whseed(self, x=0, y=0, z=0): - """Set the Wichmann-Hill seed from (x, y, z). - - These must be integers in the range [0, 256). - """ - - if not type(x) == type(y) == type(z) == int: - raise TypeError('seeds must be integers') - if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256): - raise ValueError('seeds must be in range(0, 256)') - if 0 == x == y == z: - # Initialize from current time - import time - t = long(time.time() * 256) - t = int((t&0xffffff) ^ (t>>24)) - t, x = divmod(t, 256) - t, y = divmod(t, 256) - t, z = divmod(t, 256) - # Zero is a poor seed, so substitute 1 - self._seed = (x or 1, y or 1, z or 1) - - self.gauss_next = None - - def whseed(self, a=None): - """Seed from hashable object's hash code. - - None or no argument seeds from current time. It is not guaranteed - that objects with distinct hash codes lead to distinct internal - states. - - This is obsolete, provided for compatibility with the seed routine - used prior to Python 2.1. Use the .seed() method instead. - """ - - if a is None: - self.__whseed() - return - a = hash(a) - a, x = divmod(a, 256) - a, y = divmod(a, 256) - a, z = divmod(a, 256) - x = (x + a) % 256 or 1 - y = (y + a) % 256 or 1 - z = (z + a) % 256 or 1 - self.__whseed(x, y, z) -## --------------- Operating System Random Source ------------------ - -class SystemRandom(Random): - """Alternate random number generator using sources provided - by the operating system (such as /dev/urandom on Unix or - CryptGenRandom on Windows). - - Not available on all systems (see os.urandom() for details). - """ - - def random(self): - """Get the next random number in the range [0.0, 1.0).""" - return (long(_hexlify(_urandom(7)), 16) >> 3) * RECIP_BPF - - def getrandbits(self, k): - """getrandbits(k) -> x. Generates a long int with k random bits.""" - if k <= 0: - raise ValueError('number of bits must be greater than zero') - if k != int(k): - raise TypeError('number of bits should be an integer') - bytes = (k + 7) // 8 # bits / 8 and rounded up - x = long(_hexlify(_urandom(bytes)), 16) - return x >> (bytes * 8 - k) # trim excess bits - - def _stub(self, *args, **kwds): - "Stub method. Not used for a system random number generator." - return None - seed = jumpahead = _stub - - def _notimplemented(self, *args, **kwds): - "Method should not be called for a system random number generator." - raise NotImplementedError('System entropy source does not have state.') - getstate = setstate = _notimplemented - -## -------------------- test program -------------------- - -def _test_generator(n, funccall): - import time - print n, 'times', funccall - code = compile(funccall, funccall, 'eval') - total = 0.0 - sqsum = 0.0 - smallest = 1e10 - largest = -1e10 - t0 = time.time() - for i in range(n): - x = eval(code) - total += x - sqsum = sqsum + x*x - smallest = min(x, smallest) - largest = max(x, largest) - t1 = time.time() - print round(t1-t0, 3), 'sec,', - avg = total/n - stddev = _sqrt(sqsum/n - avg*avg) - print 'avg %g, stddev %g, min %g, max %g' % \ - (avg, stddev, smallest, largest) - - -def _test(N=2000): - _test_generator(N, 'random()') - _test_generator(N, 'normalvariate(0.0, 1.0)') - _test_generator(N, 'lognormvariate(0.0, 1.0)') - _test_generator(N, 'cunifvariate(0.0, 1.0)') - _test_generator(N, 'vonmisesvariate(0.0, 1.0)') - _test_generator(N, 'gammavariate(0.01, 1.0)') - _test_generator(N, 'gammavariate(0.1, 1.0)') - _test_generator(N, 'gammavariate(0.1, 2.0)') - _test_generator(N, 'gammavariate(0.5, 1.0)') - _test_generator(N, 'gammavariate(0.9, 1.0)') - _test_generator(N, 'gammavariate(1.0, 1.0)') - _test_generator(N, 'gammavariate(2.0, 1.0)') - _test_generator(N, 'gammavariate(20.0, 1.0)') - _test_generator(N, 'gammavariate(200.0, 1.0)') - _test_generator(N, 'gauss(0.0, 1.0)') - _test_generator(N, 'betavariate(3.0, 3.0)') - -# Create one instance, seeded from current time, and export its methods -# as module-level functions. The functions share state across all uses -#(both in the user's code and in the Python libraries), but that's fine -# for most programs and is easier for the casual user than making them -# instantiate their own Random() instance. - -_inst = Random() -seed = _inst.seed -random = _inst.random -uniform = _inst.uniform -randint = _inst.randint -choice = _inst.choice -randrange = _inst.randrange -sample = _inst.sample -shuffle = _inst.shuffle -normalvariate = _inst.normalvariate -lognormvariate = _inst.lognormvariate -cunifvariate = _inst.cunifvariate -expovariate = _inst.expovariate -vonmisesvariate = _inst.vonmisesvariate -gammavariate = _inst.gammavariate -stdgamma = _inst.stdgamma -gauss = _inst.gauss -betavariate = _inst.betavariate -paretovariate = _inst.paretovariate -weibullvariate = _inst.weibullvariate -getstate = _inst.getstate -setstate = _inst.setstate -jumpahead = _inst.jumpahead - -if __name__ == '__main__': - _test() Modified: trunk/jython/Lib/test/test_random.py =================================================================== --- trunk/jython/Lib/test/test_random.py 2008-10-06 03:02:29 UTC (rev 5359) +++ trunk/jython/Lib/test/test_random.py 2008-10-06 03:12:34 UTC (rev 5360) @@ -530,6 +530,12 @@ TestDistributions, TestModule] + if test_support.is_jython: + del MersenneTwister_TestBasicOps.test_genrandbits + del MersenneTwister_TestBasicOps.test_referenceImplementation + del MersenneTwister_TestBasicOps.test_setstate_middle_arg + del MersenneTwister_TestBasicOps.test_strong_reference_implementation + try: random.SystemRandom().random() except NotImplementedError: Modified: trunk/jython/src/org/python/modules/random/PyRandom.java =================================================================== --- trunk/jython/src/org/python/modules/random/PyRandom.java 2008-10-06 03:02:29 UTC (rev 5359) +++ trunk/jython/src/org/python/modules/random/PyRandom.java 2008-10-06 03:12:34 UTC (rev 5360) @@ -5,6 +5,7 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.math.BigInteger; import java.util.Random; import org.python.core.Py; @@ -45,7 +46,9 @@ seed = new PyLong(System.currentTimeMillis()); } if (seed instanceof PyLong) { - this.javaRandom.setSeed(((PyLong)seed).asLong(0)); + PyLong max = new PyLong(Long.MAX_VALUE); + PyLong seed_modulus = (PyLong)(seed.__mod__(max)); + this.javaRandom.setSeed(((PyLong)seed_modulus).asLong(0)); } else if (seed instanceof PyInteger) { this.javaRandom.setSeed(((PyInteger)seed).getValue()); } else { @@ -137,4 +140,9 @@ double ret=(a*67108864.0+b)*(1.0/9007199254740992.0); return new PyFloat(ret); } + + @ExposedMethod + public PyLong Random_getrandbits(int k) { + return new PyLong(new BigInteger(k, javaRandom)); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2008-10-06 03:02:56
|
Revision: 5359 http://jython.svn.sourceforge.net/jython/?rev=5359&view=rev Author: zyasoft Date: 2008-10-06 03:02:29 +0000 (Mon, 06 Oct 2008) Log Message: ----------- >From http://svn.python.org/projects/python/branches/release25-maint/Lib/test/test_random.py@53510 Added Paths: ----------- trunk/jython/Lib/test/test_random.py Added: trunk/jython/Lib/test/test_random.py =================================================================== --- trunk/jython/Lib/test/test_random.py (rev 0) +++ trunk/jython/Lib/test/test_random.py 2008-10-06 03:02:29 UTC (rev 5359) @@ -0,0 +1,552 @@ +#!/usr/bin/env python + +import unittest +import random +import time +import pickle +import warnings +from math import log, exp, sqrt, pi +from test import test_support + +class TestBasicOps(unittest.TestCase): + # Superclass with tests common to all generators. + # Subclasses must arrange for self.gen to retrieve the Random instance + # to be tested. + + def randomlist(self, n): + """Helper function to make a list of random numbers""" + return [self.gen.random() for i in xrange(n)] + + def test_autoseed(self): + self.gen.seed() + state1 = self.gen.getstate() + time.sleep(0.1) + self.gen.seed() # diffent seeds at different times + state2 = self.gen.getstate() + self.assertNotEqual(state1, state2) + + def test_saverestore(self): + N = 1000 + self.gen.seed() + state = self.gen.getstate() + randseq = self.randomlist(N) + self.gen.setstate(state) # should regenerate the same sequence + self.assertEqual(randseq, self.randomlist(N)) + + def test_seedargs(self): + for arg in [None, 0, 0L, 1, 1L, -1, -1L, 10**20, -(10**20), + 3.14, 1+2j, 'a', tuple('abc')]: + self.gen.seed(arg) + for arg in [range(3), dict(one=1)]: + self.assertRaises(TypeError, self.gen.seed, arg) + self.assertRaises(TypeError, self.gen.seed, 1, 2) + self.assertRaises(TypeError, type(self.gen), []) + + def test_jumpahead(self): + self.gen.seed() + state1 = self.gen.getstate() + self.gen.jumpahead(100) + state2 = self.gen.getstate() # s/b distinct from state1 + self.assertNotEqual(state1, state2) + self.gen.jumpahead(100) + state3 = self.gen.getstate() # s/b distinct from state2 + self.assertNotEqual(state2, state3) + + self.assertRaises(TypeError, self.gen.jumpahead) # needs an arg + self.assertRaises(TypeError, self.gen.jumpahead, "ick") # wrong type + self.assertRaises(TypeError, self.gen.jumpahead, 2.3) # wrong type + self.assertRaises(TypeError, self.gen.jumpahead, 2, 3) # too many + + def test_sample(self): + # For the entire allowable range of 0 <= k <= N, validate that + # the sample is of the correct length and contains only unique items + N = 100 + population = xrange(N) + for k in xrange(N+1): + s = self.gen.sample(population, k) + self.assertEqual(len(s), k) + uniq = set(s) + self.assertEqual(len(uniq), k) + self.failUnless(uniq <= set(population)) + self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0 + + def test_sample_distribution(self): + # For the entire allowable range of 0 <= k <= N, validate that + # sample generates all possible permutations + n = 5 + pop = range(n) + trials = 10000 # large num prevents false negatives without slowing normal case + def factorial(n): + return reduce(int.__mul__, xrange(1, n), 1) + for k in xrange(n): + expected = factorial(n) // factorial(n-k) + perms = {} + for i in xrange(trials): + perms[tuple(self.gen.sample(pop, k))] = None + if len(perms) == expected: + break + else: + self.fail() + + def test_sample_inputs(self): + # SF bug #801342 -- population can be any iterable defining __len__() + self.gen.sample(set(range(20)), 2) + self.gen.sample(range(20), 2) + self.gen.sample(xrange(20), 2) + self.gen.sample(str('abcdefghijklmnopqrst'), 2) + self.gen.sample(tuple('abcdefghijklmnopqrst'), 2) + + def test_sample_on_dicts(self): + self.gen.sample(dict.fromkeys('abcdefghijklmnopqrst'), 2) + + # SF bug #1460340 -- random.sample can raise KeyError + a = dict.fromkeys(range(10)+range(10,100,2)+range(100,110)) + self.gen.sample(a, 3) + + # A followup to bug #1460340: sampling from a dict could return + # a subset of its keys or of its values, depending on the size of + # the subset requested. + N = 30 + d = dict((i, complex(i, i)) for i in xrange(N)) + for k in xrange(N+1): + samp = self.gen.sample(d, k) + # Verify that we got ints back (keys); the values are complex. + for x in samp: + self.assert_(type(x) is int) + samp.sort() + self.assertEqual(samp, range(N)) + + def test_gauss(self): + # Ensure that the seed() method initializes all the hidden state. In + # particular, through 2.2.1 it failed to reset a piece of state used + # by (and only by) the .gauss() method. + + for seed in 1, 12, 123, 1234, 12345, 123456, 654321: + self.gen.seed(seed) + x1 = self.gen.random() + y1 = self.gen.gauss(0, 1) + + self.gen.seed(seed) + x2 = self.gen.random() + y2 = self.gen.gauss(0, 1) + + self.assertEqual(x1, x2) + self.assertEqual(y1, y2) + + def test_pickling(self): + state = pickle.dumps(self.gen) + origseq = [self.gen.random() for i in xrange(10)] + newgen = pickle.loads(state) + restoredseq = [newgen.random() for i in xrange(10)] + self.assertEqual(origseq, restoredseq) + +class WichmannHill_TestBasicOps(TestBasicOps): + gen = random.WichmannHill() + + def test_setstate_first_arg(self): + self.assertRaises(ValueError, self.gen.setstate, (2, None, None)) + + def test_strong_jumpahead(self): + # tests that jumpahead(n) semantics correspond to n calls to random() + N = 1000 + s = self.gen.getstate() + self.gen.jumpahead(N) + r1 = self.gen.random() + # now do it the slow way + self.gen.setstate(s) + for i in xrange(N): + self.gen.random() + r2 = self.gen.random() + self.assertEqual(r1, r2) + + def test_gauss_with_whseed(self): + # Ensure that the seed() method initializes all the hidden state. In + # particular, through 2.2.1 it failed to reset a piece of state used + # by (and only by) the .gauss() method. + + for seed in 1, 12, 123, 1234, 12345, 123456, 654321: + self.gen.whseed(seed) + x1 = self.gen.random() + y1 = self.gen.gauss(0, 1) + + self.gen.whseed(seed) + x2 = self.gen.random() + y2 = self.gen.gauss(0, 1) + + self.assertEqual(x1, x2) + self.assertEqual(y1, y2) + + def test_bigrand(self): + # Verify warnings are raised when randrange is too large for random() + oldfilters = warnings.filters[:] + warnings.filterwarnings("error", "Underlying random") + self.assertRaises(UserWarning, self.gen.randrange, 2**60) + warnings.filters[:] = oldfilters + +class SystemRandom_TestBasicOps(TestBasicOps): + gen = random.SystemRandom() + + def test_autoseed(self): + # Doesn't need to do anything except not fail + self.gen.seed() + + def test_saverestore(self): + self.assertRaises(NotImplementedError, self.gen.getstate) + self.assertRaises(NotImplementedError, self.gen.setstate, None) + + def test_seedargs(self): + # Doesn't need to do anything except not fail + self.gen.seed(100) + + def test_jumpahead(self): + # Doesn't need to do anything except not fail + self.gen.jumpahead(100) + + def test_gauss(self): + self.gen.gauss_next = None + self.gen.seed(100) + self.assertEqual(self.gen.gauss_next, None) + + def test_pickling(self): + self.assertRaises(NotImplementedError, pickle.dumps, self.gen) + + def test_53_bits_per_float(self): + # This should pass whenever a C double has 53 bit precision. + span = 2 ** 53 + cum = 0 + for i in xrange(100): + cum |= int(self.gen.random() * span) + self.assertEqual(cum, span-1) + + def test_bigrand(self): + # The randrange routine should build-up the required number of bits + # in stages so that all bit positions are active. + span = 2 ** 500 + cum = 0 + for i in xrange(100): + r = self.gen.randrange(span) + self.assert_(0 <= r < span) + cum |= r + self.assertEqual(cum, span-1) + + def test_bigrand_ranges(self): + for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: + start = self.gen.randrange(2 ** i) + stop = self.gen.randrange(2 ** (i-2)) + if stop <= start: + return + self.assert_(start <= self.gen.randrange(start, stop) < stop) + + def test_rangelimits(self): + for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]: + self.assertEqual(set(range(start,stop)), + set([self.gen.randrange(start,stop) for i in xrange(100)])) + + def test_genrandbits(self): + # Verify ranges + for k in xrange(1, 1000): + self.assert_(0 <= self.gen.getrandbits(k) < 2**k) + + # Verify all bits active + getbits = self.gen.getrandbits + for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]: + cum = 0 + for i in xrange(100): + cum |= getbits(span) + self.assertEqual(cum, 2**span-1) + + # Verify argument checking + self.assertRaises(TypeError, self.gen.getrandbits) + self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) + self.assertRaises(ValueError, self.gen.getrandbits, 0) + self.assertRaises(ValueError, self.gen.getrandbits, -1) + self.assertRaises(TypeError, self.gen.getrandbits, 10.1) + + def test_randbelow_logic(self, _log=log, int=int): + # check bitcount transition points: 2**i and 2**(i+1)-1 + # show that: k = int(1.001 + _log(n, 2)) + # is equal to or one greater than the number of bits in n + for i in xrange(1, 1000): + n = 1L << i # check an exact power of two + numbits = i+1 + k = int(1.00001 + _log(n, 2)) + self.assertEqual(k, numbits) + self.assert_(n == 2**(k-1)) + + n += n - 1 # check 1 below the next power of two + k = int(1.00001 + _log(n, 2)) + self.assert_(k in [numbits, numbits+1]) + self.assert_(2**k > n > 2**(k-2)) + + n -= n >> 15 # check a little farther below the next power of two + k = int(1.00001 + _log(n, 2)) + self.assertEqual(k, numbits) # note the stronger assertion + self.assert_(2**k > n > 2**(k-1)) # note the stronger assertion + + +class MersenneTwister_TestBasicOps(TestBasicOps): + gen = random.Random() + + def test_setstate_first_arg(self): + self.assertRaises(ValueError, self.gen.setstate, (1, None, None)) + + def test_setstate_middle_arg(self): + # Wrong type, s/b tuple + self.assertRaises(TypeError, self.gen.setstate, (2, None, None)) + # Wrong length, s/b 625 + self.assertRaises(ValueError, self.gen.setstate, (2, (1,2,3), None)) + # Wrong type, s/b tuple of 625 ints + self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None)) + # Last element s/b an int also + self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None)) + + def test_referenceImplementation(self): + # Compare the python implementation with results from the original + # code. Create 2000 53-bit precision random floats. Compare only + # the last ten entries to show that the independent implementations + # are tracking. Here is the main() function needed to create the + # list of expected random numbers: + # void main(void){ + # int i; + # unsigned long init[4]={61731, 24903, 614, 42143}, length=4; + # init_by_array(init, length); + # for (i=0; i<2000; i++) { + # printf("%.15f ", genrand_res53()); + # if (i%5==4) printf("\n"); + # } + # } + expected = [0.45839803073713259, + 0.86057815201978782, + 0.92848331726782152, + 0.35932681119782461, + 0.081823493762449573, + 0.14332226470169329, + 0.084297823823520024, + 0.53814864671831453, + 0.089215024911993401, + 0.78486196105372907] + + self.gen.seed(61731L + (24903L<<32) + (614L<<64) + (42143L<<96)) + actual = self.randomlist(2000)[-10:] + for a, e in zip(actual, expected): + self.assertAlmostEqual(a,e,places=14) + + def test_strong_reference_implementation(self): + # Like test_referenceImplementation, but checks for exact bit-level + # equality. This should pass on any box where C double contains + # at least 53 bits of precision (the underlying algorithm suffers + # no rounding errors -- all results are exact). + from math import ldexp + + expected = [0x0eab3258d2231fL, + 0x1b89db315277a5L, + 0x1db622a5518016L, + 0x0b7f9af0d575bfL, + 0x029e4c4db82240L, + 0x04961892f5d673L, + 0x02b291598e4589L, + 0x11388382c15694L, + 0x02dad977c9e1feL, + 0x191d96d4d334c6L] + self.gen.seed(61731L + (24903L<<32) + (614L<<64) + (42143L<<96)) + actual = self.randomlist(2000)[-10:] + for a, e in zip(actual, expected): + self.assertEqual(long(ldexp(a, 53)), e) + + def test_long_seed(self): + # This is most interesting to run in debug mode, just to make sure + # nothing blows up. Under the covers, a dynamically resized array + # is allocated, consuming space proportional to the number of bits + # in the seed. Unfortunately, that's a quadratic-time algorithm, + # so don't make this horribly big. + seed = (1L << (10000 * 8)) - 1 # about 10K bytes + self.gen.seed(seed) + + def test_53_bits_per_float(self): + # This should pass whenever a C double has 53 bit precision. + span = 2 ** 53 + cum = 0 + for i in xrange(100): + cum |= int(self.gen.random() * span) + self.assertEqual(cum, span-1) + + def test_bigrand(self): + # The randrange routine should build-up the required number of bits + # in stages so that all bit positions are active. + span = 2 ** 500 + cum = 0 + for i in xrange(100): + r = self.gen.randrange(span) + self.assert_(0 <= r < span) + cum |= r + self.assertEqual(cum, span-1) + + def test_bigrand_ranges(self): + for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: + start = self.gen.randrange(2 ** i) + stop = self.gen.randrange(2 ** (i-2)) + if stop <= start: + return + self.assert_(start <= self.gen.randrange(start, stop) < stop) + + def test_rangelimits(self): + for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]: + self.assertEqual(set(range(start,stop)), + set([self.gen.randrange(start,stop) for i in xrange(100)])) + + def test_genrandbits(self): + # Verify cross-platform repeatability + self.gen.seed(1234567) + self.assertEqual(self.gen.getrandbits(100), + 97904845777343510404718956115L) + # Verify ranges + for k in xrange(1, 1000): + self.assert_(0 <= self.gen.getrandbits(k) < 2**k) + + # Verify all bits active + getbits = self.gen.getrandbits + for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]: + cum = 0 + for i in xrange(100): + cum |= getbits(span) + self.assertEqual(cum, 2**span-1) + + # Verify argument checking + self.assertRaises(TypeError, self.gen.getrandbits) + self.assertRaises(TypeError, self.gen.getrandbits, 'a') + self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) + self.assertRaises(ValueError, self.gen.getrandbits, 0) + self.assertRaises(ValueError, self.gen.getrandbits, -1) + + def test_randbelow_logic(self, _log=log, int=int): + # check bitcount transition points: 2**i and 2**(i+1)-1 + # show that: k = int(1.001 + _log(n, 2)) + # is equal to or one greater than the number of bits in n + for i in xrange(1, 1000): + n = 1L << i # check an exact power of two + numbits = i+1 + k = int(1.00001 + _log(n, 2)) + self.assertEqual(k, numbits) + self.assert_(n == 2**(k-1)) + + n += n - 1 # check 1 below the next power of two + k = int(1.00001 + _log(n, 2)) + self.assert_(k in [numbits, numbits+1]) + self.assert_(2**k > n > 2**(k-2)) + + n -= n >> 15 # check a little farther below the next power of two + k = int(1.00001 + _log(n, 2)) + self.assertEqual(k, numbits) # note the stronger assertion + self.assert_(2**k > n > 2**(k-1)) # note the stronger assertion + + def test_randrange_bug_1590891(self): + start = 1000000000000 + stop = -100000000000000000000 + step = -200 + x = self.gen.randrange(start, stop, step) + self.assert_(stop < x <= start) + self.assertEqual((x+stop)%step, 0) + +_gammacoeff = (0.9999999999995183, 676.5203681218835, -1259.139216722289, + 771.3234287757674, -176.6150291498386, 12.50734324009056, + -0.1385710331296526, 0.9934937113930748e-05, 0.1659470187408462e-06) + +def gamma(z, cof=_gammacoeff, g=7): + z -= 1.0 + sum = cof[0] + for i in xrange(1,len(cof)): + sum += cof[i] / (z+i) + z += 0.5 + return (z+g)**z / exp(z+g) * sqrt(2*pi) * sum + +class TestDistributions(unittest.TestCase): + def test_zeroinputs(self): + # Verify that distributions can handle a series of zero inputs' + g = random.Random() + x = [g.random() for i in xrange(50)] + [0.0]*5 + g.random = x[:].pop; g.uniform(1,10) + g.random = x[:].pop; g.paretovariate(1.0) + g.random = x[:].pop; g.expovariate(1.0) + g.random = x[:].pop; g.weibullvariate(1.0, 1.0) + g.random = x[:].pop; g.normalvariate(0.0, 1.0) + g.random = x[:].pop; g.gauss(0.0, 1.0) + g.random = x[:].pop; g.lognormvariate(0.0, 1.0) + g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0) + g.random = x[:].pop; g.gammavariate(0.01, 1.0) + g.random = x[:].pop; g.gammavariate(1.0, 1.0) + g.random = x[:].pop; g.gammavariate(200.0, 1.0) + g.random = x[:].pop; g.betavariate(3.0, 3.0) + + def test_avg_std(self): + # Use integration to test distribution average and standard deviation. + # Only works for distributions which do not consume variates in pairs + g = random.Random() + N = 5000 + x = [i/float(N) for i in xrange(1,N)] + for variate, args, mu, sigmasqrd in [ + (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12), + (g.expovariate, (1.5,), 1/1.5, 1/1.5**2), + (g.paretovariate, (5.0,), 5.0/(5.0-1), + 5.0/((5.0-1)**2*(5.0-2))), + (g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0), + gamma(1+2/3.0)-gamma(1+1/3.0)**2) ]: + g.random = x[:].pop + y = [] + for i in xrange(len(x)): + try: + y.append(variate(*args)) + except IndexError: + pass + s1 = s2 = 0 + for e in y: + s1 += e + s2 += (e - mu) ** 2 + N = len(y) + self.assertAlmostEqual(s1/N, mu, 2) + self.assertAlmostEqual(s2/(N-1), sigmasqrd, 2) + +class TestModule(unittest.TestCase): + def testMagicConstants(self): + self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141) + self.assertAlmostEqual(random.TWOPI, 6.28318530718) + self.assertAlmostEqual(random.LOG4, 1.38629436111989) + self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627) + + def test__all__(self): + # tests validity but not completeness of the __all__ list + self.failUnless(set(random.__all__) <= set(dir(random))) + + def test_random_subclass_with_kwargs(self): + # SF bug #1486663 -- this used to erroneously raise a TypeError + class Subclass(random.Random): + def __init__(self, newarg=None): + random.Random.__init__(self) + Subclass(newarg=1) + + +def test_main(verbose=None): + testclasses = [WichmannHill_TestBasicOps, + MersenneTwister_TestBasicOps, + TestDistributions, + TestModule] + + try: + random.SystemRandom().random() + except NotImplementedError: + pass + else: + testclasses.append(SystemRandom_TestBasicOps) + + test_support.run_unittest(*testclasses) + + # verify reference counting + import sys + if verbose and hasattr(sys, "gettotalrefcount"): + counts = [None] * 5 + for i in xrange(len(counts)): + test_support.run_unittest(*testclasses) + 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: <pj...@us...> - 2008-10-05 00:24:21
|
Revision: 5358 http://jython.svn.sourceforge.net/jython/?rev=5358&view=rev Author: pjenvey Date: 2008-10-05 00:01:52 +0000 (Sun, 05 Oct 2008) Log Message: ----------- fix mislabeled names Modified Paths: -------------- trunk/jython/src/org/python/modules/operator.java Modified: trunk/jython/src/org/python/modules/operator.java =================================================================== --- trunk/jython/src/org/python/modules/operator.java 2008-10-05 00:00:24 UTC (rev 5357) +++ trunk/jython/src/org/python/modules/operator.java 2008-10-05 00:01:52 UTC (rev 5358) @@ -254,8 +254,8 @@ dict.__setitem__("itruediv", new OperatorFunctions("itruediv", 50, 2)); dict.__setitem__("__ixor__", new OperatorFunctions("__ixor__", 51, 2)); dict.__setitem__("ixor", new OperatorFunctions("ixor", 51, 2)); - dict.__setitem__("__index__", new OperatorFunctions("__ixor__", 52, 1)); - dict.__setitem__("index", new OperatorFunctions("ixor", 52, 1)); + dict.__setitem__("__index__", new OperatorFunctions("__index__", 52, 1)); + dict.__setitem__("index", new OperatorFunctions("index", 52, 1)); dict.__setitem__("attrgetter", PyAttrGetter.TYPE); dict.__setitem__("itemgetter", PyItemGetter.TYPE); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-10-05 00:04:46
|
Revision: 5357 http://jython.svn.sourceforge.net/jython/?rev=5357&view=rev Author: pjenvey Date: 2008-10-05 00:00:24 +0000 (Sun, 05 Oct 2008) Log Message: ----------- avoid test_asynchat on BSD where it deadlocks refs #1064 Modified Paths: -------------- trunk/jython/Lib/test/regrtest.py trunk/jython/Lib/test/test_asynchat.py Modified: trunk/jython/Lib/test/regrtest.py =================================================================== --- trunk/jython/Lib/test/regrtest.py 2008-10-02 02:31:03 UTC (rev 5356) +++ trunk/jython/Lib/test/regrtest.py 2008-10-05 00:00:24 UTC (rev 5357) @@ -1379,7 +1379,6 @@ test_aepack test_al test_applesingle - test_asynchat test_audioop test_bsddb test_bsddb185 @@ -1529,8 +1528,13 @@ for skip in WIN_ONLY: self.expected.add(skip) - if test_support.is_jython and os._name != 'posix': - self.expected.add('test_mhlib') + if test_support.is_jython: + if os._name != 'posix': + self.expected.add('test_mhlib') + import platform + os_name = platform.java_ver()[3][0] + if os_name == 'Mac OS X' or 'BSD' in os_name: + self.expected.add('test_asynchat') self.valid = True Modified: trunk/jython/Lib/test/test_asynchat.py =================================================================== --- trunk/jython/Lib/test/test_asynchat.py 2008-10-02 02:31:03 UTC (rev 5356) +++ trunk/jython/Lib/test/test_asynchat.py 2008-10-05 00:00:24 UTC (rev 5357) @@ -5,6 +5,12 @@ import unittest from test import test_support +import platform +os_name = platform.java_ver()[3][0] +if os_name == 'Mac OS X' or 'BSD' in os_name: + raise test_support.TestSkipped('test_asynchat deadlocks on Jython/BSD: ' + 'http://bugs.jython.org/issue1064') + HOST = "127.0.0.1" PORT = 54322 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-10-02 02:31:19
|
Revision: 5356 http://jython.svn.sourceforge.net/jython/?rev=5356&view=rev Author: pjenvey Date: 2008-10-02 02:31:03 +0000 (Thu, 02 Oct 2008) Log Message: ----------- from: http://svn.python.org/projects/python/branches/release25-maint/Lib/test/test_asynchat.py@46906 Added Paths: ----------- trunk/jython/Lib/test/test_asynchat.py Added: trunk/jython/Lib/test/test_asynchat.py =================================================================== --- trunk/jython/Lib/test/test_asynchat.py (rev 0) +++ trunk/jython/Lib/test/test_asynchat.py 2008-10-02 02:31:03 UTC (rev 5356) @@ -0,0 +1,93 @@ +# test asynchat -- requires threading + +import thread # If this fails, we can't test this module +import asyncore, asynchat, socket, threading, time +import unittest +from test import test_support + +HOST = "127.0.0.1" +PORT = 54322 + +class echo_server(threading.Thread): + + def run(self): + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + global PORT + PORT = test_support.bind_port(sock, HOST, PORT) + sock.listen(1) + conn, client = sock.accept() + buffer = "" + while "\n" not in buffer: + data = conn.recv(1) + if not data: + break + buffer = buffer + data + while buffer: + n = conn.send(buffer) + buffer = buffer[n:] + conn.close() + sock.close() + +class echo_client(asynchat.async_chat): + + def __init__(self, terminator): + asynchat.async_chat.__init__(self) + self.contents = None + self.create_socket(socket.AF_INET, socket.SOCK_STREAM) + self.connect((HOST, PORT)) + self.set_terminator(terminator) + self.buffer = "" + + def handle_connect(self): + pass + ##print "Connected" + + def collect_incoming_data(self, data): + self.buffer = self.buffer + data + + def found_terminator(self): + #print "Received:", repr(self.buffer) + self.contents = self.buffer + self.buffer = "" + self.close() + + +class TestAsynchat(unittest.TestCase): + def setUp (self): + pass + + def tearDown (self): + pass + + def test_line_terminator(self): + s = echo_server() + s.start() + time.sleep(1) # Give server time to initialize + c = echo_client('\n') + c.push("hello ") + c.push("world\n") + asyncore.loop() + s.join() + + self.assertEqual(c.contents, 'hello world') + + def test_numeric_terminator(self): + # Try reading a fixed number of bytes + s = echo_server() + s.start() + time.sleep(1) # Give server time to initialize + c = echo_client(6L) + c.push("hello ") + c.push("world\n") + asyncore.loop() + s.join() + + self.assertEqual(c.contents, 'hello ') + + +def test_main(verbose=None): + test_support.run_unittest(TestAsynchat) + +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: <pj...@us...> - 2008-10-02 00:16:39
|
Revision: 5355 http://jython.svn.sourceforge.net/jython/?rev=5355&view=rev Author: pjenvey Date: 2008-10-02 00:16:29 +0000 (Thu, 02 Oct 2008) Log Message: ----------- whitespace Modified Paths: -------------- trunk/jython/ast/astview.py Modified: trunk/jython/ast/astview.py =================================================================== --- trunk/jython/ast/astview.py 2008-10-02 00:13:21 UTC (rev 5354) +++ trunk/jython/ast/astview.py 2008-10-02 00:16:29 UTC (rev 5355) @@ -20,10 +20,10 @@ def get_class_name(t): result = t.__class__.__name__ if result in ("expr_contextType", - "boolopType", - "unaryopType", - "cmpopType", - "operatorType"): + "boolopType", + "unaryopType", + "cmpopType", + "operatorType"): result = str(t) if result == "AugLoad": result = "Load" This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2008-10-02 00:13:27
|
Revision: 5354 http://jython.svn.sourceforge.net/jython/?rev=5354&view=rev Author: pjenvey Date: 2008-10-02 00:13:21 +0000 (Thu, 02 Oct 2008) Log Message: ----------- remove more remnants of org.python.antlr.ast.Unicode Modified Paths: -------------- trunk/jython/ast/astview.py trunk/jython/ast/jastlib.py Modified: trunk/jython/ast/astview.py =================================================================== --- trunk/jython/ast/astview.py 2008-10-01 02:01:33 UTC (rev 5353) +++ trunk/jython/ast/astview.py 2008-10-02 00:13:21 UTC (rev 5354) @@ -33,8 +33,6 @@ result = result.split(".")[-1] if result.endswith("Type"): result = result[:-4] - if result == "Unicode": - result = "Str" return result else: Modified: trunk/jython/ast/jastlib.py =================================================================== --- trunk/jython/ast/jastlib.py 2008-10-01 02:01:33 UTC (rev 5353) +++ trunk/jython/ast/jastlib.py 2008-10-02 00:13:21 UTC (rev 5354) @@ -20,8 +20,6 @@ name = s.split(".")[-1] if name.endswith("Type"): name = name[:-4] - if name == "Unicode": - name = "Str" yield name try: for field in node._fields: @@ -54,8 +52,6 @@ yield node except Exception, why: print "error parsing 'n': %s" % why - elif fname == "s" and parent.__class__.__name__ == 'org.python.antlr.ast.Unicode': - yield unicode(node) else: yield node This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-10-01 02:01:37
|
Revision: 5353 http://jython.svn.sourceforge.net/jython/?rev=5353&view=rev Author: fwierzbicki Date: 2008-10-01 02:01:33 +0000 (Wed, 01 Oct 2008) Log Message: ----------- Added castSlice and castSlices to allow optional error nodes for sliceType. Also renamed makeExpr(s) and makeStmt(s) to castExpr(s) and castStmt(s). Modified Paths: -------------- trunk/jython/grammar/Python.g trunk/jython/src/org/python/antlr/GrammarActions.java Modified: trunk/jython/grammar/Python.g =================================================================== --- trunk/jython/grammar/Python.g 2008-09-26 17:01:58 UTC (rev 5352) +++ trunk/jython/grammar/Python.g 2008-10-01 02:01:33 UTC (rev 5353) @@ -251,7 +251,9 @@ errorHandler.reportError(this, nva); errorHandler.recover(this, nva); // throw out current char and try again } catch (FailedPredicateException fp) { - //XXX: added this for failed STRINGPART + //XXX: added this for failed STRINGPART -- the FailedPredicateException + // hides a NoViableAltException. This should be the only + // FailedPredicateException that gets thrown by the lexer. errorHandler.reportError(this, fp); errorHandler.recover(this, fp); // throw out current char and try again } catch (RecognitionException re) { @@ -274,10 +276,10 @@ mtype = new Interactive($single_input.start, new stmtType[0]); } | simple_stmt NEWLINE* EOF { - mtype = new Interactive($single_input.start, actions.makeStmts($simple_stmt.stypes)); + mtype = new Interactive($single_input.start, actions.castStmts($simple_stmt.stypes)); } | compound_stmt NEWLINE+ EOF { - mtype = new Interactive($single_input.start, actions.makeStmts($compound_stmt.tree)); + mtype = new Interactive($single_input.start, actions.castStmts($compound_stmt.tree)); } ; @@ -303,7 +305,7 @@ : (NEWLINE | stmt {stypes.addAll($stmt.stypes);} )* EOF { - mtype = new Module($file_input.start, actions.makeStmts(stypes)); + mtype = new Module($file_input.start, actions.castStmts(stypes)); } ; @@ -316,7 +318,7 @@ $eval_input.tree = mtype; } : LEADING_WS? (NEWLINE)* testlist[expr_contextType.Load] (NEWLINE)* EOF { - mtype = new Expression($eval_input.start, actions.makeExpr($testlist.tree)); + mtype = new Expression($eval_input.start, actions.castExpr($testlist.tree)); } ; @@ -428,7 +430,7 @@ } : fpdef[expr_contextType.Param] (ASSIGN test[expr_contextType.Load])? { - $etype = actions.makeExpr($fpdef.tree); + $etype = actions.castExpr($fpdef.tree); if ($ASSIGN != null) { defaults.add($test.tree); } else if (!defaults.isEmpty()) { @@ -466,12 +468,12 @@ //fpdef: NAME | '(' fplist ')' fpdef[expr_contextType ctype] @after { - actions.checkAssign(actions.makeExpr($fpdef.tree)); + actions.checkAssign(actions.castExpr($fpdef.tree)); } : NAME -> ^(NAME<Name>[$NAME, $NAME.text, ctype]) | (LPAREN fpdef[null] COMMA) => LPAREN fplist RPAREN - -> ^(LPAREN<Tuple>[$fplist.start, actions.makeExprs($fplist.etypes), expr_contextType.Store]) + -> ^(LPAREN<Tuple>[$fplist.start, actions.castExprs($fplist.etypes), expr_contextType.Store]) | LPAREN fplist RPAREN -> fplist ; @@ -533,31 +535,31 @@ : ((testlist[null] augassign) => lhs=testlist[expr_contextType.AugStore] ( (aay=augassign y1=yield_expr { - actions.checkAssign(actions.makeExpr($lhs.tree)); - stype = new AugAssign($lhs.tree, actions.makeExpr($lhs.tree), $aay.op, actions.makeExpr($y1.tree)); + actions.checkAssign(actions.castExpr($lhs.tree)); + stype = new AugAssign($lhs.tree, actions.castExpr($lhs.tree), $aay.op, actions.castExpr($y1.tree)); } ) | (aat=augassign rhs=testlist[expr_contextType.Load] { - actions.checkAssign(actions.makeExpr($lhs.tree)); - stype = new AugAssign($lhs.tree, actions.makeExpr($lhs.tree), $aat.op, actions.makeExpr($rhs.tree)); + actions.checkAssign(actions.castExpr($lhs.tree)); + stype = new AugAssign($lhs.tree, actions.castExpr($lhs.tree), $aat.op, actions.castExpr($rhs.tree)); } ) ) | (testlist[null] ASSIGN) => lhs=testlist[expr_contextType.Store] ( | ((at=ASSIGN t+=testlist[expr_contextType.Store])+ - -> ^(ASSIGN<Assign>[$lhs.start, actions.makeAssignTargets(actions.makeExpr($lhs.tree), $t), + -> ^(ASSIGN<Assign>[$lhs.start, actions.makeAssignTargets(actions.castExpr($lhs.tree), $t), actions.makeAssignValue($t)]) ) | ((ay=ASSIGN y2+=yield_expr)+ - -> ^(ASSIGN<Assign>[$lhs.start, actions.makeAssignTargets(actions.makeExpr($lhs.tree), $y2), + -> ^(ASSIGN<Assign>[$lhs.start, actions.makeAssignTargets(actions.castExpr($lhs.tree), $y2), actions.makeAssignValue($y2)]) ) ) | lhs=testlist[expr_contextType.Load] { - stype = new Expr($lhs.start, actions.makeExpr($lhs.tree)); + stype = new Expr($lhs.start, actions.castExpr($lhs.tree)); } ) ; @@ -584,9 +586,9 @@ print_stmt : PRINT (t1=printlist - -> ^(PRINT<Print>[$PRINT, null, actions.makeExprs($t1.elts), $t1.newline]) + -> ^(PRINT<Print>[$PRINT, null, actions.castExprs($t1.elts), $t1.newline]) | RIGHTSHIFT t2=printlist2 - -> ^(PRINT<Print>[$PRINT, actions.makeExpr($t2.elts.get(0)), actions.makeExprs($t2.elts, 1), $t2.newline]) + -> ^(PRINT<Print>[$PRINT, actions.castExpr($t2.elts.get(0)), actions.castExprs($t2.elts, 1), $t2.newline]) | -> ^(PRINT<Print>[$PRINT, null, new exprType[0\], false]) ) @@ -636,7 +638,7 @@ //del_stmt: 'del' exprlist del_stmt : DELETE del_list - -> ^(DELETE<Delete>[$DELETE, actions.makeExprs($del_list.etypes)]) + -> ^(DELETE<Delete>[$DELETE, actions.castExprs($del_list.etypes)]) ; //pass_stmt: 'pass' @@ -670,7 +672,7 @@ return_stmt : RETURN (testlist[expr_contextType.Load] - -> ^(RETURN<Return>[$RETURN, actions.makeExpr($testlist.tree)]) + -> ^(RETURN<Return>[$RETURN, actions.castExpr($testlist.tree)]) | -> ^(RETURN<Return>[$RETURN, null]) ) @@ -678,14 +680,14 @@ //yield_stmt: yield_expr yield_stmt - : yield_expr -> ^(YIELD<Expr>[$yield_expr.start, actions.makeExpr($yield_expr.tree)]) + : yield_expr -> ^(YIELD<Expr>[$yield_expr.start, actions.castExpr($yield_expr.tree)]) ; //raise_stmt: 'raise' [test [',' test [',' test]]] raise_stmt : RAISE (t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load] (COMMA t3=test[expr_contextType.Load])?)?)? - -> ^(RAISE<Raise>[$RAISE, actions.makeExpr($t1.tree), actions.makeExpr($t2.tree), actions.makeExpr($t3.tree)]) + -> ^(RAISE<Raise>[$RAISE, actions.castExpr($t1.tree), actions.castExpr($t2.tree), actions.castExpr($t3.tree)]) ; //import_stmt: import_name | import_from @@ -778,14 +780,14 @@ : EXEC expr[expr_contextType.Load] (IN t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])?)? { - stype = new Exec($EXEC, actions.makeExpr($expr.tree), actions.makeExpr($t1.tree), actions.makeExpr($t2.tree)); + stype = new Exec($EXEC, actions.castExpr($expr.tree), actions.castExpr($t1.tree), actions.castExpr($t2.tree)); } ; //assert_stmt: 'assert' test [',' test] assert_stmt : ASSERT t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Load])? - -> ^(ASSERT<Assert>[$ASSERT, actions.makeExpr($t1.tree), actions.makeExpr($t2.tree)]) + -> ^(ASSERT<Assert>[$ASSERT, actions.castExpr($t1.tree), actions.castExpr($t2.tree)]) ; //compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef @@ -802,7 +804,7 @@ //if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] if_stmt : IF test[expr_contextType.Load] COLON ifsuite=suite elif_clause[$test.start]? - -> ^(IF<If>[$IF, actions.makeExpr($test.tree), actions.makeStmts($ifsuite.stypes), + -> ^(IF<If>[$IF, actions.castExpr($test.tree), actions.castStmts($ifsuite.stypes), actions.makeElse($elif_clause.stypes, $elif_clause.tree)]) ; @@ -813,9 +815,9 @@ } | ELIF test[expr_contextType.Load] COLON suite (e2=elif_clause[$iftest] - -> ^(ELIF<If>[$iftest, actions.makeExpr($test.tree), actions.makeStmts($suite.stypes), actions.makeElse($e2.stypes, $e2.tree)]) + -> ^(ELIF<If>[$iftest, actions.castExpr($test.tree), actions.castStmts($suite.stypes), actions.makeElse($e2.stypes, $e2.tree)]) | - -> ^(ELIF<If>[$iftest, actions.makeExpr($test.tree), actions.makeStmts($suite.stypes), new stmtType[0\]]) + -> ^(ELIF<If>[$iftest, actions.castExpr($test.tree), actions.castStmts($suite.stypes), new stmtType[0\]]) ) ; @@ -836,7 +838,7 @@ } : WHILE test[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? { - stype = actions.makeWhile($WHILE, actions.makeExpr($test.tree), $s1.stypes, $s2.stypes); + stype = actions.makeWhile($WHILE, actions.castExpr($test.tree), $s1.stypes, $s2.stypes); } ; @@ -851,7 +853,7 @@ : FOR exprlist[expr_contextType.Store] IN testlist[expr_contextType.Load] COLON s1=suite (ORELSE COLON s2=suite)? { - stype = actions.makeFor($FOR, $exprlist.etype, actions.makeExpr($testlist.tree), $s1.stypes, $s2.stypes); + stype = actions.makeFor($FOR, $exprlist.etype, actions.castExpr($testlist.tree), $s1.stypes, $s2.stypes); } ; @@ -889,8 +891,8 @@ } : WITH test[expr_contextType.Load] (with_var)? COLON suite { - stype = new With($WITH, actions.makeExpr($test.tree), $with_var.etype, - actions.makeStmts($suite.stypes)); + stype = new With($WITH, actions.castExpr($test.tree), $with_var.etype, + actions.castStmts($suite.stypes)); } ; @@ -898,15 +900,15 @@ with_var returns [exprType etype] : (AS | NAME) expr[expr_contextType.Store] { - $etype = actions.makeExpr($expr.tree); + $etype = actions.castExpr($expr.tree); } ; //except_clause: 'except' [test [',' test]] except_clause : EXCEPT (t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Store])?)? COLON suite - -> ^(EXCEPT<excepthandlerType>[$EXCEPT, actions.makeExpr($t1.tree), actions.makeExpr($t2.tree), - actions.makeStmts($suite.stypes), $EXCEPT.getLine(), $EXCEPT.getCharPositionInLine()]) + -> ^(EXCEPT<excepthandlerType>[$EXCEPT, actions.castExpr($t1.tree), actions.castExpr($t2.tree), + actions.castStmts($suite.stypes), $EXCEPT.getLine(), $EXCEPT.getCharPositionInLine()]) ; //suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT @@ -930,7 +932,7 @@ test[expr_contextType ctype] :o1=or_test[ctype] ( (IF or_test[null] ORELSE) => IF o2=or_test[ctype] ORELSE e=test[expr_contextType.Load] - -> ^(IF<IfExp>[$o1.start, actions.makeExpr($o2.tree), actions.makeExpr($o1.tree), actions.makeExpr($e.tree)]) + -> ^(IF<IfExp>[$o1.start, actions.castExpr($o2.tree), actions.castExpr($o1.tree), actions.castExpr($e.tree)]) | -> or_test ) @@ -970,7 +972,7 @@ //not_test: 'not' not_test | comparison not_test[expr_contextType ctype] : NOT nt=not_test[ctype] - -> ^(NOT<UnaryOp>[$NOT, unaryopType.Not, actions.makeExpr($nt.tree)]) + -> ^(NOT<UnaryOp>[$NOT, unaryopType.Not, actions.castExpr($nt.tree)]) | comparison[ctype] ; @@ -981,8 +983,8 @@ } @after { if (!cmps.isEmpty()) { - $comparison.tree = new Compare($left.start, actions.makeExpr($left.tree), actions.makeCmpOps(cmps), - actions.makeExprs($right)); + $comparison.tree = new Compare($left.start, actions.castExpr($left.tree), actions.makeCmpOps(cmps), + actions.castExprs($right)); } } : left=expr[ctype] @@ -1140,7 +1142,7 @@ : PLUS p=factor {$etype = new UnaryOp($PLUS, unaryopType.UAdd, $p.etype);} | MINUS m=factor {$etype = actions.negate($MINUS, $m.etype);} | TILDE t=factor {$etype = new UnaryOp($TILDE, unaryopType.Invert, $t.etype);} - | power {$etype = actions.makeExpr($power.tree);} + | power {$etype = actions.castExpr($power.tree);} ; //power: atom trailer* ['**' factor] @@ -1151,7 +1153,7 @@ : atom (t+=trailer[$atom.start, $atom.tree])* (options {greedy=true;}:d=DOUBLESTAR factor)? { //XXX: This could be better. - $etype = actions.makeExpr($atom.tree); + $etype = actions.castExpr($atom.tree); if ($t != null) { for(Object o : $t) { if ($etype instanceof Context) { @@ -1204,14 +1206,14 @@ RBRACK | LCURLY (dictmaker - -> ^(LCURLY<Dict>[$LCURLY, actions.makeExprs($dictmaker.keys), - actions.makeExprs($dictmaker.values)]) + -> ^(LCURLY<Dict>[$LCURLY, actions.castExprs($dictmaker.keys), + actions.castExprs($dictmaker.values)]) | -> ^(LCURLY<Dict>[$LCURLY, new exprType[0\], new exprType[0\]]) ) RCURLY | lb=BACKQUOTE testlist[expr_contextType.Load] rb=BACKQUOTE - -> ^(BACKQUOTE<Repr>[$lb, actions.makeExpr($testlist.tree)]) + -> ^(BACKQUOTE<Repr>[$lb, actions.castExpr($testlist.tree)]) | NAME -> ^(NAME<Name>[$NAME, $NAME.text, $expr::ctype]) | INT @@ -1241,11 +1243,11 @@ Collections.reverse(gens); comprehensionType[] c = (comprehensionType[])gens.toArray(new comprehensionType[gens.size()]); - etype = new ListComp($listmaker.start, actions.makeExpr($t.get(0)), c); + etype = new ListComp($listmaker.start, actions.castExpr($t.get(0)), c); } | (options {greedy=true;}:COMMA t+=test[$expr::ctype])* { - etype = new org.python.antlr.ast.List($lbrack, actions.makeExprs($t), $expr::ctype); + etype = new org.python.antlr.ast.List($lbrack, actions.castExprs($t), $expr::ctype); } ) (COMMA)? ; @@ -1264,18 +1266,18 @@ : t+=test[$expr::ctype] ( ((options {k=2;}: c1=COMMA t+=test[$expr::ctype])* (c2=COMMA)? -> { $c1 != null || $c2 != null }? - ^(COMMA<Tuple>[$testlist_gexp.start, actions.makeExprs($t), $expr::ctype]) + ^(COMMA<Tuple>[$testlist_gexp.start, actions.castExprs($t), $expr::ctype]) -> test ) | (gen_for[gens] { Collections.reverse(gens); comprehensionType[] c = (comprehensionType[])gens.toArray(new comprehensionType[gens.size()]); - exprType e = actions.makeExpr($t.get(0)); + exprType e = actions.castExpr($t.get(0)); if (e instanceof Context) { ((Context)e).setContext(expr_contextType.Load); } - etype = new GeneratorExp($testlist_gexp.start, actions.makeExpr($t.get(0)), c); + etype = new GeneratorExp($testlist_gexp.start, actions.castExpr($t.get(0)), c); } ) ) @@ -1295,7 +1297,7 @@ if (a == null) { a = new argumentsType($LAMBDA, new exprType[0], null, null, new exprType[0]); } - etype = new Lambda($LAMBDA, a, actions.makeExpr($test.tree)); + etype = new Lambda($LAMBDA, a, actions.castExpr($test.tree)); } ; @@ -1303,16 +1305,16 @@ trailer [Token begin, PythonTree tree] : LPAREN (arglist - -> ^(LPAREN<Call>[$begin, actions.makeExpr($tree), actions.makeExprs($arglist.args), + -> ^(LPAREN<Call>[$begin, actions.castExpr($tree), actions.castExprs($arglist.args), actions.makeKeywords($arglist.keywords), $arglist.starargs, $arglist.kwargs]) | - -> ^(LPAREN<Call>[$begin, actions.makeExpr($tree), new exprType[0\], new keywordType[0\], null, null]) + -> ^(LPAREN<Call>[$begin, actions.castExpr($tree), new exprType[0\], new keywordType[0\], null, null]) ) RPAREN | LBRACK subscriptlist[$begin] RBRACK - -> ^(LBRACK<Subscript>[$begin, actions.makeExpr($tree), (sliceType)$subscriptlist.tree, $expr::ctype]) + -> ^(LBRACK<Subscript>[$begin, actions.castExpr($tree), actions.castSlice($subscriptlist.tree), $expr::ctype]) | DOT attr - -> ^(DOT<Attribute>[$begin, actions.makeExpr($tree), $attr.text, $expr::ctype]) + -> ^(DOT<Attribute>[$begin, actions.castExpr($tree), $attr.text, $expr::ctype]) ; //subscriptlist: subscript (',' subscript)* [','] @@ -1349,7 +1351,7 @@ $sltype = actions.makeSubscript(null, $c2, $upper2.tree, $sliceop.tree); } | test[expr_contextType.Load] - -> ^(LPAREN<Index>[$test.start, actions.makeExpr($test.tree)]) + -> ^(LPAREN<Index>[$test.start, actions.castExpr($test.tree)]) ; //sliceop: ':' [test] @@ -1364,11 +1366,11 @@ exprlist[expr_contextType ctype] returns [exprType etype] : (expr[null] COMMA) => e+=expr[ctype] (options {k=2;}: COMMA e+=expr[ctype])* (COMMA)? { - $etype = new Tuple($exprlist.start, actions.makeExprs($e), ctype); + $etype = new Tuple($exprlist.start, actions.castExprs($e), ctype); } | expr[ctype] { - $etype = actions.makeExpr($expr.tree); + $etype = actions.castExpr($expr.tree); } ; @@ -1385,7 +1387,7 @@ testlist[expr_contextType ctype] : (test[null] COMMA) => t+=test[ctype] (options {k=2;}: COMMA t+=test[ctype])* (COMMA)? - -> ^(COMMA<Tuple>[$testlist.start, actions.makeExprs($t), ctype]) + -> ^(COMMA<Tuple>[$testlist.start, actions.castExprs($t), ctype]) | test[ctype] ; @@ -1410,8 +1412,8 @@ } : CLASS NAME (LPAREN testlist[expr_contextType.Load]? RPAREN)? COLON suite { - stype = new ClassDef($CLASS, actions.cantBeNone($NAME), actions.makeBases(actions.makeExpr($testlist.tree)), - actions.makeStmts($suite.stypes)); + stype = new ClassDef($CLASS, actions.cantBeNone($NAME), actions.makeBases(actions.castExpr($testlist.tree)), + actions.castStmts($suite.stypes)); } ; @@ -1434,17 +1436,17 @@ } $args=arguments; $keywords=kws; - $starargs=actions.makeExpr($s.tree); - $kwargs=actions.makeExpr($k.tree); + $starargs=actions.castExpr($s.tree); + $kwargs=actions.castExpr($k.tree); } | STAR s=test[expr_contextType.Load] (COMMA DOUBLESTAR k=test[expr_contextType.Load])? { - $starargs=actions.makeExpr($s.tree); - $kwargs=actions.makeExpr($k.tree); + $starargs=actions.castExpr($s.tree); + $kwargs=actions.castExpr($k.tree); } | DOUBLESTAR k=test[expr_contextType.Load] { - $kwargs=actions.makeExpr($k.tree); + $kwargs=actions.castExpr($k.tree); } ; @@ -1453,7 +1455,7 @@ : t1=test[expr_contextType.Load] ((ASSIGN t2=test[expr_contextType.Load]) { - $kws.add(new exprType[]{actions.makeExpr($t1.tree), actions.makeExpr($t2.tree)}); + $kws.add(new exprType[]{actions.castExpr($t1.tree), actions.castExpr($t2.tree)}); } | gen_for[$gens] { @@ -1463,7 +1465,7 @@ $genarg = true; Collections.reverse($gens); comprehensionType[] c = (comprehensionType[])$gens.toArray(new comprehensionType[$gens.size()]); - arguments.add(new GeneratorExp($t1.start, actions.makeExpr($t1.tree), c)); + arguments.add(new GeneratorExp($t1.start, actions.castExpr($t1.tree), c)); } | { if (kws.size() > 0) { @@ -1492,7 +1494,7 @@ } else { e = new exprType[0]; } - gens.add(new comprehensionType($FOR, $exprlist.etype, actions.makeExpr($testlist.tree), e)); + gens.add(new comprehensionType($FOR, $exprlist.etype, actions.castExpr($testlist.tree), e)); } ; @@ -1500,7 +1502,7 @@ list_if[List gens] returns [exprType etype] : IF test[expr_contextType.Load] (list_iter[gens])? { - $etype = actions.makeExpr($test.tree); + $etype = actions.castExpr($test.tree); } ; @@ -1523,7 +1525,7 @@ } else { e = new exprType[0]; } - gens.add(new comprehensionType($FOR, $exprlist.etype, actions.makeExpr($or_test.tree), e)); + gens.add(new comprehensionType($FOR, $exprlist.etype, actions.castExpr($or_test.tree), e)); } ; @@ -1531,14 +1533,14 @@ gen_if[List gens] returns [exprType etype] : IF test[expr_contextType.Load] gen_iter[gens]? { - $etype = actions.makeExpr($test.tree); + $etype = actions.castExpr($test.tree); } ; //yield_expr: 'yield' [testlist] yield_expr : YIELD testlist[expr_contextType.Load]? - -> ^(YIELD<Yield>[$YIELD, actions.makeExpr($testlist.tree)]) + -> ^(YIELD<Yield>[$YIELD, actions.castExpr($testlist.tree)]) ; AS : 'as' ; Modified: trunk/jython/src/org/python/antlr/GrammarActions.java =================================================================== --- trunk/jython/src/org/python/antlr/GrammarActions.java 2008-09-26 17:01:58 UTC (rev 5352) +++ trunk/jython/src/org/python/antlr/GrammarActions.java 2008-10-01 02:01:33 UTC (rev 5353) @@ -151,7 +151,7 @@ errorHandler.error("Generator expression must be parenthesized if not sole argument", t); } - exprType makeExpr(Object o) { + exprType castExpr(Object o) { if (o instanceof exprType) { return (exprType)o; } @@ -162,11 +162,11 @@ } - exprType[] makeExprs(List exprs) { - return makeExprs(exprs, 0); + exprType[] castExprs(List exprs) { + return castExprs(exprs, 0); } - exprType[] makeExprs(List exprs, int start) { + exprType[] castExprs(List exprs, int start) { if (exprs != null) { List<exprType> result = new ArrayList<exprType>(); for (int i=start; i<exprs.size(); i++) { @@ -184,14 +184,14 @@ stmtType[] makeElse(List elseSuite, PythonTree elif) { if (elseSuite != null) { - return makeStmts(elseSuite); + return castStmts(elseSuite); } else if (elif == null) { return new stmtType[0]; } return new stmtType[]{(stmtType)elif}; } - stmtType makeStmt(Object o) { + stmtType castStmt(Object o) { if (o instanceof stmtType) { return (stmtType)o; } else if (o instanceof PythonParser.stmt_return) { @@ -202,15 +202,15 @@ return null; } - stmtType[] makeStmts(PythonTree t) { + stmtType[] castStmts(PythonTree t) { return new stmtType[]{(stmtType)t}; } - stmtType[] makeStmts(List stmts) { + stmtType[] castStmts(List stmts) { if (stmts != null) { List<stmtType> result = new ArrayList<stmtType>(); for (Object o:stmts) { - result.add(makeStmt(o)); + result.add(castStmt(o)); } return (stmtType[])result.toArray(new stmtType[result.size()]); } @@ -231,8 +231,8 @@ if (test == null) { return errorHandler.errorStmt(new PythonTree(t)); } - stmtType[] o = makeStmts(orelse); - stmtType[] b = makeStmts(body); + stmtType[] o = castStmts(orelse); + stmtType[] b = castStmts(body); return new While(t, test, b, o); } @@ -242,27 +242,27 @@ } cantBeNone(target); - stmtType[] o = makeStmts(orelse); - stmtType[] b = makeStmts(body); + stmtType[] o = castStmts(orelse); + stmtType[] b = castStmts(body); return new For(t, target, iter, b, o); } stmtType makeTryExcept(Token t, List body, List handlers, List orelse, List finBody) { - stmtType[] b = makeStmts(body); + stmtType[] b = castStmts(body); excepthandlerType[] e = (excepthandlerType[])handlers.toArray(new excepthandlerType[handlers.size()]); - stmtType[] o = makeStmts(orelse); + stmtType[] o = castStmts(orelse); stmtType te = new TryExcept(t, b, e, o); if (finBody == null) { return te; } - stmtType[] f = makeStmts(finBody); + stmtType[] f = castStmts(finBody); stmtType[] mainBody = new stmtType[]{te}; return new TryFinally(t, mainBody, f); } TryFinally makeTryFinally(Token t, List body, List finBody) { - stmtType[] b = makeStmts(body); - stmtType[] f = makeStmts(finBody); + stmtType[] b = castStmts(body); + stmtType[] f = castStmts(finBody); return new TryFinally(t, b, f); } @@ -277,8 +277,8 @@ } else { a = new argumentsType(t, new exprType[0], null, null, new exprType[0]); } - stmtType[] s = makeStmts(funcStatements); - exprType[] d = makeExprs(decorators); + stmtType[] s = castStmts(funcStatements); + exprType[] d = castExprs(decorators); return new FunctionDef(t, nameToken.getText(), a, s, d); } @@ -287,7 +287,7 @@ checkAssign(lhs); e[0] = lhs; for(int i=0;i<rhs.size() - 1;i++) { - exprType r = makeExpr(rhs.get(i)); + exprType r = castExpr(rhs.get(i)); checkAssign(r); e[i + 1] = r; } @@ -295,7 +295,7 @@ } exprType makeAssignValue(List rhs) { - exprType value = makeExpr(rhs.get(rhs.size() -1)); + exprType value = castExpr(rhs.get(rhs.size() -1)); recurseSetContext(value, expr_contextType.Load); return value; } @@ -320,8 +320,8 @@ argumentsType makeArgumentsType(Token t, List params, Token snameToken, Token knameToken, List defaults) { - exprType[] p = makeExprs(params); - exprType[] d = makeExprs(defaults); + exprType[] p = castExprs(params); + exprType[] d = castExprs(defaults); String s; String k; if (snameToken == null) { @@ -338,7 +338,7 @@ } exprType[] extractArgs(List args) { - return makeExprs(args); + return castExprs(args); } keywordType[] makeKeywords(List args) { @@ -472,7 +472,7 @@ //FROM Walker: modType makeMod(PythonTree t, List stmts) { - stmtType[] s = makeStmts(stmts); + stmtType[] s = castStmts(stmts); return new Module(t, s); } @@ -481,7 +481,7 @@ } modType makeInteractive(PythonTree t, List stmts) { - stmtType[] s = makeStmts(stmts); + stmtType[] s = castStmts(stmts); return new Interactive(t, s); } @@ -490,16 +490,16 @@ return errorHandler.errorStmt(t); } cantBeNone(nameToken); - exprType[] b = makeExprs(bases); - stmtType[] s = makeStmts(body); + exprType[] b = castExprs(bases); + stmtType[] s = castStmts(body); return new ClassDef(t, nameToken.getText(), b, s); } argumentsType makeArgumentsType(PythonTree t, List params, PythonTree snameToken, PythonTree knameToken, List defaults) { - exprType[] p = makeExprs(params); - exprType[] d = makeExprs(defaults); + exprType[] p = castExprs(params); + exprType[] d = castExprs(defaults); String s; String k; if (snameToken == null) { @@ -516,22 +516,22 @@ } stmtType makeTryExcept(PythonTree t, List body, List handlers, List orelse, List finBody) { - stmtType[] b = makeStmts(body); + stmtType[] b = castStmts(body); excepthandlerType[] e = (excepthandlerType[])handlers.toArray(new excepthandlerType[handlers.size()]); - stmtType[] o = makeStmts(orelse); + stmtType[] o = castStmts(orelse); stmtType te = new TryExcept(t, b, e, o); if (finBody == null) { return te; } - stmtType[] f = makeStmts(finBody); + stmtType[] f = castStmts(finBody); stmtType[] mainBody = new stmtType[]{te}; return new TryFinally(t, mainBody, f); } TryFinally makeTryFinally(PythonTree t, List body, List finBody) { - stmtType[] b = makeStmts(body); - stmtType[] f = makeStmts(finBody); + stmtType[] b = castStmts(body); + stmtType[] f = castStmts(finBody); return new TryFinally(t, b, f); } @@ -539,8 +539,8 @@ if (test == null) { return errorHandler.errorStmt(t); } - stmtType[] o = makeStmts(orelse); - stmtType[] b = makeStmts(body); + stmtType[] o = castStmts(orelse); + stmtType[] b = castStmts(body); return new If(t, test, b, o); } @@ -548,8 +548,8 @@ if (test == null) { return errorHandler.errorStmt(t); } - stmtType[] o = makeStmts(orelse); - stmtType[] b = makeStmts(body); + stmtType[] o = castStmts(orelse); + stmtType[] b = castStmts(body); return new While(t, test, b, o); } @@ -558,8 +558,8 @@ return errorHandler.errorStmt(t); } cantBeNone(target); - stmtType[] o = makeStmts(orelse); - stmtType[] b = makeStmts(body); + stmtType[] o = castStmts(orelse); + stmtType[] b = castStmts(body); return new For(t, target, iter, b, o); } @@ -572,7 +572,7 @@ return errorHandler.errorExpr(new PythonTree(t)); } keywordType[] k = makeKeywords(keywords); - exprType[] a = makeExprs(args); + exprType[] a = castExprs(args); return new Call(t, func, a, k, starargs, kwargs); } @@ -661,18 +661,18 @@ exprType e = null; exprType o = null; if (lower != null) { - s = makeExpr(lower); + s = castExpr(lower); } if (colon != null) { isSlice = true; if (upper != null) { - e = makeExpr(upper); + e = castExpr(upper); } } if (sliceop != null) { isSlice = true; if (sliceop != null) { - o = makeExpr(sliceop); + o = castExpr(sliceop); } else { o = new Name(sliceop, "None", expr_contextType.Load); } @@ -705,28 +705,46 @@ List values = new ArrayList(); values.add(left); values.addAll(right); - return new BoolOp(left, op, makeExprs(values)); + return new BoolOp(left, op, castExprs(values)); } BinOp makeBinOp(PythonTree left, operatorType op, List rights) { - BinOp current = new BinOp(left, makeExpr(left), op, makeExpr(rights.get(0))); + BinOp current = new BinOp(left, castExpr(left), op, castExpr(rights.get(0))); for (int i = 1; i< rights.size(); i++) { - exprType right = makeExpr(rights.get(i)); + exprType right = castExpr(rights.get(i)); current = new BinOp(left, current, op, right); } return current; } BinOp makeBinOp(PythonTree left, List ops, List rights) { - BinOp current = new BinOp(left, makeExpr(left), (operatorType)ops.get(0), makeExpr(rights.get(0))); + BinOp current = new BinOp(left, castExpr(left), (operatorType)ops.get(0), castExpr(rights.get(0))); for (int i = 1; i< rights.size(); i++) { - exprType right = makeExpr(rights.get(i)); + exprType right = castExpr(rights.get(i)); operatorType op = (operatorType)ops.get(i); current = new BinOp(left, current, op, right); } return current; } + sliceType[] castSlices(List slices) { + if (slices != null) { + List<sliceType> result = new ArrayList<sliceType>(); + for (Object o:slices) { + result.add(castSlice(o)); + } + return (sliceType[])result.toArray(new sliceType[result.size()]); + } + return new sliceType[0]; + } + + sliceType castSlice(Object o) { + if (o instanceof sliceType) { + return (sliceType)o; + } + return errorHandler.errorSlice((PythonTree)o); + } + sliceType makeSliceType(Token begin, Token c1, Token c2, List sltypes) { boolean isTuple = false; if (c1 != null || c2 != null) { @@ -753,12 +771,12 @@ s = new Index(begin, t); } } else if (sltypes.size() == 1) { - s = (sliceType)sltypes.get(0); + s = castSlice(sltypes.get(0)); } else if (sltypes.size() != 0) { extslice = true; } if (extslice) { - sliceType[] st = (sliceType[])sltypes.toArray(new sliceType[sltypes.size()]); + sliceType[] st = castSlices(sltypes);//.toArray(new sliceType[sltypes.size()]); s = new ExtSlice(begin, st); } return s; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2008-09-26 17:02:02
|
Revision: 5352 http://jython.svn.sourceforge.net/jython/?rev=5352&view=rev Author: fwierzbicki Date: 2008-09-26 17:01:58 +0000 (Fri, 26 Sep 2008) Log Message: ----------- Fixes an infinite loop that occurs when there is an unterminated triple string and ListErrorHandler is being used. Modified Paths: -------------- trunk/jython/grammar/Python.g Modified: trunk/jython/grammar/Python.g =================================================================== --- trunk/jython/grammar/Python.g 2008-09-26 15:37:19 UTC (rev 5351) +++ trunk/jython/grammar/Python.g 2008-09-26 17:01:58 UTC (rev 5352) @@ -250,6 +250,10 @@ } catch (NoViableAltException nva) { errorHandler.reportError(this, nva); errorHandler.recover(this, nva); // throw out current char and try again + } catch (FailedPredicateException fp) { + //XXX: added this for failed STRINGPART + errorHandler.reportError(this, fp); + errorHandler.recover(this, fp); // throw out current char and try again } catch (RecognitionException re) { errorHandler.reportError(this, re); // match() routine has already called recover() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <th...@us...> - 2008-09-26 15:37:35
|
Revision: 5351 http://jython.svn.sourceforge.net/jython/?rev=5351&view=rev Author: thobes Date: 2008-09-26 15:37:19 +0000 (Fri, 26 Sep 2008) Log Message: ----------- Fix to allow the yield statement to apear in the first iterator of a generator expression. This exposes another problem where the stack state is not persisted from yielding to resuming. Modified Paths: -------------- trunk/jython/src/org/python/compiler/ScopesCompiler.java Modified: trunk/jython/src/org/python/compiler/ScopesCompiler.java =================================================================== --- trunk/jython/src/org/python/compiler/ScopesCompiler.java 2008-09-26 14:41:55 UTC (rev 5350) +++ trunk/jython/src/org/python/compiler/ScopesCompiler.java 2008-09-26 15:37:19 UTC (rev 5351) @@ -14,18 +14,17 @@ private ScopeInfo cur = null; private Hashtable nodeScopes; - private int level = 0; + private int level = 0; private int func_level = 0; - public ScopesCompiler(CompilationContext code_compiler, - Hashtable nodeScopes) { + public ScopesCompiler(CompilationContext code_compiler, Hashtable nodeScopes) { this.code_compiler = code_compiler; this.nodeScopes = nodeScopes; scopes = new Stack(); } public void beginScope(String name, int kind, PythonTree node, - ArgListCompiler ac) { + ArgListCompiler ac) { if (cur != null) { scopes.push(cur); } @@ -43,14 +42,15 @@ level--; ScopeInfo up = null; if (!scopes.empty()) { - up = (ScopeInfo)scopes.pop(); + up = (ScopeInfo) scopes.pop(); } //Go into the stack to find a non class containing scope to use making the closure //See PEP 227 int dist = 1; ScopeInfo referenceable = up; - for(int i = scopes.size() - 1; i >= 0 && referenceable.kind == CLASSSCOPE; i--, dist++){ - referenceable = ((ScopeInfo)scopes.get(i)); + for (int i = scopes.size() - 1; i >= 0 + && referenceable.kind == CLASSSCOPE; i--, dist++) { + referenceable = ((ScopeInfo) scopes.get(i)); } cur.cook(referenceable, dist, code_compiler); cur.dump(); // debug @@ -60,7 +60,7 @@ public void parse(PythonTree node) throws Exception { try { visit(node); - } catch(Throwable t) { + } catch (Throwable t) { throw org.python.core.ParserFacade.fixParseError(null, t, code_compiler.getFilename()); } @@ -76,8 +76,7 @@ @Override public Object visitModule(org.python.antlr.ast.Module node) - throws Exception - { + throws Exception { beginScope("<file-top>", TOPSCOPE, node, null); suite(node.body); endScope(); @@ -110,7 +109,7 @@ beginScope(node.name, FUNCSCOPE, node, ac); int n = ac.names.size(); for (int i = 0; i < n; i++) { - cur.addParam((String)ac.names.elementAt(i)); + cur.addParam((String) ac.names.elementAt(i)); } for (int i = 0; i < ac.init_code.size(); i++) { visit((stmtType) ac.init_code.elementAt(i)); @@ -133,10 +132,10 @@ beginScope("<lambda>", FUNCSCOPE, node, ac); for (Object o : ac.names) { - cur.addParam((String)o); + cur.addParam((String) o); } - for (Object o : ac.init_code) { - visit((stmtType)o); + for (Object o : ac.init_code) { + visit((stmtType) o); } cur.markFromParam(); visit(node.body); @@ -190,21 +189,21 @@ String name = node.names[i]; int prev = cur.addGlobal(name); if (prev >= 0) { - if ((prev&FROM_PARAM) != 0) { - code_compiler.error("name '"+name+"' is local and global", - true,node); + if ((prev & FROM_PARAM) != 0) { + code_compiler.error("name '" + name + + "' is local and global", true, node); } - if ((prev&GLOBAL) != 0) { + if ((prev & GLOBAL) != 0) { continue; } String what; - if ((prev&BOUND) != 0) { + if ((prev & BOUND) != 0) { what = "assignment"; } else { what = "use"; } - code_compiler.error("name '"+name+"' declared global after "+ - what, false, node); + code_compiler.error("name '" + name + + "' declared global after " + what, false, node); } } return null; @@ -238,7 +237,7 @@ String name = node.id; if (node.ctx != expr_contextType.Load) { if (name.equals("__debug__")) { - code_compiler.error("can not assign to __debug__", true,node); + code_compiler.error("can not assign to __debug__", true, node); } cur.addBound(name); } else { @@ -249,7 +248,8 @@ @Override public Object visitListComp(ListComp node) throws Exception { - String tmp ="_[" + node.getLine() + "_" + node.getCharPositionInLine() + "]"; + String tmp = "_[" + node.getLine() + "_" + node.getCharPositionInLine() + + "]"; cur.addBound(tmp); traverse(node); return null; @@ -265,30 +265,60 @@ @Override public Object visitGeneratorExp(GeneratorExp node) throws Exception { + // The first iterator is evaluated in the outer scope + /*if (node.generators != null && node.generators.length > 0) { + visit(node.generators[0].iter); + }*/ String bound_exp = "_(x)"; - String tmp ="_(" + node.getLine() + "_" + node.getCharPositionInLine() + ")"; + String tmp = "_(" + node.getLine() + "_" + node.getCharPositionInLine() + + ")"; def(tmp); ArgListCompiler ac = new ArgListCompiler(); - ac.visitArgs(new argumentsType(node, new exprType[]{new Name(node.token, bound_exp, - expr_contextType.Param)}, null, null, new exprType[0])); + ac.visitArgs(new argumentsType(node, new exprType[] { new Name( + node.token, bound_exp, expr_contextType.Param) }, null, null, + new exprType[0])); beginScope(tmp, FUNCSCOPE, node, ac); cur.addParam(bound_exp); cur.markFromParam(); cur.generator = true; cur.yield_count++; + // The reset of the iterators are evaluated in the inner scope + /*if (node.elt != null) { + visit(node.elt); + } + if (node.generators != null) { + for (int i = 0; i < node.generators.length; i++) { + if (node.generators[i] != null) { + if (i == 0) { + visit(node.generators[i].target); + if (node.generators[i].ifs != null) { + for (exprType cond : node.generators[i].ifs) { + if (cond != null) { + visit(cond); + } + } + } + } else { + visit(node.generators[i]); + } + } + } + } + /*/ traverse(node); + //*/ endScope(); return null; } @Override - public Object visitWith(With node) throws Exception { + public Object visitWith(With node) throws Exception { cur.max_with_count++; traverse(node); - + return null; } - + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |