Example #1
0
  public static PyObject EnvironmentError__str__(PyObject self, PyObject[] args, String[] kwargs) {
    PyObject errno = self.__findattr__("errno");
    PyObject strerror = self.__findattr__("strerror");
    PyObject filename = self.__findattr__("filename");
    PyString result;
    if (filename.__nonzero__()) {
      result = Py.newString("[Errno %s] %s: %s");
      result = (PyString) result.__mod__(new PyTuple(errno, strerror, filename.__repr__()));

    } else if (errno.__nonzero__() && strerror.__nonzero__()) {
      result = Py.newString("[Errno %s] %s");
      result = (PyString) result.__mod__(new PyTuple(errno, strerror));
    } else {
      return PyBaseException.TYPE.invoke("__str__", self, args, kwargs);
    }
    return result;
  }
Example #2
0
 public void __delattr__(String name) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__delattr__");
   if (impl != null) {
     impl.__get__(this, self_type).__call__(PyString.fromInterned(name));
     return;
   }
   super.__delattr__(name);
 }
 public String read(int n) throws IOException {
   if (n < 0) {
     n = (int) (file.length() - filePosition);
     if (n < 0) n = 0;
   }
   byte[] buf = new byte[n];
   n = readBytes(buf, 0, n);
   if (n < 0) n = 0;
   return PyString.from_bytes(buf, 0, n);
 }
 public void __setattr__(String name, PyObject value) {
   PyType self_type = getType();
   PyObject impl = self_type.lookup("__setattr__");
   if (impl != null) {
     impl.__get__(this, self_type).__call__(PyString.fromInterned(name), value);
     // CPython does not support instance-acquired finalizers.
     // So we don't check for __del__ here.
     return;
   }
   super.__setattr__(name, value);
 }
 public void setDict(PyObject newDict) {
   if (newDict instanceof PyStringMap || newDict instanceof PyDictionary) {
     dict = newDict;
     if (dict.__finditem__(PyString.fromInterned("__del__")) != null
         && !JyAttribute.hasAttr(this, JyAttribute.FINALIZE_TRIGGER_ATTR)) {
       FinalizeTrigger.ensureFinalizer(this);
     }
   } else {
     throw Py.TypeError("__dict__ must be set to a Dictionary " + newDict.getClass().getName());
   }
 }
 public void write(String s) throws IOException {
   byte[] bytes = PyString.to_bytes(s);
   int n = bytes.length;
   int i = 0;
   while (i < n) {
     int sz = n - i;
     sz = sz > MAX_WRITE ? MAX_WRITE : sz;
     ostream.write(bytes, i, sz);
     i += sz;
   }
 }
 public PyObject __findattr_ex__(String name) {
   PyType self_type = getType();
   // TODO: We should speed this up. As the __getattribute__ slot almost never
   //       changes, it is a good candidate for caching, as PyClass does with
   //       __getattr__. See #1102.
   PyObject getattribute = self_type.lookup("__getattribute__");
   PyString py_name = null;
   PyException firstAttributeError = null;
   try {
     if (getattribute != null) {
       py_name = PyString.fromInterned(name);
       return getattribute.__get__(this, self_type).__call__(py_name);
     } else {
       Py.Warning(String.format("__getattribute__ not found on type %s", self_type.getName()));
       PyObject ret = super.__findattr_ex__(name);
       if (ret != null) {
         return ret;
       } // else: pass through to __getitem__ invocation
     }
   } catch (PyException e) {
     if (!Py.matchException(e, Py.AttributeError)) {
       throw e;
     } else {
       firstAttributeError = e; // saved to avoid swallowing custom AttributeErrors
       // and pass through to __getattr__ invocation.
     }
   }
   PyObject getattr = self_type.lookup("__getattr__");
   if (getattr != null) {
     if (py_name == null) {
       py_name = PyString.fromInterned(name);
     }
     return getattr.__get__(this, self_type).__call__(py_name);
   }
   if (firstAttributeError != null) {
     throw firstAttributeError;
   }
   return null;
 }
 public String read(int n) throws IOException {
   if (n == 0)
     // nothing to do
     return "";
   if (n < 0) {
     // read until we hit EOF
     byte buf[] = new byte[1024];
     StringBuffer sbuf = new StringBuffer();
     for (int read = 0; read >= 0; read = istream.read(buf))
       sbuf.append(PyString.from_bytes(buf, 0, read));
     return sbuf.toString();
   }
   // read the next chunk available, but make sure it's at least
   // one byte so as not to trip the `empty string' return value
   // test done by the caller
   // int avail = istream.available();
   // n = (n > avail) ? n : avail;
   byte buf[] = new byte[n];
   int read = istream.read(buf);
   if (read < 0)
     // EOF encountered
     return "";
   return PyString.from_bytes(buf, 0, read);
 }
    public void write(String s) throws IOException {
      byte[] b = PyString.to_bytes(s);
      int len = b.length;

      // If the amount of data is small (less than a full buffer)...
      if (len < buffer.length) {
        // If any of the data fits within the buffer...
        int spaceInBuffer = 0;
        int copyLength = 0;
        if (filePosition >= bufferStart)
          spaceInBuffer = (int) ((bufferStart + buffer.length) - filePosition);
        if (spaceInBuffer > 0) {
          // Copy as much as possible to the buffer.
          copyLength = (spaceInBuffer > len) ? len : spaceInBuffer;
          System.arraycopy(b, 0, buffer, (int) (filePosition - bufferStart), copyLength);
          bufferModified = true;
          long myDataEnd = filePosition + copyLength;
          dataEnd = myDataEnd > dataEnd ? myDataEnd : dataEnd;
          dataSize = (int) (dataEnd - bufferStart);
          filePosition += copyLength;
        }

        // If there is any data remaining, move to the
        // new position and copy to the new buffer.
        if (copyLength < len) {
          seek(filePosition, 0);
          System.arraycopy(
              b, copyLength, buffer, (int) (filePosition - bufferStart), len - copyLength);
          bufferModified = true;
          long myDataEnd = filePosition + (len - copyLength);
          dataEnd = myDataEnd > dataEnd ? myDataEnd : dataEnd;
          dataSize = (int) (dataEnd - bufferStart);
          filePosition += (len - copyLength);
        }
      } else {
        // ...or write a lot of data...

        // Flush the current buffer, and write this data to the file.
        if (bufferModified) {
          flush();
          bufferStart = dataEnd = dataSize = 0;
        }
        file.write(b, 0, len);
        filePosition += len;
      }
    }
