Back to index

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

List of all members.

Public Member Functions

def path_mtime
def set_data
def get_filename
def get_data
def get_source
def get_code
def load_module
def is_package

Detailed Description

Concrete implementation of SourceLoader using the file system.

Definition at line 461 of file _bootstrap.py.


Member Function Documentation

def importlib._bootstrap.SourceLoader.get_code (   self,
  fullname 
) [inherited]
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._FileLoader.get_data (   self,
  path 
) [inherited]
Return the data from path as raw bytes.

Definition at line 455 of file _bootstrap.py.

00455 
00456     def get_data(self, path):
00457         """Return the data from path as raw bytes."""
00458         with _io.FileIO(path, 'r') as file:
00459             return file.read()
00460 

Here is the caller graph for this function:

def importlib._bootstrap._FileLoader.get_filename (   self,
  fullname 
) [inherited]
Return the path to the source file as found by the finder.

Definition at line 451 of file _bootstrap.py.

00451 
00452     def get_filename(self, fullname):
00453         """Return the path to the source file as found by the finder."""
00454         return self._path

Here is the caller graph for this function:

def importlib._bootstrap.SourceLoader.get_source (   self,
  fullname 
) [inherited]
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 
) [inherited]
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:

Return the modification time for the path.

Reimplemented from importlib._bootstrap.SourceLoader.

Definition at line 465 of file _bootstrap.py.

00465 
00466     def path_mtime(self, path):
00467         """Return the modification time for the path."""
00468         return int(_os.stat(path).st_mtime)

def importlib._bootstrap._SourceFileLoader.set_data (   self,
  path,
  data 
)
Write bytes data to a file.

Reimplemented from importlib._bootstrap.SourceLoader.

Definition at line 469 of file _bootstrap.py.

00469 
00470     def set_data(self, path, data):
00471         """Write bytes data to a file."""
00472         parent, _, filename = path.rpartition(path_sep)
00473         path_parts = []
00474         # Figure out what directories are missing.
00475         while parent and not _path_isdir(parent):
00476             parent, _, part = parent.rpartition(path_sep)
00477             path_parts.append(part)
00478         # Create needed directories.
00479         for part in reversed(path_parts):
00480             parent = _path_join(parent, part)
00481             try:
00482                 _os.mkdir(parent)
00483             except OSError as exc:
00484                 # Probably another Python process already created the dir.
00485                 if exc.errno == errno.EEXIST:
00486                     continue
00487                 else:
00488                     raise
00489             except IOError as exc:
00490                 # If can't get proper access, then just forget about writing
00491                 # the data.
00492                 if exc.errno == errno.EACCES:
00493                     return
00494                 else:
00495                     raise
00496         try:
00497             with _io.FileIO(path, 'wb') as file:
00498                 file.write(data)
00499         except IOError as exc:
00500             # Don't worry if you can't write bytecode.
00501             if exc.errno == errno.EACCES:
00502                 return
00503             else:
00504                 raise
00505 

Here is the call graph for this function:


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