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: <zy...@us...> - 2009-02-07 18:14:35
|
Revision: 6021 http://jython.svn.sourceforge.net/jython/?rev=6021&view=rev Author: zyasoft Date: 2009-02-07 18:14:33 +0000 (Sat, 07 Feb 2009) Log Message: ----------- Further refinement of nested scopes. Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBytecode.java Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-07 17:47:19 UTC (rev 6020) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-07 18:14:33 UTC (rev 6021) @@ -779,29 +779,14 @@ break; case Opcode.LOAD_CLOSURE: { -//// if (debug) { -//// System.err.println("LOAD_CLOSURE: " + Arrays.toString(f.f_env)); -//// } -// PyCell cell = (PyCell) (f.getclosure(oparg)); -// if (cell.ob_ref == null) { -// cell.ob_ref = f.getlocal(oparg); -// } -////// cell.ob_ref = f.getname(co_freevars[oparg]); -// stack.push(cell); -// stack.push(f.getclosure(oparg)); - - PyCell cell = (PyCell) (f.getclosure(oparg)); if (cell.ob_ref == null) { -// if (oparg < co_nlocals) { -// cell.ob_ref = f.getlocal(oparg); -// } else { -// String name = co_cellvars[oparg]; - -// cell.ob_ref = f.getname(name); -// } - - String name = co_cellvars[oparg]; + String name; + if (oparg >= co_cellvars.length) { + name = co_freevars[oparg - co_cellvars.length]; + } else { + name = co_cellvars[oparg]; + } // System.err.println("Loading closure: " + name); // XXX - consider some efficient lookup mechanism, like a hash if (f.f_fastlocals != null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-07 17:47:22
|
Revision: 6020 http://jython.svn.sourceforge.net/jython/?rev=6020&view=rev Author: zyasoft Date: 2009-02-07 17:47:19 +0000 (Sat, 07 Feb 2009) Log Message: ----------- Fixes nested scope treatment of free variables. Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBytecode.java branches/pbcvm/src/org/python/core/PySystemState.java Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-07 06:29:37 UTC (rev 6019) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-07 17:47:19 UTC (rev 6020) @@ -2,20 +2,27 @@ import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; public class PyBytecode extends PyBaseCode { // for debugging - private int count = 0; // total number of opcodes run - private int maxCount = -1; // less than my buffer on iterm - private static PyObject opname; + private int count = 0; // total number of opcodes run so far in this code obj + private int maxCount = -1; // if -1, no cap on number of opcodes than can be run public static boolean defaultDebug = false; + private static PyObject dis; - private static synchronized PyObject getOpname() { + private static synchronized PyObject get_dis() { + if (dis == null) { + dis = __builtin__.__import__("dis"); + } + return dis; + } + private static PyObject opname; + + private static synchronized PyObject get_opname() { if (opname == null) { - opname = __builtin__.__import__("dis").__getattr__("opname"); + opname = get_dis().__getattr__("opname"); } return opname; } @@ -37,7 +44,7 @@ public final PyObject[] co_consts; public final String[] co_names; public final int co_stacksize; // XXX - use to convert PyStack to use PyObject[] instead of ArrayList<PyObject> - public final PyObject[] co_lnotab; // ignored + public final String co_lnotab; // ignored for now private final static int CALL_FLAG_VAR = 1; private final static int CALL_FLAG_KW = 2; @@ -73,7 +80,7 @@ co_consts = constants; co_names = names; co_code = codestring.toCharArray(); - co_lnotab = null; // ignore + co_lnotab = lnotab; // ignore } private static final String[] __members__ = { "co_name", "co_argcount", @@ -201,7 +208,7 @@ if (debug) { System.err.println(co_name + ":" + count + "," + f.f_lasti + "> opcode: " + - getOpname().__getitem__(Py.newInteger(opcode)) + + get_opname().__getitem__(Py.newInteger(opcode)) + (opcode >= Opcode.HAVE_ARGUMENT ? ", oparg: " + oparg : "") + ", stack: " + stack.toString() + ", blocks: " + stringify_blocks(f)); @@ -245,9 +252,18 @@ if (debug) { System.err.println(co_name + ":" + f.f_lasti + "/" + co_code.length + ", cells:" + Arrays.toString(co_cellvars) + ", free:" + Arrays.toString(co_freevars)); + int i = 0; + for (String cellvar : co_cellvars) { + System.err.println(cellvar + " = " + f.f_env[i++]); + } + for (String freevar : co_freevars) { + System.err.println(freevar + " = " + f.f_env[i++]); + } + get_dis().invoke("disassemble", this); + } if (f.f_lasti >= co_code.length) { - throw Py.SystemError(""); + throw Py.SystemError(""); // XXX - chose an appropriate error!!! } next_instr = f.f_lasti; @@ -763,22 +779,53 @@ break; case Opcode.LOAD_CLOSURE: { - if (debug) { - System.err.println("LOAD_CLOSURE: " + Arrays.toString(f.f_env)); - } +//// if (debug) { +//// System.err.println("LOAD_CLOSURE: " + Arrays.toString(f.f_env)); +//// } +// PyCell cell = (PyCell) (f.getclosure(oparg)); +// if (cell.ob_ref == null) { +// cell.ob_ref = f.getlocal(oparg); +// } +////// cell.ob_ref = f.getname(co_freevars[oparg]); +// stack.push(cell); +// stack.push(f.getclosure(oparg)); + + PyCell cell = (PyCell) (f.getclosure(oparg)); if (cell.ob_ref == null) { - cell.ob_ref = f.getlocal(oparg); +// if (oparg < co_nlocals) { +// cell.ob_ref = f.getlocal(oparg); +// } else { +// String name = co_cellvars[oparg]; + +// cell.ob_ref = f.getname(name); +// } + + String name = co_cellvars[oparg]; +// System.err.println("Loading closure: " + name); + // XXX - consider some efficient lookup mechanism, like a hash + if (f.f_fastlocals != null) { + int i = 0; + boolean matched = false; + for (String match : co_varnames) { + if (match.equals(name)) { + matched = true; + break; + } + i++; + } + if (matched) { + cell.ob_ref = f.f_fastlocals[i]; + } + } else { + cell.ob_ref = f.f_locals.__finditem__(name); + } } -// cell.ob_ref = f.getname(co_freevars[oparg]); stack.push(cell); break; } case Opcode.LOAD_DEREF: { - if (debug) { - System.err.println("LOAD_DEREF: " + Arrays.toString(f.f_env)); - } stack.push(f.getderef(oparg)); break; } @@ -1354,7 +1401,7 @@ @Override public String toString() { - return "<" + getOpname().__getitem__(Py.newInteger(b_type)) + "," + + return "<" + get_opname().__getitem__(Py.newInteger(b_type)) + "," + b_handler + "," + b_level + ">"; } } Modified: branches/pbcvm/src/org/python/core/PySystemState.java =================================================================== --- branches/pbcvm/src/org/python/core/PySystemState.java 2009-02-07 06:29:37 UTC (rev 6019) +++ branches/pbcvm/src/org/python/core/PySystemState.java 2009-02-07 17:47:19 UTC (rev 6020) @@ -1028,7 +1028,10 @@ if(exc == null) return new PyTuple(Py.None, Py.None, Py.None); PyObject tb = exc.traceback; - return new PyTuple(exc.type, exc.value, tb == null ? Py.None : tb); + PyObject value = exc.value; + return new PyTuple(exc.type, + value == null ? Py.None : value, + tb == null ? Py.None : tb); } public static void exc_clear() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-07 06:29:41
|
Revision: 6019 http://jython.svn.sourceforge.net/jython/?rev=6019&view=rev Author: zyasoft Date: 2009-02-07 06:29:37 +0000 (Sat, 07 Feb 2009) Log Message: ----------- Fixed IMPORT_STAR for nested modules. sys.exc_info now returns Py.None for the traceback (this fixes an issue in PBC where null is being used as both a sentinel value by __getitem__ AND an actual value for exception handling). Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBytecode.java branches/pbcvm/src/org/python/core/PySystemState.java branches/pbcvm/src/org/python/core/imp.java Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-07 04:04:54 UTC (rev 6018) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-07 06:29:37 UTC (rev 6019) @@ -9,7 +9,7 @@ // for debugging private int count = 0; // total number of opcodes run - private int maxCount = 10000; // less than my buffer on iterm + private int maxCount = -1; // less than my buffer on iterm private static PyObject opname; public static boolean defaultDebug = false; @@ -263,7 +263,7 @@ f.f_savedlocals = null; } - while (!debug || (count < maxCount)) { // XXX - replace with while(true) + while (!debug || (maxCount == -1 || count < maxCount)) { // XXX - replace with while(true) try { @@ -870,7 +870,7 @@ } case Opcode.IMPORT_STAR: { - String module = stack.pop().toString(); + PyObject module = stack.pop(); imp.importAll(module, f); break; } @@ -1109,9 +1109,11 @@ if (b.b_type == Opcode.SETUP_EXCEPT) { exc.normalize(); } - stack.push(new PyStackException(exc)); - stack.dup(); // x3 to conform with CPython's calling convention, which - stack.dup(); // stores the type, val, tb separately on the stack + stack.push(exc.traceback); + stack.push(exc.value); + stack.push(new PyStackException(exc)); // instead of stack.push(exc.type);, like CPython +// stack.dup(); // x3 to conform with CPython's calling convention, which +// stack.dup(); // stores the type, val, tb separately on the stack } else { if (why == Why.RETURN || why == Why.CONTINUE) { stack.push(retval); Modified: branches/pbcvm/src/org/python/core/PySystemState.java =================================================================== --- branches/pbcvm/src/org/python/core/PySystemState.java 2009-02-07 04:04:54 UTC (rev 6018) +++ branches/pbcvm/src/org/python/core/PySystemState.java 2009-02-07 06:29:37 UTC (rev 6019) @@ -1027,7 +1027,8 @@ PyException exc = Py.getThreadState().exception; if(exc == null) return new PyTuple(Py.None, Py.None, Py.None); - return new PyTuple(exc.type, exc.value, exc.traceback); + PyObject tb = exc.traceback; + return new PyTuple(exc.type, exc.value, tb == null ? Py.None : tb); } public static void exc_clear() { Modified: branches/pbcvm/src/org/python/core/imp.java =================================================================== --- branches/pbcvm/src/org/python/core/imp.java 2009-02-07 04:04:54 UTC (rev 6018) +++ branches/pbcvm/src/org/python/core/imp.java 2009-02-07 06:29:37 UTC (rev 6019) @@ -827,22 +827,15 @@ return submods; } - private static PyTuple all = null; + private final static PyTuple all = new PyTuple(Py.newString('*')); - private synchronized static PyTuple getStarArg() { - if (all == null) { - all = new PyTuple(Py.newString('*')); - } - return all; - } - /** * Called from jython generated code when a statement like "from spam.eggs * import *" is executed. */ public static void importAll(String mod, PyFrame frame) { PyObject module = __builtin__.__import__(mod, frame.f_globals, frame - .getLocals(), getStarArg()); + .getLocals(), all); PyObject names; boolean filter = true; if (module instanceof PyJavaPackage) { @@ -860,6 +853,27 @@ loadNames(names, module, frame.getLocals(), filter); } + public static void importAll(PyObject module, PyFrame frame) { + PyObject names; + boolean filter = true; + if (module instanceof PyJavaPackage) { + names = ((PyJavaPackage) module).fillDir(); + } else { + PyObject __all__ = module.__findattr__("__all__"); + if (__all__ != null) { + names = __all__; + filter = false; + } else { + names = module.__dir__(); + } + } + + loadNames(names, module, frame.getLocals(), filter); + } + + + + /** * From a module, load the attributes found in <code>names</code> into * locals. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-07 04:04:56
|
Revision: 6018 http://jython.svn.sourceforge.net/jython/?rev=6018&view=rev Author: zyasoft Date: 2009-02-07 04:04:54 +0000 (Sat, 07 Feb 2009) Log Message: ----------- Fixed stack ops (DUP_TOPX, ROT_THREE, ROT_FOUR), INPLACE_POWER. Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBytecode.java Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-06 07:42:15 UTC (rev 6017) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-07 04:04:54 UTC (rev 6018) @@ -9,8 +9,9 @@ // for debugging private int count = 0; // total number of opcodes run - private int maxCount = 900; // less than my buffer on iterm + private int maxCount = 10000; // less than my buffer on iterm private static PyObject opname; + public static boolean defaultDebug = false; private static synchronized PyObject getOpname() { if (opname == null) { @@ -18,8 +19,12 @@ } return opname; } - private boolean debug = false; + private boolean debug; + public static void _allDebug(boolean setting) { + defaultDebug = setting; + } + public PyObject _debug(int maxCount) { debug = maxCount > 0; this.maxCount = maxCount; @@ -49,6 +54,9 @@ String codestring, PyObject[] constants, String[] names, String varnames[], String filename, String name, int firstlineno, String lnotab, String[] cellvars, String[] freevars) { + + debug = defaultDebug; + co_argcount = nargs = argcount; co_varnames = varnames; co_nlocals = nlocals; // maybe assert = varnames.length; @@ -300,15 +308,15 @@ break; case Opcode.ROT_TWO: - stack.rot(2); + stack.rot2(); break; case Opcode.ROT_THREE: - stack.rot(3); + stack.rot3(); break; case Opcode.ROT_FOUR: - stack.rot(4); + stack.rot4(); break; case Opcode.DUP_TOP: @@ -459,7 +467,7 @@ case Opcode.INPLACE_POWER: { PyObject b = stack.pop(); PyObject a = stack.pop(); - stack.push(a.__ipow__(b)); + stack.push(a._ipow(b)); break; } @@ -693,7 +701,7 @@ case Opcode.BUILD_CLASS: { PyObject methods = stack.pop(); - PyObject bases[] = ((PySequenceList)(stack.pop())).getArray(); + PyObject bases[] = ((PySequenceList) (stack.pop())).getArray(); String name = stack.pop().toString(); stack.push(Py.makeClass(name, bases, methods)); break; @@ -758,7 +766,7 @@ if (debug) { System.err.println("LOAD_CLOSURE: " + Arrays.toString(f.f_env)); } - PyCell cell = (PyCell)(f.getclosure(oparg)); + PyCell cell = (PyCell) (f.getclosure(oparg)); if (cell.ob_ref == null) { cell.ob_ref = f.getlocal(oparg); } @@ -1018,7 +1026,7 @@ case Opcode.MAKE_CLOSURE: { PyCode code = (PyCode) stack.pop(); - PyObject[] closure_cells = ((PySequenceList)(stack.pop())).getArray(); + PyObject[] closure_cells = ((PySequenceList) (stack.pop())).getArray(); // PyObject[] closure_cells = new PyCell[src_closure_cells.length]; // for (int i = 0; i < src_closure_cells.length; i++) { // PyCell cell = new PyCell(); @@ -1061,6 +1069,9 @@ PyException pye = Py.JavaError(t); why = Why.EXCEPTION; ts.exception = pye; + if (debug) { + System.err.println("Caught exception:" + pye); + } } if (why == Why.YIELD) { @@ -1074,7 +1085,9 @@ while (why != Why.NOT && blocksLeft(f)) { PyTryBlock b = popBlock(f); -// System.err.println("Processing block: " + b); + if (debug) { + System.err.println("Processing block: " + b); + } assert (why != Why.YIELD); if (b.b_type == Opcode.SETUP_LOOP && why == Why.CONTINUE) { pushBlock(f, b); @@ -1096,8 +1109,9 @@ if (b.b_type == Opcode.SETUP_EXCEPT) { exc.normalize(); } - stack.push(new PyStackException(exc)); // x3 to conform with CPython's calling convention, which - stack.dup(2); // stores the type, val, tb separately on the stack + stack.push(new PyStackException(exc)); + stack.dup(); // x3 to conform with CPython's calling convention, which + stack.dup(); // stores the type, val, tb separately on the stack } else { if (why == Why.RETURN || why == Why.CONTINUE) { stack.push(retval); @@ -1266,9 +1280,9 @@ } void dup(int n) { - PyObject v = top(); - for (int i = 0; i < n; i++) { - stack.add(v); + int length = stack.size(); + for (int i = n; i > 0; i--) { + stack.add(stack.get(length - i)); } } @@ -1281,14 +1295,36 @@ return ret; } - void rot(int n) { - int end = stack.size(); - List<PyObject> lastN = stack.subList(end - n, end); -// System.err.print("rot(" + n + "): " + lastN.toString() + " -> "); - Collections.rotate(lastN, n - 1); -// System.err.println("rot(" + n + "): " + lastN.toString()); + void rot2() { + int length = stack.size(); + PyObject v = stack.get(length - 1); + PyObject w = stack.get(length - 2); + stack.set(length - 1, w); + stack.set(length - 2, v); } + void rot3() { + int length = stack.size(); + PyObject v = stack.get(length - 1); + PyObject w = stack.get(length - 2); + PyObject x = stack.get(length - 3); + stack.set(length - 1, w); + stack.set(length - 2, x); + stack.set(length - 3, v); + } + + void rot4() { + int length = stack.size(); + PyObject u = stack.get(length - 1); + PyObject v = stack.get(length - 2); + PyObject w = stack.get(length - 3); + PyObject x = stack.get(length - 4); + stack.set(length - 1, v); + stack.set(length - 2, w); + stack.set(length - 3, x); + stack.set(length - 4, u); + } + int size() { return stack.size(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-06 07:42:18
|
Revision: 6017 http://jython.svn.sourceforge.net/jython/?rev=6017&view=rev Author: zyasoft Date: 2009-02-06 07:42:15 +0000 (Fri, 06 Feb 2009) Log Message: ----------- Fixed slice, exec support. Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBytecode.java Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-06 07:07:45 UTC (rev 6016) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-06 07:42:15 UTC (rev 6017) @@ -566,8 +566,8 @@ case Opcode.STORE_SLICE + 1: case Opcode.STORE_SLICE + 2: case Opcode.STORE_SLICE + 3: { - PyObject stop = (((opcode - Opcode.SLICE) & 2) != 0) ? stack.pop() : null; - PyObject start = (((opcode - Opcode.SLICE) & 1) != 0) ? stack.pop() : null; + PyObject stop = (((opcode - Opcode.STORE_SLICE) & 2) != 0) ? stack.pop() : null; + PyObject start = (((opcode - Opcode.STORE_SLICE) & 1) != 0) ? stack.pop() : null; PyObject obj = stack.pop(); PyObject value = stack.pop(); obj.__setslice__(start, stop, value); @@ -578,8 +578,8 @@ case Opcode.DELETE_SLICE + 1: case Opcode.DELETE_SLICE + 2: case Opcode.DELETE_SLICE + 3: { - PyObject stop = (((opcode - Opcode.SLICE) & 2) != 0) ? stack.pop() : null; - PyObject start = (((opcode - Opcode.SLICE) & 1) != 0) ? stack.pop() : null; + PyObject stop = (((opcode - Opcode.DELETE_SLICE) & 2) != 0) ? stack.pop() : null; + PyObject start = (((opcode - Opcode.DELETE_SLICE) & 1) != 0) ? stack.pop() : null; PyObject obj = stack.pop(); obj.__delslice__(start, stop); break; @@ -662,16 +662,7 @@ PyObject locals = stack.pop(); PyObject globals = stack.pop(); PyObject code = stack.pop(); - - if ((locals == null || locals == Py.None) && - (globals == null || globals == Py.None)) { - throw Py.SystemError("globals and locals cannot be NULL"); - } Py.exec(code, globals, locals); - - if (!(globals.__finditem__("__builtins__").__nonzero__())) { - globals.__setitem__("__builtins__", f.f_builtins); - } break; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-06 07:07:47
|
Revision: 6016 http://jython.svn.sourceforge.net/jython/?rev=6016&view=rev Author: zyasoft Date: 2009-02-06 07:07:45 +0000 (Fri, 06 Feb 2009) Log Message: ----------- Fixed some cellvar/freevar nested scope issues, with work to continue. Added a slightly modified version of pycimport.py from Tobias' pyasm implementation in the sandbox. This, in conjunction with compileall (run from CPython; compileall.compile_dir against the unit tests) is allowing a significant fraction of the regrtest to run against PBC-VM. Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBytecode.java Added Paths: ----------- branches/pbcvm/Lib/pycimport.py Added: branches/pbcvm/Lib/pycimport.py =================================================================== --- branches/pbcvm/Lib/pycimport.py (rev 0) +++ branches/pbcvm/Lib/pycimport.py 2009-02-06 07:07:45 UTC (rev 6016) @@ -0,0 +1,84 @@ +import sys +import os + +from org.python.core import imp as _imp, PyFrame as _Frame, Py as _Py +from marshal import Unmarshaller + +__debugging__ = False + +def __readPycHeader(file): + def read(): + return ord(file.read(1)) + magic = read() | (read()<<8) + if not ( file.read(1) == '\r' and file.read(1) == '\n' ): + raise TypeError("not valid pyc-file") + mtime = read() | (read()<<8) | (read()<<16) | (read()<<24) + return magic, mtime + +def __makeModule(name, code, path): + module = _imp.addModule(name) + builtins = _Py.getSystemState().builtins + frame = _Frame(code, module.__dict__, module.__dict__, builtins) + module.__file__ = path + code.call(frame) # execute module code + return module + +class __Importer(object): + def __init__(self, path): + if __debugging__: print "Importer invoked" + self.__path = path + def find_module(self, fullname, path=None): + if __debugging__: + print "Importer.find_module(fullname=%s, path=%s)" % ( + repr(fullname), repr(path)) + path = fullname.split('.') + filename = path[-1] + path = path[:-1] + pycfile = os.path.join(self.__path, *(path + [filename + '.pyc'])) + pyfile = os.path.join(self.__path, *(path + [filename + '.py'])) + if os.path.exists(pycfile): + f = open(pycfile, 'rb') + try: + magic, mtime = __readPycHeader(f) + except: + return None # abort! not a valid pyc-file + f.close() + if os.path.exists(pyfile): + pytime = os.stat(pyfile).st_mtime + if pytime > mtime: + return None # abort! py-file was newer + return self + else: + return None # abort! pyc-file does not exist + def load_module(self, fullname): + path = fullname.split('.') + path[-1] += '.pyc' + filename = os.path.join(self.__path, *path) + f = open(filename, 'rb') + magic, mtime = __readPycHeader(f) + #code = Unmarshaller(f, magic=magic).load() + code = Unmarshaller(f).load() + if __debugging__: print "Successfully loaded:", fullname + return __makeModule( fullname, code, filename ) + +class __MetaImporter(object): + def __init__(self): + self.__importers = {} + def find_module(self, fullname, path): + if __debugging__: print "MetaImporter.find_module(%s, %s)" % ( + repr(fullname), repr(path)) + for _path in sys.path: + if _path not in self.__importers: + try: + self.__importers[_path] = __Importer(_path) + except: + self.__importers[_path] = None + importer = self.__importers[_path] + if importer is not None: + loader = importer.find_module(fullname, path) + if loader is not None: + return loader + else: + return None + +sys.meta_path.insert(0, __MetaImporter()) Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-06 03:50:10 UTC (rev 6015) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-06 07:07:45 UTC (rev 6016) @@ -1,6 +1,7 @@ package org.python.core; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -234,7 +235,8 @@ // in a shadow version of the frame that we copy back to on entry/exit and downcalls if (debug) { - System.err.println("Entry with " + f.f_lasti + " into " + co_code.length); + System.err.println(co_name + ":" + f.f_lasti + "/" + co_code.length + + ", cells:" + Arrays.toString(co_cellvars) + ", free:" + Arrays.toString(co_freevars)); } if (f.f_lasti >= co_code.length) { throw Py.SystemError(""); @@ -700,7 +702,7 @@ case Opcode.BUILD_CLASS: { PyObject methods = stack.pop(); - PyObject bases[] = ((PyTuple)(stack.pop())).getArray(); // new PyTuple(stack.pop())).getArray(); + PyObject bases[] = ((PySequenceList)(stack.pop())).getArray(); String name = stack.pop().toString(); stack.push(Py.makeClass(name, bases, methods)); break; @@ -761,13 +763,26 @@ f.dellocal(oparg); break; - case Opcode.LOAD_CLOSURE: - stack.push(f.getclosure(oparg)); + case Opcode.LOAD_CLOSURE: { + if (debug) { + System.err.println("LOAD_CLOSURE: " + Arrays.toString(f.f_env)); + } + PyCell cell = (PyCell)(f.getclosure(oparg)); + if (cell.ob_ref == null) { + cell.ob_ref = f.getlocal(oparg); + } +// cell.ob_ref = f.getname(co_freevars[oparg]); + stack.push(cell); break; + } - case Opcode.LOAD_DEREF: + case Opcode.LOAD_DEREF: { + if (debug) { + System.err.println("LOAD_DEREF: " + Arrays.toString(f.f_env)); + } stack.push(f.getderef(oparg)); break; + } case Opcode.STORE_DEREF: f.setderef(oparg, stack.pop()); @@ -864,7 +879,7 @@ case Opcode.IMPORT_FROM: String name = co_names[oparg]; try { - stack.push(stack.pop().__getattr__(name)); + stack.push(stack.top().__getattr__(name)); } catch (PyException pye) { if (Py.matchException(pye, Py.AttributeError)) { @@ -1012,7 +1027,16 @@ case Opcode.MAKE_CLOSURE: { PyCode code = (PyCode) stack.pop(); - PyObject[] closure_cells = new PyTuple(stack.pop()).getArray(); + PyObject[] closure_cells = ((PySequenceList)(stack.pop())).getArray(); +// PyObject[] closure_cells = new PyCell[src_closure_cells.length]; +// for (int i = 0; i < src_closure_cells.length; i++) { +// PyCell cell = new PyCell(); +// cell.ob_ref = src_closure_cells[i]; +// closure_cells[i] = cell; +// } +//// for (int i = 0; i < src_closure_cells.length; i++) { +//// closure_cells[i] = f.getclosure(i); +//// } PyObject[] defaults = stack.popN(oparg); PyFunction func = new PyFunction(f.f_globals, defaults, code, closure_cells); stack.push(func); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-06 03:50:14
|
Revision: 6015 http://jython.svn.sourceforge.net/jython/?rev=6015&view=rev Author: zyasoft Date: 2009-02-06 03:50:10 +0000 (Fri, 06 Feb 2009) Log Message: ----------- Changed read/write binary_float so that it doesn't go through struct but instead j.l.Double's support for converting to/from bits. This also resolves endian ordering issues so that it follows the native endian ordering. Modified Paths: -------------- branches/pbcvm/src/org/python/modules/_marshal.java Modified: branches/pbcvm/src/org/python/modules/_marshal.java =================================================================== --- branches/pbcvm/src/org/python/modules/_marshal.java 2009-02-05 18:08:10 UTC (rev 6014) +++ branches/pbcvm/src/org/python/modules/_marshal.java 2009-02-06 03:50:10 UTC (rev 6015) @@ -105,8 +105,8 @@ } private void write_long64(long x) { - write_int((int) (x & 0xff)); - write_int((int) ((x >> 32) & 0xff)); + write_int((int) (x & 0xffffffff)); + write_int((int) ((x >> 32) & 0xffffffff)); } // writes output in 15 bit "digits" @@ -129,10 +129,8 @@ write_string(f.__repr__().toString()); } - // XXX - cache this struct object - // XXX - also fix reversed struct stuff! private void write_binary_float(PyFloat f) { - write_string(struct.pack(new PyObject[]{Py.newString("d"), f}).toString()); + write_long64(Double.doubleToLongBits(f.getValue())); } private void write_object(PyObject v, int depth) { @@ -339,9 +337,7 @@ } private double read_binary_float() { - String buffer = read_string(8); - PyFloat num = (PyFloat) ((struct.unpack("d", buffer)).__getitem__(0)); - return num.asDouble(); + return Double.longBitsToDouble(read_long64()); } private PyObject read_object_notnull(int depth) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <otm...@us...> - 2009-02-05 18:08:12
|
Revision: 6014 http://jython.svn.sourceforge.net/jython/?rev=6014&view=rev Author: otmarhumbel Date: 2009-02-05 18:08:10 +0000 (Thu, 05 Feb 2009) Log Message: ----------- added dummy_thread.py and dummy_threading.py Modified Paths: -------------- trunk/jython/CPythonLib.includes Modified: trunk/jython/CPythonLib.includes =================================================================== --- trunk/jython/CPythonLib.includes 2009-02-04 15:27:59 UTC (rev 6013) +++ trunk/jython/CPythonLib.includes 2009-02-05 18:08:10 UTC (rev 6014) @@ -52,6 +52,8 @@ DocXMLRPCServer.py dospath.py dumbdbm.py +dummy_thread.py +dummy_threading.py exceptions.py fileinput.py fnmatch.py This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <am...@us...> - 2009-02-04 15:28:03
|
Revision: 6013 http://jython.svn.sourceforge.net/jython/?rev=6013&view=rev Author: amak Date: 2009-02-04 15:27:59 +0000 (Wed, 04 Feb 2009) Log Message: ----------- Fresh start with asycnore; step 3: make necessary changes to cpython 2.5 module to work on jython 2.5. The code in this bug report now works identically to cpython. http://bugs.jython.org/issue1237 Modified Paths: -------------- trunk/jython/Lib/asyncore.py Modified: trunk/jython/Lib/asyncore.py =================================================================== --- trunk/jython/Lib/asyncore.py 2009-02-04 15:22:21 UTC (rev 6012) +++ trunk/jython/Lib/asyncore.py 2009-02-04 15:27:59 UTC (rev 6013) @@ -177,7 +177,7 @@ poll3 = poll2 # Alias for backward compatibility -def loop(timeout=30.0, use_poll=False, map=None, count=None): +def loop(timeout=30.0, use_poll=True, map=None, count=None): if map is None: map = socket_map @@ -256,13 +256,13 @@ self.family_and_type = family, type self.socket = socket.socket(family, type) self.socket.setblocking(0) - self._fileno = self.socket.fileno() + self._fileno = self.socket self.add_channel() def set_socket(self, sock, map=None): self.socket = sock ## self.__dict__['socket'] = sock - self._fileno = sock.fileno() + self._fileno = sock self.add_channel(map) def set_reuse_addr(self): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <am...@us...> - 2009-02-04 15:22:22
|
Revision: 6012 http://jython.svn.sourceforge.net/jython/?rev=6012&view=rev Author: amak Date: 2009-02-04 15:22:21 +0000 (Wed, 04 Feb 2009) Log Message: ----------- Fresh start with asyncore; step 2: copy over asyncore from cpython 2.5.2. Added Paths: ----------- trunk/jython/Lib/asyncore.py Added: trunk/jython/Lib/asyncore.py =================================================================== --- trunk/jython/Lib/asyncore.py (rev 0) +++ trunk/jython/Lib/asyncore.py 2009-02-04 15:22:21 UTC (rev 6012) @@ -0,0 +1,551 @@ +# -*- Mode: Python -*- +# Id: asyncore.py,v 2.51 2000/09/07 22:29:26 rushing Exp +# Author: Sam Rushing <ru...@ni...> + +# ====================================================================== +# Copyright 1996 by Sam Rushing +# +# All Rights Reserved +# +# Permission to use, copy, modify, and distribute this software and +# its documentation for any purpose and without fee is hereby +# granted, provided that the above copyright notice appear in all +# copies and that both that copyright notice and this permission +# notice appear in supporting documentation, and that the name of Sam +# Rushing not be used in advertising or publicity pertaining to +# distribution of the software without specific, written prior +# permission. +# +# SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN +# NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR +# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# ====================================================================== + +"""Basic infrastructure for asynchronous socket service clients and servers. + +There are only two ways to have a program on a single processor do "more +than one thing at a time". Multi-threaded programming is the simplest and +most popular way to do it, but there is another very different technique, +that lets you have nearly all the advantages of multi-threading, without +actually using multiple threads. it's really only practical if your program +is largely I/O bound. If your program is CPU bound, then pre-emptive +scheduled threads are probably what you really need. Network servers are +rarely CPU-bound, however. + +If your operating system supports the select() system call in its I/O +library (and nearly all do), then you can use it to juggle multiple +communication channels at once; doing other work while your I/O is taking +place in the "background." Although this strategy can seem strange and +complex, especially at first, it is in many ways easier to understand and +control than multi-threaded programming. The module documented here solves +many of the difficult problems for you, making the task of building +sophisticated high-performance network servers and clients a snap. +""" + +import select +import socket +import sys +import time + +import os +from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \ + ENOTCONN, ESHUTDOWN, EINTR, EISCONN, errorcode + +try: + socket_map +except NameError: + socket_map = {} + +class ExitNow(Exception): + pass + +def read(obj): + try: + obj.handle_read_event() + except ExitNow: + raise + except: + obj.handle_error() + +def write(obj): + try: + obj.handle_write_event() + except ExitNow: + raise + except: + obj.handle_error() + +def _exception (obj): + try: + obj.handle_expt_event() + except ExitNow: + raise + except: + obj.handle_error() + +def readwrite(obj, flags): + try: + if flags & (select.POLLIN | select.POLLPRI): + obj.handle_read_event() + if flags & select.POLLOUT: + obj.handle_write_event() + if flags & (select.POLLERR | select.POLLHUP | select.POLLNVAL): + obj.handle_expt_event() + except ExitNow: + raise + except: + obj.handle_error() + +def poll(timeout=0.0, map=None): + if map is None: + map = socket_map + if map: + r = []; w = []; e = [] + for fd, obj in map.items(): + is_r = obj.readable() + is_w = obj.writable() + if is_r: + r.append(fd) + if is_w: + w.append(fd) + if is_r or is_w: + e.append(fd) + if [] == r == w == e: + time.sleep(timeout) + else: + try: + r, w, e = select.select(r, w, e, timeout) + except select.error, err: + if err[0] != EINTR: + raise + else: + return + + for fd in r: + obj = map.get(fd) + if obj is None: + continue + read(obj) + + for fd in w: + obj = map.get(fd) + if obj is None: + continue + write(obj) + + for fd in e: + obj = map.get(fd) + if obj is None: + continue + _exception(obj) + +def poll2(timeout=0.0, map=None): + # Use the poll() support added to the select module in Python 2.0 + if map is None: + map = socket_map + if timeout is not None: + # timeout is in milliseconds + timeout = int(timeout*1000) + pollster = select.poll() + if map: + for fd, obj in map.items(): + flags = 0 + if obj.readable(): + flags |= select.POLLIN | select.POLLPRI + if obj.writable(): + flags |= select.POLLOUT + if flags: + # Only check for exceptions if object was either readable + # or writable. + flags |= select.POLLERR | select.POLLHUP | select.POLLNVAL + pollster.register(fd, flags) + try: + r = pollster.poll(timeout) + except select.error, err: + if err[0] != EINTR: + raise + r = [] + for fd, flags in r: + obj = map.get(fd) + if obj is None: + continue + readwrite(obj, flags) + +poll3 = poll2 # Alias for backward compatibility + +def loop(timeout=30.0, use_poll=False, map=None, count=None): + if map is None: + map = socket_map + + if use_poll and hasattr(select, 'poll'): + poll_fun = poll2 + else: + poll_fun = poll + + if count is None: + while map: + poll_fun(timeout, map) + + else: + while map and count > 0: + poll_fun(timeout, map) + count = count - 1 + +class dispatcher: + + debug = False + connected = False + accepting = False + closing = False + addr = None + + def __init__(self, sock=None, map=None): + if map is None: + self._map = socket_map + else: + self._map = map + + if sock: + self.set_socket(sock, map) + # I think it should inherit this anyway + self.socket.setblocking(0) + self.connected = True + # XXX Does the constructor require that the socket passed + # be connected? + try: + self.addr = sock.getpeername() + except socket.error: + # The addr isn't crucial + pass + else: + self.socket = None + + def __repr__(self): + status = [self.__class__.__module__+"."+self.__class__.__name__] + if self.accepting and self.addr: + status.append('listening') + elif self.connected: + status.append('connected') + if self.addr is not None: + try: + status.append('%s:%d' % self.addr) + except TypeError: + status.append(repr(self.addr)) + return '<%s at %#x>' % (' '.join(status), id(self)) + + def add_channel(self, map=None): + #self.log_info('adding channel %s' % self) + if map is None: + map = self._map + map[self._fileno] = self + + def del_channel(self, map=None): + fd = self._fileno + if map is None: + map = self._map + if map.has_key(fd): + #self.log_info('closing channel %d:%s' % (fd, self)) + del map[fd] + self._fileno = None + + def create_socket(self, family, type): + self.family_and_type = family, type + self.socket = socket.socket(family, type) + self.socket.setblocking(0) + self._fileno = self.socket.fileno() + self.add_channel() + + def set_socket(self, sock, map=None): + self.socket = sock +## self.__dict__['socket'] = sock + self._fileno = sock.fileno() + self.add_channel(map) + + def set_reuse_addr(self): + # try to re-use a server port if possible + try: + self.socket.setsockopt( + socket.SOL_SOCKET, socket.SO_REUSEADDR, + self.socket.getsockopt(socket.SOL_SOCKET, + socket.SO_REUSEADDR) | 1 + ) + except socket.error: + pass + + # ================================================== + # predicates for select() + # these are used as filters for the lists of sockets + # to pass to select(). + # ================================================== + + def readable(self): + return True + + def writable(self): + return True + + # ================================================== + # socket object methods. + # ================================================== + + def listen(self, num): + self.accepting = True + if os.name == 'nt' and num > 5: + num = 1 + return self.socket.listen(num) + + def bind(self, addr): + self.addr = addr + return self.socket.bind(addr) + + def connect(self, address): + self.connected = False + err = self.socket.connect_ex(address) + # XXX Should interpret Winsock return values + if err in (EINPROGRESS, EALREADY, EWOULDBLOCK): + return + if err in (0, EISCONN): + self.addr = address + self.connected = True + self.handle_connect() + else: + raise socket.error, (err, errorcode[err]) + + def accept(self): + # XXX can return either an address pair or None + try: + conn, addr = self.socket.accept() + return conn, addr + except socket.error, why: + if why[0] == EWOULDBLOCK: + pass + else: + raise + + def send(self, data): + try: + result = self.socket.send(data) + return result + except socket.error, why: + if why[0] == EWOULDBLOCK: + return 0 + else: + raise + return 0 + + def recv(self, buffer_size): + try: + data = self.socket.recv(buffer_size) + if not data: + # a closed connection is indicated by signaling + # a read condition, and having recv() return 0. + self.handle_close() + return '' + else: + return data + except socket.error, why: + # winsock sometimes throws ENOTCONN + if why[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN]: + self.handle_close() + return '' + else: + raise + + def close(self): + self.del_channel() + self.socket.close() + + # cheap inheritance, used to pass all other attribute + # references to the underlying socket object. + def __getattr__(self, attr): + return getattr(self.socket, attr) + + # log and log_info may be overridden to provide more sophisticated + # logging and warning methods. In general, log is for 'hit' logging + # and 'log_info' is for informational, warning and error logging. + + def log(self, message): + sys.stderr.write('log: %s\n' % str(message)) + + def log_info(self, message, type='info'): + if __debug__ or type != 'info': + print '%s: %s' % (type, message) + + def handle_read_event(self): + if self.accepting: + # for an accepting socket, getting a read implies + # that we are connected + if not self.connected: + self.connected = True + self.handle_accept() + elif not self.connected: + self.handle_connect() + self.connected = True + self.handle_read() + else: + self.handle_read() + + def handle_write_event(self): + # getting a write implies that we are connected + if not self.connected: + self.handle_connect() + self.connected = True + self.handle_write() + + def handle_expt_event(self): + self.handle_expt() + + def handle_error(self): + nil, t, v, tbinfo = compact_traceback() + + # sometimes a user repr method will crash. + try: + self_repr = repr(self) + except: + self_repr = '<__repr__(self) failed for object at %0x>' % id(self) + + self.log_info( + 'uncaptured python exception, closing channel %s (%s:%s %s)' % ( + self_repr, + t, + v, + tbinfo + ), + 'error' + ) + self.close() + + def handle_expt(self): + self.log_info('unhandled exception', 'warning') + + def handle_read(self): + self.log_info('unhandled read event', 'warning') + + def handle_write(self): + self.log_info('unhandled write event', 'warning') + + def handle_connect(self): + self.log_info('unhandled connect event', 'warning') + + def handle_accept(self): + self.log_info('unhandled accept event', 'warning') + + def handle_close(self): + self.log_info('unhandled close event', 'warning') + self.close() + +# --------------------------------------------------------------------------- +# adds simple buffered output capability, useful for simple clients. +# [for more sophisticated usage use asynchat.async_chat] +# --------------------------------------------------------------------------- + +class dispatcher_with_send(dispatcher): + + def __init__(self, sock=None, map=None): + dispatcher.__init__(self, sock, map) + self.out_buffer = '' + + def initiate_send(self): + num_sent = 0 + num_sent = dispatcher.send(self, self.out_buffer[:512]) + self.out_buffer = self.out_buffer[num_sent:] + + def handle_write(self): + self.initiate_send() + + def writable(self): + return (not self.connected) or len(self.out_buffer) + + def send(self, data): + if self.debug: + self.log_info('sending %s' % repr(data)) + self.out_buffer = self.out_buffer + data + self.initiate_send() + +# --------------------------------------------------------------------------- +# used for debugging. +# --------------------------------------------------------------------------- + +def compact_traceback(): + t, v, tb = sys.exc_info() + tbinfo = [] + assert tb # Must have a traceback + while tb: + tbinfo.append(( + tb.tb_frame.f_code.co_filename, + tb.tb_frame.f_code.co_name, + str(tb.tb_lineno) + )) + tb = tb.tb_next + + # just to be safe + del tb + + file, function, line = tbinfo[-1] + info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo]) + return (file, function, line), t, v, info + +def close_all(map=None): + if map is None: + map = socket_map + for x in map.values(): + x.socket.close() + map.clear() + +# Asynchronous File I/O: +# +# After a little research (reading man pages on various unixen, and +# digging through the linux kernel), I've determined that select() +# isn't meant for doing asynchronous file i/o. +# Heartening, though - reading linux/mm/filemap.c shows that linux +# supports asynchronous read-ahead. So _MOST_ of the time, the data +# will be sitting in memory for us already when we go to read it. +# +# What other OS's (besides NT) support async file i/o? [VMS?] +# +# Regardless, this is useful for pipes, and stdin/stdout... + +if os.name == 'posix': + import fcntl + + class file_wrapper: + # here we override just enough to make a file + # look like a socket for the purposes of asyncore. + + def __init__(self, fd): + self.fd = fd + + def recv(self, *args): + return os.read(self.fd, *args) + + def send(self, *args): + return os.write(self.fd, *args) + + read = recv + write = send + + def close(self): + os.close(self.fd) + + def fileno(self): + return self.fd + + class file_dispatcher(dispatcher): + + def __init__(self, fd, map=None): + dispatcher.__init__(self, None, map) + self.connected = True + self.set_file(fd) + # set it to non-blocking mode + flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0) + flags = flags | os.O_NONBLOCK + fcntl.fcntl(fd, fcntl.F_SETFL, flags) + + def set_file(self, fd): + self._fileno = fd + self.socket = file_wrapper(fd) + self.add_channel() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <am...@us...> - 2009-02-04 15:20:58
|
Revision: 6011 http://jython.svn.sourceforge.net/jython/?rev=6011&view=rev Author: amak Date: 2009-02-04 15:20:56 +0000 (Wed, 04 Feb 2009) Log Message: ----------- Our asyncore is completely broken; see bug 1237. Asnycore does not seem to work in Jython 2.5 (b0 or b1) http://bugs.jython.org/issue1237 Starting again from scratch; Step 1: remove existing module. Removed Paths: ------------- trunk/jython/Lib/asyncore.py Deleted: trunk/jython/Lib/asyncore.py =================================================================== --- trunk/jython/Lib/asyncore.py 2009-02-04 00:51:47 UTC (rev 6010) +++ trunk/jython/Lib/asyncore.py 2009-02-04 15:20:56 UTC (rev 6011) @@ -1,561 +0,0 @@ -# -*- Mode: Python -*- -# Id: asyncore.py,v 2.51 2000/09/07 22:29:26 rushing Exp -# Author: Sam Rushing <ru...@ni...> - -# ====================================================================== -# Copyright 1996 by Sam Rushing -# -# All Rights Reserved -# -# Permission to use, copy, modify, and distribute this software and -# its documentation for any purpose and without fee is hereby -# granted, provided that the above copyright notice appear in all -# copies and that both that copyright notice and this permission -# notice appear in supporting documentation, and that the name of Sam -# Rushing not be used in advertising or publicity pertaining to -# distribution of the software without specific, written prior -# permission. -# -# SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN -# NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR -# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS -# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, -# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN -# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -# ====================================================================== - -"""Basic infrastructure for asynchronous socket service clients and servers. - -There are only two ways to have a program on a single processor do "more -than one thing at a time". Multi-threaded programming is the simplest and -most popular way to do it, but there is another very different technique, -that lets you have nearly all the advantages of multi-threading, without -actually using multiple threads. it's really only practical if your program -is largely I/O bound. If your program is CPU bound, then pre-emptive -scheduled threads are probably what you really need. Network servers are -rarely CPU-bound, however. - -If your operating system supports the select() system call in its I/O -library (and nearly all do), then you can use it to juggle multiple -communication channels at once; doing other work while your I/O is taking -place in the "background." Although this strategy can seem strange and -complex, especially at first, it is in many ways easier to understand and -control than multi-threaded programming. The module documented here solves -many of the difficult problems for you, making the task of building -sophisticated high-performance network servers and clients a snap. -""" - -import exceptions -import select -import socket -import sys -import time - -import os -from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \ - ENOTCONN, ESHUTDOWN, EINTR, EISCONN - -try: - socket_map -except NameError: - socket_map = {} - -class ExitNow(exceptions.Exception): - pass - -def read(obj): - try: - obj.handle_read_event() - except ExitNow: - raise - except: - obj.handle_error() - -def write(obj): - try: - obj.handle_write_event() - except ExitNow: - raise - except: - obj.handle_error() - -def readwrite(obj, flags): - try: - if flags & select.POLLIN: - obj.handle_read_event() - if flags & select.POLLOUT: - obj.handle_write_event() - except ExitNow: - raise - except: - obj.handle_error() - -def poll(timeout=0.0, map=None): - if map is None: - map = socket_map - if map: - r = []; w = []; e = [] - for fd, obj in map.items(): - if obj.readable(): - r.append(fd) - if obj.writable(): - w.append(fd) - if [] == r == w == e: - time.sleep(timeout) - else: - try: - r, w, e = select.select(r, w, e, timeout) - except select.error, err: - if err[0] != EINTR: - raise - else: - return - - for fd in r: - obj = map.get(fd) - if obj is None: - continue - read(obj) - - for fd in w: - obj = map.get(fd) - if obj is None: - continue - write(obj) - -def poll2(timeout=0.0, map=None): - import poll - if map is None: - map = socket_map - if timeout is not None: - # timeout is in milliseconds - timeout = int(timeout*1000) - if map: - l = [] - for fd, obj in map.items(): - flags = 0 - if obj.readable(): - flags = poll.POLLIN - if obj.writable(): - flags = flags | poll.POLLOUT - if flags: - l.append((fd, flags)) - r = poll.poll(l, timeout) - for fd, flags in r: - obj = map.get(fd) - if obj is None: - continue - readwrite(obj, flags) - -def poll3(timeout=0.0, map=None): - # Use the poll() support added to the select module in Python 2.0 - if map is None: - map = socket_map - if timeout is not None: - # timeout is in milliseconds - timeout = int(timeout*1000) - pollster = select.poll() - if map: - for fd, obj in map.items(): - flags = 0 - if obj.readable(): - flags = select.POLLIN - if obj.writable(): - flags = flags | select.POLLOUT - if flags: - pollster.register(fd, flags) - try: - r = pollster.poll(timeout) - except select.error, err: - if err[0] != EINTR: - raise - r = [] - for fd, flags in r: - obj = map.get(fd) - if obj is None: - continue - readwrite(obj, flags) - -def loop(timeout=30.0, use_poll=0, map=None): - if map is None: - map = socket_map - - if use_poll: - if hasattr(select, 'poll'): - poll_fun = poll3 - else: - poll_fun = poll2 - else: - poll_fun = poll - - while map: - poll_fun(timeout, map) - -class dispatcher: - - debug = 0 - connected = 0 - accepting = 0 - closing = 0 - addr = None - - def __init__(self, sock=None, map=None): - if sock: - self.set_socket(sock, map) - # I think it should inherit this anyway - self.socket.setblocking(0) - self.connected = 1 - # XXX Does the constructor require that the socket passed - # be connected? - try: - self.addr = sock.getpeername() - except socket.error: - # The addr isn't crucial - pass - else: - self.socket = None - - def __repr__(self): - status = [self.__class__.__module__+"."+self.__class__.__name__] - if self.accepting and self.addr: - status.append('listening') - elif self.connected: - status.append('connected') - if self.addr is not None: - try: - status.append('%s:%d' % self.addr) - except TypeError: - status.append(repr(self.addr)) - # On some systems (RH10) id() can be a negative number. - # work around this. - MAX = 2L*sys.maxint+1 - return '<%s at %#x>' % (' '.join(status), id(self)&MAX) - - def add_channel(self, map=None): - #self.log_info('adding channel %s' % self) - if map is None: - if hasattr(self, '_map'): - map = self._map - del self._map - else: - map = socket_map - if not hasattr(self, '_fileno'): - self._fileno = self.socket.fileno() - map[self._fileno] = self - - def del_channel(self, map=None): - fd = self._fileno - if map is None: - map = socket_map - if map.has_key(fd): - #self.log_info('closing channel %d:%s' % (fd, self)) - del map[fd] - - def create_socket(self, family, type): - self.family_and_type = family, type - self.socket = socket.socket(family, type) - self.socket.setblocking(0) - - def set_socket(self, sock, map=None): - self.socket = sock -## self.__dict__['socket'] = sock - if sock.fileno(): - self.add_channel(map) - else: - self._map = map - - def set_reuse_addr(self): - # try to re-use a server port if possible - try: - self.socket.setsockopt( - socket.SOL_SOCKET, socket.SO_REUSEADDR, - self.socket.getsockopt(socket.SOL_SOCKET, - socket.SO_REUSEADDR) | 1 - ) - except socket.error: - pass - - # ================================================== - # predicates for select() - # these are used as filters for the lists of sockets - # to pass to select(). - # ================================================== - - def readable(self): - return True - - if os.name == 'mac': - # The macintosh will select a listening socket for - # write if you let it. What might this mean? - def writable(self): - return not self.accepting - else: - def writable(self): - return True - - # ================================================== - # socket object methods. - # ================================================== - - def listen(self, num): - self.accepting = 1 - if os.name == 'nt' and num > 5: - num = 1 - ret = self.socket.listen(num) - self.add_channel() - return ret - - def bind(self, addr): - self.addr = addr - return self.socket.bind(addr) - - def connect(self, address): - self.connected = 0 - err = self.socket.connect_ex(address) - # XXX Should interpret Winsock return values - if err in (EINPROGRESS, EALREADY, EWOULDBLOCK): - return - if err in (0, EISCONN): - self.add_channel() - self.addr = address - self.connected = 1 - self.handle_connect() - else: - raise socket.error, err - - def accept(self): - # XXX can return either an address pair or None - try: - conn, addr = self.socket.accept() - self.add_channel() - return conn, addr - except socket.error, why: - if why[0] == EWOULDBLOCK: - pass - else: - raise socket.error, why - - def send(self, data): - try: - result = self.socket.send(data) - return result - except socket.error, why: - if why[0] == EWOULDBLOCK: - return 0 - else: - raise socket.error, why - return 0 - - def recv(self, buffer_size): - try: - data = self.socket.recv(buffer_size) - if not data: - # a closed connection is indicated by signaling - # a read condition, and having recv() return 0. - self.handle_close() - return '' - else: - return data - except socket.error, why: - # winsock sometimes throws ENOTCONN - if why[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN]: - self.handle_close() - return '' - else: - raise socket.error, why - - def close(self): - self.del_channel() - self.socket.close() - - # cheap inheritance, used to pass all other attribute - # references to the underlying socket object. - def __getattr__(self, attr): - return getattr(self.socket, attr) - - # log and log_info may be overridden to provide more sophisticated - # logging and warning methods. In general, log is for 'hit' logging - # and 'log_info' is for informational, warning and error logging. - - def log(self, message): - sys.stderr.write('log: %s\n' % str(message)) - - def log_info(self, message, type='info'): - if __debug__ or type != 'info': - print '%s: %s' % (type, message) - - def handle_read_event(self): - if self.accepting: - # for an accepting socket, getting a read implies - # that we are connected - if not self.connected: - self.connected = 1 - self.handle_accept() - elif not self.connected: - self.handle_connect() - self.connected = 1 - self.handle_read() - else: - self.handle_read() - - def handle_write_event(self): - # getting a write implies that we are connected - if not self.connected: - self.handle_connect() - self.connected = 1 - self.handle_write() - - def handle_expt_event(self): - self.handle_expt() - - def handle_error(self): - nil, t, v, tbinfo = compact_traceback() - - # sometimes a user repr method will crash. - try: - self_repr = repr(self) - except: - self_repr = '<__repr__(self) failed for object at %0x>' % id(self) - - self.log_info( - 'uncaptured python exception, closing channel %s (%s:%s %s)' % ( - self_repr, - t, - v, - tbinfo - ), - 'error' - ) - self.close() - - def handle_expt(self): - self.log_info('unhandled exception', 'warning') - - def handle_read(self): - self.log_info('unhandled read event', 'warning') - - def handle_write(self): - self.log_info('unhandled write event', 'warning') - - def handle_connect(self): - self.log_info('unhandled connect event', 'warning') - - def handle_accept(self): - self.log_info('unhandled accept event', 'warning') - - def handle_close(self): - self.log_info('unhandled close event', 'warning') - self.close() - -# --------------------------------------------------------------------------- -# adds simple buffered output capability, useful for simple clients. -# [for more sophisticated usage use asynchat.async_chat] -# --------------------------------------------------------------------------- - -class dispatcher_with_send(dispatcher): - - def __init__(self, sock=None): - dispatcher.__init__(self, sock) - self.out_buffer = '' - - def initiate_send(self): - num_sent = 0 - num_sent = dispatcher.send(self, self.out_buffer[:512]) - self.out_buffer = self.out_buffer[num_sent:] - - def handle_write(self): - self.initiate_send() - - def writable(self): - return (not self.connected) or len(self.out_buffer) - - def send(self, data): - if self.debug: - self.log_info('sending %s' % repr(data)) - self.out_buffer = self.out_buffer + data - self.initiate_send() - -# --------------------------------------------------------------------------- -# used for debugging. -# --------------------------------------------------------------------------- - -def compact_traceback(): - t, v, tb = sys.exc_info() - tbinfo = [] - assert tb # Must have a traceback - while tb: - tbinfo.append(( - tb.tb_frame.f_code.co_filename, - tb.tb_frame.f_code.co_name, - str(tb.tb_lineno) - )) - tb = tb.tb_next - - # just to be safe - del tb - - file, function, line = tbinfo[-1] - info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo]) - return (file, function, line), t, v, info - -def close_all(map=None): - if map is None: - map = socket_map - for x in map.values(): - x.socket.close() - map.clear() - -# Asynchronous File I/O: -# -# After a little research (reading man pages on various unixen, and -# digging through the linux kernel), I've determined that select() -# isn't meant for doing asynchronous file i/o. -# Heartening, though - reading linux/mm/filemap.c shows that linux -# supports asynchronous read-ahead. So _MOST_ of the time, the data -# will be sitting in memory for us already when we go to read it. -# -# What other OS's (besides NT) support async file i/o? [VMS?] -# -# Regardless, this is useful for pipes, and stdin/stdout... - -if os.name == 'posix': - import fcntl - - class file_wrapper: - # here we override just enough to make a file - # look like a socket for the purposes of asyncore. - - def __init__(self, fd): - self.fd = fd - - def recv(self, *args): - return os.read(self.fd, *args) - - def send(self, *args): - return os.write(self.fd, *args) - - read = recv - write = send - - def close(self): - return os.close(self.fd) - - def fileno(self): - return self.fd - - class file_dispatcher(dispatcher): - - def __init__(self, fd): - dispatcher.__init__(self) - self.connected = 1 - # set it to non-blocking mode - flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0) - flags = flags | os.O_NONBLOCK - fcntl.fcntl(fd, fcntl.F_SETFL, flags) - self.set_file(fd) - - def set_file(self, fd): - self._fileno = fd - self.socket = file_wrapper(fd) - self.add_channel() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <le...@us...> - 2009-02-04 00:51:50
|
Revision: 6010 http://jython.svn.sourceforge.net/jython/?rev=6010&view=rev Author: leosoto Date: 2009-02-04 00:51:47 +0000 (Wed, 04 Feb 2009) Log Message: ----------- PyObject#reduce_2: Use invoke("iteritems") instead of this.iteritems() to avoid problems with dict subclasses which override iteritems on python code. Fixes #1257 Modified Paths: -------------- trunk/jython/src/org/python/core/PyObject.java Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2009-02-03 21:25:23 UTC (rev 6009) +++ trunk/jython/src/org/python/core/PyObject.java 2009-02-04 00:51:47 UTC (rev 6010) @@ -3904,7 +3904,7 @@ if (!(this instanceof PyDictionary)) { dictitems = Py.None; } else { - dictitems = ((PyDictionary)this).iteritems(); + dictitems = invoke("iteritems"); } PyObject copyreg = __builtin__.__import__("copy_reg", null, null, Py.EmptyTuple); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-02-03 21:25:29
|
Revision: 6009 http://jython.svn.sourceforge.net/jython/?rev=6009&view=rev Author: pjenvey Date: 2009-02-03 21:25:23 +0000 (Tue, 03 Feb 2009) Log Message: ----------- include PATH for platforms lacking bash in /bin Modified Paths: -------------- trunk/jython/Lib/test/test_jython_initializer.py Modified: trunk/jython/Lib/test/test_jython_initializer.py =================================================================== --- trunk/jython/Lib/test/test_jython_initializer.py 2009-02-03 20:19:57 UTC (rev 6008) +++ trunk/jython/Lib/test/test_jython_initializer.py 2009-02-03 21:25:23 UTC (rev 6009) @@ -1,14 +1,16 @@ -import unittest +import os import subprocess import sys +import unittest from test import test_support class TestUsingInitializer(unittest.TestCase): + def test_syspath_initializer(self): - fn = test_support.findfile("check_for_initializer_in_syspath.py") - ret = subprocess.Popen([sys.executable, fn], - env={"CLASSPATH":"tests/data/initializer"}).wait() - self.assertEquals(0, ret) + fn = test_support.findfile('check_for_initializer_in_syspath.py') + env = dict(CLASSPATH='tests/data/initializer', + PATH=os.environ.get('PATH', '')) + self.assertEquals(0, subprocess.call([sys.executable, fn], env=env)) def test_main(): test_support.run_unittest(TestUsingInitializer) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-02-03 20:20:02
|
Revision: 6008 http://jython.svn.sourceforge.net/jython/?rev=6008&view=rev Author: pjenvey Date: 2009-02-03 20:19:57 +0000 (Tue, 03 Feb 2009) Log Message: ----------- fix test_version per r6003 Modified Paths: -------------- trunk/jython/Lib/test/test_cmd_line.py Modified: trunk/jython/Lib/test/test_cmd_line.py =================================================================== --- trunk/jython/Lib/test/test_cmd_line.py 2009-02-03 12:01:46 UTC (rev 6007) +++ trunk/jython/Lib/test/test_cmd_line.py 2009-02-03 20:19:57 UTC (rev 6008) @@ -45,11 +45,8 @@ self.assertTrue('usage' in self.start_python('-h')) def test_version(self): - from org.python.util import InteractiveConsole - expected = InteractiveConsole.getDefaultBanner() - reported = self.start_python('-V') - self.assertTrue(reported.startswith(expected), - "-V should start with '%s' but it printed '%s'" % (expected, reported)) + version = 'Jython %d.%d' % sys.version_info[:2] + self.assertTrue(self.start_python('-V').startswith(version)) def test_main(): test.test_support.run_unittest(CmdLineTest) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <am...@us...> - 2009-02-03 12:02:21
|
Revision: 6007 http://jython.svn.sourceforge.net/jython/?rev=6007&view=rev Author: amak Date: 2009-02-03 12:01:46 +0000 (Tue, 03 Feb 2009) Log Message: ----------- Committing the latest version of modjy. See the release notes for details. Added Paths: ----------- trunk/jython/extlibs/modjy_0_25_2.zip Removed Paths: ------------- trunk/jython/extlibs/modjy_0_25_1.zip Deleted: trunk/jython/extlibs/modjy_0_25_1.zip =================================================================== (Binary files differ) Added: trunk/jython/extlibs/modjy_0_25_2.zip =================================================================== (Binary files differ) Property changes on: trunk/jython/extlibs/modjy_0_25_2.zip ___________________________________________________________________ Added: svn:mime-type + application/octet-stream This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <am...@us...> - 2009-02-02 15:50:35
|
Revision: 6006 http://jython.svn.sourceforge.net/jython/?rev=6006&view=rev Author: amak Date: 2009-02-02 15:50:25 +0000 (Mon, 02 Feb 2009) Log Message: ----------- Fix for bug 1258. select() semantics differ from CPython, causing pydoc HTTPd to fail http://bugs.jython.org/issue1258 Thanks to Ryan Blair for reporting the bug and providing a patch. Modified Paths: -------------- trunk/jython/Lib/pydoc.py Modified: trunk/jython/Lib/pydoc.py =================================================================== --- trunk/jython/Lib/pydoc.py 2009-02-01 08:24:06 UTC (rev 6005) +++ trunk/jython/Lib/pydoc.py 2009-02-02 15:50:25 UTC (rev 6006) @@ -1959,13 +1959,17 @@ self.callback = callback self.base.__init__(self, self.address, self.handler) - def serve_until_quit(self): - import select - self.quit = False - while not self.quit: - rd, wr, ex = select.select([self.socket.fileno()], [], [], 1) - if rd: self.handle_request() - + def serve_until_quit(self): + import sys + if sys.platform.startswith('java'): + from select import cpython_compatible_select as select + else: + from select import select + self.quit = False + while not self.quit: + rd, wr, ex = select([self.socket], [], [], 1) + if rd: self.handle_request() + def server_activate(self): self.base.server_activate(self) if self.callback: self.callback(self) @@ -1976,7 +1980,7 @@ try: try: DocServer(port, callback).serve_until_quit() - except (KeyboardInterrupt, select.error): + except (KeyboardInterrupt, select.error): pass finally: if completer: completer() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-01 08:24:15
|
Revision: 6005 http://jython.svn.sourceforge.net/jython/?rev=6005&view=rev Author: zyasoft Date: 2009-02-01 08:24:06 +0000 (Sun, 01 Feb 2009) Log Message: ----------- Added _marshal, a Java implementation of the Python module, and made current. test_marshal now passes (with minor modifications around GC and floating point accuracy on to/from string representation), except for support of code objects. That's next. Pulled IOFile (now PyIOFile) from cPickle to share its duck typing with _marshal A historical note: from what I can discern in the log, the original marshal.py was written by Guido van Rossum specifically for "JPython" in 1997, one of the few bits he apparently did. Now it survives only as a shell, to import in _marshal. Modified Paths: -------------- branches/pbcvm/Lib/marshal.py branches/pbcvm/Lib/new.py branches/pbcvm/Lib/test/test_marshal.py branches/pbcvm/src/org/python/modules/Setup.java branches/pbcvm/src/org/python/modules/cPickle.java branches/pbcvm/src/org/python/modules/struct.java Added Paths: ----------- branches/pbcvm/src/org/python/modules/PyIOFile.java branches/pbcvm/src/org/python/modules/PyIOFileFactory.java branches/pbcvm/src/org/python/modules/_marshal.java Modified: branches/pbcvm/Lib/marshal.py =================================================================== --- branches/pbcvm/Lib/marshal.py 2009-02-01 08:12:13 UTC (rev 6004) +++ branches/pbcvm/Lib/marshal.py 2009-02-01 08:24:06 UTC (rev 6005) @@ -5,332 +5,24 @@ """ -import StringIO -import string -from types import * -try: - import new -except ImportError: - new = None +import cStringIO +from _marshal import Marshaller, Unmarshaller -TYPE_NULL = '0' -TYPE_NONE = 'N' -TYPE_FALSE = 'F' -TYPE_TRUE = 'T' -TYPE_ELLIPSIS = '.' -TYPE_INT = 'i' -TYPE_INT64 = 'I' -TYPE_FLOAT = 'f' -TYPE_COMPLEX = 'x' -TYPE_LONG = 'l' -TYPE_STRING = 's' -TYPE_TUPLE = '(' -TYPE_LIST = '[' -TYPE_DICT = '{' -TYPE_CODE = 'c' -TYPE_UNKNOWN = '?' +def dump(x, f, version=2): + Marshaller(f, version).dump(x) +# XXX - added just for debugging. remove! +def load(f, debug=False): + u = Unmarshaller(f) + if debug: + u._debug() + return u.load() -class Marshaller: - - dispatch = {} - - def __init__(self, f): - self.f = f - - def dump(self, x): - self.dispatch[type(x)](self, x) - - def w_long64(self, x): - self.w_long(x) - self.w_long(x>>32) - - def w_long(self, x): - write = self.f.write - write(chr((x) & 0xff)) - write(chr((x>> 8) & 0xff)) - write(chr((x>>16) & 0xff)) - write(chr((x>>24) & 0xff)) - - def w_short(self, x): - write = self.f.write - write(chr((x) & 0xff)) - write(chr((x>> 8) & 0xff)) - - def dump_none(self, x): - self.f.write(TYPE_NONE) - dispatch[NoneType] = dump_none - - def dump_bool(self, x): - if x: - self.f.write(TYPE_TRUE) - else: - self.f.write(TYPE_FALSE) - dispatch[BooleanType] = dump_bool - - def dump_ellipsis(self, x): - self.f.write(TYPE_ELLIPSIS) - try: - dispatch[EllipsisType] = dump_ellipsis - except NameError: - pass - - def dump_int(self, x): - y = x>>31 - if y and y != -1: - self.f.write(TYPE_INT64) - self.w_long64(x) - else: - self.f.write(TYPE_INT) - self.w_long(x) - dispatch[IntType] = dump_int - - def dump_long(self, x): - self.f.write(TYPE_LONG) - sign = 1 - if x < 0: - sign = -1 - x = -x - digits = [] - while x: - digits.append(x & 0x7FFF) - x = x>>15 - self.w_long(len(digits) * sign) - for d in digits: - self.w_short(d) - dispatch[LongType] = dump_long - - def dump_float(self, x): - write = self.f.write - write(TYPE_FLOAT) - s = `x` - write(chr(len(s))) - write(s) - dispatch[FloatType] = dump_float - - def dump_complex(self, x): - write = self.f.write - write(TYPE_COMPLEX) - s = `x.real` - write(chr(len(s))) - write(s) - s = `x.imag` - write(chr(len(s))) - write(s) - try: - dispatch[ComplexType] = dump_complex - except NameError: - pass - - def dump_string(self, x): - self.f.write(TYPE_STRING) - self.w_long(len(x)) - self.f.write(x) - dispatch[StringType] = dump_string - - def dump_tuple(self, x): - self.f.write(TYPE_TUPLE) - self.w_long(len(x)) - for item in x: - self.dump(item) - dispatch[TupleType] = dump_tuple - - def dump_list(self, x): - self.f.write(TYPE_LIST) - self.w_long(len(x)) - for item in x: - self.dump(item) - dispatch[ListType] = dump_list - - def dump_dict(self, x): - self.f.write(TYPE_DICT) - for key, value in x.items(): - self.dump(key) - self.dump(value) - self.f.write(TYPE_NULL) - dispatch[DictionaryType] = dump_dict - - def dump_code(self, x): - self.f.write(TYPE_CODE) - self.w_short(x.co_argcount) - self.w_short(x.co_nlocals) - self.w_short(x.co_stacksize) - self.w_short(x.co_flags) - self.dump(x.co_code) - self.dump(x.co_consts) - self.dump(x.co_names) - self.dump(x.co_varnames) - self.dump(x.co_filename) - self.dump(x.co_name) - self.w_short(x.co_firstlineno) - self.dump(x.co_lnotab) - try: - dispatch[CodeType] = dump_code - except NameError: - pass - - -class NULL: - pass - -class Unmarshaller: - - dispatch = {} - - def __init__(self, f): - self.f = f - - def load(self): - c = self.f.read(1) - if not c: - raise EOFError - return self.dispatch[c](self) - - def r_short(self): - read = self.f.read - lo = ord(read(1)) - hi = ord(read(1)) - x = lo | (hi<<8) - if x & 0x8000: - x = x - 0x10000 - return x - - def r_long(self): - read = self.f.read - a = ord(read(1)) - b = ord(read(1)) - c = ord(read(1)) - d = ord(read(1)) - # convert unsigned d to signed to avoid d << 24 possibly - # overflowing into a long value - if d > 127: - d -= 256 - x = a | (b<<8) | (c<<16) | (d<<24) - if x & 0x80000000 and x > 0: - x = string.atoi(x - 0x100000000L) - return x - - def r_long64(self): - a = self.r_long() - b = self.r_long() - return a | (b<<32) - - def load_null(self): - return NULL - dispatch[TYPE_NULL] = load_null - - def load_none(self): - return None - dispatch[TYPE_NONE] = load_none - - def load_False(self): - return False - dispatch[TYPE_FALSE] = load_False - - def load_True(self): - return True - dispatch[TYPE_TRUE] = load_True - - def load_ellipsis(self): - return EllipsisType - dispatch[TYPE_ELLIPSIS] = load_ellipsis - - def load_int(self): - return self.r_long() - dispatch[TYPE_INT] = load_int - - def load_int64(self): - return self.r_long64() - dispatch[TYPE_INT64] = load_int64 - - def load_long(self): - size = self.r_long() - sign = 1 - if size < 0: - sign = -1 - size = -size - x = 0L - for i in range(size): - d = self.r_short() - x = x | (d<<(i*15L)) - return x * sign - dispatch[TYPE_LONG] = load_long - - def load_float(self): - n = ord(self.f.read(1)) - s = self.f.read(n) - return string.atof(s) - dispatch[TYPE_FLOAT] = load_float - - def load_complex(self): - n = ord(self.f.read(1)) - s = self.f.read(n) - real = float(s) - n = ord(self.f.read(1)) - s = self.f.read(n) - imag = float(s) - return complex(real, imag) - dispatch[TYPE_COMPLEX] = load_complex - - def load_string(self): - n = self.r_long() - return self.f.read(n) - dispatch[TYPE_STRING] = load_string - - def load_tuple(self): - return tuple(self.load_list()) - dispatch[TYPE_TUPLE] = load_tuple - - def load_list(self): - n = self.r_long() - list = [] - for i in range(n): - list.append(self.load()) - return list - dispatch[TYPE_LIST] = load_list - - def load_dict(self): - d = {} - while 1: - key = self.load() - if key is NULL: - break - value = self.load() - d[key] = value - return d - dispatch[TYPE_DICT] = load_dict - - def load_code(self): - argcount = self.r_short() - nlocals = self.r_short() - stacksize = self.r_short() - flags = self.r_short() - code = self.load() - consts = self.load() - names = self.load() - varnames = self.load() - filename = self.load() - name = self.load() - firstlineno = self.r_short() - lnotab = self.load() - if not new: - raise RuntimeError, "can't unmarshal code objects; no 'new' module" - return new.code(argcount, nlocals, stacksize, flags, code, consts, - names, varnames, filename, name, firstlineno, lnotab) - dispatch[TYPE_CODE] = load_code - - -def dump(x, f): - Marshaller(f).dump(x) - -def load(f): - return Unmarshaller(f).load() - -def dumps(x): - f = StringIO.StringIO() - dump(x, f) +def dumps(x, version=2): + f = cStringIO.StringIO() + dump(x, f, version) return f.getvalue() def loads(s): - f = StringIO.StringIO(s) + f = cStringIO.StringIO(s) return load(f) Modified: branches/pbcvm/Lib/new.py =================================================================== --- branches/pbcvm/Lib/new.py 2009-02-01 08:12:13 UTC (rev 6004) +++ branches/pbcvm/Lib/new.py 2009-02-01 08:24:06 UTC (rev 6005) @@ -20,3 +20,5 @@ # from types import CodeType as code #except ImportError: # pass + +from org.python.core import PyBytecode as code Modified: branches/pbcvm/Lib/test/test_marshal.py =================================================================== --- branches/pbcvm/Lib/test/test_marshal.py 2009-02-01 08:12:13 UTC (rev 6004) +++ branches/pbcvm/Lib/test/test_marshal.py 2009-02-01 08:24:06 UTC (rev 6005) @@ -1,12 +1,25 @@ #!/usr/bin/env python # -*- coding: iso-8859-1 -*- +from __future__ import with_statement from test import test_support import marshal import sys import unittest import os +# the original had incorrect semantics for non-refcounting GCs: +# marshal.dump(expected, file(test_support.TESTFN, "wb")) +# got = marshal.load(file(test_support.TESTFN, "rb")) + +def roundtrip(item): + with open(test_support.TESTFN, "wb") as test_file: + marshal.dump(item, test_file) + with open(test_support.TESTFN, "rb") as test_file: + got = marshal.load(file(test_support.TESTFN, "rb")) #, debug=True) + return got + + class IntTestCase(unittest.TestCase): def test_ints(self): # Test the full range of Python ints. @@ -16,9 +29,7 @@ s = marshal.dumps(expected) got = marshal.loads(s) self.assertEqual(expected, got) - marshal.dump(expected, file(test_support.TESTFN, "wb")) - got = marshal.load(file(test_support.TESTFN, "rb")) - self.assertEqual(expected, got) + self.assertEqual(expected, roundtrip(expected)) n = n >> 1 os.unlink(test_support.TESTFN) @@ -51,8 +62,7 @@ new = marshal.loads(marshal.dumps(b)) self.assertEqual(b, new) self.assertEqual(type(b), type(new)) - marshal.dump(b, file(test_support.TESTFN, "wb")) - new = marshal.load(file(test_support.TESTFN, "rb")) + new = roundtrip(b) self.assertEqual(b, new) self.assertEqual(type(b), type(new)) @@ -67,8 +77,7 @@ s = marshal.dumps(f) got = marshal.loads(s) self.assertEqual(f, got) - marshal.dump(f, file(test_support.TESTFN, "wb")) - got = marshal.load(file(test_support.TESTFN, "rb")) + got = roundtrip(f) self.assertEqual(f, got) n /= 123.4567 @@ -92,15 +101,18 @@ s = marshal.dumps(f, 1) got = marshal.loads(s) - self.assertEqual(f, got) + if test_support.is_jython: + self.assertAlmostEqual(f, got) + else: + self.assertEqual(f, got) - marshal.dump(f, file(test_support.TESTFN, "wb")) - got = marshal.load(file(test_support.TESTFN, "rb")) + got = roundtrip(f) self.assertEqual(f, got) - marshal.dump(f, file(test_support.TESTFN, "wb"), 1) - got = marshal.load(file(test_support.TESTFN, "rb")) - self.assertEqual(f, got) + # XXX - not certain what this extra arg to dump is! + #marshal.dump(f, file(test_support.TESTFN, "wb"), 1) + #got = marshal.load(file(test_support.TESTFN, "rb")) + #self.assertEqual(f, got) n *= 123.4567 os.unlink(test_support.TESTFN) @@ -110,8 +122,7 @@ new = marshal.loads(marshal.dumps(s)) self.assertEqual(s, new) self.assertEqual(type(s), type(new)) - marshal.dump(s, file(test_support.TESTFN, "wb")) - new = marshal.load(file(test_support.TESTFN, "rb")) + new = roundtrip(s) self.assertEqual(s, new) self.assertEqual(type(s), type(new)) os.unlink(test_support.TESTFN) @@ -121,21 +132,20 @@ new = marshal.loads(marshal.dumps(s)) self.assertEqual(s, new) self.assertEqual(type(s), type(new)) - marshal.dump(s, file(test_support.TESTFN, "wb")) - new = marshal.load(file(test_support.TESTFN, "rb")) + new = roundtrip(s) self.assertEqual(s, new) self.assertEqual(type(s), type(new)) os.unlink(test_support.TESTFN) - def test_buffer(self): - for s in ["", "Andr\xE8 Previn", "abc", " "*10000]: - b = buffer(s) - new = marshal.loads(marshal.dumps(b)) - self.assertEqual(s, new) - marshal.dump(b, file(test_support.TESTFN, "wb")) - new = marshal.load(file(test_support.TESTFN, "rb")) - self.assertEqual(s, new) - os.unlink(test_support.TESTFN) + if not test_support.is_jython: + def test_buffer(self): + for s in ["", "Andr\xE8 Previn", "abc", " "*10000]: + b = buffer(s) + new = marshal.loads(marshal.dumps(b)) + self.assertEqual(s, new) + new = roundtrip(b) + self.assertEqual(s, new) + os.unlink(test_support.TESTFN) class ExceptionTestCase(unittest.TestCase): def test_exceptions(self): @@ -143,10 +153,11 @@ self.assertEqual(StopIteration, new) class CodeTestCase(unittest.TestCase): - def test_code(self): - co = ExceptionTestCase.test_exceptions.func_code - new = marshal.loads(marshal.dumps(co)) - self.assertEqual(co, new) + if not test_support.is_jython: # XXX - need to use the PBC compilation backend, which doesn't exist yet + def test_code(self): + co = ExceptionTestCase.test_exceptions.func_code + new = marshal.loads(marshal.dumps(co)) + self.assertEqual(co, new) class ContainerTestCase(unittest.TestCase): d = {'astring': 'fo...@ba...z.spam', @@ -161,8 +172,7 @@ def test_dict(self): new = marshal.loads(marshal.dumps(self.d)) self.assertEqual(self.d, new) - marshal.dump(self.d, file(test_support.TESTFN, "wb")) - new = marshal.load(file(test_support.TESTFN, "rb")) + new = roundtrip(self.d) self.assertEqual(self.d, new) os.unlink(test_support.TESTFN) @@ -170,8 +180,7 @@ lst = self.d.items() new = marshal.loads(marshal.dumps(lst)) self.assertEqual(lst, new) - marshal.dump(lst, file(test_support.TESTFN, "wb")) - new = marshal.load(file(test_support.TESTFN, "rb")) + new = roundtrip(lst) self.assertEqual(lst, new) os.unlink(test_support.TESTFN) @@ -179,8 +188,7 @@ t = tuple(self.d.keys()) new = marshal.loads(marshal.dumps(t)) self.assertEqual(t, new) - marshal.dump(t, file(test_support.TESTFN, "wb")) - new = marshal.load(file(test_support.TESTFN, "rb")) + new = roundtrip(t) self.assertEqual(t, new) os.unlink(test_support.TESTFN) @@ -191,8 +199,7 @@ self.assertEqual(t, new) self.assert_(isinstance(new, constructor)) self.assertNotEqual(id(t), id(new)) - marshal.dump(t, file(test_support.TESTFN, "wb")) - new = marshal.load(file(test_support.TESTFN, "rb")) + new = roundtrip(t) self.assertEqual(t, new) os.unlink(test_support.TESTFN) Added: branches/pbcvm/src/org/python/modules/PyIOFile.java =================================================================== --- branches/pbcvm/src/org/python/modules/PyIOFile.java (rev 0) +++ branches/pbcvm/src/org/python/modules/PyIOFile.java 2009-02-01 08:24:06 UTC (rev 6005) @@ -0,0 +1,24 @@ + + +package org.python.modules; + +/** +PyIOFiles encapsulates and optimise access to the different file +representation. Used by cPickle and marshall. + */ + +public interface PyIOFile { + + public abstract void write(String str); + // Usefull optimization since most data written are chars. + + public abstract void write(char str); + + public abstract void flush(); + + public abstract String read(int len); + // Usefull optimization since all readlines removes the + // trainling newline. + + public abstract String readlineNoNl(); +} \ No newline at end of file Added: branches/pbcvm/src/org/python/modules/PyIOFileFactory.java =================================================================== --- branches/pbcvm/src/org/python/modules/PyIOFileFactory.java (rev 0) +++ branches/pbcvm/src/org/python/modules/PyIOFileFactory.java 2009-02-01 08:24:06 UTC (rev 6005) @@ -0,0 +1,141 @@ +package org.python.modules; + +import org.python.core.Py; +import org.python.core.PyFile; +import org.python.core.PyInteger; +import org.python.core.PyObject; +import org.python.core.PyString; +import org.python.core.PyType; +import org.python.core.__builtin__; + +// XXX - add support for StringIO, not just cStringIO + +public class PyIOFileFactory { + + private PyIOFileFactory() { + } + + public static PyIOFile createIOFile(PyObject file) { + Object f = file.__tojava__(cStringIO.StringIO.class); + if (f != Py.NoConversion) { + return new cStringIOFile(file); + } else if (__builtin__.isinstance(file, FileType)) { + return new FileIOFile(file); + } else { + return new ObjectIOFile(file); + } + } + + private static PyType FileType = PyType.fromClass(PyFile.class); + + // Use a cStringIO as a file. + static class cStringIOFile implements PyIOFile { + + cStringIO.StringIO file; + + cStringIOFile(PyObject file) { + this.file = (cStringIO.StringIO) file.__tojava__(Object.class); + } + + public void write(String str) { + file.write(str); + } + + public void write(char ch) { + file.writeChar(ch); + } + + public void flush() { + } + + public String read(int len) { + return file.read(len).asString(); + } + + public String readlineNoNl() { + return file.readlineNoNl().asString(); + } + } + + + // Use a PyFile as a file. + static class FileIOFile implements PyIOFile { + + PyFile file; + + FileIOFile(PyObject file) { + this.file = (PyFile) file.__tojava__(PyFile.class); + if (this.file.getClosed()) { + throw Py.ValueError("I/O operation on closed file"); + } + } + + public void write(String str) { + file.write(str); + } + + public void write(char ch) { + file.write(cStringIO.getString(ch)); + } + + public void flush() { + } + + public String read(int len) { + return file.read(len).toString(); + } + + public String readlineNoNl() { + String line = file.readline().toString(); + return line.substring(0, line.length() - 1); + } + } + + + // Use any python object as a file. + static class ObjectIOFile implements PyIOFile { + + char[] charr = new char[1]; + StringBuilder buff = new StringBuilder(); + PyObject write; + PyObject read; + PyObject readline; + final int BUF_SIZE = 256; + + ObjectIOFile(PyObject file) { +// this.file = file; + write = file.__findattr__("write"); + read = file.__findattr__("read"); + readline = file.__findattr__("readline"); + } + + public void write(String str) { + buff.append(str); + if (buff.length() > BUF_SIZE) { + flush(); + } + } + + public void write(char ch) { + buff.append(ch); + if (buff.length() > BUF_SIZE) { + flush(); + } + } + + public void flush() { + write.__call__(new PyString(buff.toString())); + buff.setLength(0); + } + + public String read(int len) { + return read.__call__(new PyInteger(len)).toString(); + } + + public String readlineNoNl() { + String line = readline.__call__().toString(); + return line.substring(0, line.length() - 1); + } + } +} + Modified: branches/pbcvm/src/org/python/modules/Setup.java =================================================================== --- branches/pbcvm/src/org/python/modules/Setup.java 2009-02-01 08:12:13 UTC (rev 6004) +++ branches/pbcvm/src/org/python/modules/Setup.java 2009-02-01 08:24:06 UTC (rev 6005) @@ -55,6 +55,7 @@ "_functools:org.python.modules._functools._functools", "_csv:org.python.modules._csv._csv", "_systemrestart", - "_ast:org.python.antlr.ast.AstModule" + "_ast:org.python.antlr.ast.AstModule", + "_marshal" }; } Added: branches/pbcvm/src/org/python/modules/_marshal.java =================================================================== --- branches/pbcvm/src/org/python/modules/_marshal.java (rev 0) +++ branches/pbcvm/src/org/python/modules/_marshal.java 2009-02-01 08:24:06 UTC (rev 6005) @@ -0,0 +1,528 @@ +package org.python.modules; + +import java.math.BigInteger; +import org.python.core.BaseSet; +import org.python.core.ClassDictInit; +import org.python.core.PyObject; +import org.python.core.PyString; +import org.python.core.Py; +import org.python.core.PyBytecode; +import org.python.core.PyComplex; +import org.python.core.PyDictionary; +import org.python.core.PyFloat; +import org.python.core.PyFrozenSet; +import org.python.core.PyInteger; +import org.python.core.PyList; +import org.python.core.PyLong; +import org.python.core.PySet; +import org.python.core.PyTuple; +import org.python.core.PyUnicode; + +public class _marshal implements ClassDictInit { + + public static void classDictInit(PyObject dict) { + dict.__setitem__("__name__", Py.newString("_marshal")); + } + private final static char TYPE_NULL = '0'; + private final static char TYPE_NONE = 'N'; + private final static char TYPE_FALSE = 'F'; + private final static char TYPE_TRUE = 'T'; + private final static char TYPE_STOPITER = 'S'; + private final static char TYPE_ELLIPSIS = '.'; + private final static char TYPE_INT = 'i'; + private final static char TYPE_INT64 = 'I'; + private final static char TYPE_FLOAT = 'f'; + private final static char TYPE_BINARY_FLOAT = 'g'; + private final static char TYPE_COMPLEX = 'x'; + private final static char TYPE_BINARY_COMPLEX = 'y'; + private final static char TYPE_LONG = 'l'; + private final static char TYPE_STRING = 's'; + private final static char TYPE_INTERNED = 't'; + private final static char TYPE_STRINGREF = 'R'; + private final static char TYPE_TUPLE = '('; + private final static char TYPE_LIST = '['; + private final static char TYPE_DICT = '{'; + private final static char TYPE_CODE = 'c'; + private final static char TYPE_UNICODE = 'u'; + private final static char TYPE_UNKNOWN = '?'; + private final static char TYPE_SET = '<'; + private final static char TYPE_FROZENSET = '>'; + private final static int MAX_MARSHAL_STACK_DEPTH = 2000; + private final static int CURRENT_VERSION = 2; + + public static class Marshaller extends PyObject { + + private final PyIOFile file; + private final int version; + + public Marshaller(PyObject file) { + this(file, CURRENT_VERSION); + } + + public Marshaller(PyObject file, int version) { + this.file = PyIOFileFactory.createIOFile(file); + this.version = version; + } + private boolean debug = false; + + public void _debug() { + debug = true; + } + + public void dump(PyObject obj) { + write_object(obj, 0); + } + + private void write_byte(char c) { + if (debug) { + System.err.print("[" + (int) c + "]"); + } + file.write(c); + } + + private void write_string(String s) { + file.write(s); + } + + private void write_strings(String[] some_strings, int depth) { + PyObject items[] = new PyObject[some_strings.length]; + for (int i = 0; i < some_strings.length; i++) { + items[i] = Py.newString(some_strings[i]); + } + write_object(new PyTuple(items), depth + 1); + } + + private void write_short(short x) { + write_byte((char) (x & 0xff)); + write_byte((char) ((x >> 8) & 0xff)); + } + + private void write_int(int x) { + write_byte((char) (x & 0xff)); + write_byte((char) ((x >> 8) & 0xff)); + write_byte((char) ((x >> 16) & 0xff)); + write_byte((char) ((x >> 24) & 0xff)); + } + + private void write_long64(long x) { + write_int((int) (x & 0xff)); + write_int((int) ((x >> 32) & 0xff)); + } + + // writes output in 15 bit "digits" + private void write_long(BigInteger x) { + int sign = x.signum(); + if (sign < 0) { + x = x.negate(); + } + int num_bits = x.bitLength(); + int num_digits = num_bits / 15 + (num_bits % 15 == 0 ? 0 : 1); + write_int(sign < 0 ? -num_digits : num_digits); + BigInteger mask = BigInteger.valueOf(0x7FFF); + for (int i = 0; i < num_digits; i++) { + write_short(x.and(mask).shortValue()); + x = x.shiftRight(15); + } + } + + private void write_float(PyFloat f) { + write_string(f.__repr__().toString()); + } + + // XXX - cache this struct object + // XXX - also fix reversed struct stuff! + private void write_binary_float(PyFloat f) { + write_string(struct.pack(new PyObject[]{Py.newString("d"), f}).toString()); + } + + private void write_object(PyObject v, int depth) { + if (depth >= MAX_MARSHAL_STACK_DEPTH) { + throw Py.ValueError("Maximum marshal stack depth"); // XXX - fix this exception + } else if (v == null) { + write_byte(TYPE_NULL); + } else if (v == Py.None) { + write_byte(TYPE_NONE); + } else if (v == Py.StopIteration) { + write_byte(TYPE_STOPITER); + } else if (v == Py.Ellipsis) { + write_byte(TYPE_ELLIPSIS); + } else if (v == Py.False) { + write_byte(TYPE_FALSE); + } else if (v == Py.True) { + write_byte(TYPE_TRUE); + } else if (v instanceof PyInteger) { + write_byte(TYPE_INT); + write_int(((PyInteger) v).asInt()); + } else if (v instanceof PyLong) { + write_byte(TYPE_LONG); + write_long(((PyLong) v).getValue()); + } else if (v instanceof PyFloat) { + if (version == CURRENT_VERSION) { + write_byte(TYPE_BINARY_FLOAT); + write_binary_float((PyFloat) v); + } else { + write_byte(TYPE_FLOAT); + write_float((PyFloat) v); + } + } else if (v instanceof PyComplex) { + PyComplex x = (PyComplex) v; + if (version == CURRENT_VERSION) { + write_byte(TYPE_BINARY_COMPLEX); + write_binary_float(x.getReal()); + write_binary_float(x.getImag()); + } else { + write_byte(TYPE_COMPLEX); + write_float(x.getReal()); + write_float(x.getImag()); + } + } else if (v instanceof PyUnicode) { + write_byte(TYPE_UNICODE); + String buffer = ((PyUnicode) v).encode("utf-8").toString(); + write_int(buffer.length()); + write_string(buffer); + } else if (v instanceof PyString) { + // ignore interning + write_byte(TYPE_STRING); + write_int(v.__len__()); + write_string(v.toString()); + } else if (v instanceof PyTuple) { + write_byte(TYPE_TUPLE); + PyTuple t = (PyTuple) v; + int n = t.__len__(); + write_int(n); + for (int i = 0; i < n; i++) { + write_object(t.__getitem__(i), depth + 1); + } + } else if (v instanceof PyList) { + write_byte(TYPE_LIST); + PyList list = (PyList) v; + int n = list.__len__(); + write_int(n); + for (int i = 0; i < n; i++) { + write_object(list.__getitem__(i), depth + 1); + } + } else if (v instanceof PyDictionary) { + write_byte(TYPE_DICT); + PyDictionary dict = (PyDictionary) v; + for (PyObject item : dict.iteritems().asIterable()) { + PyTuple pair = (PyTuple) item; + write_object(pair.__getitem__(0), depth + 1); + write_object(pair.__getitem__(1), depth + 1); + } + write_object(null, depth + 1); + } else if (v instanceof BaseSet) { + if (v instanceof PySet) { + write_byte(TYPE_SET); + } else { + write_byte(TYPE_FROZENSET); + } + int n = v.__len__(); + write_int(n); + BaseSet set = (BaseSet) v; + for (PyObject item : set.asIterable()) { + write_object(item, depth + 1); + } + } else if (v instanceof PyBytecode) { + PyBytecode code = (PyBytecode) v; + write_byte(TYPE_CODE); + write_int(code.co_argcount); + write_int(code.co_nlocals); + write_int(code.co_stacksize); + write_int(code.co_flags); + write_object(Py.newString(new String(code.co_code)), depth + 1); + write_object(new PyTuple(code.co_consts), depth + 1); + write_strings(code.co_names, depth + 1); + write_strings(code.co_varnames, depth + 1); + write_strings(code.co_freevars, depth + 1); + write_strings(code.co_cellvars, depth + 1); + write_object(Py.newString(code.co_name), depth + 1); + write_int(code.co_firstlineno); + write_object(new PyTuple(code.co_lnotab), depth + 1); + } else { + write_byte(TYPE_UNKNOWN); + } + + depth--; + + } + } + + public static class Unmarshaller extends PyObject { + + private final PyIOFile file; + private final PyList strings = new PyList(); + private final int version; + int depth = 0; + + public Unmarshaller(PyObject file) { + this(file, CURRENT_VERSION); + } + + public Unmarshaller(PyObject file, int version) { + this.file = PyIOFileFactory.createIOFile(file); + this.version = version; + } + private boolean debug = false; + + public void _debug() { + debug = true; + } + + public PyObject load() { + try { + PyObject obj = read_object(0); + if (obj == null) { + throw Py.TypeError("NULL object in marshal data"); + } + return obj; + } catch (StringIndexOutOfBoundsException e) { + // convert from our PyIOFile abstraction to what marshal in CPython returns + // (although it's really just looking for no bombing) + throw Py.EOFError("EOF read where object expected"); + } + } + + private int read_byte() { + int b = file.read(1).charAt(0); + if (debug) { + System.err.print("[" + b + "]"); + } + return b; + } + + private String read_string(int n) { + return file.read(n); + } + + private int read_short() { + int x = read_byte(); + x |= read_byte() << 8; + return x; + } + + private int read_int() { // cpython calls this r_long + int x = read_byte(); + x |= read_byte() << 8; + x |= read_byte() << 16; + x |= read_byte() << 24; + return x; + } + + private long read_long64() { // cpython calls this r_long64 + long lo4 = read_int(); + long hi4 = read_int(); + long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL); + return x; + } + + private BigInteger read_long() { + int size = read_int(); + int sign = 1; + if (size < 0) { + sign = -1; + size = -size; + } + BigInteger result = BigInteger.ZERO; + for (int i = 0; i < size; i++) { + String digits = String.valueOf(read_short()); + result = result.or(new BigInteger(digits).shiftLeft(i * 15)); + } + if (sign < 0) { + result = result.negate(); + } + return result; + } + + private double read_float() { + int size = read_byte(); + return Py.newString(read_string(size)).atof(); + } + + private double read_binary_float() { + String buffer = read_string(8); + PyFloat num = (PyFloat) ((struct.unpack("d", buffer)).__getitem__(0)); + return num.asDouble(); + } + + private PyObject read_object_notnull(int depth) { + PyObject v = read_object(depth); + if (v == null) { + throw Py.ValueError("bad marshal data"); + } + return v; + } + + private String[] read_strings(int depth) { + PyTuple t = (PyTuple) read_object_notnull(depth); + String some_strings[] = new String[t.__len__()]; + int i = 0; + for (PyObject item : t.asIterable()) { + some_strings[i++] = item.toString(); + } + return some_strings; + } + + private PyObject read_object(int depth) { + if (depth >= MAX_MARSHAL_STACK_DEPTH) { + throw Py.ValueError("Maximum marshal stack depth"); // XXX - fix this exception + } + int type = read_byte(); + switch (type) { + + case TYPE_NULL: + return null; + + case TYPE_NONE: + return Py.None; + + case TYPE_STOPITER: + return Py.StopIteration; + + case TYPE_ELLIPSIS: + return Py.Ellipsis; + + case TYPE_FALSE: + return Py.False; + + case TYPE_TRUE: + return Py.True; + + case TYPE_INT: + return Py.newInteger(read_int()); + + case TYPE_INT64: + return Py.newInteger(read_long64()); + + case TYPE_LONG: { + return Py.newLong(read_long()); + } + + case TYPE_FLOAT: + return Py.newFloat(read_float()); + + case TYPE_BINARY_FLOAT: + return Py.newFloat(read_binary_float()); + + case TYPE_COMPLEX: { + double real = read_float(); + double imag = read_float(); + return new PyComplex(real, imag); + } + + case TYPE_BINARY_COMPLEX: { + double real = read_binary_float(); + double imag = read_binary_float(); + return new PyComplex(real, imag); + } + + case TYPE_INTERNED: + case TYPE_STRING: { + int size = read_int(); + String s = read_string(size); + if (type == TYPE_INTERNED) { + s.intern(); // do we really honor like this? + PyString pys = PyString.fromInterned(s); + strings.append(pys); + return pys; + } else { + return Py.newString(s); + } + } + + case TYPE_STRINGREF: { + int i = read_int(); + return strings.__getitem__(i); + } + + case TYPE_UNICODE: { + int n = read_int(); + PyString buffer = Py.newString(read_string(n)); + return buffer.decode("utf-8"); + } + + case TYPE_TUPLE: { + int n = read_int(); + if (n < 0) { + throw Py.ValueError("bad marshal data"); + } + PyObject items[] = new PyObject[n]; + for (int i = 0; i < n; i++) { + items[i] = read_object_notnull(depth + 1); + } + return new PyTuple(items); + } + + case TYPE_LIST: { + int n = read_int(); + if (n < 0) { + throw Py.ValueError("bad marshal data"); + } + PyObject items[] = new PyObject[n]; + for (int i = 0; i < n; i++) { + items[i] = read_object_notnull(depth + 1); + } + return new PyList(items); + } + + case TYPE_DICT: { + PyDictionary d = new PyDictionary(); + while (true) { + PyObject key = read_object(depth + 1); + if (key == null) { + break; + } + PyObject value = read_object(depth + 1); + if (value != null) { + d.__setitem__(key, value); + } + } + return d; + } + + case TYPE_SET: + case TYPE_FROZENSET: { + int n = read_int(); + PyObject items[] = new PyObject[n]; + for (int i = 0; i < n; i++) { + items[i] = read_object(depth + 1); + } + PyTuple v = new PyTuple(items); + if (type == TYPE_SET) { + return new PySet(v); + } else { + return new PyFrozenSet(v); + } + } + + + case TYPE_CODE: { + // XXX - support restricted execution mode? not certain if this is just legacy + int argcount = read_int(); + int nlocals = read_int(); + int stacksize = read_int(); + int flags = read_int(); + String code = read_object_notnull(depth + 1).toString(); + PyObject consts[] = ((PyTuple) read_object_notnull(depth + 1)).getArray(); + String names[] = read_strings(depth + 1); + String varnames[] = read_strings(depth + 1); + String freevars[] = read_strings(depth + 1); + String cellvars[] = read_strings(depth + 1); + String filename = read_object_notnull(depth + 1).toString(); + String name = read_object_notnull(depth + 1).toString(); + int firstlineno = read_int(); + String lnotab = read_object_notnull(depth + 1).toString(); + + return new PyBytecode( + argcount, nlocals, stacksize, flags, + code, consts, names, varnames, + filename, name, firstlineno, lnotab, + cellvars, freevars); + } + + default: + throw Py.ValueError("bad marshal data"); + } + } + } +} + Modified: branches/pbcvm/src/org/python/modules/cPickle.java =================================================================== --- branches/pbcvm/src/org/python/modules/cPickle.java 2009-02-01 08:12:13 UTC (rev 6004) +++ branches/pbcvm/src/org/python/modules/cPickle.java 2009-02-01 08:24:06 UTC (rev 6005) @@ -658,143 +658,15 @@ - // Factory for creating IOFile representation. - private static IOFile createIOFile(PyObject file) { - Object f = file.__tojava__(cStringIO.StringIO.class); - if (f != Py.NoConversion) - return new cStringIOFile(file); - else if (__builtin__.isinstance(file, FileType)) - return new FileIOFile(file); - else - return new ObjectIOFile(file); - } - - - // IOFiles encapsulates and optimise access to the different file - // representation. - interface IOFile { - public abstract void write(String str); - // Usefull optimization since most data written are chars. - public abstract void write(char str); - public abstract void flush(); - public abstract String read(int len); - // Usefull optimization since all readlines removes the - // trainling newline. - public abstract String readlineNoNl(); - - } - - - // Use a cStringIO as a file. - static class cStringIOFile implements IOFile { - cStringIO.StringIO file; - - cStringIOFile(PyObject file) { - this.file = (cStringIO.StringIO)file.__tojava__(Object.class); - } - - public void write(String str) { - file.write(str); - } - - public void write(char ch) { - file.writeChar(ch); - } - - public void flush() {} - - public String read(int len) { - return file.read(len).asString(); - } - - public String readlineNoNl() { - return file.readlineNoNl().asString(); - } - } - - - // Use a PyFile as a file. - static class FileIOFile implements IOFile { - PyFile file; - - FileIOFile(PyObject file) { - this.file = (PyFile)file.__tojava__(PyFile.class); - if (this.file.getClosed()) - throw Py.ValueError("I/O operation on closed file"); - } - - public void write(String str) { - file.write(str); - } - - public void write(char ch) { - file.write(cStringIO.getString(ch)); - } - - public void flush() {} - - public String read(int len) { - return file.read(len).toString(); - } - - public String readlineNoNl() { - String line = file.readline().toString(); - return line.substring(0, line.length()-1); - } - } - - - // Use any python object as a file. - static class ObjectIOFile implements IOFile { - char[] charr = new char[1]; - StringBuilder buff = new StringBuilder(); - PyObject write; - PyObject read; - PyObject readline; - final int BUF_SIZE = 256; - - ObjectIOFile(PyObject file) { -// this.file = file; - write = file.__findattr__("write"); - read = file.__findattr__("read"); - readline = file.__findattr__("readline"); - } - - public void write(String str) { - buff.append(str); - if (buff.length() > BUF_SIZE) - flush(); - } - - public void write(char ch) { - buff.append(ch); - if (buff.length() > BUF_SIZE) - flush(); - } - - public void flush() { - write.__call__(new PyString(buff.toString())); - buff.setLength(0); - } - - public String read(int len) { - return read.__call__(new PyInteger(len)).toString(); - } - - public String readlineNoNl() { - String line = readline.__call__().toString(); - return line.substring(0, line.length()-1); - } - } - - + // Factory for creating PyIOFile representation. + /** * The Pickler object * @see cPickle#Pickler(PyObject) * @see cPickle#Pickler(PyObject,int) */ static public class Pickler { - private IOFile file; + private PyIOFile file; private int protocol; /** @@ -827,7 +699,7 @@ public Pickler(PyObject file, int protocol) { - this.file = createIOFile(file); + this.file = PyIOFileFactory.createIOFile(file); this.protocol = protocol; } @@ -1700,7 +1572,7 @@ */ static public class Unpickler { - private IOFile file; + private PyIOFile file; public Map<String,PyObject> memo = Generic.map(); @@ -1723,7 +1595,7 @@ Unpickler(PyObject file) { - this.file = createIOFile(file); + this.file = PyIOFileFactory.createIOFile(file); } Modified: branches/pbcvm/src/org/python/modules/struct.java =================================================================== --- branches/pbcvm/src/org/python/modules/struct.java 2009-02-01 08:12:13 UTC (rev 6004) +++ branches/pbcvm/src/org/python/modules/struct.java 2009-02-01 08:24:06 UTC (rev 6005) @@ -11,7 +11,6 @@ import org.python.core.Py; import org.python.core.PyException; import org.python.core.PyFloat; -import org.python.core.PyInteger; import org.python.core.PyList; import org.python.core.PyLong; import org.python.core.PyObject; @@ -20,13 +19,7 @@ import org.python.core.PyTuple; import java.math.BigInteger; -import java.util.Arrays; -import org.python.core.ClassDictInit; import org.python.core.PyArray; -import org.python.core.PyType; -import org.python.expose.ExposedGet; -import org.python.expose.ExposedNew; -import org.python.expose.ExposedType; /** * This module performs conversions between Python values and C @@ -103,7 +96,7 @@ * <tr><td align=center><samp>I</samp></td> * <td><tt>unsigned int</tt></td> * <td>integer</td> - * <tr><td align=center><samp>l</samp></td> + * <tr><td align=center><samp>size</samp></td> * <td><tt>long</tt></td> * <td>integer</td> * <tr><td align=center><samp>L</samp></td> @@ -410,13 +403,13 @@ } ByteStream(String s, int offset) { - int l = s.length() - offset; - data = new char[l]; + int size = s.length() - offset; + data = new char[size]; s.getChars(offset, s.length(), data, 0); - len = l; + len = size; pos = 0; -// System.out.println("s.length()=" + s.length() + ",offset=" + offset + ",l=" + l + ",data=" + Arrays.toString(data)); +// System.out.println("s.length()=" + s.length() + ",offset=" + offset + ",size=" + size + ",data=" + Arrays.toString(data)); } int readByte() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-01 08:12:17
|
Revision: 6004 http://jython.svn.sourceforge.net/jython/?rev=6004&view=rev Author: zyasoft Date: 2009-02-01 08:12:13 +0000 (Sun, 01 Feb 2009) Log Message: ----------- From http://svn.python.org/projects/python/branches/release25-maint/Lib/test/test_marshal@27825 Added Paths: ----------- branches/pbcvm/Lib/test/test_marshal.py Added: branches/pbcvm/Lib/test/test_marshal.py =================================================================== --- branches/pbcvm/Lib/test/test_marshal.py (rev 0) +++ branches/pbcvm/Lib/test/test_marshal.py 2009-02-01 08:12:13 UTC (rev 6004) @@ -0,0 +1,257 @@ +#!/usr/bin/env python +# -*- coding: iso-8859-1 -*- + +from test import test_support +import marshal +import sys +import unittest +import os + +class IntTestCase(unittest.TestCase): + def test_ints(self): + # Test the full range of Python ints. + n = sys.maxint + while n: + for expected in (-n, n): + s = marshal.dumps(expected) + got = marshal.loads(s) + self.assertEqual(expected, got) + marshal.dump(expected, file(test_support.TESTFN, "wb")) + got = marshal.load(file(test_support.TESTFN, "rb")) + self.assertEqual(expected, got) + n = n >> 1 + os.unlink(test_support.TESTFN) + + def test_int64(self): + # Simulate int marshaling on a 64-bit box. This is most interesting if + # we're running the test on a 32-bit box, of course. + + def to_little_endian_string(value, nbytes): + bytes = [] + for i in range(nbytes): + bytes.append(chr(value & 0xff)) + value >>= 8 + return ''.join(bytes) + + maxint64 = (1L << 63) - 1 + minint64 = -maxint64-1 + + for base in maxint64, minint64, -maxint64, -(minint64 >> 1): + while base: + s = 'I' + to_little_endian_string(base, 8) + got = marshal.loads(s) + self.assertEqual(base, got) + if base == -1: # a fixed-point for shifting right 1 + base = 0 + else: + base >>= 1 + + def test_bool(self): + for b in (True, False): + new = marshal.loads(marshal.dumps(b)) + self.assertEqual(b, new) + self.assertEqual(type(b), type(new)) + marshal.dump(b, file(test_support.TESTFN, "wb")) + new = marshal.load(file(test_support.TESTFN, "rb")) + self.assertEqual(b, new) + self.assertEqual(type(b), type(new)) + +class FloatTestCase(unittest.TestCase): + def test_floats(self): + # Test a few floats + small = 1e-25 + n = sys.maxint * 3.7e250 + while n > small: + for expected in (-n, n): + f = float(expected) + s = marshal.dumps(f) + got = marshal.loads(s) + self.assertEqual(f, got) + marshal.dump(f, file(test_support.TESTFN, "wb")) + got = marshal.load(file(test_support.TESTFN, "rb")) + self.assertEqual(f, got) + n /= 123.4567 + + f = 0.0 + s = marshal.dumps(f, 2) + got = marshal.loads(s) + self.assertEqual(f, got) + # and with version <= 1 (floats marshalled differently then) + s = marshal.dumps(f, 1) + got = marshal.loads(s) + self.assertEqual(f, got) + + n = sys.maxint * 3.7e-250 + while n < small: + for expected in (-n, n): + f = float(expected) + + s = marshal.dumps(f) + got = marshal.loads(s) + self.assertEqual(f, got) + + s = marshal.dumps(f, 1) + got = marshal.loads(s) + self.assertEqual(f, got) + + marshal.dump(f, file(test_support.TESTFN, "wb")) + got = marshal.load(file(test_support.TESTFN, "rb")) + self.assertEqual(f, got) + + marshal.dump(f, file(test_support.TESTFN, "wb"), 1) + got = marshal.load(file(test_support.TESTFN, "rb")) + self.assertEqual(f, got) + n *= 123.4567 + os.unlink(test_support.TESTFN) + +class StringTestCase(unittest.TestCase): + def test_unicode(self): + for s in [u"", u"Andr\xE8 Previn", u"abc", u" "*10000]: + new = marshal.loads(marshal.dumps(s)) + self.assertEqual(s, new) + self.assertEqual(type(s), type(new)) + marshal.dump(s, file(test_support.TESTFN, "wb")) + new = marshal.load(file(test_support.TESTFN, "rb")) + self.assertEqual(s, new) + self.assertEqual(type(s), type(new)) + os.unlink(test_support.TESTFN) + + def test_string(self): + for s in ["", "Andr\xE8 Previn", "abc", " "*10000]: + new = marshal.loads(marshal.dumps(s)) + self.assertEqual(s, new) + self.assertEqual(type(s), type(new)) + marshal.dump(s, file(test_support.TESTFN, "wb")) + new = marshal.load(file(test_support.TESTFN, "rb")) + self.assertEqual(s, new) + self.assertEqual(type(s), type(new)) + os.unlink(test_support.TESTFN) + + def test_buffer(self): + for s in ["", "Andr\xE8 Previn", "abc", " "*10000]: + b = buffer(s) + new = marshal.loads(marshal.dumps(b)) + self.assertEqual(s, new) + marshal.dump(b, file(test_support.TESTFN, "wb")) + new = marshal.load(file(test_support.TESTFN, "rb")) + self.assertEqual(s, new) + os.unlink(test_support.TESTFN) + +class ExceptionTestCase(unittest.TestCase): + def test_exceptions(self): + new = marshal.loads(marshal.dumps(StopIteration)) + self.assertEqual(StopIteration, new) + +class CodeTestCase(unittest.TestCase): + def test_code(self): + co = ExceptionTestCase.test_exceptions.func_code + new = marshal.loads(marshal.dumps(co)) + self.assertEqual(co, new) + +class ContainerTestCase(unittest.TestCase): + d = {'astring': 'fo...@ba...z.spam', + 'afloat': 7283.43, + 'anint': 2**20, + 'ashortlong': 2L, + 'alist': ['.zyx.41'], + 'atuple': ('.zyx.41',)*10, + 'aboolean': False, + 'aunicode': u"Andr\xE8 Previn" + } + def test_dict(self): + new = marshal.loads(marshal.dumps(self.d)) + self.assertEqual(self.d, new) + marshal.dump(self.d, file(test_support.TESTFN, "wb")) + new = marshal.load(file(test_support.TESTFN, "rb")) + self.assertEqual(self.d, new) + os.unlink(test_support.TESTFN) + + def test_list(self): + lst = self.d.items() + new = marshal.loads(marshal.dumps(lst)) + self.assertEqual(lst, new) + marshal.dump(lst, file(test_support.TESTFN, "wb")) + new = marshal.load(file(test_support.TESTFN, "rb")) + self.assertEqual(lst, new) + os.unlink(test_support.TESTFN) + + def test_tuple(self): + t = tuple(self.d.keys()) + new = marshal.loads(marshal.dumps(t)) + self.assertEqual(t, new) + marshal.dump(t, file(test_support.TESTFN, "wb")) + new = marshal.load(file(test_support.TESTFN, "rb")) + self.assertEqual(t, new) + os.unlink(test_support.TESTFN) + + def test_sets(self): + for constructor in (set, frozenset): + t = constructor(self.d.keys()) + new = marshal.loads(marshal.dumps(t)) + self.assertEqual(t, new) + self.assert_(isinstance(new, constructor)) + self.assertNotEqual(id(t), id(new)) + marshal.dump(t, file(test_support.TESTFN, "wb")) + new = marshal.load(file(test_support.TESTFN, "rb")) + self.assertEqual(t, new) + os.unlink(test_support.TESTFN) + +class BugsTestCase(unittest.TestCase): + def test_bug_5888452(self): + # Simple-minded check for SF 588452: Debug build crashes + marshal.dumps([128] * 1000) + + def test_patch_873224(self): + self.assertRaises(Exception, marshal.loads, '0') + self.assertRaises(Exception, marshal.loads, 'f') + self.assertRaises(Exception, marshal.loads, marshal.dumps(5L)[:-1]) + + def test_version_argument(self): + # Python 2.4.0 crashes for any call to marshal.dumps(x, y) + self.assertEquals(marshal.loads(marshal.dumps(5, 0)), 5) + self.assertEquals(marshal.loads(marshal.dumps(5, 1)), 5) + + def test_fuzz(self): + # simple test that it's at least not *totally* trivial to + # crash from bad marshal data + for c in [chr(i) for i in range(256)]: + try: + marshal.loads(c) + except Exception: + pass + + def test_loads_recursion(self): + s = 'c' + ('X' * 4*4) + '{' * 2**20 + self.assertRaises(ValueError, marshal.loads, s) + + def test_recursion_limit(self): + # Create a deeply nested structure. + head = last = [] + # The max stack depth should match the value in Python/marshal.c. + MAX_MARSHAL_STACK_DEPTH = 2000 + for i in range(MAX_MARSHAL_STACK_DEPTH - 2): + last.append([0]) + last = last[-1] + + # Verify we don't blow out the stack with dumps/load. + data = marshal.dumps(head) + new_head = marshal.loads(data) + # Don't use == to compare objects, it can exceed the recursion limit. + self.assertEqual(len(new_head), len(head)) + self.assertEqual(len(new_head[0]), len(head[0])) + self.assertEqual(len(new_head[-1]), len(head[-1])) + + last.append([0]) + self.assertRaises(ValueError, marshal.dumps, head) + +def test_main(): + test_support.run_unittest(IntTestCase, + FloatTestCase, + StringTestCase, + CodeTestCase, + ContainerTestCase, + ExceptionTestCase, + BugsTestCase) + +if __name__ == "__main__": + test_main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-01-31 02:20:53
|
Revision: 6003 http://jython.svn.sourceforge.net/jython/?rev=6003&view=rev Author: pjenvey Date: 2009-01-31 02:20:48 +0000 (Sat, 31 Jan 2009) Log Message: ----------- make --version resemble CPython Modified Paths: -------------- trunk/jython/src/org/python/util/jython.java Modified: trunk/jython/src/org/python/util/jython.java =================================================================== --- trunk/jython/src/org/python/util/jython.java 2009-01-29 20:51:21 UTC (rev 6002) +++ trunk/jython/src/org/python/util/jython.java 2009-01-31 02:20:48 UTC (rev 6003) @@ -11,6 +11,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; +import org.python.Version; import org.python.core.Options; import org.python.core.Py; import org.python.core.PyCode; @@ -119,8 +120,7 @@ CommandLineOptions opts = new CommandLineOptions(); if (!opts.parse(args)) { if (opts.version) { - PySystemState.determinePlatform(System.getProperties()); - System.err.println(InteractiveConsole.getDefaultBanner()); + System.err.println("Jython " + Version.PY_VERSION); System.exit(0); } if (!opts.runModule) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <am...@us...> - 2009-01-29 20:51:24
|
Revision: 6002 http://jython.svn.sourceforge.net/jython/?rev=6002&view=rev Author: amak Date: 2009-01-29 20:51:21 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Back-porting changes relating to UDP broadcast. Also back-porting some variable scoping changes to the test_socket module. Modified Paths: -------------- branches/Release_2_2maint/jython/Lib/socket.py branches/Release_2_2maint/jython/Lib/test/test_socket.py Modified: branches/Release_2_2maint/jython/Lib/socket.py =================================================================== --- branches/Release_2_2maint/jython/Lib/socket.py 2009-01-29 17:59:04 UTC (rev 6001) +++ branches/Release_2_2maint/jython/Lib/socket.py 2009-01-29 20:51:21 UTC (rev 6002) @@ -177,6 +177,9 @@ TCP_NODELAY = 256 +INADDR_ANY = "0.0.0.0" +INADDR_BROADCAST = "255.255.255.255" + # Options with negative constants are not supported # They are being added here so that code that refers to them # will not break with an AttributeError @@ -712,7 +715,7 @@ def _get_jsocket(self): return self.sock_impl.jsocket -def _unpack_address_tuple(address_tuple, for_tx=False): +def _unpack_address_tuple(address_tuple): # TODO: Upgrade to support the 4-tuples used for IPv6 addresses # which include flowinfo and scope_id. # To be upgraded in synch with getaddrinfo @@ -722,11 +725,6 @@ or type(address_tuple[1]) is not type(0): raise TypeError(error_message) hostname = address_tuple[0].strip() - if hostname == "<broadcast>": - if for_tx: - hostname = "255.255.255.255" - else: - hostname = "0.0.0.0" return hostname, address_tuple[1] class _tcpsocket(_nonblocking_api_mixin): @@ -902,6 +900,8 @@ try: assert not self.sock_impl host, port = _unpack_address_tuple(addr) + if host == "": + host = INADDR_ANY host_address = java.net.InetAddress.getByName(host) self.sock_impl = _datagram_socket_impl(port, host_address, self.pending_options[SO_REUSEADDR]) self._config() @@ -941,7 +941,9 @@ if not self.sock_impl: self.sock_impl = _datagram_socket_impl() self._config() - host, port = _unpack_address_tuple(addr, True) + host, port = _unpack_address_tuple(addr) + if host == "<broadcast>": + host = INADDR_BROADCAST byte_array = java.lang.String(data).getBytes('iso-8859-1') result = self.sock_impl.sendto(byte_array, host, port, flags) return result Modified: branches/Release_2_2maint/jython/Lib/test/test_socket.py =================================================================== --- branches/Release_2_2maint/jython/Lib/test/test_socket.py 2009-01-29 17:59:04 UTC (rev 6001) +++ branches/Release_2_2maint/jython/Lib/test/test_socket.py 2009-01-29 20:51:21 UTC (rev 6002) @@ -28,10 +28,13 @@ class SocketTCPTest(unittest.TestCase): + HOST = HOST + PORT = PORT + def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.serv.bind((HOST, PORT)) + self.serv.bind((self.HOST, self.PORT)) self.serv.listen(1) def tearDown(self): @@ -40,10 +43,13 @@ class SocketUDPTest(unittest.TestCase): + HOST = HOST + PORT = PORT + def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.serv.bind((HOST, PORT)) + self.serv.bind((self.HOST, self.PORT)) def tearDown(self): self.serv.close() @@ -196,7 +202,7 @@ def clientSetUp(self): ThreadedTCPSocketTest.clientSetUp(self) - self.cli.connect((HOST, PORT)) + self.cli.connect((self.HOST, self.PORT)) self.serv_conn = self.cli def clientTearDown(self): @@ -540,7 +546,7 @@ # First listen on a server socket, so that the connection won't be refused. server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.bind( (HOST, PORT) ) - server_sock.listen() + server_sock.listen(50) # Now do the tests sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._testSetAndGetOption(sock, option, values) @@ -561,7 +567,7 @@ self._testSetAndGetOption(sock, option, values) # now bind and listen on the socket i.e. cause the implementation socket to be created sock.bind( (HOST, PORT) ) - sock.listen() + sock.listen(50) self.failUnlessEqual(sock.getsockopt(socket.SOL_SOCKET, option), values[-1], \ "Option value '%s'='%s' did not propagate to implementation socket" % (option, values[-1])) self._testSetAndGetOption(sock, option, values) @@ -825,7 +831,7 @@ self.assertEqual(msg, MSG) def _testSendtoAndRecv(self): - self.cli.sendto(MSG, 0, (HOST, PORT)) + self.cli.sendto(MSG, 0, (self.HOST, self.PORT)) def testSendtoAndRecvTimeoutMode(self): # Need to test again in timeout mode, which follows @@ -836,7 +842,7 @@ def _testSendtoAndRecvTimeoutMode(self): self.cli.settimeout(10) - self.cli.sendto(MSG, 0, (HOST, PORT)) + self.cli.sendto(MSG, 0, (self.HOST, self.PORT)) def testRecvFrom(self): # Testing recvfrom() over UDP @@ -844,7 +850,7 @@ self.assertEqual(msg, MSG) def _testRecvFrom(self): - self.cli.sendto(MSG, 0, (HOST, PORT)) + self.cli.sendto(MSG, 0, (self.HOST, self.PORT)) def testRecvFromTimeoutMode(self): # Need to test again in timeout mode, which follows @@ -855,7 +861,7 @@ def _testRecvFromTimeoutMode(self): self.cli.settimeout(10) - self.cli.sendto(MSG, 0, (HOST, PORT)) + self.cli.sendto(MSG, 0, (self.HOST, self.PORT)) def testSendtoEightBitSafe(self): # This test is necessary because java only supports signed bytes @@ -863,7 +869,7 @@ self.assertEqual(msg, EIGHT_BIT_MSG) def _testSendtoEightBitSafe(self): - self.cli.sendto(EIGHT_BIT_MSG, 0, (HOST, PORT)) + self.cli.sendto(EIGHT_BIT_MSG, 0, (self.HOST, self.PORT)) def testSendtoEightBitSafeTimeoutMode(self): # Need to test again in timeout mode, which follows @@ -874,7 +880,7 @@ def _testSendtoEightBitSafeTimeoutMode(self): self.cli.settimeout(10) - self.cli.sendto(EIGHT_BIT_MSG, 0, (HOST, PORT)) + self.cli.sendto(EIGHT_BIT_MSG, 0, (self.HOST, self.PORT)) class UDPBroadcastTest(ThreadedUDPSocketTest): @@ -883,13 +889,13 @@ self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) def testBroadcast(self): - self.serv.bind( ("<broadcast>", PORT) ) + self.serv.bind( ("", self.PORT) ) msg = self.serv.recv(len(EIGHT_BIT_MSG)) self.assertEqual(msg, EIGHT_BIT_MSG) def _testBroadcast(self): self.cli.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) - self.cli.sendto(EIGHT_BIT_MSG, ("<broadcast>", PORT) ) + self.cli.sendto(EIGHT_BIT_MSG, ("<broadcast>", self.PORT) ) class BasicSocketPairTest(SocketPairTest): @@ -956,7 +962,7 @@ def _testAcceptConnection(self): # Make a connection to the server - self.cli.connect((HOST, PORT)) + self.cli.connect((self.HOST, self.PORT)) # # AMAK: 20070311 @@ -971,7 +977,7 @@ def _testBlockingConnect(self): # Testing blocking connect self.cli.settimeout(10) - self.cli.connect((HOST, PORT)) + self.cli.connect((self.HOST, self.PORT)) def testNonBlockingConnect(self): # Testing non-blocking connect @@ -980,7 +986,7 @@ def _testNonBlockingConnect(self): # Testing non-blocking connect self.cli.setblocking(0) - result = self.cli.connect_ex((HOST, PORT)) + result = self.cli.connect_ex((self.HOST, self.PORT)) rfds, wfds, xfds = select.select([], [self.cli], []) self.failUnless(self.cli in wfds) try: @@ -999,13 +1005,13 @@ def _testConnectWithLocalBind(self): # Testing blocking connect with local bind - cli_port = PORT - 1 + cli_port = self.PORT - 1 while True: # Keep trying until a local port is available self.cli.settimeout(1) - self.cli.bind( (HOST, cli_port) ) + self.cli.bind( (self.HOST, cli_port) ) try: - self.cli.connect((HOST, PORT)) + self.cli.connect((self.HOST, self.PORT)) break except socket.error, se: # cli_port is in use (maybe in TIME_WAIT state from a @@ -1033,7 +1039,7 @@ self.fail("Non-blocking socket with data should been in read list.") def _testRecvData(self): - self.cli.connect((HOST, PORT)) + self.cli.connect((self.HOST, self.PORT)) self.cli.send(MSG) def testRecvNoData(self): @@ -1048,7 +1054,7 @@ self.fail("Non-blocking recv of no data should have raised socket.error.") def _testRecvNoData(self): - self.cli.connect((HOST, PORT)) + self.cli.connect((self.HOST, self.PORT)) time.sleep(0.1) class NonBlockingUDPTests(ThreadedUDPSocketTest): pass @@ -1096,7 +1102,7 @@ self.cli_file = self.cli.makefile('wb') self.cli_file.close() try: - self.cli.sendto(MSG, 0, (HOST, PORT)) + self.cli.sendto(MSG, 0, (self.HOST, self.PORT)) except Exception, x: self.fail("Closing file wrapper appears to have closed underlying socket: %s" % str(x)) @@ -1108,7 +1114,7 @@ def _testCloseSocketDoesNotCloseFile(self): try: - self.cli.sendto(MSG, 0, (HOST, PORT)) + self.cli.sendto(MSG, 0, (self.HOST, self.PORT)) except Exception, x: self.fail("Closing file wrapper appears to have closed underlying socket: %s" % str(x)) @@ -1496,12 +1502,12 @@ def testBindException(self): # First bind to the target port self.s.bind( (HOST, PORT) ) - self.s.listen() + self.s.listen(50) try: # And then try to bind again t = socket.socket(socket.AF_INET, socket.SOCK_STREAM) t.bind( (HOST, PORT) ) - t.listen() + t.listen(50) except socket.error, se: self.failUnlessEqual(se[0], errno.EADDRINUSE) except Exception, x: @@ -1571,7 +1577,7 @@ self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def testShutdownIOOnListener(self): - self.socket.listen() # socket is now a server socket + self.socket.listen(50) # socket is now a server socket try: self.socket.shutdown(socket.SHUT_RDWR) except Exception, x: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <am...@us...> - 2009-01-29 17:59:13
|
Revision: 6001 http://jython.svn.sourceforge.net/jython/?rev=6001&view=rev Author: amak Date: 2009-01-29 17:59:04 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Re-arranging the socket shutdown methods. TCP client sockets can shutdown their input streams, but this is not appropriate for either TCP server sockets or UDP sockets, which don't have input and output streams. For these latter two types, the shutdown method should have the same effect as the close method, and thus the shutdown method is a no-op for these types. I have documented this difference between cpython and jython on the wiki. This should finally resolve bug 1121: listening socket shutdown expects the wrong kind of socket http://bugs.jython.org/issue1121 Modified Paths: -------------- trunk/jython/Lib/socket.py trunk/jython/Lib/test/test_socket.py Modified: trunk/jython/Lib/socket.py =================================================================== --- trunk/jython/Lib/socket.py 2009-01-29 17:41:00 UTC (rev 6000) +++ trunk/jython/Lib/socket.py 2009-01-29 17:59:04 UTC (rev 6001) @@ -248,22 +248,6 @@ def close(self): self.jsocket.close() - def shutdownInput(self): - try: - self.jsocket.shutdownInput() - except AttributeError, ax: - pass # Fail silently server sockets - except java.lang.Exception, jlx: - raise _map_exception(jlx) - - def shutdownOutput(self): - try: - self.jsocket.shutdownOutput() - except AttributeError, ax: - pass # Fail silently server sockets - except java.lang.Exception, jlx: - raise _map_exception(jlx) - def getchannel(self): return self.jchannel @@ -340,6 +324,12 @@ else: return self._do_write_nio(buf) + def shutdown(self, how): + if how in (SHUT_RD, SHUT_RDWR): + self.jsocket.shutdownInput() + if how in (SHUT_WR, SHUT_RDWR): + self.jsocket.shutdownOutput() + class _server_socket_impl(_nio_impl): options = { @@ -371,6 +361,13 @@ new_cli_sock = self.jsocket.accept() return _client_socket_impl(new_cli_sock) + def shutdown(self, how): + # This is no-op on java, for server sockets. + # What the user wants to achieve is achieved by calling close() on + # java/jython. But we can't call that here because that would then + # later cause the user explicit close() call to fail + pass + class _datagram_socket_impl(_nio_impl): options = { @@ -406,6 +403,13 @@ """ self.jchannel.disconnect() + def shutdown(self, how): + # This is no-op on java, for datagram sockets. + # What the user wants to achieve is achieved by calling close() on + # java/jython. But we can't call that here because that would then + # later cause the user explicit close() call to fail + pass + def _do_send_net(self, byte_array, socket_address, flags): # Need two separate implementations because the java.nio APIs do not support timeouts num_bytes = len(byte_array) @@ -689,6 +693,22 @@ except java.lang.Exception, jlx: raise _map_exception(jlx) + def shutdown(self, how): + assert how in (SHUT_RD, SHUT_WR, SHUT_RDWR) + if not self.sock_impl: + raise error(errno.ENOTCONN, "Transport endpoint is not connected") + try: + self.sock_impl.shutdown(how) + except java.lang.Exception, jlx: + raise _map_exception(jlx) + + def close(self): + try: + if self.sock_impl: + self.sock_impl.close() + except java.lang.Exception, jlx: + raise _map_exception(jlx) + def _config(self): assert self.mode in _permitted_modes if self.sock_impl: @@ -878,15 +898,6 @@ except java.lang.Exception, jlx: raise _map_exception(jlx) - def shutdown(self, how): - if not self.sock_impl: - raise error(errno.ENOTCONN, "Transport endpoint is not connected") - assert how in (SHUT_RD, SHUT_WR, SHUT_RDWR) - if how in (SHUT_RD, SHUT_RDWR): - self.sock_impl.shutdownInput() - if how in (SHUT_WR, SHUT_RDWR): - self.sock_impl.shutdownOutput() - def close(self): try: if self.istream: @@ -1015,13 +1026,6 @@ def __del__(self): self.close() - def close(self): - try: - if self.sock_impl: - self.sock_impl.close() - except java.lang.Exception, jlx: - raise _map_exception(jlx) - _socketmethods = ( 'bind', 'connect', 'connect_ex', 'fileno', 'listen', 'getpeername', 'getsockname', 'getsockopt', 'setsockopt', Modified: trunk/jython/Lib/test/test_socket.py =================================================================== --- trunk/jython/Lib/test/test_socket.py 2009-01-29 17:41:00 UTC (rev 6000) +++ trunk/jython/Lib/test/test_socket.py 2009-01-29 17:59:04 UTC (rev 6001) @@ -822,10 +822,20 @@ def setUp(self): self.sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) + def testBindSpecific(self): + self.sock.bind( (self.HOST, self.PORT) ) # Use a specific port + actual_port = self.sock.getsockname()[1] + self.failUnless(actual_port == self.PORT, + "Binding to specific port number should have returned same number: %d != %d" % (actual_port, self.PORT)) + def testBindEphemeral(self): self.sock.bind( (self.HOST, 0) ) # let system choose a free port self.failUnless(self.sock.getsockname()[1] != 0, "Binding to port zero should have allocated an ephemeral port number") + def testShutdown(self): + self.sock.bind( (self.HOST, self.PORT) ) + self.sock.shutdown(socket.SHUT_RDWR) + def tearDown(self): self.sock.close() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <am...@us...> - 2009-01-29 17:41:07
|
Revision: 6000 http://jython.svn.sourceforge.net/jython/?rev=6000&view=rev Author: amak Date: 2009-01-29 17:41:00 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Re-arranging the socket shutdown methods. TCP client sockets can shutdown their input streams, but this is not appropriate for either TCP server sockets or UDP sockets, which don't have input and output streams. For these latter two types, the shutdown method should have the same effect as the close method, and thus the shutdown method is a no-op for these types. I have documented this difference between cpython and jython on the wiki. This should finally resolve bug 1121: listening socket shutdown expects the wrong kind of socket http://bugs.jython.org/issue1121 Modified Paths: -------------- branches/Release_2_2maint/jython/Lib/socket.py branches/Release_2_2maint/jython/Lib/test/test_socket.py Modified: branches/Release_2_2maint/jython/Lib/socket.py =================================================================== --- branches/Release_2_2maint/jython/Lib/socket.py 2009-01-29 14:24:33 UTC (rev 5999) +++ branches/Release_2_2maint/jython/Lib/socket.py 2009-01-29 17:41:00 UTC (rev 6000) @@ -239,22 +239,6 @@ def close(self): self.jsocket.close() - def shutdownInput(self): - try: - self.jsocket.shutdownInput() - except AttributeError, ax: - pass # Fail silently server sockets - except java.lang.Exception, jlx: - raise _map_exception(jlx) - - def shutdownOutput(self): - try: - self.jsocket.shutdownOutput() - except AttributeError, ax: - pass # Fail silently server sockets - except java.lang.Exception, jlx: - raise _map_exception(jlx) - def getchannel(self): return self.jchannel @@ -329,6 +313,12 @@ else: return self._do_write_nio(buf) + def shutdown(self, how): + if how in (SHUT_RD, SHUT_RDWR): + self.jsocket.shutdownInput() + if how in (SHUT_WR, SHUT_RDWR): + self.jsocket.shutdownOutput() + class _server_socket_impl(_nio_impl): options = { @@ -359,6 +349,13 @@ new_cli_sock = self.jsocket.accept() return _client_socket_impl(new_cli_sock) + def shutdown(self, how): + # This is no-op on java, for server sockets. + # What the user wants to achieve is achieved by calling close() on + # java/jython. But we can't call that here because that would then + # later cause the user explicit close() call to fail + pass + class _datagram_socket_impl(_nio_impl): options = { @@ -393,6 +390,13 @@ """ self.jchannel.disconnect() + def shutdown(self, how): + # This is no-op on java, for datagram sockets. + # What the user wants to achieve is achieved by calling close() on + # java/jython. But we can't call that here because that would then + # later cause the user explicit close() call to fail + pass + def _do_send_net(self, byte_array, socket_address, flags): # Need two separate implementations because the java.nio APIs do not support timeouts num_bytes = len(byte_array) @@ -674,6 +678,22 @@ except java.lang.Exception, jlx: raise _map_exception(jlx) + def shutdown(self, how): + assert how in (SHUT_RD, SHUT_WR, SHUT_RDWR) + if not self.sock_impl: + raise error(errno.ENOTCONN, "Transport endpoint is not connected") + try: + self.sock_impl.shutdown(how) + except java.lang.Exception, jlx: + raise _map_exception(jlx) + + def close(self): + try: + if self.sock_impl: + self.sock_impl.close() + except java.lang.Exception, jlx: + raise _map_exception(jlx) + def _config(self): assert self.mode in _permitted_modes if self.sock_impl: @@ -859,15 +879,6 @@ except java.lang.Exception, jlx: raise _map_exception(jlx) - def shutdown(self, how): - if not self.sock_impl: - raise error(errno.ENOTCONN, "Transport endpoint is not connected") - assert how in (SHUT_RD, SHUT_WR, SHUT_RDWR) - if how in (SHUT_RD, SHUT_RDWR): - self.sock_impl.shutdownInput() - if how in (SHUT_WR, SHUT_RDWR): - self.sock_impl.shutdownOutput() - def close(self): try: if self.istream: @@ -877,8 +888,7 @@ if self.sock_impl: self.sock_impl.close() except java.lang.Exception, jlx: - raise _map_exception(jlx) - + raise _map_exception(jlx) class _udpsocket(_nonblocking_api_mixin): @@ -991,14 +1001,7 @@ def __del__(self): self.close() - - def close(self): - try: - if self.sock_impl: - self.sock_impl.close() - except java.lang.Exception, jlx: - raise _map_exception(jlx) - + _socketmethods = ( 'bind', 'connect', 'connect_ex', 'fileno', 'listen', 'getpeername', 'getsockname', 'getsockopt', 'setsockopt', Modified: branches/Release_2_2maint/jython/Lib/test/test_socket.py =================================================================== --- branches/Release_2_2maint/jython/Lib/test/test_socket.py 2009-01-29 14:24:33 UTC (rev 5999) +++ branches/Release_2_2maint/jython/Lib/test/test_socket.py 2009-01-29 17:41:00 UTC (rev 6000) @@ -797,10 +797,20 @@ def setUp(self): self.sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) + def testBindSpecific(self): + self.sock.bind( (self.HOST, self.PORT) ) # Use a specific port + actual_port = self.sock.getsockname()[1] + self.failUnless(actual_port == self.PORT, + "Binding to specific port number should have returned same number: %d != %d" % (actual_port, self.PORT)) + def testBindEphemeral(self): self.sock.bind( (self.HOST, 0) ) # let system choose a free port self.failUnless(self.sock.getsockname()[1] != 0, "Binding to port zero should have allocated an ephemeral port number") + def testShutdown(self): + self.sock.bind( (self.HOST, self.PORT) ) + self.sock.shutdown(socket.SHUT_RDWR) + def tearDown(self): self.sock.close() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-01-29 14:24:38
|
Revision: 5999 http://jython.svn.sourceforge.net/jython/?rev=5999&view=rev Author: zyasoft Date: 2009-01-29 14:24:33 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Extract script now processes most/all of the test.test_* modules with a test_main and importable by CPython. May want to consider generalizing support for instance methods and closure setup with respect to new.function (= PyFunction), but this seems purely a testing nicety for top-level defs. Modified Paths: -------------- branches/pbcvm/Tools/pbcvm/extract.py Modified: branches/pbcvm/Tools/pbcvm/extract.py =================================================================== --- branches/pbcvm/Tools/pbcvm/extract.py 2009-01-29 03:44:58 UTC (rev 5998) +++ branches/pbcvm/Tools/pbcvm/extract.py 2009-01-29 14:24:33 UTC (rev 5999) @@ -4,6 +4,7 @@ Jython the desired PBC. """ +from __future__ import with_statement import inspect import networkx # add dependency to a setup script? probably overkill import sys @@ -13,97 +14,128 @@ argcount nlocals stacksize flags code consts names varnames filename name firstlineno lnotab -""".split()] # add freevars cellvars? + freevars cellvars +""".split()] # add -# this has to be instantiated per module! so simply move into a class -_codeobjs = {} -_codeobjs_names = {} -counters = defaultdict(int) -depend = networkx.DiGraph() -root = "root" +ROOT = object() -def extract(mod): - functionobjs = candidate_functions(mod) - for name, f in functionobjs: - #print >> sys.stderr, "Extracting", f - extract_code_obj(f) - codes = {} - for code, definition in _codeobjs.iteritems(): - codes[_codeobjs_names[code]] = definition - print "from %s import *" % mod.__name__ - print "from org.python.core import PyBytecode" - print - print "_codeobjs = {}" - print +class Extract(object): - objs = networkx.topological_sort(depend) - for obj in objs: - if not inspect.iscode(obj): - continue - name = _codeobjs_names[obj] - print "_codeobjs[%r] = %s" % (name, _codeobjs[obj]) - print - for name, f in functionobjs: - print "%s.func_code = _codeobjs[%r]" % (name, _codeobjs_names[f.func_code]) + def __init__(self, mod, writer): + self.mod = mod + self.writer = writer + self.codeobjs = {} + self.codeobjs_names = {} + self.counters = defaultdict(int) + self.depend = networkx.DiGraph() + + def extract(self): + mod = self.mod + writer = self.writer + functionobjs = self.candidate_functions() + for name, f in functionobjs: + self.extract_code_obj(f) + + print >> writer, "from %s import *" % mod.__name__ + print >> writer, "from org.python.core import PyBytecode" + print >> writer + print >> writer, "_codeobjs = {}" + print >> writer + + objs = networkx.topological_sort(self.depend) + for obj in objs: + if not inspect.iscode(obj): + continue + name = self.codeobjs_names[obj] + print >> writer, "_codeobjs[%r] = %s" % (name, self.codeobjs[obj]) + print >> writer + for name, f in functionobjs: + # this may be a Jython diff, need to determine further; need to check if im_func or not on the object + print >> writer, "try: %s.func_code = _codeobjs[%r]" % (name, self.codeobjs_names[f.func_code]) + print >> writer, "except (AttributeError, ValueError): pass" # ignore setting cells, im_func, etc... %s.im_func.func_code = _codeobjs[%r]" % (name, self.codeobjs_names[f.func_code]) - print - print 'if __name__ == "__main__":' - print ' test_main()' + print >> writer + print >> writer, 'if __name__ == "__main__":' + print >> writer, ' test_main()' + def candidate_functions(self): + """functions and methods we will retrieve code objects""" + + mod = self.mod + functions = inspect.getmembers(mod, inspect.isfunction) + functions = [(name, f) for (name, f) in functions if not inspect.isbuiltin(f) and name != "test_main"] -def candidate_functions(mod): - """functions and methods we will retrieve code objects""" + classes = inspect.getmembers(mod, inspect.isclass) + for classname, cls in classes: + for methodname, method in inspect.getmembers(cls, inspect.ismethod): + if inspect.getmodule(method) == mod: + functions.append(("%s.%s" % (classname, methodname), method)) + return functions - functions = inspect.getmembers(mod, inspect.isfunction) - functions = [(name, f) for (name, f) in functions if not inspect.isbuiltin(f)] + def extract_code_obj(self, f_or_code): + if inspect.iscode(f_or_code): + code = f_or_code + else: + code = f_or_code.func_code + self.extract_def(code) - classes = inspect.getmembers(mod, inspect.isclass) - for classname, cls in classes: - #print >> sys.stderr, "Extracting from", cls - for methodname, method in inspect.getmembers(cls, inspect.ismethod): - #print >> sys.stderr, "Extracting method", method - if inspect.getmodule(method) == mod: - functions.append(("%s.%s" % (classname, methodname), method)) - return functions + def extract_def(self, code): + if code in self.codeobjs_names: + #print >> sys.stderr, "Already seen", code + return "_codeobjs[%r]" % (self.codeobjs_names[code],) -def extract_code_obj(f_or_code): - if inspect.iscode(f_or_code): - code = f_or_code - else: - code = f_or_code.func_code - extract_def(code) + co_name = code.co_name + #print >> sys.stderr, "Processing", code + name = co_name + "." + str(self.counters[co_name]) + self.counters[co_name] += 1 + self.codeobjs_names[code] = name + values = [] + self.depend.add_edge(code, ROOT) + for attr in attrs: + # treat co_consts specially - maybe also need to use pickling in case repr is not suitable + if attr == 'co_consts': + co_consts = [] + for const in getattr(code, attr): + if inspect.iscode(const): + #print >> sys.stderr, "Extracting code const " + str(const) + co_consts.append(self.extract_def(const)) + self.depend.add_edge(const, code) + else: + co_consts.append(repr(const)) + values.append((attr, "["+', '.join(co_consts)+"]")) + else: + values.append((attr, repr(getattr(code, attr)))) + self.codeobjs[code] = "PyBytecode(\n" + '\n'.join([' '* 4 + v + ', # ' + attr for (attr, v) in values])+"\n )" + return "_codeobjs[%r]" % (name,) -def extract_def(code): - if code in _codeobjs_names: - print >> sys.stderr, "Already seen", code - return "_codeobjs[%r]" % (_codeobjs_names[code],) - co_name = code.co_name - print >> sys.stderr, "Processing", code - name = co_name + "." + str(counters[co_name]) - counters[co_name] += 1 - _codeobjs_names[code] = name - # need to treat co_consts specially - maybe use pickling if repr is not suitable? - values = [] - depend.add_edge(code, root) - for attr in attrs: - if attr == 'co_consts': - co_consts = [] - for const in getattr(code, attr): - if inspect.iscode(const): - print >> sys.stderr, "Extracting code const " + str(const) - co_consts.append(extract_def(const)) - depend.add_edge(const, code) - else: - co_consts.append(repr(const)) - values.append((attr, "["+', '.join(co_consts)+"]")) - else: - values.append((attr, repr(getattr(code, attr)))) - _codeobjs[code] = "PyBytecode(\n" + '\n'.join([' '* 4 + v + ', # ' + attr for (attr, v) in values])+"\n )" - return "_codeobjs[%r]" % (name,) +# for now, just use the cwd +def import_modules(path): + import glob + import os.path + sys.path.insert(0, path) + for name in sorted(glob.iglob("test/test_*.py")): + modname = os.path.splitext(os.path.basename(name))[0] + qualified_name = "test." + modname + print "Trying", qualified_name + try: + topmod = __import__(qualified_name) + except Exception, e: + print "Could not import", qualified_name, ":", repr(e) + continue + mod = getattr(topmod, modname) + try: + mod.test_main + except AttributeError, e: + print "No test_main in", qualified_name + continue + output_name = "test/" + modname + "_pbc.py" + with open(output_name, "w") as f: + print "Extracting", name, "to", output_name + Extract(mod, f).extract() + if __name__ == '__main__': - modname = sys.argv[1] - mod = __import__(modname) - extract(mod) + path = modname = sys.argv[1] + import_modules(path) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-01-29 03:45:03
|
Revision: 5998 http://jython.svn.sourceforge.net/jython/?rev=5998&view=rev Author: zyasoft Date: 2009-01-29 03:44:58 +0000 (Thu, 29 Jan 2009) Log Message: ----------- extract.py now creates modules where codeobjects respect their dependency relationships (via a tsort). Modified Paths: -------------- branches/pbcvm/Tools/pbcvm/extract.py branches/pbcvm/src/org/python/core/PyBytecode.java Modified: branches/pbcvm/Tools/pbcvm/extract.py =================================================================== --- branches/pbcvm/Tools/pbcvm/extract.py 2009-01-29 03:23:40 UTC (rev 5997) +++ branches/pbcvm/Tools/pbcvm/extract.py 2009-01-29 03:44:58 UTC (rev 5998) @@ -1,11 +1,11 @@ -"""Given a module, codegens a new module where all functions imported -from it (using __all__ ?) are replaced with functions tied to -PyBytecode, including references to code objs in co_consts. Other -objects are simply imported from the original object. Hopefully this -provides an opportunity to test something two different ways, which -seems nice.""" +"""Given a module, generates a new module where all functions that are + not builtin (including within classes) have their func_code pointed + to a PyBytecode constructor. This enables CPython to generate for + Jython the desired PBC. +""" import inspect +import networkx # add dependency to a setup script? probably overkill import sys from collections import defaultdict @@ -19,10 +19,9 @@ _codeobjs = {} _codeobjs_names = {} counters = defaultdict(int) +depend = networkx.DiGraph() +root = "root" -# XXX - need to capture with a toposort the dependencies of -# recursive code objects and emit in that order - def extract(mod): functionobjs = candidate_functions(mod) for name, f in functionobjs: @@ -32,12 +31,17 @@ for code, definition in _codeobjs.iteritems(): codes[_codeobjs_names[code]] = definition print "from %s import *" % mod.__name__ - print "from org.python.core import PyBytecode, PyFunction" + print "from org.python.core import PyBytecode" print print "_codeobjs = {}" print - for name, obj in sorted(codes.iteritems()): - print "_codeobjs[%r] = %s" % (name, obj) + + objs = networkx.topological_sort(depend) + for obj in objs: + if not inspect.iscode(obj): + continue + name = _codeobjs_names[obj] + print "_codeobjs[%r] = %s" % (name, _codeobjs[obj]) print for name, f in functionobjs: print "%s.func_code = _codeobjs[%r]" % (name, _codeobjs_names[f.func_code]) @@ -81,6 +85,7 @@ _codeobjs_names[code] = name # need to treat co_consts specially - maybe use pickling if repr is not suitable? values = [] + depend.add_edge(code, root) for attr in attrs: if attr == 'co_consts': co_consts = [] @@ -88,6 +93,7 @@ if inspect.iscode(const): print >> sys.stderr, "Extracting code const " + str(const) co_consts.append(extract_def(const)) + depend.add_edge(const, code) else: co_consts.append(repr(const)) values.append((attr, "["+', '.join(co_consts)+"]")) Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-01-29 03:23:40 UTC (rev 5997) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-01-29 03:44:58 UTC (rev 5998) @@ -27,10 +27,10 @@ // end debugging public final static int CO_MAXBLOCKS = 20; // same as in CPython - public final char[] co_code; // to avoid sign issues + public final char[] co_code; // widened to char to avoid signed byte issues public final PyObject[] co_consts; public final String[] co_names; - public final int co_stacksize; // ignored, probably shouldn't be + public final int co_stacksize; // XXX - use to convert PyStack to use PyObject[] instead of ArrayList<PyObject> public final PyObject[] co_lnotab; // ignored private final static int CALL_FLAG_VAR = 1; private final static int CALL_FLAG_KW = 2; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-01-29 03:23:42
|
Revision: 5997 http://jython.svn.sourceforge.net/jython/?rev=5997&view=rev Author: fwierzbicki Date: 2009-01-29 03:23:40 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Added more constructors to BasePackageManager, cleaned up commented code in PyJavaPackage. Modified Paths: -------------- trunk/sandbox/wierzbicki/jlr_experiment/src/org/python/core/PyJavaPackage.java trunk/sandbox/wierzbicki/jlr_experiment/src/org/python/core/packagecache/BasePackageManager.java Modified: trunk/sandbox/wierzbicki/jlr_experiment/src/org/python/core/PyJavaPackage.java =================================================================== --- trunk/sandbox/wierzbicki/jlr_experiment/src/org/python/core/PyJavaPackage.java 2009-01-28 20:32:23 UTC (rev 5996) +++ trunk/sandbox/wierzbicki/jlr_experiment/src/org/python/core/PyJavaPackage.java 2009-01-29 03:23:40 UTC (rev 5997) @@ -176,9 +176,4 @@ public Object getClasses() { return clsSet; } public Object getMembers() { return __dict__; } public void setPackageManager (PackageManager mgr) { __mgr__ = mgr; } - //public Object addClass(String string, Class<?> clazz); - //public JavaPackage addPackage(String name); - //public JavaPackage addPackage(String name, String jarfile); - //public void addPlaceholders(String classes); - } Modified: trunk/sandbox/wierzbicki/jlr_experiment/src/org/python/core/packagecache/BasePackageManager.java =================================================================== --- trunk/sandbox/wierzbicki/jlr_experiment/src/org/python/core/packagecache/BasePackageManager.java 2009-01-28 20:32:23 UTC (rev 5996) +++ trunk/sandbox/wierzbicki/jlr_experiment/src/org/python/core/packagecache/BasePackageManager.java 2009-01-29 03:23:40 UTC (rev 5997) @@ -46,9 +46,31 @@ // for default cache (local fs based) impl protected File cachedir; + public BasePackageManager(JavaPackage top) { + this(top, null, true, null, null); + } + + public BasePackageManager(JavaPackage top, File cachedir) { + this(top, cachedir, true, null, null); + } + public BasePackageManager(JavaPackage top, File cachedir, + boolean respectJavaAccessibility) { + this(top, cachedir, respectJavaAccessibility, null, null); + } + + + public BasePackageManager(JavaPackage top, + File cachedir, boolean respectJavaAccessibility, + List<String> classpaths) { + this(top, cachedir, respectJavaAccessibility, classpaths, null); + } + + public BasePackageManager(JavaPackage top, + File cachedir, + boolean respectJavaAccessibility, List<String> classpaths, List<String> jarpaths) { @@ -66,7 +88,6 @@ } saveCache(); } - } public abstract Object makeJavaPackage(String name, String classes, String jarfile); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |