[pypy-commit] pypy py3k: kill module/_file. Files are handled by the _io module now

antocuni noreply at buildbot.pypy.org
Tue Feb 28 16:44:05 CET 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: py3k
Changeset: r52989:96fc36348096
Date: 2012-02-28 16:43 +0100
http://bitbucket.org/pypy/pypy/changeset/96fc36348096/

Log:	kill module/_file. Files are handled by the _io module now

diff --git a/pypy/module/_file/__init__.py b/pypy/module/_file/__init__.py
deleted file mode 100644
--- a/pypy/module/_file/__init__.py
+++ /dev/null
@@ -1,46 +0,0 @@
-
-# Package initialisation
-from pypy.interpreter.mixedmodule import MixedModule
-import sys
-
-class Module(MixedModule):
-    appleveldefs = {
-    }
-
-    interpleveldefs = {
-        "file": "interp_file.W_File",
-        "set_file_encoding": "interp_file.set_file_encoding",
-    }
-
-    def __init__(self, space, *args):
-        "NOT_RPYTHON"
-
-        # on windows with oo backends, remove file.truncate,
-        # because the implementation is based on rffi
-        if (sys.platform == 'win32' and
-            space.config.translation.type_system == 'ootype'):
-            from pypy.module._file.interp_file import W_File
-            del W_File.typedef.rawdict['truncate']
-
-        MixedModule.__init__(self, space, *args)
-
-    def shutdown(self, space):
-        # at shutdown, flush all open streams.  Ignore I/O errors.
-        from pypy.module._file.interp_file import getopenstreams, StreamErrors
-        openstreams = getopenstreams(space)
-        while openstreams:
-            for stream in openstreams.keys():
-                try:
-                    del openstreams[stream]
-                except KeyError:
-                    pass    # key was removed in the meantime
-                else:
-                    try:
-                        stream.flush()
-                    except StreamErrors:
-                        pass
-
-    def setup_after_space_initialization(self):
-        from pypy.module._file.interp_file import W_File
-        from pypy.objspace.std.transparent import register_proxyable
-        register_proxyable(self.space, W_File)
diff --git a/pypy/module/_file/interp_file.py b/pypy/module/_file/interp_file.py
deleted file mode 100644
--- a/pypy/module/_file/interp_file.py
+++ /dev/null
@@ -1,569 +0,0 @@
-import py
-import os
-import stat
-import errno
-from pypy.rlib import streamio
-from pypy.rlib.rarithmetic import r_longlong
-from pypy.rlib.rstring import StringBuilder
-from pypy.module._file.interp_stream import W_AbstractStream, StreamErrors
-from pypy.module.posix.interp_posix import dispatch_filename
-from pypy.interpreter.error import OperationError, operationerrfmt
-from pypy.interpreter.typedef import (TypeDef, GetSetProperty,
-    interp_attrproperty, make_weakref_descr, interp_attrproperty_w)
-from pypy.interpreter.gateway import interp2app, unwrap_spec
-from pypy.interpreter.streamutil import wrap_streamerror, wrap_oserror_as_ioerror
-
-class W_File(W_AbstractStream):
-    """An interp-level file object.  This implements the same interface than
-    the app-level files, with the following differences:
-
-      * method names are prefixed with 'file_'
-      * the 'normal' app-level constructor is implemented by file___init__().
-      * the methods with the 'direct_' prefix should be used if the caller
-          locks and unlocks the file itself, and takes care of StreamErrors.
-    """
-
-    # Default values until the file is successfully opened
-    stream   = None
-    w_name   = None
-    mode     = "<uninitialized file>"
-    binary   = False
-    softspace= 0     # Required according to file object docs
-    encoding = None
-    errors   = None
-    fd       = -1
-
-    newlines = 0     # Updated when the stream is closed
-
-    def __init__(self, space):
-        self.space = space
-
-    def __del__(self):
-        # assume that the file and stream objects are only visible in the
-        # thread that runs __del__, so no race condition should be possible
-        self.clear_all_weakrefs()
-        if self.stream is not None:
-            self.enqueue_for_destruction(self.space, W_File.destructor,
-                                         'close() method of ')
-
-    def destructor(self):
-        assert isinstance(self, W_File)
-        try:
-            self.direct_close()
-        except StreamErrors, e:
-            operr = wrap_streamerror(self.space, e, self.w_name)
-            raise operr
-
-    def fdopenstream(self, stream, fd, mode, w_name=None):
-        self.fd = fd
-        self.mode = mode
-        self.binary = "b" in mode
-        if w_name is not None:
-            self.w_name = w_name
-        self.stream = stream
-        if stream.flushable():
-            getopenstreams(self.space)[stream] = None
-
-    def check_not_dir(self, fd):
-        try:
-            st = os.fstat(fd)
-        except OSError:
-            pass
-        else:
-            if (stat.S_ISDIR(st[0])):
-                ose = OSError(errno.EISDIR, '')
-                raise wrap_oserror_as_ioerror(self.space, ose, self.w_name)
-
-    def check_mode_ok(self, mode):
-        if (not mode or mode[0] not in ['r', 'w', 'a', 'U'] or
-            ('U' in mode and ('w' in mode or 'a' in mode))):
-            space = self.space
-            raise operationerrfmt(space.w_ValueError,
-                                  "invalid mode: '%s'", mode)
-
-    def check_closed(self):
-        if self.stream is None:
-            raise OperationError(self.space.w_ValueError,
-                self.space.wrap("I/O operation on closed file")
-            )
-
-    def getstream(self):
-        """Return self.stream or raise an app-level ValueError if missing
-        (i.e. if the file is closed)."""
-        self.check_closed()
-        return self.stream
-
-    def _when_reading_first_flush(self, otherfile):
-        """Flush otherfile before reading from self."""
-        self.stream = streamio.CallbackReadFilter(self.stream,
-                                                  otherfile._try_to_flush)
-
-    def _try_to_flush(self):
-        stream = self.stream
-        if stream is not None:
-            stream.flush()
-
-    # ____________________________________________________________
-    #
-    # The 'direct_' methods assume that the caller already acquired the
-    # file lock.  They don't convert StreamErrors to OperationErrors, too.
-
-    @unwrap_spec(mode=str, buffering=int)
-    def direct___init__(self, w_name, mode='r', buffering=-1):
-        self.direct_close()
-        self.w_name = w_name
-        self.check_mode_ok(mode)
-        stream = dispatch_filename(streamio.open_file_as_stream)(
-            self.space, w_name, mode, buffering)
-        fd = stream.try_to_find_file_descriptor()
-        self.check_not_dir(fd)
-        self.fdopenstream(stream, fd, mode)
-
-    def direct___enter__(self):
-        self.check_closed()
-        return self
-
-    def file__exit__(self, __args__):
-        """__exit__(*excinfo) -> None. Closes the file."""
-        self.space.call_method(self, "close")
-        # can't return close() value
-        return None
-
-    def direct_fdopen(self, fd, mode='r', buffering=-1):
-        self.direct_close()
-        self.w_name = self.space.wrap('<fdopen>')
-        self.check_mode_ok(mode)
-        stream = streamio.fdopen_as_stream(fd, mode, buffering)
-        self.fdopenstream(stream, fd, mode)
-
-    def direct_close(self):
-        space = self.space
-        stream = self.stream
-        if stream is not None:
-            self.newlines = self.stream.getnewlines()
-            self.stream = None
-            self.fd = -1
-            openstreams = getopenstreams(self.space)
-            try:
-                del openstreams[stream]
-            except KeyError:
-                pass
-            stream.close()
-
-    def direct_fileno(self):
-        self.getstream()    # check if the file is still open
-        return self.fd
-
-    def direct_flush(self):
-        self.getstream().flush()
-
-    def direct___next__(self):
-        line = self.getstream().readline()
-        if line == '':
-            raise OperationError(self.space.w_StopIteration, self.space.w_None)
-        return line
-
-    @unwrap_spec(n=int)
-    def direct_read(self, n=-1):
-        stream = self.getstream()
-        if n < 0:
-            return stream.readall()
-        else:
-            result = StringBuilder(n)
-            while n > 0:
-                data = stream.read(n)
-                if not data:
-                    break
-                n -= len(data)
-                result.append(data)
-            return result.build()
-
-    @unwrap_spec(size=int)
-    def direct_readline(self, size=-1):
-        stream = self.getstream()
-        if size < 0:
-            return stream.readline()
-        else:
-            # very inefficient unless there is a peek()
-            result = []
-            while size > 0:
-                # "peeks" on the underlying stream to see how many chars
-                # we can safely read without reading past an end-of-line
-                peeked = stream.peek()
-                pn = peeked.find("\n", 0, size)
-                if pn < 0:
-                    pn = min(size-1, len(peeked))
-                c = stream.read(pn + 1)
-                if not c:
-                    break
-                result.append(c)
-                if c.endswith('\n'):
-                    break
-                size -= len(c)
-            return ''.join(result)
-
-    @unwrap_spec(size=int)
-    def direct_readlines(self, size=0):
-        stream = self.getstream()
-        # this is implemented as: .read().split('\n')
-        # except that it keeps the \n in the resulting strings
-        if size <= 0:
-            data = stream.readall()
-        else:
-            data = stream.read(size)
-        result = []
-        splitfrom = 0
-        for i in range(len(data)):
-            if data[i] == '\n':
-                result.append(data[splitfrom : i + 1])
-                splitfrom = i + 1
-        #
-        if splitfrom < len(data):
-            # there is a partial line at the end.  If size > 0, it is likely
-            # to be because the 'read(size)' returned data up to the middle
-            # of a line.  In that case, use 'readline()' to read until the
-            # end of the current line.
-            data = data[splitfrom:]
-            if size > 0:
-                data += stream.readline()
-            result.append(data)
-        return result
-
-    @unwrap_spec(offset=r_longlong, whence=int)
-    def direct_seek(self, offset, whence=0):
-        self.getstream().seek(offset, whence)
-
-    def direct_tell(self):
-        return self.getstream().tell()
-
-    def direct_truncate(self, w_size=None):  # note: a wrapped size!
-        stream = self.getstream()
-        space = self.space
-        if w_size is None or space.is_w(w_size, space.w_None):
-            size = stream.tell()
-        else:
-            size = space.r_longlong_w(w_size)
-        stream.truncate(size)
-
-    def direct_write(self, w_data):
-        space = self.space
-        if not self.binary and space.isinstance_w(w_data, space.w_unicode):
-            w_data = space.call_method(w_data, "encode", space.wrap(self.encoding), space.wrap(self.errors))
-        data = space.bufferstr_w(w_data)
-        self.do_direct_write(data)
-
-    def do_direct_write(self, data):
-        self.softspace = 0
-        self.getstream().write(data)
-
-    def direct___iter__(self):
-        self.getstream()
-        return self
-    direct_xreadlines = direct___iter__
-
-    def direct_isatty(self):
-        self.getstream()    # check if the file is still open
-        return os.isatty(self.fd)
-
-    # ____________________________________________________________
-    #
-    # The 'file_' methods are the one exposed to app-level.
-
-    def file_fdopen(self, fd, mode="r", buffering=-1):
-        try:
-            self.direct_fdopen(fd, mode, buffering)
-        except StreamErrors, e:
-            raise wrap_streamerror(self.space, e, self.w_name)
-
-    _exposed_method_names = []
-
-    def _decl(class_scope, name, docstring,
-              wrapresult="space.wrap(result)"):
-        # hack hack to build a wrapper around the direct_xxx methods.
-        # The wrapper adds lock/unlock calls and a space.wrap() on
-        # the result, conversion of stream errors to OperationErrors,
-        # and has the specified docstring and unwrap_spec.
-        direct_fn = class_scope['direct_' + name]
-        co = direct_fn.func_code
-        argnames = co.co_varnames[:co.co_argcount]
-        defaults = direct_fn.func_defaults or ()
-        unwrap_spec = getattr(direct_fn, 'unwrap_spec', None)
-
-        args = []
-        for i, argname in enumerate(argnames):
-            try:
-                default = defaults[-len(argnames) + i]
-            except IndexError:
-                args.append(argname)
-            else:
-                args.append('%s=%r' % (argname, default))
-        sig = ', '.join(args)
-        assert argnames[0] == 'self'
-        callsig = ', '.join(argnames[1:])
-
-        src = py.code.Source("""
-            def file_%(name)s(%(sig)s):
-                %(docstring)r
-                space = self.space
-                self.lock()
-                try:
-                    try:
-                        result = self.direct_%(name)s(%(callsig)s)
-                    except StreamErrors, e:
-                        raise wrap_streamerror(space, e, self.w_name)
-                finally:
-                    self.unlock()
-                return %(wrapresult)s
-        """ % locals())
-        exec str(src) in globals(), class_scope
-        if unwrap_spec is not None:
-            class_scope['file_' + name].unwrap_spec = unwrap_spec
-        class_scope['_exposed_method_names'].append(name)
-
-
-    _decl(locals(), "__init__", """Opens a file.""")
-
-    _decl(locals(), "__enter__", """__enter__() -> self.""")
-
-    _decl(locals(), "close",
-        """close() -> None or (perhaps) an integer.  Close the file.
-
-Sets data attribute .closed to True.  A closed file cannot be used for
-further I/O operations.  close() may be called more than once without
-error.  Some kinds of file objects (for example, opened by popen())
-may return an exit status upon closing.""")
-        # NB. close() needs to use the stream lock to avoid double-closes or
-        # close-while-another-thread-uses-it.
-
-
-    _decl(locals(), "fileno",
-        '''fileno() -> integer "file descriptor".
-
-This is needed for lower-level file interfaces, such os.read().''')
-
-    _decl(locals(), "flush",
-        """flush() -> None.  Flush the internal I/O buffer.""")
-
-    _decl(locals(), "isatty",
-        """isatty() -> true or false.  True if the file is connected to a tty device.""")
-
-    _decl(locals(), "__next__",
-        """__next__() -> the next line in the file, or raise StopIteration""")
-
-    _decl(locals(), "read",
-        """read([size]) -> read at most size bytes, returned as a string.
-
-If the size argument is negative or omitted, read until EOF is reached.
-Notice that when in non-blocking mode, less data than what was requested
-may be returned, even if no size parameter was given.""")
-
-    _decl(locals(), "readline",
-        """readline([size]) -> next line from the file, as a string.
-
-Retain newline.  A non-negative size argument limits the maximum
-number of bytes to return (an incomplete line may be returned then).
-Return an empty string at EOF.""")
-
-    _decl(locals(), "readlines",
-        """readlines([size]) -> list of strings, each a line from the file.
-
-Call readline() repeatedly and return a list of the lines so read.
-The optional size argument, if given, is an approximate bound on the
-total number of bytes in the lines returned.""",
-        wrapresult = "wrap_list_of_str(space, result)")
-
-    _decl(locals(), "seek",
-        """seek(offset[, whence]) -> None.  Move to new file position.
-
-Argument offset is a byte count.  Optional argument whence defaults to
-0 (offset from start of file, offset should be >= 0); other values are 1
-(move relative to current position, positive or negative), and 2 (move
-relative to end of file, usually negative, although many platforms allow
-seeking beyond the end of a file).  If the file is opened in text mode,
-only offsets returned by tell() are legal.  Use of other offsets causes
-undefined behavior.
-Note that not all file objects are seekable.""")
-
-    _decl(locals(), "tell",
-        "tell() -> current file position, an integer (may be a long integer).")
-
-    _decl(locals(), "truncate",
-        """truncate([size]) -> None.  Truncate the file to at most size bytes.
-
-Size defaults to the current file position, as returned by tell().""")
-
-    _decl(locals(), "write",
-        """write(str) -> None.  Write string str to file.
-
-Note that due to buffering, flush() or close() may be needed before
-the file on disk reflects the data written.""")
-
-    _decl(locals(), "__iter__",
-        """Iterating over files, as in 'for line in f:', returns each line of
-the file one by one.""")
-
-    _decl(locals(), "xreadlines",
-        """xreadlines() -> returns self.
-
-For backward compatibility. File objects now include the performance
-optimizations previously implemented in the xreadlines module.""")
-
-    def file__repr__(self):
-        if self.stream is None:
-            head = "closed"
-        else:
-            head = "open"
-        info = "%s file %s, mode '%s'" % (
-            head,
-            self.getdisplayname(),
-            self.mode)
-        return self.getrepr(self.space, info)
-
-    def getdisplayname(self):
-        w_name = self.w_name
-        if w_name is None:
-            return '?'
-        elif self.space.is_true(self.space.isinstance(w_name,
-                                                      self.space.w_str)):
-            return "'%s'" % self.space.str_w(w_name)
-        else:
-            return self.space.str_w(self.space.repr(w_name))
-
-    def file_writelines(self, w_lines):
-        """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."""
-
-        space = self.space
-        self.check_closed()
-        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
-            self.file_write(w_line)
-
-    def file_readinto(self, w_rwbuffer):
-        """readinto() -> Undocumented.  Don't use this; it may go away."""
-        # XXX not the most efficient solution as it doesn't avoid the copying
-        space = self.space
-        rwbuffer = space.rwbuffer_w(w_rwbuffer)
-        w_data = self.file_read(rwbuffer.getlength())
-        data = space.str_w(w_data)
-        rwbuffer.setslice(0, data)
-        return space.wrap(len(data))
-
-
-# ____________________________________________________________
-
-
-def descr_file__new__(space, w_subtype, __args__):
-    file = space.allocate_instance(W_File, w_subtype)
-    W_File.__init__(file, space)
-    return space.wrap(file)
-
- at unwrap_spec(fd=int, mode=str, buffering=int)
-def descr_file_fdopen(space, w_subtype, fd, mode='r', buffering=-1):
-    file = space.allocate_instance(W_File, w_subtype)
-    W_File.__init__(file, space)
-    file.file_fdopen(fd, mode, buffering)
-    return space.wrap(file)
-
-def descr_file_closed(space, file):
-    return space.wrap(file.stream is None)
-
-def descr_file_newlines(space, file):
-    if file.stream:
-        newlines = file.stream.getnewlines()
-    else:
-        newlines = file.newlines
-    if newlines == 0:
-        return space.w_None
-    elif newlines == 1:
-        return space.wrap("\r")
-    elif newlines == 2:
-        return space.wrap("\n")
-    elif newlines == 4:
-        return space.wrap("\r\n")
-    result = []
-    if newlines & 1:
-        result.append(space.wrap('\r'))
-    if newlines & 2:
-        result.append(space.wrap('\n'))
-    if newlines & 4:
-        result.append(space.wrap('\r\n'))
-    return space.newtuple(result[:])
-
-def descr_file_softspace(space, file):
-    return space.wrap(file.softspace)
-
-def descr_file_setsoftspace(space, file, w_newvalue):
-    file.softspace = space.int_w(w_newvalue)
-
-# ____________________________________________________________
-
-W_File.typedef = TypeDef(
-    "file",
-    __doc__ = """file(name[, mode[, buffering]]) -> file object
-
-Open a file.  The mode can be 'r', 'w' or 'a' for reading (default),
-writing or appending.  The file will be created if it doesn't exist
-when opened for writing or appending; it will be truncated when
-opened for writing.  Add a 'b' to the mode for binary files.
-Add a '+' to the mode to allow simultaneous reading and writing.
-If the buffering argument is given, 0 means unbuffered, 1 means line
-buffered, and larger numbers specify the buffer size.
-Add a 'U' to mode to open the file for input with universal newline
-support.  Any line ending in the input file will be seen as a '\n'
-in Python.  Also, a file so opened gains the attribute 'newlines';
-the value for this attribute is one of None (no newline read yet),
-'\r', '\n', '\r\n' or a tuple containing all the newline types seen.
-
-Note:  open() is an alias for file().
-""",
-    __new__  = interp2app(descr_file__new__),
-    fdopen   = interp2app(descr_file_fdopen, as_classmethod=True),
-    name     = interp_attrproperty_w('w_name', cls=W_File, doc="file name"),
-    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),
-    errors = interp_attrproperty('errors', 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,
-                              doc="end-of-line convention used in this file"),
-    softspace= GetSetProperty(descr_file_softspace,
-                              descr_file_setsoftspace,
-                              cls=W_File,
-                              doc="Support for 'print'."),
-    __repr__ = interp2app(W_File.file__repr__),
-    readinto = interp2app(W_File.file_readinto),
-    writelines = interp2app(W_File.file_writelines),
-    __exit__ = interp2app(W_File.file__exit__),
-    __weakref__ = make_weakref_descr(W_File),
-    **dict([(name, interp2app(getattr(W_File, 'file_' + name)))
-                for name in W_File._exposed_method_names])
-    )
-
-# ____________________________________________________________
-
-def wrap_list_of_str(space, lst):
-    return space.newlist([space.wrap(s) for s in lst])
-
-class FileState:
-    def __init__(self, space):
-        self.openstreams = {}
-
-def getopenstreams(space):
-    return space.fromcache(FileState).openstreams
-
-
- at unwrap_spec(file=W_File, encoding="str_or_None", errors="str_or_None")
-def set_file_encoding(space, file, encoding=None, errors=None):
-    file.encoding = encoding
-    file.errors = errors
diff --git a/pypy/module/_file/interp_stream.py b/pypy/module/_file/interp_stream.py
deleted file mode 100644
--- a/pypy/module/_file/interp_stream.py
+++ /dev/null
@@ -1,123 +0,0 @@
-import py
-from pypy.rlib import streamio
-from pypy.rlib.streamio import StreamErrors
-
-from pypy.interpreter.error import OperationError
-from pypy.interpreter.baseobjspace import ObjSpace, Wrappable
-from pypy.interpreter.typedef import TypeDef
-from pypy.interpreter.gateway import interp2app
-from pypy.interpreter.streamutil import wrap_streamerror, wrap_oserror_as_ioerror
-
-
-class W_AbstractStream(Wrappable):
-    """Base class for interp-level objects that expose streams to app-level"""
-    slock = None
-    slockowner = None
-    # Locking issues:
-    # * Multiple threads can access the same W_AbstractStream in
-    #   parallel, because many of the streamio calls eventually
-    #   release the GIL in some external function call.
-    # * Parallel accesses have bad (and crashing) effects on the
-    #   internal state of the buffering levels of the stream in
-    #   particular.
-    # * We can't easily have a lock on each W_AbstractStream because we
-    #   can't translate prebuilt lock objects.
-    # We are still protected by the GIL, so the easiest is to create
-    # the lock on-demand.
-
-    def __init__(self, space, stream):
-        self.space = space
-        self.stream = stream
-
-    def _try_acquire_lock(self):
-        # this function runs with the GIL acquired so there is no race
-        # condition in the creation of the lock
-        if self.slock is None:
-            self.slock = self.space.allocate_lock()
-        me = self.space.getexecutioncontext()   # used as thread ident
-        if self.slockowner is me:
-            return False    # already acquired by the current thread
-        self.slock.acquire(True)
-        assert self.slockowner is None
-        self.slockowner = me
-        return True
-
-    def _release_lock(self):
-        self.slockowner = None
-        self.slock.release()
-
-    def lock(self):
-        if not self._try_acquire_lock():
-            raise OperationError(self.space.w_RuntimeError,
-                                 self.space.wrap("stream lock already held"))
-
-    def unlock(self):
-        me = self.space.getexecutioncontext()   # used as thread ident
-        if self.slockowner is not me:
-            raise OperationError(self.space.w_RuntimeError,
-                                 self.space.wrap("stream lock is not held"))
-        self._release_lock()
-
-    def _freeze_(self):
-        # remove the lock object, which will be created again as needed at
-        # run-time.
-        self.slock = None
-        assert self.slockowner is None
-        return False
-
-    def stream_read(self, n):
-        """
-        An interface for direct interp-level usage of W_AbstractStream,
-        e.g. from interp_marshal.py.
-        NOTE: this assumes that the stream lock is already acquired.
-        Like os.read(), this can return less than n bytes.
-        """
-        try:
-            return self.stream.read(n)
-        except StreamErrors, e:
-            raise wrap_streamerror(self.space, e)
-
-    def do_write(self, data):
-        """
-        An interface for direct interp-level usage of W_Stream,
-        e.g. from interp_marshal.py.
-        NOTE: this assumes that the stream lock is already acquired.
-        """
-        try:
-            self.stream.write(data)
-        except StreamErrors, e:
-            raise wrap_streamerror(self.space, e)
-
-# ____________________________________________________________
-
-class W_Stream(W_AbstractStream):
-    """A class that exposes the raw stream interface to app-level."""
-    # this exists for historical reasons, and kept around in case we want
-    # to re-expose the raw stream interface to app-level.
-
-for name, argtypes in streamio.STREAM_METHODS.iteritems():
-    numargs = len(argtypes)
-    args = ", ".join(["v%s" % i for i in range(numargs)])
-    exec py.code.Source("""
-    def %(name)s(self, space, %(args)s):
-        acquired = self.try_acquire_lock()
-        try:
-            try:
-                result = self.stream.%(name)s(%(args)s)
-            except streamio.StreamError, e:
-                raise OperationError(space.w_ValueError,
-                                     space.wrap(e.message))
-            except OSError, e:
-                raise wrap_oserror_as_ioerror(space, e)
-        finally:
-            if acquired:
-                self.release_lock()
-        return space.wrap(result)
-    %(name)s.unwrap_spec = [W_Stream, ObjSpace] + argtypes
-    """ % locals()).compile() in globals()
-
-W_Stream.typedef = TypeDef("Stream",
-    lock   = interp2app(W_Stream.lock),
-    unlock = interp2app(W_Stream.unlock),
-    **dict([(name, interp2app(globals()[name]))
-                for name, _ in streamio.STREAM_METHODS.iteritems()]))
diff --git a/pypy/module/_file/test/__init__.py b/pypy/module/_file/test/__init__.py
deleted file mode 100644
diff --git a/pypy/module/_file/test/test_file.py b/pypy/module/_file/test/test_file.py
deleted file mode 100644
--- a/pypy/module/_file/test/test_file.py
+++ /dev/null
@@ -1,454 +0,0 @@
-from __future__ import with_statement
-import py, os, errno
-
-from pypy.conftest import gettestobjspace, option
-
-def getfile(space):
-    return space.appexec([], """():
-        try:
-            import _file
-            return _file.file
-        except ImportError:     # when running with py.test -A
-            return file
-    """)
-
-class AppTestFile(object):
-    def setup_class(cls):
-        cls.space = gettestobjspace(usemodules=("_file", ))
-        cls.w_temppath = cls.space.wrap(
-            str(py.test.ensuretemp("fileimpl").join("foo.txt")))
-        cls.w_file = getfile(cls.space)
-
-    def test_simple(self):
-        f = self.file(self.temppath, "w")
-        f.write("foo")
-        f.close()
-        f = self.file(self.temppath, "r")
-        raises(TypeError, f.read, None)
-        try:
-            s = f.read()
-            assert s == "foo"
-        finally:
-            f.close()
-
-    def test_readline(self):
-        f = self.file(self.temppath, "w")
-        try:
-            f.write("foo\nbar\n")
-        finally:
-            f.close()
-        f = self.file(self.temppath, "r")
-        raises(TypeError, f.readline, None)
-        try:
-            s = f.readline()
-            assert s == "foo\n"
-            s = f.readline()
-            assert s == "bar\n"
-        finally:
-            f.close()
-
-    def test_readlines(self):
-        f = self.file(self.temppath, "w")
-        try:
-            f.write("foo\nbar\n")
-        finally:
-            f.close()
-        f = self.file(self.temppath, "r")
-        raises(TypeError, f.readlines, None)
-        try:
-            s = f.readlines()
-            assert s == ["foo\n", "bar\n"]
-        finally:
-            f.close()
-
-
-    def test_fdopen(self):
-        import os
-        f = self.file(self.temppath, "w")
-        try:
-            f.write("foo\nbaz\n")
-        finally:
-            f.close()
-        try:
-            fdopen = self.file.fdopen
-        except AttributeError:
-            fdopen = os.fdopen      # when running with -A
-        fd = os.open(self.temppath, os.O_WRONLY | os.O_CREAT)
-        f2 = fdopen(fd, "a")
-        f2.seek(0, 2)
-        f2.write("bar\nboo")
-        f2.close()
-        # don't close fd, will get a whining __del__
-        f = self.file(self.temppath, "r")
-        try:
-            s = f.read()
-            assert s == "foo\nbaz\nbar\nboo"
-        finally:
-            f.close()
-
-    def test_badmode(self):
-        raises(ValueError, self.file, "foo", "bar")
-
-    def test_wraposerror(self):
-        raises(IOError, self.file, "hopefully/not/existant.bar")
-
-    def test_correct_file_mode(self):
-        import os
-        f = self.file(self.temppath, "w")
-        umask = os.umask(0)
-        os.umask(umask)
-        try:
-            f.write("foo")
-        finally:
-            f.close()
-        assert oct(os.stat(self.temppath).st_mode & 0777) == oct(0666 & ~umask)
-
-    def test_newlines(self):
-        import os
-        f = self.file(self.temppath, "wb")
-        f.write("\r\n")
-        assert f.newlines is None
-        f.close()
-        assert f.newlines is None
-        f = self.file(self.temppath, "rU")
-        res = f.read()
-        assert res == "\n"
-        assert f.newlines == "\r\n"
-        f.close()
-        assert f.newlines == "\r\n"
-
-        # now use readline()
-        f = self.file(self.temppath, "rU")
-        res = f.readline()
-        assert res == "\n"
-        # this tell() is necessary for CPython as well to update f.newlines
-        f.tell()
-        assert f.newlines == "\r\n"
-        res = f.readline()
-        assert res == ""
-        assert f.newlines == "\r\n"
-        f.close()
-
-    def test_unicode(self):
-        import os
-        f = self.file(self.temppath, "w")
-        f.write(u"hello\n")
-        raises(UnicodeEncodeError, f.write, u'\xe9')
-        f.close()
-        f = self.file(self.temppath, "r")
-        res = f.read()
-        assert res == "hello\n"
-        assert type(res) is str
-        f.close()
-
-    def test_unicode_filename(self):
-        import sys
-        try:
-            u'\xe9'.encode(sys.getfilesystemencoding())
-        except UnicodeEncodeError:
-            skip("encoding not good enough")
-        f = self.file(self.temppath + u'\xe9', "w")
-        f.close()
-
-    def test_oserror_has_filename(self):
-        try:
-            f = self.file("file that is clearly not there")
-        except IOError, e:
-            assert e.filename == 'file that is clearly not there'
-        else:
-            raise Exception("did not raise")
-
-    def test_readline_mixed_with_read(self):
-        s = '''From MAILER-DAEMON Wed Jan 14 14:42:30 2009
-From: foo
-
-0
-From MAILER-DAEMON Wed Jan 14 14:42:44 2009
-Return-Path: <gkj at gregorykjohnson.com>
-X-Original-To: gkj+person at localhost
-Delivered-To: gkj+person at localhost
-Received: from localhost (localhost [127.0.0.1])
-        by andy.gregorykjohnson.com (Postfix) with ESMTP id 356ED9DD17
-        for <gkj+person at localhost>; Wed, 13 Jul 2005 17:23:16 -0400 (EDT)
-Delivered-To: gkj at sundance.gregorykjohnson.com'''
-        f = self.file(self.temppath, "w")
-        f.write(s)
-        f.close()
-        f = self.file(self.temppath, "r")
-        f.seek(0L)
-        f.readline()
-        pos = f.tell()
-        assert f.read(12L) == 'From: foo\n\n0'
-        f.seek(pos)
-        assert f.read(12L) == 'From: foo\n\n0'
-        f.close()
-
-    def test_invalid_modes(self):
-        raises(ValueError, self.file, self.temppath, "aU")
-        raises(ValueError, self.file, self.temppath, "wU+")
-        raises(ValueError, self.file, self.temppath, "")
-
-    def test_write_resets_softspace(self):
-        f = self.file(self.temppath, "w")
-        print >> f, '.',
-        f.write(',')
-        print >> f, '.',
-        f.close()
-        f = self.file(self.temppath, "r")
-        res = f.read()
-        assert res == ".,."
-        f.close()
-
-    def test_open_dir(self):
-        import os
-
-        exc = raises(IOError, self.file, os.curdir)
-        assert exc.value.filename == os.curdir
-        exc = raises(IOError, self.file, os.curdir, 'w')
-        assert exc.value.filename == os.curdir
-
-    def test_encoding_errors(self):
-        import _file
-
-        with self.file(self.temppath, "w") as f:
-            _file.set_file_encoding(f, "utf-8")
-            f.write(u'15\u20ac')
-
-            assert f.encoding == "utf-8"
-            assert f.errors is None
-
-        with self.file(self.temppath, "r") as f:
-            data = f.read()
-            assert data == '15\xe2\x82\xac'
-
-        with self.file(self.temppath, "w") as f:
-            _file.set_file_encoding(f, "iso-8859-1", "ignore")
-            f.write(u'15\u20ac')
-
-            assert f.encoding == "iso-8859-1"
-            assert f.errors == "ignore"
-
-        with self.file(self.temppath, "r") as f:
-            data = f.read()
-            assert data == "15"
-
-    def test_exception_from_close(self):
-        import os
-        f = self.file(self.temppath, 'w')
-        os.close(f.fileno())
-        raises(IOError, f.close)    # bad file descriptor
-
-    def test_exception_from_del(self):
-        import os, gc, sys, cStringIO
-        f = self.file(self.temppath, 'w')
-        g = cStringIO.StringIO()
-        preverr = sys.stderr
-        try:
-            sys.stderr = g
-            os.close(f.fileno())
-            del f
-            gc.collect()     # bad file descriptor in f.__del__()
-        finally:
-            sys.stderr = preverr
-        import errno
-        assert os.strerror(errno.EBADF) in g.getvalue()
-        # the following is a "nice to have" feature that CPython doesn't have
-        if '__pypy__' in sys.builtin_module_names:
-            assert self.temppath in g.getvalue()
-
-
-class AppTestNonblocking(object):
-    def setup_class(cls):
-        from pypy.module._file.interp_file import W_File
-
-        cls.old_read = os.read
-
-        if option.runappdirect:
-            py.test.skip("works with internals of _file impl on py.py")
-        import platform
-        if platform.system() == 'Windows':
-            # XXX This test crashes until someone implements something like
-            # XXX verify_fd from
-            # XXX http://hg.python.org/cpython/file/80ddbd822227/Modules/posixmodule.c#l434
-            # XXX and adds it to fopen
-            assert False
-
-        state = [0]
-        def read(fd, n=None):
-            if fd != 42:
-                return cls.old_read(fd, n)
-            if state[0] == 0:
-                state[0] += 1
-                return "xyz"
-            if state[0] < 3:
-                state[0] += 1
-                raise OSError(errno.EAGAIN, "xyz")
-            return ''
-        os.read = read
-        stdin = W_File(cls.space)
-        stdin.file_fdopen(42, "r", 1)
-        stdin.name = '<stdin>'
-        cls.w_stream = stdin
-
-    def teardown_class(cls):
-        os.read = cls.old_read
-
-    def test_nonblocking_file(self):
-        res = self.stream.read()
-        assert res == 'xyz'
-
-class AppTestConcurrency(object):
-    # these tests only really make sense on top of a translated pypy-c,
-    # because on top of py.py the inner calls to os.write() don't
-    # release our object space's GIL.
-    def setup_class(cls):
-        if not option.runappdirect:
-            py.test.skip("likely to deadlock when interpreted by py.py")
-        cls.space = gettestobjspace(usemodules=("_file", "thread"))
-        cls.w_temppath = cls.space.wrap(
-            str(py.test.ensuretemp("fileimpl").join("concurrency.txt")))
-        cls.w_file = getfile(cls.space)
-
-    def test_concurrent_writes(self):
-        # check that f.write() is atomic
-        import thread, time
-        f = self.file(self.temppath, "w+b")
-        def writer(i):
-            for j in range(150):
-                f.write('%3d %3d\n' % (i, j))
-            locks[i].release()
-        locks = []
-        for i in range(10):
-            lock = thread.allocate_lock()
-            lock.acquire()
-            locks.append(lock)
-        for i in range(10):
-            thread.start_new_thread(writer, (i,))
-        # wait until all threads are done
-        for i in range(10):
-            locks[i].acquire()
-        f.seek(0)
-        lines = f.readlines()
-        lines.sort()
-        assert lines == ['%3d %3d\n' % (i, j) for i in range(10)
-                                              for j in range(150)]
-        f.close()
-
-    def test_parallel_writes_and_reads(self):
-        # Warning: a test like the one below deadlocks CPython
-        # http://bugs.python.org/issue1164
-        # It also deadlocks on py.py because the space GIL is not
-        # released.
-        import thread, sys, os
-        try:
-            fdopen = self.file.fdopen
-        except AttributeError:
-            # when running with -A
-            skip("deadlocks on top of CPython")
-        read_fd, write_fd = os.pipe()
-        fread = fdopen(read_fd, 'rb', 200)
-        fwrite = fdopen(write_fd, 'wb', 200)
-        run = True
-        readers_done = [0]
-
-        def writer():
-            f = 0.1
-            while run:
-                print >> fwrite, f,
-                f = 4*f - 3*f*f
-            print >> fwrite, "X"
-            fwrite.flush()
-            sys.stdout.write('writer ends\n')
-
-        def reader(j):
-            while True:
-                data = fread.read(1)
-                #sys.stdout.write('%d%r ' % (j, data))
-                if data == "X":
-                    break
-            sys.stdout.write('reader ends\n')
-            readers_done[0] += 1
-
-        for j in range(3):
-            thread.start_new_thread(reader, (j,))
-            thread.start_new_thread(writer, ())
-
-        import time
-        t = time.time() + 5
-        print "start of test"
-        while time.time() < t:
-            time.sleep(1)
-        print "end of test"
-
-        assert readers_done[0] == 0
-        run = False    # end the writers
-        for i in range(600):
-            time.sleep(0.4)
-            sys.stdout.flush()
-            x = readers_done[0]
-            if x == 3:
-                break
-            print 'readers_done == %d, still waiting...' % (x,)
-        else:
-            raise Exception("time out")
-        print 'Passed.'
-
-
-class AppTestFile25:
-    def setup_class(cls):
-        cls.space = gettestobjspace(usemodules=("_file", ))
-        cls.w_temppath = cls.space.wrap(
-            str(py.test.ensuretemp("fileimpl").join("foo.txt")))
-        cls.w_file = getfile(cls.space)
-
-    def test___enter__(self):
-        f = self.file(self.temppath, 'w')
-        assert f.__enter__() is f
-
-    def test___exit__(self):
-        f = self.file(self.temppath, 'w')
-        assert f.__exit__() is None
-        assert f.closed
-
-    def test_file_and_with_statement(self):
-        with self.file(self.temppath, 'w') as f:
-            f.write('foo')
-        assert f.closed
-
-        with self.file(self.temppath, 'r') as f:
-            s = f.readline()
-
-        assert s == "foo"
-        assert f.closed
-
-    def test_subclass_with(self):
-        file = self.file
-        class C(file):
-            def __init__(self, *args, **kwargs):
-                self.subclass_closed = False
-                file.__init__(self, *args, **kwargs)
-
-            def close(self):
-                self.subclass_closed = True
-                file.close(self)
-
-        with C(self.temppath, 'w') as f:
-            pass
-        assert f.subclass_closed
-
-def test_flush_at_exit():
-    from pypy import conftest
-    from pypy.tool.option import make_config, make_objspace
-    from pypy.tool.udir import udir
-
-    tmpfile = udir.join('test_flush_at_exit')
-    config = make_config(conftest.option)
-    space = make_objspace(config)
-    space.appexec([space.wrap(str(tmpfile))], """(tmpfile):
-        f = open(tmpfile, 'w')
-        f.write('42')
-        # no flush() and no close()
-        import sys; sys._keepalivesomewhereobscure = f
-    """)
-    space.finish()
-    assert tmpfile.read() == '42'
diff --git a/pypy/module/_file/test/test_file_extra.py b/pypy/module/_file/test/test_file_extra.py
deleted file mode 100644
--- a/pypy/module/_file/test/test_file_extra.py
+++ /dev/null
@@ -1,608 +0,0 @@
-import os, random, sys
-import pypy.tool.udir
-import py
-from pypy.conftest import gettestobjspace
-
-udir = pypy.tool.udir.udir.ensure('test_file_extra', dir=1)
-
-
-# XXX this file is a random test.  It may only fail occasionally
-# depending on the details of the random string SAMPLE.
-
-SAMPLE = ''.join([chr(random.randrange(0, 256)) for i in range(12487)])
-for extra in ['\r\r', '\r\n', '\n\r', '\n\n']:
-    for i in range(20):
-        j = random.randrange(0, len(SAMPLE)+1)
-        SAMPLE = SAMPLE[:j] + extra + SAMPLE[j:]
-    if random.random() < 0.1:
-        SAMPLE += extra    # occasionally, also test strings ending in an EOL
-
-
-def setup_module(mod):
-    udir.join('sample').write(SAMPLE, 'wb')
-
-
-class BaseROTests:
-    sample = SAMPLE
-
-    def get_expected_lines(self):
-        lines = self.sample.split('\n')
-        for i in range(len(lines)-1):
-            lines[i] += '\n'
-        # if self.sample ends exactly in '\n', the split() gives a
-        # spurious empty line at the end.  Fix it:
-        if lines[-1] == '':
-            del lines[-1]
-        return lines
-
-    def test_simple_tell(self):
-        assert self.file.tell() == 0
-
-    def test_plain_read(self):
-        data1 = self.file.read()
-        assert data1 == self.sample
-
-    def test_readline(self):
-        lines = self.expected_lines
-        for sampleline in lines:
-            inputline = self.file.readline()
-            assert inputline == sampleline
-        for i in range(5):
-            inputline = self.file.readline()
-            assert inputline == ""
-
-    def test_readline_max(self):
-        import random
-        i = 0
-        stop = 0
-        while stop < 5:
-            max = random.randrange(0, 100)
-            sampleline = self.sample[i:i+max]
-            nexteol = sampleline.find('\n')
-            if nexteol >= 0:
-                sampleline = sampleline[:nexteol+1]
-            inputline = self.file.readline(max)
-            assert inputline == sampleline
-            i += len(sampleline)
-            if i == len(self.sample):
-                stop += 1
-
-    def test_iter(self):
-        inputlines = list(self.file)
-        assert inputlines == self.expected_lines
-
-    def test_isatty(self):
-        assert not self.file.isatty()
-
-    def test_next(self):
-        lines = self.expected_lines
-        for sampleline in lines:
-            inputline = self.file.next()
-            assert inputline == sampleline
-        for i in range(5):
-            raises(StopIteration, self.file.next)
-
-    def test_read(self):
-        import random
-        i = 0
-        stop = 0
-        while stop < 5:
-            max = random.randrange(0, 100)
-            samplebuf = self.sample[i:i+max]
-            inputbuf = self.file.read(max)
-            assert inputbuf == samplebuf
-            i += len(samplebuf)
-            if i == len(self.sample):
-                stop += 1
-
-    def test_readlines(self):
-        lines = self.file.readlines()
-        assert lines == self.expected_lines
-
-    def test_readlines_max(self):
-        import random
-        i = 0
-        stop = 0
-        samplelines = self.expected_lines
-        while stop < 5:
-            morelines = self.file.readlines(random.randrange(1, 300))
-            for inputline in morelines:
-                assert inputline == samplelines[0]
-                samplelines.pop(0)
-            if not samplelines:
-                stop += 1
-            else:
-                assert len(morelines) >= 1    # otherwise, this test (and
-                                              # real programs) would be prone
-                                              # to endless loops
-
-    def test_seek(self):
-        import random
-        for i in range(100):
-            position = random.randrange(0, len(self.sample))
-            self.file.seek(position)
-            inputchar = self.file.read(1)
-            assert inputchar == self.sample[position]
-        for i in range(100):
-            position = random.randrange(0, len(self.sample))
-            self.file.seek(position - len(self.sample), 2)
-            inputchar = self.file.read(1)
-            assert inputchar == self.sample[position]
-        prevpos = position + 1
-        for i in range(100):
-            position = random.randrange(0, len(self.sample))
-            self.file.seek(position - prevpos, 1)
-            inputchar = self.file.read(1)
-            assert inputchar == self.sample[position]
-            prevpos = position + 1
-
-    def test_tell(self):
-        import random
-        for i in range(100):
-            position = random.randrange(0, len(self.sample)+1)
-            self.file.seek(position)
-            told = self.file.tell()
-            assert told == position
-        for i in range(100):
-            position = random.randrange(0, len(self.sample)+1)
-            self.file.seek(position - len(self.sample), 2)
-            told = self.file.tell()
-            assert told == position
-        prevpos = position
-        for i in range(100):
-            position = random.randrange(0, len(self.sample)+1)
-            self.file.seek(position - prevpos, 1)
-            told = self.file.tell()
-            assert told == position
-            prevpos = position
-
-    def test_tell_and_seek_back(self):
-        import random
-        i = 0
-        stop = 0
-        secondpass = []
-        while stop < 5:
-            max = random.randrange(0, 100)
-            samplebuf = self.sample[i:i+max]
-            secondpass.append((self.file.tell(), i))
-            inputbuf = self.file.read(max)
-            assert inputbuf == samplebuf
-            i += len(samplebuf)
-            if i == len(self.sample):
-                stop += 1
-        for i in range(100):
-            saved_position, i = random.choice(secondpass)
-            max = random.randrange(0, 100)
-            samplebuf = self.sample[i:i+max]
-            self.file.seek(saved_position)
-            inputbuf = self.file.read(max)
-            assert inputbuf == samplebuf
-
-    def test_xreadlines(self):
-        assert self.file.xreadlines() is self.file
-
-    def test_attr(self):
-        f = self.file
-        if self.expected_filename is not None:
-            assert f.name == self.expected_filename
-        if self.expected_mode is not None:
-            assert f.mode == self.expected_mode
-        assert f.closed == False
-        assert not f.softspace
-        raises((TypeError, AttributeError), 'f.name = 42')
-        raises((TypeError, AttributeError), 'f.name = "stuff"')
-        raises((TypeError, AttributeError), 'f.mode = "r"')
-        raises((TypeError, AttributeError), 'f.closed = True')
-        f.softspace = True
-        assert f.softspace
-        f.softspace = False
-        assert not f.softspace
-        f.close()
-        assert f.closed == True
-
-    def test_repr(self):
-        assert repr(self.file).startswith(
-            "<open file '%s', mode '%s' at 0x" % (
-                self.expected_filename, self.expected_mode))
-        self.file.close()
-        assert repr(self.file).startswith(
-            "<closed file '%s', mode '%s' at 0x" % (
-                self.expected_filename, self.expected_mode))
-        
-# ____________________________________________________________
-#
-#  Basic 'rb' mode
-
-class AppTestFile(BaseROTests):
-    expected_filename  = str(udir.join('sample'))
-    expected_mode      = 'rb'
-    extra_args = ()
-
-    def setup_method(self, method):
-        space = self.space
-        if hasattr(space, 'gettypeobject'):
-            from pypy.module._file.interp_file import W_File
-            w_filetype = space.gettypeobject(W_File.typedef)
-        else:
-            w_filetype = file    # TinyObjSpace, for "py.test -A"
-        self.w_file = space.call_function(
-            w_filetype,
-            space.wrap(self.expected_filename),
-            space.wrap(self.expected_mode),
-            *[space.wrap(a) for a in self.extra_args])
-        self.w_sample = space.wrap(self.sample)
-        self.w_expected_filename = space.wrap(self.expected_filename)
-        self.w_expected_mode = space.wrap(self.expected_mode)
-        self.w_expected_lines = space.wrap(self.get_expected_lines())
-
-    def teardown_method(self, method):
-        self.space.call_method(self.w_file, 'close')
-
-
-class AppTestUnbufferedFile(AppTestFile):
-    extra_args = (0,)
-
-
-class AppTestLineBufferedFile(AppTestFile):
-    extra_args = (1,)
-
-
-class AppTestLargeBufferFile(AppTestFile):
-    extra_args = (len(SAMPLE),)
-
-
-# ____________________________________________________________
-#
-#  Check on top of CPython
-
-
-class TestWithCPython(BaseROTests):
-    expected_filename = str(udir.join('sample'))
-    expected_mode     = 'rb'
-
-    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()
-
-# ____________________________________________________________
-#
-#  Files built with fdopen()
-
-class AppTestFdOpen(BaseROTests):
-    expected_filename  = '<fdopen>'
-    expected_mode      = 'rb'
-    extra_args = ()
-
-    def setup_method(self, method):
-        space = self.space
-        O_BINARY = getattr(os, "O_BINARY", 0)
-        if hasattr(space, 'gettypeobject'):
-            from pypy.module._file.interp_file import W_File
-            w_filetype = space.gettypeobject(W_File.typedef)
-        else:
-            w_filetype = os    # TinyObjSpace, for "py.test -A"
-                               # (CPython has no file.fdopen, only os.fdopen)
-        fd = os.open(AppTestFile.expected_filename, os.O_RDONLY | O_BINARY)
-        self.w_file = space.call_method(
-            w_filetype,
-            'fdopen',
-            space.wrap(fd),
-            space.wrap(self.expected_mode),
-            *[space.wrap(a) for a in self.extra_args])
-        self.w_fd = space.wrap(fd)
-        self.w_sample = space.wrap(self.sample)
-        self.w_expected_filename = space.wrap(self.expected_filename)
-        self.w_expected_mode = space.wrap(self.expected_mode)
-        self.w_expected_lines = space.wrap(self.get_expected_lines())
-
-    def teardown_method(self, method):
-        self.space.call_method(self.w_file, 'close')
-
-    def test_fileno(self):
-        assert self.file.fileno() == self.fd
-
-
-class AppTestUnbufferedFdOpen(AppTestFdOpen):
-    extra_args = (0,)
-
-
-class AppTestLineBufferedFdOpen(AppTestFdOpen):
-    extra_args = (1,)
-
-
-class AppTestLargeBufferFdOpen(AppTestFdOpen):
-    extra_args = (len(SAMPLE),)
-
-
-# ____________________________________________________________
-#
-#  Universal newlines
-
-class AppTestUniversalNewlines(AppTestFile):
-    expected_mode = 'rU'
-    sample = '\n'.join((SAMPLE+'X').splitlines(False))[:-1]
-    # ^^^ if SAMPLE ends in any end-of-line character combination, read()ing
-    # it in 'rU' mode gives a final '\n', but splitlines(False) doesn't give
-    # a final empty line.  Adding and removing an extra 'X' avoids this
-    # corner case.
-
-    def test_seek(self):
-        skip("does not apply in universal newlines mode")
-
-    test_tell = test_seek
-
-
-class AppTestUnbufferedUniversal(AppTestUniversalNewlines):
-    extra_args = (0,)
-
-
-class AppTestLineBufferedUniversal(AppTestUniversalNewlines):
-    extra_args = (1,)
-
-
-class AppTestLargeBufferUniversal(AppTestUniversalNewlines):
-    extra_args = (len(SAMPLE),)
-
-
-# ____________________________________________________________
-#
-#  A few extra tests
-
-class AppTestAFewExtra:
-
-    def setup_class(cls):
-        space = gettestobjspace(usemodules=('array',))
-        cls.space = space
-
-    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_nasty_writelines(self):
-        # The stream lock should be released between writes
-        fn = self.temptestfile
-        f = file(fn, 'w')
-        def nasty():
-            for i in range(5):
-                if i == 3:
-                    # should not raise because of acquired lock
-                    f.close()
-                yield str(i)
-        exc = raises(ValueError, f.writelines, nasty())
-        assert exc.value.message == "I/O operation on closed file"
-        f.close()
-
-    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):
-        import os
-        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', 1000)
-        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(self):
-        try:
-            f = file('/dev/tty')
-        except IOError:
-            pass
-        else:
-            assert f.isatty()
-            f.close()
-
-    def test_truncate(self):
-        fn = self.temptestfile
-        f = open(fn, 'w+b')
-        f.write('hello world')
-        f.seek(7)
-        f.truncate()
-        f.seek(0)
-        data = f.read()
-        assert data == 'hello w'
-        f.seek(0, 2)
-        assert f.tell() == 7
-        f.seek(0)
-        f.truncate(3)
-        data = f.read(123)
-        assert data == 'hel'
-        f.close()
-
-        import errno, sys
-        f = open(fn)
-        exc = raises(EnvironmentError, f.truncate, 3)
-        if sys.platform == 'win32':
-            assert exc.value.winerror == 5 # ERROR_ACCESS_DENIED
-        else:
-            # CPython explicitely checks the file mode
-            # PyPy relies on the libc to raise the error
-            assert (exc.value.message == "File not open for writing" or
-                    exc.value.errno == errno.EINVAL)
-        f.close()
-
-    def test_readinto(self):
-        from array import array
-        a = array('c')
-        a.fromstring('0123456789')
-        fn = self.temptestfile
-        f = open(fn, 'w+b')
-        f.write('foobar')
-        f.seek(0)
-        n = f.readinto(a)
-        f.close()
-        assert n == 6
-        assert len(a) == 10
-        assert a.tostring() == 'foobar6789'
-
-    def test_weakref(self):
-        """Files are weakrefable."""
-        import weakref
-        fn = self.temptestfile
-        f = open(fn, 'wb')
-        ref = weakref.ref(f)
-        ref().write('hello')
-        assert f.tell() == 5
-        f.close()
-
-    def test_weakref_dies_before_file_closes(self):
-        # Hard-to-reproduce failure (which should now be fixed).
-        # I think that this is how lib-python/modified-2.5.2/test_file.py
-        # sometimes failed on a Boehm pypy-c.
-        import weakref, gc
-        fn = self.temptestfile
-        f = open(fn, 'wb')
-        f.close()
-        f = open(fn, 'rb')
-        ref = weakref.ref(f)
-        attempts = range(10)
-        del f
-        for i in attempts:
-            f1 = ref()
-            if f1 is None:
-                break     # all gone
-            assert not f1.closed   # if still reachable, should be still open
-            del f1
-            gc.collect()
-
-    def test_ValueError(self):
-        fn = self.temptestfile
-        f = open(fn, 'wb')
-        f.close()
-        raises(ValueError, f.fileno)
-        raises(ValueError, f.flush)
-        raises(ValueError, f.isatty)
-        raises(ValueError, f.next)
-        raises(ValueError, f.read)
-        raises(ValueError, f.readline)
-        raises(ValueError, f.readlines)
-        raises(ValueError, f.seek, 0)
-        raises(ValueError, f.tell)
-        raises(ValueError, f.truncate)
-        raises(ValueError, f.write, "")
-        raises(ValueError, f.writelines, [])
-        raises(ValueError, iter, f)
-        raises(ValueError, f.xreadlines)
-        raises(ValueError, f.__enter__)
-        f.close()     # accepted as a no-op
-
-    def test_docstrings(self):
-        assert file.closed.__doc__ == 'True if the file is closed'
-
-    def test_repr_unicode_filename(self):
-        f = open(unicode(self.temptestfile), 'w')
-        assert repr(f).startswith("<open file " + 
-                                  repr(unicode(self.temptestfile)))
-        f.close()
-
diff --git a/pypy/module/_file/test/test_large_file.py b/pypy/module/_file/test/test_large_file.py
deleted file mode 100644
--- a/pypy/module/_file/test/test_large_file.py
+++ /dev/null
@@ -1,69 +0,0 @@
-import py
-
-from pypy.conftest import gettestobjspace
-from pypy.module._file.test.test_file import getfile
-
-class AppTestLargeFile(object):
-    def setup_class(cls):
-        cls.space = gettestobjspace(usemodules=("_file", ))
-        cls.w_temppath = cls.space.wrap(
-            str(py.test.ensuretemp("fileimpl").join("large.data")))
-        cls.w_file = getfile(cls.space)
-
-    def setup_method(self, meth):
-        if getattr(meth, 'need_sparse_files', False):
-            from pypy.module.posix.test.test_posix2 import need_sparse_files
-            need_sparse_files()
-
-    def test_large_seek_offsets(self):
-        FAR = 0x122223333
-        f = self.file(self.temppath, "w+b")
-        f.write("hello world")
-        f.seek(FAR)
-        assert f.tell() == FAR
-        f.seek(-10, 1)
-        assert f.tell() == FAR - 10
-        f.seek(0x123456789, 1)
-        assert f.tell() == FAR - 10 + 0x123456789
-        f.seek(-FAR, 1)
-        assert f.tell() == -10 + 0x123456789
-        f.seek(FAR, 2)
-        assert f.tell() == len("hello world") + FAR
-        f.close()
-
-    def test_large_sparse(self):
-        FAR = 0x122223333
-        f = self.file(self.temppath, "w+b")
-        f.seek(FAR)
-        f.write('end')
-        f.seek(0)
-        data = f.read(1234)
-        assert data == 1234 * '\x00'
-        f.seek(FAR-2-1234, 1)
-        data = f.read(4321)
-        assert data == '\x00\x00end'
-        f.seek(-1, 2)
-        assert f.tell() == FAR + 2
-        f.truncate()
-        f.seek(0, 2)
-        assert f.tell() == FAR + 2
-        f.truncate(FAR + 1)
-        f.seek(FAR-2, 0)
-        data = f.read(1)
-        assert data == '\x00'
-        assert f.tell() == FAR - 1
-        data = f.read(1)
-        assert data == '\x00'
-        data = f.read(1)
-        assert data == 'e'
-        data = f.read(1)
-        assert data == ''
-        assert f.tell() == FAR + 1
-        import sys
-        if FAR > sys.maxint:
-            f.seek(0)
-            raises(OverflowError, f.read, FAR)
-            raises(OverflowError, f.readline, FAR)
-            raises(OverflowError, f.readlines, FAR)
-        f.close()
-    test_large_sparse.need_sparse_files = True


More information about the pypy-commit mailing list