[Python-checkins] r52250 - sandbox/trunk/import_in_py/importer.py

brett.cannon python-checkins at python.org
Mon Oct 9 20:47:22 CEST 2006


Author: brett.cannon
Date: Mon Oct  9 20:47:22 2006
New Revision: 52250

Modified:
   sandbox/trunk/import_in_py/importer.py
Log:
Add rough bytecode handler (contributed by Paul Moore).

No tests yet.  Also lacking error checking.


Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py	(original)
+++ sandbox/trunk/import_in_py/importer.py	Mon Oct  9 20:47:22 2006
@@ -189,8 +189,21 @@
 
     # XXX 'handles' should be a property that returns based on whether -O was
     # specified when running the interpreter.
+    handles = 'pyc'
     
     # XXX Should probably add a handle_string() method to PySourceHandler() so
     # that if the .pyc is outdated it can easily use PySourceHandler to do the
     # import for it and then write out the new .pyc .
     # XXX Writing out a new .pyc should be made optional.
+
+    def handle_file(self, module, fullname, path, file_path):
+        """Import the Python bytecode file at 'path' and use it to initialize
+        'module'."""
+        module.__file__ = file_path
+        module.__name__ = fullname
+        with open(file_path, 'rb') as bytecode_file:
+            magic = bytecode_file.read(4)
+            mtime = bytecode_file.read(4)
+            compiled_code = marshal.load(bytecode_file)
+        exec compiled_code in module.__dict__
+        return module


More information about the Python-checkins mailing list