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).
This commit is contained in:
Alex Martelli 2006-08-24 02:58:11 +00:00
parent b5d47efe92
commit 01c77c6628
17 changed files with 53 additions and 53 deletions

View File

@ -354,7 +354,7 @@ def _init_posix():
# load the installed pyconfig.h: # load the installed pyconfig.h:
try: try:
filename = get_config_h_filename() filename = get_config_h_filename()
parse_config_h(file(filename), g) parse_config_h(open(filename), g)
except IOError, msg: except IOError, msg:
my_msg = "invalid Python installation: unable to open %s" % filename my_msg = "invalid Python installation: unable to open %s" % filename
if hasattr(msg, "strerror"): if hasattr(msg, "strerror"):

View File

@ -16,7 +16,7 @@ def make_pat():
kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b" kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b"
builtinlist = [str(name) for name in dir(__builtin__) builtinlist = [str(name) for name in dir(__builtin__)
if not name.startswith('_')] if not name.startswith('_')]
# self.file = file("file") : # self.file = open("file") :
# 1st 'file' colorized normal, 2nd as builtin, 3rd as string # 1st 'file' colorized normal, 2nd as builtin, 3rd as string
builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b" builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b"
comment = any("COMMENT", [r"#[^\n]*"]) comment = any("COMMENT", [r"#[^\n]*"])

View File

@ -173,7 +173,7 @@ class Stats:
def dump_stats(self, filename): def dump_stats(self, filename):
"""Write the profile data to a file we know how to load back.""" """Write the profile data to a file we know how to load back."""
f = file(filename, 'wb') f = open(filename, 'wb')
try: try:
marshal.dump(self.stats, f) marshal.dump(self.stats, f)
finally: finally:

View File

@ -274,7 +274,7 @@ class _Printer(object):
for filename in self.__files: for filename in self.__files:
filename = os.path.join(dir, filename) filename = os.path.join(dir, filename)
try: try:
fp = file(filename, "rU") fp = open(filename, "rU")
data = fp.read() data = fp.read()
fp.close() fp.close()
break break

View File

@ -934,7 +934,7 @@ class TarFile(object):
self.mode = {"r": "rb", "a": "r+b", "w": "wb"}[mode] self.mode = {"r": "rb", "a": "r+b", "w": "wb"}[mode]
if not fileobj: if not fileobj:
fileobj = file(self.name, self.mode) fileobj = open(self.name, self.mode)
self._extfileobj = False self._extfileobj = False
else: else:
if self.name is None and hasattr(fileobj, "name"): if self.name is None and hasattr(fileobj, "name"):
@ -1083,7 +1083,7 @@ class TarFile(object):
tarname = pre + ext tarname = pre + ext
if fileobj is None: if fileobj is None:
fileobj = file(name, mode + "b") fileobj = open(name, mode + "b")
if mode != "r": if mode != "r":
name = tarname name = tarname
@ -1355,7 +1355,7 @@ class TarFile(object):
# Append the tar header and data to the archive. # Append the tar header and data to the archive.
if tarinfo.isreg(): if tarinfo.isreg():
f = file(name, "rb") f = open(name, "rb")
self.addfile(tarinfo, f) self.addfile(tarinfo, f)
f.close() f.close()
@ -1617,7 +1617,7 @@ class TarFile(object):
"""Make a file called targetpath. """Make a file called targetpath.
""" """
source = self.extractfile(tarinfo) source = self.extractfile(tarinfo)
target = file(targetpath, "wb") target = open(targetpath, "wb")
copyfileobj(source, target) copyfileobj(source, target)
source.close() source.close()
target.close() target.close()

View File

@ -246,7 +246,7 @@ class BoolTest(unittest.TestCase):
def test_fileclosed(self): def test_fileclosed(self):
try: try:
f = file(test_support.TESTFN, "w") f = open(test_support.TESTFN, "w")
self.assertIs(f.closed, False) self.assertIs(f.closed, False)
f.close() f.close()
self.assertIs(f.closed, True) self.assertIs(f.closed, True)

View File

