org.python.core
Class imp

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

public class imp
extends java.lang.Object

Utility functions for "import" support.


Field Summary
static int APIVersion
           
static int DEFAULT_LEVEL
           
static java.util.concurrent.locks.ReentrantLock importLock
          Synchronizes import operations
 
Method Summary
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, java.lang.String sourceFilename, java.lang.String compiledFilename)
           
static byte[] compileSource(java.lang.String name, java.io.InputStream fp, java.lang.String filename)
           
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 java.lang.ClassLoader getSyspathJavaLoader()
           
static void importAll(java.lang.String mod, PyFrame frame)
          Called from jython generated code when a statement like "from spam.eggs import *" is executed.
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, PyFrame frame)
          Deprecated. use importFromAs with level param.
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 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)
          Called from jython generated code when a statement like "import spam" is executed.
static PyObject importOneAs(java.lang.String mod, PyFrame frame)
          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)
           
static byte[] readCode(java.lang.String name, java.io.InputStream fp, boolean testing)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

APIVersion

public static final int APIVersion
See Also:
Constant Field Values

DEFAULT_LEVEL

public static final int DEFAULT_LEVEL
See Also:
Constant Field Values

importLock

public static final java.util.concurrent.locks.ReentrantLock importLock
Synchronizes import operations

Method Detail

getSyspathJavaLoader

public static java.lang.ClassLoader getSyspathJavaLoader()

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

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)

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 null is returned

Returns:
the compiledFilename eventually used or null if a compiledFilename couldn't be determined of 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)

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)

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).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(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

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


importOneAs

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


importFrom

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

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)
Called from jython generated code when a statement like "from spam.eggs import *" is executed.



Jython homepage