org.python.core
Class imp

java.lang.Object
  extended by org.python.core.imp

public class imp
extends 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.


Field Summary
static int DEFAULT_LEVEL
           
static ReentrantLock importLock
          Synchronizes import operations
static int NO_MTIME
           
 
Method Summary
static PyModule addModule(String name)
          If the given name is found in sys.modules, the entry from there is returned.
static String cacheCompiledSource(String sourceFilename, String compiledFilename, byte[] compiledSource)
          Stores the bytes in compiledSource in compiledFilename.
static boolean caseok(File file, String filename)
           
static byte[] compileSource(String name, File file)
           
static byte[] compileSource(String name, File file, String sourceFilename)
           
static byte[] compileSource(String name, File file, String sourceFilename, String compiledFilename)
           
static byte[] compileSource(String name, InputStream fp, String filename)
           
static byte[] compileSource(String name, InputStream fp, String filename, long mtime)
           
static PyObject createFromCode(String name, PyCode c)
          Returns a module with the given name whose contents are the results of running c.
static PyObject createFromCode(String name, PyCode c, String moduleLocation)
          Returns a module with the given name whose contents are the results of running c.
static PyObject createFromSource(String name, InputStream fp, String filename)
           
static PyObject createFromSource(String name, InputStream fp, String filename, String outFilename)
           
static PyObject createFromSource(String name, InputStream fp, String filename, String outFilename, long mtime)
           
static int getAPIVersion()
           
static ClassLoader getParentClassLoader()
          Selects the parent class loader for Jython, to be used for dynamically loaded classes and resources.
static ClassLoader getSyspathJavaLoader()
           
static void importAll(PyObject module, PyFrame frame)
           
static void importAll(String mod, PyFrame frame)
          Deprecated. 
static void importAll(String mod, PyFrame frame, int level)
          Called from jython generated code when a statement like "from spam.eggs import *" is executed.
static PyObject[] importFrom(String mod, String[] names, PyFrame frame)
          Deprecated. use importFrom with level param.
static PyObject[] importFrom(String mod, 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(String mod, String[] names, PyFrame frame)
          Deprecated. use importFromAs with level param.
static PyObject[] importFromAs(String mod, String[] names, 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 importName(String name, boolean top)
          Import a module by name.
static PyObject importName(String name, boolean top, PyObject modDict, PyObject fromlist, int level)
          Import a module by name.
static PyObject importOne(String mod, PyFrame frame)
          Deprecated. 
static PyObject importOne(String mod, PyFrame frame, int level)
          Called from jython generated code when a statement like "import spam" is executed.
static PyObject importOneAs(String mod, PyFrame frame)
          Deprecated. 
static PyObject importOneAs(String mod, PyFrame frame, int level)
          Called from jython generated code when a statement like "import spam as foo" is executed.
static PyObject load(String name)
          Load the module by name.
static PyObject loadFromCompiled(String name, InputStream stream, String sourceName, String compiledName)
           
static String makeCompiledFilename(String filename)
           
static byte[] readCode(String name, InputStream fp, boolean testing)
           
static byte[] readCode(String name, InputStream fp, boolean testing, long mtime)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_MTIME

public static final int NO_MTIME
See Also:
Constant Field Values

DEFAULT_LEVEL

public static final int DEFAULT_LEVEL
See Also:
Constant Field Values

importLock

public static final ReentrantLock importLock
Synchronizes import operations

Method Detail

getSyspathJavaLoader

public static ClassLoader getSyspathJavaLoader()

getParentClassLoader

public static 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:

Returns:
the parent class loader for Jython or null if both the current and context classloaders are null.

addModule

public static PyModule addModule(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(String name,
                              InputStream fp,
                              boolean testing)
                       throws IOException
Throws:
IOException

readCode

public static byte[] readCode(String name,
                              InputStream fp,
                              boolean testing,
                              long mtime)
                       throws IOException
Throws:
IOException

compileSource

public static byte[] compileSource(String name,
                                   File file)

compileSource

public static byte[] compileSource(String name,
                                   File file,
                                   String sourceFilename)

compileSource

public static byte[] compileSource(String name,
                                   File file,
                                   String sourceFilename,
                                   String compiledFilename)

makeCompiledFilename

public static String makeCompiledFilename(String filename)

cacheCompiledSource

public static String cacheCompiledSource(String sourceFilename,
                                         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(String name,
                                   InputStream fp,
                                   String filename)

compileSource

public static byte[] compileSource(String name,
                                   InputStream fp,
                                   String filename,
                                   long mtime)

createFromSource

public static PyObject createFromSource(String name,
                                        InputStream fp,
                                        String filename)

createFromSource

public static PyObject createFromSource(String name,
                                        InputStream fp,
                                        String filename,
                                        String outFilename)

createFromSource

public static PyObject createFromSource(String name,
                                        InputStream fp,
                                        String filename,
                                        String outFilename,
                                        long mtime)

createFromCode

public static PyObject createFromCode(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(String name,
                                      PyCode c,
                                      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).getAbsoultePath(). If c comes from a remote file or is a jar moduleLocation should be the full uri for c.


loadFromCompiled

public static PyObject loadFromCompiled(String name,
                                        InputStream stream,
                                        String sourceName,
                                        String compiledName)

caseok

public static boolean caseok(File file,
                             String filename)

load

public static PyObject load(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(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(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(String mod,
                                            PyFrame frame)
Deprecated. 

Called from jython generated code when a statement like "import spam" is executed.


importOne

public static PyObject importOne(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(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(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(String mod,
                                               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(String mod,
                                    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(String mod,
                                                 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(String mod,
                                      String[] names,
                                      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(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(String mod,
                                        PyFrame frame)
Deprecated. 


importAll

public static void importAll(PyObject module,
                             PyFrame frame)

getAPIVersion

public static int getAPIVersion()


Jython homepage