Commits

Philip Jenvey committed 3dbcf5f

add more missing dict-like methods to the jsr223 scope. fixes the warning
module under jsr223
fixes #1698

  • Participants
  • Parent commits 4ae43e0

Comments (0)

Files changed (3)

 Jython 2.5.2
   Bugs Fixed
     - [ 1667 ] thread.local subclasses with constructor params fail
+    - [ 1698 ] warnings module fails under JSR-223
 
 Jython 2.5.2rc3
   Bugs Fixed

File src/org/python/jsr223/PyScriptEngineScope.java

         return new ScopeIterator(this);
     }
 
+    @ExposedMethod(defaults = "Py.None")
+    final PyObject scope_get(PyObject keyObj, PyObject defaultObj) {
+        String key = keyObj.asString();
+        int scope = context.getAttributesScope(key);
+        return scope == -1 ? defaultObj : Py.java2py(context.getAttribute(key, scope));
+    }
+
+    @ExposedMethod
+    final boolean scope_has_key(PyObject key) {
+        return context.getAttributesScope(key.asString()) != -1;
+    }
+
+    @Override
+    public boolean __contains__(PyObject obj) {
+        return scope___contains__(obj);
+    }
+
+    @ExposedMethod
+    final boolean scope___contains__(PyObject obj) {
+        return scope_has_key(obj);
+    }
+
+    @ExposedMethod(defaults = "Py.None")
+    final PyObject scope_setdefault(PyObject keyObj, PyObject failObj) {
+        PyObject result;
+        String key = keyObj.asString();
+        int scope = context.getAttributesScope(key);
+        if (scope == -1) {
+            scope = ScriptContext.ENGINE_SCOPE;
+            context.setAttribute(key,
+                                 failObj instanceof PyType
+                                 ? failObj : failObj.__tojava__(Object.class),
+                                 scope);
+            result = failObj;
+        } else {
+            result = Py.java2py(context.getAttribute(key, scope));
+        }
+        return result;
+    }
+
     @Override
     public String toString() {
         return getDictionary().toString();

File tests/java/org/python/jsr223/ScriptEngineTest.java

         assertEquals("a string", pythonEngine.get("result"));
         assertEquals("a string", pythonEngine.get("result2"));
     }
+
+    public void testIssue1698() throws ScriptException{
+        ScriptEngineManager manager = new ScriptEngineManager();
+        ScriptEngine pythonEngine = manager.getEngineByName("python");
+        pythonEngine.eval("import warnings");
+        // Would previously fail
+        pythonEngine.eval("warnings.warn('test')");
+    }
     
 }