public boolean before(Authentication authentication, MethodInvocation mi, PreInvocationAttribute preAttr) { PythonInterpreterPreInvocationAttribute pythonAttr = (PythonInterpreterPreInvocationAttribute) preAttr; String script = pythonAttr.getScript(); PythonInterpreter python = new PythonInterpreter(); python.set("authentication", authentication); python.set("args", createArgumentMap(mi)); python.set("method", mi.getMethod().getName()); Resource scriptResource = new PathMatchingResourcePatternResolver() .getResource(script); try { python.execfile(scriptResource.getInputStream()); } catch (IOException e) { throw new IllegalArgumentException("Couldn't run python script, " + script, e); } PyObject allowed = python.get("allow"); if (allowed == null) { throw new IllegalStateException("Python script did not set the permit flag"); } return (Boolean) Py.tojava(allowed, Boolean.class); }
/** * Return the RawIOBase associated with the specified file descriptor. * * Raises a Python exception is the file descriptor is invalid * * @param fd * a Jython file descriptor object * @return the RawIOBase associated with the file descriptor */ public static RawIOBase get(PyObject fd) { return Py.tojava(fd, RawIOBase.class); } }
protected Object get(String name) { //Bug 2208873: Don't use _ip.get(String, Object) since it //doesn't handle null well PyObject val = _ip.get(name); return val != null ? Py.tojava(val, Object.class) : null; }
protected Object get(String name) { //Bug 2208873: Don't use _ip.get(String, Object) since it //doesn't handle null well PyObject val = _ip.get(name); return val != null ? Py.tojava(val, Object.class): null; }
@Override public PyObject __call__(PyObject key) { return asMap().containsKey(Py.tojava(key, Object.class)) ? Py.True : Py.False; } };
@Override public PyObject __call__(PyObject key, PyObject _default) { Object jkey = Py.tojava(key, Object.class); Object jval = asMap().get(jkey); if (jval == null) { asMap().put(jkey, _default == Py.None ? Py.None : Py.tojava(_default, Object.class)); return _default; } return Py.java2py(jval); } };
@Override public PyObject __call__(PyObject name) { try { return objectGetattribute.__call__(self, name); } catch (PyException pye) { if (!pye.match(Py.AttributeError)) { throw pye; } } CustomizableMapHolder inst = Py.tojava(self, CustomizableMapHolder.class); return Py.java2py(inst.held.get(Py.tojava(name, String.class))); } };
@Override public PyObject __call__(PyObject key) { Object jkey = Py.tojava(key, Object.class); if (asMap().remove(jkey) == null) { throw Py.KeyError(key); } return Py.None; } };
private boolean getSensitive(Map<String, Serializable> executionResultContext, boolean systemPropertiesInContext) { if (systemPropertiesInContext) { Map<String, Serializable> context = new HashMap<>(executionResultContext); PyObject rawSystemProperties = (PyObject) context.remove(SYSTEM_PROPERTIES_MAP); @SuppressWarnings("unchecked") Map<String, Value> systemProperties = Py.tojava(rawSystemProperties, Map.class); @SuppressWarnings("unchecked") Collection<Serializable> systemPropertyValues = (Collection) systemProperties.values(); return checkSensitivity(systemPropertyValues) || checkSensitivity(context.values()); } else { return (checkSensitivity(executionResultContext.values())); } }
@Override public PyObject __call__(PyObject key, PyObject _default) { Object jkey = Py.tojava(key, Object.class); if (asMap().containsKey(jkey)) { return Py.java2py(asMap().get(jkey)); } else { return _default; } } };
public static Object tojava(PyObject o, String s) { Class<?> c = findClass(s); if (c == null) { throw Py.TypeError("can't convert to: " + s); } return tojava(o, c); // prev:Class.forName } /* Helper functions for PyProxy's */
@Override public void run() { PythonInterpreter interp = new PythonInterpreter(); interp.exec("import sys"); interp.set("a", new PyInteger(41)); int set = Py.tojava(interp.get("a"), Integer.class); assertEquals(41, set); interp.exec("x = 'hello ' + 'goodbye'"); assertEquals("hello goodbye", Py.tojava(interp.get("x"), String.class)); doneSignal.countDown(); } }.start();
@Override public PyObject __call__(PyObject key) { Object jkey = Py.tojava(key, Object.class); if (asMap().containsKey(jkey)) { return Py.java2py(asMap().get(jkey)); } else { throw Py.KeyError(key); } } };
@Override public boolean _doset(PyObject self, PyObject value) { Object jself = Py.tojava(self, addMethod.getDeclaringClass()); if (!(value instanceof PyCompoundCallable)) { PyCompoundCallable func = new PyCompoundCallable(); setFunction(jself, func); func.append(value); } else { setFunction(jself, value); } return true; }
/** * Show that a PythonInterpreter comes by default with a PlainConsole (not JLine say). */ public void testConsoleIsPlain() throws Exception { PythonInterpreter interp = new PythonInterpreter(); interp.exec("import sys"); Console console = Py.tojava(interp.eval("sys._jy_console"), Console.class); assertEquals(PlainConsole.class, console.getClass()); Console console2 = Py.getConsole(); assertEquals(PlainConsole.class, console2.getClass()); }