示例#1
0
 public PyFloat __float__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__float__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (res instanceof PyFloat) return (PyFloat) res;
     throw Py.TypeError(
         "__float__" + " returned non-" + "float" + " (type " + res.getType().fastGetName() + ")");
   }
   return super.__float__();
 }
示例#2
0
 public String toString() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__repr__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (!(res instanceof PyString))
       throw Py.TypeError(
           "__repr__ returned non-string (type " + res.getType().fastGetName() + ")");
     return ((PyString) res).toString();
   }
   return super.toString();
 }
示例#3
0
 public PyObject __index__() {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__index__");
   if (impl != null) {
     PyObject res = impl.__get__(this, self_type).__call__();
     if (res instanceof PyInteger || res instanceof PyLong) {
       return res;
     }
     throw Py.TypeError(
         String.format(
             "__index__ returned non-(int,long) (type %s)", res.getType().fastGetName()));
   }
   return super.__index__();
 }
 public void dispatch__init__(PyType type, PyObject[] args, String[] keywords) {
   PyType self_type = getType();
   if (self_type.isSubType(type)) {
     PyObject impl = self_type.lookup("__init__");
     if (impl != null) {
       PyObject res = impl.__get__(this, self_type).__call__(args, keywords);
       if (res != Py.None) {
         throw Py.TypeError(
             String.format(
                 "__init__() should return None, not '%.200s'", res.getType().fastGetName()));
       }
       proxyInit();
     }
   }
 }
  @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___iadd___doc)
  final synchronized PyObject list___iadd__(PyObject o) {
    PyType oType = o.getType();
    if (oType == TYPE || oType == PyTuple.TYPE || this == o) {
      extend(fastSequence(o, "argument must be iterable"));
      return this;
    }

    PyObject it;
    try {
      it = o.__iter__();
    } catch (PyException pye) {
      if (!pye.match(Py.TypeError)) {
        throw pye;
      }
      return null;
    }
    extend(it);
    return this;
  }
示例#6
0
  @ExposedNew
  static final PyObject function___new__(
      PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) {
    ArgParser ap =
        new ArgParser(
            "function",
            args,
            keywords,
            new String[] {"code", "globals", "name", "argdefs", "closure"},
            0);
    PyObject code = ap.getPyObject(0);
    PyObject globals = ap.getPyObject(1);
    PyObject name = ap.getPyObject(2, Py.None);
    PyObject defaults = ap.getPyObject(3, Py.None);
    PyObject closure = ap.getPyObject(4, Py.None);

    if (!(code instanceof PyBaseCode)) {
      throw Py.TypeError("function() argument 1 must be code, not " + code.getType().fastGetName());
    }
    if (name != Py.None && !Py.isInstance(name, PyString.TYPE)) {
      throw Py.TypeError("arg 3 (name) must be None or string");
    }
    if (defaults != Py.None && !(defaults instanceof PyTuple)) {
      throw Py.TypeError("arg 4 (defaults) must be None or tuple");
    }

    PyBaseCode tcode = (PyBaseCode) code;
    int nfree = tcode.co_freevars == null ? 0 : tcode.co_freevars.length;
    if (!(closure instanceof PyTuple)) {
      if (nfree > 0 && closure == Py.None) {
        throw Py.TypeError("arg 5 (closure) must be tuple");
      } else if (closure != Py.None) {
        throw Py.TypeError("arg 5 (closure) must be None or tuple");
      }
    }

    int nclosure = closure == Py.None ? 0 : closure.__len__();
    if (nfree != nclosure) {
      throw Py.ValueError(
          String.format(
              "%s requires closure of length %d, not %d", tcode.co_name, nfree, nclosure));
    }
    if (nclosure > 0) {
      for (PyObject o : ((PyTuple) closure).asIterable()) {
        if (!(o instanceof PyCell)) {
          throw Py.TypeError(
              String.format("arg 5 (closure) expected cell, found %s", o.getType().fastGetName()));
        }
      }
    }

    PyFunction function =
        new PyFunction(
            globals,
            defaults == Py.None ? null : ((PyTuple) defaults).getArray(),
            tcode,
            null,
            closure == Py.None ? null : ((PyTuple) closure).getArray());
    if (name != Py.None) {
      function.__name__ = name.toString();
    }
    return function;
  }
示例#7
0
  @ExposedNew
  public static PyObject complex_new(
      PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) {
    ArgParser ap = new ArgParser("complex", args, keywords, "real", "imag");
    PyObject real = ap.getPyObject(0, Py.Zero);
    PyObject imag = ap.getPyObject(1, null);

    // Special-case for single argument that is already complex
    if (real.getType() == TYPE && new_.for_type == subtype && imag == null) {
      return real;
    }
    if (real instanceof PyString) {
      if (imag != null) {
        throw Py.TypeError("complex() can't take second arg if first is a string");
      }
      return real.__complex__();
    }
    if (imag != null && imag instanceof PyString) {
      throw Py.TypeError("complex() second arg can't be a string");
    }

    try {
      real = real.__complex__();
    } catch (PyException pye) {
      if (!Py.matchException(pye, Py.AttributeError)) {
        // __complex__ not supported
        throw pye;
      }
      // otherwise try other means
    }

    PyComplex complexReal;
    PyComplex complexImag;
    PyFloat toFloat = null;
    if (real instanceof PyComplex) {
      complexReal = (PyComplex) real;
    } else {
      try {
        toFloat = real.__float__();
      } catch (PyException pye) {
        if (Py.matchException(pye, Py.AttributeError)) {
          // __float__ not supported
          throw Py.TypeError("complex() argument must be a string or a number");
        }
        throw pye;
      }
      complexReal = new PyComplex(toFloat.getValue());
    }

    if (imag == null) {
      complexImag = new PyComplex(0.0);
    } else if (imag instanceof PyComplex) {
      complexImag = (PyComplex) imag;
    } else {
      toFloat = null;
      try {
        toFloat = imag.__float__();
      } catch (PyException pye) {
        if (Py.matchException(pye, Py.AttributeError)) {
          // __float__ not supported
          throw Py.TypeError("complex() argument must be a string or a number");
        }
        throw pye;
      }
      complexImag = new PyComplex(toFloat.getValue());
    }

    complexReal.real -= complexImag.imag;
    complexReal.imag += complexImag.real;
    if (new_.for_type != subtype) {
      complexReal = new PyComplexDerived(subtype, complexReal.real, complexReal.imag);
    }
    return complexReal;
  }