From: <otm...@us...> - 2010-11-06 00:15:39
|
Revision: 7169 http://jython.svn.sourceforge.net/jython/?rev=7169&view=rev Author: otmarhumbel Date: 2010-11-06 00:15:33 +0000 (Sat, 06 Nov 2010) Log Message: ----------- let the engine scope behave more like locals and globals; fixes issue #1674 Modified Paths: -------------- trunk/jython/NEWS trunk/jython/src/org/python/jsr223/PyScriptEngineScope.java trunk/jython/tests/java/org/python/jsr223/ScriptEngineTest.java Modified: trunk/jython/NEWS =================================================================== --- trunk/jython/NEWS 2010-10-24 21:44:54 UTC (rev 7168) +++ trunk/jython/NEWS 2010-11-06 00:15:33 UTC (rev 7169) @@ -1,5 +1,9 @@ Jython NEWS +Jython 2.5.2rc3 + Bugs Fixed + - [ 1674 ] PDB crashes under the JSR-223 scripting engine + Jython 2.5.2rc2 Bugs Fixed - [ 1665 ] cPickle calls __import__ with illegal parameters Modified: trunk/jython/src/org/python/jsr223/PyScriptEngineScope.java =================================================================== --- trunk/jython/src/org/python/jsr223/PyScriptEngineScope.java 2010-10-24 21:44:54 UTC (rev 7168) +++ trunk/jython/src/org/python/jsr223/PyScriptEngineScope.java 2010-11-06 00:15:33 UTC (rev 7169) @@ -1,10 +1,15 @@ package org.python.jsr223; +import java.util.HashMap; import java.util.List; +import java.util.Map; + import javax.script.Bindings; import javax.script.ScriptContext; import javax.script.ScriptEngine; import org.python.core.Py; +import org.python.core.PyDictionary; +import org.python.core.PyIterator; import org.python.core.PyList; import org.python.core.PyObject; import org.python.core.PyString; @@ -56,16 +61,31 @@ return members; } - // Not necessary for functionality; present to satisfy __builtin__.PyMapping_check + // satisfy mapping and lookup @ExposedMethod + @Override public PyObject __getitem__(PyObject key) { - return super.__getitem__(key); + return __finditem__(key); } + // satisfy iterable + @ExposedMethod + @Override + public PyObject __iter__() { + return new ScopeIterator(this); + } + + @Override + public String toString() { + return getDictionary().toString(); + } + + @Override public PyObject __finditem__(PyObject key) { return __finditem__(key.asString()); } + @Override public PyObject __finditem__(String key) { int scope = context.getAttributesScope(key); if (scope == -1) @@ -74,10 +94,12 @@ } @ExposedMethod + @Override public void __setitem__(PyObject key, PyObject value) { __setitem__(key.asString(), value); } + @Override public void __setitem__(String key, PyObject value) { int scope = context.getAttributesScope(key); if (scope == -1) @@ -86,14 +108,57 @@ } @ExposedMethod + @Override public void __delitem__(PyObject key) { __delitem__(key.asString()); } + @Override public void __delitem__(String key) { int scope = context.getAttributesScope(key); if (scope == -1) throw Py.KeyError(key); context.removeAttribute(key, scope); } + + private Map<PyObject, PyObject> getMap() { + ScopeIterator iterator = new ScopeIterator(this); + Map<PyObject, PyObject> map = new HashMap<PyObject, PyObject>(iterator.size()); + PyObject key = iterator.__iternext__(); + while (key != null) { + map.put(key, __finditem__(key)); + key = iterator.__iternext__(); + } + return map; + } + + private PyDictionary getDictionary() { + return new PyDictionary(getMap()); + } + + public class ScopeIterator extends PyIterator { + private int _index; + private int _size; + private PyObject _keys; + + ScopeIterator(PyScriptEngineScope scope) { + _keys = scope.scope_keys(); + _size = _keys.__len__(); + _index = -1; + } + + public int size() { + return _size; + } + + @Override + public PyObject __iternext__() { + PyObject result = null; + _index++; + if (_index < size()) { + result = _keys.__getitem__(_index); + } + return result; + } + } } Modified: trunk/jython/tests/java/org/python/jsr223/ScriptEngineTest.java =================================================================== --- trunk/jython/tests/java/org/python/jsr223/ScriptEngineTest.java 2010-10-24 21:44:54 UTC (rev 7168) +++ trunk/jython/tests/java/org/python/jsr223/ScriptEngineTest.java 2010-11-06 00:15:33 UTC (rev 7169) @@ -206,4 +206,48 @@ Object newStringCapitalize = invocableEngine.invokeMethod("test", "capitalize"); assertEquals(newStringCapitalize, "Test"); } + + public void testPdb() { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + // String from issue 1674 + String pdbString = "from pdb import set_trace; set_trace()"; + try { + pythonEngine.eval(pdbString); + fail("bdb.BdbQuit expected"); + } catch (ScriptException e) { + assertTrue(e.getMessage().startsWith("bdb.BdbQuit")); + } + } + + public void testScope_repr() throws ScriptException { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + pythonEngine.eval("a = 4"); + pythonEngine.eval("b = 'hi'"); + pythonEngine.eval("localrepr = `locals()`"); + assertEquals("{'b': u'hi', 'a': 4}", pythonEngine.get("localrepr")); + } + + public void testScope_iter() throws ScriptException { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + pythonEngine.eval("a = 4"); + pythonEngine.eval("b = 'hi'"); + pythonEngine.eval("list = []"); + pythonEngine.eval("for loc in locals(): list.append(loc)"); + pythonEngine.eval("listrepr = `list`"); + assertEquals("[u'a', u'b', u'list']", pythonEngine.get("listrepr")); + } + + public void testScope_lookup() throws ScriptException{ + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + pythonEngine.eval("a = 4"); + pythonEngine.eval("b = 'hi'"); + pythonEngine.eval("var_a = locals()['a']"); + pythonEngine.eval("arepr = `var_a`"); + assertEquals("4", pythonEngine.get("arepr")); + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |