Examples of PySystemState


Examples of org.python.core.PySystemState

        for (String wopt : opts.warnoptions) {
            warnoptions.append(new PyString(wopt));
        }
        Py.getSystemState().setWarnoptions(warnoptions);

        PySystemState systemState = Py.getSystemState();
        // Decide if stdin is interactive
        if (!opts.fixInteractive || opts.interactive) {
            opts.interactive = ((PyFile)Py.defaultSystemState.stdin).isatty();
            if (!opts.interactive) {
                systemState.ps1 = systemState.ps2 = Py.EmptyString;
            }
        }

        // Now create an interpreter
        InteractiveConsole interp = newInterpreter(opts.interactive);
        systemState.__setattr__("_jy_interpreter", Py.java2py(interp));

        // Print banner and copyright information (or not)
        if (opts.interactive && opts.notice && !opts.runModule) {
            System.err.println(InteractiveConsole.getDefaultBanner());
        }

        if (Options.importSite) {
            try {
                imp.load("site");
                if (opts.interactive && opts.notice && !opts.runModule) {
                    System.err.println(COPYRIGHT);
                }
            } catch (PyException pye) {
                if (!pye.match(Py.ImportError)) {
                    System.err.println("error importing site");
                    Py.printException(pye);
                    System.exit(-1);
                }
            }
        }

        if (opts.division != null) {
            if ("old".equals(opts.division)) {
                Options.divisionWarning = 0;
            } else if ("warn".equals(opts.division)) {
                Options.divisionWarning = 1;
            } else if ("warnall".equals(opts.division)) {
                Options.divisionWarning = 2;
            } else if ("new".equals(opts.division)) {
                Options.Qnew = true;
                interp.cflags.setFlag(CodeFlag.CO_FUTURE_DIVISION);
            }
        }

        // was there a filename on the command line?
        if (opts.filename != null) {
            String path;
            try {
                 path = new File(opts.filename).getCanonicalFile().getParent();
            } catch (IOException ioe) {
                 path = new File(opts.filename).getAbsoluteFile().getParent();
            }
            if (path == null) {
                path = "";
            }
            Py.getSystemState().path.insert(0, new PyString(path));
            if (opts.jar) {
              try {
                runJar(opts.filename);
              } catch (Throwable t) {
                    Py.printException(t);
                    System.exit(-1);               
              }
            } else if (opts.filename.equals("-")) {
                try {
                    interp.globals.__setitem__(new PyString("__file__"), new PyString("<stdin>"));
                    interp.execfile(System.in, "<stdin>");
                } catch (Throwable t) {
                    Py.printException(t);
                }
            } else {
                try {
                   interp.globals.__setitem__(new PyString("__file__"),
                                              new PyString(opts.filename));

                   FileInputStream file;
                   try {
                       file = new FileInputStream(new RelativeFile(opts.filename));
                   } catch (FileNotFoundException e) {
                       throw Py.IOError(e);
                   }
                   try {
                       if (PosixModule.getPOSIX().isatty(file.getFD())) {
                           opts.interactive = true;
                           interp.interact(null, new PyFile(file));
                           return;
                       } else {
                           interp.execfile(file, opts.filename);
                       }
                   } finally {
                       file.close();
                   }
                } catch (Throwable t) {
                    if (t instanceof PyException
                        && ((PyException)t).match(_systemrestart.SystemRestart)) {
                        // Shutdown this instance...
                        shouldRestart = true;
                        shutdownInterpreter();
                        interp.cleanup();
                        // ..reset the state...
                        Py.setSystemState(new PySystemState());
                        // ...and start again
                        return;
                    } else {
                        Py.printException(t);
                        interp.cleanup();
View Full Code Here

Examples of org.python.core.PySystemState

        PySystemState.add_extdir(rootPath + "WEB-INF" + File.separator + "lib", true);
    }

    protected static PythonInterpreter createInterpreter(ServletContext servletContext) {
        String rootPath = getRootPath(servletContext);
        PySystemState sys = new PySystemState();
        PythonInterpreter interp = new PythonInterpreter(Py.newStringMap(), sys);
        sys.path.append(new PyString(rootPath));

        String modulesDir = rootPath + "WEB-INF" + File.separator + "jython";
        sys.path.append(new PyString(modulesDir));
View Full Code Here

Examples of org.python.core.PySystemState

        return Py.tojava(val, javaclass);
    }

    public void cleanup() {
        setSystemState();
        PySystemState sys = Py.getSystemState();
        sys.callExitFunc();
        try {
            sys.stdout.invoke("flush");
        } catch (PyException pye) {
            // fall through
        }
        try {
            sys.stderr.invoke("flush");
        } catch (PyException pye) {
            // fall through
        }
        sys.cleanup();
    }
View Full Code Here

Examples of org.python.core.PySystemState

                    Properties postProperties,
                    String[] argv,
                    ClassLoader classLoader,
                    ExtensiblePyObjectAdapter adapter) {
        postProperties.put(PySystemState.PYTHON_CACHEDIR_SKIP, "true");
        PySystemState defaultState =
            PySystemState.doInitialize(preProperties, postProperties, argv, classLoader, adapter);
        defaultState.path.append(Py.newString("/from_SyspathAppendingInitializer_with_love"));
    }
View Full Code Here

Examples of org.python.core.PySystemState

    @Override
    public void init() throws ServletException {
        try {
            Properties props = readConfiguration();
            PythonInterpreter.initialize(System.getProperties(), props, new String[0]);
            PySystemState systemState = new PySystemState();
            interp = new PythonInterpreter(null, systemState);
            setupEnvironment(interp, props, systemState);
            try {
                interp.exec("from modjy.modjy import " + MODJY_PYTHON_CLASSNAME);
            } catch (PyException ix) {
View Full Code Here

Examples of org.python.core.PySystemState

    private PythonInterpreter interp;

    @Override
    protected void setUp() throws Exception {
        PySystemState sys = new PySystemState();
        interp = new PythonInterpreter(new PyStringMap(), sys);
    }
View Full Code Here

Examples of org.python.core.PySystemState

    private PythonInterpreter interp;

    @Override
    protected void setUp() throws Exception {
        PySystemState sys = new PySystemState();
        interp = new PythonInterpreter(new PyStringMap(), sys);
    }
View Full Code Here

Examples of org.python.core.PySystemState

    private PythonInterpreter interp;

    @Override
    protected void setUp() throws Exception {
        interp = new PythonInterpreter(new PyStringMap(), new PySystemState());
        interp.exec("from java.io import Serializable");
        interp.exec("class Test(Serializable): pass");
        interp.exec("x = Test()");
    }
View Full Code Here

Examples of org.python.core.PySystemState

    private PythonInterpreter interp;

    @Override
    protected void setUp() throws Exception {
        interp = new PythonInterpreter(new PyStringMap(), new PySystemState());
    }
View Full Code Here

Examples of org.python.core.PySystemState

        }
        Object o = file.__tojava__(InputStream.class);
        if (o == Py.NoConversion) {
            throw Py.TypeError("must be a file-like object");
        }
        PySystemState sys = Py.getSystemState();
        String compiledFilename =
                org.python.core.imp.makeCompiledFilename(sys.getPath(filename));
        mod = org.python.core.imp.createFromSource(modname.intern(), (InputStream)o,
                                                   filename, compiledFilename);
        PyObject modules = sys.modules;
        modules.__setitem__(modname.intern(), mod);
        return mod;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.