[pypy-svn] r49414 - in pypy/branch/pypy-interp-file/module/_file: . test
arigo at codespeak.net
arigo at codespeak.net
Wed Dec 5 20:23:50 CET 2007
Author: arigo
Date: Wed Dec 5 20:23:50 2007
New Revision: 49414
Modified:
pypy/branch/pypy-interp-file/module/_file/app_file.py
pypy/branch/pypy-interp-file/module/_file/interp_file.py
pypy/branch/pypy-interp-file/module/_file/test/test_file_extra.py
Log:
Progress.
Modified: pypy/branch/pypy-interp-file/module/_file/app_file.py
==============================================================================
--- pypy/branch/pypy-interp-file/module/_file/app_file.py (original)
+++ pypy/branch/pypy-interp-file/module/_file/app_file.py Wed Dec 5 20:23:50 2007
@@ -186,7 +186,7 @@
raise ValueError('I/O operation on closed file')
return self.stream.write(data)
- def writelines(self, sequence_of_strings):
+ #def writelines(self, sequence_of_strings):
"""writelines(sequence_of_strings) -> None. Write the strings to the file.
Note that newlines are not added. The sequence can be any iterable object
Modified: pypy/branch/pypy-interp-file/module/_file/interp_file.py
==============================================================================
--- pypy/branch/pypy-interp-file/module/_file/interp_file.py (original)
+++ pypy/branch/pypy-interp-file/module/_file/interp_file.py Wed Dec 5 20:23:50 2007
@@ -167,6 +167,19 @@
def direct_write(self, data):
self.getstream().write(data)
+ def direct_writelines(self, w_lines): # note: a wrapped list!
+ stream = self.getstream()
+ space = self.space
+ w_iterator = space.iter(w_lines)
+ while True:
+ try:
+ w_line = space.next(w_iterator)
+ except OperationError, e:
+ if not e.match(space, space.w_StopIteration):
+ raise
+ break # done
+ stream.write(space.str_w(w_line))
+
def direct___iter__(self):
self.getstream()
return self
@@ -296,6 +309,12 @@
Note that due to buffering, flush() or close() may be needed before
the file on disk reflects the data written.""")
+ _decl(locals(), "writelines", ['self', W_Root],
+ """writelines(sequence_of_strings) -> None. Write the strings to the file.
+
+Note that newlines are not added. The sequence can be any iterable object
+producing strings. This is equivalent to calling write() for each string.""")
+
_decl(locals(), "__iter__", ['self'],
"""Iterating over files, as in 'for line in f:', returns each line of
the file one by one.""")
@@ -387,6 +406,7 @@
mode = interp_attrproperty('mode', cls=W_File,
doc = "file mode ('r', 'U', 'w', 'a', "
"possibly with 'b' or '+' added)"),
+ encoding = interp_attrproperty('encoding', cls=W_File),
closed = GetSetProperty(descr_file_closed, cls=W_File,
doc="True if the file is closed"),
newlines = GetSetProperty(descr_file_newlines, cls=W_File,
Modified: pypy/branch/pypy-interp-file/module/_file/test/test_file_extra.py
==============================================================================
--- pypy/branch/pypy-interp-file/module/_file/test/test_file_extra.py (original)
+++ pypy/branch/pypy-interp-file/module/_file/test/test_file_extra.py Wed Dec 5 20:23:50 2007
@@ -246,6 +246,7 @@
def setup_method(self, method):
self.file = open(self.expected_filename, self.expected_mode)
+ self.expected_lines = self.get_expected_lines()
def teardown_method(self, method):
self.file.close()
@@ -322,128 +323,135 @@
#
# A few extra tests
-def app_test_case_readonly():
- fn = str(udir.join('temptestfile'))
- f = _file.file(fn, 'w')
- assert f.name == fn
- assert f.mode == 'w'
- assert f.closed == False
- assert f.encoding == None # Fix when we find out what this is
- raises((TypeError, AttributeError), setattr, f, 'name', 42)
-
-def app_test_readlines():
- fn = str(udir.join('temptestfile'))
- lines = ['line%d\n' % i for i in range(1000)]
- f=_file.file(fn, 'w')
- f.writelines(lines)
- f.close()
- assert open(fn, 'r').readlines() == lines
- assert _file.file(fn, 'r').readlines() == lines
- somelines = _file.file(fn, 'r').readlines(2000)
- assert len(somelines) > 200
- assert somelines == lines[:len(somelines)]
-
-def app_test_rw_bin():
- flags = 'w+b'
- checkflags = 'rb'
- eolstyles = [('', ''), ('\n', '\n'),
- ('\r', '\r'), ('\r\n', '\r\n')]
- fn = str(udir.join('temptestfile'))
- f = _file.file(fn, flags)
- expected = ''
- pos = 0
- for i in range(5000):
- x = random.random()
- if x < 0.4:
- l = int(x*100)
- buf = f.read(l)
- assert buf == expected[pos:pos+l]
- pos += len(buf)
- elif x < 0.75:
- writeeol, expecteol = random.choice(eolstyles)
- x = str(x)
- buf1 = x+writeeol
- buf2 = x+expecteol
- f.write(buf1)
- expected = expected[:pos] + buf2 + expected[pos+len(buf2):]
- pos += len(buf2)
- elif x < 0.80:
- pos = random.randint(0, len(expected))
- f.seek(pos)
- elif x < 0.85:
- pos = random.randint(0, len(expected))
- f.seek(pos - len(expected), 2)
- elif x < 0.90:
- currentpos = pos
- pos = random.randint(0, len(expected))
- f.seek(pos - currentpos, 1)
- elif x < 0.95:
- assert f.tell() == pos
- else:
- f.flush()
- g = open(fn, checkflags)
- buf = g.read()
- g.close()
- assert buf == expected
- f.close()
- g = open(fn, checkflags)
- buf = g.read()
- g.close()
- assert buf == expected
-
-def app_test_rw():
- fn = str(udir.join('temptestfile'))
- f = _file.file(fn, 'w+')
- f.write('hello\nworld\n')
- f.seek(0)
- assert f.read() == 'hello\nworld\n'
- f.close()
-
-def app_test_r_universal():
- fn = str(udir.join('temptestfile'))
- f = open(fn, 'wb')
- f.write('hello\r\nworld\r\n')
- f.close()
- f = _file.file(fn, 'rU')
- assert f.read() == 'hello\nworld\n'
- f.close()
-
-def app_test_flush():
- fn = str(udir.join('temptestfile'))
- f = _file.file(fn, 'w', 0)
- f.write('x')
- assert os.stat(fn).st_size == 1
- f.close()
-
- f = _file.file(fn, 'wb', 1)
- f.write('x')
- assert os.stat(fn).st_size == 0
- f.write('\n')
- assert os.stat(fn).st_size == 2
- f.write('x')
- assert os.stat(fn).st_size == 2
- f.flush()
- assert os.stat(fn).st_size == 3
- f.close()
- assert os.stat(fn).st_size == 3
-
- f = _file.file(fn, 'wb', 100)
- f.write('x')
- assert os.stat(fn).st_size == 0
- f.write('\n')
- assert os.stat(fn).st_size == 0
- f.write('x')
- assert os.stat(fn).st_size == 0
- f.flush()
- assert os.stat(fn).st_size == 3
- f.close()
- assert os.stat(fn).st_size == 3
-
-def app_test_isatty():
- try:
- f = file('/dev/tty')
- except IOError:
- pass
- else:
- assert f.isatty()
+class AppTestAFewExtra:
+
+ def setup_method(self, method):
+ fn = str(udir.join('temptestfile'))
+ self.w_temptestfile = self.space.wrap(fn)
+
+ def test_case_readonly(self):
+ fn = self.temptestfile
+ f = file(fn, 'w')
+ assert f.name == fn
+ assert f.mode == 'w'
+ assert f.closed == False
+ assert f.encoding == None # Fix when we find out what this is
+ raises((TypeError, AttributeError), setattr, f, 'name', 42)
+
+ def test_readlines(self):
+ fn = self.temptestfile
+ lines = ['line%d\n' % i for i in range(1000)]
+ f = file(fn, 'w')
+ f.writelines(lines)
+ f.close()
+ assert open(fn, 'r').readlines() == lines
+ assert file(fn, 'r').readlines() == lines
+ somelines = file(fn, 'r').readlines(2000)
+ assert len(somelines) > 200
+ assert somelines == lines[:len(somelines)]
+
+ def test_rw_bin(self):
+ import random
+ flags = 'w+b'
+ checkflags = 'rb'
+ eolstyles = [('', ''), ('\n', '\n'),
+ ('\r', '\r'), ('\r\n', '\r\n')]
+ fn = self.temptestfile
+ f = file(fn, flags)
+ expected = ''
+ pos = 0
+ for i in range(5000):
+ x = random.random()
+ if x < 0.4:
+ l = int(x*100)
+ buf = f.read(l)
+ assert buf == expected[pos:pos+l]
+ pos += len(buf)
+ elif x < 0.75:
+ writeeol, expecteol = random.choice(eolstyles)
+ x = str(x)
+ buf1 = x+writeeol
+ buf2 = x+expecteol
+ f.write(buf1)
+ expected = expected[:pos] + buf2 + expected[pos+len(buf2):]
+ pos += len(buf2)
+ elif x < 0.80:
+ pos = random.randint(0, len(expected))
+ f.seek(pos)
+ elif x < 0.85:
+ pos = random.randint(0, len(expected))
+ f.seek(pos - len(expected), 2)
+ elif x < 0.90:
+ currentpos = pos
+ pos = random.randint(0, len(expected))
+ f.seek(pos - currentpos, 1)
+ elif x < 0.95:
+ assert f.tell() == pos
+ else:
+ f.flush()
+ g = open(fn, checkflags)
+ buf = g.read()
+ g.close()
+ assert buf == expected
+ f.close()
+ g = open(fn, checkflags)
+ buf = g.read()
+ g.close()
+ assert buf == expected
+
+ def test_rw(self):
+ fn = self.temptestfile
+ f = file(fn, 'w+')
+ f.write('hello\nworld\n')
+ f.seek(0)
+ assert f.read() == 'hello\nworld\n'
+ f.close()
+
+ def test_r_universal(self):
+ fn = self.temptestfile
+ f = open(fn, 'wb')
+ f.write('hello\r\nworld\r\n')
+ f.close()
+ f = file(fn, 'rU')
+ assert f.read() == 'hello\nworld\n'
f.close()
+
+ def test_flush(self):
+ fn = self.temptestfile
+ f = file(fn, 'w', 0)
+ f.write('x')
+ assert os.stat(fn).st_size == 1
+ f.close()
+
+ f = file(fn, 'wb', 1)
+ f.write('x')
+ assert os.stat(fn).st_size == 0
+ f.write('\n')
+ assert os.stat(fn).st_size == 2
+ f.write('x')
+ assert os.stat(fn).st_size == 2
+ f.flush()
+ assert os.stat(fn).st_size == 3
+ f.close()
+ assert os.stat(fn).st_size == 3
+
+ f = file(fn, 'wb', 100)
+ f.write('x')
+ assert os.stat(fn).st_size == 0
+ f.write('\n')
+ assert os.stat(fn).st_size == 0
+ f.write('x')
+ assert os.stat(fn).st_size == 0
+ f.flush()
+ assert os.stat(fn).st_size == 3
+ f.close()
+ assert os.stat(fn).st_size == 3
+
+ def test_isatty():
+ try:
+ f = file('/dev/tty')
+ except IOError:
+ pass
+ else:
+ assert f.isatty()
+ f.close()
More information about the Pypy-commit
mailing list