[libvirt] [PATCH python 00/15] Initial work porting to python3

From: "Daniel P. Berrange" <berrange@redhat.com> This series starts, but does not complete, the work to port over to build with python3. This involves making the code generator able to run under python3, as well as making it generate code which is python3 compatible. And of course the examples are fixed up too Some things still todo - Deal with String vs Unicode problems - Python2 defaulted to C strings, while Python3 defaults to Unicode - Deal with Int vs Long problems - Python2 had two integer types, Int & Long, but Python3 only has one, Long. - Fix examples to not use libxml2 since it isn't compiled for Python3 in Fedora. - Probably more bugs to find... Daniel P. Berrange (15): generator: Don't use 'list' as a variable name generator: Remove string.lower(XXX) with XXX.lower() generator: Invoke print("...") instead of print "..." generator: Cast iterators to a list() explicitly generator: Remove use of 'has_key' function generator: Update to use sort() 'key' param generator: Remove use of string.replace and string.find functions generator: Sort enums and functions when generating code setup: Fix exception catching syntax examples: Invoke print("...") instead of print "..." examples: Fix exception catching syntax examples: Ensure we write bytes to the self-pipe generator: Fix exception catching syntax in generated code override: Fix native module registration to work with Python3 override: Fix exception catching syntax examples/consolecallback.py | 6 +- examples/dominfo.py | 14 +- examples/domrestore.py | 17 ++- examples/domsave.py | 15 +- examples/domstart.py | 19 ++- examples/esxlist.py | 14 +- examples/event-test.py | 74 +++++----- examples/topology.py | 14 +- generator.py | 328 +++++++++++++++++++++--------------------- libvirt-lxc-override.c | 73 +++++++--- libvirt-override-virStream.py | 6 +- libvirt-override.c | 75 +++++++--- libvirt-override.py | 4 +- libvirt-qemu-override.c | 73 +++++++--- setup.py | 2 +- 15 files changed, 417 insertions(+), 317 deletions(-) -- 1.8.3.1

From: "Daniel P. Berrange" <berrange@redhat.com> In python3 if we use 'list' as a variable name it causes it to hide the corresponding 'list()' function from the entire function that holds the variable. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/generator.py b/generator.py index a9f98ab..a40ed11 100755 --- a/generator.py +++ b/generator.py @@ -1434,8 +1434,8 @@ def buildWrappers(module): else: classes.write(" def __init__(self, _obj=None):\n") if reference_keepers.has_key(classname): - list = reference_keepers[classname] - for ref in list: + rlist = reference_keepers[classname] + for ref in rlist: classes.write(" self.%s = None\n" % ref[1]) if classname in [ "virDomain", "virNetwork", "virInterface", "virNodeDevice", "virSecret", "virStream", @@ -1589,8 +1589,8 @@ def buildWrappers(module): # tclass = classes_type[ret[0]][2] if reference_keepers.has_key(tclass): - list = reference_keepers[tclass] - for pref in list: + rlist = reference_keepers[tclass] + for pref in rlist: if pref[0] == classname: classes.write(" __tmp.%s = self\n" % pref[1]) -- 1.8.3.1

On Tue, Dec 3, 2013 at 10:36 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
In python3 if we use 'list' as a variable name it causes it to hide the corresponding 'list()' function from the entire function that holds the variable.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/generator.py b/generator.py index a9f98ab..a40ed11 100755 --- a/generator.py +++ b/generator.py @@ -1434,8 +1434,8 @@ def buildWrappers(module): else: classes.write(" def __init__(self, _obj=None):\n") if reference_keepers.has_key(classname): - list = reference_keepers[classname] - for ref in list: + rlist = reference_keepers[classname] + for ref in rlist: classes.write(" self.%s = None\n" % ref[1]) if classname in [ "virDomain", "virNetwork", "virInterface", "virNodeDevice", "virSecret", "virStream", @@ -1589,8 +1589,8 @@ def buildWrappers(module): # tclass = classes_type[ret[0]][2] if reference_keepers.has_key(tclass): - list = reference_keepers[tclass] - for pref in list: + rlist = reference_keepers[tclass] + for pref in rlist: if pref[0] == classname: classes.write(" __tmp.%s = self\n" % pref[1]) -- 1.8.3.1
ACK. -- Doug Goldstein

From: "Daniel P. Berrange" <berrange@redhat.com> In python3 the string.lower() method doesn't exist, the lower() function can only be executed against a string variable directly. Python2 supported both approaches so this change is compatible Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 90 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/generator.py b/generator.py index a40ed11..0c31ab2 100755 --- a/generator.py +++ b/generator.py @@ -1021,139 +1021,139 @@ def nameFixup(name, classe, type, file): l = len(classe) if name[0:l] == listname: func = name[l:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:16] == "virNetworkDefine": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:19] == "virNetworkCreateXML": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:16] == "virNetworkLookup": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:18] == "virInterfaceDefine": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:21] == "virInterfaceCreateXML": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:18] == "virInterfaceLookup": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:15] == "virSecretDefine": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:15] == "virSecretLookup": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:17] == "virNWFilterDefine": func = name[3:] - func = string.lower(func[0:3]) + func[3:] + func = func[0:3].lower() + func[3:] elif name[0:17] == "virNWFilterLookup": func = name[3:] - func = string.lower(func[0:3]) + func[3:] + func = func[0:3].lower() + func[3:] elif name[0:20] == "virStoragePoolDefine": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:23] == "virStoragePoolCreateXML": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:20] == "virStoragePoolLookup": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:19] == "virStorageVolDefine": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:19] == "virStorageVolLookup": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:20] == "virDomainGetCPUStats": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:12] == "virDomainGet": func = name[12:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:29] == "virDomainSnapshotLookupByName": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:26] == "virDomainSnapshotListNames": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:28] == "virDomainSnapshotNumChildren": func = name[17:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:20] == "virDomainSnapshotNum": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:26] == "virDomainSnapshotCreateXML": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:24] == "virDomainSnapshotCurrent": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:17] == "virDomainSnapshot": func = name[17:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:9] == "virDomain": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:13] == "virNetworkGet": func = name[13:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:10] == "virNetwork": func = name[10:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:15] == "virInterfaceGet": func = name[15:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:12] == "virInterface": func = name[12:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:12] == 'virSecretGet': func = name[12:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:9] == 'virSecret': func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:14] == 'virNWFilterGet': func = name[14:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:11] == 'virNWFilter': func = name[11:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:12] == 'virStreamNew': func = "newStream" elif name[0:9] == 'virStream': func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:17] == "virStoragePoolGet": func = name[17:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:14] == "virStoragePool": func = name[14:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:16] == "virStorageVolGet": func = name[16:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:13] == "virStorageVol": func = name[13:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:13] == "virNodeDevice": if name[13:16] == "Get": - func = string.lower(name[16]) + name[17:] + func = name[16].lower() + name[17:] elif name[13:19] == "Lookup" or name[13:19] == "Create": - func = string.lower(name[3]) + name[4:] + func = name[3].lower() + name[4:] else: - func = string.lower(name[13]) + name[14:] + func = name[13].lower() + name[14:] elif name[0:7] == "virNode": func = name[7:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:10] == "virConnect": func = name[10:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:3] == "xml": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] else: func = name if func == "iD": -- 1.8.3.1

On Tue, Dec 3, 2013 at 10:36 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
In python3 the string.lower() method doesn't exist, the lower() function can only be executed against a string variable directly. Python2 supported both approaches so this change is compatible
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 90 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 45 insertions(+), 45 deletions(-)
diff --git a/generator.py b/generator.py index a40ed11..0c31ab2 100755 --- a/generator.py +++ b/generator.py @@ -1021,139 +1021,139 @@ def nameFixup(name, classe, type, file): l = len(classe) if name[0:l] == listname: func = name[l:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:16] == "virNetworkDefine": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:19] == "virNetworkCreateXML": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:16] == "virNetworkLookup": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:18] == "virInterfaceDefine": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:21] == "virInterfaceCreateXML": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:18] == "virInterfaceLookup": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:15] == "virSecretDefine": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:15] == "virSecretLookup": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:17] == "virNWFilterDefine": func = name[3:] - func = string.lower(func[0:3]) + func[3:] + func = func[0:3].lower() + func[3:] elif name[0:17] == "virNWFilterLookup": func = name[3:] - func = string.lower(func[0:3]) + func[3:] + func = func[0:3].lower() + func[3:] elif name[0:20] == "virStoragePoolDefine": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:23] == "virStoragePoolCreateXML": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:20] == "virStoragePoolLookup": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:19] == "virStorageVolDefine": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:19] == "virStorageVolLookup": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:20] == "virDomainGetCPUStats": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:12] == "virDomainGet": func = name[12:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:29] == "virDomainSnapshotLookupByName": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:26] == "virDomainSnapshotListNames": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:28] == "virDomainSnapshotNumChildren": func = name[17:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:20] == "virDomainSnapshotNum": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:26] == "virDomainSnapshotCreateXML": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:24] == "virDomainSnapshotCurrent": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:17] == "virDomainSnapshot": func = name[17:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:9] == "virDomain": func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:13] == "virNetworkGet": func = name[13:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:10] == "virNetwork": func = name[10:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:15] == "virInterfaceGet": func = name[15:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:12] == "virInterface": func = name[12:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:12] == 'virSecretGet': func = name[12:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:9] == 'virSecret': func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:14] == 'virNWFilterGet': func = name[14:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:11] == 'virNWFilter': func = name[11:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:12] == 'virStreamNew': func = "newStream" elif name[0:9] == 'virStream': func = name[9:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:17] == "virStoragePoolGet": func = name[17:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:14] == "virStoragePool": func = name[14:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:16] == "virStorageVolGet": func = name[16:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:13] == "virStorageVol": func = name[13:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:13] == "virNodeDevice": if name[13:16] == "Get": - func = string.lower(name[16]) + name[17:] + func = name[16].lower() + name[17:] elif name[13:19] == "Lookup" or name[13:19] == "Create": - func = string.lower(name[3]) + name[4:] + func = name[3].lower() + name[4:] else: - func = string.lower(name[13]) + name[14:] + func = name[13].lower() + name[14:] elif name[0:7] == "virNode": func = name[7:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:10] == "virConnect": func = name[10:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] elif name[0:3] == "xml": func = name[3:] - func = string.lower(func[0:1]) + func[1:] + func = func[0:1].lower() + func[1:] else: func = name if func == "iD": -- 1.8.3.1
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
ACK. -- Doug Goldstein

From: "Daniel P. Berrange" <berrange@redhat.com> The 'print' method must be called as a function in python3, ie with brackets. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/generator.py b/generator.py index 0c31ab2..23bae9a 100755 --- a/generator.py +++ b/generator.py @@ -48,24 +48,24 @@ class docParser(xml.sax.handler.ContentHandler): def close(self): if debug: - print "close" + print("close") def getmethodname(self): return self._methodname def data(self, text): if debug: - print "data %s" % text + print("data %s" % text) self._data.append(text) def cdata(self, text): if debug: - print "data %s" % text + print("data %s" % text) self._data.append(text) def start(self, tag, attrs): if debug: - print "start %s, %s" % (tag, attrs) + print("start %s, %s" % (tag, attrs)) if tag == 'function': self._data = [] self.in_function = 1 @@ -123,7 +123,7 @@ class docParser(xml.sax.handler.ContentHandler): def end(self, tag): if debug: - print "end %s" % tag + print("end %s" % tag) if tag == 'function': # fuctions come from source files, hence 'virerror.c' if self.function is not None: @@ -600,7 +600,7 @@ def print_function_wrapper(module, name, output, export, include): if module == "libvirt-qemu": (desc, ret, args, file, mod, cond) = qemu_functions[name] except: - print "failed to get function %s infos" % name + print("failed to get function %s infos" % name) return if skipped_modules.has_key(module): @@ -794,7 +794,7 @@ def buildStubs(module, api_xml): global onlyOverrides if module not in ["libvirt", "libvirt-qemu", "libvirt-lxc"]: - print "ERROR: Unknown module type: %s" % module + print("ERROR: Unknown module type: %s" % module) return None if module == "libvirt": @@ -818,13 +818,13 @@ def buildStubs(module, api_xml): (parser, target) = getparser() parser.feed(data) parser.close() - except IOError, msg: - print file, ":", msg + except IOError as msg: + print(file, ":", msg) sys.exit(1) n = len(funcs.keys()) if not quiet: - print "Found %d functions in %s" % ((n), api_xml) + print("Found %d functions in %s" % ((n), api_xml)) override_api_xml = "%s-override-api.xml" % module py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject") @@ -837,13 +837,13 @@ def buildStubs(module, api_xml): (parser, target) = getparser() parser.feed(data) parser.close() - except IOError, msg: - print file, ":", msg + except IOError as msg: + print(file, ":", msg) if not quiet: # XXX: This is not right, same function already in @functions # will be overwritten. - print "Found %d functions in %s" % ((len(funcs.keys()) - n), override_api_xml) + print("Found %d functions in %s" % ((len(funcs.keys()) - n), override_api_xml)) nb_wrap = 0 failed = 0 skipped = 0 @@ -883,15 +883,15 @@ def buildStubs(module, api_xml): wrapper.close() if not quiet: - print "Generated %d wrapper functions" % nb_wrap + print("Generated %d wrapper functions" % nb_wrap) if unknown_types: - print "Missing type converters: " + print("Missing type converters: ") for type in unknown_types.keys(): - print "%s:%d " % (type, len(unknown_types[type])), + print("%s:%d " % (type, len(unknown_types[type]))) for f in funcs_failed: - print "ERROR: failed %s" % f + print("ERROR: failed %s" % f) if failed > 0: return -1 @@ -1229,7 +1229,7 @@ def buildWrappers(module): global functions_noexcept if not module == "libvirt": - print "ERROR: Unknown module type: %s" % module + print("ERROR: Unknown module type: %s" % module) return None for type in classes_type.keys(): @@ -1786,7 +1786,7 @@ def qemuBuildWrappers(module): global qemu_functions if not module == "libvirt-qemu": - print "ERROR: only libvirt-qemu is supported" + print("ERROR: only libvirt-qemu is supported") return None extra_file = "%s-override.py" % module @@ -1897,7 +1897,7 @@ def lxcBuildWrappers(module): global lxc_functions if not module == "libvirt-lxc": - print "ERROR: only libvirt-lxc is supported" + print("ERROR: only libvirt-lxc is supported") return None extra_file = "%s-override.py" % module @@ -2018,7 +2018,7 @@ elif sys.argv[1] == "libvirt-lxc": elif sys.argv[1] == "libvirt-qemu": qemuBuildWrappers(sys.argv[1]) else: - print "ERROR: unknown module %s" % sys.argv[1] + print("ERROR: unknown module %s" % sys.argv[1]) sys.exit(1) sys.exit(0) -- 1.8.3.1

