def _gettextwriter(out, encoding): if out is None: import sys out = sys.stdout if isinstance(out, io.RawIOBase): buffer = io.BufferedIOBase(out) buffer.close = lambda: None else: buffer = io.BufferedIOBase() buffer.writable = lambda: True buffer.write = out.write try: buffer.seekable = out.seekable buffer.tell = out.tell except AttributeError: pass class UnbufferedTextIOWrapper(io.TextIOWrapper): def write(self, s): super(UnbufferedTextIOWrapper, self).write(s) self.flush() return UnbufferedTextIOWrapper(buffer, encoding=encoding, errors='xmlcharrefreplace', newline='\n')
def _gettextwriter(out, encoding): if out is None: import sys out = sys.stdout if isinstance(out, io.RawIOBase): buffer = io.BufferedIOBase(out) # Keep the original file open when the TextIOWrapper is # destroyed buffer.close = lambda: None else: # This is to handle passed objects that aren't in the # IOBase hierarchy, but just have a write method buffer = io.BufferedIOBase() buffer.writable = lambda: True buffer.write = out.write try: # TextIOWrapper uses this methods to determine # if BOM (for UTF-16, etc) should be added buffer.seekable = out.seekable buffer.tell = out.tell except AttributeError: pass # wrap a binary writer with TextIOWrapper class UnbufferedTextIOWrapper(io.TextIOWrapper): def write(self, s): super(UnbufferedTextIOWrapper, self).write(s) self.flush() return UnbufferedTextIOWrapper(buffer, encoding=encoding, errors='xmlcharrefreplace', newline='\n')
def test_openclose(self): import io with io.BufferedIOBase() as f: assert not f.closed f._checkClosed() assert f.closed raises(ValueError, f._checkClosed)
def test_delegated_methods(self, method, mocker): self.slipstream = SlipStream(io.BufferedIOBase()) mocker.patch.object(sliplib.io.BufferedIOBase, method) getattr(sliplib.io.BufferedIOBase, method).reset_mock() getattr(self.slipstream, method)() # Don't care about the arguments getattr(sliplib.io.BufferedIOBase, method).assert_called_once_with() self.slipstream.close()
def _gettextwriter(out, encoding): if out is None: import sys return sys.stdout if isinstance(out, io.TextIOBase): return out if isinstance(out, (codecs.StreamWriter, codecs.StreamReaderWriter)): return out if isinstance(out, io.RawIOBase): class _wrapper: __qualname__ = '_gettextwriter.<locals>._wrapper' __class__ = out.__class__ def __getattr__(self, name): return getattr(out, name) buffer = _wrapper() buffer.close = lambda : None else: buffer = io.BufferedIOBase() buffer.writable = lambda : True buffer.write = out.write try: buffer.seekable = out.seekable buffer.tell = out.tell except AttributeError: pass return io.TextIOWrapper(buffer, encoding=encoding, errors='xmlcharrefreplace', newline='\n', write_through=True)
def _get_writer(file_or_filename, encoding): try: write = file_or_filename.write except AttributeError: if encoding == 'unicode': file = open(file_or_filename, 'w') else: file = open(file_or_filename, 'w', encoding=encoding, errors='xmlcharrefreplace') with file: yield file.write if encoding == 'unicode': yield write else: with contextlib.ExitStack() as stack: if isinstance(file_or_filename, io.BufferedIOBase): file = file_or_filename elif isinstance(file_or_filename, io.RawIOBase): file = io.BufferedWriter(file_or_filename) stack.callback(file.detach) else: file = io.BufferedIOBase() file.writable = lambda : True file.write = write try: file.seekable = file_or_filename.seekable file.tell = file_or_filename.tell except AttributeError: pass file = io.TextIOWrapper(file, encoding=encoding, errors='xmlcharrefreplace', newline='\n') stack.callback(file.detach) yield file.write
def SetUp(self): self.SelectApi( 'v1' if self.track.prefix is None else self.track.prefix) self.c_store_load = self.StartObjectPatch( c_store, 'LoadIfEnabled', autospec=True, return_value='an access token') self.c_store_refresh = self.StartObjectPatch(c_store, 'Refresh', autospec=True, return_value=None) # Mock how StdinSocket accesses stdin. if platforms.OperatingSystem.IsWindows(): self.StartPatch('ctypes.windll.kernel32.GetStdHandle', autospec=True) self.StartPatch('ctypes.windll.kernel32.ReadFile') read_data = [b'data1', b'data2', b''] def ReadFile(unused_h, buf, unused_bufsize, number_of_bytes_read, unused_overlapped): b = read_data.pop(0) number_of_bytes_read._obj.value = len(b) buf.raw = b return 1 ctypes.windll.kernel32.ReadFile.side_effect = ReadFile else: self.StartPatch('fcntl.fcntl', autospec=True) if six.PY2: self.StartPatch('sys.stdin', autospec=True) self.stdin_mock = sys.stdin else: # Manually create an autospec object because sys.stdin has already been # replaced by FakeStd which autospec can't handle buffer correctly. self.StartPatch('sys.stdin', autospec=io.TextIOWrapper(io.BufferedIOBase())) self.stdin_mock = sys.stdin.buffer self.stdin_mock.read.side_effect = [b'data1', b'data2', b''] # WebSocket mock. self.websocket_mock = mock.MagicMock() def WebSocketInitGenerator(): yield self.websocket_mock raise RuntimeError('No more websocket mocks') self.websocket_cls_mock = self.StartObjectPatch( iap_tunnel_websocket, 'IapTunnelWebSocket', autospec=True, side_effect=WebSocketInitGenerator())
def _get_writer(file_or_filename, encoding): # returns text write method and release all resources after using try: write = file_or_filename.write except AttributeError: # file_or_filename is a file name f = open( file_or_filename, "w", encoding="utf-8" if encoding == "unicode" else encoding, errors="xmlcharrefreplace", ) with f: yield f.write else: # file_or_filename is a file-like object # encoding determines if it is a text or binary writer if encoding == "unicode": # use a text writer as is yield write else: # wrap a binary writer with TextIOWrapper detach_buffer = False if isinstance(file_or_filename, io.BufferedIOBase): buf = file_or_filename elif isinstance(file_or_filename, io.RawIOBase): buf = io.BufferedWriter(file_or_filename) detach_buffer = True else: # This is to handle passed objects that aren't in the # IOBase hierarchy, but just have a write method buf = io.BufferedIOBase() buf.writable = lambda: True buf.write = write try: # TextIOWrapper uses this methods to determine # if BOM (for UTF-16, etc) should be added buf.seekable = file_or_filename.seekable buf.tell = file_or_filename.tell except AttributeError: pass wrapper = io.TextIOWrapper( buf, encoding=encoding, errors="xmlcharrefreplace", newline="\n", ) try: yield wrapper.write finally: # Keep the original file open when the TextIOWrapper and # the BufferedWriter are destroyed wrapper.detach() if detach_buffer: buf.detach()
def testFlush(self): # Autospeccing sys.stdout should work, but Kokoro sets --capture=fd which # causes sys.stdout to be replaced with something that autospec can't handle # correctly. # This autospec makes sys.stdout look like python3, but that still works # well enough in python2 for the purpose of this test. self.StartPatch('sys.stdout', autospec=io.TextIOWrapper(io.BufferedIOBase())) sock = iap_tunnel._StdinSocket() sock.send(b'data1') sock.send(b'a\na\0a\xffa') if six.PY2: self.assertEqual(sys.stdout.flush.call_count, 2) else: self.assertEqual(sys.stdout.buffer.flush.call_count, 2)
def _gettextwriter(out, encoding): if out is None: import sys return sys.stdout if isinstance(out, io.TextIOBase): # use a text writer as is return out if isinstance(out, (codecs.StreamWriter, codecs.StreamReaderWriter)): # use a codecs stream writer as is return out # wrap a binary writer with TextIOWrapper if isinstance(out, io.RawIOBase): # Keep the original file open when the TextIOWrapper is # destroyed class _wrapper: __class__ = out.__class__ def __getattr__(self, name): return getattr(out, name) buffer = _wrapper() buffer.close = lambda: None else: # This is to handle passed objects that aren't in the # IOBase hierarchy, but just have a write method buffer = io.BufferedIOBase() buffer.writable = lambda: True buffer.write = out.write try: # TextIOWrapper uses this methods to determine # if BOM (for UTF-16, etc) should be added buffer.seekable = out.seekable buffer.tell = out.tell except AttributeError: pass return io.TextIOWrapper(buffer, encoding=encoding, errors='xmlcharrefreplace', newline='\n', write_through=True)
try: # python 2.6 import fractions import number import io from io import StringIO as TextIO # built-in functions (CH 2) a['ByteArrayType'] = bytearray([1]) # numeric and mathematical types (CH 9) a['FractionType'] = fractions.Fraction() a['NumberType'] = numbers.Number() # generic operating system services (CH 15) a['IOBaseType'] = io.IOBase() a['RawIOBaseType'] = io.RawIOBase() a['TextIOBaseType'] = io.TextIOBase() a['BufferedIOBaseType'] = io.BufferedIOBase() a['UnicodeIOType'] = TextIO() # the new StringIO a['LoggingAdapterType'] = logging.LoggingAdapter(_logger, _dict) # pickle ok if HAS_CTYPES: a['CBoolType'] = ctypes.c_bool(1) a['CLongDoubleType'] = ctypes.c_longdouble() except ImportError: pass try: # python 2.7 import argparse # data types (CH 8) a['OrderedDictType'] = collections.OrderedDict(_dict) a['CounterType'] = collections.Counter(_dict) if HAS_CTYPES: a['CSSizeTType'] = ctypes.c_ssize_t()
def __init__(self, port, baud): self.port = port self.baud = baud self.ser = serial.Serial(port, baud) self.sio = io.TextIOWrapper(io.BufferedIOBase(self.ser, self.ser))
def test_openclose(self): import io with io.BufferedIOBase() as f: assert not f.closed assert f.closed
def test_exception_for_not_supported_operations(self, method): self.slipstream = SlipStream(io.BufferedIOBase()) with pytest.raises(AttributeError): getattr(self.slipstream, method) self.slipstream.close()