Package org.python.core
Class imp
java.lang.Object
org.python.core.imp
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
Modifier and TypeClassDescriptionstatic class
A bundle of a file name, the file's content and a last modified time, with no behaviour. -
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic PyModule
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
Check that the canonical name offile
matchesfilename
, case-sensitively, even when the OS platform is case-insensitive.static byte[]
compileSource
(String name, File source) Compile Python source in file to a class file represented by a byte array.static byte[]
compileSource
(String name, File source, String filename) Compile Python source in file to a class file represented by a byte array.static byte[]
compileSource
(String name, File source, String sourceFilename, String compiledFilename) Deprecated.static byte[]
compileSource
(String name, InputStream source, String filename) Compile Python source to a class file represented by a byte array.static byte[]
compileSource
(String name, InputStream source, String filename, long mtime) Compile Python source to a class file represented by a byte array.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) Return a Python module with the givenname
whose attributes are the result of runningPyCode 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) Compile Jython source (as anInputStream
) to a module.static String
For project internal use, equivalent tofileSystemDecode(p, true)
(seefileSystemDecode(PyObject, boolean)
).static 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 eithernull
orPy.ImportError(String)
calls (and a log message), which usually results in quiet failure.static int
static PyObject
static ClassLoader
Selects the parent class loader for Jython, to be used for dynamically loaded classes and resources.static ClassLoader
static void
Deprecated.static void
Called from jython generated code when a statement like "from spam.eggs import *" is executed.static void
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, 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
(String mod, String[] names, PyFrame frame) Deprecated.use importFromAs with level param.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
Deprecated.static PyObject
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 the module by name.static PyObject
loadFromCompiled
(String name, InputStream stream, String sourceName, String compiledName) static String
makeCompiledFilename
(String filename) Remove the last three characters of a file name and add the compiled suffix "$py.class".static byte[]
readCode
(String name, 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
(String name, InputStream fp, boolean testing, long sourceLastModified) AsreadCodeData(String, InputStream, boolean, long)
but return only the class file bytes as an array.static imp.CodeData
readCodeData
(String name, InputStream fp, boolean testing) AsreadCodeData(String, InputStream, boolean, long)
but do not check last-modified time.static imp.CodeData
readCodeData
(String name, InputStream fp, boolean testing, long sourceLastModified) Create aimp.CodeData
object 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 Details
-
NO_MTIME
public static final int NO_MTIME- See Also:
-
DEFAULT_LEVEL
public static final int DEFAULT_LEVEL- See Also:
-
-
Method Details
-
getSyspathJavaLoader
-
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
If the given name is found in sys.modules, the entry from there is returned. Otherwise a newPyModule
is 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
AsreadCodeData(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 anull
not an exception- Returns:
- the class file bytes as an array or
null
on failure (iftesting
). - Throws:
PyException
-ImportError
on API or last-modified time mismatchIOException
- from read failures
-
readCode
public static byte[] readCode(String name, InputStream fp, boolean testing, long sourceLastModified) throws IOException AsreadCodeData(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 anull
not an exceptionsourceLastModified
- time expected to matchMTime
annotation in the class file- Returns:
- the class file bytes as an array or
null
on failure (iftesting
). - Throws:
PyException
-ImportError
on API or last-modified time mismatchIOException
- from read failures
-
readCodeData
public static imp.CodeData readCodeData(String name, InputStream fp, boolean testing) throws 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 anull
not an exception- Returns:
- the
CodeData
bundle ornull
on failure (iftesting
). - Throws:
PyException
-ImportError
on API mismatchIOException
- from read failures
-
readCodeData
public static imp.CodeData readCodeData(String name, InputStream fp, boolean testing, long sourceLastModified) throws IOException, PyException Create aimp.CodeData
object 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.MTime
annotation 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 anull
not an exceptionsourceLastModified
- time expected to matchMTime
annotation in the class file- Returns:
- the
CodeData
bundle ornull
on failure (iftesting
). - Throws:
PyException
-ImportError
on API or last-modified time mismatchIOException
- from read failures
-
compileSource
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
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 (ornull
to use that in source)- Returns:
- Java byte code as array
-
compileSource
@Deprecated public static byte[] compileSource(String name, File source, String sourceFilename, 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 (ornull
to use that in source)compiledFilename
- ignored (huh?)- Returns:
- Java byte code as array
-
makeCompiledFilename
Remove the last three characters of a file name and add the compiled suffix "$py.class". -
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
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 (ornull
if unknown)- Returns:
- Java byte code as array
-
compileSource
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 (ornull
if unknown)mtime
- last-modified time of source, to annotate class- Returns:
- Java byte code as array
-
createFromSource
-
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) 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
Returns a module with the given name whose contents are the results of running c. __file__ is set to whatever is in c. -
createFromCode
Return a Python module with the givenname
whose attributes are the result of runningPyCode c
. IfmoduleLocation != null
it is used to set__file__
.In normal circumstances, if
c
comes from a local.py
file or compiled$py.class
class the caller should should setmoduleLocation
to something likenew File(moduleLocation).getAbsolutePath()
. Ifc
comes from a remote file or is a JAR,moduleLocation
should 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
-
getImporter
-
loadFromCompiled
public static PyObject loadFromCompiled(String name, InputStream stream, String sourceName, String compiledName) -
caseok
Check that the canonical name offile
matchesfilename
, 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 thatfile
was derived from attempting to findfilename
, so it returnstrue
on a case-sensitive platform.Algorithmically, we return
true
if any of the following is true:Options.caseok
istrue
(default isfalse
).- The platform is case sensitive (according to
PlatformUtil.isCaseInsensitive()
) - The name part of the canonical path of
file
starts withfilename
- The name of any sibling (in the same directory as)
file
equalsfilename
- Parameters:
file
- to be testedfilename
- to be matched- Returns:
file
matchesfilename
-
load
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
A wrapper forPy.fileSystemDecode(PyObject)
for project internal use within the import mechanism to convert decoding errors that occur during import to eithernull
orPy.ImportError(String)
calls (and a log message), which usually results in quiet failure.- Parameters:
p
- assumed to be a (partial) file pathraiseImportError
- if true andp
cannot be decoded raiseImportError
.- Returns:
- String form of the object
p
(ornull
).
-
fileSystemDecode
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
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(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.Called from jython generated code when a statement like "import spam" is executed. -
importOne
Called from jython generated code when a statement like "import spam" is executed. -
importOneAs
Deprecated.Called from jython generated code when a statement like "import spam as foo" is executed. -
importOneAs
Called from jython generated code when a statement like "import spam as foo" is executed. -
importFrom
Deprecated.use importFrom with level param.replaced by importFrom with level param. Kept for backwards compatibility. -
importFrom
Called from jython generated code when a statement like "from spam.eggs import foo, bar" is executed. -
importFromAs
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
Called from jython generated code when a statement like "from spam.eggs import *" is executed. -
importAll
Deprecated. -
importAll
-
getAPIVersion
public static int getAPIVersion()
-
compileSource(String, File, String)
instead.