public String asStringOrNull(int index) throws ConversionException { return asString(index); }
public String asStringOrNull(){ return asString(); }
@Override public PyObject __finditem__(PyObject key) { return __finditem__(key.asString()); }
/** * Append to a formatting result, the presentation of one object, according to a given format * specification and the object's <code>__format__</code> method. * * @param fieldObj to format. * @param formatSpec specification to apply. * @param result to which the result will be appended. */ private void renderField(PyObject fieldObj, String formatSpec, StringBuilder result) { PyString formatSpecStr = formatSpec == null ? Py.EmptyString : new PyString(formatSpec); result.append(fieldObj.__format__(formatSpecStr).asString()); }
@ExposedMethod @Override public void __setitem__(PyObject key, PyObject value) { __setitem__(key.asString(), value); }
@ExposedMethod @Override public void __delitem__(PyObject key) { __delitem__(key.asString()); }
@ExposedMethod final boolean scope_has_key(PyObject key) { return context.getAttributesScope(key.asString()) != -1; }
public String getStyleDefs(String selector) { return formatter.invoke("get_style_defs", new PyString(selector)).asString(); }
public static String getPyObjectValueAsString(PyObject obj) { if (obj == null) return ""; PyType type = obj.getType(); if (type.getName().equals("long")) return Long.toString(obj.asLong()); else if (type.getName().equals("int")) return Integer.toString(obj.asInt()); else if (type.getName().equals("bool")) return obj.asInt() != 0 ? "true" : "false"; return obj.asString(); }
private static String ensureStringAttribute(PyObject name) { String nameStr; if (name instanceof PyUnicode) { nameStr = ((PyUnicode)name).encode(); } else if (name instanceof PyString) { nameStr = name.asString(); } else { throw Py.TypeError(String.format("attribute name must be string, not '%.200s'", name.getType().fastGetName())); } return nameStr; } }
@ExposedMethod public final PyObject find_function(PyObject name) { return new TextSymbol(this, name.asString(), findSymbol(name)); }
@ExposedMethod public final PyObject find_variable(PyObject name) { return new DataSymbol(this, name.asString(), findSymbol(name)); }
private Map<String, Serializable> getPythonLocals() { Map<String, Serializable> result = new HashMap<>(); if(interpreter.getLocals() != null) { for (PyObject pyObject : interpreter.getLocals().asIterable()) { String key = pyObject.asString(); PyObject value = interpreter.get(key); if (keyIsExcluded(key, value)) { continue; } result.put(key, value); } } return result; }
public static boolean getPyObjectValueAsBoolean(PyObject obj) { if (obj == null) return false; PyType type = obj.getType(); if (type.getName().equals("long")) return (obj.asLong() != 0); else if (type.getName().equals("int")) return (obj.asInt() != 0); else if (type.getName().equals("bool")) return (obj.asInt() != 0); else if (type.getName().equals("NoneType")) return false; return obj.asString().length() != 0; }
@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)); }
@ExposedSet(name = "value") public void setValue(PyObject value) { byte[] str = value.asString().getBytes(); DirectMemory m = AllocatedNativeMemory.allocate(str.length + 1, false); m.putZeroTerminatedByteArray(0, str, 0, str.length); this.memory = m; if (hasReferenceMemory()) { getReferenceMemory().putAddress(0, m); } }
@ExposedNew public static PyObject StringCData_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { // No args == create NULL pointer if (args.length == 0) { return new StringCData(subtype, CType.typeOf(subtype), NullMemory.INSTANCE); } byte[] str = args[0].asString().getBytes(); DirectMemory m = AllocatedNativeMemory.allocate(str.length + 1, false); m.putZeroTerminatedByteArray(0, str, 0, str.length); return new StringCData(subtype, CType.typeOf(subtype), m); }
static void raise_errmsg(String msg, PyObject s, PyObject pos, PyObject end) { /* Use the Python function json.decoder.errmsg to raise a nice looking ValueError exception */ final PyObject errmsg_fn = get_errmsg_fn(); if (errmsg_fn != null) { throw Py.ValueError(errmsg_fn.__call__(Py.newString(msg), s, pos, end).asString()); } else { throw Py.ValueError(msg); } }
@ExposedMethod public final PyObject find_symbol(PyObject name) { long address = findSymbol(name); return new Symbol(this, name.asString(), new NativeMemory(address)); }