summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorRobert Kern <robert.kern@gmail.com>2008-04-17 20:16:11 +0000
committerRobert Kern <robert.kern@gmail.com>2008-04-17 20:16:11 +0000
commitddde39b22effbcd4799dc2dd4a229dcac0964205 (patch)
tree47bd9236e33bc38070c92c1c3a0fe80093820388 /numpy/lib
parent6e78d7dde1bc3f6247afde492bae4ae830e95d65 (diff)
downloadnumpy-ddde39b22effbcd4799dc2dd4a229dcac0964205.tar.gz
Don't require gzip or bz2 until the actual functionality is requested.
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/_datasource.py15
-rw-r--r--numpy/lib/io.py8
-rw-r--r--numpy/lib/tests/test__datasource.py16
3 files changed, 32 insertions, 7 deletions
diff --git a/numpy/lib/_datasource.py b/numpy/lib/_datasource.py
index 06aae85d8..3fe1df615 100644
--- a/numpy/lib/_datasource.py
+++ b/numpy/lib/_datasource.py
@@ -34,17 +34,24 @@ Example:
__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):
diff --git a/numpy/lib/io.py b/numpy/lib/io.py
index 6edf902e3..9e61ab2f8 100644
--- a/numpy/lib/io.py
+++ b/numpy/lib/io.py
@@ -8,7 +8,6 @@ __all__ = ['savetxt', 'loadtxt',
import numpy as np
import format
-import zipfile
import cStringIO
import tempfile
import os
@@ -42,6 +41,9 @@ class NpzFile(object):
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 @@ def savez(file, *args, **kwds):
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'
diff --git a/numpy/lib/tests/test__datasource.py b/numpy/lib/tests/test__datasource.py
index 89dd86687..f308cd1ad 100644
--- a/numpy/lib/tests/test__datasource.py
+++ b/numpy/lib/tests/test__datasource.py
@@ -1,6 +1,4 @@
-import bz2
-import gzip
import os
import sys
import struct
@@ -90,6 +88,13 @@ class TestDataSourceOpen(NumpyTestCase):
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 @@ class TestDataSourceOpen(NumpyTestCase):
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')