Skip to content

Commit

Permalink
More py2 removal (#1286)
Browse files Browse the repository at this point in the history
* Replace unicode with string

done by following command:
find . -name '*.py' -type f -exec sed -i 's/u"/"/g' {} \;

* Replace all instances of unicode with strings

* remove all instances of 'from __future__ import...'

* trollius is no more

* setup.py: drop py2
  • Loading branch information
om26er authored and oberstet committed Jan 10, 2020
1 parent f84a851 commit d3f674a
Show file tree
Hide file tree
Showing 277 changed files with 2,247 additions and 2,689 deletions.
7 changes: 2 additions & 5 deletions DEVELOPERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,18 @@ That is, **use an assert if the following holds true: if the assert fails, it me
In contrast, to check e.g. for user errors, such as application code using the wrong type when calling into the library, use Exceptions:

```python
import six

def foo(uri):
if type(uri) != six.text_type:
if type(uri) != str:
raise RuntimeError(u"URIs for foo() must be unicode - got {} instead".format(type(uri)))
```

In this specific example, we also have a WAMP defined error (which would be preferred compared to the generic exception used above):

```python
import six
from autobahn.wamp import ApplicationError

def foo(uri):
if type(uri) != six.text_type:
if type(uri) != str:
raise ApplicationError(ApplicationError.INVALID_URI,
u"URIs for foo() must be unicode - got {} instead".format(type(uri)))
```
Expand Down
2 changes: 0 additions & 2 deletions autobahn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#
###############################################################################

from __future__ import absolute_import

from autobahn._version import __version__

version = __version__
4 changes: 0 additions & 4 deletions autobahn/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
#
###############################################################################

from __future__ import absolute_import
from __future__ import print_function


# this module is available as the 'wamp' command-line tool or as
# 'python -m autobahn'

Expand Down
2 changes: 1 addition & 1 deletion autobahn/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
#
###############################################################################

__version__ = u'20.1.1'
__version__ = '20.1.1'
4 changes: 1 addition & 3 deletions autobahn/asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
###############################################################################


from __future__ import absolute_import

import sys
import platform

Expand All @@ -51,7 +49,7 @@
'ApplicationSession',
)

__ident__ = u'Autobahn/{}-asyncio-{}/{}'.format(autobahn.__version__, platform.python_implementation(), '.'.join([str(x) for x in list(sys.version_info[:3])]))
__ident__ = 'Autobahn/{}-asyncio-{}/{}'.format(autobahn.__version__, platform.python_implementation(), '.'.join([str(x) for x in list(sys.version_info[:3])]))
"""
AutobahnPython library implementation (eg. "Autobahn/0.13.0-asyncio-CPython/3.5.1")
"""
47 changes: 19 additions & 28 deletions autobahn/asyncio/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,11 @@
#
###############################################################################


from __future__ import absolute_import, print_function

import ssl # XXX what Python version is this always available at?
import asyncio
import ssl
import signal
from functools import wraps

try:
import asyncio
except ImportError:
# Trollius >= 0.3 was renamed to asyncio
# noinspection PyUnresolvedReferences
import trollius as asyncio

import txaio
txaio.use_asyncio() # noqa

Expand Down Expand Up @@ -71,7 +62,7 @@ def _create_transport_factory(loop, transport, session_factory):
"""
Create a WAMP-over-XXX transport factory.
"""
if transport.type == u'websocket':
if transport.type == 'websocket':
serializers = create_transport_serializers(transport)
factory = WampWebSocketClientFactory(
session_factory,
Expand All @@ -80,7 +71,7 @@ def _create_transport_factory(loop, transport, session_factory):
proxy=transport.proxy, # either None or a dict with host, port
)

elif transport.type == u'rawsocket':
elif transport.type == 'rawsocket':
serializer = create_transport_serializer(transport.serializers[0])
factory = WampRawSocketClientFactory(session_factory, serializer=serializer)