On Tue, Dec 3, 2013 at 10:36 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
The 'print' method must be called as a function in python3, ie with brackets.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-)
diff --git a/generator.py b/generator.py index 0c31ab2..23bae9a 100755 --- a/generator.py +++ b/generator.py @@ -48,24 +48,24 @@ class docParser(xml.sax.handler.ContentHandler):
def close(self): if debug: - print "close" + print("close")
def getmethodname(self): return self._methodname
def data(self, text): if debug: - print "data %s" % text + print("data %s" % text) self._data.append(text)
def cdata(self, text): if debug: - print "data %s" % text + print("data %s" % text) self._data.append(text)
def start(self, tag, attrs): if debug: - print "start %s, %s" % (tag, attrs) + print("start %s, %s" % (tag, attrs)) if tag == 'function': self._data = [] self.in_function = 1 @@ -123,7 +123,7 @@ class docParser(xml.sax.handler.ContentHandler):
def end(self, tag): if debug: - print "end %s" % tag + print("end %s" % tag) if tag == 'function': # fuctions come from source files, hence 'virerror.c' if self.function is not None: @@ -600,7 +600,7 @@ def print_function_wrapper(module, name, output, export, include): if module == "libvirt-qemu": (desc, ret, args, file, mod, cond) = qemu_functions[name] except: - print "failed to get function %s infos" % name + print("failed to get function %s infos" % name) return
if skipped_modules.has_key(module): @@ -794,7 +794,7 @@ def buildStubs(module, api_xml): global onlyOverrides
if module not in ["libvirt", "libvirt-qemu", "libvirt-lxc"]: - print "ERROR: Unknown module type: %s" % module + print("ERROR: Unknown module type: %s" % module) return None
if module == "libvirt": @@ -818,13 +818,13 @@ def buildStubs(module, api_xml): (parser, target) = getparser() parser.feed(data) parser.close() - except IOError, msg: - print file, ":", msg + except IOError as msg: + print(file, ":", msg) sys.exit(1)
n = len(funcs.keys()) if not quiet: - print "Found %d functions in %s" % ((n), api_xml) + print("Found %d functions in %s" % ((n), api_xml))
override_api_xml = "%s-override-api.xml" % module py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject") @@ -837,13 +837,13 @@ def buildStubs(module, api_xml): (parser, target) = getparser() parser.feed(data) parser.close() - except IOError, msg: - print file, ":", msg + except IOError as msg: + print(file, ":", msg)
if not quiet: # XXX: This is not right, same function already in @functions # will be overwritten. - print "Found %d functions in %s" % ((len(funcs.keys()) - n), override_api_xml) + print("Found %d functions in %s" % ((len(funcs.keys()) - n), override_api_xml)) nb_wrap = 0 failed = 0 skipped = 0 @@ -883,15 +883,15 @@ def buildStubs(module, api_xml): wrapper.close()
if not quiet: - print "Generated %d wrapper functions" % nb_wrap + print("Generated %d wrapper functions" % nb_wrap)
if unknown_types: - print "Missing type converters: " + print("Missing type converters: ") for type in unknown_types.keys(): - print "%s:%d " % (type, len(unknown_types[type])), + print("%s:%d " % (type, len(unknown_types[type])))
for f in funcs_failed: - print "ERROR: failed %s" % f + print("ERROR: failed %s" % f)
if failed > 0: return -1 @@ -1229,7 +1229,7 @@ def buildWrappers(module): global functions_noexcept
if not module == "libvirt": - print "ERROR: Unknown module type: %s" % module + print("ERROR: Unknown module type: %s" % module) return None
for type in classes_type.keys(): @@ -1786,7 +1786,7 @@ def qemuBuildWrappers(module): global qemu_functions
if not module == "libvirt-qemu": - print "ERROR: only libvirt-qemu is supported" + print("ERROR: only libvirt-qemu is supported") return None
extra_file = "%s-override.py" % module @@ -1897,7 +1897,7 @@ def lxcBuildWrappers(module): global lxc_functions
if not module == "libvirt-lxc": - print "ERROR: only libvirt-lxc is supported" + print("ERROR: only libvirt-lxc is supported") return None
extra_file = "%s-override.py" % module @@ -2018,7 +2018,7 @@ elif sys.argv[1] == "libvirt-lxc": elif sys.argv[1] == "libvirt-qemu": qemuBuildWrappers(sys.argv[1]) else: - print "ERROR: unknown module %s" % sys.argv[1] + print("ERROR: unknown module %s" % sys.argv[1]) sys.exit(1)
sys.exit(0) -- 1.8.3.1
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
My patch actually uses "myprint" and provides that function which just calls the built in function when possible but provides one when not possible. Would we want to go with that approach instead? -- Doug Goldstein

On Tue, Dec 03, 2013 at 10:44:07AM -0600, Doug Goldstein wrote:
On Tue, Dec 3, 2013 at 10:36 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
The 'print' method must be called as a function in python3, ie with brackets.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-)
diff --git a/generator.py b/generator.py index 0c31ab2..23bae9a 100755 --- a/generator.py +++ b/generator.py @@ -48,24 +48,24 @@ class docParser(xml.sax.handler.ContentHandler):
def close(self): if debug: - print "close" + print("close")
def getmethodname(self): return self._methodname
def data(self, text): if debug: - print "data %s" % text + print("data %s" % text) self._data.append(text)
def cdata(self, text): if debug: - print "data %s" % text + print("data %s" % text) self._data.append(text)
def start(self, tag, attrs): if debug: - print "start %s, %s" % (tag, attrs) + print("start %s, %s" % (tag, attrs)) if tag == 'function': self._data = [] self.in_function = 1 @@ -123,7 +123,7 @@ class docParser(xml.sax.handler.ContentHandler):
def end(self, tag): if debug: - print "end %s" % tag + print("end %s" % tag) if tag == 'function': # fuctions come from source files, hence 'virerror.c' if self.function is not None: @@ -600,7 +600,7 @@ def print_function_wrapper(module, name, output, export, include): if module == "libvirt-qemu": (desc, ret, args, file, mod, cond) = qemu_functions[name] except: - print "failed to get function %s infos" % name + print("failed to get function %s infos" % name) return
if skipped_modules.has_key(module): @@ -794,7 +794,7 @@ def buildStubs(module, api_xml): global onlyOverrides
if module not in ["libvirt", "libvirt-qemu", "libvirt-lxc"]: - print "ERROR: Unknown module type: %s" % module + print("ERROR: Unknown module type: %s" % module) return None
if module == "libvirt": @@ -818,13 +818,13 @@ def buildStubs(module, api_xml): (parser, target) = getparser() parser.feed(data) parser.close() - except IOError, msg: - print file, ":", msg + except IOError as msg: + print(file, ":", msg) sys.exit(1)
n = len(funcs.keys()) if not quiet: - print "Found %d functions in %s" % ((n), api_xml) + print("Found %d functions in %s" % ((n), api_xml))
override_api_xml = "%s-override-api.xml" % module py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject") @@ -837,13 +837,13 @@ def buildStubs(module, api_xml): (parser, target) = getparser() parser.feed(data) parser.close() - except IOError, msg: - print file, ":", msg + except IOError as msg: + print(file, ":", msg)
if not quiet: # XXX: This is not right, same function already in @functions # will be overwritten. - print "Found %d functions in %s" % ((len(funcs.keys()) - n), override_api_xml) + print("Found %d functions in %s" % ((len(funcs.keys()) - n), override_api_xml)) nb_wrap = 0 failed = 0 skipped = 0 @@ -883,15 +883,15 @@ def buildStubs(module, api_xml): wrapper.close()
if not quiet: - print "Generated %d wrapper functions" % nb_wrap + print("Generated %d wrapper functions" % nb_wrap)
if unknown_types: - print "Missing type converters: " + print("Missing type converters: ") for type in unknown_types.keys(): - print "%s:%d " % (type, len(unknown_types[type])), + print("%s:%d " % (type, len(unknown_types[type])))
for f in funcs_failed: - print "ERROR: failed %s" % f + print("ERROR: failed %s" % f)
if failed > 0: return -1 @@ -1229,7 +1229,7 @@ def buildWrappers(module): global functions_noexcept
if not module == "libvirt": - print "ERROR: Unknown module type: %s" % module + print("ERROR: Unknown module type: %s" % module) return None
for type in classes_type.keys(): @@ -1786,7 +1786,7 @@ def qemuBuildWrappers(module): global qemu_functions
if not module == "libvirt-qemu": - print "ERROR: only libvirt-qemu is supported" + print("ERROR: only libvirt-qemu is supported") return None
extra_file = "%s-override.py" % module @@ -1897,7 +1897,7 @@ def lxcBuildWrappers(module): global lxc_functions
if not module == "libvirt-lxc": - print "ERROR: only libvirt-lxc is supported" + print("ERROR: only libvirt-lxc is supported") return None
extra_file = "%s-override.py" % module @@ -2018,7 +2018,7 @@ elif sys.argv[1] == "libvirt-lxc": elif sys.argv[1] == "libvirt-qemu": qemuBuildWrappers(sys.argv[1]) else: - print "ERROR: unknown module %s" % sys.argv[1] + print("ERROR: unknown module %s" % sys.argv[1]) sys.exit(1)
sys.exit(0) -- 1.8.3.1
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
My patch actually uses "myprint" and provides that function which just calls the built in function when possible but provides one when not possible. Would we want to go with that approach instead?
Not sure I understand what your patch is doing ? Why would we need to create a wrapper function, instead of just using this syntax which works on python 2+3 Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

On Tue, Dec 3, 2013 at 10:53 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
On Tue, Dec 03, 2013 at 10:44:07AM -0600, Doug Goldstein wrote:
On Tue, Dec 3, 2013 at 10:36 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
The 'print' method must be called as a function in python3, ie with brackets.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-)
diff --git a/generator.py b/generator.py index 0c31ab2..23bae9a 100755 --- a/generator.py +++ b/generator.py @@ -48,24 +48,24 @@ class docParser(xml.sax.handler.ContentHandler):
def close(self): if debug: - print "close" + print("close")
def getmethodname(self): return self._methodname
def data(self, text): if debug: - print "data %s" % text + print("data %s" % text) self._data.append(text)
def cdata(self, text): if debug: - print "data %s" % text + print("data %s" % text) self._data.append(text)
def start(self, tag, attrs): if debug: - print "start %s, %s" % (tag, attrs) + print("start %s, %s" % (tag, attrs)) if tag == 'function': self._data = [] self.in_function = 1 @@ -123,7 +123,7 @@ class docParser(xml.sax.handler.ContentHandler):
def end(self, tag): if debug: - print "end %s" % tag + print("end %s" % tag) if tag == 'function': # fuctions come from source files, hence 'virerror.c' if self.function is not None: @@ -600,7 +600,7 @@ def print_function_wrapper(module, name, output, export, include): if module == "libvirt-qemu": (desc, ret, args, file, mod, cond) = qemu_functions[name] except: - print "failed to get function %s infos" % name + print("failed to get function %s infos" % name) return
if skipped_modules.has_key(module): @@ -794,7 +794,7 @@ def buildStubs(module, api_xml): global onlyOverrides
if module not in ["libvirt", "libvirt-qemu", "libvirt-lxc"]: - print "ERROR: Unknown module type: %s" % module + print("ERROR: Unknown module type: %s" % module) return None
if module == "libvirt": @@ -818,13 +818,13 @@ def buildStubs(module, api_xml): (parser, target) = getparser() parser.feed(data) parser.close() - except IOError, msg: - print file, ":", msg + except IOError as msg: + print(file, ":", msg) sys.exit(1)
n = len(funcs.keys()) if not quiet: - print "Found %d functions in %s" % ((n), api_xml) + print("Found %d functions in %s" % ((n), api_xml))
override_api_xml = "%s-override-api.xml" % module py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject") @@ -837,13 +837,13 @@ def buildStubs(module, api_xml): (parser, target) = getparser() parser.feed(data) parser.close() - except IOError, msg: - print file, ":", msg + except IOError as msg: + print(file, ":", msg)
if not quiet: # XXX: This is not right, same function already in @functions # will be overwritten. - print "Found %d functions in %s" % ((len(funcs.keys()) - n), override_api_xml) + print("Found %d functions in %s" % ((len(funcs.keys()) - n), override_api_xml)) nb_wrap = 0 failed = 0 skipped = 0 @@ -883,15 +883,15 @@ def buildStubs(module, api_xml): wrapper.close()
if not quiet: - print "Generated %d wrapper functions" % nb_wrap + print("Generated %d wrapper functions" % nb_wrap)
if unknown_types: - print "Missing type converters: " + print("Missing type converters: ") for type in unknown_types.keys(): - print "%s:%d " % (type, len(unknown_types[type])), + print("%s:%d " % (type, len(unknown_types[type])))
for f in funcs_failed: - print "ERROR: failed %s" % f + print("ERROR: failed %s" % f)
if failed > 0: return -1 @@ -1229,7 +1229,7 @@ def buildWrappers(module): global functions_noexcept
if not module == "libvirt": - print "ERROR: Unknown module type: %s" % module + print("ERROR: Unknown module type: %s" % module) return None
for type in classes_type.keys(): @@ -1786,7 +1786,7 @@ def qemuBuildWrappers(module): global qemu_functions
if not module == "libvirt-qemu": - print "ERROR: only libvirt-qemu is supported" + print("ERROR: only libvirt-qemu is supported") return None
extra_file = "%s-override.py" % module @@ -1897,7 +1897,7 @@ def lxcBuildWrappers(module): global lxc_functions
if not module == "libvirt-lxc": - print "ERROR: only libvirt-lxc is supported" + print("ERROR: only libvirt-lxc is supported") return None
extra_file = "%s-override.py" % module @@ -2018,7 +2018,7 @@ elif sys.argv[1] == "libvirt-lxc": elif sys.argv[1] == "libvirt-qemu": qemuBuildWrappers(sys.argv[1]) else: - print "ERROR: unknown module %s" % sys.argv[1] + print("ERROR: unknown module %s" % sys.argv[1]) sys.exit(1)
sys.exit(0) -- 1.8.3.1
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
My patch actually uses "myprint" and provides that function which just calls the built in function when possible but provides one when not possible. Would we want to go with that approach instead?
Not sure I understand what your patch is doing ? Why would we need to create a wrapper function, instead of just using this syntax which works on python 2+3
We talked about this on IRC. Dan dropped the syntaxes (e.g. trailing comma) that wouldn't work on older Pythons so this is good. ACK. -- Doug Goldstein

