⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imp.java

📁 无线通信的主要编程软件,是无线通信工作人员的必备工具,关天相关教程我会在后续传上.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// Copyright (c) Corporation for National Research Initiativespackage org.python.core;import java.lang.reflect.*;import java.io.*;import java.util.Hashtable;import java.util.Properties;import java.util.zip.*;/** * Utility functions for "import" support. */public class imp{    public static final int APIVersion = 12;    private imp() { ; }    public static PyModule addModule(String name) {        PyObject modules = Py.getSystemState().modules;        PyModule module = (PyModule)modules.__finditem__(name);        if (module != null) {            return module;        }        module = new PyModule(name, null);        modules.__setitem__(name, module);        return module;    }    private static byte[] readBytes(InputStream fp) {        try {            byte[] buf = FileUtil.readBytes( fp );            fp.close();            return buf;        } catch (IOException ioe) {            throw Py.IOError(ioe);        }    }    private static InputStream makeStream(File file) {        try {            return new FileInputStream(file);        } catch (IOException ioe) {            throw Py.IOError(ioe);        }    }    private static PyObject createFromPyClass(String name, InputStream fp,                                              boolean testing,                                              String fileName)    {        byte[] data = readBytes(fp);        int n = data.length;        int api = (data[n-4]<<24)+(data[n-3]<<16)+(data[n-2]<<8)+data[n-1];        if (api != APIVersion) {            if (testing) {                return null;            } else {                throw Py.ImportError("invalid api version("+api+" != "+                                     APIVersion+") in: "+name);            }        }        //System.err.println("APIVersion: "+api);        PyCode code;        try {            code = BytecodeLoader.makeCode(name+"$py", data);        } catch (Throwable t) {            if (testing)                return null;            else                throw Py.JavaError(t);        }        Py.writeComment("import", "'" + name + "' as " + fileName);        return createFromCode(name, code);    }    public static byte[] compileSource(String name, File file) {        return compileSource(name, file, null, null);    }    public static byte[] compileSource(String name, File file,                                       String filename, String outFilename)    {        if (filename == null) {            filename = file.toString();        }        if (outFilename == null) {            outFilename = filename.substring(0,filename.length()-3)+                "$py.class";        }        return compileSource(name, makeStream(file), filename, outFilename);    }    static byte[] compileSource(String name, InputStream fp,                                String filename)    {        String outFilename = null;        if (filename != null) {            outFilename = filename.substring(0,filename.length()-3)+                "$py.class";        }        return compileSource(name, fp, filename, outFilename);    }    static byte[] compileSource(String name, InputStream fp, String filename,                                String outFilename)    {        try {            ByteArrayOutputStream ofp = new ByteArrayOutputStream();            if (filename == null)                filename = "<unknown>";            org.python.parser.ast.modType node = null; //*Forte*            try {                node = parser.parse(fp, "exec", filename, null);            } finally {                fp.close();            }            org.python.compiler.Module.compile(node, ofp, name+"$py",                                               filename, true, false, true,                                               null);            if (outFilename != null) {                File classFile = new File(outFilename);                try {                    FileOutputStream fop = new FileOutputStream(classFile);                    ofp.writeTo(fop);                    fop.close();                } catch (IOException exc) {                    // If we can't write the cache file, just fail silently                }            }            return ofp.toByteArray();        } catch (Throwable t) {            throw parser.fixParseError(null, t, filename);        }    }    private static PyObject createFromSource(String name, InputStream fp,                                             String filename)    {        byte[] bytes = compileSource(name, fp, filename);        Py.writeComment("import", "'" + name + "' as " + filename);        PyCode code = BytecodeLoader.makeCode(name+"$py", bytes);        return createFromCode(name, code);    }    private static PyObject createFromSource(String name, InputStream fp,                                             String filename,                                             String outFilename)    {        byte[] bytes = compileSource(name, fp, filename, outFilename);        Py.writeComment("import", "'" + name + "' as " + filename);        PyCode code = BytecodeLoader.makeCode(name+"$py", bytes);        return createFromCode(name, code);    }    static PyObject createFromCode(String name, PyCode c) {        PyModule module = addModule(name);        PyTableCode code = null;        if (c instanceof PyTableCode)            code = (PyTableCode)c;        PyFrame f = new PyFrame(code, module.__dict__, module.__dict__, null);        code.call(f);        return module;    }    private static Object syspathJavaLoaderLock = new Object();    private static ClassLoader syspathJavaLoader = null;    public static ClassLoader getSyspathJavaLoader() {        synchronized (syspathJavaLoaderLock) {            if (syspathJavaLoader == null) {                syspathJavaLoader = new SyspathJavaLoader();            }        }        return syspathJavaLoader;    }    private static PyObject createFromClass(String name, Class c) {        //Two choices.  c implements PyRunnable or c is Java package        //System.err.println("create from class: "+name+", "+c);        Class interfaces[] = c.getInterfaces();        for (int i=0; i<interfaces.length; i++) {            if (interfaces[i] == PyRunnable.class) {                //System.err.println("is runnable");                try {                    PyObject o = createFromCode(                        name, ((PyRunnable)c.newInstance()).getMain());                    return o;                } catch (InstantiationException e) {                    throw Py.JavaError(e);                } catch (IllegalAccessException e) {                    throw Py.JavaError(e);                }            }        }        return PyJavaClass.lookup(c);    }    private static PyObject loadBuiltin(String name, PyList path) {        if (name == "sys") {            Py.writeComment("import", "'" + name + "' as sys in " +                            "builtin modules");            return Py.java2py(Py.getSystemState());        }        String mod = PySystemState.getBuiltin(name);        if (mod != null) {            Class c = Py.findClassEx(mod, "builtin modules");            if (c != null) {                Py.writeComment("import", "'" + name + "' as " + mod +                                " in builtin modules");                try {                    return createFromClass(name, c);                }                catch (NoClassDefFoundError e) {                    throw Py.ImportError("Cannot import " + name +                                         ", missing class " +                                         c.getName());                }            }        }        return null;    }    private static Class findPyClass(String modName) {        if (Py.frozenPackage != null) {            modName = Py.frozenPackage+"."+modName;        }        return Py.findClassEx(modName+"$_PyInner", "precompiled");    }    private static PyObject loadPrecompiled(String name, String modName,                                            PyList path)    {        if (Py.frozenModules != null) {            //System.out.println("precomp: "+name+", "+modName);            Class c;            if (Py.frozenModules.get(modName+".__init__") != null) {                //System.err.println("trying: "+modName+".__init__$_PyInner");                c = findPyClass(modName+".__init__");                if (c == null) return null;                Py.writeComment("import", "'" + modName + "' as " +                                "precompiled package");                //System.err.println("found: "+modName+".__init__$_PyInner");                PyModule m = addModule(modName);                m.__dict__.__setitem__("__path__", new PyList());            }            else if (Py.frozenModules.get(modName) != null) {                c = findPyClass(modName);                if (c == null) return null;                Py.writeComment("import", "'" + modName + "' as " +                                "precompiled module");            }            else return null;            //System.err.println("creating: "+modName+", "+c);            return createFromClass(modName, c);        }        return null;    }    static PyObject loadFromZipFile(String name, String modName,                                    SyspathArchive zipArchive) {        PyObject o = null;        ZipEntry pkgEntry = null;        String entryName = name;        String pyName = entryName +".py";        String className = entryName +"$py.class";        try {            String sourceName = entryName + "/__init__.py";            String compledName = entryName + "/__init__$py.class";            ZipEntry sourceEntry = zipArchive.getEntry(sourceName);            ZipEntry compiledEntry = zipArchive.getEntry(compledName);            if (sourceEntry != null || compiledEntry != null) {                Py.writeDebug("import", "trying package: " + modName +                            " in jar/zip file " + zipArchive);                PyModule m = addModule(modName);                SyspathArchive subArchive = zipArchive.makeSubfolder(name);                PyList zipPath = new PyList(new PyObject[] { subArchive });                m.__dict__.__setitem__("__path__", zipPath);                o = loadFromZipFile("__init__", modName, subArchive);                if (o != null) {                    return m;                }            }            ZipEntry pyEntry = zipArchive.getEntry(pyName);            ZipEntry classEntry = zipArchive.getEntry(className);            if (pyEntry != null) {                Py.writeDebug("import", "trying source entry: " + pyName +                              " from jar/zip file " + zipArchive);                if (classEntry != null) {                    Py.writeDebug("import", "trying precompiled entry " +                                  className + " from jar/zip file " +                                  zipArchive);                    long pyTime = pyEntry.getTime();                    long classTime = classEntry.getTime();                    if (classTime >= pyTime) {                        InputStream is =                                    zipArchive.getInputStream(classEntry);                        o = createFromPyClass(modName, is, true,                                              classEntry.getName());                        if (o != null) {                            return o;                        }                    }                }                InputStream is = zipArchive.getInputStream(pyEntry);                return createFromSource(modName, is, pyEntry.getName(), null);            }        } catch (Exception e) {            Py.writeDebug("import", "loadFromZipFile exception: " +                          e.toString());            e.printStackTrace();        }        return null;    }    private static boolean isSyspathArchive(PyObject entry, boolean isDir) {        if (entry instanceof SyspathArchive)            return true;        if (isDir)            return false;        return SyspathArchive.getArchiveName(entry.toString()) != null;    }    static PyObject loadFromPath(String name, PyList path) {        return loadFromPath(name, name, path);    }    static PyObject loadFromPath(String name, String modName, PyList path) {        //System.err.println("load-from-path:"+name+" "+modName+" "+path);        PyObject o = loadPrecompiled(name, modName, path);        if (o != null) return o;        int nlen = name.length();        String pyName = name+".py";        String className = name+"$py.class";        int n = path.__len__();        for (int i=0; i<n; i++) {            PyObject entry = path.__getitem__(i);            String dirName = entry.toString();            // The empty string translates into the current working            // directory, which is usually provided on the system property            // "user.dir".  Don't rely on File's constructor to provide            // this correctly.            if (dirName.length() == 0) {                dirName = null;            }            // First check for packages            File dir = new File(dirName, name);            boolean isDir = dir.isDirectory();            if (isSyspathArchive(entry, isDir)) {                Py.writeDebug("import", "trying " + modName +                              " in jar/zip file " + dirName);                if (!(entry instanceof SyspathArchive)) {                    try {                        entry = new SyspathArchive(dirName);                        path.__setitem__(i, entry);                    } catch (IOException exc) {                        // Silently ignore corrupt and missing .zip files.                        continue;                    }                }                PyObject ret = loadFromZipFile(name, modName,                                              (SyspathArchive)entry);                // Not found in zip/jar file check next item in path                if (ret != null) {                    return ret;                }            }            if (isDir && caseok(dir, name, nlen) &&                (new File(dir, "__init__.py").isFile() ||

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -