From 008cd4810ae8857f6527a1fc0923c8d06b21e68e Mon Sep 17 00:00:00 2001 From: Alexander Akulich Date: Sun, 1 Nov 2020 15:37:04 +0300 Subject: Port tests to Python 3 --- tests/twisted/channels/join-muc-channel-bouncer.py | 2 +- tests/twisted/channels/join-muc-channel.py | 2 +- tests/twisted/channels/muc-channel-topic.py | 2 +- tests/twisted/channels/room-list-channel.py | 4 ++-- tests/twisted/channels/room-list-multiple.py | 2 +- tests/twisted/cm/protocol.py | 2 +- tests/twisted/connect/connect-success-ssl.py | 2 +- tests/twisted/connect/connect-success.py | 2 +- tests/twisted/connect/invalid-nick.py | 14 ++++++------ tests/twisted/idletest.py | 8 +++---- tests/twisted/messages/contactinfo-request.py | 2 +- tests/twisted/messages/invalid-utf8.py | 15 ++++++------- tests/twisted/servicetest.py | 26 +++++++++++----------- 13 files changed, 41 insertions(+), 42 deletions(-) diff --git a/tests/twisted/channels/join-muc-channel-bouncer.py b/tests/twisted/channels/join-muc-channel-bouncer.py index 09f12a1..e2e85fb 100644 --- a/tests/twisted/channels/join-muc-channel-bouncer.py +++ b/tests/twisted/channels/join-muc-channel-bouncer.py @@ -41,7 +41,7 @@ def test(q, bus, conn, stream): EventPattern('dbus-signal', signal='StatusChanged', args=[1, 1]), EventPattern('irc-connected')) q.expect('dbus-signal', signal='SelfHandleChanged', - args=[1L]) + args=[1]) q.expect('dbus-signal', signal='StatusChanged', args=[0, 1]) CHANNEL_NAME = "#idletest" diff --git a/tests/twisted/channels/join-muc-channel.py b/tests/twisted/channels/join-muc-channel.py index 613756e..2ea9d28 100644 --- a/tests/twisted/channels/join-muc-channel.py +++ b/tests/twisted/channels/join-muc-channel.py @@ -14,7 +14,7 @@ def test(q, bus, conn, stream): EventPattern('dbus-signal', signal='StatusChanged', args=[1, 1]), EventPattern('irc-connected')) q.expect('dbus-signal', signal='SelfHandleChanged', - args=[1L]) + args=[1]) q.expect('dbus-signal', signal='StatusChanged', args=[0, 1]) room_handles = conn.RequestHandles(HT_ROOM, ['#idletest']) call_async(q, conn, 'RequestChannel', CHANNEL_TYPE_TEXT, HT_ROOM, room_handles[0], True) diff --git a/tests/twisted/channels/muc-channel-topic.py b/tests/twisted/channels/muc-channel-topic.py index c460ece..58accb1 100644 --- a/tests/twisted/channels/muc-channel-topic.py +++ b/tests/twisted/channels/muc-channel-topic.py @@ -88,7 +88,7 @@ def test(q, bus, conn, stream): # No topic set subject_props = channel.Properties.GetAll(CHANNEL_IFACE_SUBJECT) assertEquals('', subject_props['Subject']) - assertEquals(0x7fffffffffffffffL, subject_props['Timestamp']) + assertEquals(0x7fffffffffffffff, subject_props['Timestamp']) assertEquals('', subject_props['Actor']) assertEquals(0, subject_props['ActorHandle']) diff --git a/tests/twisted/channels/room-list-channel.py b/tests/twisted/channels/room-list-channel.py index 6200904..0907f54 100644 --- a/tests/twisted/channels/room-list-channel.py +++ b/tests/twisted/channels/room-list-channel.py @@ -42,7 +42,7 @@ def test(q, bus, conn, stream): EventPattern('dbus-signal', signal='StatusChanged', args=[1, 1]), EventPattern('irc-connected')) q.expect('dbus-signal', signal='SelfHandleChanged', - args=[1L]) + args=[1]) call_async(q, conn, 'CreateChannel', { cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_ROOM_LIST }, @@ -65,7 +65,7 @@ def test(q, bus, conn, stream): predicate=looks_like_a_room_list) chan = bus.get_object(conn.bus_name, path) - list_chan = dbus.Interface(chan, tp_name_prefix + u'.Channel.Type.RoomList') + list_chan = dbus.Interface(chan, tp_name_prefix + '.Channel.Type.RoomList') list_chan.ListRooms(); q.expect('dbus-signal', signal='GotRooms', predicate=lambda x:check_rooms(x.args[0])) diff --git a/tests/twisted/channels/room-list-multiple.py b/tests/twisted/channels/room-list-multiple.py index 568f4b4..06b138f 100644 --- a/tests/twisted/channels/room-list-multiple.py +++ b/tests/twisted/channels/room-list-multiple.py @@ -18,7 +18,7 @@ def test(q, bus, conn, stream): EventPattern('dbus-signal', signal='StatusChanged', args=[1, 1]), EventPattern('irc-connected')) q.expect('dbus-signal', signal='SelfHandleChanged', - args=[1L]) + args=[1]) # request a roomlist channel call_async(q, conn, 'CreateChannel', diff --git a/tests/twisted/cm/protocol.py b/tests/twisted/cm/protocol.py index 8fa3ece..f972dba 100644 --- a/tests/twisted/cm/protocol.py +++ b/tests/twisted/cm/protocol.py @@ -31,7 +31,7 @@ def test(q, bus, conn, server): proto_props = unwrap(proto_prop_iface.GetAll(cs.PROTOCOL)) for key in ['Parameters', 'Interfaces', 'ConnectionInterfaces', - 'RequestableChannelClasses', u'VCardField', u'EnglishName', u'Icon']: + 'RequestableChannelClasses', 'VCardField', 'EnglishName', 'Icon']: a = local_props[cs.PROTOCOL + '.' + key] b = proto_props[key] assertEquals(a, b) diff --git a/tests/twisted/connect/connect-success-ssl.py b/tests/twisted/connect/connect-success-ssl.py index 33062c5..8456a4a 100644 --- a/tests/twisted/connect/connect-success-ssl.py +++ b/tests/twisted/connect/connect-success-ssl.py @@ -21,7 +21,7 @@ def test(q, bus, conn, stream): cert.Accept() q.expect('dbus-signal', signal='SelfHandleChanged', - args=[1L]) + args=[1]) q.expect('dbus-signal', signal='StatusChanged', args=[0, 1]) call_async(q, conn, 'Disconnect') q.expect_many( diff --git a/tests/twisted/connect/connect-success.py b/tests/twisted/connect/connect-success.py index 3281764..82dd34e 100644 --- a/tests/twisted/connect/connect-success.py +++ b/tests/twisted/connect/connect-success.py @@ -12,7 +12,7 @@ def test(q, bus, conn, stream): EventPattern('dbus-signal', signal='StatusChanged', args=[1, 1]), EventPattern('irc-connected')) q.expect('dbus-signal', signal='SelfHandleChanged', - args=[1L]) + args=[1]) q.expect('dbus-signal', signal='StatusChanged', args=[0, 1]) call_async(q, conn, 'Disconnect') q.expect_many( diff --git a/tests/twisted/connect/invalid-nick.py b/tests/twisted/connect/invalid-nick.py index 44c1f92..b824fae 100644 --- a/tests/twisted/connect/invalid-nick.py +++ b/tests/twisted/connect/invalid-nick.py @@ -28,37 +28,37 @@ def test(): try: connect('nick with spaces') raise RuntimeError('Invalid nick not rejected') - except dbus.DBusException, e: + except dbus.DBusException as e: assert e.get_dbus_name() == INVALID_HANDLE try: connect('') # empty nick raise RuntimeError('Invalid nick not rejected') - except dbus.DBusException, e: + except dbus.DBusException as e: assert e.get_dbus_name() == INVALID_HANDLE try: connect('#foo') # invalid chars raise RuntimeError('Invalid nick not rejected') - except dbus.DBusException, e: + except dbus.DBusException as e: assert e.get_dbus_name() == INVALID_HANDLE try: - connect(u'김정은') # unicode + connect('김정은') # unicode raise RuntimeError('Invalid nick not rejected') - except dbus.DBusException, e: + except dbus.DBusException as e: assert e.get_dbus_name() == INVALID_HANDLE try: connect('12foo') # numbers not allowed as first char raise RuntimeError('Invalid nick not rejected') - except dbus.DBusException, e: + except dbus.DBusException as e: assert e.get_dbus_name() == INVALID_HANDLE try: connect('-foo') # '-' not allowed as first char raise RuntimeError('Invalid nick not rejected') - except dbus.DBusException, e: + except dbus.DBusException as e: assert e.get_dbus_name() == INVALID_HANDLE # should pass succeed without an exception diff --git a/tests/twisted/idletest.py b/tests/twisted/idletest.py index b1ad857..bf080e4 100644 --- a/tests/twisted/idletest.py +++ b/tests/twisted/idletest.py @@ -146,9 +146,9 @@ class BaseIRCServer(irc.IRC): f = getattr(self, 'handle%s' % command) try: f(params, prefix) - except Exception, e: + except Exception as e: self.log('handler failed: %s' % e) - except Exception, e: + except Exception as e: self.log('No handler for command %s: %s' % (command, e)) class SSLIRCServer(BaseIRCServer): @@ -244,7 +244,7 @@ def exec_test_deferred (funs, params, protocol=None, timeout=None): for f in funs: conn = make_connection(bus, queue.append, params) f(queue, bus, conn, server) - except Exception, e: + except Exception as e: import traceback traceback.print_exc() error = e @@ -271,7 +271,7 @@ def exec_test_deferred (funs, params, protocol=None, timeout=None): # exited and refdbg can generate its report time.sleep(5.5) - except dbus.DBusException, e: + except dbus.DBusException as e: pass def exec_tests(funs, params=None, protocol=None, timeout=None): diff --git a/tests/twisted/messages/contactinfo-request.py b/tests/twisted/messages/contactinfo-request.py index 1350afe..cd524a0 100644 --- a/tests/twisted/messages/contactinfo-request.py +++ b/tests/twisted/messages/contactinfo-request.py @@ -38,7 +38,7 @@ def test(q, bus, conn, stream): EventPattern('dbus-signal', signal='StatusChanged', args=[1, 1]), EventPattern('irc-connected')) q.expect('dbus-signal', signal='SelfHandleChanged', - args=[1L]) + args=[1]) q.expect('dbus-signal', signal='StatusChanged', args=[0, 1]) self_handle = conn.GetSelfHandle() diff --git a/tests/twisted/messages/invalid-utf8.py b/tests/twisted/messages/invalid-utf8.py index 725e3bd..c3a4389 100644 --- a/tests/twisted/messages/invalid-utf8.py +++ b/tests/twisted/messages/invalid-utf8.py @@ -14,7 +14,7 @@ def test(q, bus, conn, stream): q.expect('dbus-signal', signal='StatusChanged', args=[0, 1]) test_with_message(q, stream, ["I'm no ", " Buddhist"]) - test_with_message(q, stream, [u"björk"] * 3) + test_with_message(q, stream, ["björk"] * 3) test_with_message(q, stream, ["", "lolllllll"]) test_with_message(q, stream, ["hello", ""]) @@ -28,12 +28,11 @@ def test(q, bus, conn, stream): WELL_FORMED_BUT_INVALID_UTF8_BYTES = "\xed\xa0\x80" def test_with_message(q, stream, parts): - invalid_utf8 = WELL_FORMED_BUT_INVALID_UTF8_BYTES.join( - part.encode('utf-8') for part in parts) + invalid_utf8 = WELL_FORMED_BUT_INVALID_UTF8_BYTES.join(parts) # Idle's default character set is UTF-8. We send it a message which is # basically UTF-8, except that one of its code points is invalid. - stream.sendMessage('PRIVMSG', bytes(stream.nick), ':%s' % invalid_utf8, + stream.sendMessage('PRIVMSG', stream.nick, ':%s' % invalid_utf8, prefix='remoteuser') # Idle should signal that *something* was received. If it hasn't validated @@ -46,17 +45,17 @@ def test_with_message(q, stream, parts): # Don't make any assumption about how many U+FFFD REPLACEMENT CHARACTERs # are used to replace surprising bytes. - received_parts = [ part for part in re.split(u"\ufffd|\\?", content) - if part != u'' + received_parts = [ part for part in re.split("\ufffd|\\?", content) + if part != '' ] - if parts[0] == u'björk': + if parts[0] == 'björk': # The valid UTF-8 gets lost in transit, because we fall back # to assuming ASCII when g_convert() fails (this didn't happen # when we tested with noncharacters - oh well). assertEquals(['bj', 'rk', 'bj', 'rk', 'bj', 'rk'], received_parts) else: - assertEquals(filter(lambda s: s != u'', parts), received_parts) + assertEquals([s for s in parts if s != ''], received_parts) if __name__ == '__main__': exec_test(test) diff --git a/tests/twisted/servicetest.py b/tests/twisted/servicetest.py index 3065e15..f4b50c9 100644 --- a/tests/twisted/servicetest.py +++ b/tests/twisted/servicetest.py @@ -3,9 +3,9 @@ Infrastructure code for testing connection managers. """ -from twisted.internet import glib2reactor +from twisted.internet import gireactor from twisted.internet.protocol import Protocol, Factory, ClientFactory -glib2reactor.install() +gireactor.install() import sys import pprint @@ -62,7 +62,7 @@ class EventPattern: if event.type != self.type: return False - for key, value in self.properties.iteritems(): + for key, value in self.properties.items(): try: if getattr(event, key) != value: return False @@ -95,14 +95,14 @@ class BaseEventQueue: def log(self, s): if self.verbose: - print s + print(s) def log_event(self, event): if self.verbose: self.log('got event:') if self.verbose: - map(self.log, format_event(event)) + list(map(self.log, format_event(event))) def forbid_events(self, patterns): """ @@ -123,9 +123,9 @@ class BaseEventQueue: def _check_forbidden(self, event): for e in self.forbidden_events: if e.match(event): - print "forbidden event occurred:" + print("forbidden event occurred:") for x in format_event(event): - print x + print(x) assert False def expect(self, type, **kw): @@ -303,18 +303,18 @@ def unwrap(x): printed.""" if isinstance(x, list): - return map(unwrap, x) + return list(map(unwrap, x)) if isinstance(x, tuple): return tuple(map(unwrap, x)) if isinstance(x, dict): - return dict([(unwrap(k), unwrap(v)) for k, v in x.iteritems()]) + return dict([(unwrap(k), unwrap(v)) for k, v in x.items()]) if isinstance(x, dbus.Boolean): return bool(x) - for t in [unicode, str, long, int, float]: + for t in [str, str, int, int, float]: if isinstance(x, t): return t(x) @@ -354,7 +354,7 @@ class ProxyWrapper: dbus.Interface(object, tp_name_prefix + '.Properties') self.interfaces = dict([ (name, dbus.Interface(object, iface)) - for name, iface in others.iteritems()]) + for name, iface in others.items()]) def __getattr__(self, name): if name in self.interfaces: @@ -409,7 +409,7 @@ def make_connection(bus, event_func, name, proto, params): event_func( Event('dbus-signal', path=unwrap(kw['path']), - signal=kw['member'], args=map(unwrap, args), + signal=kw['member'], args=list(map(unwrap, args)), interface=kw['interface'])), None, # signal name None, # interface @@ -472,7 +472,7 @@ def watch_tube_signals(q, tube): q.handle_event(Event('tube-signal', path=kwargs['path'], signal=kwargs['member'], - args=map(unwrap, args), + args=list(map(unwrap, args)), tube=tube)) tube.add_signal_receiver(got_signal_cb, -- cgit v1.2.1