From: "Daniel P. Berrange" <berrange@redhat.com> In python3 various methods list 'dict.keys()' do not return a list, so we must explicitly cast the result. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/generator.py b/generator.py index 23bae9a..ce2797e 100755 --- a/generator.py +++ b/generator.py @@ -603,7 +603,7 @@ def print_function_wrapper(module, name, output, export, include): print("failed to get function %s infos" % name) return - if skipped_modules.has_key(module): + if module in skipped_modules: return 0 if module == "libvirt": @@ -822,7 +822,7 @@ def buildStubs(module, api_xml): print(file, ":", msg) sys.exit(1) - n = len(funcs.keys()) + n = len(list(funcs.keys())) if not quiet: print("Found %d functions in %s" % ((n), api_xml)) @@ -843,7 +843,7 @@ def buildStubs(module, api_xml): if not quiet: # XXX: This is not right, same function already in @functions # will be overwritten. - print("Found %d functions in %s" % ((len(funcs.keys()) - n), override_api_xml)) + print("Found %d functions in %s" % ((len(list(funcs.keys())) - n), override_api_xml)) nb_wrap = 0 failed = 0 skipped = 0 @@ -865,7 +865,7 @@ def buildStubs(module, api_xml): wrapper.write("#include \"typewrappers.h\"\n") wrapper.write("#include \"build/" + module + ".h\"\n\n") - for function in funcs.keys(): + for function in list(funcs.keys()): # Skip the functions which are not for the module ret = print_function_wrapper(module, function, wrapper, export, include) if ret < 0: @@ -887,7 +887,7 @@ def buildStubs(module, api_xml): if unknown_types: print("Missing type converters: ") - for type in unknown_types.keys(): + for type in list(unknown_types.keys()): print("%s:%d " % (type, len(unknown_types[type]))) for f in funcs_failed: @@ -1232,7 +1232,7 @@ def buildWrappers(module): print("ERROR: Unknown module type: %s" % module) return None - for type in classes_type.keys(): + for type in list(classes_type.keys()): function_classes[classes_type[type][2]] = [] # @@ -1246,12 +1246,12 @@ def buildWrappers(module): for classe in primary_classes: classes_list.append(classe) classes_processed[classe] = () - for type in classes_type.keys(): + for type in list(classes_type.keys()): tinfo = classes_type[type] if tinfo[2] == classe: ctypes.append(type) ctypes_processed[type] = () - for type in classes_type.keys(): + for type in list(classes_type.keys()): if ctypes_processed.has_key(type): continue tinfo = classes_type[type] @@ -1262,7 +1262,7 @@ def buildWrappers(module): ctypes.append(type) ctypes_processed[type] = () - for name in functions.keys(): + for name in list(functions.keys()): found = 0 (desc, ret, args, file, mod, cond) = functions[name] for type in ctypes: @@ -1772,10 +1772,10 @@ def buildWrappers(module): # # Generate enum constants # - for type,enum in enums.items(): + for type,enum in list(enums.items()): classes.write("# %s\n" % type) - items = enum.items() - items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1]))) + items = list(enum.items()) + items.sort(lambda i1,i2: cmp(int(i1[1]),int(i2[1]))) for name,value in items: classes.write("%s = %s\n" % (name,value)) classes.write("\n") @@ -1832,7 +1832,7 @@ def qemuBuildWrappers(module): # # Generate functions directly, no classes # - for name in qemu_functions.keys(): + for name in list(qemu_functions.keys()): func = nameFixup(name, 'None', None, None) (desc, ret, args, file, mod, cond) = qemu_functions[name] fd.write("def %s(" % func) @@ -1882,10 +1882,10 @@ def qemuBuildWrappers(module): # # Generate enum constants # - for type,enum in qemu_enums.items(): + for type,enum in list(qemu_enums.items()): fd.write("# %s\n" % type) - items = enum.items() - items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1]))) + items = list(enum.items()) + items.sort(lambda i1,i2: cmp(int(i1[1]),int(i2[1]))) for name,value in items: fd.write("%s = %s\n" % (name,value)) fd.write("\n") @@ -1943,7 +1943,7 @@ def lxcBuildWrappers(module): # # Generate functions directly, no classes # - for name in lxc_functions.keys(): + for name in list(lxc_functions.keys()): func = nameFixup(name, 'None', None, None) (desc, ret, args, file, mod, cond) = lxc_functions[name] fd.write("def %s(" % func) @@ -1993,10 +1993,10 @@ def lxcBuildWrappers(module): # # Generate enum constants # - for type,enum in lxc_enums.items(): + for type,enum in list(lxc_enums.items()): fd.write("# %s\n" % type) - items = enum.items() - items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1]))) + items = list(enum.items()) + items.sort(lambda i1,i2: cmp(int(i1[1]),int(i2[1]))) for name,value in items: fd.write("%s = %s\n" % (name,value)) fd.write("\n") -- 1.8.3.1

On Tue, Dec 3, 2013 at 10:36 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
In python3 various methods list 'dict.keys()' do not return a list, so we must explicitly cast the result.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-)
diff --git a/generator.py b/generator.py index 23bae9a..ce2797e 100755 --- a/generator.py +++ b/generator.py @@ -603,7 +603,7 @@ def print_function_wrapper(module, name, output, export, include): print("failed to get function %s infos" % name) return
- if skipped_modules.has_key(module): + if module in skipped_modules: return 0
if module == "libvirt": @@ -822,7 +822,7 @@ def buildStubs(module, api_xml): print(file, ":", msg) sys.exit(1)
- n = len(funcs.keys()) + n = len(list(funcs.keys())) if not quiet: print("Found %d functions in %s" % ((n), api_xml))
@@ -843,7 +843,7 @@ def buildStubs(module, api_xml): if not quiet: # XXX: This is not right, same function already in @functions # will be overwritten. - print("Found %d functions in %s" % ((len(funcs.keys()) - n), override_api_xml)) + print("Found %d functions in %s" % ((len(list(funcs.keys())) - n), override_api_xml)) nb_wrap = 0 failed = 0 skipped = 0 @@ -865,7 +865,7 @@ def buildStubs(module, api_xml): wrapper.write("#include \"typewrappers.h\"\n") wrapper.write("#include \"build/" + module + ".h\"\n\n")
- for function in funcs.keys(): + for function in list(funcs.keys()): # Skip the functions which are not for the module ret = print_function_wrapper(module, function, wrapper, export, include) if ret < 0: @@ -887,7 +887,7 @@ def buildStubs(module, api_xml):
if unknown_types: print("Missing type converters: ") - for type in unknown_types.keys(): + for type in list(unknown_types.keys()): print("%s:%d " % (type, len(unknown_types[type])))
for f in funcs_failed: @@ -1232,7 +1232,7 @@ def buildWrappers(module): print("ERROR: Unknown module type: %s" % module) return None
- for type in classes_type.keys(): + for type in list(classes_type.keys()): function_classes[classes_type[type][2]] = []
# @@ -1246,12 +1246,12 @@ def buildWrappers(module): for classe in primary_classes: classes_list.append(classe) classes_processed[classe] = () - for type in classes_type.keys(): + for type in list(classes_type.keys()): tinfo = classes_type[type] if tinfo[2] == classe: ctypes.append(type) ctypes_processed[type] = () - for type in classes_type.keys(): + for type in list(classes_type.keys()): if ctypes_processed.has_key(type): continue tinfo = classes_type[type] @@ -1262,7 +1262,7 @@ def buildWrappers(module): ctypes.append(type) ctypes_processed[type] = ()
- for name in functions.keys(): + for name in list(functions.keys()): found = 0 (desc, ret, args, file, mod, cond) = functions[name] for type in ctypes: @@ -1772,10 +1772,10 @@ def buildWrappers(module): # # Generate enum constants # - for type,enum in enums.items(): + for type,enum in list(enums.items()): classes.write("# %s\n" % type) - items = enum.items() - items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1]))) + items = list(enum.items()) + items.sort(lambda i1,i2: cmp(int(i1[1]),int(i2[1]))) for name,value in items: classes.write("%s = %s\n" % (name,value)) classes.write("\n") @@ -1832,7 +1832,7 @@ def qemuBuildWrappers(module): # # Generate functions directly, no classes # - for name in qemu_functions.keys(): + for name in list(qemu_functions.keys()): func = nameFixup(name, 'None', None, None) (desc, ret, args, file, mod, cond) = qemu_functions[name] fd.write("def %s(" % func) @@ -1882,10 +1882,10 @@ def qemuBuildWrappers(module): # # Generate enum constants # - for type,enum in qemu_enums.items(): + for type,enum in list(qemu_enums.items()): fd.write("# %s\n" % type) - items = enum.items() - items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1]))) + items = list(enum.items()) + items.sort(lambda i1,i2: cmp(int(i1[1]),int(i2[1]))) for name,value in items: fd.write("%s = %s\n" % (name,value)) fd.write("\n") @@ -1943,7 +1943,7 @@ def lxcBuildWrappers(module): # # Generate functions directly, no classes # - for name in lxc_functions.keys(): + for name in list(lxc_functions.keys()): func = nameFixup(name, 'None', None, None) (desc, ret, args, file, mod, cond) = lxc_functions[name] fd.write("def %s(" % func) @@ -1993,10 +1993,10 @@ def lxcBuildWrappers(module): # # Generate enum constants # - for type,enum in lxc_enums.items(): + for type,enum in list(lxc_enums.items()): fd.write("# %s\n" % type) - items = enum.items() - items.sort(lambda i1,i2: cmp(long(i1[1]),long(i2[1]))) + items = list(enum.items()) + items.sort(lambda i1,i2: cmp(int(i1[1]),int(i2[1]))) for name,value in items: fd.write("%s = %s\n" % (name,value)) fd.write("\n") -- 1.8.3.1
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
ACK. -- Doug Goldstein

On 12/03/2013 09:36 AM, Daniel P. Berrange wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
In python3 various methods list 'dict.keys()' do not return a list, so we must explicitly cast the result.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-)
diff --git a/generator.py b/generator.py index 23bae9a..ce2797e 100755 --- a/generator.py +++ b/generator.py @@ -603,7 +603,7 @@ def print_function_wrapper(module, name, output, export, include): print("failed to get function %s infos" % name) return
- if skipped_modules.has_key(module): + if module in skipped_modules: return 0
This hunk belongs in 5/15 -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

