GNOME Bugzilla – Bug 655623
[glib build error] AttributeError: 'dict' object has no attribute 'has_key' with Python3
Last modified: 2012-09-29 08:41:24 UTC
I'm building current git devel (glib-v2.29.14-56-gc3af3d8). That's what I've got on 'make' stage: make[4]: Entering directory `/root/build-farm/glib-v2.29.14-56-gc3af3d8.builddir/gio/tests' GEN gdbus-test-codegen-generated.c Traceback (most recent call last):
+ Trace 227950
codegen_main()
parsed_ifaces = parser.parse_dbus_xml(xml_data)
parser = DBusXMLParser(xml_data)
self._parser.Parse(xml_data)
if attrs.has_key('name') and self.doc_comment_last_symbol == attrs['name']:
make[4]: *** [gdbus-test-codegen-generated.c] Error 1 make[4]: Leaving directory `/root/build-farm/glib-v2.29.14-56-gc3af3d8.builddir/gio/tests' make[3]: *** [all-recursive] Error 1 make[3]: Leaving directory `/root/build-farm/glib-v2.29.14-56-gc3af3d8.builddir/gio' make[2]: *** [all] Error 2 make[2]: Leaving directory `/root/build-farm/glib-v2.29.14-56-gc3af3d8.builddir/gio' make[1]: *** [all-recursive] Error 1 make[1]: Leaving directory `/root/build-farm/glib-v2.29.14-56-gc3af3d8.builddir' make: *** [all] Error 2 ERROR: 'make' error. Abort. By searching for this error message (AttributeError: 'dict' object has no attribute 'has_key'), I found that this is the problem of using python2-specific syntax (my setup doesn't have any ancient py2, only current python3). 1. eng2sp = { "one":"uno", "two":"dos", "three":"tres" } 2. 3. # works in Python2, but not Python3 4. # Python3 gives error ... 5. # AttributeError: 'dict' object has no attribute 'has_key' 6. #print( eng2sp.has_key( "one" ) ) # True 7. 8. # works with Python2 and Python3 9. print( "one" in eng2sp ) # True
The following patch commands can lead you to successfull build even with current python (to bootstrap, you do also need to copy a lot of .libs/* from 3 different directories to /usr/lib/ at the build time): # gio's 'gdbus-codegen' has a lot of ancient-python2-specific stuff, which is absolutely unacceptable in 2011; # so let's update the code to current python implementation, # and forget completely about using/supporting ancient python2: sed -i "s/attrs\.has_key('name')/'name' in attrs/" ./gio/gdbus-codegen/parser.py sed -i "s/self\.doc_comment_params\.has_key(attrs\['name'])/attrs['name'] in self.doc_comment_params/" ./gio/gdbus-codegen/parser.py sed -i "s/self\.doc_comment_params\.has_key('short_description')/'short_description' in self.doc_comment_params/" ./gio/gdbus-codegen/parser.py sed -i "s/self\.doc_comment_params\.has_key('since')/'since' in self.doc_comment_params/" ./gio/gdbus-codegen/parser.py sed -i "s/self\.expand_member_dict\.keys()/list(self.expand_member_dict.keys())/" ./gio/gdbus-codegen/codegen_docbook.py sed -i "s/self\.expand_iface_dict\.keys()/list(self.expand_iface_dict.keys())/" ./gio/gdbus-codegen/codegen_docbook.py sed -i "s/self\.out = file/self.out = open/" ./gio/gdbus-codegen/codegen_docbook.py sed -i "s/h = file(/h = open(/" ./gio/gdbus-codegen/codegen_main.py sed -i "s/c = file(/c = open(/" ./gio/gdbus-codegen/codegen_main.py sed -i "s/sort(cmp=/sort(/" ./gio/gdbus-codegen/codegen.py They do fix: 1) using of 'has_key' instead of 'in'; 2) wrong thoughts that keys() on dict does return a list; 3) 'open' instead of 'file'; 4) ancient snake-2 only 'cpm=' inside sort().
Created attachment 201180 [details] [review] a proper patch, with working version sort
Created attachment 201181 [details] [review] one that works with older versions again (hopefully) I've really got to start testing what I write - functools.cmp_to_key was only added in 2.7...
A duplicated effort of the patch for this ticket was already applied to glib. *** This bug has been marked as a duplicate of bug 678066 ***