Exemple #1
0
 /**
  * Convenience method for constructing a type object of a Python exception, named as given, and
  * added to the namespace of the "_io" module.
  *
  * @param dict module dictionary
  * @param excname name of the exception
  * @param bases one or more bases (superclasses)
  * @return the constructed exception type
  */
 private static PyType makeException(PyObject dict, String excname, PyObject... bases) {
   PyStringMap classDict = new PyStringMap();
   classDict.__setitem__("__module__", Py.newString("_io"));
   PyType type = (PyType) Py.makeClass(excname, bases, classDict);
   dict.__setitem__(excname, type);
   return type;
 }
  public PyScriptEngine() {
    if (!initialized) {
      initializer();
      initialized = true;
    }

    // Set __name__ == "__main__" (useful for python scripting)
    // Without this, it is "__builtin__"
    PyStringMap dict = new PyStringMap();
    dict.__setitem__("__name__", new PyString("__main__"));

    // start with a fresh PySystemState
    PySystemState sys = new PySystemState();

    py = new PythonInterpreter(dict, sys);
  }
 public void evalFile(String s) {
   for (String s2 : bindings.keySet()) {
     py.set(s2, bindings.get(s2));
   }
   py.setOut(getWriter());
   py.setErr(getErrorWriter());
   try {
     py.execfile(s);
     PyStringMap locals = (PyStringMap) py.getLocals();
     Object[] values = locals.values().toArray();
     Object[] keys = locals.keys().toArray();
     bindings.clear();
     for (int i = 0; i < keys.length; ++i) {
       bindings.put((String) keys[i], values[i]);
     }
   } catch (PyException pe) {
     getErrorWriter().write(pe.toString());
     getErrorWriter().flush();
   }
 }
 protected void append_json_string_map_repr(StringBuffer buf, PyStringMap map)
     throws JSONEncodeError {
   append_json_map_repr(buf, map, map.keys());
 }