예제 #1
0
 public static PyObject SyntaxError() {
   PyObject __dict__ = new PyStringMap();
   defineSlots(__dict__, "msg", "filename", "lineno", "offset", "text", "print_file_and_line");
   __dict__.__setitem__("__init__", bindStaticJavaMethod("__init__", "SyntaxError__init__"));
   __dict__.__setitem__("__str__", bindStaticJavaMethod("__str__", "SyntaxError__str__"));
   return __dict__;
 }
예제 #2
0
 private static PyObject buildClass(
     PyObject dict, String classname, String superclass, PyObject classDict, String doc) {
   classDict.__setitem__("__doc__", Py.newString(doc));
   PyType type =
       (PyType) Py.makeClass("exceptions." + classname, dict.__finditem__(superclass), classDict);
   type.builtin = true;
   dict.__setitem__(classname, type);
   return type;
 }
예제 #3
0
 public static PyObject EnvironmentError() {
   PyObject dict = new PyStringMap();
   defineSlots(dict, "errno", "strerror", "filename");
   dict.__setitem__("__init__", bindStaticJavaMethod("__init__", "EnvironmentError__init__"));
   dict.__setitem__("__str__", bindStaticJavaMethod("__str__", "EnvironmentError__str__"));
   dict.__setitem__(
       "__reduce__", bindStaticJavaMethod("__reduce__", "EnvironmentError__reduce__"));
   return dict;
 }
예제 #4
0
 /**
  * Define __slots__ in dict with the specified slot names
  *
  * @param dict a PyObject dict
  * @param slotNames slot String names
  */
 private static void defineSlots(PyObject dict, String... slotNames) {
   PyObject[] slots = new PyObject[slotNames.length];
   for (int i = 0; i < slotNames.length; i++) {
     slots[i] = Py.newString(slotNames[i]);
   }
   dict.__setitem__("__slots__", new PyTuple(slots));
 }
예제 #5
0
 /** <i>Internal use only. Do not call this method explicit.</i> */
 public static void classDictInit(PyObject dict) {
   dict.__setitem__("__nonzero__", new SeqFuncs("__nonzero__", 1, 0));
   dict.__setitem__("__getitem__", new SeqFuncs("__getitem__", 11, 1));
   dict.__setitem__("__delitem__", new SeqFuncs("__delitem__", 12, 1));
   dict.__setitem__("__mul__", new SeqFuncs("__mul__", 13, 1));
   dict.__setitem__("__rmul__", new SeqFuncs("__rmul__", 14, 1));
   dict.__setitem__("__cmp__", new SeqFuncs("__cmp__", 15, 1));
   dict.__setitem__("__setitem__", new SeqFuncs("__setitem__", 21, 2));
   dict.__setitem__("__getslice__", new SeqFuncs("__getslice__", 31, 3));
   dict.__setitem__("__delslice__", new SeqFuncs("__delslice__", 32, 3));
   dict.__setitem__("__setslice__", new SeqFuncs("__setslice__", 41, 4));
   // TBD: __tojava__()
   // hide these from Python!
   dict.__setitem__("classDictInit", null);
 }
예제 #6
0
 public static PyObject UnicodeTranslateError() {
   PyObject dict = new PyStringMap();
   dict.__setitem__("__init__", bindStaticJavaMethod("__init__", "UnicodeTranslateError__init__"));
   dict.__setitem__("__str__", bindStaticJavaMethod("__str__", "UnicodeTranslateError__str__"));
   return dict;
 }
예제 #7
0
 public static PyObject KeyError() {
   PyObject dict = new PyStringMap();
   dict.__setitem__("__str__", bindStaticJavaMethod("__str__", "KeyError__str__"));
   return dict;
 }
예제 #8
0
 public static PyObject SystemExit() {
   PyObject dict = new PyStringMap();
   defineSlots(dict, "code");
   dict.__setitem__("__init__", bindStaticJavaMethod("__init__", "SystemExit__init__"));
   return dict;
 }
