[Python-checkins] r58723 - sandbox/trunk/import_in_py/zipimport_/tests.py sandbox/trunk/import_in_py/zipimport_/zipimport.py

brett.cannon python-checkins at python.org
Wed Oct 31 04:25:30 CET 2007


Author: brett.cannon
Date: Wed Oct 31 04:25:30 2007
New Revision: 58723

Modified:
   sandbox/trunk/import_in_py/zipimport_/tests.py
   sandbox/trunk/import_in_py/zipimport_/zipimport.py
Log:
Return None for zipimporter.get_source() when bytecode exists but no source.


Modified: sandbox/trunk/import_in_py/zipimport_/tests.py
==============================================================================
--- sandbox/trunk/import_in_py/zipimport_/tests.py	(original)
+++ sandbox/trunk/import_in_py/zipimport_/tests.py	Wed Oct 31 04:25:30 2007
@@ -199,12 +199,24 @@
             importer.find_module('_top_level')
             self.assertEqual(example_code, importer.get_source('_top_level'))
 
+    def test_no_top_level_source(self):
+        with temp_zipfile(source=False) as zip_path:
+            importer = zipimport.zipimporter(zip_path)
+            importer.find_module('_top_level')
+            self.assertEqual(importer.get_source('_top_level'), None)
+
     def test_pkg_source(self):
         with temp_zipfile(bytecode=False) as zip_path:
             importer = zipimport.zipimporter(zip_path)
             importer.find_module('_pkg')
             self.assertEqual(example_code, importer.get_source('_pkg'))
 
+    def test_no_pkg_source(self):
+        with temp_zipfile(source=False) as zip_path:
+            importer = zipimport.zipimporter(zip_path)
+            importer.find_module('_pkg')
+            self.assertEqual(importer.get_source('_pkg'), None)
+
 
 class GetCode(unittest.TestCase):
 

Modified: sandbox/trunk/import_in_py/zipimport_/zipimport.py
==============================================================================
--- sandbox/trunk/import_in_py/zipimport_/zipimport.py	(original)
+++ sandbox/trunk/import_in_py/zipimport_/zipimport.py	Wed Oct 31 04:25:30 2007
@@ -146,12 +146,18 @@
         if the module does not exist, or None if only bytecode exists."""
         tail = fullname.rpartition('.')[2]
         paths = self._check_paths(tail, '__init__')
-        if paths and paths[0]:
-            source_path = paths[0]
+        if paths:
+            if paths[0]:
+                source_path = paths[0]
+            else:
+                return None
         else:
             paths = self._check_paths(tail)
-            if paths and paths[0]:
-                source_path = paths[0]
+            if paths:
+                if paths[0]:
+                    source_path = paths[0]
+                else:
+                    return None
             else:
                 raise ZipImportError("%s is not known" % fullname)
         with contextlib.closing(zipfile.ZipFile(self.archive)) as zip_:


More information about the Python-checkins mailing list