[Python-checkins] r57241 - sandbox/trunk/import_in_py/_importlib.py

brett.cannon python-checkins at python.org
Tue Aug 21 06:19:14 CEST 2007


Author: brett.cannon
Date: Tue Aug 21 06:19:13 2007
New Revision: 57241

Modified:
   sandbox/trunk/import_in_py/_importlib.py
Log:
Tweak get_bytecode to return the magic number and timestamp (as an int) along
with the data from a bytecode file.  Also changed write_bytecode to take in the
timestamp to use along with the bytecode for a code object.

This was all to have the loaders handle how to write things out to disk more so
that handle_py deals with no bytes directly.


Modified: sandbox/trunk/import_in_py/_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/_importlib.py	(original)
+++ sandbox/trunk/import_in_py/_importlib.py	Tue Aug 21 06:19:13 2007
@@ -324,10 +324,7 @@
         module.__path__  = [module.__file__.rsplit(path_sep, 1)[0]]
     # Try to use bytecode if it is available.
     if bytecode_path:
-        data = loader.get_bytecode(name)
-        magic = data[:4]
-        pyc_timestamp = _r_long(data[4:8])
-        bytecode = data[8:]
+        magic, pyc_timestamp, bytecode = loader.get_bytecode(name)
         try:
             # Verify that the magic number is valid.
             if imp.get_magic() != magic:
@@ -364,10 +361,8 @@
     # Generate bytecode and write it out.
     if source_timestamp is None:  # May have from bytecode verification.
         source_timestamp = loader.mod_time(name)
-    data = imp.get_magic()
-    data += _w_long(source_timestamp)
-    data += marshal.dumps(code_object)
-    loader.write_bytecode(name, data)
+    data = marshal.dumps(code_object)
+    loader.write_bytecode(name, source_timestamp, data)
     return module
 
 
@@ -454,7 +449,8 @@
 
     @check_name
     def get_bytecode(self, name):
-        """Return the bytecode for the module, or None if it is not available.
+        """Return the magic number, timestamp, and the module bytecode for the
+        module.
 
         Raises ImportError (just like get_source) if the laoder cannot handle
         the module.
@@ -463,13 +459,14 @@
         try:
             with open(self._bytecode_path, 'rb') as bytecode_file:
                 data = bytecode_file.read()
-            return data
+            return data[:4], _r_long(data[4:8]), data[8:]
         except AttributeError:
             return None
 
     @check_name
-    def write_bytecode(self, name, data):
-        """Write out 'data' for the specified module, returning a boolean
+    def write_bytecode(self, name, timestamp, data):
+        """Write out 'data' for the specified module using the specific
+        timestamp, returning a boolean
         signifying if the write-out actually occurred.
 
         Raises ImportError (just like get_source) if the specified module
@@ -483,6 +480,8 @@
             bytecode_path = base_path + suffix_list(imp.PY_COMPILED)[0]
         try:
             with open(bytecode_path, 'wb') as bytecode_file:
+                bytecode_file.write(imp.get_magic())
+                bytecode_file.write(_w_long(timestamp))
                 bytecode_file.write(data)
                 return True
         except IOError as exc:
@@ -513,7 +512,14 @@
         specified module.
         
         """
-        raise NotImplementedError
+        source = self.get_source(fullname)
+        if source is not None:
+            return compile(source, self._py_path, 'exec')
+        else:
+            magic, mtime, bytecode = self.get_bytecode(fullname)
+            if imp.get_magic() != magic:
+                raise ImportError('no source and bytecode bad')
+            return marshal.loads(bytecode)
 
 
 class FileImporter(object):


More information about the Python-checkins mailing list