def fileio_helper(): bytes_io_list = bytesio_helper() file_io_list = [] for i in xrange(len(bytes_io_list)): f = FileIO(TEMP_READINTO_NAME % i, "w") f.write(bytes_io_list[i]) f.close() file_io_list.append(FileIO(TEMP_READINTO_NAME % i, "r")) return file_io_list
def fileio_helper(): bytes_io_list = bytesio_helper() file_io_list = [] for i in xrange(len(bytes_io_list)): with FileIO(TEMP_READINTO_NAME % i, "w") as f: f.write(bytes_io_list[i]) file_io_list.append(FileIO(TEMP_READINTO_NAME % i, "r")) return file_io_list
def fileio_helper(temp_readinto_name_template): bytes_io_list = bytesio_helper() file_io_list = [] for i in range(len(bytes_io_list)): with FileIO(temp_readinto_name_template % i, "w") as f: f.write(bytes_io_list[i]) file_io_list.append(FileIO(temp_readinto_name_template % i, "r")) return file_io_list
def test__FileIO_readall(self): r, w = os.pipe() wf = FileIO(w, 'w') def writefile(): time.sleep(0.05) wf.write(bytes(bytearray(b'abc'))) wf.close() wf.write(bytes(bytearray(b'abc\n'))) t = threading.Thread(target=writefile) t.start() with FileIO(r, 'r') as rf: self.assertEqual(rf.readall(), b'abc\nabc') t.join()
def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True): if not isinstance(file, (basestring, int, long)): raise TypeError('invalid file: %r' % file) if not isinstance(mode, basestring): raise TypeError('invalid mode: %r' % mode) if not isinstance(buffering, (int, long)): raise TypeError('invalid buffering: %r' % buffering) if encoding is not None and not isinstance(encoding, basestring): raise TypeError('invalid encoding: %r' % encoding) if errors is not None and not isinstance(errors, basestring): raise TypeError('invalid errors: %r' % errors) modes = set(mode) if modes - set('arwb+tU') or len(mode) > len(modes): raise ValueError('invalid mode: %r' % mode) reading = 'r' in modes writing = 'w' in modes appending = 'a' in modes updating = '+' in modes text = 't' in modes binary = 'b' in modes if 'U' in modes: if writing or appending: raise ValueError("can't use U and writing mode at once") reading = True if text and binary: raise ValueError("can't have text and binary mode at once") if reading + writing + appending > 1: raise ValueError("can't have read/write/append mode at once") if not (reading or writing or appending): raise ValueError('must have exactly one of read/write/append mode') if binary and encoding is not None: raise ValueError("binary mode doesn't take an encoding argument") if binary and errors is not None: raise ValueError("binary mode doesn't take an errors argument") if binary and newline is not None: raise ValueError("binary mode doesn't take a newline argument") raw = FileIO(file, (reading and 'r' or '') + (writing and 'w' or '') + (appending and 'a' or '') + (updating and '+' or ''), closefd) line_buffering = False if buffering == 1 or buffering < 0 and raw.isatty(): buffering = -1 line_buffering = True if buffering < 0: buffering = DEFAULT_BUFFER_SIZE try: bs = os.fstat(raw.fileno()).st_blksize except (os.error, AttributeError): pass else: if bs > 1: buffering = bs if buffering < 0: raise ValueError('invalid buffering size') if buffering == 0: if binary: return raw raise ValueError("can't have unbuffered text I/O") if updating: buffer = BufferedRandom(raw, buffering) else: if writing or appending: buffer = BufferedWriter(raw, buffering) else: if reading: buffer = BufferedReader(raw, buffering) else: raise ValueError('unknown mode: %r' % mode) if binary: return buffer text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering) text.mode = mode return text
def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None): if not isinstance(file, (str, bytes, int)): raise TypeError('invalid file: %r' % file) if not isinstance(mode, str): raise TypeError('invalid mode: %r' % mode) if not isinstance(buffering, int): raise TypeError('invalid buffering: %r' % buffering) if encoding is not None and not isinstance(encoding, str): raise TypeError('invalid encoding: %r' % encoding) if errors is not None and not isinstance(errors, str): raise TypeError('invalid errors: %r' % errors) modes = set(mode) if modes - set('axrwb+tU') or len(mode) > len(modes): raise ValueError('invalid mode: %r' % mode) creating = 'x' in modes reading = 'r' in modes writing = 'w' in modes appending = 'a' in modes updating = '+' in modes text = 't' in modes binary = 'b' in modes if 'U' in modes: if creating or writing or appending: raise ValueError("can't use U and writing mode at once") reading = True if text and binary: raise ValueError("can't have text and binary mode at once") if creating + reading + writing + appending > 1: raise ValueError("can't have read/write/append mode at once") if not (creating or (reading or (writing or appending))): raise ValueError('must have exactly one of read/write/append mode') if binary and encoding is not None: raise ValueError("binary mode doesn't take an encoding argument") if binary and errors is not None: raise ValueError("binary mode doesn't take an errors argument") if binary and newline is not None: raise ValueError("binary mode doesn't take a newline argument") raw = FileIO(file, (creating and 'x' or '') + (reading and 'r' or '') + (writing and 'w' or '') + (appending and 'a' or '') + (updating and '+' or ''), closefd, opener=opener) line_buffering = False if buffering == 1 or buffering < 0 and raw.isatty(): buffering = -1 line_buffering = True if buffering < 0: buffering = DEFAULT_BUFFER_SIZE try: bs = os.fstat(raw.fileno()).st_blksize except (os.error, AttributeError): pass if bs > 1: buffering = bs if buffering < 0: raise ValueError('invalid buffering size') if buffering == 0: if binary: return raw raise ValueError("can't have unbuffered text I/O") if updating: buffer = BufferedRandom(raw, buffering) elif creating or writing or appending: buffer = BufferedWriter(raw, buffering) elif reading: buffer = BufferedReader(raw, buffering) else: raise ValueError('unknown mode: %r' % mode) if binary: return buffer text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering) text.mode = mode return text
def fill_qx(qxfile: _io.FileIO, lexer: dict): # header if lexer.get('header'): qxfile.write("header\n{") qxfile.write(lexer['header']) qxfile.write("}\n\n") # define if lexer.get('define'): qxfile.write("define\n{") for key, val in lexer['define'].items(): qxfile.write("\t{}\t{}\n".format(key, val)) qxfile.write("}\n\n") # todo token? # body if lexer.get('body'): qxfile.write("body\n{") qxfile.write(lexer['body']) qxfile.write("}\n\n") # init if lexer.get('init'): qxfile.write("init\n{") qxfile.write(lexer['init']) qxfile.write("}\n\n") # todo modes for mod in lexer['modes']: if type(mod) is dict: pass else: # mindenképpen str qxfile.write("start = {};\n".format(mod)) pass