예제 #9
0
  /** <i>Internal use only. Do not call this method explicit.</i> */
  public static void classDictInit(PyObject dict) {
    dict.invoke("clear");
    dict.__setitem__("__name__", new PyString("exceptions"));
    dict.__setitem__("__doc__", new PyString(__doc__));

    ThreadState ts = Py.getThreadState();
    if (ts.getSystemState() == null) { // TODO: is this check still useful??
      ts.setSystemState(Py.defaultSystemState);
    }
    // Push frame
    PyFrame frame = new PyFrame(null, new PyStringMap());
    frame.f_back = ts.frame;
    if (frame.f_builtins == null) {
      if (frame.f_back != null) {
        frame.f_builtins = frame.f_back.f_builtins;
      } else {
        frame.f_builtins = PySystemState.getDefaultBuiltins();
      }
    }
    ts.frame = frame;

    dict.__setitem__("BaseException", PyBaseException.TYPE);

    buildClass(dict, "KeyboardInterrupt", "BaseException", "Program interrupted by user.");

    buildClass(
        dict, "SystemExit", "BaseException", SystemExit(), "Request to exit from the interpreter.");

    buildClass(
        dict, "Exception", "BaseException", "Common base class for all non-exit exceptions.");

    buildClass(
        dict,
        "StandardError",
        "Exception",
        "Base class for all standard Python exceptions that do not represent\n"
            + "interpreter exiting.");

    buildClass(dict, "SyntaxError", "StandardError", SyntaxError(), "Invalid syntax.");

    buildClass(dict, "IndentationError", "SyntaxError", "Improper indentation.");

    buildClass(dict, "TabError", "IndentationError", "Improper mixture of spaces and tabs.");

    buildClass(
        dict,
        "EnvironmentError",
        "StandardError",
        EnvironmentError(),
        "Base class for I/O related errors.");

    buildClass(dict, "IOError", "EnvironmentError", "I/O operation failed.");

    buildClass(dict, "OSError", "EnvironmentError", "OS system call failed.");

    buildClass(dict, "RuntimeError", "StandardError", "Unspecified run-time error.");

    buildClass(
        dict,
        "NotImplementedError",
        "RuntimeError",
        "Method or function hasn't been implemented yet.");

    buildClass(
        dict,
        "SystemError",
        "StandardError",
        "Internal error in the Python interpreter.\n\n"
            + "Please report this to the Python maintainer, "
            + "along with the traceback,\n"
            + "the Python version, and the hardware/OS "
            + "platform and version.");

    buildClass(
        dict, "ReferenceError", "StandardError", "Weak ref proxy used after referent went away.");

    buildClass(dict, "EOFError", "StandardError", "Read beyond end of file.");

    buildClass(
        dict,
        "ImportError",
        "StandardError",
        "Import can't find module, or can't find name in module.");

    buildClass(dict, "TypeError", "StandardError", "Inappropriate argument type.");

    buildClass(
        dict, "ValueError", "StandardError", "Inappropriate argument value (of correct type).");

    buildClass(dict, "UnicodeError", "ValueError", "Unicode related error.");

    buildClass(
        dict,
        "UnicodeEncodeError",
        "UnicodeError",
        UnicodeEncodeError(),
        "Unicode encoding error.");

    buildClass(
        dict,
        "UnicodeDecodeError",
        "UnicodeError",
        UnicodeDecodeError(),
        "Unicode decoding error.");

    buildClass(
        dict,
        "UnicodeTranslateError",
        "UnicodeError",
        UnicodeTranslateError(),
        "Unicode translation error.");

    buildClass(dict, "AssertionError", "StandardError", "Assertion failed.");

    buildClass(dict, "ArithmeticError", "StandardError", "Base class for arithmetic errors.");

    buildClass(dict, "OverflowError", "ArithmeticError", "Result too large to be represented.");

    buildClass(dict, "FloatingPointError", "ArithmeticError", "Floating point operation failed.");

    buildClass(
        dict,
        "ZeroDivisionError",
        "ArithmeticError",
        "Second argument to a division or modulo operation " + "was zero.");

    buildClass(dict, "LookupError", "StandardError", "Base class for lookup errors.");

    buildClass(dict, "IndexError", "LookupError", "Sequence index out of range.");

    buildClass(dict, "KeyError", "LookupError", KeyError(), "Mapping key not found.");

    buildClass(dict, "AttributeError", "StandardError", "Attribute not found.");

    buildClass(dict, "NameError", "StandardError", "Name not found globally.");

    buildClass(
        dict, "UnboundLocalError", "NameError", "Local name referenced but not bound to a value.");

    buildClass(dict, "MemoryError", "StandardError", "Out of memory.");

    buildClass(dict, "BufferError", "StandardError", "Buffer error.");

    buildClass(dict, "StopIteration", "Exception", "Signal the end from iterator.next().");

    buildClass(dict, "GeneratorExit", "BaseException", "Request that a generator exit.");

    buildClass(dict, "Warning", "Exception", "Base class for warning categories.");

    buildClass(dict, "UserWarning", "Warning", "Base class for warnings generated by user code.");

    buildClass(
        dict,
        "DeprecationWarning",
        "Warning",
        "Base class for warnings about deprecated features.");

    buildClass(
        dict,
        "PendingDeprecationWarning",
        "Warning",
        "Base class for warnings about features which will be deprecated\n" + "in the future.");

    buildClass(dict, "SyntaxWarning", "Warning", "Base class for warnings about dubious syntax.");

    buildClass(
        dict,
        "RuntimeWarning",
        "Warning",
        "Base class for warnings about dubious runtime behavior.");

    buildClass(
        dict,
        "FutureWarning",
        "Warning",
        "Base class for warnings about constructs that will change semantically\n"
            + "in the future.");

    buildClass(
        dict,
        "ImportWarning",
        "Warning",
        "Base class for warnings about probable mistakes in module imports");

    buildClass(
        dict,
        "UnicodeWarning",
        "Warning",
        "Base class for warnings about Unicode related problems, mostly\n"
            + "related to conversion problems.");

    buildClass(
        dict,
        "BytesWarning",
        "Warning",
        "Base class for warnings about bytes and buffer related problems, mostly\n"
            + "related to conversion from str or comparing to str.");

    // Initialize ZipImportError here, where it's safe to; it's
    // needed immediately
    zipimport.initClassExceptions(dict);

    ts.frame = ts.frame.f_back;
  }
 /**
  * Set a variable in the local namespace
  *
  * @param name the name of the variable
  * @param value the value to set the variable to
  */
 public void set(String name, PyObject value) {
   locals.__setitem__(name.intern(), value);
 }
 /**
  * Set a variable in the local namespace
  *
  * @param name the name of the variable
  * @param value the value to set the variable to. Will be automatically converted to an
  *     appropriate Python object.
  */
 public void set(String name, Object value) {
   locals.__setitem__(name.intern(), Py.java2py(value));
 }
