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

📄 py.java

📁 无线通信的主要编程软件,是无线通信工作人员的必备工具,关天相关教程我会在后续传上.
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    }    public static void initProxy(PyProxy proxy, String module, String pyclass,                                 Object[] args, String[] packages,                                 String[] props,                                 String frozenPackage,                                 String[] modules)    {        initProperties(null, packages, props, frozenPackage, modules,                    proxy.getClass().getClassLoader());        if (proxy._getPyInstance() != null)            return;        ThreadState ts = getThreadState();        PyInstance instance = ts.getInitializingProxy();        if (instance != null) {            if (instance.javaProxy != null)                throw Py.TypeError("Proxy instance reused");            instance.javaProxy = proxy;            proxy._setPyInstance(instance);            proxy._setPySystemState(ts.systemState);            return;        }        //System.out.println("path: "+sys.path.__str__());        PyObject mod;        // ??pending: findClass or should avoid sys.path loading?        Class modClass = Py.findClass(module+"$_PyInner");        if (modClass != null) {            //System.err.println("found as class: "+modClass);            PyCode code=null;            try {                code = ((PyRunnable)modClass.newInstance()).getMain();            } catch (Throwable t) {                throw Py.JavaError(t);            }            mod = imp.createFromCode(module, code);        } else {            mod = imp.importName(module.intern(), false);            //System.err.println("found as mod: "+mod);        }        PyClass pyc = (PyClass)mod.__getattr__(pyclass.intern());        instance = new PyInstance(pyc);        instance.javaProxy = proxy;        proxy._setPyInstance(instance);        proxy._setPySystemState(ts.systemState);        PyObject[] pargs;        if (args == null || args.length == 0) {            pargs = Py.EmptyObjects;        } else {            pargs = new PyObject[args.length];            for(int i=0; i<args.length; i++)                pargs[i] = Py.java2py(args[i]);        }        instance.__init__(pargs, Py.NoKeywords);    }    public static void initRunnable(String module, PyObject dict) {        Class mainClass=null;        try {            // ??pending: should use Py.findClass?            mainClass = Class.forName(module);        } catch (ClassNotFoundException exc) {            System.err.println("Error running main.  Can't find: "+module);            System.exit(-1);        }        PyCode code=null;        try {            code = ((PyRunnable)mainClass.newInstance()).getMain();        } catch (Throwable t) {            System.err.println("Invalid class (runnable): "+module+"$py");            System.exit(-1);        }        Py.runCode(code, dict, dict);    }    public static void runMain(Class mainClass, String[] args,                               String[] packages,                               String[] props,                               String frozenPackage,                               String[] modules) throws Exception    {        //System.err.println("main: "+module);        initProperties(args, packages, props, frozenPackage, modules,                       mainClass.getClassLoader());        try {            PyCode code=null;            try {                code = ((PyRunnable)mainClass.newInstance()).getMain();            } catch (Throwable t) {                System.err.println("Invalid class: " + mainClass.getName() +                                   "$py");                System.exit(-1);            }            PyObject mod = imp.createFromCode("__main__", code);        } catch (PyException e) {            Py.getSystemState().callExitFunc();            if (Py.matchException(e, Py.SystemExit))                return;            throw e;        }        Py.getSystemState().callExitFunc();    }    private static String getStackTrace(Throwable javaError) {        ByteArrayOutputStream buf = new ByteArrayOutputStream();        javaError.printStackTrace(new PrintStream(buf));        String str = buf.toString();        int index = -1;        if (index == -1)            index = str.indexOf(                "at org.python.core.PyReflectedConstructor.call");        if (index == -1)            index = str.indexOf("at org.python.core.PyReflectedMethod.call");        if (index == -1)            index = str.indexOf(                "at org/python/core/PyReflectedConstructor.call");        if (index == -1)            index = str.indexOf("at org/python/core/PyReflectedMethod.call");        if (index != -1)            index = str.lastIndexOf("\n", index);        int index0 = str.indexOf("\n");        if (index >= index0)            str = str.substring(index0+1,index+1);        return str;    }    /* Display a PyException and stack trace */    public static void printException(Throwable t) {        printException(t, null, null);    }    public static void printException(Throwable t, PyFrame f) {        printException(t, f, null);    }    public static synchronized void printException(Throwable t, PyFrame f,                                                   PyObject file)    {        //System.err.println("printingException: "+t+", "+file);        StdoutWrapper stderr = Py.stderr;        if (file != null) {            stderr = new FixedFileWrapper(file);        }        if (Options.showJavaExceptions) {            stderr.println("Java Traceback:");            java.io.CharArrayWriter buf = new java.io.CharArrayWriter();            if (t instanceof PyException) {                ((PyException)t).super__printStackTrace(                    new java.io.PrintWriter(buf));            } else {                t.printStackTrace(new java.io.PrintWriter(buf));            }            stderr.print(buf.toString());        }        PyException exc = Py.JavaError(t);        maybeSystemExit(exc);        if (f != null && exc.traceback.tb_frame != f) {            exc.traceback = new PyTraceback(exc.traceback);        }        setException(exc, f);        ThreadState ts = getThreadState();        ts.systemState.last_value = exc.value;        ts.systemState.last_type = exc.type;        ts.systemState.last_traceback = exc.traceback;        PyObject exceptHook = ts.systemState.__findattr__("excepthook");        if (exceptHook != null) {            try {                exceptHook.__call__(exc.type, exc.value, exc.traceback);            } catch (PyException exc2) {                stderr.println("Error in sys.excepthook:");                displayException(exc2.type, exc2.value, exc2.traceback, file);                stderr.println();                stderr.println("Original exception was:");                displayException(exc.type, exc.value, exc.traceback, file);            }        } else {            stderr.println("sys.excepthook is missing");            displayException(exc.type, exc.value, exc.traceback, file);        }        ts.exception = null;    }    public static void displayException(PyObject type, PyObject value,                                        PyObject tb, PyObject file)    {        StdoutWrapper stderr = Py.stderr;        if (file != null) {            stderr = new FixedFileWrapper(file);        }        if (tb instanceof PyTraceback)            stderr.print(((PyTraceback) tb).dumpStack());        if (__builtin__.isinstance(value, (PyClass) Py.SyntaxError)) {            stderr.println("  File \""+value.__findattr__("filename")+                           "\", line "+value.__findattr__("lineno"));            PyObject text = value.__findattr__("text");            if (text != Py.None && text.__len__() != 0) {                stderr.println("\t"+text);                String space = "\t";                int col = value.__findattr__("offset").__int__().getValue();                for(int j=1; j<col; j++)                    space = space+" ";                stderr.println(space+"^");            }        }        if (value instanceof PyJavaInstance) {            Object javaError = value.__tojava__(Throwable.class);            if (javaError != null && javaError != Py.NoConversion) {                stderr.println(getStackTrace((Throwable)javaError));            }        }        stderr.println(formatException(type, value, tb));    }    static String formatException(PyObject type, PyObject value, PyObject tb) {        StringBuffer buf = new StringBuffer();        PyObject typeName;        if (type instanceof PyClass) {            buf.append(((PyClass) type).__name__);        } else {            buf.append(type.__str__());        }        if (value != Py.None) {            buf.append(": ");            if (__builtin__.isinstance(value, (PyClass) Py.SyntaxError)) {                buf.append(value.__getitem__(0).__str__());            } else {                buf.append(value.__str__());            }        }        return buf.toString();    }    /* Equivalent to Python's assert statement */    public static void assert_(PyObject test, PyObject message) {        if (!test.__nonzero__()) {            throw new PyException(Py.AssertionError, message);        }    }    public static void assert_(PyObject test) {        assert_(test, Py.None);    }    /* Helpers to implement finally clauses */    public static void addTraceback(Throwable t, PyFrame frame) {        PyException e = Py.JavaError(t);        //Add another traceback object to the exception if needed        if (e.traceback.tb_frame != frame) {            e.traceback = new PyTraceback(e.traceback);        }    }    /* Helpers to implement except clauses */    public static PyException setException(Throwable t, PyFrame frame) {        //System.out.println("Py.setException");        PyException pye = Py.JavaError(t);        pye.instantiate();        ThreadState ts = getThreadState();        ts.exception = pye;        /*.type;          ts.exc_value = pye.value;          ts.exc_traceback = pye.traceback;*/        return pye;    }    public static boolean matchException(PyException pye, PyObject e) {        pye.instantiate();        // FIXME, see bug 737978        //        // A special case for IOError's to allow them to also match        // java.io.IOExceptions.  This is a hack for 1.0.x until I can do        // it right in 1.1        if (e == Py.IOError) {            if (__builtin__.isinstance(                pye.value,                PyJavaClass.lookup(java.io.IOException.class)))            {                return true;            }        }        // FIXME too, same approach for OutOfMemoryError        if (e == Py.MemoryError) {            if (__builtin__.isinstance(                pye.value,                PyJavaClass.lookup(java.lang.OutOfMemoryError.class)))            {                return true;            }        }                if (e == Py.IOError) {            if (__builtin__.isinstance(                pye.value,                PyJavaClass.lookup(java.io.IOException.class)))            {                return true;            }        }        // same approach for OutOfMemoryError                        if (e instanceof PyClass) {            return __builtin__.isinstance(pye.value, (PyClass)e);        }        else {            if (e == pye.type)                return true;            if (e instanceof PyTuple) {                PyObject[] l = ((PyTuple)e).list;                for (int i=0; i<l.length; i++) {                    if (matchException(pye, l[i]))                        return true;                }            }            return false;        }    }    /* Implement the raise statement */    // reraise the current exception    public static PyException makeException() {        ThreadState ts = getThreadState();        if (ts.exception == null) {            throw Py.ValueError("no exception to reraise");        }        return ts.exception;    }    public static PyException makeException(PyObject type) {        if (type instanceof PyInstance) {            return new PyException(type.__class__, type);        } else {            return makeException(type, Py.None);        }    }    public static PyException makeException(PyObject type, PyObject value) {        if (type instanceof PyInstance) {            if (value != Py.None) {                throw TypeError("instance exceptions may not have " +                                "a separate value");            } else {                return new PyException(type.__class__, type);            }        }        PyException exc = new PyException(type, value);        exc.instantiate();        return exc;    }

⌨️ 快捷键说明

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