Esempio n. 1
0
  public static PyString UnicodeTranslateError__str__(
      PyObject self, PyObject[] args, String[] kwargs) {
    int start = self.__getattr__("start").asInt();
    int end = self.__getattr__("end").asInt();
    // Get reason as a string, which it might not be if it's been modified after we
    // were contructed
    PyObject reason = self.__getattr__("reason").__str__();
    PyObject object = getUnicode(self.__getattr__("object"), "object");

    String result;
    if (start < object.__len__() && end == (start + 1)) {
      int badchar = object.toString().codePointAt(start);
      String badCharStr;
      if (badchar <= 0xff) {
        badCharStr = String.format("x%02x", badchar);
      } else if (badchar <= 0xffff) {
        badCharStr = String.format("u%04x", badchar);
      } else {
        badCharStr = String.format("U%08x", badchar);
      }
      result =
          String.format(
              "can't translate character u'\\%s' in position %d: %.400s",
              badCharStr, start, reason);
    } else {
      result =
          String.format(
              "can't translate characters in position %d-%d: %.400s", start, end - 1, reason);
    }
    return Py.newString(result);
  }
Esempio n. 2
0
  public static PyString UnicodeDecodeError__str__(
      PyObject self, PyObject[] args, String[] kwargs) {
    int start = self.__getattr__("start").asInt();
    int end = self.__getattr__("end").asInt();
    // Get reason and encoding as strings, which they might not be if they've been
    // modified after we were contructed
    PyObject reason = self.__getattr__("reason").__str__();
    PyObject encoding = self.__getattr__("encoding").__str__();
    PyObject object = getString(self.__getattr__("object"), "object");

    String result;
    if (start < object.__len__() && end == (start + 1)) {
      int badByte = (object.toString().charAt(start)) & 0xff;
      result =
          String.format(
              "'%.400s' codec can't decode byte 0x%x in position %d: %.400s",
              encoding, badByte, start, reason);
    } else {
      result =
          String.format(
              "'%.400s' codec can't decode bytes in position %d-%d: %.400s",
              encoding, start, end - 1, reason);
    }
    return Py.newString(result);
  }
Esempio n. 3
0
 /**
  * Determine the end position for UnicodeErrors.
  *
  * @param self a UnicodeError value
  * @param unicode whether the UnicodeError object should be unicode
  * @return an the end position
  */
 public static int getEnd(PyObject self, boolean unicode) {
   int end = self.__getattr__("end").asInt();
   PyObject object;
   if (unicode) {
     object = getUnicode(self.__getattr__("object"), "object");
   } else {
     object = getString(self.__getattr__("object"), "object");
   }
   if (end < 1) {
     end = 1;
   }
   if (end > object.__len__()) {
     end = object.__len__();
   }
   return end;
 }
Esempio n. 4
0
 /**
  * Determine the start position for UnicodeErrors.
  *
  * @param self a UnicodeError value
  * @param unicode whether the UnicodeError object should be unicode
  * @return an the start position
  */
 public static int getStart(PyObject self, boolean unicode) {
   int start = self.__getattr__("start").asInt();
   PyObject object;
   if (unicode) {
     object = getUnicode(self.__getattr__("object"), "object");
   } else {
     object = getString(self.__getattr__("object"), "object");
   }
   if (start < 0) {
     start = 0;
   }
   if (start >= object.__len__()) {
     start = object.__len__() - 1;
   }
   return start;
 }
Esempio n. 5
0
  public static PyString SyntaxError__str__(PyObject self, PyObject[] arg, String[] kwargs) {
    PyObject msg = self.__getattr__("msg");
    PyObject str = msg.__str__();
    if (!(msg instanceof PyString)) {
      return Py.newString(str.toString());
    }

    PyObject filename = self.__findattr__("filename");
    PyObject lineno = self.__findattr__("lineno");
    boolean haveFilename = filename instanceof PyString;
    boolean haveLieno = lineno instanceof PyInteger;
    if (!haveFilename && !haveLieno) {
      return (PyString) str;
    }

    String result;
    if (haveFilename && haveLieno) {
      result =
          String.format("%s (%s, line %d)", str, basename(filename.toString()), lineno.asInt());
    } else if (haveFilename) {
      result = String.format("%s (%s)", str, basename(filename.toString()));
    } else {
      result = String.format("%s (line %d)", str, lineno.asInt());
    }

    return Py.newString(result);
  }
 // TBD: this should be unnecessary
 public PyObject __dir__() {
   PyString members[] = new PyString[__members__.length];
   for (int i = 0; i < __members__.length; i++) members[i] = new PyString(__members__[i]);
   PyList ret = new PyList(members);
   PyObject k = im_func.__getattr__("__dict__").invoke("keys");
   ret.extend(k);
   return ret;
 }