From: "Daniel P. Berrange" <berrange@redhat.com> The code 'XXX.has_key(YYYY)' must be changed to be of the form 'YYY in XXXX' which works in Python2 and 3 Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 106 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/generator.py b/generator.py index ce2797e..8cce800 100755 --- a/generator.py +++ b/generator.py @@ -76,11 +76,11 @@ class docParser(xml.sax.handler.ContentHandler): self.function_return = None self.function_file = None self.function_module= None - if attrs.has_key('name'): + if 'name' in attrs: self.function = attrs['name'] - if attrs.has_key('file'): + if 'file' in attrs: self.function_file = attrs['file'] - if attrs.has_key('module'): + if 'module' in attrs: self.function_module= attrs['module'] elif tag == 'cond': self._data = [] @@ -91,24 +91,24 @@ class docParser(xml.sax.handler.ContentHandler): self.function_arg_name = None self.function_arg_type = None self.function_arg_info = None - if attrs.has_key('name'): + if 'name' in attrs: self.function_arg_name = attrs['name'] if self.function_arg_name == 'from': self.function_arg_name = 'frm' - if attrs.has_key('type'): + if 'type' in attrs: self.function_arg_type = attrs['type'] - if attrs.has_key('info'): + if 'info' in attrs: self.function_arg_info = attrs['info'] elif tag == 'return': if self.in_function == 1: self.function_return_type = None self.function_return_info = None self.function_return_field = None - if attrs.has_key('type'): + if 'type' in attrs: self.function_return_type = attrs['type'] - if attrs.has_key('info'): + if 'info' in attrs: self.function_return_info = attrs['info'] - if attrs.has_key('field'): + if 'field' in attrs: self.function_return_field = attrs['field'] elif tag == 'enum': # enums come from header files, hence virterror.h @@ -207,7 +207,7 @@ def lxc_function(name, desc, ret, args, file, module, cond): lxc_functions[name] = (desc, ret, args, file, module, cond) def enum(type, name, value): - if not enums.has_key(type): + if type not in enums: enums[type] = {} if value == 'VIR_TYPED_PARAM_INT': value = 1 @@ -233,14 +233,14 @@ def enum(type, name, value): enums[type][name] = value def lxc_enum(type, name, value): - if not lxc_enums.has_key(type): + if type not in lxc_enums: lxc_enums[type] = {} if onlyOverrides and name not in lxc_enums[type]: return lxc_enums[type][name] = value def qemu_enum(type, name, value): - if not qemu_enums.has_key(type): + if type not in qemu_enums: qemu_enums[type] = {} if onlyOverrides and name not in qemu_enums[type]: return @@ -637,7 +637,7 @@ def print_function_wrapper(module, name, output, export, include): if arg[1][0:6] == "const ": arg[1] = arg[1][6:] c_args = c_args + " %s %s;\n" % (arg[1], arg[0]) - if py_types.has_key(arg[1]): + if arg[1] in py_types: (f, t, n, c) = py_types[arg[1]] if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0): f = 't#' @@ -659,9 +659,9 @@ def print_function_wrapper(module, name, output, export, include): c_call = c_call + ", " c_call = c_call + "%s" % (arg[0]) else: - if skipped_types.has_key(arg[1]): + if arg[1] in skipped_types: return 0 - if unknown_types.has_key(arg[1]): + if arg[1] in unknown_types: lst = unknown_types[arg[1]] lst.append(name) else: @@ -683,7 +683,7 @@ def print_function_wrapper(module, name, output, export, include): else: c_call = "\n %s(%s);\n" % (name, c_call) ret_convert = " Py_INCREF(Py_None);\n return Py_None;\n" - elif py_types.has_key(ret[0]): + elif ret[0] in py_types: (f, t, n, c) = py_types[ret[0]] c_return = " %s c_retval;\n" % (ret[0]) if file == "python_accessor" and ret[2] is not None: @@ -692,16 +692,16 @@ def print_function_wrapper(module, name, output, export, include): c_call = "\n c_retval = %s(%s);\n" % (name, c_call) ret_convert = " py_retval = libvirt_%sWrap((%s) c_retval);\n" % (n,c) ret_convert = ret_convert + " return py_retval;\n" - elif py_return_types.has_key(ret[0]): + elif ret[0] in py_return_types: (f, t, n, c) = py_return_types[ret[0]] c_return = " %s c_retval;\n" % (ret[0]) c_call = "\n c_retval = %s(%s);\n" % (name, c_call) ret_convert = " py_retval = libvirt_%sWrap((%s) c_retval);\n" % (n,c) ret_convert = ret_convert + " return py_retval;\n" else: - if skipped_types.has_key(ret[0]): + if ret[0] in skipped_types: return 0 - if unknown_types.has_key(ret[0]): + if ret[0] in unknown_types: lst = unknown_types[ret[0]] lst.append(name) else: @@ -1252,10 +1252,10 @@ def buildWrappers(module): ctypes.append(type) ctypes_processed[type] = () for type in list(classes_type.keys()): - if ctypes_processed.has_key(type): + if type in ctypes_processed: continue tinfo = classes_type[type] - if not classes_processed.has_key(tinfo[2]): + if tinfo[2] not in classes_processed: classes_list.append(tinfo[2]) classes_processed[tinfo[2]] = () @@ -1317,7 +1317,7 @@ def buildWrappers(module): if extra is not None: extra.close() - if function_classes.has_key("None"): + if "None" in function_classes: flist = function_classes["None"] flist.sort(functionCompare) oldfile = "" @@ -1342,7 +1342,7 @@ def buildWrappers(module): writeDoc(module, name, args, ' ', classes) for arg in args: - if classes_type.has_key(arg[1]): + if arg[1] in classes_type: classes.write(" if %s is None: %s__o = None\n" % (arg[0], arg[0])) classes.write(" else: %s__o = %s%s\n" % @@ -1357,17 +1357,17 @@ def buildWrappers(module): if n != 0: classes.write(", ") classes.write("%s" % arg[0]) - if classes_type.has_key(arg[1]): + if arg[1] in classes_type: classes.write("__o") n = n + 1 classes.write(")\n") if ret[0] != "void": - if classes_type.has_key(ret[0]): + if ret[0] in classes_type: # # Raise an exception # - if functions_noexcept.has_key(name): + if name in functions_noexcept: classes.write(" if ret is None:return None\n") else: classes.write( @@ -1382,8 +1382,8 @@ def buildWrappers(module): # several things that we can do, depending on the # contents of functions_int_*: elif is_integral_type (ret[0]): - if not functions_noexcept.has_key (name): - if functions_int_exception_test.has_key (name): + if name not in functions_noexcept: + if name in functions_int_exception_test: test = functions_int_exception_test[name] else: test = functions_int_default_test @@ -1393,8 +1393,8 @@ def buildWrappers(module): classes.write(" return ret\n") elif is_python_noninteger_type (ret[0]): - if not functions_noexcept.has_key (name): - if functions_list_exception_test.has_key (name): + if name not in functions_noexcept: + if name in functions_list_exception_test: test = functions_list_exception_test[name] else: test = functions_list_default_test @@ -1412,11 +1412,11 @@ def buildWrappers(module): if classname == "None": pass else: - if classes_ancestor.has_key(classname): + if classname in classes_ancestor: classes.write("class %s(%s):\n" % (classname, classes_ancestor[classname])) classes.write(" def __init__(self, _obj=None):\n") - if reference_keepers.has_key(classname): + if classname in reference_keepers: rlist = reference_keepers[classname] for ref in rlist: classes.write(" self.%s = None\n" % ref[1]) @@ -1433,7 +1433,7 @@ def buildWrappers(module): classes.write(" def __init__(self, dom, _obj=None):\n") else: classes.write(" def __init__(self, _obj=None):\n") - if reference_keepers.has_key(classname): + if classname in reference_keepers: rlist = reference_keepers[classname] for ref in rlist: classes.write(" self.%s = None\n" % ref[1]) @@ -1450,7 +1450,7 @@ def buildWrappers(module): classes.write(" self._conn = dom.connect()\n") classes.write(" self._o = _obj\n\n") destruct=None - if classes_destructors.has_key(classname): + if classname in classes_destructors: classes.write(" def __del__(self):\n") classes.write(" if self._o is not None:\n") classes.write(" libvirtmod.%s(self._o)\n" % @@ -1458,12 +1458,12 @@ def buildWrappers(module): classes.write(" self._o = None\n\n") destruct=classes_destructors[classname] - if not class_skip_connect_impl.has_key(classname): + if classname not in class_skip_connect_impl: # Build python safe 'connect' method classes.write(" def connect(self):\n") classes.write(" return self._conn\n\n") - if class_domain_impl.has_key(classname): + if classname in class_domain_impl: classes.write(" def domain(self):\n") classes.write(" return self._dom\n\n") @@ -1502,7 +1502,7 @@ def buildWrappers(module): writeDoc(module, name, args, ' ', classes) n = 0 for arg in args: - if classes_type.has_key(arg[1]): + if arg[1] in classes_type: if n != index: classes.write(" if %s is None: %s__o = None\n" % (arg[0], arg[0])) @@ -1520,11 +1520,11 @@ def buildWrappers(module): classes.write(", ") if n != index: classes.write("%s" % arg[0]) - if classes_type.has_key(arg[1]): + if arg[1] in classes_type: classes.write("__o") else: classes.write("self") - if classes_type.has_key(arg[1]): + if arg[1] in classes_type: classes.write(classes_type[arg[1]][0]) n = n + 1 classes.write(")\n") @@ -1534,11 +1534,11 @@ def buildWrappers(module): # For functions returning object types: if ret[0] != "void": - if classes_type.has_key(ret[0]): + if ret[0] in classes_type: # # Raise an exception # - if functions_noexcept.has_key(name): + if name in functions_noexcept: classes.write( " if ret is None:return None\n") else: @@ -1588,7 +1588,7 @@ def buildWrappers(module): # See reference_keepers for the list # tclass = classes_type[ret[0]][2] - if reference_keepers.has_key(tclass): + if tclass in reference_keepers: rlist = reference_keepers[tclass] for pref in rlist: if pref[0] == classname: @@ -1596,7 +1596,7 @@ def buildWrappers(module): pref[1]) # Post-processing - just before we return. - if function_post.has_key(name): + if name in function_post: classes.write(" %s\n" % (function_post[name])) @@ -1604,16 +1604,16 @@ def buildWrappers(module): # return the class # classes.write(" return __tmp\n") - elif converter_type.has_key(ret[0]): + elif ret[0] in converter_type: # # Raise an exception # - if functions_noexcept.has_key(name): + if name in functions_noexcept: classes.write( " if ret is None:return None") # Post-processing - just before we return. - if function_post.has_key(name): + if name in function_post: classes.write(" %s\n" % (function_post[name])) @@ -1625,8 +1625,8 @@ def buildWrappers(module): # are several things that we can do, depending on # the contents of functions_int_*: elif is_integral_type (ret[0]): - if not functions_noexcept.has_key (name): - if functions_int_exception_test.has_key (name): + if name not in functions_noexcept: + if name in functions_int_exception_test: test = functions_int_exception_test[name] else: test = functions_int_default_test @@ -1660,15 +1660,15 @@ def buildWrappers(module): ("ret", name)) # Post-processing - just before we return. - if function_post.has_key(name): + if name in function_post: classes.write(" %s\n" % (function_post[name])) classes.write (" return ret\n") elif is_python_noninteger_type (ret[0]): - if not functions_noexcept.has_key (name): - if functions_list_exception_test.has_key (name): + if name not in functions_noexcept: + if name in functions_list_exception_test: test = functions_list_exception_test[name] else: test = functions_list_default_test @@ -1702,7 +1702,7 @@ def buildWrappers(module): ("ret", name)) # Post-processing - just before we return. - if function_post.has_key(name): + if name in function_post: classes.write(" %s\n" % (function_post[name])) @@ -1710,7 +1710,7 @@ def buildWrappers(module): else: # Post-processing - just before we return. - if function_post.has_key(name): + if name in function_post: classes.write(" %s\n" % (function_post[name])) -- 1.8.3.1

