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

numpy-svn@scip... numpy-svn@scip...
Sat Feb 20 12:14:51 CST 2010


Author: ptvirtan
Date: 2010-02-20 12:14:51 -0600 (Sat, 20 Feb 2010)
New Revision: 8170

Modified:
   trunk/numpy/lib/_datasource.py
   trunk/numpy/lib/tests/test__datasource.py
Log:
3K: lib: Make _datasource and its tests Py3 compatible + slight cleanup of the code

Modified: trunk/numpy/lib/_datasource.py
===================================================================
--- trunk/numpy/lib/_datasource.py	2010-02-20 18:14:30 UTC (rev 8169)
+++ trunk/numpy/lib/_datasource.py	2010-02-20 18:14:51 UTC (rev 8170)
@@ -35,8 +35,10 @@
 __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
 # deferring the import of bz2 and gzip until needed
@@ -281,16 +283,15 @@
         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):

Modified: trunk/numpy/lib/tests/test__datasource.py
===================================================================
--- trunk/numpy/lib/tests/test__datasource.py	2010-02-20 18:14:30 UTC (rev 8169)
+++ trunk/numpy/lib/tests/test__datasource.py	2010-02-20 18:14:51 UTC (rev 8170)
@@ -7,6 +7,8 @@
 
 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 @@
 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



More information about the Numpy-svn mailing list