[CRIU] [PATCH 1/2] python: futurize

Radostin Stoyanov rstoyanov1 at gmail.com
Sat May 4 21:37:36 MSK 2019


On 03/05/2019 16:28, Kir Kolyshkin wrote:
> Make python code work with both python2 and python3.
> Initially I tried to do that manually but later I found
> this wonderful tool called futurize.
>
> This patch is a result of running `futurize --stage1 -w`
> and `futurize --stage2 -w`.
>
> As a result, I can run it with either python2 or python3.
>
> Signed-off-by: Kir Kolyshkin <kolyshkin at gmail.com>
> ---
>  coredump/criu_coredump/__init__.py |  5 +-
>  coredump/criu_coredump/coredump.py | 30 +++++++-----
>  coredump/criu_coredump/elf.py      |  4 +-
>  lib/py/cli.py                      |  5 +-
>  lib/py/criu.py                     |  6 ++-
>  lib/py/images/images.py            | 20 ++++----
>  lib/py/images/pb2dict.py           |  4 +-
>  scripts/magic-gen.py               |  2 +
>  soccr/test/run.py                  |  5 +-
>  test/check_actions.py              |  1 +
>  test/crit-recode.py                |  1 +
>  test/exhaustive/pipe.py            |  4 +-
>  test/exhaustive/unix.py            | 22 +++++----
>  test/inhfd/pipe.py                 |  1 +
>  test/inhfd/tty.py                  |  1 +
>  test/others/ext-tty/run.py         |  2 +
>  test/others/mounts/mounts.py       |  2 +
>  test/others/rpc/config_file.py     |  1 +
>  test/others/rpc/errno.py           |  5 +-
>  test/others/rpc/ps_test.py         |  1 +
>  test/others/rpc/restore-loop.py    |  1 +
>  test/others/rpc/test.py            |  1 +
>  test/others/rpc/version.py         |  1 +
>  test/others/shell-job/run.py       |  4 +-
>  test/zdtm.py                       | 77 ++++++++++++++----------------
>  25 files changed, 122 insertions(+), 84 deletions(-)
>
> diff --git a/coredump/criu_coredump/__init__.py b/coredump/criu_coredump/__init__.py
> index 213af42ec..fb992462b 100644
> --- a/coredump/criu_coredump/__init__.py
> +++ b/coredump/criu_coredump/__init__.py
> @@ -1,2 +1,3 @@
> -from coredump import *
> -import elf
> +from __future__ import absolute_import
> +from .coredump import *
> +from . import elf
> diff --git a/coredump/criu_coredump/coredump.py b/coredump/criu_coredump/coredump.py
> index 963e8c61b..ecafd62c6 100644
> --- a/coredump/criu_coredump/coredump.py
> +++ b/coredump/criu_coredump/coredump.py
> @@ -1,3 +1,9 @@
> +from __future__ import absolute_import
> +from __future__ import division
> +from builtins import str
> +from builtins import range
> +from past.utils import old_div
> +from builtins import object
>  # Functions and classes for creating core dump from criu images.
>  # Code is inspired by outdated google coredumper(RIP) [1] and
>  # fs/binfmt_elf.h from Linux kernel [2].
> @@ -29,7 +35,7 @@
>  #	4) VMAs contents;
>  #
>  import io
> -import elf
> +from . import elf
>  import ctypes
>  from pycriu import images
>  
> @@ -60,13 +66,13 @@ prot = {
>  	"PROT_EXEC"	: 0x4
>  }
>  
> -class elf_note:
> +class elf_note(object):
>  	nhdr	= None	# Elf_Nhdr;
>  	owner	= None	# i.e. CORE or LINUX;
>  	data	= None	# Ctypes structure with note data;
>  
>  
> -class coredump:
> +class coredump(object):
>  	"""
>  	A class to keep elf core dump components inside and
>  	functions to properly write them to file.
> @@ -117,7 +123,7 @@ class coredump:
>  		f.write(buf.read())
>  
>  
> -class coredump_generator:
> +class coredump_generator(object):
>  	"""
>  	Generate core dump from criu images.
>  	"""
> @@ -476,7 +482,7 @@ class coredump_generator:
>  		Generate NT_AUXV note for thread tid of process pid.
>  		"""
>  		mm = self.mms[pid]
> -		num_auxv = len(mm["mm_saved_auxv"])/2
> +		num_auxv = old_div(len(mm["mm_saved_auxv"]),2)
Would it be cleaner to use the following import line and/or the //
operator [1] instead?

    from __future__ import division

The main difference is that:

Python 2:
    3 / 2 == 1
    3 // 2 == 1

Python 3:
    3 / 2 == 1.5
    3 // 2 == 1

