Esempio n. 1
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);
  }
 public int __len__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__len__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     return res.asInt();
   }
   return super.__len__();
 }
Esempio n. 3
0
 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);
   }
 }
Esempio n. 4
0
 public int __cmp__(PyObject other) {
   PyType self_type = getType();
   PyObject[] where_type = new PyObject[1];
   PyObject impl = self_type.lookup_where("__cmp__", where_type);
   // Full Compatibility with CPython __cmp__:
   // If the derived type don't override __cmp__, the
   // *internal* super().__cmp__ should be called, not the
   // exposed one. The difference is that the exposed __cmp__
   // throws a TypeError if the argument is an instance of the same type.
   if (impl == null || where_type[0] == TYPE || Py.isSubClass(TYPE, where_type[0])) {
     return super.__cmp__(other);
   }
   PyObject res = impl.__get__(this, self_type).__call__(other);
   if (res == Py.NotImplemented) {
     return -2;
   }
   int c = res.asInt();
   return c < 0 ? -1 : c > 0 ? 1 : 0;
 }
Esempio n. 5
0
 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);
   }
 }