@ -243,7 +243,7 @@ class BZ2FileTest(BaseTest):
self.createTempFile() self.createTempFile()
bz2f = BZ2File(self.filename, "U") bz2f = BZ2File(self.filename, "U")
bz2f.close() bz2f.close()
f = file(self.filename) f = open(self.filename)
f.seek(0, 2) f.seek(0, 2)
self.assertEqual(f.tell(), len(self.DATA)) self.assertEqual(f.tell(), len(self.DATA))
f.close() f.close()

View File

@ -2338,7 +2338,7 @@ def inherits():
self.ateof = 1 self.ateof = 1
return s return s
f = file(name=TESTFN, mode='w') f = open(name=TESTFN, mode='w')
lines = ['a\n', 'b\n', 'c\n'] lines = ['a\n', 'b\n', 'c\n']
try: try:
f.writelines(lines) f.writelines(lines)
@ -2394,7 +2394,7 @@ def restricted():
sandbox = rexec.RExec() sandbox = rexec.RExec()
code1 = """f = open(%r, 'w')""" % TESTFN code1 = """f = open(%r, 'w')""" % TESTFN
code2 = """f = file(%r, 'w')""" % TESTFN code2 = """f = open(%r, 'w')""" % TESTFN
code3 = """\ code3 = """\
f = open(%r) f = open(%r)
t = type(f) # a sneaky way to get the file() constructor t = type(f) # a sneaky way to get the file() constructor

View File

@ -130,7 +130,7 @@ class GetSourceBase(unittest.TestCase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
unittest.TestCase.__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): def sourcerange(self, top, bottom):
lines = self.source.split("\n") lines = self.source.split("\n")

View File

@ -661,7 +661,7 @@ class TestCase(unittest.TestCase):
# Test iterators with file.writelines(). # Test iterators with file.writelines().
def test_writelines(self): def test_writelines(self):
f = file(TESTFN, "w") f = open(TESTFN, "w")
try: try:
self.assertRaises(TypeError, f.writelines, None) self.assertRaises(TypeError, f.writelines, None)
@ -700,7 +700,7 @@ class TestCase(unittest.TestCase):
f.writelines(Whatever(6, 6+2000)) f.writelines(Whatever(6, 6+2000))
f.close() f.close()
f = file(TESTFN) f = open(TESTFN)
expected = [str(i) + "\n" for i in range(1, 2006)] expected = [str(i) + "\n" for i in range(1, 2006)]
self.assertEqual(list(f), expected) self.assertEqual(list(f), expected)

View File

