From f10994ac586be987bf546faf7cdc6bd19b0335e9 Mon Sep 17 00:00:00 2001 From: Justin Hendrick Date: Tue, 25 Mar 2025 21:26:49 -0700 Subject: [PATCH] chore!: Migrate to Python 3 BREAKING CHANGE: drop support for Python 2 Signed-off-by: Justin Hendrick --- Makefile | 2 +- configure | 5 +- pebble.py | 2 +- scripts/acpi_extract.py | 6 +- scripts/analyse-9p-simpletrace.py | 88 ++++++++-------- scripts/analyze-migration.py | 20 ++-- scripts/dump-guest-memory.py | 10 +- scripts/ordereddict.py | 128 ----------------------- scripts/png_to_cstruct.py | 24 ++--- scripts/qapi.py | 50 +++++---- scripts/qemugdb/mtree.py | 10 +- scripts/qmp/qmp.py | 12 +-- scripts/simpletrace.py | 4 +- scripts/tracetool.py | 4 +- scripts/tracetool/__init__.py | 14 +-- scripts/vmstate-static-checker.py | 88 ++++++++-------- tests/Makefile | 2 +- tests/image-fuzzer/qcow2/__init__.py | 2 +- tests/image-fuzzer/qcow2/fuzz.py | 3 +- tests/image-fuzzer/qcow2/layout.py | 8 +- tests/image-fuzzer/runner.py | 63 +++++------ tests/qapi-schema/test-qapi.py | 26 ++--- tests/qemu-iotests/iotests.py | 17 ++- tests/qemu-iotests/nbd-fault-injector.py | 10 +- tests/qemu-iotests/qcow2.py | 38 +++---- tests/qemu-iotests/qed.py | 28 ++--- 26 files changed, 263 insertions(+), 401 deletions(-) delete mode 100644 scripts/ordereddict.py diff --git a/Makefile b/Makefile index ccc8970ed2d..8f58cc350ea 100644 --- a/Makefile +++ b/Makefile @@ -250,7 +250,7 @@ qemu-ga$(EXESUF): QEMU_CFLAGS += -I qga/qapi-generated gen-out-type = $(subst .,-,$(suffix $@)) -qapi-py = $(SRC_PATH)/scripts/qapi.py $(SRC_PATH)/scripts/ordereddict.py +qapi-py = $(SRC_PATH)/scripts/qapi.py qga/qapi-generated/qga-qapi-types.c qga/qapi-generated/qga-qapi-types.h :\ $(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-types.py $(qapi-py) diff --git a/configure b/configure index bf1a2406b4a..d6cf9e75568 100755 --- a/configure +++ b/configure @@ -1186,9 +1186,8 @@ fi # Note that if the Python conditional here evaluates True we will exit # with status 1 which is a shell 'false' value. -if ! $python -c 'import sys; sys.exit(sys.version_info < (2,6) or sys.version_info >= (3,))'; then - error_exit "Cannot use '$python', Python 2.6 or later is required." \ - "Note that Python 3 or later is not yet supported." \ +if ! $python -c 'import sys; sys.exit(sys.version_info < (3,8))'; then + error_exit "Cannot use '$python', Python 3.8 or later is required." \ "Use --python=/path/to/python to specify a supported Python." fi diff --git a/pebble.py b/pebble.py index b5760a996d4..5062a1f27b9 100755 --- a/pebble.py +++ b/pebble.py @@ -54,7 +54,7 @@ if args.vnc_ws: cmd_line += "-vnc :1,websocket=4444 " - print "Executing command line: \n ", cmd_line + print("Executing command line: \n ", cmd_line) os.system(cmd_line) diff --git a/scripts/acpi_extract.py b/scripts/acpi_extract.py index 10c1ffb368c..81bbe895728 100755 --- a/scripts/acpi_extract.py +++ b/scripts/acpi_extract.py @@ -252,12 +252,12 @@ def aml_package_start(offset): lineno = lineno + 1 debug = "input line %d: %s" % (lineno, line) #ASL listing: space, then line#, then ...., then code - pasl = re.compile('^\s+([0-9]+)(:\s\s|\.\.\.\.)\s*') + pasl = re.compile(r'^\s+([0-9]+)(:\s\s|\.\.\.\.)\s*') m = pasl.search(line) if (m): add_asl(lineno, pasl.sub("", line)); # AML listing: offset in hex, then ...., then code - paml = re.compile('^([0-9A-Fa-f]+)(:\s\s|\.\.\.\.)\s*') + paml = re.compile(r'^([0-9A-Fa-f]+)(:\s\s|\.\.\.\.)\s*') m = paml.search(line) if (m): add_aml(m.group(1), paml.sub("", line)) @@ -357,7 +357,7 @@ def get_value_type(maxvalue): return "char" # Pretty print output -for array in output.keys(): +for array in list(output.keys()): otype = get_value_type(max(output[array])) odata = [] for value in output[array]: diff --git a/scripts/analyse-9p-simpletrace.py b/scripts/analyse-9p-simpletrace.py index 3c3dee43377..437d6a6a529 100755 --- a/scripts/analyse-9p-simpletrace.py +++ b/scripts/analyse-9p-simpletrace.py @@ -79,135 +79,135 @@ class VirtFSRequestTracker(simpletrace.Analyzer): def begin(self): - print "Pretty printing 9p simpletrace log ..." + print("Pretty printing 9p simpletrace log ...") def v9fs_rerror(self, tag, id, err): - print "RERROR (tag =", tag, ", id =", symbol_9p[id], ", err = \"", os.strerror(err), "\")" + print("RERROR (tag =", tag, ", id =", symbol_9p[id], ", err = \"", os.strerror(err), "\")") def v9fs_version(self, tag, id, msize, version): - print "TVERSION (tag =", tag, ", msize =", msize, ", version =", version, ")" + print("TVERSION (tag =", tag, ", msize =", msize, ", version =", version, ")") def v9fs_version_return(self, tag, id, msize, version): - print "RVERSION (tag =", tag, ", msize =", msize, ", version =", version, ")" + print("RVERSION (tag =", tag, ", msize =", msize, ", version =", version, ")") def v9fs_attach(self, tag, id, fid, afid, uname, aname): - print "TATTACH (tag =", tag, ", fid =", fid, ", afid =", afid, ", uname =", uname, ", aname =", aname, ")" + print("TATTACH (tag =", tag, ", fid =", fid, ", afid =", afid, ", uname =", uname, ", aname =", aname, ")") def v9fs_attach_return(self, tag, id, type, version, path): - print "RATTACH (tag =", tag, ", qid={type =", type, ", version =", version, ", path =", path, "})" + print("RATTACH (tag =", tag, ", qid={type =", type, ", version =", version, ", path =", path, "})") def v9fs_stat(self, tag, id, fid): - print "TSTAT (tag =", tag, ", fid =", fid, ")" + print("TSTAT (tag =", tag, ", fid =", fid, ")") def v9fs_stat_return(self, tag, id, mode, atime, mtime, length): - print "RSTAT (tag =", tag, ", mode =", mode, ", atime =", atime, ", mtime =", mtime, ", length =", length, ")" + print("RSTAT (tag =", tag, ", mode =", mode, ", atime =", atime, ", mtime =", mtime, ", length =", length, ")") def v9fs_getattr(self, tag, id, fid, request_mask): - print "TGETATTR (tag =", tag, ", fid =", fid, ", request_mask =", hex(request_mask), ")" + print("TGETATTR (tag =", tag, ", fid =", fid, ", request_mask =", hex(request_mask), ")") def v9fs_getattr_return(self, tag, id, result_mask, mode, uid, gid): - print "RGETATTR (tag =", tag, ", result_mask =", hex(result_mask), ", mode =", oct(mode), ", uid =", uid, ", gid =", gid, ")" + print("RGETATTR (tag =", tag, ", result_mask =", hex(result_mask), ", mode =", oct(mode), ", uid =", uid, ", gid =", gid, ")") def v9fs_walk(self, tag, id, fid, newfid, nwnames): - print "TWALK (tag =", tag, ", fid =", fid, ", newfid =", newfid, ", nwnames =", nwnames, ")" + print("TWALK (tag =", tag, ", fid =", fid, ", newfid =", newfid, ", nwnames =", nwnames, ")") def v9fs_walk_return(self, tag, id, nwnames, qids): - print "RWALK (tag =", tag, ", nwnames =", nwnames, ", qids =", hex(qids), ")" + print("RWALK (tag =", tag, ", nwnames =", nwnames, ", qids =", hex(qids), ")") def v9fs_open(self, tag, id, fid, mode): - print "TOPEN (tag =", tag, ", fid =", fid, ", mode =", oct(mode), ")" + print("TOPEN (tag =", tag, ", fid =", fid, ", mode =", oct(mode), ")") def v9fs_open_return(self, tag, id, type, version, path, iounit): - print "ROPEN (tag =", tag, ", qid={type =", type, ", version =", version, ", path =", path, "}, iounit =", iounit, ")" + print("ROPEN (tag =", tag, ", qid={type =", type, ", version =", version, ", path =", path, "}, iounit =", iounit, ")") def v9fs_lcreate(self, tag, id, dfid, flags, mode, gid): - print "TLCREATE (tag =", tag, ", dfid =", dfid, ", flags =", oct(flags), ", mode =", oct(mode), ", gid =", gid, ")" + print("TLCREATE (tag =", tag, ", dfid =", dfid, ", flags =", oct(flags), ", mode =", oct(mode), ", gid =", gid, ")") def v9fs_lcreate_return(self, tag, id, type, version, path, iounit): - print "RLCREATE (tag =", tag, ", qid={type =", type, ", version =", version, ", path =", path, "}, iounit =", iounit, ")" + print("RLCREATE (tag =", tag, ", qid={type =", type, ", version =", version, ", path =", path, "}, iounit =", iounit, ")") def v9fs_fsync(self, tag, id, fid, datasync): - print "TFSYNC (tag =", tag, ", fid =", fid, ", datasync =", datasync, ")" + print("TFSYNC (tag =", tag, ", fid =", fid, ", datasync =", datasync, ")") def v9fs_clunk(self, tag, id, fid): - print "TCLUNK (tag =", tag, ", fid =", fid, ")" + print("TCLUNK (tag =", tag, ", fid =", fid, ")") def v9fs_read(self, tag, id, fid, off, max_count): - print "TREAD (tag =", tag, ", fid =", fid, ", off =", off, ", max_count =", max_count, ")" + print("TREAD (tag =", tag, ", fid =", fid, ", off =", off, ", max_count =", max_count, ")") def v9fs_read_return(self, tag, id, count, err): - print "RREAD (tag =", tag, ", count =", count, ", err =", err, ")" + print("RREAD (tag =", tag, ", count =", count, ", err =", err, ")") def v9fs_readdir(self, tag, id, fid, offset, max_count): - print "TREADDIR (tag =", tag, ", fid =", fid, ", offset =", offset, ", max_count =", max_count, ")" + print("TREADDIR (tag =", tag, ", fid =", fid, ", offset =", offset, ", max_count =", max_count, ")") def v9fs_readdir_return(self, tag, id, count, retval): - print "RREADDIR (tag =", tag, ", count =", count, ", retval =", retval, ")" + print("RREADDIR (tag =", tag, ", count =", count, ", retval =", retval, ")") def v9fs_write(self, tag, id, fid, off, count, cnt): - print "TWRITE (tag =", tag, ", fid =", fid, ", off =", off, ", count =", count, ", cnt =", cnt, ")" + print("TWRITE (tag =", tag, ", fid =", fid, ", off =", off, ", count =", count, ", cnt =", cnt, ")") def v9fs_write_return(self, tag, id, total, err): - print "RWRITE (tag =", tag, ", total =", total, ", err =", err, ")" + print("RWRITE (tag =", tag, ", total =", total, ", err =", err, ")") def v9fs_create(self, tag, id, fid, name, perm, mode): - print "TCREATE (tag =", tag, ", fid =", fid, ", perm =", oct(perm), ", name =", name, ", mode =", oct(mode), ")" + print("TCREATE (tag =", tag, ", fid =", fid, ", perm =", oct(perm), ", name =", name, ", mode =", oct(mode), ")") def v9fs_create_return(self, tag, id, type, version, path, iounit): - print "RCREATE (tag =", tag, ", qid={type =", type, ", version =", version, ", path =", path, "}, iounit =", iounit, ")" + print("RCREATE (tag =", tag, ", qid={type =", type, ", version =", version, ", path =", path, "}, iounit =", iounit, ")") def v9fs_symlink(self, tag, id, fid, name, symname, gid): - print "TSYMLINK (tag =", tag, ", fid =", fid, ", name =", name, ", symname =", symname, ", gid =", gid, ")" + print("TSYMLINK (tag =", tag, ", fid =", fid, ", name =", name, ", symname =", symname, ", gid =", gid, ")") def v9fs_symlink_return(self, tag, id, type, version, path): - print "RSYMLINK (tag =", tag, ", qid={type =", type, ", version =", version, ", path =", path, "})" + print("RSYMLINK (tag =", tag, ", qid={type =", type, ", version =", version, ", path =", path, "})") def v9fs_flush(self, tag, id, flush_tag): - print "TFLUSH (tag =", tag, ", flush_tag =", flush_tag, ")" + print("TFLUSH (tag =", tag, ", flush_tag =", flush_tag, ")") def v9fs_link(self, tag, id, dfid, oldfid, name): - print "TLINK (tag =", tag, ", dfid =", dfid, ", oldfid =", oldfid, ", name =", name, ")" + print("TLINK (tag =", tag, ", dfid =", dfid, ", oldfid =", oldfid, ", name =", name, ")") def v9fs_remove(self, tag, id, fid): - print "TREMOVE (tag =", tag, ", fid =", fid, ")" + print("TREMOVE (tag =", tag, ", fid =", fid, ")") def v9fs_wstat(self, tag, id, fid, mode, atime, mtime): - print "TWSTAT (tag =", tag, ", fid =", fid, ", mode =", oct(mode), ", atime =", atime, "mtime =", mtime, ")" + print("TWSTAT (tag =", tag, ", fid =", fid, ", mode =", oct(mode), ", atime =", atime, "mtime =", mtime, ")") def v9fs_mknod(self, tag, id, fid, mode, major, minor): - print "TMKNOD (tag =", tag, ", fid =", fid, ", mode =", oct(mode), ", major =", major, ", minor =", minor, ")" + print("TMKNOD (tag =", tag, ", fid =", fid, ", mode =", oct(mode), ", major =", major, ", minor =", minor, ")") def v9fs_lock(self, tag, id, fid, type, start, length): - print "TLOCK (tag =", tag, ", fid =", fid, "type =", type, ", start =", start, ", length =", length, ")" + print("TLOCK (tag =", tag, ", fid =", fid, "type =", type, ", start =", start, ", length =", length, ")") def v9fs_lock_return(self, tag, id, status): - print "RLOCK (tag =", tag, ", status =", status, ")" + print("RLOCK (tag =", tag, ", status =", status, ")") def v9fs_getlock(self, tag, id, fid, type, start, length): - print "TGETLOCK (tag =", tag, ", fid =", fid, "type =", type, ", start =", start, ", length =", length, ")" + print("TGETLOCK (tag =", tag, ", fid =", fid, "type =", type, ", start =", start, ", length =", length, ")") def v9fs_getlock_return(self, tag, id, type, start, length, proc_id): - print "RGETLOCK (tag =", tag, "type =", type, ", start =", start, ", length =", length, ", proc_id =", proc_id, ")" + print("RGETLOCK (tag =", tag, "type =", type, ", start =", start, ", length =", length, ", proc_id =", proc_id, ")") def v9fs_mkdir(self, tag, id, fid, name, mode, gid): - print "TMKDIR (tag =", tag, ", fid =", fid, ", name =", name, ", mode =", mode, ", gid =", gid, ")" + print("TMKDIR (tag =", tag, ", fid =", fid, ", name =", name, ", mode =", mode, ", gid =", gid, ")") def v9fs_mkdir_return(self, tag, id, type, version, path, err): - print "RMKDIR (tag =", tag, ", qid={type =", type, ", version =", version, ", path =", path, "}, err =", err, ")" + print("RMKDIR (tag =", tag, ", qid={type =", type, ", version =", version, ", path =", path, "}, err =", err, ")") def v9fs_xattrwalk(self, tag, id, fid, newfid, name): - print "TXATTRWALK (tag =", tag, ", fid =", fid, ", newfid =", newfid, ", xattr name =", name, ")" + print("TXATTRWALK (tag =", tag, ", fid =", fid, ", newfid =", newfid, ", xattr name =", name, ")") def v9fs_xattrwalk_return(self, tag, id, size): - print "RXATTRWALK (tag =", tag, ", xattrsize =", size, ")" + print("RXATTRWALK (tag =", tag, ", xattrsize =", size, ")") def v9fs_xattrcreate(self, tag, id, fid, name, size, flags): - print "TXATTRCREATE (tag =", tag, ", fid =", fid, ", name =", name, ", xattrsize =", size, ", flags =", flags, ")" + print("TXATTRCREATE (tag =", tag, ", fid =", fid, ", name =", name, ", xattrsize =", size, ", flags =", flags, ")") def v9fs_readlink(self, tag, id, fid): - print "TREADLINK (tag =", tag, ", fid =", fid, ")" + print("TREADLINK (tag =", tag, ", fid =", fid, ")") def v9fs_readlink_return(self, tag, id, target): - print "RREADLINK (tag =", tag, ", target =", target, ")" + print("RREADLINK (tag =", tag, ", target =", target, ")") simpletrace.run(VirtFSRequestTracker()) diff --git a/scripts/analyze-migration.py b/scripts/analyze-migration.py index 14553876a28..e8d00f4a4db 100755 --- a/scripts/analyze-migration.py +++ b/scripts/analyze-migration.py @@ -86,8 +86,8 @@ def read_migration_debug_json(self): # Find the last NULL byte, then the first brace after that. This should # be the beginning of our JSON data. - nulpos = data.rfind("\0") - jsonpos = data.find("{", nulpos) + nulpos = data.rfind(b"\0") + jsonpos = data.find(b"{", nulpos) # Check backwards from there and see whether we guessed right self.file.seek(datapos + jsonpos - 5, 0) @@ -162,7 +162,7 @@ def read(self): len = self.file.read64() self.sizeinfo[self.name] = '0x%016x' % len if self.write_memory: - print self.name + print(self.name) mkdir_p('./' + os.path.dirname(self.name)) f = open('./' + self.name, "wb") f.truncate(0) @@ -359,7 +359,7 @@ def __init__(self, desc, file): array_len = field.pop('array_len') field['index'] = 0 new_fields.append(field) - for i in xrange(1, array_len): + for i in range(1, array_len): c = field.copy() c['index'] = i new_fields.append(c) @@ -426,7 +426,7 @@ def getDictArray(self, array): def getDictOrderedDict(self, dict): r = collections.OrderedDict() - for (key, value) in dict.items(): + for (key, value) in list(dict.items()): r[key] = self.getDictItem(value) return r @@ -558,7 +558,7 @@ def load_vmsd_json(self, file): def getDict(self): r = collections.OrderedDict() - for (key, value) in self.sections.items(): + for (key, value) in list(self.sections.items()): key = "%s (%d)" % ( value.section_key[0], key ) r[key] = value.getDict() return r @@ -584,7 +584,7 @@ def default(self, o): dump = MigrationDump(args.file) dump.read(desc_only = True) - print "desc.json" + print("desc.json") f = open("desc.json", "wb") f.truncate() f.write(jsonenc.encode(dump.vmsd_desc)) @@ -592,7 +592,7 @@ def default(self, o): dump.read(write_memory = True) dict = dump.getDict() - print "state.json" + print("state.json") f = open("state.json", "wb") f.truncate() f.write(jsonenc.encode(dict)) @@ -601,10 +601,10 @@ def default(self, o): dump = MigrationDump(args.file) dump.read(dump_memory = args.memory) dict = dump.getDict() - print jsonenc.encode(dict) + print(jsonenc.encode(dict)) elif args.dump == "desc": dump = MigrationDump(args.file) dump.read(desc_only = True) - print jsonenc.encode(dump.vmsd_desc) + print(jsonenc.encode(dump.vmsd_desc)) else: raise Exception("Please specify either -x, -d state or -d dump") diff --git a/scripts/dump-guest-memory.py b/scripts/dump-guest-memory.py index 08796fff8cf..a2dadcb9dea 100644 --- a/scripts/dump-guest-memory.py +++ b/scripts/dump-guest-memory.py @@ -136,7 +136,7 @@ def guest_phys_blocks_init(self): self.guest_phys_blocks = [] def guest_phys_blocks_append(self): - print "guest RAM blocks:" + print("guest RAM blocks:") print ("target_start target_end host_addr message " "count") print ("---------------- ---------------- ---------------- ------- " @@ -187,9 +187,9 @@ def guest_phys_blocks_append(self): predecessor["target_end"] = target_end message = "joined" - print ("%016x %016x %016x %-7s %5u" % + print(("%016x %016x %016x %-7s %5u" % (target_start, target_end, host_addr.cast(self.uintptr_t), - message, len(self.guest_phys_blocks))) + message, len(self.guest_phys_blocks)))) def cpu_get_dump_info(self): # We can't synchronize the registers with KVM post-mortem, and @@ -309,8 +309,8 @@ def dump_iterate(self, vmcore): for block in self.guest_phys_blocks: cur = block["host_addr"] left = block["target_end"] - block["target_start"] - print ("dumping range at %016x for length %016x" % - (cur.cast(self.uintptr_t), left)) + print(("dumping range at %016x for length %016x" % + (cur.cast(self.uintptr_t), left))) while (left > 0): chunk_size = min(self.TARGET_PAGE_SIZE, left) chunk = qemu_core.read_memory(cur, chunk_size) diff --git a/scripts/ordereddict.py b/scripts/ordereddict.py deleted file mode 100644 index 2d1d81370bb..00000000000 --- a/scripts/ordereddict.py +++ /dev/null @@ -1,128 +0,0 @@ -# Copyright (c) 2009 Raymond Hettinger -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation files -# (the "Software"), to deal in the Software without restriction, -# including without limitation the rights to use, copy, modify, merge, -# publish, distribute, sublicense, and/or sell copies of the Software, -# and to permit persons to whom the Software is furnished to do so, -# subject to the following conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. - -from UserDict import DictMixin - - -class OrderedDict(dict, DictMixin): - - def __init__(self, *args, **kwds): - if len(args) > 1: - raise TypeError('expected at most 1 arguments, got %d' % len(args)) - try: - self.__end - except AttributeError: - self.clear() - self.update(*args, **kwds) - - def clear(self): - self.__end = end = [] - end += [None, end, end] # sentinel node for doubly linked list - self.__map = {} # key --> [key, prev, next] - dict.clear(self) - - def __setitem__(self, key, value): - if key not in self: - end = self.__end - curr = end[1] - curr[2] = end[1] = self.__map[key] = [key, curr, end] - dict.__setitem__(self, key, value) - - def __delitem__(self, key): - dict.__delitem__(self, key) - key, prev, next = self.__map.pop(key) - prev[2] = next - next[1] = prev - - def __iter__(self): - end = self.__end - curr = end[2] - while curr is not end: - yield curr[0] - curr = curr[2] - - def __reversed__(self): - end = self.__end - curr = end[1] - while curr is not end: - yield curr[0] - curr = curr[1] - - def popitem(self, last=True): - if not self: - raise KeyError('dictionary is empty') - if last: - key = reversed(self).next() - else: - key = iter(self).next() - value = self.pop(key) - return key, value - - def __reduce__(self): - items = [[k, self[k]] for k in self] - tmp = self.__map, self.__end - del self.__map, self.__end - inst_dict = vars(self).copy() - self.__map, self.__end = tmp - if inst_dict: - return (self.__class__, (items,), inst_dict) - return self.__class__, (items,) - - def keys(self): - return list(self) - - setdefault = DictMixin.setdefault - update = DictMixin.update - pop = DictMixin.pop - values = DictMixin.values - items = DictMixin.items - iterkeys = DictMixin.iterkeys - itervalues = DictMixin.itervalues - iteritems = DictMixin.iteritems - - def __repr__(self): - if not self: - return '%s()' % (self.__class__.__name__,) - return '%s(%r)' % (self.__class__.__name__, self.items()) - - def copy(self): - return self.__class__(self) - - @classmethod - def fromkeys(cls, iterable, value=None): - d = cls() - for key in iterable: - d[key] = value - return d - - def __eq__(self, other): - if isinstance(other, OrderedDict): - if len(self) != len(other): - return False - for p, q in zip(self.items(), other.items()): - if p != q: - return False - return True - return dict.__eq__(self, other) - - def __ne__(self, other): - return not self == other diff --git a/scripts/png_to_cstruct.py b/scripts/png_to_cstruct.py index 362ec9fd065..b81dd4e88e2 100755 --- a/scripts/png_to_cstruct.py +++ b/scripts/png_to_cstruct.py @@ -49,23 +49,23 @@ def png_to_bytes(png_reader, converter=to_rgb222): def png_to_cstruct(png_reader, dst_name, var_name): height, width, image = png_to_bytes(png_reader) - print "// C struct format image converted from '%s' using png_to_cstruct.py" % args.src.name - print "static uint8_t *get_%s_image(int *width, int *height) {\n" % (var_name) - print " *width = %d;" % (width) - print " *height = %d;" % (height) - print " static uint8_t %s[%d] = {\n" % (var_name, width*height) + print("// C struct format image converted from '%s' using png_to_cstruct.py" % args.src.name) + print("static uint8_t *get_%s_image(int *width, int *height) {\n" % (var_name)) + print(" *width = %d;" % (width)) + print(" *height = %d;" % (height)) + print(" static uint8_t %s[%d] = {\n" % (var_name, width*height)) for line in image: - print " ", + print(" ", end=' ') for i, pixel in enumerate(line): if (i > 0) and (i % 12) == 0: - print "\n ", - print " 0x%02x," % pixel, - print "\n" + print("\n ", end=' ') + print(" 0x%02x," % pixel, end=' ') + print("\n") - print " };" - print " return %s;" % (var_name) - print "}" + print(" };") + print(" return %s;" % (var_name)) + print("}") if __name__=='__main__': diff --git a/scripts/qapi.py b/scripts/qapi.py index 7c50cc4c876..800458afa06 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -12,12 +12,11 @@ # See the COPYING file in the top-level directory. import re -from ordereddict import OrderedDict +from collections import OrderedDict import errno import getopt import os import sys -import string builtin_types = { 'str': 'QTYPE_QSTRING', @@ -152,7 +151,7 @@ def __init__(self, fp, previously_included=[], incl_info=None): continue try: fobj = open(incl_abs_fname, 'r') - except IOError, e: + except IOError as e: raise QAPIExprError(expr_info, '%s: %s' % (e.strerror, include)) exprs_include = QAPISchemaParser(fobj, previously_included, @@ -487,7 +486,7 @@ def check_type(expr_info, source, value, allow_array=False, "%s should be a dictionary or type name" % source) # value is a dictionary, check that each member is okay - for (key, arg) in value.items(): + for (key, arg) in list(value.items()): check_name(expr_info, "Member of %s" % source, key, allow_optional=allow_optional) if c_name(key, False) == 'u' or c_name(key, False).startswith('has_'): @@ -506,7 +505,7 @@ def check_member_clash(expr_info, base_name, data, source=""): base = find_struct(base_name) assert base base_members = base['data'] - for key in data.keys(): + for key in list(data.keys()): if key.startswith('*'): key = key[1:] if key in base_members or "*" + key in base_members: @@ -593,7 +592,7 @@ def check_union(expr, expr_info): "type" % discriminator) # Check every branch - for (key, value) in members.items(): + for (key, value) in list(members.items()): check_name(expr_info, "Member of union '%s'" % name, key) # Each value must name a known type; furthermore, in flat unions, @@ -638,7 +637,7 @@ def check_alternate(expr, expr_info): types_seen = {} # Check every branch - for (key, value) in members.items(): + for (key, value) in list(members.items()): check_name(expr_info, "Member of alternate '%s'" % name, key) # Check for conflicts in the generated enum @@ -706,7 +705,7 @@ def check_keys(expr_elem, meta, required, optional=[]): raise QAPIExprError(info, "'%s' key must have a string value" % meta) required = required + [meta] - for (key, value) in expr.items(): + for (key, value) in list(expr.items()): if key not in required and key not in optional: raise QAPIExprError(info, "Unknown key '%s' in %s '%s'" @@ -726,7 +725,7 @@ def check_exprs(exprs): global all_names # Learn the types and check for valid expression keys - for builtin in builtin_types.keys(): + for builtin in list(builtin_types.keys()): all_names[builtin] = 'built-in' for expr_elem in exprs: expr = expr_elem['expr'] @@ -1148,8 +1147,8 @@ def __init__(self, fname): self._predefining = False self._def_exprs() self.check() - except (QAPISchemaError, QAPIExprError), err: - print >>sys.stderr, err + except (QAPISchemaError, QAPIExprError) as err: + print(err, file=sys.stderr) exit(1) def _def_entity(self, ent): @@ -1235,7 +1234,7 @@ def _make_member(self, name, typ, info): def _make_members(self, data, info): return [self._make_member(key, value, info) - for (key, value) in data.iteritems()] + for (key, value) in data.items()] def _def_struct_type(self, expr, info): name = expr['struct'] @@ -1269,10 +1268,10 @@ def _def_union_type(self, expr, info): tag_member = None if tag_name: variants = [self._make_variant(key, value) - for (key, value) in data.iteritems()] + for (key, value) in data.items()] else: variants = [self._make_simple_variant(key, value, info) - for (key, value) in data.iteritems()] + for (key, value) in data.items()] tag_member = self._make_implicit_tag(name, info, variants) self._def_entity( QAPISchemaObjectType(name, info, base, @@ -1285,7 +1284,7 @@ def _def_alternate_type(self, expr, info): name = expr['alternate'] data = expr['data'] variants = [self._make_variant(key, value) - for (key, value) in data.iteritems()] + for (key, value) in data.items()] tag_member = self._make_implicit_tag(name, info, variants) self._def_entity( QAPISchemaAlternateType(name, info, @@ -1336,7 +1335,7 @@ def _def_exprs(self): assert False def check(self): - for ent in self._entity_dict.values(): + for ent in list(self._entity_dict.values()): ent.check(self) def visit(self, visitor): @@ -1392,7 +1391,7 @@ def c_enum_const(type_name, const_name, prefix=None): type_name = prefix return camel_to_upper(type_name + '_' + const_name) -c_name_trans = string.maketrans('.-', '__') +c_name_trans = str.maketrans('.-', '__') # Map @name to a valid C identifier. @@ -1639,8 +1638,8 @@ def parse_command_line(extra_options="", extra_long_options=[]): "chp:o:" + extra_options, ["source", "header", "prefix=", "output-dir="] + extra_long_options) - except getopt.GetoptError, err: - print >>sys.stderr, "%s: %s" % (sys.argv[0], str(err)) + except getopt.GetoptError as err: + print("%s: %s" % (sys.argv[0], str(err)), file=sys.stderr) sys.exit(1) output_dir = "" @@ -1654,9 +1653,8 @@ def parse_command_line(extra_options="", extra_long_options=[]): if o in ("-p", "--prefix"): match = re.match('([A-Za-z_.-][A-Za-z0-9_.-]*)?', a) if match.end() != len(a): - print >>sys.stderr, \ - "%s: 'funny character '%s' in argument of --prefix" \ - % (sys.argv[0], a[match.end()]) + print("%s: 'funny character '%s' in argument of --prefix" \ + % (sys.argv[0], a[match.end()]), file=sys.stderr) sys.exit(1) prefix = a elif o in ("-o", "--output-dir"): @@ -1673,7 +1671,7 @@ def parse_command_line(extra_options="", extra_long_options=[]): do_h = True if len(args) != 1: - print >>sys.stderr, "%s: need exactly one argument" % sys.argv[0] + print("%s: need exactly one argument" % sys.argv[0], file=sys.stderr) sys.exit(1) fname = args[0] @@ -1693,7 +1691,7 @@ def open_output(output_dir, do_c, do_h, prefix, c_file, h_file, if output_dir: try: os.makedirs(output_dir) - except os.error, e: + except os.error as e: if e.errno != errno.EEXIST: raise @@ -1701,8 +1699,8 @@ def maybe_open(really, name, opt): if really: return open(name, opt) else: - import StringIO - return StringIO.StringIO() + import io + return io.StringIO() fdef = maybe_open(do_c, c_file, 'w') fdecl = maybe_open(do_h, h_file, 'w') diff --git a/scripts/qemugdb/mtree.py b/scripts/qemugdb/mtree.py index 06011c30ccc..cc8131c2e75 100644 --- a/scripts/qemugdb/mtree.py +++ b/scripts/qemugdb/mtree.py @@ -21,7 +21,7 @@ def isnull(ptr): return ptr == gdb.Value(0).cast(ptr.type) def int128(p): - return long(p['lo']) + (long(p['hi']) << 64) + return int(p['lo']) + (int(p['hi']) << 64) class MtreeCommand(gdb.Command): '''Display the memory tree hierarchy''' @@ -40,11 +40,11 @@ def queue_root(self, varname): def process_queue(self): while self.queue: ptr = self.queue.pop(0) - if long(ptr) in self.seen: + if int(ptr) in self.seen: continue self.print_item(ptr) def print_item(self, ptr, offset = gdb.Value(0), level = 0): - self.seen.add(long(ptr)) + self.seen.add(int(ptr)) addr = ptr['addr'] addr += offset size = int128(ptr['size']) @@ -58,8 +58,8 @@ def print_item(self, ptr, offset = gdb.Value(0), level = 0): klass = ' (RAM)' gdb.write('%s%016x-%016x %s%s (@ %s)\n' % (' ' * level, - long(addr), - long(addr + (size - 1)), + int(addr), + int(addr + (size - 1)), ptr['name'].string(), klass, ptr, diff --git a/scripts/qmp/qmp.py b/scripts/qmp/qmp.py index 1d38e3e9e78..ddfdfc8b138 100644 --- a/scripts/qmp/qmp.py +++ b/scripts/qmp/qmp.py @@ -53,7 +53,7 @@ def __get_sock(self): def __negotiate_capabilities(self): greeting = self.__json_read() - if greeting is None or not greeting.has_key('QMP'): + if greeting is None or 'QMP' not in greeting: raise QMPConnectError # Greeting seems ok, negotiate capabilities resp = self.cmd('qmp_capabilities') @@ -92,8 +92,8 @@ def __get_events(self, wait=False): self.__sock.setblocking(0) try: self.__json_read() - except socket.error, err: - if err[0] == errno.EAGAIN: + except socket.error as err: + if err.args[0] == errno.EAGAIN: # No data available pass self.__sock.setblocking(1) @@ -150,8 +150,8 @@ def cmd_obj(self, qmp_cmd): """ try: self.__sock.sendall(json.dumps(qmp_cmd)) - except socket.error, err: - if err[0] == errno.EPIPE: + except socket.error as err: + if err.args[0] == errno.EPIPE: return raise socket.error(err) return self.__json_read() @@ -173,7 +173,7 @@ def cmd(self, name, args=None, id=None): def command(self, cmd, **kwds): ret = self.cmd(cmd, kwds) - if ret.has_key('error'): + if 'error' in ret: raise Exception(ret['error']['desc']) return ret['return'] diff --git a/scripts/simpletrace.py b/scripts/simpletrace.py index 3916c6d14ae..c71b5b0e121 100755 --- a/scripts/simpletrace.py +++ b/scripts/simpletrace.py @@ -129,7 +129,7 @@ def build_fn(analyzer, event): return analyzer.catchall event_argcount = len(event.args) - fn_argcount = len(inspect.getargspec(fn)[0]) - 1 + fn_argcount = len(inspect.getfullargspec(fn)[0]) - 1 if fn_argcount == event_argcount + 1: # Include timestamp as first argument return lambda _, rec: fn(*((rec[1:2],) + rec[3:3 + event_argcount])) @@ -190,6 +190,6 @@ def catchall(self, event, rec): else: fields.append('%s=0x%x' % (name, rec[i])) i += 1 - print ' '.join(fields) + print(' '.join(fields)) run(Formatter()) diff --git a/scripts/tracetool.py b/scripts/tracetool.py index 83bde7bda90..7b82959e84a 100755 --- a/scripts/tracetool.py +++ b/scripts/tracetool.py @@ -71,7 +71,7 @@ def main(args): try: opts, args = getopt.getopt(args[1:], "", long_opts) - except getopt.GetoptError, err: + except getopt.GetoptError as err: error_opt(str(err)) check_backends = False @@ -132,7 +132,7 @@ def main(args): try: tracetool.generate(sys.stdin, arg_format, arg_backends, binary=binary, probe_prefix=probe_prefix) - except tracetool.TracetoolError, e: + except tracetool.TracetoolError as e: error_opt(str(e)) if __name__ == "__main__": diff --git a/scripts/tracetool/__init__.py b/scripts/tracetool/__init__.py index 181675f00e8..f0410d7cc81 100644 --- a/scripts/tracetool/__init__.py +++ b/scripts/tracetool/__init__.py @@ -139,12 +139,12 @@ class Event(object): """ - _CRE = re.compile("((?P[\w\s]+)\s+)?" - "(?P\w+)" - "\((?P[^)]*)\)" - "\s*" - "(?:(?:(?P\".+),)?\s*(?P\".+))?" - "\s*") + _CRE = re.compile(r"((?P[\w\s]+)\s+)?" + r"(?P\w+)" + r"\((?P[^)]*)\)" + r"\s*" + r"(?:(?:(?P\".+),)?\s*(?P\".+))?" + r"\s*") _VALID_PROPS = set(["disable", "tcg", "tcg-trans", "tcg-exec"]) @@ -228,7 +228,7 @@ def __repr__(self): self.args, fmt) - _FMT = re.compile("(%[\d\.]*\w+|%.*PRI\S+)") + _FMT = re.compile(r"(%[\d\.]*\w+|%.*PRI\S+)") def formats(self): """List of argument print formats.""" diff --git a/scripts/vmstate-static-checker.py b/scripts/vmstate-static-checker.py index b6c0bbead9e..de54c403737 100755 --- a/scripts/vmstate-static-checker.py +++ b/scripts/vmstate-static-checker.py @@ -151,7 +151,7 @@ def check_fields(src_fields, dest_fields, desc, sec): while True: if advance_src: try: - s_item = s_iter.next() + s_item = next(s_iter) except StopIteration: if s_iter_list == []: break @@ -166,14 +166,14 @@ def check_fields(src_fields, dest_fields, desc, sec): if advance_dest: try: - d_item = d_iter.next() + d_item = next(d_iter) except StopIteration: if d_iter_list == []: # We were not in a substruct - print "Section \"" + sec + "\",", - print "Description " + "\"" + desc + "\":", - print "expected field \"" + s_item["field"] + "\",", - print "while dest has no further fields" + print("Section \"" + sec + "\",", end=' ') + print("Description " + "\"" + desc + "\":", end=' ') + print("expected field \"" + s_item["field"] + "\",", end=' ') + print("while dest has no further fields") bump_taint() break @@ -191,10 +191,10 @@ def check_fields(src_fields, dest_fields, desc, sec): advance_dest = True continue if unused_count < 0: - print "Section \"" + sec + "\",", - print "Description \"" + desc + "\":", - print "unused size mismatch near \"", - print s_item["field"] + "\"" + print("Section \"" + sec + "\",", end=' ') + print("Description \"" + desc + "\":", end=' ') + print("unused size mismatch near \"", end=' ') + print(s_item["field"] + "\"") bump_taint() break continue @@ -205,10 +205,10 @@ def check_fields(src_fields, dest_fields, desc, sec): advance_src = True continue if unused_count < 0: - print "Section \"" + sec + "\",", - print "Description \"" + desc + "\":", - print "unused size mismatch near \"", - print d_item["field"] + "\"" + print("Section \"" + sec + "\",", end=' ') + print("Description \"" + desc + "\":", end=' ') + print("unused size mismatch near \"", end=' ') + print(d_item["field"] + "\"") bump_taint() break continue @@ -256,10 +256,10 @@ def check_fields(src_fields, dest_fields, desc, sec): unused_count = s_item["size"] - d_item["size"] continue - print "Section \"" + sec + "\",", - print "Description \"" + desc + "\":", - print "expected field \"" + s_item["field"] + "\",", - print "got \"" + d_item["field"] + "\"; skipping rest" + print("Section \"" + sec + "\",", end=' ') + print("Description \"" + desc + "\":", end=' ') + print("expected field \"" + s_item["field"] + "\",", end=' ') + print("got \"" + d_item["field"] + "\"; skipping rest") bump_taint() break @@ -283,8 +283,8 @@ def check_subsections(src_sub, dest_sub, desc, sec): check_descriptions(s_item, d_item, sec) if not found: - print "Section \"" + sec + "\", Description \"" + desc + "\":", - print "Subsection \"" + s_item["name"] + "\" not found" + print("Section \"" + sec + "\", Description \"" + desc + "\":", end=' ') + print("Subsection \"" + s_item["name"] + "\" not found") bump_taint() @@ -293,8 +293,8 @@ def check_description_in_list(s_item, d_item, sec, desc): return if not "Description" in d_item: - print "Section \"" + sec + "\", Description \"" + desc + "\",", - print "Field \"" + s_item["field"] + "\": missing description" + print("Section \"" + sec + "\", Description \"" + desc + "\",", end=' ') + print("Field \"" + s_item["field"] + "\": missing description") bump_taint() return @@ -305,17 +305,17 @@ def check_descriptions(src_desc, dest_desc, sec): check_version(src_desc, dest_desc, sec, src_desc["name"]) if not check_fields_match(sec, src_desc["name"], dest_desc["name"]): - print "Section \"" + sec + "\":", - print "Description \"" + src_desc["name"] + "\"", - print "missing, got \"" + dest_desc["name"] + "\" instead; skipping" + print("Section \"" + sec + "\":", end=' ') + print("Description \"" + src_desc["name"] + "\"", end=' ') + print("missing, got \"" + dest_desc["name"] + "\" instead; skipping") bump_taint() return for f in src_desc: if not f in dest_desc: - print "Section \"" + sec + "\"", - print "Description \"" + src_desc["name"] + "\":", - print "Entry \"" + f + "\" missing" + print("Section \"" + sec + "\"", end=' ') + print("Description \"" + src_desc["name"] + "\":", end=' ') + print("Entry \"" + f + "\" missing") bump_taint() continue @@ -328,39 +328,39 @@ def check_descriptions(src_desc, dest_desc, sec): def check_version(s, d, sec, desc=None): if s["version_id"] > d["version_id"]: - print "Section \"" + sec + "\"", + print("Section \"" + sec + "\"", end=' ') if desc: - print "Description \"" + desc + "\":", - print "version error:", s["version_id"], ">", d["version_id"] + print("Description \"" + desc + "\":", end=' ') + print("version error:", s["version_id"], ">", d["version_id"]) bump_taint() if not "minimum_version_id" in d: return if s["version_id"] < d["minimum_version_id"]: - print "Section \"" + sec + "\"", + print("Section \"" + sec + "\"", end=' ') if desc: - print "Description \"" + desc + "\":", - print "minimum version error:", s["version_id"], "<", - print d["minimum_version_id"] + print("Description \"" + desc + "\":", end=' ') + print("minimum version error:", s["version_id"], "<", end=' ') + print(d["minimum_version_id"]) bump_taint() def check_size(s, d, sec, desc=None, field=None): if s["size"] != d["size"]: - print "Section \"" + sec + "\"", + print("Section \"" + sec + "\"", end=' ') if desc: - print "Description \"" + desc + "\"", + print("Description \"" + desc + "\"", end=' ') if field: - print "Field \"" + field + "\"", - print "size mismatch:", s["size"], ",", d["size"] + print("Field \"" + field + "\"", end=' ') + print("size mismatch:", s["size"], ",", d["size"]) bump_taint() def check_machine_type(s, d): if s["Name"] != d["Name"]: - print "Warning: checking incompatible machine types:", - print "\"" + s["Name"] + "\", \"" + d["Name"] + "\"" + print("Warning: checking incompatible machine types:", end=' ') + print("\"" + s["Name"] + "\", \"" + d["Name"] + "\"") return @@ -394,7 +394,7 @@ def main(): # doesn't exist in dest. dest_sec = get_changed_sec_name(sec) if not dest_sec in dest_data: - print "Section \"" + sec + "\" does not exist in dest" + print("Section \"" + sec + "\" does not exist in dest") bump_taint() continue @@ -409,8 +409,8 @@ def main(): for entry in s: if not entry in d: - print "Section \"" + sec + "\": Entry \"" + entry + "\"", - print "missing" + print("Section \"" + sec + "\": Entry \"" + entry + "\"", end=' ') + print("missing") bump_taint() continue diff --git a/tests/Makefile b/tests/Makefile index 2e15eca3c5b..f29ef201818 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,6 +1,6 @@ export SRC_PATH -qapi-py = $(SRC_PATH)/scripts/qapi.py $(SRC_PATH)/scripts/ordereddict.py +qapi-py = $(SRC_PATH)/scripts/qapi.py # Get the list of all supported sysemu targets SYSEMU_TARGET_LIST := $(subst -softmmu.mak,,$(notdir \ diff --git a/tests/image-fuzzer/qcow2/__init__.py b/tests/image-fuzzer/qcow2/__init__.py index e2ebe193113..ed3af5da863 100644 --- a/tests/image-fuzzer/qcow2/__init__.py +++ b/tests/image-fuzzer/qcow2/__init__.py @@ -1 +1 @@ -from layout import create_image +from .layout import create_image diff --git a/tests/image-fuzzer/qcow2/fuzz.py b/tests/image-fuzzer/qcow2/fuzz.py index 20eba6bc1bb..dcba6f8bae5 100644 --- a/tests/image-fuzzer/qcow2/fuzz.py +++ b/tests/image-fuzzer/qcow2/fuzz.py @@ -17,6 +17,7 @@ # import random +from functools import reduce UINT8 = 0xff UINT16 = 0xffff @@ -66,7 +67,7 @@ def random_bits(bit_ranges): bit_numbers = [] # Select random amount of random positions in bit_ranges for rng in bit_ranges: - bit_numbers += random.sample(range(rng[0], rng[1] + 1), + bit_numbers += random.sample(list(range(rng[0], rng[1] + 1)), random.randint(0, rng[1] - rng[0] + 1)) val = 0 # Set bits on selected positions to ones diff --git a/tests/image-fuzzer/qcow2/layout.py b/tests/image-fuzzer/qcow2/layout.py index 63e801f4e81..b27d5e654ac 100644 --- a/tests/image-fuzzer/qcow2/layout.py +++ b/tests/image-fuzzer/qcow2/layout.py @@ -18,7 +18,7 @@ import random import struct -import fuzz +from . import fuzz from math import ceil from os import urandom from itertools import chain @@ -297,8 +297,8 @@ def create_l1_entry(l2_cluster, l1_offset, guest): l2 = [] else: meta_data = self._get_metadata() - guest_clusters = random.sample(range(self.image_size / - self.cluster_size), + guest_clusters = random.sample(list(range(self.image_size / + self.cluster_size)), len(self.data_clusters)) # Number of entries in a L1/L2 table l_size = self.cluster_size / UINT64_S @@ -587,7 +587,7 @@ def _alloc_data(img_size, cluster_size): """Return a set of random indices of clusters allocated for guest data. """ num_of_cls = img_size/cluster_size - return set(random.sample(range(1, num_of_cls + 1), + return set(random.sample(list(range(1, num_of_cls + 1)), random.randint(0, num_of_cls))) def _get_metadata(self): diff --git a/tests/image-fuzzer/runner.py b/tests/image-fuzzer/runner.py index be7e283dd9e..758d5f4b36e 100755 --- a/tests/image-fuzzer/runner.py +++ b/tests/image-fuzzer/runner.py @@ -27,7 +27,7 @@ from itertools import count import time import getopt -import StringIO +import io import resource try: @@ -36,9 +36,8 @@ try: import simplejson as json except ImportError: - print >>sys.stderr, \ - "Warning: Module for JSON processing is not found.\n" \ - "'--config' and '--command' options are not supported." + print("Warning: Module for JSON processing is not found.\n" \ + "'--config' and '--command' options are not supported.", file=sys.stderr) # Backing file sizes in MB MAX_BACKING_FILE_SIZE = 10 @@ -56,7 +55,7 @@ def str_signal(sig): """ Convert a numeric value of a system signal to the string one defined by the current operational system. """ - for k, v in signal.__dict__.items(): + for k, v in list(signal.__dict__.items()): if v == sig: return k @@ -128,7 +127,7 @@ def __init__(self, test_id, seed, work_dir, run_log, if seed is not None: self.seed = seed else: - self.seed = str(random.randint(0, sys.maxint)) + self.seed = str(random.randint(0, sys.maxsize)) random.seed(self.seed) self.init_path = os.getcwd() @@ -157,10 +156,9 @@ def __init__(self, test_id, seed, work_dir, run_log, try: os.makedirs(self.current_dir) - except OSError, e: - print >>sys.stderr, \ - "Error: The working directory '%s' cannot be used. Reason: %s"\ - % (self.work_dir, e[1]) + except OSError as e: + print("Error: The working directory '%s' cannot be used. Reason: %s"\ + % (self.work_dir, e), file=sys.stderr) raise TestException self.log = open(os.path.join(self.current_dir, "test.log"), "w") self.parent_log = open(run_log, "a") @@ -184,7 +182,7 @@ def _create_backing_file(self): MAX_BACKING_FILE_SIZE) * (1 << 20) cmd = self.qemu_img + ['create', '-f', backing_file_fmt, backing_file_name, str(backing_file_size)] - temp_log = StringIO.StringIO() + temp_log = io.StringIO() retcode = run_app(temp_log, cmd) if retcode == 0: temp_log.close() @@ -241,13 +239,13 @@ def execute(self, input_commands=None, fuzz_config=None): "Backing file: %s\n" \ % (self.seed, " ".join(current_cmd), self.current_dir, backing_file_name) - temp_log = StringIO.StringIO() + temp_log = io.StringIO() try: retcode = run_app(temp_log, current_cmd) - except OSError, e: + except OSError as e: multilog("%sError: Start of '%s' failed. Reason: %s\n\n" % (test_summary, os.path.basename(current_cmd[0]), - e[1]), + e), sys.stderr, self.log, self.parent_log) raise TestException @@ -277,7 +275,7 @@ def finish(self): if __name__ == '__main__': def usage(): - print """ + print(""" Usage: runner.py [OPTION...] TEST_DIR IMG_GENERATOR Set up test environment in TEST_DIR and run a test in it. A module for @@ -326,7 +324,7 @@ def usage(): If '--config' argument is specified, fields not listed in the configuration array will not be fuzzed. - """ + """) def run_test(test_id, seed, work_dir, run_log, cleanup, log_all, command, fuzz_config): @@ -356,9 +354,8 @@ def should_continue(duration, start_time): opts, args = getopt.gnu_getopt(sys.argv[1:], 'c:hs:kvd:', ['command=', 'help', 'seed=', 'config=', 'keep_passed', 'verbose', 'duration=']) - except getopt.error, e: - print >>sys.stderr, \ - "Error: %s\n\nTry 'runner.py --help' for more information" % e + except getopt.error as e: + print("Error: %s\n\nTry 'runner.py --help' for more information" % e, file=sys.stderr) sys.exit(1) command = None @@ -374,10 +371,9 @@ def should_continue(duration, start_time): elif opt in ('-c', '--command'): try: command = json.loads(arg) - except (TypeError, ValueError, NameError), e: - print >>sys.stderr, \ - "Error: JSON array of test commands cannot be loaded.\n" \ - "Reason: %s" % e + except (TypeError, ValueError, NameError) as e: + print("Error: JSON array of test commands cannot be loaded.\n" \ + "Reason: %s" % e, file=sys.stderr) sys.exit(1) elif opt in ('-k', '--keep_passed'): cleanup = False @@ -390,16 +386,14 @@ def should_continue(duration, start_time): elif opt == '--config': try: config = json.loads(arg) - except (TypeError, ValueError, NameError), e: - print >>sys.stderr, \ - "Error: JSON array with the fuzzer configuration cannot" \ - " be loaded\nReason: %s" % e + except (TypeError, ValueError, NameError) as e: + print("Error: JSON array with the fuzzer configuration cannot" \ + " be loaded\nReason: %s" % e, file=sys.stderr) sys.exit(1) if not len(args) == 2: - print >>sys.stderr, \ - "Expected two parameters\nTry 'runner.py --help'" \ - " for more information." + print("Expected two parameters\nTry 'runner.py --help'" \ + " for more information.", file=sys.stderr) sys.exit(1) work_dir = os.path.realpath(args[0]) @@ -414,10 +408,9 @@ def should_continue(duration, start_time): try: image_generator = __import__(generator_name) - except ImportError, e: - print >>sys.stderr, \ - "Error: The image generator '%s' cannot be imported.\n" \ - "Reason: %s" % (generator_name, e) + except ImportError as e: + print("Error: The image generator '%s' cannot be imported.\n" \ + "Reason: %s" % (generator_name, e), file=sys.stderr) sys.exit(1) # Enable core dumps @@ -428,7 +421,7 @@ def should_continue(duration, start_time): test_id = count(1) while should_continue(duration, start_time): try: - run_test(str(test_id.next()), seed, work_dir, run_log, cleanup, + run_test(str(next(test_id)), seed, work_dir, run_log, cleanup, log_all, command, config) except (KeyboardInterrupt, SystemExit): sys.exit(1) diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py index 649677e017c..0e672ec505c 100644 --- a/tests/qapi-schema/test-qapi.py +++ b/tests/qapi-schema/test-qapi.py @@ -18,39 +18,39 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): def visit_enum_type(self, name, info, values, prefix): - print 'enum %s %s' % (name, values) + print('enum %s %s' % (name, values)) if prefix: - print ' prefix %s' % prefix + print(' prefix %s' % prefix) def visit_object_type(self, name, info, base, members, variants): - print 'object %s' % name + print('object %s' % name) if base: - print ' base %s' % base.name + print(' base %s' % base.name) for m in members: - print ' member %s: %s optional=%s' % \ - (m.name, m.type.name, m.optional) + print(' member %s: %s optional=%s' % \ + (m.name, m.type.name, m.optional)) self._print_variants(variants) def visit_alternate_type(self, name, info, variants): - print 'alternate %s' % name + print('alternate %s' % name) self._print_variants(variants) def visit_command(self, name, info, arg_type, ret_type, gen, success_response): - print 'command %s %s -> %s' % \ - (name, arg_type and arg_type.name, ret_type and ret_type.name) - print ' gen=%s success_response=%s' % (gen, success_response) + print('command %s %s -> %s' % \ + (name, arg_type and arg_type.name, ret_type and ret_type.name)) + print(' gen=%s success_response=%s' % (gen, success_response)) def visit_event(self, name, info, arg_type): - print 'event %s %s' % (name, arg_type and arg_type.name) + print('event %s %s' % (name, arg_type and arg_type.name)) @staticmethod def _print_variants(variants): if variants: if variants.tag_name: - print ' tag %s' % variants.tag_name + print(' tag %s' % variants.tag_name) for v in variants.variants: - print ' case %s: %s' % (v.name, v.type.name) + print(' case %s: %s' % (v.name, v.type.name)) schema = QAPISchema(sys.argv[1]) schema.visit(QAPISchemaTestVisitor()) diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py index e02245ed077..1bf293086b4 100644 --- a/tests/qemu-iotests/iotests.py +++ b/tests/qemu-iotests/iotests.py @@ -19,7 +19,6 @@ import os import re import subprocess -import string import unittest import sys sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'scripts')) @@ -93,7 +92,7 @@ def compare_images(img1, img2): def create_image(name, size): '''Create a fully-allocated raw image with sector markers''' - file = open(name, 'w') + file = open(name, 'wb') i = 0 while i < size: sector = struct.pack('>l504xl', i / 512, i / 512) @@ -198,7 +197,7 @@ def send_fd_scm(self, fd_file_path): assert self._qmp.is_scm_available() bin = socket_scm_helper if os.path.exists(bin) == False: - print "Scm help program does not present, path '%s'." % bin + print("Scm help program does not present, path '%s'." % bin) return -1 fd_param = ["%s" % bin, "%d" % self._qmp.get_sock_fd(), @@ -235,11 +234,11 @@ def shutdown(self): os.remove(self._qemu_log_path) self._popen = None - underscore_to_dash = string.maketrans('_', '-') + underscore_to_dash = str.maketrans('_', '-') def qmp(self, cmd, conv_keys=True, **args): '''Invoke a QMP command and return the result dict''' qmp_args = dict() - for k in args.keys(): + for k in list(args.keys()): if conv_keys: qmp_args[k.translate(self.underscore_to_dash)] = args[k] else: @@ -367,7 +366,7 @@ def wait_ready(self, drive='drive0'): def wait_ready_and_cancel(self, drive='drive0'): self.wait_ready(drive=drive) event = self.cancel_and_wait(drive=drive) - self.assertEquals(event['event'], 'BLOCK_JOB_COMPLETED') + self.assertEqual(event['event'], 'BLOCK_JOB_COMPLETED') self.assert_qmp(event, 'data/type', 'mirror') self.assert_qmp(event, 'data/offset', event['data']['len']) @@ -388,7 +387,7 @@ def notrun(reason): seq = os.path.basename(sys.argv[0]) open('%s/%s.notrun' % (output_dir, seq), 'wb').write(reason + '\n') - print '%s not run: %s' % (seq, reason) + print('%s not run: %s' % (seq, reason)) sys.exit(0) def main(supported_fmts=[], supported_oses=['linux']): @@ -404,13 +403,13 @@ def main(supported_fmts=[], supported_oses=['linux']): # We need to filter out the time taken from the output so that qemu-iotest # can reliably diff the results against master output. - import StringIO + import io if debug: output = sys.stdout verbosity = 2 sys.argv.remove('-d') else: - output = StringIO.StringIO() + output = io.StringIO() class MyTestRunner(unittest.TextTestRunner): def __init__(self, stream=output, descriptions=True, verbosity=verbosity): diff --git a/tests/qemu-iotests/nbd-fault-injector.py b/tests/qemu-iotests/nbd-fault-injector.py index 6c07191a5a2..cd248a80c7e 100755 --- a/tests/qemu-iotests/nbd-fault-injector.py +++ b/tests/qemu-iotests/nbd-fault-injector.py @@ -47,7 +47,7 @@ import socket import struct import collections -import ConfigParser +import configparser FAKE_DISK_SIZE = 8 * 1024 * 1024 * 1024 # 8 GB @@ -110,7 +110,7 @@ def check(self, event, io, bufsize=None): for rule in self.rules: if rule.match(event, io): if rule.when == 0 or bufsize is None: - print 'Closing connection on rule match %s' % rule.name + print('Closing connection on rule match %s' % rule.name) sys.exit(0) if rule.when != -1: return rule.when @@ -182,7 +182,7 @@ def handle_connection(conn, use_export): elif req.type == NBD_CMD_DISC: break else: - print 'unrecognized command type %#02x' % req.type + print('unrecognized command type %#02x' % req.type) break conn.close() @@ -223,7 +223,7 @@ def parse_config(config): return rules def load_rules(filename): - config = ConfigParser.RawConfigParser() + config = configparser.RawConfigParser() with open(filename, 'rt') as f: config.readfp(f, filename) return parse_config(config) @@ -239,7 +239,7 @@ def open_socket(path): sock = socket.socket(socket.AF_UNIX) sock.bind(path) sock.listen(0) - print 'Listening on %s' % path + print('Listening on %s' % path) return sock def usage(args): diff --git a/tests/qemu-iotests/qcow2.py b/tests/qemu-iotests/qcow2.py index 9cc4cf7d08d..4092863e756 100755 --- a/tests/qemu-iotests/qcow2.py +++ b/tests/qemu-iotests/qcow2.py @@ -129,8 +129,8 @@ def update(self, fd): def dump(self): for f in QcowHeader.fields: - print "%-25s" % f[2], f[1] % self.__dict__[f[2]] - print "" + print("%-25s" % f[2], f[1] % self.__dict__[f[2]]) + print("") def dump_extensions(self): for ex in self.extensions: @@ -141,11 +141,11 @@ def dump_extensions(self): else: data = "" - print "Header extension:" - print "%-25s %#x" % ("magic", ex.magic) - print "%-25s %d" % ("length", ex.length) - print "%-25s %s" % ("data", data) - print "" + print("Header extension:") + print("%-25s %#x" % ("magic", ex.magic)) + print("%-25s %d" % ("length", ex.length)) + print("%-25s %s" % ("data", data)) + print("") def cmd_dump_header(fd): @@ -157,12 +157,12 @@ def cmd_set_header(fd, name, value): try: value = int(value, 0) except: - print "'%s' is not a valid number" % value + print("'%s' is not a valid number" % value) sys.exit(1) fields = (field[2] for field in QcowHeader.fields) if not name in fields: - print "'%s' is not a known header field" % name + print("'%s' is not a known header field" % name) sys.exit(1) h = QcowHeader(fd) @@ -173,7 +173,7 @@ def cmd_add_header_ext(fd, magic, data): try: magic = int(magic, 0) except: - print "'%s' is not a valid magic number" % magic + print("'%s' is not a valid magic number" % magic) sys.exit(1) h = QcowHeader(fd) @@ -188,7 +188,7 @@ def cmd_del_header_ext(fd, magic): try: magic = int(magic, 0) except: - print "'%s' is not a valid magic number" % magic + print("'%s' is not a valid magic number" % magic) sys.exit(1) h = QcowHeader(fd) @@ -200,7 +200,7 @@ def cmd_del_header_ext(fd, magic): h.extensions.remove(ex) if not found: - print "No such header extension" + print("No such header extension") return h.update(fd) @@ -211,7 +211,7 @@ def cmd_set_feature_bit(fd, group, bit): if bit < 0 or bit >= 64: raise ValueError except: - print "'%s' is not a valid bit number in range [0, 64)" % bit + print("'%s' is not a valid bit number in range [0, 64)" % bit) sys.exit(1) h = QcowHeader(fd) @@ -222,7 +222,7 @@ def cmd_set_feature_bit(fd, group, bit): elif group == 'autoclear': h.autoclear_features |= 1 << bit else: - print "'%s' is not a valid group, try 'incompatible', 'compatible', or 'autoclear'" % group + print("'%s' is not a valid group, try 'incompatible', 'compatible', or 'autoclear'" % group) sys.exit(1) h.update(fd) @@ -248,16 +248,16 @@ def main(filename, cmd, args): else: handler(fd, *args) return - print "Unknown command '%s'" % cmd + print("Unknown command '%s'" % cmd) finally: fd.close() def usage(): - print "Usage: %s [, ...]" % sys.argv[0] - print "" - print "Supported commands:" + print("Usage: %s [, ...]" % sys.argv[0]) + print("") + print("Supported commands:") for name, handler, num_args, desc in cmds: - print " %-20s - %s" % (name, desc) + print(" %-20s - %s" % (name, desc)) if __name__ == '__main__': if len(sys.argv) < 3: diff --git a/tests/qemu-iotests/qed.py b/tests/qemu-iotests/qed.py index 52ff845590a..137c884230f 100755 --- a/tests/qemu-iotests/qed.py +++ b/tests/qemu-iotests/qed.py @@ -74,7 +74,7 @@ def store_header(self): def read_table(self, offset): size = self.header['table_size'] * self.header['cluster_size'] s = self.raw_pread(offset, size) - table = [unpack_table_elem(s[i:i + table_elem_size]) for i in xrange(0, size, table_elem_size)] + table = [unpack_table_elem(s[i:i + table_elem_size]) for i in range(0, size, table_elem_size)] return table def load_l1_table(self): @@ -108,12 +108,12 @@ def corrupt_table_invalidate(qed, table): def cmd_show(qed, *args): '''show [header|l1|l2 ]- Show header or l1/l2 tables''' if not args or args[0] == 'header': - print qed.header + print(qed.header) elif args[0] == 'l1': - print qed.l1_table + print(qed.l1_table) elif len(args) == 2 and args[0] == 'l2': offset = int(args[1]) - print qed.read_table(offset) + print(qed.read_table(offset)) else: err('unrecognized sub-command') @@ -146,7 +146,7 @@ def cmd_invalidate(qed, table_level): def cmd_need_check(qed, *args): '''need-check [on|off] - Test, set, or clear the QED_F_NEED_CHECK header bit''' if not args: - print bool(qed.header['features'] & QED_F_NEED_CHECK) + print(bool(qed.header['features'] & QED_F_NEED_CHECK)) return if args[0] == 'on': @@ -165,7 +165,7 @@ def cmd_zero_cluster(qed, pos, *args): err('expected one argument') n = int(args[0]) - for i in xrange(n): + for i in range(n): l1_index = pos / qed.header['cluster_size'] / len(qed.l1_table) if qed.l1_table[l1_index] == 0: err('no l2 table allocated') @@ -184,7 +184,7 @@ def cmd_copy_metadata(qed, outfile): # Match file size out.seek(qed.filesize - 1) - out.write('\0') + out.write(b'\0') # Copy header clusters out.seek(0) @@ -193,7 +193,7 @@ def cmd_copy_metadata(qed, outfile): # Copy L1 table out.seek(qed.header['l1_table_offset']) - s = ''.join(pack_table_elem(x) for x in qed.l1_table) + s = b''.join(pack_table_elem(x) for x in qed.l1_table) out.write(s) # Copy L2 tables @@ -202,17 +202,17 @@ def cmd_copy_metadata(qed, outfile): continue l2_table = qed.read_table(l2_offset) out.seek(l2_offset) - s = ''.join(pack_table_elem(x) for x in l2_table) + s = b''.join(pack_table_elem(x) for x in l2_table) out.write(s) out.close() def usage(): - print 'Usage: %s [, ...]' % sys.argv[0] - print - print 'Supported commands:' + print('Usage: %s [, ...]' % sys.argv[0]) + print() + print('Supported commands:') for cmd in sorted(x for x in globals() if x.startswith('cmd_')): - print globals()[cmd].__doc__ + print(globals()[cmd].__doc__) sys.exit(1) def main(): @@ -227,7 +227,7 @@ def main(): qed = QED(open(filename, 'r+b')) try: globals()[cmd](qed, *sys.argv[3:]) - except TypeError, e: + except TypeError as e: sys.stderr.write(globals()[cmd].__doc__ + '\n') sys.exit(1)