Top Description Inners Fields Constructors Methods
org.python.core

public Class imp

extends Object
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .File, .FileInputStream, .FileOutputStream, .IOException, .InputStream, java.nio.file.Files, java.util.Date, .Map, java.util.concurrent.locks.ReentrantLock, java.util.logging.Level, .Logger, org.python.compiler.Module, org.python.core.util.FileUtil, .LimitedCache, .PlatformUtil

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 and Inner Type Summary

Modifier and TypeClass and Description
public static class
imp.CodeData

A bundle of a file name, the file's content and a last modified time, with no behaviour.

pack-priv static enum
imp.CodeImport

A two-way selector given to imp#createFromPyClass(String, InputStream, boolean, String, String, long, CodeImport) that tells it whether the source file name to give the module-class constructor, and which ends up in co_filename attribute of the module's PyCode, should be from the caller or the compiled file.

Field Summary

Modifier and TypeField and Description
private static final PyTuple
private static final int
public static final int
private static LimitedCache<PyObject, String>
private static final boolean
private static Logger
public static final int
private static final PyObject
nonEmptyFromlist

A non-empty fromlist for __import__'ing sub-modules.

private static final String

Constructor Summary

AccessConstructor and Description
private
imp()

Method Summary

Modifier and TypeMethod and Description
private static PyObject
_reload(PyModule m, PyObject modules, Map<String, PyModule> modules_reloading)

public static PyModule

Returns:

created PyModule
addModule
(String
fully-qualified name of the module
name
)

If the given name is found in sys.modules, the entry from there is returned.

public static String

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.
cacheCompiledSource
(String sourceFilename, String compiledFilename, byte[] compiledSource)

Stores the bytes in compiledSource in compiledFilename.

public static boolean

Returns:

file matches filename
caseok
(File
to be tested
file
,
String
to be matched
filename
)

Check that the canonical name of file matches filename, case-sensitively, even when the OS platform is case-insensitive.

private static void
checkName(String name)

Enforce ASCII module name, as a guard on module names supplied as an argument.

private static void
checkNotFile(String name)

Defend against attempt to import by filename (withdrawn feature).

public static byte[]

Returns:

Java byte code as array
compileSource
(String
of module (class name will be name$py)
name
,
File
file containing the source
source
)

Compile Python source in file to a class file represented by a byte array.

public static byte[]

Returns:

Java byte code as array
compileSource
(String
of module (class name will be name$py)
name
,
File
file containing the source
source
,
String
explicit source file name (or null to use that in source)
filename
)

Compile Python source in file to a class file represented by a byte array.

public static byte[]

Returns:

Java byte code as array
compileSource
(String
of module (class name will be name$py)
name
,
File
file containing the source
source
,
String
explicit source file name (or null to use that in source)
sourceFilename
,
String
ignored (huh?)
compiledFilename
)
Deprecated Use compileSource(String, File, String) instead.

Compile Python source in file to a class file represented by a byte array.

public static byte[]

Returns:

Java byte code as array
compileSource
(String
of module (class name will be name$py)
name
,
InputStream
open input stream (will be closed)
source
,
String
of source (or null if unknown)
filename
)

Compile Python source to a class file represented by a byte array.

public static byte[]

Returns:

Java byte code as array
compileSource
(String
of module (class name will be name$py)
name
,
InputStream
open input stream (will be closed)
source
,
String
of source (or null if unknown)
filename
,
long
last-modified time of source, to annotate class
mtime
)

Compile Python source to a class file represented by a byte array.

pack-priv static PyObject
public static PyObject
createFromCode(String name, PyCode c)

Returns a module with the given name whose contents are the results of running c.

public static PyObject

Returns:

the module object
createFromCode
(String
fully-qualified name of the module
name
,
PyCode
code supplying the module
c
,
String
to become __file__ if not null
moduleLocation
)

Return a Python module with the given name whose attributes are the result of running PyCode c.

