def descr_init(self, space, w_name, mode='r', closefd=True): if space.isinstance_w(w_name, space.w_float): raise oefmt(space.w_TypeError, "integer argument expected, got float") fd = -1 try: fd = space.c_int_w(w_name) except OperationError as e: pass else: if fd < 0: raise oefmt(space.w_ValueError, "negative file descriptor") self.readable, self.writable, self.appending, flags = decode_mode(space, mode) fd_is_own = False try: if fd >= 0: try: os.fstat(fd) except OSError as e: if e.errno == errno.EBADF: raise wrap_oserror(space, e) # else: pass self.fd = fd self.closefd = bool(closefd) else: self.closefd = True if not closefd: raise oefmt(space.w_ValueError, "Cannot use closefd=False with file name") from pypy.module.posix.interp_posix import ( dispatch_filename, rposix) try: self.fd = dispatch_filename(rposix.open)( space, w_name, flags, 0666) except OSError as e: raise wrap_oserror2(space, e, w_name, exception_name='w_IOError') finally: fd_is_own = True self._dircheck(space, w_name) space.setattr(self, space.newtext("name"), w_name) if self.appending: # For consistent behaviour, we explicitly seek to the end of file # (otherwise, it might be done only on the first write()). try: os.lseek(self.fd, 0, os.SEEK_END) except OSError as e: raise wrap_oserror(space, e, exception_name='w_IOError') except: if not fd_is_own: self.fd = -1 raise
def descr_init(self, space, w_name, mode='r', closefd=True): if space.isinstance_w(w_name, space.w_float): raise oefmt(space.w_TypeError, "integer argument expected, got float") fd = -1 try: fd = space.c_int_w(w_name) except OperationError as e: pass else: if fd < 0: raise oefmt(space.w_ValueError, "negative file descriptor") self.readable, self.writable, self.appending, flags = decode_mode(space, mode) fd_is_own = False try: if fd >= 0: try: os.fstat(fd) except OSError as e: if e.errno == errno.EBADF: raise wrap_oserror(space, e) # else: pass self.fd = fd self.closefd = bool(closefd) else: self.closefd = True if not closefd: raise oefmt(space.w_ValueError, "Cannot use closefd=False with file name") from pypy.module.posix.interp_posix import ( dispatch_filename, rposix) try: self.fd = dispatch_filename(rposix.open)( space, w_name, flags, 0666) except OSError as e: raise wrap_oserror2(space, e, w_name, exception_name='w_IOError') finally: fd_is_own = True self._dircheck(space, w_name) space.setattr(self, space.wrap("name"), w_name) if self.appending: # For consistent behaviour, we explicitly seek to the end of file # (otherwise, it might be done only on the first write()). try: os.lseek(self.fd, 0, os.SEEK_END) except OSError as e: raise wrap_oserror(space, e, exception_name='w_IOError') except: if not fd_is_own: self.fd = -1 raise
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, signal_checker(self.space)) fd = stream.try_to_find_file_descriptor() self.check_not_dir(fd) self.fdopenstream(stream, fd, mode)
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, signal_checker(self.space)) fd = stream.try_to_find_file_descriptor() self.check_not_dir(fd) self.fdopenstream(stream, fd, mode)
def descr_init(self, space, w_name, mode='r', closefd=True, w_opener=None): if self.fd >= 0: if self.closefd: self._close(space) else: self.fd = -1 if space.isinstance_w(w_name, space.w_float): raise oefmt(space.w_TypeError, "integer argument expected, got float") fd = -1 try: fd = space.c_int_w(w_name) except OperationError as e: pass else: if fd < 0: raise oefmt(space.w_ValueError, "negative file descriptor") self.readable, self.writable, self.created, self.appending, flags = decode_mode(space, mode) if rposix.O_CLOEXEC is not None: flags |= rposix.O_CLOEXEC fd_is_own = False try: if fd >= 0: self.fd = fd self.closefd = bool(closefd) else: self.closefd = True if not closefd: raise oefmt(space.w_ValueError, "Cannot use closefd=False with file name") if space.is_none(w_opener): from pypy.module.posix.interp_posix import dispatch_filename, fspath w_path = fspath(space, w_name) while True: try: self.fd = dispatch_filename(rposix.open)( space, w_path, flags, 0666) fd_is_own = True break except OSError as e: wrap_oserror2(space, e, w_name, w_exception_class=space.w_IOError, eintr_retry=True) try: _open_inhcache.set_non_inheritable(self.fd) except OSError as e: raise wrap_oserror2(space, e, w_name, eintr_retry=False) else: w_fd = space.call_function(w_opener, w_name, space.newint(flags)) try: self.fd = space.int_w(w_fd) if self.fd < 0: # The opener returned a negative result instead # of raising an exception raise oefmt(space.w_ValueError, "opener returned %d", self.fd) fd_is_own = True except OperationError as e: if not e.match(space, space.w_TypeError): raise raise oefmt(space.w_TypeError, "expected integer from opener") if not rposix._WIN32: try: rposix.set_inheritable(self.fd, False) except OSError as e: raise wrap_oserror2(space, e, w_name, eintr_retry=False) try: st = os.fstat(self.fd) except OSError as e: raise wrap_oserror(space, e, eintr_retry=False) # On Unix, fopen will succeed for directories. # In Python, there should be no file objects referring to # directories, so we need a check. if stat.S_ISDIR(st.st_mode): raise wrap_oserror2(space, OSError(errno.EISDIR, "fstat"), w_name, w_exception_class=space.w_IOError, eintr_retry=False) self.blksize = DEFAULT_BUFFER_SIZE if HAS_BLKSIZE and st.st_blksize > 1: self.blksize = st.st_blksize _setfd_binary(self.fd) space.setattr(self, space.newtext("name"), w_name) if self.appending: # For consistent behaviour, we explicitly seek to the end of file # (otherwise, it might be done only on the first write()). try: os.lseek(self.fd, 0, os.SEEK_END) except OSError as e: raise wrap_oserror(space, e, w_exception_class=space.w_IOError, eintr_retry=False) except: if not fd_is_own: self.fd = -1 self._close(space) raise
except OSError, e: if e.errno == errno.EBADF: raise wrap_oserror(space, e) # else: pass self.fd = fd self.closefd = bool(closefd) else: self.closefd = True if not closefd: raise OperationError(space.w_ValueError, space.wrap( "Cannot use closefd=False with file name")) from pypy.module.posix.interp_posix import ( dispatch_filename, rposix) try: self.fd = dispatch_filename(rposix.open)( space, w_name, flags, 0666) except OSError, e: raise wrap_oserror2(space, e, w_name, exception_name='w_IOError') finally: fd_is_own = True self._dircheck(space, w_name) space.setattr(self, space.wrap("name"), w_name) if self.appending: # For consistent behaviour, we explicitly seek to the end of file # (otherwise, it might be done only on the first write()). try: os.lseek(self.fd, 0, os.SEEK_END) except OSError, e:
if e.errno == errno.EBADF: raise wrap_oserror(space, e) # else: pass self.fd = fd self.closefd = bool(closefd) else: self.closefd = True if not closefd: raise OperationError( space.w_ValueError, space.wrap("Cannot use closefd=False with file name")) from pypy.module.posix.interp_posix import (dispatch_filename, rposix) try: self.fd = dispatch_filename(rposix.open)(space, w_name, flags, 0666) except OSError, e: raise wrap_oserror2(space, e, w_name, exception_name='w_IOError') finally: fd_is_own = True self._dircheck(space, w_name) space.setattr(self, space.wrap("name"), w_name) if self.appending: # For consistent behaviour, we explicitly seek to the end of file # (otherwise, it might be done only on the first write()).