@ -16,8 +16,8 @@ class IntTestCase(unittest.TestCase):
s = marshal.dumps(expected) s = marshal.dumps(expected)
got = marshal.loads(s) got = marshal.loads(s)
self.assertEqual(expected, got) self.assertEqual(expected, got)
marshal.dump(expected, file(test_support.TESTFN, "wb")) marshal.dump(expected, open(test_support.TESTFN, "wb"))
got = marshal.load(file(test_support.TESTFN, "rb")) got = marshal.load( open(test_support.TESTFN, "rb"))
self.assertEqual(expected, got) self.assertEqual(expected, got)
n = n >> 1 n = n >> 1
os.unlink(test_support.TESTFN) os.unlink(test_support.TESTFN)
@ -51,8 +51,8 @@ class IntTestCase(unittest.TestCase):
new = marshal.loads(marshal.dumps(b)) new = marshal.loads(marshal.dumps(b))
self.assertEqual(b, new) self.assertEqual(b, new)
self.assertEqual(type(b), type(new)) self.assertEqual(type(b), type(new))
marshal.dump(b, file(test_support.TESTFN, "wb")) marshal.dump(b, open(test_support.TESTFN, "wb"))
new = marshal.load(file(test_support.TESTFN, "rb")) new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(b, new) self.assertEqual(b, new)
self.assertEqual(type(b), type(new)) self.assertEqual(type(b), type(new))
@ -67,8 +67,8 @@ class FloatTestCase(unittest.TestCase):
s = marshal.dumps(f) s = marshal.dumps(f)
got = marshal.loads(s) got = marshal.loads(s)
self.assertEqual(f, got) self.assertEqual(f, got)
marshal.dump(f, file(test_support.TESTFN, "wb")) marshal.dump(f, open(test_support.TESTFN, "wb"))
got = marshal.load(file(test_support.TESTFN, "rb")) got = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(f, got) self.assertEqual(f, got)
n /= 123.4567 n /= 123.4567
@ -94,12 +94,12 @@ class FloatTestCase(unittest.TestCase):
got = marshal.loads(s) got = marshal.loads(s)
self.assertEqual(f, got) self.assertEqual(f, got)
marshal.dump(f, file(test_support.TESTFN, "wb")) marshal.dump(f, open(test_support.TESTFN, "wb"))
got = marshal.load(file(test_support.TESTFN, "rb")) got = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(f, got) self.assertEqual(f, got)
marshal.dump(f, file(test_support.TESTFN, "wb"), 1) marshal.dump(f, open(test_support.TESTFN, "wb"), 1)
got = marshal.load(file(test_support.TESTFN, "rb")) got = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(f, got) self.assertEqual(f, got)
n *= 123.4567 n *= 123.4567
os.unlink(test_support.TESTFN) os.unlink(test_support.TESTFN)
@ -110,8 +110,8 @@ class StringTestCase(unittest.TestCase):
new = marshal.loads(marshal.dumps(s)) new = marshal.loads(marshal.dumps(s))
self.assertEqual(s, new) self.assertEqual(s, new)
self.assertEqual(type(s), type(new)) self.assertEqual(type(s), type(new))
marshal.dump(s, file(test_support.TESTFN, "wb")) marshal.dump(s, open(test_support.TESTFN, "wb"))
new = marshal.load(file(test_support.TESTFN, "rb")) new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(s, new) self.assertEqual(s, new)
self.assertEqual(type(s), type(new)) self.assertEqual(type(s), type(new))
os.unlink(test_support.TESTFN) os.unlink(test_support.TESTFN)
@ -121,8 +121,8 @@ class StringTestCase(unittest.TestCase):
new = marshal.loads(marshal.dumps(s)) new = marshal.loads(marshal.dumps(s))
self.assertEqual(s, new) self.assertEqual(s, new)
self.assertEqual(type(s), type(new)) self.assertEqual(type(s), type(new))
marshal.dump(s, file(test_support.TESTFN, "wb")) marshal.dump(s, open(test_support.TESTFN, "wb"))
new = marshal.load(file(test_support.TESTFN, "rb")) new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(s, new) self.assertEqual(s, new)
self.assertEqual(type(s), type(new)) self.assertEqual(type(s), type(new))
os.unlink(test_support.TESTFN) os.unlink(test_support.TESTFN)
@ -132,8 +132,8 @@ class StringTestCase(unittest.TestCase):
b = buffer(s) b = buffer(s)
new = marshal.loads(marshal.dumps(b)) new = marshal.loads(marshal.dumps(b))
self.assertEqual(s, new) self.assertEqual(s, new)
marshal.dump(b, file(test_support.TESTFN, "wb")) marshal.dump(b, open(test_support.TESTFN, "wb"))
new = marshal.load(file(test_support.TESTFN, "rb")) new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(s, new) self.assertEqual(s, new)
os.unlink(test_support.TESTFN) os.unlink(test_support.TESTFN)
@ -161,8 +161,8 @@ class ContainerTestCase(unittest.TestCase):
def test_dict(self): def test_dict(self):
new = marshal.loads(marshal.dumps(self.d)) new = marshal.loads(marshal.dumps(self.d))
self.assertEqual(self.d, new) self.assertEqual(self.d, new)
marshal.dump(self.d, file(test_support.TESTFN, "wb")) marshal.dump(self.d, open(test_support.TESTFN, "wb"))
new = marshal.load(file(test_support.TESTFN, "rb")) new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(self.d, new) self.assertEqual(self.d, new)
os.unlink(test_support.TESTFN) os.unlink(test_support.TESTFN)
@ -170,8 +170,8 @@ class ContainerTestCase(unittest.TestCase):
lst = self.d.items() lst = self.d.items()
new = marshal.loads(marshal.dumps(lst)) new = marshal.loads(marshal.dumps(lst))
self.assertEqual(lst, new) self.assertEqual(lst, new)
marshal.dump(lst, file(test_support.TESTFN, "wb")) marshal.dump(lst, open(test_support.TESTFN, "wb"))
new = marshal.load(file(test_support.TESTFN, "rb")) new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(lst, new) self.assertEqual(lst, new)
os.unlink(test_support.TESTFN) os.unlink(test_support.TESTFN)
@ -179,8 +179,8 @@ class ContainerTestCase(unittest.TestCase):
t = tuple(self.d.keys()) t = tuple(self.d.keys())
new = marshal.loads(marshal.dumps(t)) new = marshal.loads(marshal.dumps(t))
self.assertEqual(t, new) self.assertEqual(t, new)
marshal.dump(t, file(test_support.TESTFN, "wb")) marshal.dump(t, open(test_support.TESTFN, "wb"))
new = marshal.load(file(test_support.TESTFN, "rb")) new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(t, new) self.assertEqual(t, new)
os.unlink(test_support.TESTFN) os.unlink(test_support.TESTFN)
@ -191,8 +191,8 @@ class ContainerTestCase(unittest.TestCase):
self.assertEqual(t, new) self.assertEqual(t, new)
self.assert_(isinstance(new, constructor)) self.assert_(isinstance(new, constructor))
self.assertNotEqual(id(t), id(new)) self.assertNotEqual(id(t), id(new))
marshal.dump(t, file(test_support.TESTFN, "wb")) marshal.dump(t, open(test_support.TESTFN, "wb"))
new = marshal.load(file(test_support.TESTFN, "rb")) new = marshal.load(open(test_support.TESTFN, "rb"))
self.assertEqual(t, new) self.assertEqual(t, new)
os.unlink(test_support.TESTFN) os.unlink(test_support.TESTFN)

View File

@ -273,7 +273,7 @@ class WalkTests(unittest.TestCase):
os.makedirs(sub11_path) os.makedirs(sub11_path)
os.makedirs(sub2_path) os.makedirs(sub2_path)
for path in tmp1_path, tmp2_path, tmp3_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.write("I'm " + path + " and proud of it. Blame test_os.\n")
f.close() f.close()
@ -361,10 +361,10 @@ class MakedirTests (unittest.TestCase):
class DevNullTests (unittest.TestCase): class DevNullTests (unittest.TestCase):
def test_devnull(self): def test_devnull(self):
f = file(os.devnull, 'w') f = open(os.devnull, 'w')
f.write('hello') f.write('hello')
f.close() f.close()
f = file(os.devnull, 'r') f = open(os.devnull, 'r')
self.assertEqual(f.read(), '') self.assertEqual(f.read(), '')
f.close() f.close()

View File

@ -333,11 +333,11 @@ class WriteStreamTest(WriteTest):
f.close() f.close()
elif self.comp == "bz2": elif self.comp == "bz2":
f = bz2.BZ2Decompressor() f = bz2.BZ2Decompressor()
s = file(self.dstname).read() s = open(self.dstname).read()
s = f.decompress(s) s = f.decompress(s)
self.assertEqual(len(f.unused_data), 0, "trailing data") self.assertEqual(len(f.unused_data), 0, "trailing data")
else: else:
f = file(self.dstname) f = open(self.dstname)
s = f.read() s = f.read()
f.close() f.close()

View File

@ -152,7 +152,7 @@ class TestUnicodeFiles(unittest.TestCase):
# top-level 'test' functions would be if they could take params # top-level 'test' functions would be if they could take params
def _test_single(self, filename): def _test_single(self, filename):
remove_if_exists(filename) remove_if_exists(filename)
f = file(filename, "w") f = open(filename, "w")
f.close() f.close()
try: try:
self._do_single(filename) self._do_single(filename)
@ -170,7 +170,7 @@ class TestUnicodeFiles(unittest.TestCase):
def _test_equivalent(self, filename1, filename2): def _test_equivalent(self, filename1, filename2):
remove_if_exists(filename1) remove_if_exists(filename1)
self.failUnless(not os.path.exists(filename2)) self.failUnless(not os.path.exists(filename2))
f = file(filename1, "w") f = open(filename1, "w")
f.close() f.close()
try: try:
self._do_equivilent(filename1, filename2) self._do_equivilent(filename1, filename2)