pack-priv static PyObject

Returns:

the module or null on failure (if testing).
createFromPyClass
(String
module name on which to base the class name as name + "$py"
name
,
InputStream
stream from which to read class file (closed when read)
fp
,
boolean
if true, failures are signalled by a null not an exception
testing
,
String
used for identification in messages and the constructor of the named class.
sourceName
,
String
used for identification in messages and __file__.
compiledName
)

As createFromPyClass(String, InputStream, boolean, String, String, long, CodeImport) but always constructs the named class using sourceName as argument and makes no check on the last-modified time.

pack-priv static PyObject

Returns:

the module or null on failure (if testing).
createFromPyClass
(String
module name on which to base the class name as name + "$py"
name
,
InputStream
stream from which to read class file (closed when read)
fp
,
boolean
if true, failures are signalled by a null not an exception
testing
,
String
used for identification in messages and the constructor of the named class.
sourceName
,
String
used for identification in messages and __file__.
compiledName
,
long
time expected to match MTime annotation in the class file
sourceLastModified
)

As createFromPyClass(String, InputStream, boolean, String, String, long, CodeImport) but always constructs the named class using sourceName as argument.

pack-priv static PyObject

Returns:

the module or null on failure (if testing).
createFromPyClass
(String
module name on which to base the class name as name + "$py"
name
,
InputStream
stream from which to read class file (closed when read)
fp
,
boolean
if true, failures are signalled by a null not an exception
testing
,
String
used for identification in messages.
sourceName
,
String
used for identification in messages and __file__.
compiledName
,
long
time expected to match MTime annotation in the class file
sourceLastModified
,
imp.CodeImport
choose what to use as the file name when initialising the class
source
)

Create a Python module from its compiled form, reading the class file from the open input stream passed in (which is closed once read).

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

public static PyObject

Returns:

created module
createFromSource
(String
of the module to create (class will be name$py)
name
,
InputStream
opened on the (Jython) source to compile (will be closed)
fp
,
String
of the source backing fp (to embed in class as data)
filename
,
String
in which to write the compiled class
outFilename
,
long
last modified time of the file backing fp
mtime
)

Compile Jython source (as an InputStream) to a module.

private static void
ensureFromList(PyObject
module we are importing from
mod
,
PyObject
tuple of names to import
fromlist
,
String
of module we are importing from (as given, may be relative)
name
)

Ensure that the items mentioned in the from-list of an import are actually present, even if they are modules we have not imported yet.

private static void
ensureFromList(PyObject
module we are importing from
mod
,
PyObject
tuple of names to import
fromlist
,
String
of module we are importing from (as given, may be relative)
name
,
boolean
true, when this method calls itself
recursive
)

Ensure that the items mentioned in the from-list of an import are actually present, even if they are modules we have not imported yet.

public static String

Returns:

String form of the object p (or null).
fileSystemDecode
(PyObject
assumed to be a (partial) file path
p
,
boolean
if true and p cannot be decoded raise ImportError.
raiseImportError
)

A wrapper for Py#fileSystemDecode(PyObject) for project internal use within the import mechanism to convert decoding errors that occur during import to either null or Py#ImportError(String) calls (and a log message), which usually results in quiet failure.

public static String

Returns:

String form of the object p.
fileSystemDecode
(PyObject
assumed to be a (partial) file path
p
)

For project internal use, equivalent to fileSystemDecode(p, true) (see fileSystemDecode(PyObject, boolean)).

pack-priv static PyObject

Returns:

the module if we can load it (or null if we can't).
find_module
(String
simple name of the module.
name
,
String
fully-qualified (dotted) name of the module (ending in name).
moduleName
,
PyList
__path__ of the enclosing package (or null if top level).
path
)

Try to load a Python module from sys.meta_path, as a built-in module, or from either the __path__ of the enclosing package or sys.path if the module is being sought at the top level.

private static String

Returns:

the parent name for a module
get_parent
(PyObject
the __dict__ of a loaded module that is the context of the import statement
dict
,
int
used for relative and absolute imports. -1 means try both, 0 means absolute only, positive ints represent the level to look upward for a relative path (1 means current package, 2 means one level up). See PEP 328 at http://www.python.org/dev/peps/pep-0328/
level
)

Find the parent package name for a module.

public static int
public static PyObject
public static ClassLoader

Returns:

the parent class loader for Jython or null if both the current and context class loaders are null.
getParentClassLoader
()

Selects the parent class loader for Jython, to be used for dynamically loaded classes and resources.

pack-priv static PyObject

Returns:

the importer object for the path element or Py.None for "fall-back".
getPathImporter
(PyObject
normally sys.path_importer_cache
cache
,
PyList
normally (@code sys.path_hooks}
hooks
,
PyObject
an element of sys.path or of a package's __path__
p
)

Return an importer object for an element of sys.path or of a package's __path__, possibly by fetching it from the cache.

public static ClassLoader
private static PyObject

Returns:

the named module (never null or None)
import_first
(String
fully-qualified name of module to import import a.b.c.m
name
,
StringBuilder
used as a workspace as the search descends the package hierarchy
parentName
)

Top of the import logic in the case of a simple import a.b.c.m.

private static PyObject

Returns:

the named module (never returns null or None)
import_first
(String
fully-qualified name of module to import a.b.c.m.
name
,
StringBuilder
used as a workspace as the search descends the package hierarchy
parentName
,
String
the "outer" name by which the module is known a.b.c.m.
fullName
,
PyObject
names to import from the module n1, n2, n3.
fromlist
)

Top of the import logic in the case of a complex from a.b.c.m import n1, n2, n3.

private static PyObject

Returns:

the last named module (never null or None)
import_logic
(PyObject
top module of the import
mod
,
StringBuilder
used as a workspace as the search descends the package hierarchy
parentName
,
String
b.c.m
restOfName
,
String
a.b.c.m
fullName
,
PyObject
names to import from the module n1, n2, n3.
fromlist
)

Iterate through the components (after the first) of a fully-qualified module name a.b.c.m finding the corresponding modules a.b, a.b.c, and a.b.c.m, importing them if necessary.

private static PyObject

Returns:

an imported module (Java or Python)
import_module_level
(String
qualified name of the package/module to import (may be relative)
name
,
boolean
if true, return the top module in the name, otherwise the last
top
,
PyObject
the __dict__ of the importing module (used to navigate a relative import)
modDict
,
PyObject
list of names being imported
fromlist
,
int
0=absolute, n>0=relative levels to go up - 1, -1=try relative then absolute.
level
)

Import a module by name.

private static PyObject

Returns:

the imported module (or null or Py#None on failure).
import_next
(PyObject
if not null, a package where the module may be an attribute.
mod
,
StringBuilder
parent name of the module. (Buffer is appended with "." and name.
parentName
,
String
the (simple) name of the module to load
name
,
String
name to use with the JavaImportHelper.
outerFullName
,
PyObject
if not null the import is from <module> import <fromlist>
fromlist
)

Try to import the module named by parentName.name.

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.

public static void
importAll(String mod, PyFrame frame)
Deprecated

public static void
importAll(PyObject module, PyFrame frame)

public static PyObject[]
importFrom(String mod, String[] names, PyFrame frame)
Deprecated use importFrom with level param.

replaced by importFrom with level param.

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.

public static PyObject[]
importFromAs(String mod, String[] names, PyFrame frame)
Deprecated use importFromAs with level param.

replaced by importFromAs with level param.

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.

public static PyObject

Returns:

an imported module (Java or Python)
importName
(String
the name of the package to import
name
,
boolean
if true, return the top module in the name, otherwise the last
top
)

Import a module by name.

public static PyObject

Returns:

an imported module (Java or Python)
importName
(String
the fully-qualified name of the package/module to import
name
,
boolean
if true, return the top module in the name, otherwise the last
top
,
PyObject
the __dict__ of the importing module (used for name in relative import)
modDict
,
PyObject
list of names being imported
fromlist
,
int
0=absolute, n>0=relative levels to go up, -1=try relative then absolute.
level
)

Import a module by name.

public static PyObject
importOne(String mod, PyFrame frame)
Deprecated

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

public static PyObject
importOne(String mod, PyFrame frame, int level)

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

public static PyObject
importOneAs(String mod, PyFrame frame)
Deprecated

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

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.

private static boolean
isAncestor(ClassLoader possibleAncestor, ClassLoader subject)

True iff a possibleAncestor is the ancestor of the subject.

public static PyObject

Returns:

the loaded module
load
(String
the name of the module to load
name
)

Load the module by name.

private static PyObject

Returns:

the module named
loadBuiltin
(String
name of module
name
)

Load a built-in module by reference to PySystemState#builtins, which maps Python module names to class names.

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

pack-priv static PyObject
loadFromLoader(PyObject importer, String name)

pack-priv static PyObject

Returns:

the module if we can load it (or null if we can't).
loadFromSource
(PySystemState
the sys module of the interpreter importing the module.
sys
,
String
by which to look for files or a directory representing the module.
name
,
String
name by which to enter the module in sys.modules.
modName
,
String
where to look for the name.
location
)

Import a module defined in Python by loading it from source (or a compiled name$pyclass) file in the specified location (often an entry from sys.path, or a sub-directory of it named for the modName.

private static void
loadNames(PyObject
the names to load from the module
names
,
PyObject
the fully imported module
module
,
PyObject
the namespace into which names will be loaded
locals
,
boolean
if true, if the name starts with an underscore '_' do not add it to locals
filter
)

From a module, load the attributes found in names into locals.

public static String
makeCompiledFilename(String filename)

Remove the last three characters of a file name and add the compiled suffix "$py.class".

private static InputStream
makeStream(File file)

Open a file, raising a PyException on error.

private static byte[]

Returns:

bytes read
readBytes
(InputStream
to read
fp
)

Read a stream as a new byte array and close the stream.

public static byte[]

Returns:

the class file bytes as an array or null on failure (if testing).
readCode
(String
of source file (used for identification in error/log messages)
name
,
InputStream
stream from which to read class file (closed when read)
fp
,
boolean
if true, failures are signalled by a null not an exception
testing
)

As readCodeData(String, InputStream, boolean, long) but do not check last-modified time and return only the class file bytes as an array.

public static byte[]

Returns:

the class file bytes as an array or null on failure (if testing).
readCode
(String
of source file (used for identification in error/log messages)
name
,
InputStream
stream from which to read class file (closed when read)
fp
,
boolean
if true, failures are signalled by a null not an exception
testing
,
long
time expected to match MTime annotation in the class file
sourceLastModified
)

As readCodeData(String, InputStream, boolean, long) but return only the class file bytes as an array.

public static imp.CodeData

Returns:

the CodeData bundle or null on failure (if testing).
readCodeData
(String
of source file (used for identification in error/log messages)
name
,
InputStream
stream from which to read class file (closed when read)
fp
,
boolean
if true, failures are signalled by a null not an exception
testing
)

As readCodeData(String, InputStream, boolean, long) but do not check last-modified time.

public static imp.CodeData

Returns:

the CodeData bundle or null on failure (if testing).
readCodeData
(String
of source file (used for identification in error/log messages)
name
,
InputStream
stream from which to read class file (closed when read)
fp
,
boolean
if true, failures are signalled by a null not an exception
testing
,
long
time expected to match MTime annotation in the class file
sourceLastModified
)

Create a 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.

pack-priv static PyObject
private static void
removeModule(String
the module name
name
)

Remove name from sys.modules if present.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait