static FlinkException createAndLogException(PyException pe, Logger log) { StringWriter sw = new StringWriter(); try (PrintWriter pw = new PrintWriter(sw)) { pe.printStackTrace(pw); } String pythonStackTrace = sw.toString().trim(); log.error("Python function failed: " + System.lineSeparator() + pythonStackTrace); return new FlinkException("Python function failed: " + pythonStackTrace); } }
public void setError(Throwable e) { StringBuilder sb = new StringBuilder("<html> "); if (e instanceof NodeRenderException) { Node node = ((NodeRenderException) e).getNode(); sb.append("<b>"); sb.append(node.getName()); sb.append(":</b> "); } sb.append("<u>"); nodeRenderException = getRootCause(e); if (nodeRenderException instanceof PyException) { PyException ex = (PyException) nodeRenderException; if (ex.value != null) { sb.append(ex.value.toString()); } else { sb.append(ex.toString()); } } else if (nodeRenderException instanceof OutOfMemoryError) { sb.append("Out of memory. Are you trying to process an infinite list?"); } else { sb.append(nodeRenderException.getMessage()); } sb.append("</u></html>"); errorLabel.setText(sb.toString()); errorLabel.setVisible(true); }
/** * @deprecated As of Jython 2.5, use {@link PyException#match} instead. */ @Deprecated public static boolean matchException(PyException pye, PyObject exc) { return pye.match(exc); }
final private int marker() { for (int k = stackTop-1; k >= 0; k--) if (stack[k] == mark) return stackTop-k-1; throw new PyException(UnpicklingError, "Inputstream corrupt, marker not found"); }
static void maybeSystemExit(PyException exc) { if (exc.match(Py.SystemExit)) { PyObject value = exc.value; if (PyException.isExceptionInstance(exc.value)) { value = value.__findattr__("code"); } Py.getSystemState().callExitFunc(); if (value instanceof PyInteger) { System.exit(((PyInteger) value).getValue()); } else { if (value != Py.None) { try { Py.println(value); System.exit(1); } catch (Throwable t) { // continue } } System.exit(0); } } } public static PyObject StopIteration;
/** * Construct a PyBuffer on a given object suitable for reading into from the underlying stream. * The buffer returned will be navigable as a 1D contiguous writable sequence of bytes. * * @param obj to be wrapped and presented as a buffer * @return a 1D contiguous PyBuffer of bytes * @throws PyException (BufferError) if object has buffer API, but is not 1D contiguous bytes * @throws PyException (TypeError) if object not convertible to a byte array */ protected static PyBuffer writablePyBuffer(PyObject obj) throws PyException { if (obj instanceof BufferProtocol) { try { return ((BufferProtocol)obj).getBuffer(PyBUF.WRITABLE); } catch (PyException pye) { if (pye.match(Py.BufferError)) { // If we can't get a buffer on the object, say it's the wrong type throw Py.TypeError(String.format("(BufferError) %s", pye.getMessage())); } else { throw pye; } } } else { // Can't be a buffer: complain throw tailoredTypeError("read-write buffer", obj); } }
if (isExceptionClass(type)) { PyException pye = new PyException(type, value, (PyTraceback)traceback); pye.normalize(); if (!isExceptionInstance(pye.value)) { throw Py.TypeError(String.format( "calling %s() should have returned an instance of BaseException, not '%s'", } else if (isExceptionInstance(type)) { return new PyException(type, value, (PyTraceback)traceback);
PyObject value = stack.pop(); PyObject type = stack.pop(); throw PyException.doRaise(type, value, tb); throw PyException.doRaise(type, value, null); throw PyException.doRaise(type, null, null); throw PyException.doRaise(null, null, null); default: throw Py.SystemError("bad RAISE_VARARGS oparg"); if (a instanceof PyStackException) { PyException pye = ((PyStackException) a).exception; stack.push(Py.newBoolean(pye.match(b))); } else { stack.push(Py.newBoolean(new PyException(a).match(b))); if (pye.match(Py.AttributeError)) { throw Py.ImportError(String.format("cannot import name %.230s", name)); } else { if (!pye.match(Py.StopIteration)) { throw pye; PyException exc = ts.exception; if (b.b_type == Opcode.SETUP_EXCEPT) { exc.normalize();
if (isExceptionInstance(value)) { inClass = value.fastGetClass(); if (isExceptionClass(type)) { if (inClass == null || !Py.isSubClass(inClass, type)) { PyObject[] args;
public Object evaluate(String script) { try { interpreter.exec(script); } catch (org.python.core.PyException e) { throw new InterpreterException(e, e.getMessage(), -1, -1); } catch (RuntimeException re) { throw new InterpreterException(re, re.getMessage(), -1, -1); } return null; }
public static String formatException(PyObject type, PyObject value, boolean useRepr) { StringBuilder buf = new StringBuilder(); if (PyException.isExceptionClass(type)) { String className = PyException.exceptionClassName(type); int lastDot = className.lastIndexOf('.'); if (lastDot != -1) {
public static PyException makeException(PyObject type, PyObject value, PyObject traceback) { return PyException.doRaise(type, value, traceback); }
@Override public void __del_builtin__() { if (gi_frame == null || gi_frame.f_lasti == -1) { return; } try { close(); } catch (PyException pye) { // PEP 342 specifies that if an exception is raised by close, // we output to stderr and then forget about it; String className = PyException.exceptionClassName(pye.type); int lastDot = className.lastIndexOf('.'); if (lastDot != -1) { className = className.substring(lastDot + 1); } String msg = String.format("Exception %s: %s in %s", className, pye.value.__repr__(), __repr__()); Py.println(Py.getSystemState().stderr, Py.newString(msg)); } catch (Throwable t) { // but we currently ignore any Java exception completely. perhaps we // can also output something meaningful too? } }
@Override public ScriptExecutionResult execute(GameScript<PyCode> script, ScriptBindings bindings, boolean returnResult) throws Exception { PyCode pythonScript = script.getScript(); for (String variableName : bindings.keySet()) { pythonInterpreter.set(variableName, bindings.get(variableName)); } try { pythonInterpreter.exec(pythonScript); } catch (PyException e) { if(e.getCause() instanceof ScriptSkippedException) { throw new ScriptSkippedException(); } else { throw e; } } if(!returnResult) { return null; } //TODO: Find way to extract all variables ScriptExecutionResult executionResult = new ScriptExecutionResult(null); for(String variableName : bindings.keySet()) { executionResult.put(variableName, pythonInterpreter.get(variableName, Object.class)); } return executionResult; }
/** * A function that returns a {@link PyException}, which is a Java exception suitable for * throwing, and that will be raised as an <code>UnsupportedOperation</code> Python exception. * * @param message text message parameter to the Python exception * @return nascent <code>UnsupportedOperation</code> Python exception */ public static PyException UnsupportedOperation(String message) { return new PyException(UnsupportedOperation, message); }
if (e.match(Py.SystemExit)) { PyObject value = e.value; if (PyException.isExceptionInstance(e.value)) { value = value.__findattr__("code");
return ((BufferProtocol)obj).getBuffer(PyBUF.SIMPLE); } catch (PyException pye) { if (pye.match(Py.BufferError)) { throw Py.TypeError(String.format("(BufferError) %s", pye.getMessage())); } else { throw pye;
/** * Exit a Python program with the given status. * * @param status the value to exit with * @exception Py.SystemExit always throws this exception. When caught at top level the program * will exit. */ public static void exit(PyObject status) { throw new PyException(Py.SystemExit, status); }