예제 #12
0
  public static void typeSetup(PyObject dict, PyType.Newstyle marker) {
    dict.__setitem__("mode", new PyGetSetDescr("mode", PyFile.class, "getMode", null, null));
    dict.__setitem__("name", new PyGetSetDescr("name", PyFile.class, "getName", null, null));
    dict.__setitem__("closed", new PyGetSetDescr("closed", PyFile.class, "getClosed", null, null));
    class exposed___cmp__ extends PyBuiltinMethodNarrow {

      exposed___cmp__(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed___cmp__(self, info);
      }

      public PyObject __call__(PyObject arg0) {
        int ret = ((PyFile) self).file___cmp__(arg0);
        if (ret == -2) {
          throw Py.TypeError(
              "file"
                  + ".__cmp__(x,y) requires y to be '"
                  + "file"
                  + "', not a '"
                  + (arg0).getType().fastGetName()
                  + "'");
        }
        return Py.newInteger(ret);
      }
    }
    dict.__setitem__(
        "__cmp__",
        new PyMethodDescr("__cmp__", PyFile.class, 1, 1, new exposed___cmp__(null, null)));
    class exposed___iter__ extends PyBuiltinMethodNarrow {

      exposed___iter__(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed___iter__(self, info);
      }

      public PyObject __call__() {
        return ((PyFile) self).file___iter__();
      }
    }
    dict.__setitem__(
        "__iter__",
        new PyMethodDescr("__iter__", PyFile.class, 0, 0, new exposed___iter__(null, null)));
    class exposed___iternext__ extends PyBuiltinMethodNarrow {

      exposed___iternext__(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed___iternext__(self, info);
      }

      public PyObject __call__() {
        return ((PyFile) self).file___iternext__();
      }
    }
    dict.__setitem__(
        "__iternext__",
        new PyMethodDescr(
            "__iternext__", PyFile.class, 0, 0, new exposed___iternext__(null, null)));
    class exposed___nonzero__ extends PyBuiltinMethodNarrow {

      exposed___nonzero__(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed___nonzero__(self, info);
      }

      public PyObject __call__() {
        return Py.newBoolean(((PyFile) self).file___nonzero__());
      }
    }
    dict.__setitem__(
        "__nonzero__",
        new PyMethodDescr("__nonzero__", PyFile.class, 0, 0, new exposed___nonzero__(null, null)));
    class exposed___repr__ extends PyBuiltinMethodNarrow {

      exposed___repr__(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed___repr__(self, info);
      }

      public PyObject __call__() {
        return new PyString(((PyFile) self).file_toString());
      }
    }
    dict.__setitem__(
        "__repr__",
        new PyMethodDescr("__repr__", PyFile.class, 0, 0, new exposed___repr__(null, null)));
    class exposed___str__ extends PyBuiltinMethodNarrow {

      exposed___str__(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed___str__(self, info);
      }

      public PyObject __call__() {
        return new PyString(((PyFile) self).file_toString());
      }
    }
    dict.__setitem__(
        "__str__",
        new PyMethodDescr("__str__", PyFile.class, 0, 0, new exposed___str__(null, null)));
    class exposed_close extends PyBuiltinMethodNarrow {

      exposed_close(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed_close(self, info);
      }

      public PyObject __call__() {
        ((PyFile) self).file_close();
        return Py.None;
      }
    }
    dict.__setitem__(
        "close", new PyMethodDescr("close", PyFile.class, 0, 0, new exposed_close(null, null)));
    class exposed_flush extends PyBuiltinMethodNarrow {

      exposed_flush(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed_flush(self, info);
      }

      public PyObject __call__() {
        ((PyFile) self).file_flush();
        return Py.None;
      }
    }
    dict.__setitem__(
        "flush", new PyMethodDescr("flush", PyFile.class, 0, 0, new exposed_flush(null, null)));
    class exposed_read extends PyBuiltinMethodNarrow {

      exposed_read(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed_read(self, info);
      }

      public PyObject __call__(PyObject arg0) {
        try {
          return new PyString(((PyFile) self).file_read(arg0.asInt(0)));
        } catch (PyObject.ConversionException e) {
          String msg;
          switch (e.index) {
            case 0:
              msg = "expected an integer";
              break;
            default:
              msg = "xxx";
          }
          throw Py.TypeError(msg);
        }
      }

      public PyObject __call__() {
        return new PyString(((PyFile) self).file_read());
      }
    }
    dict.__setitem__(
        "read", new PyMethodDescr("read", PyFile.class, 0, 1, new exposed_read(null, null)));
    class exposed_readline extends PyBuiltinMethodNarrow {

      exposed_readline(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed_readline(self, info);
      }

      public PyObject __call__(PyObject arg0) {
        try {
          return new PyString(((PyFile) self).file_readline(arg0.asInt(0)));
        } catch (PyObject.ConversionException e) {
          String msg;
          switch (e.index) {
            case 0:
              msg = "expected an integer";
              break;
            default:
              msg = "xxx";
          }
          throw Py.TypeError(msg);
        }
      }

      public PyObject __call__() {
        return new PyString(((PyFile) self).file_readline());
      }
    }
    dict.__setitem__(
        "readline",
        new PyMethodDescr("readline", PyFile.class, 0, 1, new exposed_readline(null, null)));
    class exposed_readlines extends PyBuiltinMethodNarrow {

      exposed_readlines(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed_readlines(self, info);
      }

      public PyObject __call__(PyObject arg0) {
        try {
          return ((PyFile) self).file_readlines(arg0.asInt(0));
        } catch (PyObject.ConversionException e) {
          String msg;
          switch (e.index) {
            case 0:
              msg = "expected an integer";
              break;
            default:
              msg = "xxx";
          }
          throw Py.TypeError(msg);
        }
      }

      public PyObject __call__() {
        return ((PyFile) self).file_readlines();
      }
    }
    dict.__setitem__(
        "readlines",
        new PyMethodDescr("readlines", PyFile.class, 0, 1, new exposed_readlines(null, null)));
    class exposed_seek extends PyBuiltinMethodNarrow {

      exposed_seek(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed_seek(self, info);
      }

      public PyObject __call__(PyObject arg0, PyObject arg1) {
        try {
          ((PyFile) self).file_seek(arg0.asLong(0), arg1.asInt(1));
          return Py.None;
        } catch (PyObject.ConversionException e) {
          String msg;
          switch (e.index) {
            case 0:
              msg = "expected a long";
              break;
            case 1:
              msg = "expected an integer";
              break;
            default:
              msg = "xxx";
          }
          throw Py.TypeError(msg);
        }
      }

      public PyObject __call__(PyObject arg0) {
        try {
          ((PyFile) self).file_seek(arg0.asLong(0));
          return Py.None;
        } catch (PyObject.ConversionException e) {
          String msg;
          switch (e.index) {
            case 0:
              msg = "expected a long";
              break;
            default:
              msg = "xxx";
          }
          throw Py.TypeError(msg);
        }
      }
    }
    dict.__setitem__(
        "seek", new PyMethodDescr("seek", PyFile.class, 1, 2, new exposed_seek(null, null)));
    class exposed_tell extends PyBuiltinMethodNarrow {

      exposed_tell(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed_tell(self, info);
      }

      public PyObject __call__() {
        return new PyLong(((PyFile) self).file_tell());
      }
    }
    dict.__setitem__(
        "tell", new PyMethodDescr("tell", PyFile.class, 0, 0, new exposed_tell(null, null)));
    class exposed_next extends PyBuiltinMethodNarrow {

      exposed_next(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed_next(self, info);
      }

      public PyObject __call__() {
        return ((PyFile) self).file_next();
      }
    }
    dict.__setitem__(
        "next", new PyMethodDescr("next", PyFile.class, 0, 0, new exposed_next(null, null)));
    class exposed_truncate extends PyBuiltinMethodNarrow {

      exposed_truncate(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed_truncate(self, info);
      }

      public PyObject __call__(PyObject arg0) {
        try {
          ((PyFile) self).file_truncate(arg0.asLong(0));
          return Py.None;
        } catch (PyObject.ConversionException e) {
          String msg;
          switch (e.index) {
            case 0:
              msg = "expected a long";
              break;
            default:
              msg = "xxx";
          }
          throw Py.TypeError(msg);
        }
      }

      public PyObject __call__() {
        ((PyFile) self).file_truncate();
        return Py.None;
      }
    }
    dict.__setitem__(
        "truncate",
        new PyMethodDescr("truncate", PyFile.class, 0, 1, new exposed_truncate(null, null)));
    class exposed_write extends PyBuiltinMethodNarrow {

      exposed_write(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed_write(self, info);
      }

      public PyObject __call__(PyObject arg0) {
        ((PyFile) self).file_write(arg0);
        return Py.None;
      }
    }
    dict.__setitem__(
        "write", new PyMethodDescr("write", PyFile.class, 1, 1, new exposed_write(null, null)));
    class exposed_writelines extends PyBuiltinMethodNarrow {

      exposed_writelines(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed_writelines(self, info);
      }

      public PyObject __call__(PyObject arg0) {
        ((PyFile) self).file_writelines(arg0);
        return Py.None;
      }
    }
    dict.__setitem__(
        "writelines",
        new PyMethodDescr("writelines", PyFile.class, 1, 1, new exposed_writelines(null, null)));
    class exposed_xreadlines extends PyBuiltinMethodNarrow {

      exposed_xreadlines(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed_xreadlines(self, info);
      }

      public PyObject __call__() {
        return ((PyFile) self).file_xreadlines();
      }
    }
    dict.__setitem__(
        "xreadlines",
        new PyMethodDescr("xreadlines", PyFile.class, 0, 0, new exposed_xreadlines(null, null)));
    class exposed___init__ extends PyBuiltinMethod {

      exposed___init__(PyObject self, PyBuiltinFunction.Info info) {
        super(self, info);
      }

      public PyBuiltinFunction bind(PyObject self) {
        return new exposed___init__(self, info);
      }

      public PyObject __call__(PyObject[] args) {
        return __call__(args, Py.NoKeywords);
      }

      public PyObject __call__(PyObject[] args, String[] keywords) {
        ((PyFile) self).file_init(args, keywords);
        return Py.None;
      }
    }
    dict.__setitem__(
        "__init__",
        new PyMethodDescr("__init__", PyFile.class, -1, -1, new exposed___init__(null, null)));
    dict.__setitem__(
        "__new__",
        new PyNewWrapper(PyFile.class, "__new__", -1, -1) {

          public PyObject new_impl(
              boolean init, PyType subtype, PyObject[] args, String[] keywords) {
            PyFile newobj;
            if (for_type == subtype) {
              newobj = null;
              if (init) {
                if (args.length == 0) {
                  newobj = new PyFile();
                  newobj.file_init(args, keywords);
                } else if (args[0] instanceof PyString
                    || (args[0] instanceof PyJavaInstance
                        && ((PyJavaInstance) args[0]).javaProxy == String.class)) {
                  // If first arg is a PyString or String, assume its being
                  // called as a builtin.
                  newobj = new PyFile();
                  newobj.file_init(args, keywords);
                  newobj.closer = new Closer(newobj.file);
                } else {
                  // assume it's being called as a java class
                  PyJavaClass pjc = new PyJavaClass(PyFile.class);
                  newobj = (PyFile) pjc.__call__(args, keywords);
                }
              } else {
                newobj = new PyFile();
              }
            } else {
              newobj = new PyFileDerived(subtype);
            }
            return newobj;
          }
        });
  }