On Tue, Dec 3, 2013 at 10:36 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
The code 'XXX.has_key(YYYY)' must be changed to be of the form 'YYY in XXXX' which works in Python2 and 3
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 106 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 53 insertions(+), 53 deletions(-)
diff --git a/generator.py b/generator.py index ce2797e..8cce800 100755 --- a/generator.py +++ b/generator.py @@ -76,11 +76,11 @@ class docParser(xml.sax.handler.ContentHandler): self.function_return = None self.function_file = None self.function_module= None - if attrs.has_key('name'): + if 'name' in attrs: self.function = attrs['name'] - if attrs.has_key('file'): + if 'file' in attrs: self.function_file = attrs['file'] - if attrs.has_key('module'): + if 'module' in attrs: self.function_module= attrs['module'] elif tag == 'cond': self._data = [] @@ -91,24 +91,24 @@ class docParser(xml.sax.handler.ContentHandler): self.function_arg_name = None self.function_arg_type = None self.function_arg_info = None - if attrs.has_key('name'): + if 'name' in attrs: self.function_arg_name = attrs['name'] if self.function_arg_name == 'from': self.function_arg_name = 'frm' - if attrs.has_key('type'): + if 'type' in attrs: self.function_arg_type = attrs['type'] - if attrs.has_key('info'): + if 'info' in attrs: self.function_arg_info = attrs['info'] elif tag == 'return': if self.in_function == 1: self.function_return_type = None self.function_return_info = None self.function_return_field = None - if attrs.has_key('type'): + if 'type' in attrs: self.function_return_type = attrs['type'] - if attrs.has_key('info'): + if 'info' in attrs: self.function_return_info = attrs['info'] - if attrs.has_key('field'): + if 'field' in attrs: self.function_return_field = attrs['field'] elif tag == 'enum': # enums come from header files, hence virterror.h @@ -207,7 +207,7 @@ def lxc_function(name, desc, ret, args, file, module, cond): lxc_functions[name] = (desc, ret, args, file, module, cond)
def enum(type, name, value): - if not enums.has_key(type): + if type not in enums: enums[type] = {} if value == 'VIR_TYPED_PARAM_INT': value = 1 @@ -233,14 +233,14 @@ def enum(type, name, value): enums[type][name] = value
def lxc_enum(type, name, value): - if not lxc_enums.has_key(type): + if type not in lxc_enums: lxc_enums[type] = {} if onlyOverrides and name not in lxc_enums[type]: return lxc_enums[type][name] = value
def qemu_enum(type, name, value): - if not qemu_enums.has_key(type): + if type not in qemu_enums: qemu_enums[type] = {} if onlyOverrides and name not in qemu_enums[type]: return @@ -637,7 +637,7 @@ def print_function_wrapper(module, name, output, export, include): if arg[1][0:6] == "const ": arg[1] = arg[1][6:] c_args = c_args + " %s %s;\n" % (arg[1], arg[0]) - if py_types.has_key(arg[1]): + if arg[1] in py_types: (f, t, n, c) = py_types[arg[1]] if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0): f = 't#' @@ -659,9 +659,9 @@ def print_function_wrapper(module, name, output, export, include): c_call = c_call + ", " c_call = c_call + "%s" % (arg[0]) else: - if skipped_types.has_key(arg[1]): + if arg[1] in skipped_types: return 0 - if unknown_types.has_key(arg[1]): + if arg[1] in unknown_types: lst = unknown_types[arg[1]] lst.append(name) else: @@ -683,7 +683,7 @@ def print_function_wrapper(module, name, output, export, include): else: c_call = "\n %s(%s);\n" % (name, c_call) ret_convert = " Py_INCREF(Py_None);\n return Py_None;\n" - elif py_types.has_key(ret[0]): + elif ret[0] in py_types: (f, t, n, c) = py_types[ret[0]] c_return = " %s c_retval;\n" % (ret[0]) if file == "python_accessor" and ret[2] is not None: @@ -692,16 +692,16 @@ def print_function_wrapper(module, name, output, export, include): c_call = "\n c_retval = %s(%s);\n" % (name, c_call) ret_convert = " py_retval = libvirt_%sWrap((%s) c_retval);\n" % (n,c) ret_convert = ret_convert + " return py_retval;\n" - elif py_return_types.has_key(ret[0]): + elif ret[0] in py_return_types: (f, t, n, c) = py_return_types[ret[0]] c_return = " %s c_retval;\n" % (ret[0]) c_call = "\n c_retval = %s(%s);\n" % (name, c_call) ret_convert = " py_retval = libvirt_%sWrap((%s) c_retval);\n" % (n,c) ret_convert = ret_convert + " return py_retval;\n" else: - if skipped_types.has_key(ret[0]): + if ret[0] in skipped_types: return 0 - if unknown_types.has_key(ret[0]): + if ret[0] in unknown_types: lst = unknown_types[ret[0]] lst.append(name) else: @@ -1252,10 +1252,10 @@ def buildWrappers(module): ctypes.append(type) ctypes_processed[type] = () for type in list(classes_type.keys()): - if ctypes_processed.has_key(type): + if type in ctypes_processed: continue tinfo = classes_type[type] - if not classes_processed.has_key(tinfo[2]): + if tinfo[2] not in classes_processed: classes_list.append(tinfo[2]) classes_processed[tinfo[2]] = ()
@@ -1317,7 +1317,7 @@ def buildWrappers(module): if extra is not None: extra.close()
- if function_classes.has_key("None"): + if "None" in function_classes: flist = function_classes["None"] flist.sort(functionCompare) oldfile = "" @@ -1342,7 +1342,7 @@ def buildWrappers(module): writeDoc(module, name, args, ' ', classes)
for arg in args: - if classes_type.has_key(arg[1]): + if arg[1] in classes_type: classes.write(" if %s is None: %s__o = None\n" % (arg[0], arg[0])) classes.write(" else: %s__o = %s%s\n" % @@ -1357,17 +1357,17 @@ def buildWrappers(module): if n != 0: classes.write(", ") classes.write("%s" % arg[0]) - if classes_type.has_key(arg[1]): + if arg[1] in classes_type: classes.write("__o") n = n + 1 classes.write(")\n")
if ret[0] != "void": - if classes_type.has_key(ret[0]): + if ret[0] in classes_type: # # Raise an exception # - if functions_noexcept.has_key(name): + if name in functions_noexcept: classes.write(" if ret is None:return None\n") else: classes.write( @@ -1382,8 +1382,8 @@ def buildWrappers(module): # several things that we can do, depending on the # contents of functions_int_*: elif is_integral_type (ret[0]): - if not functions_noexcept.has_key (name): - if functions_int_exception_test.has_key (name): + if name not in functions_noexcept: + if name in functions_int_exception_test: test = functions_int_exception_test[name] else: test = functions_int_default_test @@ -1393,8 +1393,8 @@ def buildWrappers(module): classes.write(" return ret\n")
elif is_python_noninteger_type (ret[0]): - if not functions_noexcept.has_key (name): - if functions_list_exception_test.has_key (name): + if name not in functions_noexcept: + if name in functions_list_exception_test: test = functions_list_exception_test[name] else: test = functions_list_default_test @@ -1412,11 +1412,11 @@ def buildWrappers(module): if classname == "None": pass else: - if classes_ancestor.has_key(classname): + if classname in classes_ancestor: classes.write("class %s(%s):\n" % (classname, classes_ancestor[classname])) classes.write(" def __init__(self, _obj=None):\n") - if reference_keepers.has_key(classname): + if classname in reference_keepers: rlist = reference_keepers[classname] for ref in rlist: classes.write(" self.%s = None\n" % ref[1]) @@ -1433,7 +1433,7 @@ def buildWrappers(module): classes.write(" def __init__(self, dom, _obj=None):\n") else: classes.write(" def __init__(self, _obj=None):\n") - if reference_keepers.has_key(classname): + if classname in reference_keepers: rlist = reference_keepers[classname] for ref in rlist: classes.write(" self.%s = None\n" % ref[1]) @@ -1450,7 +1450,7 @@ def buildWrappers(module): classes.write(" self._conn = dom.connect()\n") classes.write(" self._o = _obj\n\n") destruct=None - if classes_destructors.has_key(classname): + if classname in classes_destructors: classes.write(" def __del__(self):\n") classes.write(" if self._o is not None:\n") classes.write(" libvirtmod.%s(self._o)\n" % @@ -1458,12 +1458,12 @@ def buildWrappers(module): classes.write(" self._o = None\n\n") destruct=classes_destructors[classname]
- if not class_skip_connect_impl.has_key(classname): + if classname not in class_skip_connect_impl: # Build python safe 'connect' method classes.write(" def connect(self):\n") classes.write(" return self._conn\n\n")
- if class_domain_impl.has_key(classname): + if classname in class_domain_impl: classes.write(" def domain(self):\n") classes.write(" return self._dom\n\n")
@@ -1502,7 +1502,7 @@ def buildWrappers(module): writeDoc(module, name, args, ' ', classes) n = 0 for arg in args: - if classes_type.has_key(arg[1]): + if arg[1] in classes_type: if n != index: classes.write(" if %s is None: %s__o = None\n" % (arg[0], arg[0])) @@ -1520,11 +1520,11 @@ def buildWrappers(module): classes.write(", ") if n != index: classes.write("%s" % arg[0]) - if classes_type.has_key(arg[1]): + if arg[1] in classes_type: classes.write("__o") else: classes.write("self") - if classes_type.has_key(arg[1]): + if arg[1] in classes_type: classes.write(classes_type[arg[1]][0]) n = n + 1 classes.write(")\n") @@ -1534,11 +1534,11 @@ def buildWrappers(module):
# For functions returning object types: if ret[0] != "void": - if classes_type.has_key(ret[0]): + if ret[0] in classes_type: # # Raise an exception # - if functions_noexcept.has_key(name): + if name in functions_noexcept: classes.write( " if ret is None:return None\n") else: @@ -1588,7 +1588,7 @@ def buildWrappers(module): # See reference_keepers for the list # tclass = classes_type[ret[0]][2] - if reference_keepers.has_key(tclass): + if tclass in reference_keepers: rlist = reference_keepers[tclass] for pref in rlist: if pref[0] == classname: @@ -1596,7 +1596,7 @@ def buildWrappers(module): pref[1])
# Post-processing - just before we return. - if function_post.has_key(name): + if name in function_post: classes.write(" %s\n" % (function_post[name]))
@@ -1604,16 +1604,16 @@ def buildWrappers(module): # return the class # classes.write(" return __tmp\n") - elif converter_type.has_key(ret[0]): + elif ret[0] in converter_type: # # Raise an exception # - if functions_noexcept.has_key(name): + if name in functions_noexcept: classes.write( " if ret is None:return None")
# Post-processing - just before we return. - if function_post.has_key(name): + if name in function_post: classes.write(" %s\n" % (function_post[name]))
@@ -1625,8 +1625,8 @@ def buildWrappers(module): # are several things that we can do, depending on # the contents of functions_int_*: elif is_integral_type (ret[0]): - if not functions_noexcept.has_key (name): - if functions_int_exception_test.has_key (name): + if name not in functions_noexcept: + if name in functions_int_exception_test: test = functions_int_exception_test[name] else: test = functions_int_default_test @@ -1660,15 +1660,15 @@ def buildWrappers(module): ("ret", name))
# Post-processing - just before we return. - if function_post.has_key(name): + if name in function_post: classes.write(" %s\n" % (function_post[name]))
classes.write (" return ret\n")
elif is_python_noninteger_type (ret[0]): - if not functions_noexcept.has_key (name): - if functions_list_exception_test.has_key (name): + if name not in functions_noexcept: + if name in functions_list_exception_test: test = functions_list_exception_test[name] else: test = functions_list_default_test @@ -1702,7 +1702,7 @@ def buildWrappers(module): ("ret", name))
# Post-processing - just before we return. - if function_post.has_key(name): + if name in function_post: classes.write(" %s\n" % (function_post[name]))
@@ -1710,7 +1710,7 @@ def buildWrappers(module):
else: # Post-processing - just before we return. - if function_post.has_key(name): + if name in function_post: classes.write(" %s\n" % (function_post[name]))
-- 1.8.3.1
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
ACK. -- Doug Goldstein

On Tue, Dec 03, 2013 at 04:36:41PM +0000, Daniel P. Berrange wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
The code 'XXX.has_key(YYYY)' must be changed to be of the form 'YYY in XXXX' which works in Python2 and 3
Self-NACK. For reasons I don't yet understand this breaks on python 2.4 Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

From: "Daniel P. Berrange" <berrange@redhat.com> The sort() method previously took either a compartor function or a key function. Only the latter is supported in Python3. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/generator.py b/generator.py index 8cce800..9f4b76b 100755 --- a/generator.py +++ b/generator.py @@ -1172,23 +1172,9 @@ def nameFixup(name, classe, type, file): return func -def functionCompare(info1, info2): - (index1, func1, name1, ret1, args1, file1, mod1) = info1 - (index2, func2, name2, ret2, args2, file2, mod2) = info2 - if file1 == file2: - if func1 < func2: - return -1 - if func1 > func2: - return 1 - if file1 == "python_accessor": - return -1 - if file2 == "python_accessor": - return 1 - if file1 < file2: - return -1 - if file1 > file2: - return 1 - return 0 +def functionSortKey(info): + (index, func, name, ret, args, filename, mod) = info + return func, filename def writeDoc(module, name, args, indent, output): if module == "libvirt": @@ -1319,7 +1305,7 @@ def buildWrappers(module): if "None" in function_classes: flist = function_classes["None"] - flist.sort(functionCompare) + flist.sort(key=functionSortKey) oldfile = "" for info in flist: (index, func, name, ret, args, file, mod) = info @@ -1468,7 +1454,7 @@ def buildWrappers(module): classes.write(" return self._dom\n\n") flist = function_classes[classname] - flist.sort(functionCompare) + flist.sort(key=functionSortKey) oldfile = "" for info in flist: (index, func, name, ret, args, file, mod) = info @@ -1775,7 +1761,7 @@ def buildWrappers(module): for type,enum in list(enums.items()): classes.write("# %s\n" % type) items = list(enum.items()) - items.sort(lambda i1,i2: cmp(int(i1[1]),int(i2[1]))) + items.sort(key=lambda i: int(i[1])) for name,value in items: classes.write("%s = %s\n" % (name,value)) classes.write("\n") @@ -1885,7 +1871,7 @@ def qemuBuildWrappers(module): for type,enum in list(qemu_enums.items()): fd.write("# %s\n" % type) items = list(enum.items()) - items.sort(lambda i1,i2: cmp(int(i1[1]),int(i2[1]))) + items.sort(key=lambda i: int(i[1])) for name,value in items: fd.write("%s = %s\n" % (name,value)) fd.write("\n") @@ -1996,7 +1982,7 @@ def lxcBuildWrappers(module): for type,enum in list(lxc_enums.items()): fd.write("# %s\n" % type) items = list(enum.items()) - items.sort(lambda i1,i2: cmp(int(i1[1]),int(i2[1]))) + items.sort(key=lambda i: int(i[1])) for name,value in items: fd.write("%s = %s\n" % (name,value)) fd.write("\n") -- 1.8.3.1

On Tue, Dec 3, 2013 at 10:36 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
The sort() method previously took either a compartor function or a key function. Only the latter is supported in Python3.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-)
diff --git a/generator.py b/generator.py index 8cce800..9f4b76b 100755 --- a/generator.py +++ b/generator.py @@ -1172,23 +1172,9 @@ def nameFixup(name, classe, type, file): return func
-def functionCompare(info1, info2): - (index1, func1, name1, ret1, args1, file1, mod1) = info1 - (index2, func2, name2, ret2, args2, file2, mod2) = info2 - if file1 == file2: - if func1 < func2: - return -1 - if func1 > func2: - return 1 - if file1 == "python_accessor": - return -1 - if file2 == "python_accessor": - return 1 - if file1 < file2: - return -1 - if file1 > file2: - return 1 - return 0 +def functionSortKey(info): + (index, func, name, ret, args, filename, mod) = info + return func, filename
def writeDoc(module, name, args, indent, output): if module == "libvirt": @@ -1319,7 +1305,7 @@ def buildWrappers(module):
if "None" in function_classes: flist = function_classes["None"] - flist.sort(functionCompare) + flist.sort(key=functionSortKey) oldfile = "" for info in flist: (index, func, name, ret, args, file, mod) = info @@ -1468,7 +1454,7 @@ def buildWrappers(module): classes.write(" return self._dom\n\n")
flist = function_classes[classname] - flist.sort(functionCompare) + flist.sort(key=functionSortKey) oldfile = "" for info in flist: (index, func, name, ret, args, file, mod) = info @@ -1775,7 +1761,7 @@ def buildWrappers(module): for type,enum in list(enums.items()): classes.write("# %s\n" % type) items = list(enum.items()) - items.sort(lambda i1,i2: cmp(int(i1[1]),int(i2[1]))) + items.sort(key=lambda i: int(i[1])) for name,value in items: classes.write("%s = %s\n" % (name,value)) classes.write("\n") @@ -1885,7 +1871,7 @@ def qemuBuildWrappers(module): for type,enum in list(qemu_enums.items()): fd.write("# %s\n" % type) items = list(enum.items()) - items.sort(lambda i1,i2: cmp(int(i1[1]),int(i2[1]))) + items.sort(key=lambda i: int(i[1])) for name,value in items: fd.write("%s = %s\n" % (name,value)) fd.write("\n") @@ -1996,7 +1982,7 @@ def lxcBuildWrappers(module): for type,enum in list(lxc_enums.items()): fd.write("# %s\n" % type) items = list(enum.items()) - items.sort(lambda i1,i2: cmp(int(i1[1]),int(i2[1]))) + items.sort(key=lambda i: int(i[1])) for name,value in items: fd.write("%s = %s\n" % (name,value)) fd.write("\n") -- 1.8.3.1
Visually makes sense. Have not run it locally but ACK from a visual stand point. -- Doug Goldstein

On 12/03/2013 09:36 AM, Daniel P. Berrange wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
The sort() method previously took either a compartor function
s/compartor/comparator/
or a key function. Only the latter is supported in Python3.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-)
-- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

From: "Daniel P. Berrange" <berrange@redhat.com> Call the 'replace' and 'find' functions directly on the string variables, instead of via the 'string' module. Python3 only accepts the latter syntax Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/generator.py b/generator.py index 9f4b76b..af05d7c 100755 --- a/generator.py +++ b/generator.py @@ -1186,15 +1186,15 @@ def writeDoc(module, name, args, indent, output): if funcs[name][0] is None or funcs[name][0] == "": return val = funcs[name][0] - val = string.replace(val, "NULL", "None") + val = val.replace("NULL", "None") output.write(indent) output.write('"""') - i = string.find(val, "\n") + i = val.find("\n") while i >= 0: str = val[0:i+1] val = val[i+1:] output.write(str) - i = string.find(val, "\n") + i = val.find("\n") output.write(indent) output.write(val) output.write(' """\n') -- 1.8.3.1

From: "Daniel P. Berrange" <berrange@redhat.com> To assist in diff comparisons between code generated with different versions of Python, do an explicit sort of all functions and enums. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/generator.py b/generator.py index af05d7c..17f00d6 100755 --- a/generator.py +++ b/generator.py @@ -865,7 +865,10 @@ def buildStubs(module, api_xml): wrapper.write("#include \"typewrappers.h\"\n") wrapper.write("#include \"build/" + module + ".h\"\n\n") - for function in list(funcs.keys()): + funcnames = list(funcs.keys()) + if funcnames is not None: + funcnames.sort() + for function in funcnames: # Skip the functions which are not for the module ret = print_function_wrapper(module, function, wrapper, export, include) if ret < 0: @@ -1758,7 +1761,10 @@ def buildWrappers(module): # # Generate enum constants # - for type,enum in list(enums.items()): + enumvals = list(enums.items()) + if enumvals is not None: + enumvals.sort(key=lambda x: x[0]) + for type,enum in enumvals: classes.write("# %s\n" % type) items = list(enum.items()) items.sort(key=lambda i: int(i[1])) -- 1.8.3.1

On Tue, Dec 3, 2013 at 10:36 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
To assist in diff comparisons between code generated with different versions of Python, do an explicit sort of all functions and enums.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/generator.py b/generator.py index af05d7c..17f00d6 100755 --- a/generator.py +++ b/generator.py @@ -865,7 +865,10 @@ def buildStubs(module, api_xml): wrapper.write("#include \"typewrappers.h\"\n") wrapper.write("#include \"build/" + module + ".h\"\n\n")
- for function in list(funcs.keys()): + funcnames = list(funcs.keys()) + if funcnames is not None: + funcnames.sort() + for function in funcnames: # Skip the functions which are not for the module ret = print_function_wrapper(module, function, wrapper, export, include) if ret < 0: @@ -1758,7 +1761,10 @@ def buildWrappers(module): # # Generate enum constants # - for type,enum in list(enums.items()): + enumvals = list(enums.items()) + if enumvals is not None: + enumvals.sort(key=lambda x: x[0]) + for type,enum in enumvals: classes.write("# %s\n" % type) items = list(enum.items()) items.sort(key=lambda i: int(i[1])) -- 1.8.3.1
ACK. -- Doug Goldstein

From: "Daniel P. Berrange" <berrange@redhat.com> In Python3 you cannot use 'except Foo, e' you must use 'except Foo as e' instead, or just 'except Foo' if the variable isn't required. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index bf222f8..ecbce1f 100755 --- a/setup.py +++ b/setup.py @@ -40,7 +40,7 @@ try: spawn([pkgcfg, "--atleast-version=%s" % MIN_LIBVIRT_LXC, "libvirt"]) -except DistutilsExecError,e: +except DistutilsExecError: have_libvirt_lxc=False def get_pkgconfig_data(args, mod, required=True): -- 1.8.3.1

On Tue, Dec 3, 2013 at 10:36 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
In Python3 you cannot use 'except Foo, e' you must use 'except Foo as e' instead, or just 'except Foo' if the variable isn't required.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/setup.py b/setup.py index bf222f8..ecbce1f 100755 --- a/setup.py +++ b/setup.py @@ -40,7 +40,7 @@ try: spawn([pkgcfg, "--atleast-version=%s" % MIN_LIBVIRT_LXC, "libvirt"]) -except DistutilsExecError,e: +except DistutilsExecError: have_libvirt_lxc=False
def get_pkgconfig_data(args, mod, required=True): -- 1.8.3.1
ACK. -- Doug Goldstein

