diff options
author | Pauli Virtanen <pav@iki.fi> | 2010-02-20 18:14:51 +0000 |
---|---|---|
committer | Pauli Virtanen <pav@iki.fi> | 2010-02-20 18:14:51 +0000 |
commit | 4bf9144224f26c740d6e8e6261e2eac068d854e0 (patch) | |
tree | 767a996a210ebe1811309e6c525ed9d642d1bab0 | |
parent | 550df270b2daf3eb231e2232269b987d4b6120f4 (diff) | |
download | numpy-4bf9144224f26c740d6e8e6261e2eac068d854e0.tar.gz |
3K: lib: Make _datasource and its tests Py3 compatible + slight cleanup of the code
-rw-r--r-- | numpy/lib/_datasource.py | 17 | ||||
-rw-r--r-- | numpy/lib/tests/test__datasource.py | 4 |
2 files changed, 12 insertions, 9 deletions
diff --git a/numpy/lib/_datasource.py b/numpy/lib/_datasource.py index 4aef6e982..ce6d2391b 100644 --- a/numpy/lib/_datasource.py +++ b/numpy/lib/_datasource.py @@ -35,7 +35,9 @@ Example:: __docformat__ = "restructuredtext en" import os -from shutil import rmtree +from shutil import rmtree, copyfile, copyfileobj + +_open = open # Using a class instead of a module-level dictionary # to reduce the inital 'import numpy' overhead by @@ -281,16 +283,15 @@ class DataSource (object): if self._isurl(path): try: openedurl = urlopen(path) - file(upath, 'w').write(openedurl.read()) + f = _open(upath, 'wb') + try: + copyfileobj(openedurl, f) + finally: + f.close() except URLError: raise URLError("URL not found: %s" % path) else: - try: - # TODO: Why not just copy the file with shutils.copyfile? - fp = file(path, 'r') - file(upath, 'w').write(fp.read()) - except IOError: - raise IOError("File not found: %s" % path) + shutil.copyfile(path, upath) return upath def _findfile(self, path): diff --git a/numpy/lib/tests/test__datasource.py b/numpy/lib/tests/test__datasource.py index 90a137498..29dbb7ae5 100644 --- a/numpy/lib/tests/test__datasource.py +++ b/numpy/lib/tests/test__datasource.py @@ -7,6 +7,8 @@ import urllib2 from numpy.testing import * +from numpy.compat import asbytes + import numpy.lib._datasource as datasource def urlopen_stub(url, data=None): @@ -36,7 +38,7 @@ http_fakefile = 'fake.txt' malicious_files = ['/etc/shadow', '../../shadow', '..\\system.dat', 'c:\\windows\\system.dat'] -magic_line = 'three is the magic number' +magic_line = asbytes('three is the magic number') # Utility functions used by many TestCases |