Back to index

python3.2  3.2.2
Public Member Functions
importlib._bootstrap.SourceLoader Class Reference
Inheritance diagram for importlib._bootstrap.SourceLoader:
Inheritance graph
[legend]
Collaboration diagram for importlib._bootstrap.SourceLoader:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def path_mtime
def set_data
def get_source
def get_code
def load_module
def is_package

Detailed Description

Definition at line 346 of file _bootstrap.py.


Member Function Documentation

def importlib._bootstrap.SourceLoader.get_code (   self,
  fullname 
)
Concrete implementation of InspectLoader.get_code.

Reading of bytecode requires path_mtime to be implemented. To write
bytecode, set_data must also be implemented.

Reimplemented in importlib.abc.PyPycLoader, and importlib.test.source.test_abc_loader.PyPycLoaderMock.

Definition at line 378 of file _bootstrap.py.

00378 
00379     def get_code(self, fullname):
00380         """Concrete implementation of InspectLoader.get_code.
00381 
00382         Reading of bytecode requires path_mtime to be implemented. To write
00383         bytecode, set_data must also be implemented.
00384 
00385         """
00386         source_path = self.get_filename(fullname)
00387         bytecode_path = imp.cache_from_source(source_path)
00388         source_mtime = None
00389         if bytecode_path is not None:
00390             try:
00391                 source_mtime = self.path_mtime(source_path)
00392             except NotImplementedError:
00393                 pass
00394             else:
00395                 try:
00396                     data = self.get_data(bytecode_path)
00397                 except IOError:
00398                     pass
00399                 else:
00400                     try:
00401                         bytes_data = self._bytes_from_bytecode(fullname, data,
00402                                                                source_mtime)
00403                     except (ImportError, EOFError):
00404                         pass
00405                     else:
00406                         found = marshal.loads(bytes_data)
00407                         if isinstance(found, code_type):
00408                             return found
00409                         else:
00410                             msg = "Non-code object in {}"
00411                             raise ImportError(msg.format(bytecode_path))
00412         source_bytes = self.get_data(source_path)
00413         code_object = compile(source_bytes, source_path, 'exec',
00414                                 dont_inherit=True)
00415         if (not sys.dont_write_bytecode and bytecode_path is not None and
00416                 source_mtime is not None):
00417             # If e.g. Jython ever implements imp.cache_from_source to have
00418             # their own cached file format, this block of code will most likely
00419             # throw an exception.
00420             data = bytearray(imp.get_magic())
00421             data.extend(marshal._w_long(source_mtime))
00422             data.extend(marshal.dumps(code_object))
00423             try:
00424                 self.set_data(bytecode_path, data)
00425             except NotImplementedError:
00426                 pass
00427         return code_object

Here is the call graph for this function:

Here is the caller graph for this function:

def importlib._bootstrap.SourceLoader.get_source (   self,
  fullname 
)
Concrete implementation of InspectLoader.get_source.

Definition at line 366 of file _bootstrap.py.

00366 
00367     def get_source(self, fullname):
00368         """Concrete implementation of InspectLoader.get_source."""
00369         import tokenize
00370         path = self.get_filename(fullname)
00371         try:
00372             source_bytes = self.get_data(path)
00373         except IOError:
00374             raise ImportError("source not available through get_data()")
00375         encoding = tokenize.detect_encoding(_io.BytesIO(source_bytes).readline)
00376         newline_decoder = _io.IncrementalNewlineDecoder(None, True)
00377         return newline_decoder.decode(source_bytes.decode(encoding[0]))

Here is the call graph for this function:

Here is the caller graph for this function:

def importlib._bootstrap._LoaderBasics.is_package (   self,
  fullname 
) [inherited]
Concrete implementation of InspectLoader.is_package by checking if
the path returned by get_filename has a filename of '__init__.py'.

Reimplemented in importlib.test.source.test_abc_loader.PyPycLoaderMock, importlib.abc.PyLoader, and importlib.test.source.test_abc_loader.PyLoaderMock.

Definition at line 299 of file _bootstrap.py.

00299 
00300     def is_package(self, fullname):
00301         """Concrete implementation of InspectLoader.is_package by checking if
00302         the path returned by get_filename has a filename of '__init__.py'."""
00303         filename = self.get_filename(fullname).rpartition(path_sep)[2]
00304         return filename.rsplit('.', 1)[0] == '__init__'

Here is the call graph for this function:

Here is the caller graph for this function:

def importlib._bootstrap.SourceLoader.load_module (   self,
  fullname 
)
Concrete implementation of Loader.load_module.

Requires ExecutionLoader.get_filename and ResourceLoader.get_data to be
implemented to load source code. Use of bytecode is dictated by whether
get_code uses/writes bytecode.

Definition at line 428 of file _bootstrap.py.

00428 
00429     def load_module(self, fullname):
00430         """Concrete implementation of Loader.load_module.
00431 
00432         Requires ExecutionLoader.get_filename and ResourceLoader.get_data to be
00433         implemented to load source code. Use of bytecode is dictated by whether
00434         get_code uses/writes bytecode.
00435 
00436         """
00437         return self._load_module(fullname)
00438 

Here is the call graph for this function:

Here is the caller graph for this function:

Optional method that returns the modification time (an int) for the
specified path, where path is a str.

Implementing this method allows the loader to read bytecode files.

Reimplemented in importlib._bootstrap._SourceFileLoader, importlib.abc.SourceLoader, and importlib.test.source.test_abc_loader.SourceLoaderMock.

Definition at line 348 of file _bootstrap.py.

00348 
00349     def path_mtime(self, path):
00350         """Optional method that returns the modification time (an int) for the
00351         specified path, where path is a str.
00352 
00353         Implementing this method allows the loader to read bytecode files.
00354 
00355         """
00356         raise NotImplementedError

Here is the caller graph for this function:

def importlib._bootstrap.SourceLoader.set_data (   self,
  path,
  data 
)
Optional method which writes data (bytes) to a file path (a str).

Implementing this method allows for the writing of bytecode files.

Reimplemented in importlib._bootstrap._SourceFileLoader, importlib.abc.SourceLoader, and importlib.test.source.test_abc_loader.SourceLoaderMock.

Definition at line 357 of file _bootstrap.py.

00357 
00358     def set_data(self, path, data):
00359         """Optional method which writes data (bytes) to a file path (a str).
00360 
00361         Implementing this method allows for the writing of bytecode files.
00362 
00363         """
00364         raise NotImplementedError
00365 

Here is the caller graph for this function:


The documentation for this class was generated from the following file: