def test_is_valid_fd(self): assert rposix.is_valid_fd(0) == 1 fid = open(str(udir.join('validate_test.txt')), 'w') fd = fid.fileno() assert rposix.is_valid_fd(fd) == 1 fid.close() assert rposix.is_valid_fd(fd) == 0
def test_is_valid_fd(self): if os.name != 'nt': py.test.skip('relevant for windows only') assert rposix.is_valid_fd(0) == 1 fid = open(str(udir.join('validate_test.txt')), 'w') fd = fid.fileno() assert rposix.is_valid_fd(fd) == 1 fid.close() assert rposix.is_valid_fd(fd) == 0
def device_encoding(space, fd): """device_encoding(fd) -> str Return a string describing the encoding of the device if the output is a terminal; else return None. """ if not (rposix.is_valid_fd(fd) and os.isatty(fd)): return space.w_None if _WIN32: if fd == 0: return space.wrap('cp%d' % rwin32.GetConsoleCP()) if fd in (1, 2): return space.wrap('cp%d' % rwin32.GetConsoleOutputCP()) from rpython.rlib import rlocale if rlocale.HAVE_LANGINFO: codeset = rlocale.nl_langinfo(rlocale.CODESET) if codeset: return space.wrap(codeset) return space.w_None
def get_fileno_and_file(self, w_file): space = self.space if space.is_none(w_file): w_file = space.sys.get('stderr') if space.is_none(w_file): raise oefmt(space.w_RuntimeError, "sys.stderr is None") elif space.isinstance_w(w_file, space.w_int): fd = space.c_int_w(w_file) if fd < 0 or not is_valid_fd(fd): raise oefmt(space.w_ValueError, "file is not a valid file descriptor") return fd, None fd = space.c_int_w(space.call_method(w_file, 'fileno')) try: space.call_method(w_file, 'flush') except OperationError as e: if e. async (space): raise pass # ignore flush() error
def get_fileno_and_file(self, w_file): space = self.space if space.is_none(w_file): w_file = space.sys.get('stderr') if space.is_none(w_file): raise oefmt(space.w_RuntimeError, "sys.stderr is None") elif space.isinstance_w(w_file, space.w_int): fd = space.c_int_w(w_file) if fd < 0 or not is_valid_fd(fd): raise oefmt(space.w_ValueError, "file is not a valid file descriptor") return fd, None fd = space.c_int_w(space.call_method(w_file, 'fileno')) try: space.call_method(w_file, 'flush') except OperationError as e: if e.async(space): raise pass # ignore flush() error
def _setfd_binary(fd): #Allow this to succeed on invalid fd's if rposix.is_valid_fd(fd): _setmode(fd, os.O_BINARY)
def _setfd_binary(fd): # Allow this to succeed on invalid fd's if rposix.is_valid_fd(fd): _setmode(fd, os.O_BINARY)
def fclose(fp): if not is_valid_fd(fileno(fp)): return -1 return _fclose(fp)
def is_valid_fp(fp): return is_valid_fd(fileno(fp))