From: "Daniel P. Berrange" <berrange@redhat.com> The 'print' method must be called as a function in python3, ie with brackets. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- examples/consolecallback.py | 6 ++-- examples/dominfo.py | 14 +++++----- examples/domrestore.py | 17 ++++++------ examples/domsave.py | 15 +++++----- examples/domstart.py | 19 ++++++------- examples/esxlist.py | 14 +++++----- examples/event-test.py | 68 ++++++++++++++++++++++----------------------- examples/topology.py | 14 +++++----- 8 files changed, 82 insertions(+), 85 deletions(-) diff --git a/examples/consolecallback.py b/examples/consolecallback.py index d8e33a9..c539a92 100644 --- a/examples/consolecallback.py +++ b/examples/consolecallback.py @@ -62,14 +62,14 @@ def lifecycle_callback (connection, domain, event, detail, console): # main if len(sys.argv) != 3: - print "Usage:", sys.argv[0], "URI UUID" - print "for example:", sys.argv[0], "'qemu:///system' '32ad945f-7e78-c33a-e96d-39f25e025d81'" + print("Usage:", sys.argv[0], "URI UUID") + print("for example:", sys.argv[0], "'qemu:///system' '32ad945f-7e78-c33a-e96d-39f25e025d81'") sys.exit(1) uri = sys.argv[1] uuid = sys.argv[2] -print "Escape character is ^]" +print("Escape character is ^]") logging.basicConfig(filename='msg.log', level=logging.DEBUG) logging.info("URI: %s", uri) logging.info("UUID: %s", uuid) diff --git a/examples/dominfo.py b/examples/dominfo.py index bfa3ca3..d3049cd 100755 --- a/examples/dominfo.py +++ b/examples/dominfo.py @@ -8,15 +8,15 @@ import libxml2 import pdb def usage(): - print 'Usage: %s DOMAIN' % sys.argv[0] - print ' Print information about the domain DOMAIN' + print('Usage: %s DOMAIN' % sys.argv[0]) + print(' Print information about the domain DOMAIN') def print_section(title): - print "\n%s" % title - print "=" * 60 + print("\n%s" % title) + print("=" * 60) def print_entry(key, value): - print "%-10s %-10s" % (key, value) + print("%-10s %-10s" % (key, value)) def print_xml(key, ctx, path): res = ctx.xpathEval(path) @@ -36,14 +36,14 @@ name = sys.argv[1] # Connect to libvirt conn = libvirt.openReadOnly(None) if conn is None: - print 'Failed to open connection to the hypervisor' + print('Failed to open connection to the hypervisor') sys.exit(1) try: dom = conn.lookupByName(name) # Annoyiingly, libvirt prints its own error message here except libvirt.libvirtError: - print "Domain %s is not running" % name + print("Domain %s is not running" % name) sys.exit(0) info = dom.info() diff --git a/examples/domrestore.py b/examples/domrestore.py index fffc90f..06fdfbc 100755 --- a/examples/domrestore.py +++ b/examples/domrestore.py @@ -8,10 +8,10 @@ import libxml2 import pdb def usage(): - print 'Usage: %s DIR' % sys.argv[0] - print ' Restore all the domains contained in DIR' - print ' It is assumed that all files in DIR are' - print ' images of domU\'s previously created with save' + print('Usage: %s DIR' % sys.argv[0]) + print(' Restore all the domains contained in DIR') + print(' It is assumed that all files in DIR are') + print(' images of domU\'s previously created with save') if len(sys.argv) != 2: usage() @@ -22,15 +22,14 @@ imgs = os.listdir(dir) conn = libvirt.open(None) if conn is None: - print 'Failed to open connection to the hypervisor' + print('Failed to open connection to the hypervisor') sys.exit(1) for img in imgs: file = os.path.join(dir, img) - print "Restoring %s ... " % img, - sys.stdout.flush() + print("Restoring %s ... " % img) ret = conn.restore(file) if ret == 0: - print "done" + print("done") else: - print "error %d" % ret + print("error %d" % ret) diff --git a/examples/domsave.py b/examples/domsave.py index bac4536..727217c 100755 --- a/examples/domsave.py +++ b/examples/domsave.py @@ -8,9 +8,9 @@ import libxml2 import pdb def usage(): - print 'Usage: %s DIR' % sys.argv[0] - print ' Save all currently running domU\'s into DIR' - print ' DIR must exist and be writable by this process' + print('Usage: %s DIR' % sys.argv[0]) + print(' Save all currently running domU\'s into DIR') + print(' DIR must exist and be writable by this process') if len(sys.argv) != 2: usage() @@ -20,7 +20,7 @@ dir = sys.argv[1] conn = libvirt.open(None) if conn is None: - print 'Failed to open connection to the hypervisor' + print('Failed to open connection to the hypervisor') sys.exit(1) doms = conn.listDomainsID() @@ -28,13 +28,12 @@ for id in doms: if id == 0: continue dom = conn.lookupByID(id) - print "Saving %s[%d] ... " % (dom.name(), id), - sys.stdout.flush() + print("Saving %s[%d] ... " % (dom.name(), id)) path = os.path.join(dir, dom.name()) ret = dom.save(path) if ret == 0: - print "done" + print("done") else: - print "error %d" % ret + print("error %d" % ret) #pdb.set_trace() diff --git a/examples/domstart.py b/examples/domstart.py index b14fad1..ce1b60c 100755 --- a/examples/domstart.py +++ b/examples/domstart.py @@ -20,11 +20,11 @@ def read_domain(fname): return (name, xmldesc) def usage(): - print 'Usage: %s domain.xml' % sys.argv[0] - print ' Check that the domain described by DOMAIN.XML is running' - print ' If the domain is not running, create it' - print ' DOMAIN.XML must be a XML description of the domain' - print ' in libvirt\'s XML format' + print('Usage: %s domain.xml' % sys.argv[0]) + print(' Check that the domain described by DOMAIN.XML is running') + print(' If the domain is not running, create it') + print(' DOMAIN.XML must be a XML description of the domain') + print(' in libvirt\'s XML format') if len(sys.argv) != 2: usage() @@ -34,17 +34,16 @@ if len(sys.argv) != 2: conn = libvirt.open(None) if conn is None: - print 'Failed to open connection to the hypervisor' + print('Failed to open connection to the hypervisor') sys.exit(1) try: dom = conn.lookupByName(name) except libvirt.libvirtError: - print "Starting domain %s ... " % name, - sys.stdout.flush() + print("Starting domain %s ... " % name) dom = conn.createLinux(xmldesc, 0) if dom is None: - print "failed" + print("failed") sys.exit(1) else: - print "done" + print("done") diff --git a/examples/esxlist.py b/examples/esxlist.py index c55424f..0d47b00 100755 --- a/examples/esxlist.py +++ b/examples/esxlist.py @@ -10,8 +10,8 @@ import getpass def usage(): - print "Usage: %s HOSTNAME" % sys.argv[0] - print " List active domains of HOSTNAME and print some info" + print("Usage: %s HOSTNAME" % sys.argv[0]) + print(" List active domains of HOSTNAME and print some info") # This is the callback method passed to libvirt.openAuth() (see below). @@ -51,12 +51,12 @@ def request_credentials(credentials, user_data): def print_section(title): - print "\n%s" % title - print "=" * 60 + print("\n%s" % title) + print("=" * 60) def print_entry(key, value): - print "%-10s %-10s" % (key, value) + print("%-10s %-10s" % (key, value)) def print_xml(key, ctx, path): @@ -100,7 +100,7 @@ auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_NOECHOPROMPT], conn = libvirt.openAuth(uri, auth, 0) if conn is None: - print "Failed to open connection to %s" % hostname + print("Failed to open connection to %s" % hostname) sys.exit(1) state_names = { libvirt.VIR_DOMAIN_RUNNING : "running", @@ -136,7 +136,7 @@ for id in conn.listDomainsID(): ctx.setContextNode(d) if not first: - print "------------------------------------------------------------" + print("------------------------------------------------------------") else: first = False diff --git a/examples/event-test.py b/examples/event-test.py index 84f5259..cf1a8b8 100644 --- a/examples/event-test.py +++ b/examples/event-test.py @@ -30,7 +30,7 @@ do_debug = False def debug(msg): global do_debug if do_debug: - print msg + print(msg) # # This general purpose event loop will support waiting for file handle @@ -456,50 +456,50 @@ def detailToString(event, detail): return eventStrings[event][detail] def myDomainEventCallback1 (conn, dom, event, detail, opaque): - print "myDomainEventCallback1 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(), + print("myDomainEventCallback1 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(), eventToString(event), - detailToString(event, detail)) + detailToString(event, detail))) def myDomainEventCallback2 (conn, dom, event, detail, opaque): - print "myDomainEventCallback2 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(), + print("myDomainEventCallback2 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(), eventToString(event), - detailToString(event, detail)) + detailToString(event, detail))) def myDomainEventRebootCallback(conn, dom, opaque): - print "myDomainEventRebootCallback: Domain %s(%s)" % (dom.name(), dom.ID()) + print("myDomainEventRebootCallback: Domain %s(%s)" % (dom.name(), dom.ID())) def myDomainEventRTCChangeCallback(conn, dom, utcoffset, opaque): - print "myDomainEventRTCChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), utcoffset) + print("myDomainEventRTCChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), utcoffset)) def myDomainEventWatchdogCallback(conn, dom, action, opaque): - print "myDomainEventWatchdogCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), action) + print("myDomainEventWatchdogCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), action)) def myDomainEventIOErrorCallback(conn, dom, srcpath, devalias, action, opaque): - print "myDomainEventIOErrorCallback: Domain %s(%s) %s %s %d" % (dom.name(), dom.ID(), srcpath, devalias, action) + print("myDomainEventIOErrorCallback: Domain %s(%s) %s %s %d" % (dom.name(), dom.ID(), srcpath, devalias, action)) def myDomainEventGraphicsCallback(conn, dom, phase, localAddr, remoteAddr, authScheme, subject, opaque): - print "myDomainEventGraphicsCallback: Domain %s(%s) %d %s" % (dom.name(), dom.ID(), phase, authScheme) + print("myDomainEventGraphicsCallback: Domain %s(%s) %d %s" % (dom.name(), dom.ID(), phase, authScheme)) def myDomainEventDiskChangeCallback(conn, dom, oldSrcPath, newSrcPath, devAlias, reason, opaque): - print "myDomainEventDiskChangeCallback: Domain %s(%s) disk change oldSrcPath: %s newSrcPath: %s devAlias: %s reason: %s" % ( - dom.name(), dom.ID(), oldSrcPath, newSrcPath, devAlias, reason) + print("myDomainEventDiskChangeCallback: Domain %s(%s) disk change oldSrcPath: %s newSrcPath: %s devAlias: %s reason: %s" % ( + dom.name(), dom.ID(), oldSrcPath, newSrcPath, devAlias, reason)) def myDomainEventTrayChangeCallback(conn, dom, devAlias, reason, opaque): - print "myDomainEventTrayChangeCallback: Domain %s(%s) tray change devAlias: %s reason: %s" % ( - dom.name(), dom.ID(), devAlias, reason) + print("myDomainEventTrayChangeCallback: Domain %s(%s) tray change devAlias: %s reason: %s" % ( + dom.name(), dom.ID(), devAlias, reason)) def myDomainEventPMWakeupCallback(conn, dom, reason, opaque): - print "myDomainEventPMWakeupCallback: Domain %s(%s) system pmwakeup" % ( - dom.name(), dom.ID()) + print("myDomainEventPMWakeupCallback: Domain %s(%s) system pmwakeup" % ( + dom.name(), dom.ID())) def myDomainEventPMSuspendCallback(conn, dom, reason, opaque): - print "myDomainEventPMSuspendCallback: Domain %s(%s) system pmsuspend" % ( - dom.name(), dom.ID()) + print("myDomainEventPMSuspendCallback: Domain %s(%s) system pmsuspend" % ( + dom.name(), dom.ID())) def myDomainEventBalloonChangeCallback(conn, dom, actual, opaque): - print "myDomainEventBalloonChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), actual) + print("myDomainEventBalloonChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), actual)) def myDomainEventPMSuspendDiskCallback(conn, dom, reason, opaque): - print "myDomainEventPMSuspendDiskCallback: Domain %s(%s) system pmsuspend_disk" % ( - dom.name(), dom.ID()) + print("myDomainEventPMSuspendDiskCallback: Domain %s(%s) system pmsuspend_disk" % ( + dom.name(), dom.ID())) def myDomainEventDeviceRemovedCallback(conn, dom, dev, opaque): - print "myDomainEventDeviceRemovedCallback: Domain %s(%s) device removed: %s" % ( - dom.name(), dom.ID(), dev) + print("myDomainEventDeviceRemovedCallback: Domain %s(%s) device removed: %s" % ( + dom.name(), dom.ID(), dev)) run = True @@ -507,27 +507,27 @@ def myConnectionCloseCallback(conn, reason, opaque): reasonStrings = ( "Error", "End-of-file", "Keepalive", "Client", ) - print "myConnectionCloseCallback: %s: %s" % (conn.getURI(), reasonStrings[reason]) + print("myConnectionCloseCallback: %s: %s" % (conn.getURI(), reasonStrings[reason])) run = False -def usage(out=sys.stderr): - print >>out, "usage: "+os.path.basename(sys.argv[0])+" [-hdl] [uri]" - print >>out, " uri will default to qemu:///system" - print >>out, " --help, -h Print this help message" - print >>out, " --debug, -d Print debug output" - print >>out, " --loop, -l Toggle event-loop-implementation" +def usage(): + print("usage: "+os.path.basename(sys.argv[0])+" [-hdl] [uri]") + print(" uri will default to qemu:///system") + print(" --help, -h Print(this help message") + print(" --debug, -d Print(debug output") + print(" --loop, -l Toggle event-loop-implementation") def main(): try: opts, args = getopt.getopt(sys.argv[1:], "hdl", ["help", "debug", "loop"]) except getopt.GetoptError, err: # print help information and exit: - print str(err) # will print something like "option -a not recognized" + print(str(err)) # will print something like "option -a not recognized" usage() sys.exit(2) for o, a in opts: if o in ("-h", "--help"): - usage(sys.stdout) + usage() sys.exit() if o in ("-d", "--debug"): global do_debug @@ -541,7 +541,7 @@ def main(): else: uri = "qemu:///system" - print "Using uri:" + uri + print("Using uri:" + uri) # Run a background thread with the event loop if use_pure_python_event_loop: @@ -554,7 +554,7 @@ def main(): # Close connection on exit (to test cleanup paths) old_exitfunc = getattr(sys, 'exitfunc', None) def exit(): - print "Closing " + str(vc) + print("Closing " + vc.getURI()) vc.close() if (old_exitfunc): old_exitfunc() sys.exitfunc = exit diff --git a/examples/topology.py b/examples/topology.py index 62effe3..191669c 100755 --- a/examples/topology.py +++ b/examples/topology.py @@ -13,13 +13,13 @@ from xml.dom import minidom try: conn = libvirt.openReadOnly(None) except libvirt.libvirtError: - print 'Failed to connect to the hypervisor' + print('Failed to connect to the hypervisor') sys.exit(1) try: capsXML = conn.getCapabilities() except libvirt.libvirtError: - print 'Failed to request capabilities' + print('Failed to request capabilities') sys.exit(1) caps = minidom.parseString(capsXML) @@ -38,8 +38,8 @@ siblingsIds = [ proc.getAttribute('siblings') for proc in cells.getElementsByTagName('cpu') if proc.getAttribute('siblings') not in siblingsIds ] -print "Host topology" -print "NUMA nodes:", cells.getAttribute('num') -print " Sockets:", len(set(socketIds)) -print " Cores:", len(set(siblingsIds)) -print " Threads:", total_cpus +print("Host topology") +print("NUMA nodes:", cells.getAttribute('num')) +print(" Sockets:", len(set(socketIds))) +print(" Cores:", len(set(siblingsIds))) +print(" Threads:", total_cpus) -- 1.8.3.1