View File

@ -27,7 +27,7 @@ class urlopen_FileTests(unittest.TestCase):
def setUp(self): def setUp(self):
"""Setup of a temp file to use for testing""" """Setup of a temp file to use for testing"""
self.text = "test_urllib: %s\n" % self.__class__.__name__ self.text = "test_urllib: %s\n" % self.__class__.__name__
FILE = file(test_support.TESTFN, 'wb') FILE = open(test_support.TESTFN, 'wb')
try: try:
FILE.write(self.text) FILE.write(self.text)
finally: finally:
@ -139,7 +139,7 @@ class urlretrieve_FileTests(unittest.TestCase):
self.registerFileForCleanUp(test_support.TESTFN) self.registerFileForCleanUp(test_support.TESTFN)
self.text = 'testing urllib.urlretrieve' self.text = 'testing urllib.urlretrieve'
try: try:
FILE = file(test_support.TESTFN, 'wb') FILE = open(test_support.TESTFN, 'wb')
FILE.write(self.text) FILE.write(self.text)
FILE.close() FILE.close()
finally: finally:
@ -192,7 +192,7 @@ class urlretrieve_FileTests(unittest.TestCase):
self.assertEqual(second_temp, result[0]) self.assertEqual(second_temp, result[0])
self.assert_(os.path.exists(second_temp), "copy of the file was not " self.assert_(os.path.exists(second_temp), "copy of the file was not "
"made") "made")
FILE = file(second_temp, 'rb') FILE = open(second_temp, 'rb')
try: try:
text = FILE.read() text = FILE.read()
FILE.close() FILE.close()

View File

@ -120,7 +120,7 @@ class urlretrieveNetworkTests(unittest.TestCase):
file_location,info = urllib.urlretrieve("http://www.python.org/") file_location,info = urllib.urlretrieve("http://www.python.org/")
self.assert_(os.path.exists(file_location), "file location returned by" self.assert_(os.path.exists(file_location), "file location returned by"
" urlretrieve is not a valid path") " urlretrieve is not a valid path")
FILE = file(file_location) FILE = open(file_location)
try: try:
self.assert_(FILE.read(), "reading from the file location returned" self.assert_(FILE.read(), "reading from the file location returned"
" by urlretrieve failed") " by urlretrieve failed")
@ -134,7 +134,7 @@ class urlretrieveNetworkTests(unittest.TestCase):
test_support.TESTFN) test_support.TESTFN)
self.assertEqual(file_location, test_support.TESTFN) self.assertEqual(file_location, test_support.TESTFN)
self.assert_(os.path.exists(file_location)) self.assert_(os.path.exists(file_location))
FILE = file(file_location) FILE = open(file_location)
try: try:
self.assert_(FILE.read(), "reading from temporary file failed") self.assert_(FILE.read(), "reading from temporary file failed")
finally: finally:

View File

@ -210,7 +210,7 @@ class UnixBrowser(BaseBrowser):
cmdline = [self.name] + raise_opt + args cmdline = [self.name] + raise_opt + args
if remote or self.background: if remote or self.background:
inout = file(os.devnull, "r+") inout = open(os.devnull, "r+")
else: else:
# for TTY browsers, we need stdin/out # for TTY browsers, we need stdin/out
inout = None inout = None
@ -334,7 +334,7 @@ class Konqueror(BaseBrowser):
else: else:
action = "openURL" action = "openURL"
devnull = file(os.devnull, "r+") devnull = open(os.devnull, "r+")
# if possible, put browser in separate process group, so # if possible, put browser in separate process group, so
# keyboard interrupts don't affect browser as well as Python # keyboard interrupts don't affect browser as well as Python
setsid = getattr(os, 'setsid', None) setsid = getattr(os, 'setsid', None)