Package org.python.core

Examples of org.python.core.PyObject


        // Dummy exec in order to speed up response on first command
        exec("2");
        // System.err.println("interp2");
        boolean more = false;
        while (true) {
            PyObject prompt = more ? systemState.ps2 : systemState.ps1;
            String line;
            try {
                line = raw_input(prompt);
            } catch (PyException exc) {
                if (!Py.matchException(exc, Py.EOFError))
View Full Code Here


            String file = StringUtils.replaceAllSlashes(module.getFile().getAbsolutePath());
            interpreter.set("visitor", this);

            List<String> splitInLines = StringUtils.splitInLines(document.get());
            interpreter.set("lines", splitInLines);
            PyObject tempPep8 = pep8;
            if (tempPep8 != null) {
                interpreter.set("pep8", tempPep8);
            } else {
                interpreter.set("pep8", Py.None);
            }
View Full Code Here

                codecs.decoding_error("charmap", v, errors, "ordinal not in range(255)");
                i++;
                continue;
            }

            PyObject w = Py.newInteger(ch);
            PyObject x = mapping.__finditem__(w);
            if (x == null) {
                /* No mapping found: default to Latin-1 mapping if possible */
                v.append(ch);
                continue;
            }

            /* Apply mapping */
            if (x instanceof PyInteger) {
                int value = ((PyInteger) x).getValue();
                if (value < 0 || value > 65535)
                    throw Py.TypeError("character mapping must be in range(65535)");
                v.append((char) value);
            } else if (x == Py.None) {
                codecs.decoding_error("charmap", v, errors, "character maps to <undefined>");
            } else if (x instanceof PyString) {
                v.append(x.toString());
            } else {
                /* wrong return value */
                throw Py.TypeError("character mapping must return integer, " + "None or unicode");
            }
        }
View Full Code Here

        int size = str.length();
        StringBuffer v = new StringBuffer(size);

        for (int i = 0; i < size; i++) {
            char ch = str.charAt(i);
            PyObject w = Py.newInteger(ch);
            PyObject x = mapping.__finditem__(w);
            if (x == null) {
                /* No mapping found: default to Latin-1 mapping if possible */
                if (ch < 256)
                    v.append(ch);
                else
                    codecs.encoding_error("charmap", v, errors, "missing character mapping");
                continue;
            }
            if (x instanceof PyInteger) {
                int value = ((PyInteger) x).getValue();
                if (value < 0 || value > 255)
                    throw Py.TypeError("character mapping must be in range(256)");
                v.append((char) value);
            } else if (x == Py.None) {
                codecs.encoding_error("charmap", v, errors, "character maps to <undefined>");
            } else if (x instanceof PyString) {
                v.append(x.toString());
            } else {
                /* wrong return value */
                throw Py.TypeError("character mapping must return " + "integer, None or unicode");
            }
        }
View Full Code Here

    public static PyObject find_module(String name) {
        return find_module(name, null);
    }

    public static PyObject load_source(String modname, String filename) {
        PyObject mod = Py.None;
        //XXX: bufsize is ignored in PyFile now, but look 3rd arg if this ever changes.
        PyFile file = new PyFile(filename, "r", 1024);
        Object o = file.__tojava__(InputStream.class);
        if (o == Py.NoConversion) {
            throw Py.TypeError("must be a file-like object");
        }
        try {
            mod = org.python.core.imp.createFromSource(modname.intern(), FileUtil.readBytes((InputStream) o),
                    filename.toString());
        } catch (IOException e) {
            throw Py.IOError(e);
        }
        PyObject modules = Py.getSystemState().modules;
        modules.__setitem__(modname.intern(), mod);
        return mod;
    }
View Full Code Here

    public static PyObject find_module(String name, PyObject path) {
        if (path == null || path == Py.None) {
            path = Py.getSystemState().path;
        }

        PyObject iter = path.__iter__();
        for (PyObject p = null; (p = iter.__iternext__()) != null;) {
            ModuleInfo mi = findFromSource(name, p, false);
            if (mi == null) {
                continue;
            }
            return new PyTuple(
View Full Code Here

        }
        throw Py.ImportError("No module named " + name);
    }

    public static PyObject load_module(String name, PyObject file, PyObject filename, PyTuple data) {
        PyObject mod = Py.None;
        int type = ((PyInteger) data.__getitem__(2).__int__()).getValue();
        while (mod == Py.None) {
            Object o = file.__tojava__(InputStream.class);
            if (o == Py.NoConversion) {
                throw Py.TypeError("must be a file-like object");
            }
            switch (type) {
                case PY_SOURCE:
                    try {
                        mod = org.python.core.imp.createFromSource(name.intern(), FileUtil.readBytes((InputStream) o),
                                filename.toString());
                    } catch (IOException e1) {
                        throw Py.IOError(e1);
                    }
                    break;
                case PY_COMPILED:
                    try {
                        mod = org.python.core.imp.loadFromCompiled(name.intern(), FileUtil.readBytes((InputStream) o),
                                filename.toString());
                    } catch (IOException e) {
                        throw Py.IOError(e);
                    }
                    break;
                case PKG_DIRECTORY:
                    PyModule m = org.python.core.imp.addModule(name);
                    m.__dict__.__setitem__("__path__", new PyList(new PyObject[] { filename }));
                    m.__dict__.__setitem__("__file__", filename);
                    ModuleInfo mi = findFromSource(name, filename, true);
                    type = mi.type;
                    file = mi.file;
                    filename = new PyString(mi.filename);
                    break;
                default:
                    throw Py.ImportError("No module named " + name);
            }
        }
        PyObject modules = Py.getSystemState().modules;
        modules.__setitem__(name.intern(), mod);
        return mod;
    }
View Full Code Here

        dict = subtype.instDict();
    }

    public PyString __str__() {
        PyType self_type = getType();
        PyObject impl = self_type.lookup("__str__");
        if (impl != null) {
            PyObject res = impl.__get__(this, self_type).__call__();
            if (res instanceof PyString)
                return (PyString) res;
            throw Py.TypeError("__str__" + " should return a " + "string");
        }
        return super.__str__();
View Full Code Here

        return super.__str__();
    }

    public PyString __repr__() {
        PyType self_type = getType();
        PyObject impl = self_type.lookup("__repr__");
        if (impl != null) {
            PyObject res = impl.__get__(this, self_type).__call__();
            if (res instanceof PyString)
                return (PyString) res;
            throw Py.TypeError("__repr__" + " should return a " + "string");
        }
        return super.__repr__();
View Full Code Here

        return super.__repr__();
    }

    public PyString __hex__() {
        PyType self_type = getType();
        PyObject impl = self_type.lookup("__hex__");
        if (impl != null) {
            PyObject res = impl.__get__(this, self_type).__call__();
            if (res instanceof PyString)
                return (PyString) res;
            throw Py.TypeError("__hex__" + " should return a " + "string");
        }
        return super.__hex__();
View Full Code Here

TOP

Related Classes of org.python.core.PyObject

Copyright © 2018 www.massapicom. 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.