FS#56934 - [python-dbus] python3.x does not ship dict.has_key()

Attached to Project: Arch Linux
Opened by Martin Kühne (mar77i) - Wednesday, 03 January 2018, 10:57 GMT
Last edited by freswa (frederik) - Saturday, 26 September 2020, 20:22 GMT
Task Type Bug Report
Category Packages: Extra
Status Closed
Assigned To Jan de Groot (JGC)
Architecture All
Severity Medium
Priority Normal
Reported Version
Due in Version Undecided
Due Date Undecided
Percent Complete 100%
Votes 0
Private No

Details

Description:

avahi-discover breaks with an exception from python-dbus, because somehow the python-dbus connection.py uses the python2 dict.has_key(), which is no longer available.

Additional info:
* package version(s)
- extra/python-dbus 1.2.4-2
- extra/avahi 0.7-2

Steps to reproduce:

$ avahi-discover
Browsing domain 'local' on -1.-1 ...
ERROR:dbus.connection:Exception in handler for D-Bus signal:
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/dbus/connection.py", line 230, in maybe_handle_message
self._handler(*args, **kwargs)
File "/bin/avahi-discover", line 212, in new_domain
if self.zc_ifaces.has_key((interface,protocol)) == False:
AttributeError: 'dict' object has no attribute 'has_key'
This task depends upon

Closed by  freswa (frederik)
Saturday, 26 September 2020, 20:22 GMT
Reason for closing:  Fixed
Additional comments about closing:  avahi 0.8+6+g751be80-1 please see comments for details.
Comment by Martin Kühne (mar77i) - Wednesday, 03 January 2018, 12:10 GMT
UPDATE: sorry, the error is in /usr/bin/avahi-discover from extra/avahi 0.7-2, therefore the title is misleading. If you can, please change the title to match the actual problem.
Comment by loqs (loqs) - Saturday, 06 January 2018, 00:47 GMT
$ head -3 /usr/bin/avahi-discover
#!/usr/bin/python3
# -*-python-*-
# This file is part of avahi.
This seems like a bug in avahi to me not python-dbus avahi-discover specifies python3
https://github.com/lathiat/avahi/blob/6242e5f0fe001b7de2ccaa9431db279b2ee76b83/avahi-python/avahi-discover/avahi-discover.py#L270
self.zc_ifaces = {} would be a python3 dictionary
https://github.com/lathiat/avahi/blob/6242e5f0fe001b7de2ccaa9431db279b2ee76b83/avahi-python/avahi-discover/avahi-discover.py#L212
if self.zc_ifaces.has_key((interface,protocol)) == False:
https://docs.python.org/3.0/whatsnew/3.0.html#builtins Removed. dict.has_key() – use the in operator instead.
so using the removed builtin has_key fails
Comment by loqs (loqs) - Saturday, 06 January 2018, 12:40 GMT
@mar77i can you see if either of the following fixes the issue
1) change the first line of /usr/bin/avahi-discover from #!/usr/bin/python3 to #!/usr/bin/python2 and ensure the package python2-dbus is installed
2) sudo 2to3 -nw /usr/bin/avahi-discover
to reset the file to its original state `pacman -S avahi`
Comment by Martin Kühne (mar77i) - Saturday, 06 January 2018, 13:28 GMT
While 1) will likely work as long as python2 lives, I'd personally lean towards 2).

Using 2to3 works great here too:

# 2to3 -nw /usr/bin/avahi-discover
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored /usr/bin/avahi-discover
--- /usr/bin/avahi-discover (original)
+++ /usr/bin/avahi-discover (refactored)
@@ -17,7 +17,7 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.

-from __future__ import print_function
+

import os, sys

@@ -209,10 +209,10 @@
self.new_service_type(interface, protocol, self.stype, domain)

def new_domain(self,interface, protocol, domain, flags):
- if self.zc_ifaces.has_key((interface,protocol)) == False:
+ if ((interface,protocol) in self.zc_ifaces) == False:
ifn = self.get_interface_name(interface, protocol)
self.zc_ifaces[(interface,protocol)] = self.insert_row(self.treemodel, None, ifn,None,interface,protocol,None,domain)
- if self.zc_domains.has_key((interface,protocol,domain)) == False:
+ if ((interface,protocol,domain) in self.zc_domains) == False:
self.zc_domains[(interface,protocol,domain)] = self.insert_row(self.treemodel, self.zc_ifaces[(interface,protocol)], domain,None,interface,protocol,None,domain)
if domain != "local":
self.browse_domain(interface, protocol, domain)
@@ -233,7 +233,7 @@
if len(txt) != 0:
txts = ""
txtd = self.pair_to_dict(txt)
- for k,v in txtd.items():
+ for k,v in list(txtd.items()):
txts+="<b>" + _("TXT") + " <i>%s</i></b> = %s\n" % (k,v)
else:
txts = "<b>" + _("TXT Data:") + "</b> <i>" + _("empty") + "</i>"
RefactoringTool: Files that were modified:
RefactoringTool: /usr/bin/avahi-discover
Comment by loqs (loqs) - Saturday, 06 January 2018, 23:05 GMT
If option 2) is chosen 2to3 could also be used on avahi-bookmarks then  FS#55740  could be closed after changing the twisted dependency
although avahi-bookmarks would still be broken as https://github.com/lathiat/avahi/blob/6242e5f0fe001b7de2ccaa9431db279b2ee76b83/avahi-python/avahi-bookmarks.in#L151
indents with a tab instead of spaces required to match the existing indentation
Comment by Martin Kühne (mar77i) - Saturday, 06 January 2018, 23:20 GMT
...for spaces vs tabs I guess one could work around with expand(1) from coreutils?

Needless to say that all of this should go upstream one way or another.
Comment by loqs (loqs) - Sunday, 07 January 2018, 21:05 GMT
Agreed it all should be fixed upstream. Patch for avahi-discover is trivial, the output of 2to3 with the removal of from '__future__ import print_function' so it remains python2 compatible.
The patch for avahi-bookmarks turned out to be much more expansive than expected so keeping that using python2 is probably safer for now just fix optional dependencies python2-dbus and python2-gobject2
The patch for avahi-bookmarks in addition to general 2 to 3 compatibility fixes switches legacy GObject which is python2 only for gobject introspection which has bindings for both 2 and 3,
twisted.web's resource.Resource render_GET method requires a string to be returned under python2 but bytes under python3, accommodate this by checking python version so in theory the code
should still be python2 compatible.
Comment by loqs (loqs) - Wednesday, 24 January 2018, 21:42 GMT
The avahi-discover issue is already reported upstream https://github.com/lathiat/avahi/pull/146
Comment by loqs (loqs) - Monday, 07 September 2020, 03:07 GMT

Loading...