On Tue, Dec 3, 2013 at 10:36 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
The 'print' method must be called as a function in python3, ie with brackets.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- examples/consolecallback.py | 6 ++-- examples/dominfo.py | 14 +++++----- examples/domrestore.py | 17 ++++++------ examples/domsave.py | 15 +++++----- examples/domstart.py | 19 ++++++------- examples/esxlist.py | 14 +++++----- examples/event-test.py | 68 ++++++++++++++++++++++----------------------- examples/topology.py | 14 +++++----- 8 files changed, 82 insertions(+), 85 deletions(-)
diff --git a/examples/consolecallback.py b/examples/consolecallback.py index d8e33a9..c539a92 100644 --- a/examples/consolecallback.py +++ b/examples/consolecallback.py @@ -62,14 +62,14 @@ def lifecycle_callback (connection, domain, event, detail, console):
# main if len(sys.argv) != 3: - print "Usage:", sys.argv[0], "URI UUID" - print "for example:", sys.argv[0], "'qemu:///system' '32ad945f-7e78-c33a-e96d-39f25e025d81'" + print("Usage:", sys.argv[0], "URI UUID") + print("for example:", sys.argv[0], "'qemu:///system' '32ad945f-7e78-c33a-e96d-39f25e025d81'") sys.exit(1)
uri = sys.argv[1] uuid = sys.argv[2]
-print "Escape character is ^]" +print("Escape character is ^]") logging.basicConfig(filename='msg.log', level=logging.DEBUG) logging.info("URI: %s", uri) logging.info("UUID: %s", uuid) diff --git a/examples/dominfo.py b/examples/dominfo.py index bfa3ca3..d3049cd 100755 --- a/examples/dominfo.py +++ b/examples/dominfo.py @@ -8,15 +8,15 @@ import libxml2 import pdb
def usage(): - print 'Usage: %s DOMAIN' % sys.argv[0] - print ' Print information about the domain DOMAIN' + print('Usage: %s DOMAIN' % sys.argv[0]) + print(' Print information about the domain DOMAIN')
def print_section(title): - print "\n%s" % title - print "=" * 60 + print("\n%s" % title) + print("=" * 60)
def print_entry(key, value): - print "%-10s %-10s" % (key, value) + print("%-10s %-10s" % (key, value))
def print_xml(key, ctx, path): res = ctx.xpathEval(path) @@ -36,14 +36,14 @@ name = sys.argv[1] # Connect to libvirt conn = libvirt.openReadOnly(None) if conn is None: - print 'Failed to open connection to the hypervisor' + print('Failed to open connection to the hypervisor') sys.exit(1)
try: dom = conn.lookupByName(name) # Annoyiingly, libvirt prints its own error message here except libvirt.libvirtError: - print "Domain %s is not running" % name + print("Domain %s is not running" % name) sys.exit(0)
info = dom.info() diff --git a/examples/domrestore.py b/examples/domrestore.py index fffc90f..06fdfbc 100755 --- a/examples/domrestore.py +++ b/examples/domrestore.py @@ -8,10 +8,10 @@ import libxml2 import pdb
def usage(): - print 'Usage: %s DIR' % sys.argv[0] - print ' Restore all the domains contained in DIR' - print ' It is assumed that all files in DIR are' - print ' images of domU\'s previously created with save' + print('Usage: %s DIR' % sys.argv[0]) + print(' Restore all the domains contained in DIR') + print(' It is assumed that all files in DIR are') + print(' images of domU\'s previously created with save')
if len(sys.argv) != 2: usage() @@ -22,15 +22,14 @@ imgs = os.listdir(dir)
conn = libvirt.open(None) if conn is None: - print 'Failed to open connection to the hypervisor' + print('Failed to open connection to the hypervisor') sys.exit(1)
for img in imgs: file = os.path.join(dir, img) - print "Restoring %s ... " % img, - sys.stdout.flush() + print("Restoring %s ... " % img) ret = conn.restore(file) if ret == 0: - print "done" + print("done") else: - print "error %d" % ret + print("error %d" % ret) diff --git a/examples/domsave.py b/examples/domsave.py index bac4536..727217c 100755 --- a/examples/domsave.py +++ b/examples/domsave.py @@ -8,9 +8,9 @@ import libxml2 import pdb
def usage(): - print 'Usage: %s DIR' % sys.argv[0] - print ' Save all currently running domU\'s into DIR' - print ' DIR must exist and be writable by this process' + print('Usage: %s DIR' % sys.argv[0]) + print(' Save all currently running domU\'s into DIR') + print(' DIR must exist and be writable by this process')
if len(sys.argv) != 2: usage() @@ -20,7 +20,7 @@ dir = sys.argv[1]
conn = libvirt.open(None) if conn is None: - print 'Failed to open connection to the hypervisor' + print('Failed to open connection to the hypervisor') sys.exit(1)
doms = conn.listDomainsID() @@ -28,13 +28,12 @@ for id in doms: if id == 0: continue dom = conn.lookupByID(id) - print "Saving %s[%d] ... " % (dom.name(), id), - sys.stdout.flush() + print("Saving %s[%d] ... " % (dom.name(), id)) path = os.path.join(dir, dom.name()) ret = dom.save(path) if ret == 0: - print "done" + print("done") else: - print "error %d" % ret + print("error %d" % ret)
#pdb.set_trace() diff --git a/examples/domstart.py b/examples/domstart.py index b14fad1..ce1b60c 100755 --- a/examples/domstart.py +++ b/examples/domstart.py @@ -20,11 +20,11 @@ def read_domain(fname): return (name, xmldesc)
def usage(): - print 'Usage: %s domain.xml' % sys.argv[0] - print ' Check that the domain described by DOMAIN.XML is running' - print ' If the domain is not running, create it' - print ' DOMAIN.XML must be a XML description of the domain' - print ' in libvirt\'s XML format' + print('Usage: %s domain.xml' % sys.argv[0]) + print(' Check that the domain described by DOMAIN.XML is running') + print(' If the domain is not running, create it') + print(' DOMAIN.XML must be a XML description of the domain') + print(' in libvirt\'s XML format')
if len(sys.argv) != 2: usage() @@ -34,17 +34,16 @@ if len(sys.argv) != 2:
conn = libvirt.open(None) if conn is None: - print 'Failed to open connection to the hypervisor' + print('Failed to open connection to the hypervisor') sys.exit(1)
try: dom = conn.lookupByName(name) except libvirt.libvirtError: - print "Starting domain %s ... " % name, - sys.stdout.flush() + print("Starting domain %s ... " % name) dom = conn.createLinux(xmldesc, 0) if dom is None: - print "failed" + print("failed") sys.exit(1) else: - print "done" + print("done") diff --git a/examples/esxlist.py b/examples/esxlist.py index c55424f..0d47b00 100755 --- a/examples/esxlist.py +++ b/examples/esxlist.py @@ -10,8 +10,8 @@ import getpass
def usage(): - print "Usage: %s HOSTNAME" % sys.argv[0] - print " List active domains of HOSTNAME and print some info" + print("Usage: %s HOSTNAME" % sys.argv[0]) + print(" List active domains of HOSTNAME and print some info")
# This is the callback method passed to libvirt.openAuth() (see below). @@ -51,12 +51,12 @@ def request_credentials(credentials, user_data):
def print_section(title): - print "\n%s" % title - print "=" * 60 + print("\n%s" % title) + print("=" * 60)
def print_entry(key, value): - print "%-10s %-10s" % (key, value) + print("%-10s %-10s" % (key, value))
def print_xml(key, ctx, path): @@ -100,7 +100,7 @@ auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_NOECHOPROMPT], conn = libvirt.openAuth(uri, auth, 0)
if conn is None: - print "Failed to open connection to %s" % hostname + print("Failed to open connection to %s" % hostname) sys.exit(1)
state_names = { libvirt.VIR_DOMAIN_RUNNING : "running", @@ -136,7 +136,7 @@ for id in conn.listDomainsID(): ctx.setContextNode(d)
if not first: - print "------------------------------------------------------------" + print("------------------------------------------------------------") else: first = False
diff --git a/examples/event-test.py b/examples/event-test.py index 84f5259..cf1a8b8 100644 --- a/examples/event-test.py +++ b/examples/event-test.py @@ -30,7 +30,7 @@ do_debug = False def debug(msg): global do_debug if do_debug: - print msg + print(msg)
# # This general purpose event loop will support waiting for file handle @@ -456,50 +456,50 @@ def detailToString(event, detail): return eventStrings[event][detail]
def myDomainEventCallback1 (conn, dom, event, detail, opaque): - print "myDomainEventCallback1 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(), + print("myDomainEventCallback1 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(), eventToString(event), - detailToString(event, detail)) + detailToString(event, detail)))
def myDomainEventCallback2 (conn, dom, event, detail, opaque): - print "myDomainEventCallback2 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(), + print("myDomainEventCallback2 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(), eventToString(event), - detailToString(event, detail)) + detailToString(event, detail)))
def myDomainEventRebootCallback(conn, dom, opaque): - print "myDomainEventRebootCallback: Domain %s(%s)" % (dom.name(), dom.ID()) + print("myDomainEventRebootCallback: Domain %s(%s)" % (dom.name(), dom.ID()))
def myDomainEventRTCChangeCallback(conn, dom, utcoffset, opaque): - print "myDomainEventRTCChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), utcoffset) + print("myDomainEventRTCChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), utcoffset))
def myDomainEventWatchdogCallback(conn, dom, action, opaque): - print "myDomainEventWatchdogCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), action) + print("myDomainEventWatchdogCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), action))
def myDomainEventIOErrorCallback(conn, dom, srcpath, devalias, action, opaque): - print "myDomainEventIOErrorCallback: Domain %s(%s) %s %s %d" % (dom.name(), dom.ID(), srcpath, devalias, action) + print("myDomainEventIOErrorCallback: Domain %s(%s) %s %s %d" % (dom.name(), dom.ID(), srcpath, devalias, action))
def myDomainEventGraphicsCallback(conn, dom, phase, localAddr, remoteAddr, authScheme, subject, opaque): - print "myDomainEventGraphicsCallback: Domain %s(%s) %d %s" % (dom.name(), dom.ID(), phase, authScheme) + print("myDomainEventGraphicsCallback: Domain %s(%s) %d %s" % (dom.name(), dom.ID(), phase, authScheme))
def myDomainEventDiskChangeCallback(conn, dom, oldSrcPath, newSrcPath, devAlias, reason, opaque): - print "myDomainEventDiskChangeCallback: Domain %s(%s) disk change oldSrcPath: %s newSrcPath: %s devAlias: %s reason: %s" % ( - dom.name(), dom.ID(), oldSrcPath, newSrcPath, devAlias, reason) + print("myDomainEventDiskChangeCallback: Domain %s(%s) disk change oldSrcPath: %s newSrcPath: %s devAlias: %s reason: %s" % ( + dom.name(), dom.ID(), oldSrcPath, newSrcPath, devAlias, reason)) def myDomainEventTrayChangeCallback(conn, dom, devAlias, reason, opaque): - print "myDomainEventTrayChangeCallback: Domain %s(%s) tray change devAlias: %s reason: %s" % ( - dom.name(), dom.ID(), devAlias, reason) + print("myDomainEventTrayChangeCallback: Domain %s(%s) tray change devAlias: %s reason: %s" % ( + dom.name(), dom.ID(), devAlias, reason)) def myDomainEventPMWakeupCallback(conn, dom, reason, opaque): - print "myDomainEventPMWakeupCallback: Domain %s(%s) system pmwakeup" % ( - dom.name(), dom.ID()) + print("myDomainEventPMWakeupCallback: Domain %s(%s) system pmwakeup" % ( + dom.name(), dom.ID())) def myDomainEventPMSuspendCallback(conn, dom, reason, opaque): - print "myDomainEventPMSuspendCallback: Domain %s(%s) system pmsuspend" % ( - dom.name(), dom.ID()) + print("myDomainEventPMSuspendCallback: Domain %s(%s) system pmsuspend" % ( + dom.name(), dom.ID())) def myDomainEventBalloonChangeCallback(conn, dom, actual, opaque): - print "myDomainEventBalloonChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), actual) + print("myDomainEventBalloonChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), actual)) def myDomainEventPMSuspendDiskCallback(conn, dom, reason, opaque): - print "myDomainEventPMSuspendDiskCallback: Domain %s(%s) system pmsuspend_disk" % ( - dom.name(), dom.ID()) + print("myDomainEventPMSuspendDiskCallback: Domain %s(%s) system pmsuspend_disk" % ( + dom.name(), dom.ID())) def myDomainEventDeviceRemovedCallback(conn, dom, dev, opaque): - print "myDomainEventDeviceRemovedCallback: Domain %s(%s) device removed: %s" % ( - dom.name(), dom.ID(), dev) + print("myDomainEventDeviceRemovedCallback: Domain %s(%s) device removed: %s" % ( + dom.name(), dom.ID(), dev))
run = True
@@ -507,27 +507,27 @@ def myConnectionCloseCallback(conn, reason, opaque): reasonStrings = ( "Error", "End-of-file", "Keepalive", "Client", ) - print "myConnectionCloseCallback: %s: %s" % (conn.getURI(), reasonStrings[reason]) + print("myConnectionCloseCallback: %s: %s" % (conn.getURI(), reasonStrings[reason])) run = False
-def usage(out=sys.stderr): - print >>out, "usage: "+os.path.basename(sys.argv[0])+" [-hdl] [uri]" - print >>out, " uri will default to qemu:///system" - print >>out, " --help, -h Print this help message" - print >>out, " --debug, -d Print debug output" - print >>out, " --loop, -l Toggle event-loop-implementation" +def usage(): + print("usage: "+os.path.basename(sys.argv[0])+" [-hdl] [uri]") + print(" uri will default to qemu:///system") + print(" --help, -h Print(this help message") + print(" --debug, -d Print(debug output") + print(" --loop, -l Toggle event-loop-implementation")
def main(): try: opts, args = getopt.getopt(sys.argv[1:], "hdl", ["help", "debug", "loop"]) except getopt.GetoptError, err: # print help information and exit: - print str(err) # will print something like "option -a not recognized" + print(str(err)) # will print something like "option -a not recognized" usage() sys.exit(2) for o, a in opts: if o in ("-h", "--help"): - usage(sys.stdout) + usage() sys.exit() if o in ("-d", "--debug"): global do_debug @@ -541,7 +541,7 @@ def main(): else: uri = "qemu:///system"
- print "Using uri:" + uri + print("Using uri:" + uri)
# Run a background thread with the event loop if use_pure_python_event_loop: @@ -554,7 +554,7 @@ def main(): # Close connection on exit (to test cleanup paths) old_exitfunc = getattr(sys, 'exitfunc', None) def exit(): - print "Closing " + str(vc) + print("Closing " + vc.getURI()) vc.close() if (old_exitfunc): old_exitfunc() sys.exitfunc = exit diff --git a/examples/topology.py b/examples/topology.py index 62effe3..191669c 100755 --- a/examples/topology.py +++ b/examples/topology.py @@ -13,13 +13,13 @@ from xml.dom import minidom try: conn = libvirt.openReadOnly(None) except libvirt.libvirtError: - print 'Failed to connect to the hypervisor' + print('Failed to connect to the hypervisor') sys.exit(1)
try: capsXML = conn.getCapabilities() except libvirt.libvirtError: - print 'Failed to request capabilities' + print('Failed to request capabilities') sys.exit(1)
caps = minidom.parseString(capsXML) @@ -38,8 +38,8 @@ siblingsIds = [ proc.getAttribute('siblings') for proc in cells.getElementsByTagName('cpu') if proc.getAttribute('siblings') not in siblingsIds ]
-print "Host topology" -print "NUMA nodes:", cells.getAttribute('num') -print " Sockets:", len(set(socketIds)) -print " Cores:", len(set(siblingsIds)) -print " Threads:", total_cpus +print("Host topology") +print("NUMA nodes:", cells.getAttribute('num')) +print(" Sockets:", len(set(socketIds))) +print(" Cores:", len(set(siblingsIds))) +print(" Threads:", total_cpus) -- 1.8.3.1
I stuck a from __future__ import print_function at the top of all the examples, which you may want to do as well. -- Doug Goldstein