Expand Down Expand Up @@ -130,8 +121,8 @@ def _is_ssl_error(self, e):

def _check_native_endpoint(self, endpoint):
if isinstance(endpoint, dict):
if u'tls' in endpoint:
tls = endpoint[u'tls']
if 'tls' in endpoint:
tls = endpoint['tls']
if isinstance(tls, (dict, bool)):
pass
elif isinstance(tls, ssl.SSLContext):
Expand All @@ -158,7 +149,7 @@ def _connect_transport(self, loop, transport, session_factory, done):
# own method (or three!)...

if transport.proxy:
timeout = transport.endpoint.get(u'timeout', 10) # in seconds
timeout = transport.endpoint.get('timeout', 10) # in seconds
if type(timeout) != int:
raise ValueError('invalid type {} for timeout in client endpoint configuration'.format(type(timeout)))
# do we support HTTPS proxies?
Expand All @@ -171,37 +162,37 @@ def _connect_transport(self, loop, transport, session_factory, done):
time_f = asyncio.ensure_future(asyncio.wait_for(f, timeout=timeout))
return self._wrap_connection_future(transport, done, time_f)

elif transport.endpoint[u'type'] == u'tcp':
elif transport.endpoint['type'] == 'tcp':

version = transport.endpoint.get(u'version', 4)
version = transport.endpoint.get('version', 4)
if version not in [4, 6]:
raise ValueError('invalid IP version {} in client endpoint configuration'.format(version))

host = transport.endpoint[u'host']
host = transport.endpoint['host']
if type(host) != str:
raise ValueError('invalid type {} for host in client endpoint configuration'.format(type(host)))

port = transport.endpoint[u'port']
port = transport.endpoint['port']
if type(port) != int:
raise ValueError('invalid type {} for port in client endpoint configuration'.format(type(port)))

timeout = transport.endpoint.get(u'timeout', 10) # in seconds
timeout = transport.endpoint.get('timeout', 10) # in seconds
if type(timeout) != int:
raise ValueError('invalid type {} for timeout in client endpoint configuration'.format(type(timeout)))

tls = transport.endpoint.get(u'tls', None)
tls = transport.endpoint.get('tls', None)
tls_hostname = None

# create a TLS enabled connecting TCP socket
if tls:
if isinstance(tls, dict):
for k in tls.keys():
if k not in [u"hostname", u"trust_root"]:
if k not in ["hostname", "trust_root"]:
raise ValueError("Invalid key '{}' in 'tls' config".format(k))
hostname = tls.get(u'hostname', host)
hostname = tls.get('hostname', host)
if type(hostname) != str:
raise ValueError('invalid type {} for hostname in TLS client endpoint configuration'.format(hostname))
cert_fname = tls.get(u'trust_root', None)
cert_fname = tls.get('trust_root', None)

tls_hostname = hostname
tls = True
Expand Down Expand Up @@ -232,9 +223,9 @@ def _connect_transport(self, loop, transport, session_factory, done):
time_f = asyncio.ensure_future(asyncio.wait_for(f, timeout=timeout))
return self._wrap_connection_future(transport, done, time_f)

elif transport.endpoint[u'type'] == u'unix':
path = transport.endpoint[u'path']
timeout = int(transport.endpoint.get(u'timeout', 10)) # in seconds
elif transport.endpoint['type'] == 'unix':
path = transport.endpoint['path']
timeout = int(transport.endpoint.get('timeout', 10)) # in seconds

