def returnConnected(server, client): """Take two Protocol instances and connect them.""" cio = BytesIO() sio = BytesIO() client.makeConnection(FileWrapper(cio)) server.makeConnection(FileWrapper(sio)) pump = IOPump(client, server, cio, sio) # Challenge-response authentication: pump.flush() # Uh... pump.flush() return pump
def main(): """ Main function to be run if __name__ == "__main__". """ config = WorkerOptions() config.parseOptions() from twisted.trial._dist.worker import WorkerProtocol workerProtocol = WorkerProtocol(config['force-gc']) protocolIn = os.fdopen(_WORKER_AMP_STDIN) protocolOut = os.fdopen(_WORKER_AMP_STDOUT, 'w') workerProtocol.makeConnection(FileWrapper(protocolOut)) observer = WorkerLogObserver(workerProtocol) startLoggingWithObserver(observer.emit, False) while True: try: r = protocolIn.read(1) except IOError, e: if e.args[0] == errno.EINTR: sys.exc_clear() continue else: raise if r == '': break else: workerProtocol.dataReceived(r) protocolOut.flush() sys.stdout.flush() sys.stderr.flush()
def test_write(self): """ L{twisted.internet.protocol.FileWrapper.write} """ wrapper = FileWrapper(BytesIO()) wrapper.write(b"test1") self.assertEqual(wrapper.file.getvalue(), b"test1") wrapper = FileWrapper(BytesIO()) # BytesIO() cannot accept unicode, so this will # cause an exception to be thrown which will be # handled by FileWrapper.handle_exception(). wrapper.write("stuff") self.assertNotEqual(wrapper.file.getvalue(), "stuff")
def test_writeSequence(self): """ L{twisted.internet.protocol.FileWrapper.writeSequence} """ wrapper = FileWrapper(BytesIO()) wrapper.writeSequence([b"test1", b"test2"]) self.assertEqual(wrapper.file.getvalue(), b"test1test2") wrapper = FileWrapper(BytesIO()) # In Python 3, b"".join([u"a", u"b"]) will raise a TypeError self.assertRaises(TypeError, wrapper.writeSequence, ["test3", "test4"])
def main(_fdopen=os.fdopen): """ Main function to be run if __name__ == "__main__". @param _fdopen: If specified, the function to use in place of C{os.fdopen}. @param _fdopen: C{callable} """ config = WorkerOptions() config.parseOptions() from twisted.trial._dist.worker import WorkerProtocol workerProtocol = WorkerProtocol(config['force-gc']) protocolIn = _fdopen(_WORKER_AMP_STDIN) protocolOut = _fdopen(_WORKER_AMP_STDOUT, 'w') workerProtocol.makeConnection(FileWrapper(protocolOut)) observer = WorkerLogObserver(workerProtocol) startLoggingWithObserver(observer.emit, False) while True: try: r = protocolIn.read(1) if isinstance(r, unicode): r = r.encode("utf-8") except IOError as e: if e.args[0] == errno.EINTR: if sys.version_info < (3, 0): sys.exc_clear() continue else: raise if r == b'': break else: workerProtocol.dataReceived(r) protocolOut.flush() sys.stdout.flush() sys.stderr.flush() if config.tracer: sys.settrace(None) results = config.tracer.results() results.write_results(show_missing=True, summary=False, coverdir=config.coverdir().path)
def test_writeSequence(self): """ L{twisted.internet.protocol.FileWrapper.writeSequence} """ wrapper = FileWrapper(BytesIO()) wrapper.writeSequence([b"test1", b"test2"]) self.assertEqual(wrapper.file.getvalue(), b"test1test2") wrapper = FileWrapper(BytesIO()) if _PY3: # In Python 3, b"".join([u"a", u"b"]) will raise a TypeError self.assertRaises(TypeError, wrapper.writeSequence, [u"test3", u"test4"]) else: # In Python 2, b"".join([u"a", u"b"]) # will give u"ab", but writing unicode to BytesIO # will throw an exception which will be caught # and ignored by FileWrapper.handle_exception() wrapper.writeSequence([u"test3", u"test4"]) self.assertTrue(len(wrapper.file.getvalue()) == 0)
def test_write(self): """ L{twisted.internet.protocol.FileWrapper.write} """ wrapper = FileWrapper(BytesIO()) wrapper.write(b"test1") self.assertEqual(wrapper.file.getvalue(), b"test1") wrapper = FileWrapper(BytesIO()) # BytesIO() cannot accept unicode, so this will # cause an exception to be thrown which will be # handled by FileWrapper.handle_exception(). wrapper.write(u"stuff") self.assertNotEqual(wrapper.file.getvalue(), u"stuff")
'<!DOCTYPE' and '>' as an argument. """ print '!DOCTYPE', repr(doctype) def gotTagEnd(self, name): '''Encountered closing tag Default behaviour is to print.''' print 'end', name if __name__ == '__main__': from cStringIO import StringIO testDocument = ''' <!DOCTYPE ignore all this shit, hah its malformed!!!!@$> <?xml version="suck it"?> <foo> A <bar /> <baz boz="buz">boz &zop;</baz> <![CDATA[ foo bar baz ]]> </foo> ''' x = XMLParser() x.makeConnection(FileWrapper(StringIO())) # fn = "/home/glyph/Projects/Twisted/doc/howto/ipc10paper.html" fn = "/home/glyph/gruesome.xml" # testDocument = open(fn).read() x.dataReceived(testDocument)
def setUp(self): self.f = StringIO() self.t = FileWrapper(self.f) self.vb = VumiBotProtocol(self.nick, [self.channel], self) self.vb.makeConnection(self.t) self.recvd_messages = []
def __init__(self, stringio, q2qhost, q2qpeer): FileWrapper.__init__(self, stringio) self.q2qhost = q2qhost self.q2qpeer = q2qpeer
def make_dummy_http_request(self): """Make a dummy HTTP request for tests.""" transport = FileWrapper(io.BytesIO()) channel = http.HTTPChannel() channel.makeConnection(transport) return http.Request(channel, True)