Example #10
0
 @ExposedSet(name = "func_name")
 public void setFuncName(PyString func_name) {
   __name__ = func_name.asString();
 }
Example #11
0
 public void write(String s) throws IOException {
   ostream.write(PyString.to_bytes(s));
 }
Example #12
0
 final PyObject file___iternext__() {
   PyString s = new PyString(readline());
   if (s.__len__() == 0) return null;
   return s;
 }
Example #13
0
  @ExposedClassMethod(doc = BuiltinDocs.float_fromhex_doc)
  public static PyObject float_fromhex(PyType type, PyObject o) {
    // XXX: I'm sure this could be shortened/simplified, but Double.parseDouble() takes
    // non-hex strings and requires the form 0xNUMBERpNUMBER for hex input which
    // causes extra complexity here.

    String message = "invalid hexadecimal floating-point string";
    boolean negative = false;

    PyString s = o.__str__();
    String value = s.getString().trim().toLowerCase();

    if (value.length() == 0) {
      throw Py.ValueError(message);
    } else if (value.equals("nan") || value.equals("-nan") || value.equals("+nan")) {
      return NAN;
    } else if (value.equals("inf")
        || value.equals("infinity")
        || value.equals("+inf")
        || value.equals("+infinity")) {
      return new PyFloat(Double.POSITIVE_INFINITY);
    } else if (value.equals("-inf") || value.equals("-infinity")) {
      return new PyFloat(Double.NEGATIVE_INFINITY);
    }

    // Strip and record + or -
    if (value.charAt(0) == '-') {
      value = value.substring(1);
      negative = true;
    } else if (value.charAt(0) == '+') {
      value = value.substring(1);
    }
    if (value.length() == 0) {
      throw Py.ValueError(message);
    }

    // Append 0x if not present.
    if (!value.startsWith("0x") && !value.startsWith("0X")) {
      value = "0x" + value;
    }

    // reattach - if needed.
    if (negative) {
      value = "-" + value;
    }

    // Append p if not present.
    if (value.indexOf('p') == -1) {
      value = value + "p0";
    }

    try {
      double d = Double.parseDouble(value);
      if (Double.isInfinite(d)) {
        throw Py.OverflowError("hexadecimal value too large to represent as a float");
      }
      return new PyFloat(d);
    } catch (NumberFormatException n) {
      throw Py.ValueError(message);
    }
  }