示例#1
0
 @Override
 protected Class<?> findClass(String name) throws ClassNotFoundException {
   PySystemState sys = Py.getSystemState();
   ClassLoader sysClassLoader = sys.getClassLoader();
   if (sysClassLoader != null) {
     // sys.classLoader overrides this class loader!
     return sysClassLoader.loadClass(name);
   }
   // Search the sys.path for a .class file matching the named class.
   PyList path = sys.path;
   for (int i = 0; i < path.__len__(); i++) {
     byte[] buffer;
     PyObject entry = replacePathItem(sys, i, path);
     if (entry instanceof SyspathArchive) {
       SyspathArchive archive = (SyspathArchive) entry;
       buffer = getBytesFromArchive(archive, name);
     } else {
       if (!(entry instanceof PyUnicode)) {
         entry = entry.__str__();
       }
       String dir = entry.toString();
       buffer = getBytesFromDir(dir, name);
     }
     if (buffer != null) {
       definePackageForClass(name);
       return defineClass(name, buffer, 0, buffer.length);
     }
   }
   // couldn't find the .class file on sys.path
   throw new ClassNotFoundException(name);
 }
示例#2
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);
  }
示例#3
0
 final void file_write(PyObject o) {
   if (o instanceof PyUnicode) {
     // Call __str__ on unicode objects to encode them before writing
     file_write(o.__str__().string);
   } else if (o instanceof PyString) {
     file_write(((PyString) o).string);
   } else {
     throw Py.TypeError("write requires a string as its argument");
   }
 }
示例#4
0
  @Override
  protected Enumeration<URL> findResources(String res) throws IOException {
    List<URL> resources = new ArrayList<URL>();

    PySystemState sys = Py.getSystemState();

    if (res.charAt(0) == SLASH_CHAR) {
      res = res.substring(1);
    }
    String entryRes = res;
    if (File.separatorChar != SLASH_CHAR) {
      res = res.replace(SLASH_CHAR, File.separatorChar);
      entryRes = entryRes.replace(File.separatorChar, SLASH_CHAR);
    }

    PyList path = sys.path;
    for (int i = 0; i < path.__len__(); i++) {
      PyObject entry = replacePathItem(sys, i, path);
      if (entry instanceof SyspathArchive) {
        SyspathArchive archive = (SyspathArchive) entry;
        ZipEntry ze = archive.getEntry(entryRes);
        if (ze != null) {
          try {
            resources.add(new URL("jar:file:" + archive.asUriCompatibleString() + "!/" + entryRes));
          } catch (MalformedURLException e) {
            throw new RuntimeException(e);
          }
        }
        continue;
      }
      if (!(entry instanceof PyUnicode)) {
        entry = entry.__str__();
      }
      String dir = sys.getPath(entry.toString());
      try {
        File resource = new File(dir, res);
        if (!resource.exists()) {
          continue;
        }
        resources.add(resource.toURI().toURL());
      } catch (MalformedURLException e) {
        throw new RuntimeException(e);
      }
    }
    return Collections.enumeration(resources);
  }
示例#5
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);
    }
  }