Class imp


  • public class imp
    extends java.lang.Object
    Utility functions for "import" support. Note that this class tries to match the names of the corresponding functions from CPython's Python/import.c. In these cases we use CPython's function naming style (underscores and all lowercase) instead of Java's typical camelCase style so that it's easier to compare with import.c.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  imp.CodeData  
      static class  imp.CodeImport  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_LEVEL  
      static int NO_MTIME  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static PyModule addModule​(java.lang.String name)
      If the given name is found in sys.modules, the entry from there is returned.
      static java.lang.String cacheCompiledSource​(java.lang.String sourceFilename, java.lang.String compiledFilename, byte[] compiledSource)
      Stores the bytes in compiledSource in compiledFilename.
      static boolean caseok​(java.io.File file, java.lang.String filename)  
      static byte[] compileSource​(java.lang.String name, java.io.File file)  
      static byte[] compileSource​(java.lang.String name, java.io.File file, java.lang.String sourceFilename)  
      static byte[] compileSource​(java.lang.String name, java.io.File file, java.lang.String sourceFilename, java.lang.String compiledFilename)  
      static byte[] compileSource​(java.lang.String name, java.io.InputStream fp, java.lang.String filename)  
      static byte[] compileSource​(java.lang.String name, java.io.InputStream fp, java.lang.String filename, long mtime)  
      static PyObject createFromCode​(java.lang.String name, PyCode c)
      Returns a module with the given name whose contents are the results of running c.
      static PyObject createFromCode​(java.lang.String name, PyCode c, java.lang.String moduleLocation)
      Returns a module with the given name whose contents are the results of running c.
      static PyObject createFromSource​(java.lang.String name, java.io.InputStream fp, java.lang.String filename)  
      static PyObject createFromSource​(java.lang.String name, java.io.InputStream fp, java.lang.String filename, java.lang.String outFilename)  
      static PyObject createFromSource​(java.lang.String name, java.io.InputStream fp, java.lang.String filename, java.lang.String outFilename, long mtime)  
      static int getAPIVersion()  
      static PyObject getImporter​(PyObject p)  
      static java.lang.ClassLoader getParentClassLoader()
      Selects the parent class loader for Jython, to be used for dynamically loaded classes and resources.
      static java.lang.ClassLoader getSyspathJavaLoader()  
      static void importAll​(java.lang.String mod, PyFrame frame)
      Deprecated.
      static void importAll​(java.lang.String mod, PyFrame frame, int level)
      Called from jython generated code when a statement like "from spam.eggs import *" is executed.
      static void importAll​(PyObject module, PyFrame frame)  
      static PyObject[] importFrom​(java.lang.String mod, java.lang.String[] names, PyFrame frame)
      Deprecated.
      use importFrom with level param.
      static PyObject[] importFrom​(java.lang.String mod, java.lang.String[] names, PyFrame frame, int level)
      Called from jython generated code when a statement like "from spam.eggs import foo, bar" is executed.
      static PyObject[] importFromAs​(java.lang.String mod, java.lang.String[] names, java.lang.String[] asnames, PyFrame frame, int level)
      Called from jython generated code when a statement like "from spam.eggs import foo as spam" is executed.
      static PyObject[] importFromAs​(java.lang.String mod, java.lang.String[] names, PyFrame frame)
      Deprecated.
      use importFromAs with level param.
      static PyObject importName​(java.lang.String name, boolean top)
      Import a module by name.
      static PyObject importName​(java.lang.String name, boolean top, PyObject modDict, PyObject fromlist, int level)
      Import a module by name.
      static PyObject importOne​(java.lang.String mod, PyFrame frame)
      Deprecated.
      static PyObject importOne​(java.lang.String mod, PyFrame frame, int level)
      Called from jython generated code when a statement like "import spam" is executed.
      static PyObject importOneAs​(java.lang.String mod, PyFrame frame)
      Deprecated.
      static PyObject importOneAs​(java.lang.String mod, PyFrame frame, int level)
      Called from jython generated code when a statement like "import spam as foo" is executed.
      static PyObject load​(java.lang.String name)
      Load the module by name.
      static PyObject loadFromCompiled​(java.lang.String name, java.io.InputStream stream, java.lang.String sourceName, java.lang.String compiledName)  
      static java.lang.String makeCompiledFilename​(java.lang.String filename)
      Remove the last three characters of a file name and add the compiled suffix "$py.class".
      static byte[] readCode​(java.lang.String name, java.io.InputStream fp, boolean testing)  
      static byte[] readCode​(java.lang.String name, java.io.InputStream fp, boolean testing, long mtime)  
      static imp.CodeData readCodeData​(java.lang.String name, java.io.InputStream fp, boolean testing)  
      static imp.CodeData readCodeData​(java.lang.String name, java.io.InputStream fp, boolean testing, long mtime)  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getSyspathJavaLoader

        public static java.lang.ClassLoader getSyspathJavaLoader()
      • getParentClassLoader

        public static java.lang.ClassLoader getParentClassLoader()
        Selects the parent class loader for Jython, to be used for dynamically loaded classes and resources. Chooses between the current and context classloader based on the following criteria:
        • If both are the same classloader, return that classloader.
        • If either is null, then the non-null one is selected.
        • If both are not null, and a parent/child relationship can be determined, then the child is selected.
        • If both are not null and not on a parent/child relationship, then the current class loader is returned (since it is likely for the context class loader to not see the Jython classes)
        Returns:
        the parent class loader for Jython or null if both the current and context classloaders are null.
      • addModule

        public static PyModule addModule​(java.lang.String name)
        If the given name is found in sys.modules, the entry from there is returned. Otherwise a new PyModule is created for the name and added to sys.modules
      • readCode

        public static byte[] readCode​(java.lang.String name,
                                      java.io.InputStream fp,
                                      boolean testing)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • readCode

        public static byte[] readCode​(java.lang.String name,
                                      java.io.InputStream fp,
                                      boolean testing,
                                      long mtime)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • readCodeData

        public static imp.CodeData readCodeData​(java.lang.String name,
                                                java.io.InputStream fp,
                                                boolean testing)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • readCodeData

        public static imp.CodeData readCodeData​(java.lang.String name,
                                                java.io.InputStream fp,
                                                boolean testing,
                                                long mtime)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • compileSource

        public static byte[] compileSource​(java.lang.String name,
                                           java.io.File file)
      • compileSource

        public static byte[] compileSource​(java.lang.String name,
                                           java.io.File file,
                                           java.lang.String sourceFilename)
      • compileSource

        public static byte[] compileSource​(java.lang.String name,
                                           java.io.File file,
                                           java.lang.String sourceFilename,
                                           java.lang.String compiledFilename)
      • makeCompiledFilename

        public static java.lang.String makeCompiledFilename​(java.lang.String filename)
        Remove the last three characters of a file name and add the compiled suffix "$py.class".
      • cacheCompiledSource

        public static java.lang.String cacheCompiledSource​(java.lang.String sourceFilename,
                                                           java.lang.String compiledFilename,
                                                           byte[] compiledSource)
        Stores the bytes in compiledSource in compiledFilename. If compiledFilename is null, it's set to the results of makeCompiledFilename(sourcefileName). If sourceFilename is null or set to UNKNOWN_SOURCEFILE, then null is returned.
        Returns:
        the compiledFilename eventually used; or null if a compiledFilename couldn't be determined or if an error was thrown while writing to the cache file.
      • compileSource

        public static byte[] compileSource​(java.lang.String name,
                                           java.io.InputStream fp,
                                           java.lang.String filename)
      • compileSource

        public static byte[] compileSource​(java.lang.String name,
                                           java.io.InputStream fp,
                                           java.lang.String filename,
                                           long mtime)
      • createFromSource

        public static PyObject createFromSource​(java.lang.String name,
                                                java.io.InputStream fp,
                                                java.lang.String filename)
      • createFromSource

        public static PyObject createFromSource​(java.lang.String name,
                                                java.io.InputStream fp,
                                                java.lang.String filename,
                                                java.lang.String outFilename)
      • createFromSource

        public static PyObject createFromSource​(java.lang.String name,
                                                java.io.InputStream fp,
                                                java.lang.String filename,
                                                java.lang.String outFilename,
                                                long mtime)
      • createFromCode

        public static PyObject createFromCode​(java.lang.String name,
                                              PyCode c)
        Returns a module with the given name whose contents are the results of running c. __file__ is set to whatever is in c.
      • createFromCode

        public static PyObject createFromCode​(java.lang.String name,
                                              PyCode c,
                                              java.lang.String moduleLocation)
        Returns a module with the given name whose contents are the results of running c. Sets __file__ on the module to be moduleLocation unless moduleLocation is null. If c comes from a local .py file or compiled $py.class class moduleLocation should be the result of running new File(moduleLocation).getAbsolutePath(). If c comes from a remote file or is a jar moduleLocation should be the full uri for c.
      • loadFromCompiled

        public static PyObject loadFromCompiled​(java.lang.String name,
                                                java.io.InputStream stream,
                                                java.lang.String sourceName,
                                                java.lang.String compiledName)
      • caseok

        public static boolean caseok​(java.io.File file,
                                     java.lang.String filename)
      • load

        public static PyObject load​(java.lang.String name)
        Load the module by name. Upon loading the module it will be added to sys.modules.
        Parameters:
        name - the name of the module to load
        Returns:
        the loaded module
      • importName

        public static PyObject importName​(java.lang.String name,
                                          boolean top)
        Import a module by name.
        Parameters:
        name - the name of the package to import
        top - if true, return the top module in the name, otherwise the last
        Returns:
        an imported module (Java or Python)
      • importName

        public static PyObject importName​(java.lang.String name,
                                          boolean top,
                                          PyObject modDict,
                                          PyObject fromlist,
                                          int level)
        Import a module by name. This is the default call for __builtin__.__import__.
        Parameters:
        name - the name of the package to import
        top - if true, return the top module in the name, otherwise the last
        modDict - the __dict__ of an already imported module
        Returns:
        an imported module (Java or Python)
      • importOne

        @Deprecated
        public static PyObject importOne​(java.lang.String mod,
                                         PyFrame frame)
        Deprecated.
        Called from jython generated code when a statement like "import spam" is executed.
      • importOne

        public static PyObject importOne​(java.lang.String mod,
                                         PyFrame frame,
                                         int level)
        Called from jython generated code when a statement like "import spam" is executed.
      • importOneAs

        @Deprecated
        public static PyObject importOneAs​(java.lang.String mod,
                                           PyFrame frame)
        Deprecated.
        Called from jython generated code when a statement like "import spam as foo" is executed.
      • importOneAs

        public static PyObject importOneAs​(java.lang.String mod,
                                           PyFrame frame,
                                           int level)
        Called from jython generated code when a statement like "import spam as foo" is executed.
      • importFrom

        @Deprecated
        public static PyObject[] importFrom​(java.lang.String mod,
                                            java.lang.String[] names,
                                            PyFrame frame)
        Deprecated.
        use importFrom with level param.
        replaced by importFrom with level param. Kept for backwards compatibility.
      • importFrom

        public static PyObject[] importFrom​(java.lang.String mod,
                                            java.lang.String[] names,
                                            PyFrame frame,
                                            int level)
        Called from jython generated code when a statement like "from spam.eggs import foo, bar" is executed.
      • importFromAs

        @Deprecated
        public static PyObject[] importFromAs​(java.lang.String mod,
                                              java.lang.String[] names,
                                              PyFrame frame)
        Deprecated.
        use importFromAs with level param.
        replaced by importFromAs with level param. Kept for backwards compatibility.
      • importFromAs

        public static PyObject[] importFromAs​(java.lang.String mod,
                                              java.lang.String[] names,
                                              java.lang.String[] asnames,
                                              PyFrame frame,
                                              int level)
        Called from jython generated code when a statement like "from spam.eggs import foo as spam" is executed.
      • importAll

        public static void importAll​(java.lang.String mod,
                                     PyFrame frame,
                                     int level)
        Called from jython generated code when a statement like "from spam.eggs import *" is executed.
      • importAll

        @Deprecated
        public static void importAll​(java.lang.String mod,
                                     PyFrame frame)
        Deprecated.
      • importAll

        public static void importAll​(PyObject module,
                                     PyFrame frame)
      • getAPIVersion

        public static int getAPIVersion()