Package org.python.core
Class imp
- java.lang.Object
-
- org.python.core.imp
-
public class imp extends java.lang.ObjectUtility 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 classimp.CodeDataA bundle of a file name, the file's content and a last modified time, with no behaviour.
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_LEVELstatic intNO_MTIME
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static PyModuleaddModule(java.lang.String name)If the given name is found in sys.modules, the entry from there is returned.static java.lang.StringcacheCompiledSource(java.lang.String sourceFilename, java.lang.String compiledFilename, byte[] compiledSource)Stores the bytes in compiledSource in compiledFilename.static booleancaseok(java.io.File file, java.lang.String filename)Check that the canonical name offilematchesfilename, case-sensitively, even when the OS platform is case-insensitive.static byte[]compileSource(java.lang.String name, java.io.File source)Compile Python source in file to a class file represented by a byte array.static byte[]compileSource(java.lang.String name, java.io.File source, java.lang.String filename)Compile Python source in file to a class file represented by a byte array.static byte[]compileSource(java.lang.String name, java.io.File source, java.lang.String sourceFilename, java.lang.String compiledFilename)Deprecated.UsecompileSource(String, File, String)instead.static byte[]compileSource(java.lang.String name, java.io.InputStream source, java.lang.String filename)Compile Python source to a class file represented by a byte array.static byte[]compileSource(java.lang.String name, java.io.InputStream source, java.lang.String filename, long mtime)Compile Python source to a class file represented by a byte array.static PyObjectcreateFromCode(java.lang.String name, PyCode c)Returns a module with the given name whose contents are the results of running c.static PyObjectcreateFromCode(java.lang.String name, PyCode c, java.lang.String moduleLocation)Return a Python module with the givennamewhose attributes are the result of runningPyCode c.static PyObjectcreateFromSource(java.lang.String name, java.io.InputStream fp, java.lang.String filename)static PyObjectcreateFromSource(java.lang.String name, java.io.InputStream fp, java.lang.String filename, java.lang.String outFilename)static PyObjectcreateFromSource(java.lang.String name, java.io.InputStream fp, java.lang.String filename, java.lang.String outFilename, long mtime)Compile Jython source (as anInputStream) to a module.static java.lang.StringfileSystemDecode(PyObject p)For project internal use, equivalent tofileSystemDecode(p, true)(seefileSystemDecode(PyObject, boolean)).static java.lang.StringfileSystemDecode(PyObject p, boolean raiseImportError)A wrapper forPy.fileSystemDecode(PyObject)for project internal use within the import mechanism to convert decoding errors that occur during import to eithernullorPy.ImportError(String)calls (and a log message), which usually results in quiet failure.static intgetAPIVersion()static PyObjectgetImporter(PyObject p)static java.lang.ClassLoadergetParentClassLoader()Selects the parent class loader for Jython, to be used for dynamically loaded classes and resources.static java.lang.ClassLoadergetSyspathJavaLoader()static voidimportAll(java.lang.String mod, PyFrame frame)Deprecated.static voidimportAll(java.lang.String mod, PyFrame frame, int level)Called from jython generated code when a statement like "from spam.eggs import *" is executed.static voidimportAll(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 PyObjectimportName(java.lang.String name, boolean top)Import a module by name.static PyObjectimportName(java.lang.String name, boolean top, PyObject modDict, PyObject fromlist, int level)Import a module by name.static PyObjectimportOne(java.lang.String mod, PyFrame frame)Deprecated.static PyObjectimportOne(java.lang.String mod, PyFrame frame, int level)Called from jython generated code when a statement like "import spam" is executed.static PyObjectimportOneAs(java.lang.String mod, PyFrame frame)Deprecated.static PyObjectimportOneAs(java.lang.String mod, PyFrame frame, int level)Called from jython generated code when a statement like "import spam as foo" is executed.static PyObjectload(java.lang.String name)Load the module by name.static PyObjectloadFromCompiled(java.lang.String name, java.io.InputStream stream, java.lang.String sourceName, java.lang.String compiledName)static java.lang.StringmakeCompiledFilename(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)AsreadCodeData(String, InputStream, boolean, long)but do not check last-modified time and return only the class file bytes as an array.static byte[]readCode(java.lang.String name, java.io.InputStream fp, boolean testing, long sourceLastModified)AsreadCodeData(String, InputStream, boolean, long)but return only the class file bytes as an array.static imp.CodeDatareadCodeData(java.lang.String name, java.io.InputStream fp, boolean testing)AsreadCodeData(String, InputStream, boolean, long)but do not check last-modified time.static imp.CodeDatareadCodeData(java.lang.String name, java.io.InputStream fp, boolean testing, long sourceLastModified)Create aimp.CodeDataobject bundling the contents of a class file (given as a stream), source-last-modified time supplied, and the name of the file taken from annotations on the class.
-
-
-
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
-
-
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 class loader based on the following criteria:- If both are the same class loader, return that class loader.
- 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 class loaders 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 newPyModuleis created for the name and added tosys.modules. Creating the module does not execute the body of the module to initialise its attributes.- Parameters:
name- fully-qualified name of the module- Returns:
- created
PyModule
-
readCode
public static byte[] readCode(java.lang.String name, java.io.InputStream fp, boolean testing) throws java.io.IOExceptionAsreadCodeData(String, InputStream, boolean, long)but do not check last-modified time and return only the class file bytes as an array.- Parameters:
name- of source file (used for identification in error/log messages)fp- stream from which to read class file (closed when read)testing- iftrue, failures are signalled by anullnot an exception- Returns:
- the class file bytes as an array or
nullon failure (iftesting). - Throws:
PyException-ImportErroron API or last-modified time mismatchjava.io.IOException- from read failures
-
readCode
public static byte[] readCode(java.lang.String name, java.io.InputStream fp, boolean testing, long sourceLastModified) throws java.io.IOExceptionAsreadCodeData(String, InputStream, boolean, long)but return only the class file bytes as an array.- Parameters:
name- of source file (used for identification in error/log messages)fp- stream from which to read class file (closed when read)testing- iftrue, failures are signalled by anullnot an exceptionsourceLastModified- time expected to matchMTimeannotation in the class file- Returns:
- the class file bytes as an array or
nullon failure (iftesting). - Throws:
PyException-ImportErroron API or last-modified time mismatchjava.io.IOException- from read failures
-
readCodeData
public static imp.CodeData readCodeData(java.lang.String name, java.io.InputStream fp, boolean testing) throws java.io.IOException
AsreadCodeData(String, InputStream, boolean, long)but do not check last-modified time.- Parameters:
name- of source file (used for identification in error/log messages)fp- stream from which to read class file (closed when read)testing- iftrue, failures are signalled by anullnot an exception- Returns:
- the
CodeDatabundle ornullon failure (iftesting). - Throws:
PyException-ImportErroron API mismatchjava.io.IOException- from read failures
-
readCodeData
public static imp.CodeData readCodeData(java.lang.String name, java.io.InputStream fp, boolean testing, long sourceLastModified) throws java.io.IOException, PyException
Create aimp.CodeDataobject bundling the contents of a class file (given as a stream), source-last-modified time supplied, and the name of the file taken from annotations on the class. On the way, the method checks the API version annotation matches the current process, and that theorg.python.compiler.MTimeannotation matches the source-last-modified time passed in.- Parameters:
name- of source file (used for identification in error/log messages)fp- stream from which to read class file (closed when read)testing- iftrue, failures are signalled by anullnot an exceptionsourceLastModified- time expected to matchMTimeannotation in the class file- Returns:
- the
CodeDatabundle ornullon failure (iftesting). - Throws:
PyException-ImportErroron API or last-modified time mismatchjava.io.IOException- from read failures
-
compileSource
public static byte[] compileSource(java.lang.String name, java.io.File source)Compile Python source in file to a class file represented by a byte array.- Parameters:
name- of module (class name will be name$py)source- file containing the source- Returns:
- Java byte code as array
-
compileSource
public static byte[] compileSource(java.lang.String name, java.io.File source, java.lang.String filename)Compile Python source in file to a class file represented by a byte array.- Parameters:
name- of module (class name will be name$py)source- file containing the sourcefilename- explicit source file name (ornullto use that in source)- Returns:
- Java byte code as array
-
compileSource
@Deprecated public static byte[] compileSource(java.lang.String name, java.io.File source, java.lang.String sourceFilename, java.lang.String compiledFilename)Deprecated.UsecompileSource(String, File, String)instead.Compile Python source in file to a class file represented by a byte array.- Parameters:
name- of module (class name will be name$py)source- file containing the sourcesourceFilename- explicit source file name (ornullto use that in source)compiledFilename- ignored (huh?)- Returns:
- Java byte code as array
-
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 source, java.lang.String filename)Compile Python source to a class file represented by a byte array.- Parameters:
name- of module (class name will be name$py)source- open input stream (will be closed)filename- of source (ornullif unknown)- Returns:
- Java byte code as array
-
compileSource
public static byte[] compileSource(java.lang.String name, java.io.InputStream source, java.lang.String filename, long mtime)Compile Python source to a class file represented by a byte array.- Parameters:
name- of module (class name will be name$py)source- open input stream (will be closed)filename- of source (ornullif unknown)mtime- last-modified time of source, to annotate class- Returns:
- Java byte code as array
-
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)
Compile Jython source (as anInputStream) to a module.- Parameters:
name- of the module to create (class will be name$py)fp- opened on the (Jython) source to compile (will be closed)filename- of the source backingfp(to embed in class as data)outFilename- in which to write the compiled classmtime- last modified time of the file backingfp- Returns:
- created module
-
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)
Return a Python module with the givennamewhose attributes are the result of runningPyCode c. IfmoduleLocation != nullit is used to set__file__.In normal circumstances, if
ccomes from a local.pyfile or compiled$py.classclass the caller should should setmoduleLocationto something likenew File(moduleLocation).getAbsolutePath(). Ifccomes from a remote file or is a JAR,moduleLocationshould be the full URI for that source or class.- Parameters:
name- fully-qualified name of the modulec- code supplying the modulemoduleLocation- to become__file__if notnull- Returns:
- the module object
-
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)Check that the canonical name offilematchesfilename, case-sensitively, even when the OS platform is case-insensitive. This is used to obtain as a check during import on platforms (Windows) that may be case-insensitive regarding file open. It is assumed thatfilewas derived from attempting to findfilename, so it returnstrueon a case-sensitive platform.Algorithmically, we return
trueif any of the following is true:Options.caseokistrue(default isfalse).- The platform is case sensitive (according to
PlatformUtil.isCaseInsensitive()) - The name part of the canonical path of
filestarts withfilename - The name of any sibling (in the same directory as)
fileequalsfilename
- Parameters:
file- to be testedfilename- to be matched- Returns:
filematchesfilename
-
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
-
fileSystemDecode
public static java.lang.String fileSystemDecode(PyObject p, boolean raiseImportError)
A wrapper forPy.fileSystemDecode(PyObject)for project internal use within the import mechanism to convert decoding errors that occur during import to eithernullorPy.ImportError(String)calls (and a log message), which usually results in quiet failure.- Parameters:
p- assumed to be a (partial) file pathraiseImportError- if true andpcannot be decoded raiseImportError.- Returns:
- String form of the object
p(ornull).
-
fileSystemDecode
public static java.lang.String fileSystemDecode(PyObject p)
For project internal use, equivalent tofileSystemDecode(p, true)(seefileSystemDecode(PyObject, boolean)).- Parameters:
p- assumed to be a (partial) file path- Returns:
- String form of the object
p.
-
importName
public static PyObject importName(java.lang.String name, boolean top)
Import a module by name.- Parameters:
name- the name of the package to importtop- 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 supports the default__import__()function__builtin__.__import__. Locks the import system while it operates.- Parameters:
name- the fully-qualified name of the package/module to importtop- if true, return the top module in the name, otherwise the lastmodDict- the __dict__ of the importing module (used for name in relative import)fromlist- list of names being importedlevel- 0=absolute, n>0=relative levels to go up, -1=try relative then absolute.- 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.
-
getAPIVersion
public static int getAPIVersion()
-
-