f = loop.create_unix_connection(
protocol_factory=factory,
Expand Down
12 changes: 3 additions & 9 deletions autobahn/asyncio/rawsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@
#
###############################################################################

from __future__ import absolute_import

try:
import asyncio
except ImportError:
# trollious for py2 support - however it has been deprecated
import trollius as asyncio
import asyncio
import struct
import math
import copy
Expand Down Expand Up @@ -388,7 +382,7 @@ def supports_serializer(self, ser_id):
self.abort()
return False

def get_channel_id(self, channel_id_type=u'tls-unique'):
def get_channel_id(self, channel_id_type='tls-unique'):
"""
Implements :func:`autobahn.wamp.interfaces.ITransport.get_channel_id`
"""
Expand All @@ -412,7 +406,7 @@ def serializer_id(self):
self._serializer = copy.copy(self.factory._serializer)
return self._serializer.RAWSOCKET_SERIALIZER_ID

def get_channel_id(self, channel_id_type=u'tls-unique'):
def get_channel_id(self, channel_id_type='tls-unique'):
"""
Implements :func:`autobahn.wamp.interfaces.ITransport.get_channel_id`
"""
Expand Down
6 changes: 3 additions & 3 deletions autobahn/asyncio/test/test_asyncio_rawsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Test(TestCase):
def test_sers(self):
serializers = get_serializers()
self.assertTrue(len(serializers) > 0)
m = serializers[0]().serialize(message.Abort(u'close'))
m = serializers[0]().serialize(message.Abort('close'))
print(m)
self.assertTrue(m)

Expand Down Expand Up @@ -187,7 +187,7 @@ def fact():
s = proto._serializer.RAWSOCKET_SERIALIZER_ID
proto.data_received(bytes(bytearray([0x7F, 0xF0 | s, 0, 0])))
client.onOpen.assert_called_once_with(proto)
proto.send(message.Abort(u'close'))
proto.send(message.Abort('close'))
for d in messages[1:]:
proto.data_received(d)
self.assertTrue(client.onMessage.called)
Expand All @@ -210,7 +210,7 @@ def fact_server():
proto.data_received(bytes(bytearray([0x7F, 0xF0 | s, 0, 0])))
self.assertTrue(proto._serializer)
server.onOpen.assert_called_once_with(proto)
proto.send(message.Abort(u'close'))
proto.send(message.Abort('close'))
for d in messages[1:]:
proto.data_received(d)
self.assertTrue(server.onMessage.called)
Expand Down
10 changes: 4 additions & 6 deletions autobahn/asyncio/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#
###############################################################################

from __future__ import absolute_import

try:
from asyncio import sleep # noqa
except ImportError:
Expand All @@ -52,7 +50,7 @@ def transport_channel_id(transport, is_server, channel_id_type):
received on one TLS channel cannot be forwarded on another.
"""
if channel_id_type not in [u'tls-unique']:
if channel_id_type not in ['tls-unique']:
raise Exception("invalid channel ID type {}".format(channel_id_type))

ssl_obj = transport.get_extra_info('ssl_object')
Expand All @@ -67,11 +65,11 @@ def transport_channel_id(transport, is_server, channel_id_type):
def peer2str(peer):
if isinstance(peer, tuple):
ip_ver = 4 if len(peer) == 2 else 6
return u"tcp{2}:{0}:{1}".format(peer[0], peer[1], ip_ver)
return "tcp{2}:{0}:{1}".format(peer[0], peer[1], ip_ver)
elif isinstance(peer, str):
return u"unix:{0}".format(peer)
return "unix:{0}".format(peer)
else:
return u"?:{0}".format(peer)
return "?:{0}".format(peer)


def get_serializers():
Expand Down
12 changes: 2 additions & 10 deletions autobahn/asyncio/wamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,9 @@
#
###############################################################################

from __future__ import absolute_import
import asyncio
import signal


try:
import asyncio
except ImportError:
# Trollius >= 0.3 was renamed to asyncio
# noinspection PyUnresolvedReferences
import trollius as asyncio

import txaio
txaio.use_asyncio() # noqa

Expand Down Expand Up @@ -189,7 +181,7 @@ def create():
else:
create = make

if self.url.startswith(u'rs'):
if self.url.startswith('rs'):
# try to parse RawSocket URL ..
isSecure, host, port = parse_rs_url(self.url)

Expand Down
Loading

0 comments on commit d3f674a

Please sign in to comment.