Python 3 compatibility fixes and other fixes
These patches make gajim more compatible with python 3 and make the code more idiomatic.
The patches were partly autogenerated with the 2to3 tool and manually checked for errors. The patches are not meant to change functionality in any way. The code should work as before. If this is not the case, it should be considered a bug.
All patches are against r10504 and should be applied in the given order. There are probably lurking some conflicts, when applied out of order, but you can try to pick only the patches you want.
I can recommend the 2to3 tool for refactoring (many lines of) code. I wrote some custom fixers, far from being 100% precise, but usable. You should check the result manually -- you have been warned ;) They are attached, too.
01_remove_dict_keys_call_in_for_loop.patch:
Drops the dict.keys method in for loops which do not not modify the dict.
Rationale: Less code, code more polymorphic (every iterable would work).
02_replace_if_statement_with_dict_get.patch:
if key in dict: -> value = dict.get(key, default)
value = dict[key]
else:
value = default
value = default -> value = dict.get(key, default)
if key in dict[key]:
value = dict[key]
Rationale: Much shorter, easier to read, faster.
03_use_startswith_and_endswith_instead_of_array_indices_on_strings.patch:
s1[:x] == s2 -> s1.startswith(s2)
s1[x:] == s2 -> s1.endswith(s2)
Rationale: Easier to comprehend, no indices -> less mistakes.
04_do_not_use_empty_except_clauses.patch:
except: -> except Exception:
suite suite
Rationale: Since python 2.5 SystemExit and KeyboardInterrupt do not derive from Exception anymore and we do not want to ignore them anyway. There is a slight chance for these exceptions getting swallowed by the empty except clauses.
05_replace_file_with_open.patch:
file(name, mode) -> open(name, mode)
Rationale: file() has been removed in python 3.
06_replace_filter_lambda_with_list_comprehension.patch:
filter(lambda x: expr, iterable) -> [x for x in iterable if expr]
Rationale: More idiomatic python, easier to read, probably faster.
07_replace_map_lambda_with_list_comprehension.patch:
map(lambda x: expr, iterable) -> [expr for x in iterable]
Rationale: More idiomatic python, easier to read, probably faster.
08_replace_list_with_tuple_in_for_and_if.patch:
for x in []: -> for x in ():
suite suite
if x in []: -> if x in ():
suite suite
Rationale: Tuple is more lightweight than list.
09_replace_none_equality_test_with_identity_test.patch:
x == None -> x is None
x != None -> x is not None
Rationale: Idiomatic python, faster.
10_replace_reduce.patch:
Rationale: Reduce has been removed from python 3's builtin functions and can most of the time be replaced with something more readable.
11_replace_backticks_with_repr.patch:
`x` -> repr(x)
Rationale: Backticks have been removed from python 3.
13_fix_idioms.patch:
type(x) == y -> isinstance(x, y)
type(x) != y -> not isinstance(x, y)
type(x) == type(builtin) -> isinstance(x, builtin)
while 1: suite -> while True: suite
a = list -> a = sorted(list)
a.sort()
Rationale: Mostly fixes by the 2to3 tool.
14_fix_ne_operator.patch:
\<> -> !=
Rationale: <> has been removed from python 3.
15_misc_type_fixes.patch:
type(x) in (type(a), type(b)) -> isinstance(x, (type(a), type(b))
type(x) in (type(a is builtin), type(b is builtin)) -> isinstance(x, (a, b))
Rationale: Idiomatic python.
16_fix_tuple_params.patch:
def f(a, (b, c)): -> def f(a, b_c):
suite b, c = b_c
suite
Rationale: Tuple parameters have been removed from python 3.
17_remove_unnecessary_lambdas.patch:
Removes some maps and some lambdas.
Rationale: List comprehensions and/or generator expressions are better to read (imho), faster.
18_fix_shadowed_builtins_in_functions.patch:
def f(list): -> def f(list_):
a = list a = list_
Rationale: Shadowing builtin functions make the builtin function hard to access, might confuse the reader and can make debugging harder.
Diffstat:
setup_osx.py | 12 +-
src/advanced.py | 22 ++---
src/cell_renderer_image.py | 7 -
src/chat_control.py | 10 +-
src/common/GnuPG.py | 26 +++---
src/common/atom.py | 4
src/common/caps.py | 12 +-
src/common/commands.py | 14 +--
src/common/config.py | 16 +--
src/common/configpaths.py | 2
src/common/connection.py | 27 ++----
src/common/connection_handlers.py | 62 +++++++--------
src/common/crypto.py | 4
src/common/dataforms.py | 18 ++--
src/common/gajim.py | 2
src/common/helpers.py | 80 ++++++-------------
src/common/logger.py | 6 -
src/common/nslookup.py | 10 +-
src/common/optparser.py | 10 +-
src/common/pubsub.py | 10 +-
src/common/sleepy.py | 4
src/common/socks5.py | 22 ++---
src/common/stanza_session.py | 53 ++++++------
src/common/xmpp/auth.py | 6 -
src/common/xmpp/auth_nb.py | 10 +-
src/common/xmpp/browser.py | 22 ++---
src/common/xmpp/c14n.py | 5 -
src/common/xmpp/client.py | 10 +-
src/common/xmpp/client_nb.py | 4
src/common/xmpp/commands.py | 22 ++---
src/common/xmpp/debug.py | 26 +++---
src/common/xmpp/dispatcher.py | 18 ++--
src/common/xmpp/dispatcher_nb.py | 20 ++--
src/common/xmpp/features.py | 12 +-
src/common/xmpp/features_nb.py | 4
src/common/xmpp/filetransfer.py | 4
src/common/xmpp/protocol.py | 24 ++---
src/common/xmpp/roster.py | 6 -
src/common/xmpp/session.py | 22 ++---
src/common/xmpp/simplexml.py | 18 ++--
src/common/xmpp/transports.py | 18 ++--
src/common/xmpp/transports_nb.py | 18 ++--
src/common/zeroconf/connection_handlers_zeroconf.py | 23 ++---
src/common/zeroconf/connection_zeroconf.py | 14 +--
src/common/zeroconf/roster_zeroconf.py | 13 ---
src/common/zeroconf/zeroconf_avahi.py | 2
src/common/zeroconf/zeroconf_bonjour.py | 4
src/config.py | 52 +++++-------
src/conversation_textview.py | 52 ++++++------
src/dataforms_widget.py | 6 -
src/dialogs.py | 70 ++++++++---------
src/disco.py | 82 ++++++++++----------
src/features_window.py | 24 ++---
src/filetransfers_window.py | 32 +++----
src/gajim-remote.py | 18 +---
src/gajim.py | 52 +++++-------
src/groupchat_control.py | 42 ++++------
src/gtkexcepthook.py | 6 -
src/gtkgui_helpers.py | 30 +++----
src/htmltextview.py | 24 ++---
src/ipython_view.py | 23 +----
src/lastfm.py | 4
src/message_control.py | 4
src/message_window.py | 56 ++++++-------
src/music_track_listener.py | 25 ------
src/network_manager_listener.py | 2
src/notify.py | 40 ++++-----
src/osx/dbus.py | 8 -
src/osx/growl/Growl.py | 7 -
src/osx/growler.py | 4
src/profile_window.py | 6 -
src/remote_control.py | 8 -
src/roster_window.py | 52 +++++-------
src/search_window.py | 2
src/secrets.py | 2
src/session.py | 8 -
src/systray.py | 7 -
src/tooltips.py | 11 --
src/vcard.py | 8 -
test/lib/mock.py | 9 --
test/lib/mocks.py | 4
test/runtests.py | 2
test/test_misc_interface.py | 6 -
83 files changed, 719 insertions(+), 827 deletions(-)