From: "Daniel P. Berrange" <berrange@redhat.com> In Python3 you cannot use 'except Foo, e' you must use 'except Foo as e' instead, or just 'except Foo' if the variable isn't required. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- examples/event-test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/event-test.py b/examples/event-test.py index cf1a8b8..7fd9867 100644 --- a/examples/event-test.py +++ b/examples/event-test.py @@ -220,7 +220,7 @@ class virEventLoopPure: t.set_last_fired(now) t.dispatch() - except (os.error, select.error), e: + except (os.error, select.error) as e: if e.args[0] != errno.EINTR: raise finally: @@ -520,7 +520,7 @@ def usage(): def main(): try: opts, args = getopt.getopt(sys.argv[1:], "hdl", ["help", "debug", "loop"]) - except getopt.GetoptError, err: + except getopt.GetoptError as err: # print help information and exit: print(str(err)) # will print something like "option -a not recognized" usage() -- 1.8.3.1

From: "Daniel P. Berrange" <berrange@redhat.com> Strings in python3 default to unicode, so when writing to the self-pipe we must be sure to use bytes by calling the encode() method. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- examples/event-test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/event-test.py b/examples/event-test.py index 7fd9867..74c3634 100644 --- a/examples/event-test.py +++ b/examples/event-test.py @@ -236,7 +236,7 @@ class virEventLoopPure: def interrupt(self): if self.runningPoll and not self.pendingWakeup: self.pendingWakeup = True - os.write(self.pipetrick[1], 'c') + os.write(self.pipetrick[1], 'c'.encode("UTF-8")) # Registers a new file handle 'fd', monitoring for 'events' (libvirt -- 1.8.3.1

From: "Daniel P. Berrange" <berrange@redhat.com> In Python3 you cannot use 'except Foo, e' you must use 'except Foo as e' instead, or just 'except Foo' if the variable isn't required. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/generator.py b/generator.py index 17f00d6..6fe810f 100755 --- a/generator.py +++ b/generator.py @@ -1812,10 +1812,10 @@ def qemuBuildWrappers(module): fd.write("try:\n") fd.write(" import libvirtmod_qemu\n") - fd.write("except ImportError, lib_e:\n") + fd.write("except ImportError as lib_e:\n") fd.write(" try:\n") fd.write(" import cygvirtmod_qemu as libvirtmod_qemu\n") - fd.write(" except ImportError, cyg_e:\n") + fd.write(" except ImportError as cyg_e:\n") fd.write(" if str(cyg_e).count(\"No module named\"):\n") fd.write(" raise lib_e\n\n") @@ -1923,10 +1923,10 @@ def lxcBuildWrappers(module): fd.write("try:\n") fd.write(" import libvirtmod_lxc\n") - fd.write("except ImportError, lib_e:\n") + fd.write("except ImportError as lib_e:\n") fd.write(" try:\n") fd.write(" import cygvirtmod_lxc as libvirtmod_lxc\n") - fd.write(" except ImportError, cyg_e:\n") + fd.write(" except ImportError as cyg_e:\n") fd.write(" if str(cyg_e).count(\"No module named\"):\n") fd.write(" raise lib_e\n\n") -- 1.8.3.1

On Tue, Dec 3, 2013 at 10:36 AM, Daniel P. Berrange <berrange@redhat.com> wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
In Python3 you cannot use 'except Foo, e' you must use 'except Foo as e' instead, or just 'except Foo' if the variable isn't required.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/generator.py b/generator.py index 17f00d6..6fe810f 100755 --- a/generator.py +++ b/generator.py @@ -1812,10 +1812,10 @@ def qemuBuildWrappers(module):
fd.write("try:\n") fd.write(" import libvirtmod_qemu\n") - fd.write("except ImportError, lib_e:\n") + fd.write("except ImportError as lib_e:\n") fd.write(" try:\n") fd.write(" import cygvirtmod_qemu as libvirtmod_qemu\n") - fd.write(" except ImportError, cyg_e:\n") + fd.write(" except ImportError as cyg_e:\n") fd.write(" if str(cyg_e).count(\"No module named\"):\n") fd.write(" raise lib_e\n\n")
@@ -1923,10 +1923,10 @@ def lxcBuildWrappers(module):
fd.write("try:\n") fd.write(" import libvirtmod_lxc\n") - fd.write("except ImportError, lib_e:\n") + fd.write("except ImportError as lib_e:\n") fd.write(" try:\n") fd.write(" import cygvirtmod_lxc as libvirtmod_lxc\n") - fd.write(" except ImportError, cyg_e:\n") + fd.write(" except ImportError as cyg_e:\n") fd.write(" if str(cyg_e).count(\"No module named\"):\n") fd.write(" raise lib_e\n\n")
-- 1.8.3.1
We'll want to use sys.exc_info() for the exceptions here so older Python compat. -- Doug Goldstein

From: "Daniel P. Berrange" <berrange@redhat.com> The way native modules are registered has completely changed, so the code must be #ifdef'd for Python2 & 3 Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- libvirt-lxc-override.c | 73 +++++++++++++++++++++++++++++++++++------------ libvirt-override.c | 75 ++++++++++++++++++++++++++++++++++++------------- libvirt-qemu-override.c | 73 +++++++++++++++++++++++++++++++++++------------ 3 files changed, 166 insertions(+), 55 deletions(-) diff --git a/libvirt-lxc-override.c b/libvirt-lxc-override.c index 03b00b0..60b41d8 100644 --- a/libvirt-lxc-override.c +++ b/libvirt-lxc-override.c @@ -21,10 +21,18 @@ #include "libvirt-utils.h" #include "build/libvirt-lxc.h" -#ifndef __CYGWIN__ -extern void initlibvirtmod_lxc(void); +#if PY_MAJOR_VERSION > 2 +# ifndef __CYGWIN__ +extern PyObject *PyInit_libvirtmod_lxc(void); +# else +extern PyObject *PyInit_cygvirtmod_lxc(void); +# endif #else +# ifndef __CYGWIN__ +extern void initlibvirtmod_lxc(void); +# else extern void initcygvirtmod_lxc(void); +# endif #endif #if 0 @@ -110,30 +118,59 @@ static PyMethodDef libvirtLxcMethods[] = { {NULL, NULL, 0, NULL} }; +#if PY_MAJOR_VERSION > 2 +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, +# ifndef __CYGWIN__ + "libvirtmod_lxc", +# else + "cygvirtmod_lxc", +# endif + NULL, + -1, + libvirtLxcMethods, + NULL, + NULL, + NULL, + NULL +}; + +PyObject * +# ifndef __CYGWIN__ +PyInit_libvirtmod_lxc +# else +PyInit_cygvirtmod_lxc +# endif + (void) +{ + PyObject *module; + + if (virInitialize() < 0) + return NULL; + + module = PyModule_Create(&moduledef); + + return module; +} +#else /* ! PY_MAJOR_VERSION > 2 */ void -#ifndef __CYGWIN__ +# ifndef __CYGWIN__ initlibvirtmod_lxc -#else +# else initcygvirtmod_lxc -#endif +# endif (void) { - static int initialized = 0; - - if (initialized != 0) - return; - if (virInitialize() < 0) return; /* initialize the python extension module */ Py_InitModule((char *) -#ifndef __CYGWIN__ - "libvirtmod_lxc" -#else - "cygvirtmod_lxc" -#endif - , libvirtLxcMethods); - - initialized = 1; +# ifndef __CYGWIN__ + "libvirtmod_lxc", +# else + "cygvirtmod_lxc", +# endif + libvirtLxcMethods); } +#endif /* ! PY_MAJOR_VERSION > 2 */ diff --git a/libvirt-override.c b/libvirt-override.c index 847e6ce..aaee6b8 100644 --- a/libvirt-override.c +++ b/libvirt-override.c @@ -25,10 +25,18 @@ #include "build/libvirt.h" #include "libvirt-utils.h" -#ifndef __CYGWIN__ -extern void initlibvirtmod(void); +#if PY_MAJOR_VERSION > 2 +# ifndef __CYGWIN__ +extern PyObject *PyInit_libvirtmod(void); +# else +extern PyObject *PyInit_cygvirtmod(void); +# endif #else +# ifndef __CYGWIN__ +extern void initlibvirtmod(void); +# else extern void initcygvirtmod(void); +# endif #endif #if 0 @@ -7453,30 +7461,59 @@ static PyMethodDef libvirtMethods[] = { {NULL, NULL, 0, NULL} }; +#if PY_MAJOR_VERSION > 2 +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, +# ifndef __CYGWIN__ + "libvirtmod", +# else + "cygvirtmod", +# endif + NULL, + -1, + libvirtMethods, + NULL, + NULL, + NULL, + NULL +}; + +PyObject * +# ifndef __CYGWIN__ +PyInit_libvirtmod +# else +PyInit_cygvirtmod +# endif + (void) +{ + PyObject *module; + + if (virInitialize() < 0) + return NULL; + + module = PyModule_Create(&moduledef); + + return module; +} +#else /* ! PY_MAJOR_VERSION > 2 */ void -#ifndef __CYGWIN__ +# ifndef __CYGWIN__ initlibvirtmod -#else +# else initcygvirtmod -#endif +# endif (void) { - static int initialized = 0; - - if (initialized != 0) - return; - if (virInitialize() < 0) return; /* initialize the python extension module */ Py_InitModule((char *) -#ifndef __CYGWIN__ - "libvirtmod" -#else - "cygvirtmod" -#endif - , libvirtMethods); - - initialized = 1; -} +# ifndef __CYGWIN__ + "libvirtmod", +# else + "cygvirtmod", +# endif + libvirtMethods); +} +#endif /* ! PY_MAJOR_VERSION > 2 */ diff --git a/libvirt-qemu-override.c b/libvirt-qemu-override.c index a8e8c09..72257ac 100644 --- a/libvirt-qemu-override.c +++ b/libvirt-qemu-override.c @@ -21,10 +21,18 @@ #include "libvirt-utils.h" #include "build/libvirt-qemu.h" -#ifndef __CYGWIN__ -extern void initlibvirtmod_qemu(void); +#if PY_MAJOR_VERSION > 2 +# ifndef __CYGWIN__ +extern PyObject *PyInit_libvirtmod_qemu(void); +# else +extern PyObject *PyInit_cygvirtmod_qemu(void); +# endif #else +# ifndef __CYGWIN__ +extern void initlibvirtmod_qemu(void); +# else extern void initcygvirtmod_qemu(void); +# endif #endif #if 0 @@ -128,30 +136,59 @@ static PyMethodDef libvirtQemuMethods[] = { {NULL, NULL, 0, NULL} }; +#if PY_MAJOR_VERSION > 2 +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, +# ifndef __CYGWIN__ + "libvirtmod_qemu", +# else + "cygvirtmod_qemu", +# endif + NULL, + -1, + libvirtQemuMethods, + NULL, + NULL, + NULL, + NULL +}; + +PyObject * +# ifndef __CYGWIN__ +PyInit_libvirtmod_qemu +# else +PyInit_cygvirtmod_qemu +# endif + (void) +{ + PyObject *module; + + if (virInitialize() < 0) + return NULL; + + module = PyModule_Create(&moduledef); + + return module; +} +#else /* ! PY_MAJOR_VERSION > 2 */ void -#ifndef __CYGWIN__ +# ifndef __CYGWIN__ initlibvirtmod_qemu -#else +# else initcygvirtmod_qemu -#endif +# endif (void) { - static int initialized = 0; - - if (initialized != 0) - return; - if (virInitialize() < 0) return; /* initialize the python extension module */ Py_InitModule((char *) -#ifndef __CYGWIN__ - "libvirtmod_qemu" -#else - "cygvirtmod_qemu" -#endif - , libvirtQemuMethods); - - initialized = 1; +# ifndef __CYGWIN__ + "libvirtmod_qemu", +# else + "cygvirtmod_qemu", +# endif + libvirtQemuMethods); } +#endif /* ! PY_MAJOR_VERSION > 2 */ -- 1.8.3.1

From: "Daniel P. Berrange" <berrange@redhat.com> In Python3 you cannot use 'except Foo, e' you must use 'except Foo as e' instead, or just 'except Foo' if the variable isn't required. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- libvirt-override-virStream.py | 6 +++--- libvirt-override.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libvirt-override-virStream.py b/libvirt-override-virStream.py index 53000da..cf54a01 100644 --- a/libvirt-override-virStream.py +++ b/libvirt-override-virStream.py @@ -50,12 +50,12 @@ ret = handler(self, got, opaque) if type(ret) is int and ret < 0: raise RuntimeError("recvAll handler returned %d" % ret) - except Exception, e: + except: try: self.abort() except: pass - raise e + raise def sendAll(self, handler, opaque): """ @@ -79,7 +79,7 @@ self.abort() except: pass - raise e + raise if got == "": break diff --git a/libvirt-override.py b/libvirt-override.py index 87996f8..e356a10 100644 --- a/libvirt-override.py +++ b/libvirt-override.py @@ -5,10 +5,10 @@ # On cygwin, the DLL is called cygvirtmod.dll try: import libvirtmod -except ImportError, lib_e: +except ImportError as lib_e: try: import cygvirtmod as libvirtmod - except ImportError, cyg_e: + except ImportError as cyg_e: if str(cyg_e).count("No module named"): raise lib_e -- 1.8.3.1
participants (3)
-
Daniel P. Berrange
-
Doug Goldstein
-
Eric Blake