With this __future__ import both Python 2 and 3 will result in:
    3 / 2 == 1.5
    3 // 2 == 1

[1] https://docs.python.org/3/howto/pyporting.html#division

Radostin
>  
>  		class elf_auxv(ctypes.Structure):
>  			_fields_ = [("auxv", elf.Elf64_auxv_t*num_auxv)]
> @@ -504,7 +510,7 @@ class coredump_generator:
>  		"""
>  		mm = self.mms[pid]
>  
> -		class mmaped_file_info:
> +		class mmaped_file_info(object):
>  			start		= None
>  			end		= None
>  			file_ofs	= None
> @@ -518,10 +524,10 @@ class coredump_generator:
>  
>  			shmid	= vma["shmid"]
>  			size	= vma["end"] - vma["start"]
> -			off	= vma["pgoff"]/PAGESIZE
> +			off	= old_div(vma["pgoff"],PAGESIZE)
>  
>  			files	= self.reg_files
> -			fname	= filter(lambda x: x["id"] == shmid, files)[0]["name"]
> +			fname	= [x for x in files if x["id"] == shmid][0]["name"]
>  
>  			info = mmaped_file_info()
>  			info.start	= vma["start"]
> @@ -669,7 +675,7 @@ class coredump_generator:
>  			off	= vma["pgoff"]
>  
>  			files	= self.reg_files
> -			fname	= filter(lambda x: x["id"] == shmid, files)[0]["name"]
> +			fname	= [x for x in files if x["id"] == shmid][0]["name"]
>  
>  			f = open(fname)
>  			f.seek(off)
> @@ -693,8 +699,8 @@ class coredump_generator:
>  		# a file, and changed ones -- from pages.img.
>  		# Finally, if no page is found neither in pages.img nor
>  		# in file, hole in inserted -- a page filled with zeroes.
> -		start_page	= start/PAGESIZE
> -		end_page	= end/PAGESIZE
> +		start_page	= old_div(start,PAGESIZE)
> +		end_page	= old_div(end,PAGESIZE)
>  
>  		buf = ""
>  		for page_no in range(start_page, end_page+1):
> @@ -804,7 +810,7 @@ class coredump_generator:
>  		"""
>  		mm = self.mms[pid]
>  
> -		class vma_class:
> +		class vma_class(object):
>  			data	= None
>  			filesz	= None
>  			memsz	= None
> diff --git a/coredump/criu_coredump/elf.py b/coredump/criu_coredump/elf.py
> index 1da06a6fd..de085ddad 100644
> --- a/coredump/criu_coredump/elf.py
> +++ b/coredump/criu_coredump/elf.py
> @@ -1,3 +1,5 @@
> +from __future__ import division
> +from past.utils import old_div
>  # Define structures and constants for generating elf file.
>  import ctypes
>  
> @@ -266,7 +268,7 @@ elf_fpregset_t = user_fpregs_struct
>  # siginfo_t related constants.
>  
>  _SI_MAX_SIZE	= 128
> -_SI_PAD_SIZE	= (_SI_MAX_SIZE/ctypes.sizeof(ctypes.c_int)) - 4
> +_SI_PAD_SIZE	= (old_div(_SI_MAX_SIZE,ctypes.sizeof(ctypes.c_int))) - 4
>  
>  					#          /* kill().  */
>  class _siginfo_t_U_kill(ctypes.Structure):	#         struct
> diff --git a/lib/py/cli.py b/lib/py/cli.py
> index abaf0720c..29b21ec8f 100755
> --- a/lib/py/cli.py
> +++ b/lib/py/cli.py
> @@ -1,4 +1,5 @@
>  from __future__ import print_function
> +from builtins import object
>  import argparse
>  import sys
>  import json
> @@ -55,7 +56,7 @@ def get_task_id(p, val):
>  # Explorers
>  #
>  
> -class ps_item:
> +class ps_item(object):
>  	def __init__(self, p, core):
>  		self.pid = get_task_id(p, 'pid')
>  		self.ppid = p['ppid']
> @@ -178,7 +179,7 @@ def explore_fds(opts):
>  		print("\t%7s: %s" % ('root', get_file_str(opts, {'type': 'REG', 'id': fdi['root_id']})))
>  
>  
> -class vma_id:
> +class vma_id(object):
>  	def __init__(self):
>  		self.__ids = {}
>  		self.__last = 1
> diff --git a/lib/py/criu.py b/lib/py/criu.py
> index de1a214a3..eb1961964 100644
> --- a/lib/py/criu.py
> +++ b/lib/py/criu.py
> @@ -1,3 +1,5 @@
> +from builtins import str
> +from builtins import object
>  # Same as libcriu for C.
>  
>  import socket
> @@ -8,7 +10,7 @@ import struct
>  
>  import pycriu.rpc_pb2 as rpc
>  
> -class _criu_comm:
> +class _criu_comm(object):
>  	"""
>  	Base class for communication classes.
>  	"""
> @@ -183,7 +185,7 @@ class CRIUExceptionExternal(CRIUException):
>  		return s
>  
>  
> -class criu:
> +class criu(object):
>  	"""
>  	Call criu through RPC.
>  	"""
> diff --git a/lib/py/images/images.py b/lib/py/images/images.py
> index 7a9b9da6e..ffebfee74 100644
> --- a/lib/py/images/images.py
> +++ b/lib/py/images/images.py
> @@ -1,3 +1,5 @@
> +from builtins import range
> +from builtins import object
>  # This file contains methods to deal with criu images.
>  #
>  # According to http://criu.org/Images, criu images can be described
> @@ -67,7 +69,7 @@ class MagicException(Exception):
>  
>  # Generic class to handle loading/dumping criu images entries from/to bin
>  # format to/from dict(json).
> -class entry_handler:
> +class entry_handler(object):
>  	"""
>  	Generic class to handle loading/dumping criu images
>  	entries from/to bin format to/from dict(json).
> @@ -175,7 +177,7 @@ class entry_handler:
>  		return entries
>  
>  # Special handler for pagemap.img
> -class pagemap_handler:
> +class pagemap_handler(object):
>  	"""
>  	Special entry handler for pagemap.img, which is unique in a way
>  	that it has a header of pagemap_head type followed by entries
> @@ -221,7 +223,7 @@ class pagemap_handler:
>  		return entry_handler(None).count(f) - 1
>  
>  # Special handler for ghost-file.img
> -class ghost_file_handler:
> +class ghost_file_handler(object):
>  	def load(self, f, pretty = False, no_payload = False):
>  		entries = []
>  
> @@ -292,7 +294,7 @@ class ghost_file_handler:
>  # it doesn't really matter, because our images
>  # do not store big amounts of binary data. They
>  # are negligible comparing to pages size.
> -class pipes_data_extra_handler:
> +class pipes_data_extra_handler(object):
>  	def load(self, f, pload):
>  		size = pload.bytes
>  		data = f.read(size)
> @@ -306,7 +308,7 @@ class pipes_data_extra_handler:
>  		f.seek(pload.bytes, os.SEEK_CUR)
>  		return pload.bytes
>  
> -class sk_queues_extra_handler:
> +class sk_queues_extra_handler(object):
>  	def load(self, f, pload):
>  		size = pload.length
>  		data = f.read(size)
> @@ -321,7 +323,7 @@ class sk_queues_extra_handler:
>  		return pload.length
>  
>  
> -class tcp_stream_extra_handler:
> +class tcp_stream_extra_handler(object):
>  	def load(self, f, pbuff):
>  		d = {}
>  
> @@ -344,7 +346,7 @@ class tcp_stream_extra_handler:
>  		f.seek(0, os.SEEK_END)
>  		return pbuff.inq_len + pbuff.outq_len
>  
> -class ipc_sem_set_handler:
> +class ipc_sem_set_handler(object):
>  	def load(self, f, pbuff):
>  		entry = pb2dict.pb2dict(pbuff)
>  		size = sizeof_u16 * entry['nsems']
> @@ -375,7 +377,7 @@ class ipc_sem_set_handler:
>  		f.seek(round_up(size, sizeof_u64), os.SEEK_CUR)
>  		return size
>  
> -class ipc_msg_queue_handler:
> +class ipc_msg_queue_handler(object):
>  	def load(self, f, pbuff):
>  		entry = pb2dict.pb2dict(pbuff)
>  		messages = []
> @@ -423,7 +425,7 @@ class ipc_msg_queue_handler:
>  
>  		return pl_len
>  
> -class ipc_shm_handler:
> +class ipc_shm_handler(object):
>  	def load(self, f, pbuff):
>  		entry = pb2dict.pb2dict(pbuff)
>  		size = entry['size']
> diff --git a/lib/py/images/pb2dict.py b/lib/py/images/pb2dict.py
> index 18d4c68eb..8952e755d 100644
> --- a/lib/py/images/pb2dict.py
> +++ b/lib/py/images/pb2dict.py
> @@ -1,5 +1,7 @@
> +from __future__ import absolute_import
> +from builtins import str
>  from google.protobuf.descriptor import FieldDescriptor as FD
> -import opts_pb2
> +from . import opts_pb2
>  from ipaddress import IPv4Address, ip_address
>  from ipaddress import IPv6Address
>  import socket
> diff --git a/scripts/magic-gen.py b/scripts/magic-gen.py
> index 7088f634d..1aa7dd2ba 100755
> --- a/scripts/magic-gen.py
> +++ b/scripts/magic-gen.py
> @@ -1,4 +1,6 @@
>  #!/bin/env python2
> +from __future__ import print_function
> +from builtins import str
>  import sys
>  
>  # This program parses criu magic.h file and produces
> diff --git a/soccr/test/run.py b/soccr/test/run.py
> index a25c29263..6faf3491f 100644
> --- a/soccr/test/run.py
> +++ b/soccr/test/run.py
> @@ -1,5 +1,6 @@
>  #!/usr/bin/env python2
>  
> +from __future__ import print_function
>  import sys, os
>  import hashlib
>  from subprocess import Popen, PIPE
> @@ -41,7 +42,7 @@ m.update(str2)
>  str2 = m.hexdigest()
>  
>  if str2 != eval(s):
> -    print("FAIL", repr(str2), repr(s))
> +    print(("FAIL", repr(str2), repr(s)))
>      sys.exit(5);
>  
>  s = p1.stdout.read()
> @@ -51,7 +52,7 @@ str1 = m.hexdigest()
>  
>  s = p2.stdout.read()
>  if str1 != eval(s):
> -    print("FAIL", repr(str1), s)
> +    print(("FAIL", repr(str1), s))
>      sys.exit(5);
>  
>  if p1.wait():
> diff --git a/test/check_actions.py b/test/check_actions.py
> index 0e3daf178..7c1b41d6b 100755
> --- a/test/check_actions.py
> +++ b/test/check_actions.py
> @@ -1,5 +1,6 @@
>  #!/usr/bin/env python2
>  
> +from __future__ import print_function
>  import sys
>  import os
>  
> diff --git a/test/crit-recode.py b/test/crit-recode.py
> index 441f7757e..b2904acb3 100755
> --- a/test/crit-recode.py
> +++ b/test/crit-recode.py
> @@ -1,6 +1,7 @@
>  #!/usr/bin/env python
>  # vim: noet ts=8 sw=8 sts=8
>  
> +from __future__ import print_function
>  import pycriu
>  import sys
>  import os
> diff --git a/test/exhaustive/pipe.py b/test/exhaustive/pipe.py
> index 17e065800..2756e8fa6 100755
> --- a/test/exhaustive/pipe.py
> +++ b/test/exhaustive/pipe.py
> @@ -1,5 +1,7 @@
>  #!/usr/bin/env python
>  
> +from __future__ import print_function
> +from builtins import range
>  import argparse
>  import os
>  import signal
> @@ -76,7 +78,7 @@ def check_pipe_y(pid, fd, rw, inos):
>  	ino = get_pipe_ino(pid, fd)
>  	if ino == None:
>  		return 'missing '
> -	if not inos.has_key(fd):
> +	if fd not in inos:
>  		inos[fd] = ino
>  	elif inos[fd] != ino:
>  		return 'wrong '
> diff --git a/test/exhaustive/unix.py b/test/exhaustive/unix.py
> index 41053bd0d..05c2bd87a 100755
> --- a/test/exhaustive/unix.py
> +++ b/test/exhaustive/unix.py
> @@ -1,5 +1,7 @@
>  #!/usr/bin/env python
>  
> +from __future__ import print_function
> +from builtins import object
>  import sys
>  import os
>  import socket
> @@ -31,7 +33,7 @@ def mk_socket(st, typ):
>  	st.add_socket(sk)
>  	return sk
>  
> -class act_socket:
> +class act_socket(object):
>  	def __init__(self, typ):
>  		self.typ = typ
>  
> @@ -47,7 +49,7 @@ class act_socket:
>  		return 'socket(%s) = %d' % (sk_type_s[self.typ], self.sk_id)
>  
>  
> -class act_close:
> +class act_close(object):
>  	def __init__(self, sk_id):
>  		self.sk_id = sk_id
>  
> @@ -66,7 +68,7 @@ class act_close:
>  		return 'close(%d)' % self.sk_id
>  
>  
> -class act_listen:
> +class act_listen(object):
>  	def __init__(self, sk_id):
>  		self.sk_id = sk_id
>  
> @@ -82,7 +84,7 @@ class act_listen:
>  		return 'listen(%d)' % self.sk_id
>  
>  
> -class act_bind:
> +class act_bind(object):
>  	def __init__(self, sk_id, name_id):
>  		self.sk_id = sk_id
>  		self.name_id = name_id
> @@ -99,7 +101,7 @@ class act_bind:
>  		return 'bind(%d, $name-%d)' % (self.sk_id, self.name_id)
>  
>  
> -class act_connect:
> +class act_connect(object):
>  	def __init__(self, sk_id, listen_sk_id):
>  		self.sk_id = sk_id
>  		self.lsk_id = listen_sk_id
> @@ -128,7 +130,7 @@ class act_connect:
>  		return 'connect(%d, $name-%d)' % (self.sk_id, self.lsk_id)
>  
>  
> -class act_accept:
> +class act_accept(object):
>  	def __init__(self, sk_id):
>  		self.sk_id = sk_id
>  
> @@ -150,7 +152,7 @@ class act_accept:
>  		return 'accept(%d) = %d' % (self.sk_id, self.nsk_id)
>  
>  
> -class act_sendmsg:
> +class act_sendmsg(object):
>  	def __init__(self, sk_id, to_id):
>  		self.sk_id = sk_id
>  		self.to_id = to_id
> @@ -183,7 +185,7 @@ class act_sendmsg:
>  #
>  # Description of a socket
>  #
> -class sock:
> +class sock(object):
>  	def __init__(self, sk_id, sock_type):
>  		# ID of a socket. Since states and sockets are cloned
>  		# while we scan the tree of states the only valid way
> @@ -363,7 +365,7 @@ class sock:
>  		return dsc
>  
>  
> -class state:
> +class state(object):
>  	def __init__(self, max_sockets, sk_type):
>  		self.sockets = []
>  		self.sk_id = 0
> @@ -602,7 +604,7 @@ def chk_state(st, opts):
>  		sys.exit(ret)
>  
>  	signal_sk.close()
> -	for rsk in st.real_sockets.values():
> +	for rsk in list(st.real_sockets.values()):
>  		rsk.close()
>  
>  	print("`- dump")
> diff --git a/test/inhfd/pipe.py b/test/inhfd/pipe.py
> index 318dc862d..2b0971631 100755
> --- a/test/inhfd/pipe.py
> +++ b/test/inhfd/pipe.py
> @@ -1,3 +1,4 @@
> +from builtins import range
>  import os
>  
>  
> diff --git a/test/inhfd/tty.py b/test/inhfd/tty.py
> index ae76a96d4..5ab33f24b 100755
> --- a/test/inhfd/tty.py
> +++ b/test/inhfd/tty.py
> @@ -1,3 +1,4 @@
> +from builtins import range
>  # vim: noet ts=8 sw=8 sts=8
>  import fcntl
>  import os
> diff --git a/test/others/ext-tty/run.py b/test/others/ext-tty/run.py
> index f44b1d946..eed698d6c 100755
> --- a/test/others/ext-tty/run.py
> +++ b/test/others/ext-tty/run.py
> @@ -1,4 +1,6 @@
>  #!/usr/bin/env python2
> +from __future__ import print_function
> +from builtins import str
>  import subprocess
>  import os, sys, time, signal, pty
>  
> diff --git a/test/others/mounts/mounts.py b/test/others/mounts/mounts.py
> index dc65ba45c..8cab81273 100755
> --- a/test/others/mounts/mounts.py
> +++ b/test/others/mounts/mounts.py
> @@ -1,3 +1,5 @@
> +from __future__ import print_function
> +from builtins import range
>  import os
>  import tempfile, random
>  
> diff --git a/test/others/rpc/config_file.py b/test/others/rpc/config_file.py
> index 23a06615f..1fd860998 100755
> --- a/test/others/rpc/config_file.py
> +++ b/test/others/rpc/config_file.py
> @@ -1,5 +1,6 @@
>  #!/usr/bin/python2
>  
> +from __future__ import print_function
>  import os
>  import socket
>  import sys
> diff --git a/test/others/rpc/errno.py b/test/others/rpc/errno.py
> index ee9e90d8c..89600c9ac 100755
> --- a/test/others/rpc/errno.py
> +++ b/test/others/rpc/errno.py
> @@ -1,6 +1,9 @@
>  #!/usr/bin/python2
>  # Test criu errno
>  
> +from __future__ import print_function
> +from builtins import str
> +from builtins import object
>  import socket, os, errno
>  import rpc_pb2 as rpc
>  import argparse
> @@ -12,7 +15,7 @@ parser.add_argument('dir', type = str, help = "Directory where CRIU images shoul
>  args = vars(parser.parse_args())
>  
>  # Prepare dir for images
> -class test:
> +class test(object):
>  	def __init__(self):
>  		self.imgs_fd = os.open(args['dir'], os.O_DIRECTORY)
>  		self.s = -1
> diff --git a/test/others/rpc/ps_test.py b/test/others/rpc/ps_test.py
> index 1872120fc..ba6d8e11a 100755
> --- a/test/others/rpc/ps_test.py
> +++ b/test/others/rpc/ps_test.py
> @@ -1,5 +1,6 @@
>  #!/usr/bin/python2
>  
> +from __future__ import print_function
>  import socket, os, sys, errno
>  import rpc_pb2 as rpc
>  import argparse
> diff --git a/test/others/rpc/restore-loop.py b/test/others/rpc/restore-loop.py
> index ce5786a56..0238e4078 100755
> --- a/test/others/rpc/restore-loop.py
> +++ b/test/others/rpc/restore-loop.py
> @@ -1,5 +1,6 @@
>  #!/usr/bin/python2
>  
> +from __future__ import print_function
>  import socket, os, sys
>  import rpc_pb2 as rpc
>  import argparse
> diff --git a/test/others/rpc/test.py b/test/others/rpc/test.py
> index 0addbaedc..af60507ab 100755
> --- a/test/others/rpc/test.py
> +++ b/test/others/rpc/test.py
> @@ -1,5 +1,6 @@
>  #!/usr/bin/python2
>  
> +from __future__ import print_function
>  import socket, os, sys
>  import rpc_pb2 as rpc
>  import argparse
> diff --git a/test/others/rpc/version.py b/test/others/rpc/version.py
> index 247bc466d..b296fc4bc 100755
> --- a/test/others/rpc/version.py
> +++ b/test/others/rpc/version.py
> @@ -1,5 +1,6 @@
>  #!/usr/bin/python2
>  
> +from __future__ import print_function
>  import socket
>  import sys
>  import rpc_pb2 as rpc
> diff --git a/test/others/shell-job/run.py b/test/others/shell-job/run.py
> index 4f4dfadef..07d65afd3 100755
> --- a/test/others/shell-job/run.py
> +++ b/test/others/shell-job/run.py
> @@ -1,4 +1,6 @@
>  #!/usr/bin/env python2
> +from __future__ import print_function
> +from builtins import str
>  import os, pty, sys, subprocess
>  import termios, fcntl, time
>  
> @@ -11,7 +13,7 @@ def create_pty():
>          return (os.fdopen(fd1, "w+"), os.fdopen(fd2, "w+"))
>  
>  if not os.access("work", os.X_OK):
> -    os.mkdir("work", 0755)
> +    os.mkdir("work", 0o755)
>  
>  open("running", "w").close()
>  m,s = create_pty()
> diff --git a/test/zdtm.py b/test/zdtm.py
> index e6325d5f5..3e87b2d08 100755
> --- a/test/zdtm.py
> +++ b/test/zdtm.py
> @@ -1,6 +1,12 @@
>  #!/usr/bin/env python
>  # vim: noet ts=8 sw=8 sts=8
>  from __future__ import absolute_import, division, print_function, unicode_literals
> +from builtins import zip
> +from builtins import input
> +from builtins import oct
> +from builtins import str
> +from builtins import range
> +from builtins import object
>  from builtins import (str, open, range, zip, int, input)
>  
>  import argparse
> @@ -145,7 +151,7 @@ arch = os.uname()[4]
>  #
>  
>  
> -class host_flavor:
> +class host_flavor(object):
>  	def __init__(self, opts):
>  		self.name = "host"
>  		self.ns = False
> @@ -162,7 +168,7 @@ class host_flavor:
>  		pass
>  
>  
> -class ns_flavor:
> +class ns_flavor(object):
>  	__root_dirs = ["/bin", "/sbin", "/etc", "/lib", "/lib64", "/dev", "/dev/pts", "/dev/net", "/tmp", "/usr", "/proc", "/run"]
>  
>  	def __init__(self, opts):
> @@ -192,11 +198,7 @@ class ns_flavor:
>  
>  		# This Mayakovsky-style code gets list of libraries a binary
>  		# needs minus vdso and gate .so-s
> -		libs = map(lambda x: x[1] == '=>' and x[2] or x[0],
> -				map(lambda x: str(x).split(),
> -					filter(lambda x: not xl.match(x),
> -						map(lambda x: str(x).strip(),
> -							filter(lambda x: str(x).startswith('\t'), ldd.stdout.read().decode('ascii').splitlines())))))
> +		libs = [x[1] == '=>' and x[2] or x[0] for x in [str(x).split() for x in [x for x in [str(x).strip() for x in [x for x in ldd.stdout.read().decode('ascii').splitlines() if str(x).startswith('\t')]] if not xl.match(x)]]]
>  
>  		ldd.wait()
>  
> @@ -291,7 +293,7 @@ class userns_flavor(ns_flavor):
>  
>  
>  flavors = {'h': host_flavor, 'ns': ns_flavor, 'uns': userns_flavor}
> -flavors_codes = dict(zip(range(len(flavors)), sorted(flavors.keys())))
> +flavors_codes = dict(list(zip(list(range(len(flavors))), sorted(flavors.keys()))))
>  
>  #
>  # Helpers
> @@ -365,7 +367,7 @@ class test_fail_expected_exc(Exception):
>  #
>  
>  
> -class zdtm_test:
> +class zdtm_test(object):
>  	def __init__(self, name, desc, flavor, freezer):
>  		self.__name = name
>  		self.__desc = desc
> @@ -497,7 +499,7 @@ class zdtm_test:
>  		self.kill(signal.SIGTERM)
>  
>  		res = tail(self.__name + '.out')
> -		if 'PASS' not in list(map(lambda s: s.strip(), res.split())):
> +		if 'PASS' not in list([s.strip() for s in res.split()]):
>  			if os.access(self.__name + '.out.inprogress', os.F_OK):
>  				print_sep(self.__name + '.out.inprogress')
>  				with open(self.__name + '.out.inprogress') as fd:
> @@ -586,7 +588,7 @@ def load_module_from_file(name, path):
>  	return mod
>  
>  
> -class inhfd_test:
> +class inhfd_test(object):
>  	def __init__(self, name, desc, flavor, freezer):
>  		self.__name = os.path.basename(name)
>  		print("Load %s" % name)
> @@ -736,7 +738,7 @@ class groups_test(zdtm_test):
>  		if flavor.ns:
>  			self.__real_name = name
>  			with open(name) as fd:
> -				self.__subs = map(lambda x: x.strip(), fd.readlines())
> +				self.__subs = [x.strip() for x in fd.readlines()]
>  			print("Subs:\n%s" % '\n'.join(self.__subs))
>  		else:
>  			self.__real_name = ''
> @@ -789,7 +791,7 @@ test_classes = {'zdtm': zdtm_test, 'inhfd': inhfd_test, 'groups': groups_test}
>  join_ns_file = '/run/netns/zdtm_netns'
>  
>  
> -class criu_cli:
> +class criu_cli(object):
>  	@staticmethod
>  	def run(action, args, criu_bin, fault = None, strace = [], preexec = None, nowait = False):
>  		env = dict(os.environ, ASAN_OPTIONS = "log_path=asan.log:disable_coredump=0:detect_leaks=0")
> @@ -805,7 +807,7 @@ class criu_cli:
>  		return cr.wait()
>  
>  
> -class criu_rpc_process:
> +class criu_rpc_process(object):
>  	def wait(self):
>  		return self.criu.wait_pid(self.pid)
>  
> @@ -813,7 +815,7 @@ class criu_rpc_process:
>  		os.kill(self.pid, signal.SIGTERM)
>  
>  
> -class criu_rpc:
> +class criu_rpc(object):
>  	@staticmethod
>  	def __set_opts(criu, args, ctx):
>  		while len(args) != 0:
> @@ -934,7 +936,7 @@ class criu_rpc:
>  		return ret
>  
>  
> -class criu:
> +class criu(object):
>  	def __init__(self, opts):
>  		self.__test = None
>  		self.__dump_path = None
> @@ -1287,7 +1289,7 @@ def init_sbs():
>  
>  def sbs(what):
>  	if do_sbs:
> -		input("Pause at %s. Press Enter to continue." % what)
> +		eval(input("Pause at %s. Press Enter to continue." % what))
>  
>  
>  #
> @@ -1295,7 +1297,7 @@ def sbs(what):
>  #
>  def iter_parm(opt, dflt):
>  	x = ((opt or str(dflt)) + ":0").split(':')
> -	return (range(0, int(x[0])), float(x[1]))
> +	return (list(range(0, int(x[0]))), float(x[1]))
>  
>  
>  def cr(cr_api, test, opts):
> @@ -1352,7 +1354,7 @@ def get_visible_state(test):
>  		return ({}, {}, {})
>  
>  	r = re.compile('^[0-9]+$')
> -	pids = filter(lambda p: r.match(p), os.listdir("/proc/%s/root/proc/" % test.getpid()))
> +	pids = [p for p in os.listdir("/proc/%s/root/proc/" % test.getpid()) if r.match(p)]
>  	for pid in pids:
>  		files[pid] = set(os.listdir("/proc/%s/root/proc/%s/fd" % (test.getpid(), pid)))
>  
> @@ -1360,7 +1362,7 @@ def get_visible_state(test):
>  		last = 0
>  		mapsfd = open("/proc/%s/root/proc/%s/maps" % (test.getpid(), pid))
>  		for mp in mapsfd:
> -			m = list(map(lambda x: int('0x' + x, 0), mp.split()[0].split('-')))
> +			m = list([int('0x' + x, 0) for x in mp.split()[0].split('-')])
>  
>  			m.append(mp.split()[1])
>  
> @@ -1376,7 +1378,7 @@ def get_visible_state(test):
>  				last += 1
>  		mapsfd.close()
>  
> -		maps[pid] = set(map(lambda x: '%x-%x %s' % (x[0], x[1], " ".join(x[2:])), cmaps))
> +		maps[pid] = set(['%x-%x %s' % (x[0], x[1], " ".join(x[2:])) for x in cmaps])
>  
>  		cmounts = []
>  		try:
> @@ -1394,7 +1396,7 @@ def get_visible_state(test):
>  def check_visible_state(test, state, opts):
>  	new = get_visible_state(test)
>  
> -	for pid in state[0].keys():
> +	for pid in list(state[0].keys()):
>  		fnew = new[0][pid]
>  		fold = state[0][pid]
>  		if fnew != fold:
> @@ -1436,7 +1438,7 @@ def check_visible_state(test, state, opts):
>  			raise test_fail_exc("link remaps left")
>  
>  
> -class noop_freezer:
> +class noop_freezer(object):
>  	def __init__(self):
>  		self.kernel = False
>  
> @@ -1456,7 +1458,7 @@ class noop_freezer:
>  		return []
>  
>  
> -class cg_freezer:
> +class cg_freezer(object):
>  	def __init__(self, path, state):
>  		self.__path = '/sys/fs/cgroup/freezer/' + path
>  		self.__state = state
> @@ -1632,7 +1634,7 @@ def do_run_test(tname, tdesc, flavs, opts):
>  			print_sep("Test %s PASS" % tname)
>  
>  
> -class Launcher:
> +class Launcher(object):
>  	def __init__(self, opts, nr_tests):
>  		self.__opts = opts
>  		self.__total = nr_tests
> @@ -1847,13 +1849,10 @@ def all_tests(opts):
>  			if stat.S_IFMT(st.st_mode) in [stat.S_IFLNK, stat.S_IFSOCK]:
>  				continue
>  			files.append(fp)
> -	excl = list(map(lambda x: os.path.join(desc['dir'], x), desc['exclude']))
> -	tlist = filter(lambda x:
> -			not x.endswith('.checkskip') and
> +	excl = list([os.path.join(desc['dir'], x) for x in desc['exclude']])
> +	tlist = [x for x in [x.strip() for x in files] if not x.endswith('.checkskip') and
>  			not x.endswith('.hook') and
> -			x not in excl,
> -			map(lambda x: x.strip(), files)
> -			)
> +			x not in excl]
>  	return tlist
>  
>  
> @@ -1934,7 +1933,7 @@ def run_tests(opts):
>  		run_all = True
>  	elif opts['tests']:
>  		r = re.compile(opts['tests'])
> -		torun = filter(lambda x: r.match(x), all_tests(opts))
> +		torun = [x for x in all_tests(opts) if r.match(x)]
>  		run_all = True
>  	elif opts['test']:
>  		torun = opts['test']
> @@ -1945,7 +1944,7 @@ def run_tests(opts):
>  			return
>  
>  		with open(opts['from']) as fd:
> -			torun = map(lambda x: x.strip(), fd)
> +			torun = [x.strip() for x in fd]
>  		opts['keep_going'] = False
>  		run_all = True
>  	else:
> @@ -2086,11 +2085,11 @@ def list_tests(opts):
>  	tlist = all_tests(opts)
>  	if opts['info']:
>  		print(sti_fmt % ('Name', 'Flavors', 'Flags'))
> -		tlist = map(lambda x: show_test_info(x), tlist)
> +		tlist = [show_test_info(x) for x in tlist]
>  	print('\n'.join(tlist))
>  
>  
> -class group:
> +class group(object):
>  	def __init__(self, tname, tdesc):
>  		self.__tests = [tname]
>  		self.__desc = tdesc
> @@ -2124,9 +2123,7 @@ class group:
>  	# common method to write a "meta" auxiliary script (hook/checkskip)
>  	# which will call all tests' scripts in turn
>  	def __dump_meta(self, fname, ext):
> -		scripts = filter(lambda names: os.access(names[1], os.X_OK),
> -				map(lambda test: (test, test + ext),
> -				self.__tests))
> +		scripts = [names for names in [(test, test + ext) for test in self.__tests] if os.access(names[1], os.X_OK)]
>  		if scripts:
>  			f = open(fname + ext, "w")
>  			f.write("#!/bin/sh -e\n")
> @@ -2311,10 +2308,10 @@ if opts['debug']:
>  
>  if opts['action'] == 'run':
>  	criu.available()
> -for tst in test_classes.values():
> +for tst in list(test_classes.values()):
>  	tst.available()
>  
>  opts['action'](opts)
>  
> -for tst in test_classes.values():
> +for tst in list(test_classes.values()):
>  	tst.cleanup()



More information about the CRIU mailing list