From bf0abf911a0a755e881d9ea23fc053a23a21b54e Mon Sep 17 00:00:00 2001 From: nibrag Date: Tue, 10 May 2016 14:37:44 +0300 Subject: [PATCH] Fixed: python 3.4 compability --- aiosocks/protocols.py | 7 ++++++- tests/test_protocol.py | 17 +++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/aiosocks/protocols.py b/aiosocks/protocols.py index f7f1ec3..72a0434 100644 --- a/aiosocks/protocols.py +++ b/aiosocks/protocols.py @@ -10,6 +10,11 @@ from .errors import ( InvalidServerReply, InvalidServerVersion ) +try: + from asyncio import ensure_future +except ImportError: + ensure_future = asyncio.async + class BaseSocksProtocol(asyncio.StreamReaderProtocol): def __init__(self, proxy, proxy_auth, dst, remote_resolve=True, loop=None): @@ -36,7 +41,7 @@ class BaseSocksProtocol(asyncio.StreamReaderProtocol): self._transport = transport req_coro = self.socks_request(c.SOCKS_CMD_CONNECT) - self._negotiate_done = asyncio.ensure_future(req_coro, loop=self._loop) + self._negotiate_done = ensure_future(req_coro, loop=self._loop) @asyncio.coroutine def socks_request(self, cmd): diff --git a/tests/test_protocol.py b/tests/test_protocol.py index 3e12656..31e3f4c 100644 --- a/tests/test_protocol.py +++ b/tests/test_protocol.py @@ -7,6 +7,11 @@ from asyncio import coroutine as coro import aiosocks.constants as c from aiosocks.protocols import BaseSocksProtocol +try: + from asyncio import ensure_future +except ImportError: + ensure_future = asyncio.async + def make_socks4(loop, *, addr=None, auth=None, rr=True, dst=None, r=b''): addr = addr or aiosocks.Socks4Addr('localhost', 1080) @@ -179,7 +184,7 @@ class TestSocks4Protocol(unittest.TestCase): # valid result proto = make_socks4(self.loop, r=valid_resp) - req = asyncio.ensure_future( + req = ensure_future( proto.socks_request(c.SOCKS_CMD_CONNECT), loop=self.loop) self.loop.run_until_complete(req) @@ -323,7 +328,7 @@ class TestSocks5Protocol(unittest.TestCase): # ipv4 proto = make_socks5( self.loop, r=[b'\x01', b'\x7f\x00\x00\x01', b'\x00P']) - req = asyncio.ensure_future(proto.read_address(), loop=self.loop) + req = ensure_future(proto.read_address(), loop=self.loop) self.loop.run_until_complete(req) self.assertEqual(req.result(), ('127.0.0.1', 80)) @@ -335,7 +340,7 @@ class TestSocks5Protocol(unittest.TestCase): b'\x00P' ] proto = make_socks5(self.loop, r=resp) - req = asyncio.ensure_future(proto.read_address(), loop=self.loop) + req = ensure_future(proto.read_address(), loop=self.loop) self.loop.run_until_complete(req) self.assertEqual( @@ -344,7 +349,7 @@ class TestSocks5Protocol(unittest.TestCase): # domain proto = make_socks5( self.loop, r=[b'\x03', b'\n', b'python.org', b'\x00P']) - req = asyncio.ensure_future(proto.read_address(), loop=self.loop) + req = ensure_future(proto.read_address(), loop=self.loop) self.loop.run_until_complete(req) self.assertEqual(req.result(), (b'python.org', 80)) @@ -379,8 +384,8 @@ class TestSocks5Protocol(unittest.TestCase): b'\x01', b'\x7f\x00\x00\x01', b'\x00P'] proto = make_socks5(self.loop, r=resp) - req = asyncio.ensure_future(proto.socks_request(c.SOCKS_CMD_CONNECT), - loop=self.loop) + req = ensure_future(proto.socks_request(c.SOCKS_CMD_CONNECT), + loop=self.loop) self.loop.run_until_complete(req) self.assertEqual(req.result(), (('python.org', 80), ('127.0.0.1', 80)))