[Numpy-svn] r5040 - in trunk/numpy/lib: . tests

numpy-svn@scip... numpy-svn@scip...
Thu Apr 17 15:16:11 CDT 2008


Author: rkern
Date: 2008-04-17 15:16:11 -0500 (Thu, 17 Apr 2008)
New Revision: 5040

Modified:
   trunk/numpy/lib/_datasource.py
   trunk/numpy/lib/io.py
   trunk/numpy/lib/tests/test__datasource.py
Log:
Don't require gzip or bz2 until the actual functionality is requested.

Modified: trunk/numpy/lib/_datasource.py
===================================================================
--- trunk/numpy/lib/_datasource.py	2008-04-16 21:03:41 UTC (rev 5039)
+++ trunk/numpy/lib/_datasource.py	2008-04-17 20:16:11 UTC (rev 5040)
@@ -34,17 +34,24 @@
 
 __docformat__ = "restructuredtext en"
 
-import bz2
-import gzip
 import os
 import tempfile
 from shutil import rmtree
 from urllib2 import urlopen, URLError
 from urlparse import urlparse
 
-
 # TODO: .zip support, .tar support?
-_file_openers = {".gz":gzip.open, ".bz2":bz2.BZ2File, None:file}
+_file_openers = {None: open}
+try:
+    import bz2
+    _file_openers[".bz2"] = bz2.BZ2File
+except ImportError:
+    pass
+try:
+    import gzip
+    _file_openers[".gz"] = gzip.open
+except ImportError:
+    pass
 
 
 def open(path, mode='r', destpath=os.curdir):

Modified: trunk/numpy/lib/io.py
===================================================================
--- trunk/numpy/lib/io.py	2008-04-16 21:03:41 UTC (rev 5039)
+++ trunk/numpy/lib/io.py	2008-04-17 20:16:11 UTC (rev 5040)
@@ -8,7 +8,6 @@
 
 import numpy as np
 import format
-import zipfile
 import cStringIO
 import tempfile
 import os
@@ -42,6 +41,9 @@
     with .files and the ZipFile object itself using .zip
     """
     def __init__(self, fid):
+        # Import is postponed to here since zipfile depends on gzip, an optional
+        # component of the so-called standard library.
+        import zipfile
         _zip = zipfile.ZipFile(fid)
         self._files = _zip.namelist()
         self.files = []
@@ -165,6 +167,10 @@
     arr_0, arr_1, etc.
     """
 
+    # Import is postponed to here since zipfile depends on gzip, an optional
+    # component of the so-called standard library.
+    import zipfile
+
     if isinstance(file, str):
         if not file.endswith('.npz'):
             file = file + '.npz'

Modified: trunk/numpy/lib/tests/test__datasource.py
===================================================================
--- trunk/numpy/lib/tests/test__datasource.py	2008-04-16 21:03:41 UTC (rev 5039)
+++ trunk/numpy/lib/tests/test__datasource.py	2008-04-17 20:16:11 UTC (rev 5040)
@@ -1,6 +1,4 @@
 
-import bz2
-import gzip
 import os
 import sys
 import struct
@@ -90,6 +88,13 @@
         self.assertRaises(IOError, self.ds.open, invalid_file)
 
     def test_ValidGzipFile(self):
+        try:
+            import gzip
+        except ImportError:
+            # We don't have the bz2 capabilities to test.
+            # FIXME: when we start using nose, raise a SkipTest rather than
+            # passing the test.
+            return
         # Test datasource's internal file_opener for Gzip files.
         filepath = os.path.join(self.tmpdir, 'foobar.txt.gz')
         fp = gzip.open(filepath, 'w')
@@ -101,6 +106,13 @@
         self.assertEqual(magic_line, result)
 
     def test_ValidBz2File(self):
+        try:
+            import bz2
+        except ImportError:
+            # We don't have the bz2 capabilities to test.
+            # FIXME: when we start using nose, raise a SkipTest rather than
+            # passing the test.
+            return
         # Test datasource's internal file_opener for BZip2 files.
         filepath = os.path.join(self.tmpdir, 'foobar.txt.bz2')
         fp = bz2.BZ2File(filepath, 'w')



More information about the Numpy-svn mailing list