Anna Ravenscroft identified many occurrences of "file" used to open a file
in the stdlib and changed each of them to use "open" instead. At this
time there are no other known occurrences that can be safely changed (in
Lib and all subdirectories thereof).
diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py
index 15e1ef7..97ac480 100644
--- a/Lib/test/test_bool.py
+++ b/Lib/test/test_bool.py
@@ -246,7 +246,7 @@
def test_fileclosed(self):
try:
- f = file(test_support.TESTFN, "w")
+ f = open(test_support.TESTFN, "w")
self.assertIs(f.closed, False)
f.close()
self.assertIs(f.closed, True)
diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py
index 8561723..518005a 100644
--- a/Lib/test/test_bz2.py
+++ b/Lib/test/test_bz2.py
@@ -243,7 +243,7 @@
self.createTempFile()
bz2f = BZ2File(self.filename, "U")
bz2f.close()
- f = file(self.filename)
+ f = open(self.filename)
f.seek(0, 2)
self.assertEqual(f.tell(), len(self.DATA))
f.close()
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 3fb01e7..53054ad 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -2338,7 +2338,7 @@
self.ateof = 1
return s
- f = file(name=TESTFN, mode='w')
+ f = open(name=TESTFN, mode='w')
lines = ['a\n', 'b\n', 'c\n']
try:
f.writelines(lines)
@@ -2394,7 +2394,7 @@
sandbox = rexec.RExec()
code1 = """f = open(%r, 'w')""" % TESTFN
- code2 = """f = file(%r, 'w')""" % TESTFN
+ code2 = """f = open(%r, 'w')""" % TESTFN
code3 = """\
f = open(%r)
t = type(f) # a sneaky way to get the file() constructor
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index e9f9ef1..1030f97 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -130,7 +130,7 @@
def __init__(self, *args, **kwargs):
unittest.TestCase.__init__(self, *args, **kwargs)
- self.source = file(inspect.getsourcefile(self.fodderFile)).read()
+ self.source = open(inspect.getsourcefile(self.fodderFile)).read()
def sourcerange(self, top, bottom):
lines = self.source.split("\n")
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py
index 324eb68..81be7e1 100644
--- a/Lib/test/test_iter.py
+++ b/Lib/test/test_iter.py
@@ -661,7 +661,7 @@
# Test iterators with file.writelines().
def test_writelines(self):
- f = file(TESTFN, "w")
+ f = open(TESTFN, "w")
try:
self.assertRaises(TypeError, f.writelines, None)
@@ -700,7 +700,7 @@
f.writelines(Whatever(6, 6+2000))
f.close()
- f = file(TESTFN)
+ f = open(TESTFN)
expected = [str(i) + "\n" for i in range(1, 2006)]
self.assertEqual(list(f), expected)
diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py
index f87495b..4467577 100644
--- a/Lib/test/test_marshal.py
+++ b/Lib/test/test_marshal.py
@@ -16,8 +16,8 @@
s = marshal.dumps(expected)
got = marshal.loads(s)
self.assertEqual(expected, got)
- marshal.dump(expected, file(test_support.TESTFN, "wb"))
- got = marshal.load(file(test_support.TESTFN, "rb"))
+ marshal.dump(expected, open(test_support.TESTFN, "wb"))
+ got = marshal.load( open(test_support.TESTFN, "rb"))
self.assertEqual(expected, got)
n = n >> 1
os.unlink(test_support.TESTFN)
@@ -51,8 +51,8 @@
new = marshal.loads(marshal.dumps(b))
self.assertEqual(b, new)
self.assertEqual(type(b), type(new))
- marshal.dump(b, file(test_support.TESTFN, "wb"))
- new = marshal.load(file(test_support.TESTFN, "rb"))
+ marshal.dump(b, open(test_support.TESTFN, "wb"))
+ new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(b, new)
self.assertEqual(type(b), type(new))
@@ -67,8 +67,8 @@
s = marshal.dumps(f)
got = marshal.loads(s)
self.assertEqual(f, got)
- marshal.dump(f, file(test_support.TESTFN, "wb"))
- got = marshal.load(file(test_support.TESTFN, "rb"))
+ marshal.dump(f, open(test_support.TESTFN, "wb"))
+ got = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(f, got)
n /= 123.4567
@@ -94,12 +94,12 @@
got = marshal.loads(s)
self.assertEqual(f, got)
- marshal.dump(f, file(test_support.TESTFN, "wb"))
- got = marshal.load(file(test_support.TESTFN, "rb"))
+ marshal.dump(f, open(test_support.TESTFN, "wb"))
+ got = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(f, got)
- marshal.dump(f, file(test_support.TESTFN, "wb"), 1)
- got = marshal.load(file(test_support.TESTFN, "rb"))
+ marshal.dump(f, open(test_support.TESTFN, "wb"), 1)
+ got = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(f, got)
n *= 123.4567
os.unlink(test_support.TESTFN)
@@ -110,8 +110,8 @@
new = marshal.loads(marshal.dumps(s))
self.assertEqual(s, new)
self.assertEqual(type(s), type(new))
- marshal.dump(s, file(test_support.TESTFN, "wb"))
- new = marshal.load(file(test_support.TESTFN, "rb"))
+ marshal.dump(s, open(test_support.TESTFN, "wb"))
+ new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(s, new)
self.assertEqual(type(s), type(new))
os.unlink(test_support.TESTFN)
@@ -121,8 +121,8 @@
new = marshal.loads(marshal.dumps(s))
self.assertEqual(s, new)
self.assertEqual(type(s), type(new))
- marshal.dump(s, file(test_support.TESTFN, "wb"))
- new = marshal.load(file(test_support.TESTFN, "rb"))
+ marshal.dump(s, open(test_support.TESTFN, "wb"))
+ new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(s, new)
self.assertEqual(type(s), type(new))
os.unlink(test_support.TESTFN)
@@ -132,8 +132,8 @@
b = buffer(s)
new = marshal.loads(marshal.dumps(b))
self.assertEqual(s, new)
- marshal.dump(b, file(test_support.TESTFN, "wb"))
- new = marshal.load(file(test_support.TESTFN, "rb"))
+ marshal.dump(b, open(test_support.TESTFN, "wb"))
+ new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(s, new)
os.unlink(test_support.TESTFN)
@@ -161,8 +161,8 @@
def test_dict(self):
new = marshal.loads(marshal.dumps(self.d))
self.assertEqual(self.d, new)
- marshal.dump(self.d, file(test_support.TESTFN, "wb"))
- new = marshal.load(file(test_support.TESTFN, "rb"))
+ marshal.dump(self.d, open(test_support.TESTFN, "wb"))
+ new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(self.d, new)
os.unlink(test_support.TESTFN)
@@ -170,8 +170,8 @@
lst = self.d.items()
new = marshal.loads(marshal.dumps(lst))
self.assertEqual(lst, new)
- marshal.dump(lst, file(test_support.TESTFN, "wb"))
- new = marshal.load(file(test_support.TESTFN, "rb"))
+ marshal.dump(lst, open(test_support.TESTFN, "wb"))
+ new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(lst, new)
os.unlink(test_support.TESTFN)
@@ -179,8 +179,8 @@
t = tuple(self.d.keys())
new = marshal.loads(marshal.dumps(t))
self.assertEqual(t, new)
- marshal.dump(t, file(test_support.TESTFN, "wb"))
- new = marshal.load(file(test_support.TESTFN, "rb"))
+ marshal.dump(t, open(test_support.TESTFN, "wb"))
+ new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(t, new)
os.unlink(test_support.TESTFN)
@@ -191,8 +191,8 @@
self.assertEqual(t, new)
self.assert_(isinstance(new, constructor))
self.assertNotEqual(id(t), id(new))
- marshal.dump(t, file(test_support.TESTFN, "wb"))
- new = marshal.load(file(test_support.TESTFN, "rb"))
+ marshal.dump(t, open(test_support.TESTFN, "wb"))
+ new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(t, new)
os.unlink(test_support.TESTFN)
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 9497777..bf0e196 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -273,7 +273,7 @@
os.makedirs(sub11_path)
os.makedirs(sub2_path)
for path in tmp1_path, tmp2_path, tmp3_path:
- f = file(path, "w")
+ f = open(path, "w")
f.write("I'm " + path + " and proud of it. Blame test_os.\n")
f.close()
@@ -361,10 +361,10 @@
class DevNullTests (unittest.TestCase):
def test_devnull(self):
- f = file(os.devnull, 'w')
+ f = open(os.devnull, 'w')
f.write('hello')
f.close()
- f = file(os.devnull, 'r')
+ f = open(os.devnull, 'r')
self.assertEqual(f.read(), '')
f.close()
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index ebcb8c5..a47afa4 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -333,11 +333,11 @@
f.close()
elif self.comp == "bz2":
f = bz2.BZ2Decompressor()
- s = file(self.dstname).read()
+ s = open(self.dstname).read()
s = f.decompress(s)
self.assertEqual(len(f.unused_data), 0, "trailing data")
else:
- f = file(self.dstname)
+ f = open(self.dstname)
s = f.read()
f.close()
diff --git a/Lib/test/test_unicode_file.py b/Lib/test/test_unicode_file.py
index 6443efd..0058d98 100644
--- a/Lib/test/test_unicode_file.py
+++ b/Lib/test/test_unicode_file.py
@@ -152,7 +152,7 @@
# top-level 'test' functions would be if they could take params
def _test_single(self, filename):
remove_if_exists(filename)
- f = file(filename, "w")
+ f = open(filename, "w")
f.close()
try:
self._do_single(filename)
@@ -170,7 +170,7 @@
def _test_equivalent(self, filename1, filename2):
remove_if_exists(filename1)
self.failUnless(not os.path.exists(filename2))
- f = file(filename1, "w")
+ f = open(filename1, "w")
f.close()
try:
self._do_equivilent(filename1, filename2)
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 4579c47..eb962d2 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -27,7 +27,7 @@
def setUp(self):
"""Setup of a temp file to use for testing"""
self.text = "test_urllib: %s\n" % self.__class__.__name__
- FILE = file(test_support.TESTFN, 'wb')
+ FILE = open(test_support.TESTFN, 'wb')
try:
FILE.write(self.text)
finally:
@@ -139,7 +139,7 @@
self.registerFileForCleanUp(test_support.TESTFN)
self.text = 'testing urllib.urlretrieve'
try:
- FILE = file(test_support.TESTFN, 'wb')
+ FILE = open(test_support.TESTFN, 'wb')
FILE.write(self.text)
FILE.close()
finally:
@@ -192,7 +192,7 @@
self.assertEqual(second_temp, result[0])
self.assert_(os.path.exists(second_temp), "copy of the file was not "
"made")
- FILE = file(second_temp, 'rb')
+ FILE = open(second_temp, 'rb')
try:
text = FILE.read()
FILE.close()
diff --git a/Lib/test/test_urllibnet.py b/Lib/test/test_urllibnet.py
index 9105afe..675184b 100644
--- a/Lib/test/test_urllibnet.py
+++ b/Lib/test/test_urllibnet.py
@@ -120,7 +120,7 @@
file_location,info = urllib.urlretrieve("http://www.python.org/")
self.assert_(os.path.exists(file_location), "file location returned by"
" urlretrieve is not a valid path")
- FILE = file(file_location)
+ FILE = open(file_location)
try:
self.assert_(FILE.read(), "reading from the file location returned"
" by urlretrieve failed")
@@ -134,7 +134,7 @@
test_support.TESTFN)
self.assertEqual(file_location, test_support.TESTFN)
self.assert_(os.path.exists(file_location))
- FILE = file(file_location)
+ FILE = open(file_location)
try:
self.assert_(FILE.read(), "reading from temporary file failed")
finally: