From fe653b7c62e2b5c32fad02ae81bf2bb324dca7d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20K=C5=82oczko?= Date: Tue, 28 May 2024 22:57:27 +0000 Subject: [PATCH] really drop python<=3.7 support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Filter all code over `pyupgrade --py38-plus`. Signed-off-by: Tomasz Kłoczko --- scripts/libtss2_build.py | 2 +- scripts/prepare_headers.py | 30 +++++++++++++++--------------- setup.py | 10 ++++------ src/tpm2_pytss/TCTI.py | 2 +- src/tpm2_pytss/TSS2_Exception.py | 2 +- src/tpm2_pytss/encoding.py | 4 ++-- src/tpm2_pytss/policy.py | 2 +- src/tpm2_pytss/tsskey.py | 2 +- src/tpm2_pytss/types.py | 4 ++-- test/TSS2_BaseTest.py | 14 +++++++------- test/test_crypto.py | 6 +++--- test/test_types.py | 2 +- 12 files changed, 39 insertions(+), 41 deletions(-) diff --git a/scripts/libtss2_build.py b/scripts/libtss2_build.py index d95af3eb..acc3a963 100644 --- a/scripts/libtss2_build.py +++ b/scripts/libtss2_build.py @@ -40,7 +40,7 @@ if not os.path.isabs(PATH): PATH = os.path.join(os.getcwd(), PATH) -print("adding path: {}".format(PATH)) +print(f"adding path: {PATH}") sys.path.insert(0, PATH) from prepare_headers import prepare diff --git a/scripts/prepare_headers.py b/scripts/prepare_headers.py index a7529b36..ec57fe3d 100644 --- a/scripts/prepare_headers.py +++ b/scripts/prepare_headers.py @@ -24,23 +24,23 @@ def remove_common_guards(s): # Remove certain macros s = re.sub("#define TSS2_API_VERSION.*", "", s) s = re.sub("#define TSS2_ABI_VERSION.*", "", s) - s = re.sub("#define TSS2_RC_LAYER\(level\).*", "", s) - s = re.sub("(#define.*)TSS2_RC_LAYER\(0xff\)", "\g<1>0xff0000", s) + s = re.sub(r"#define TSS2_RC_LAYER\(level\).*", "", s) + s = re.sub(r"(#define.*)TSS2_RC_LAYER\(0xff\)", r"\g<1>0xff0000", s) # Remove comments - s = re.sub("/\*.*?\*/", "", s, flags=re.MULTILINE) + s = re.sub(r"/\*.*?\*/", "", s, flags=re.MULTILINE) # Restructure #defines with ... - s = re.sub("(#define [A-Za-z0-9_]+) +\(\(.*?\) \(.*?\)\)", "\g<1>...", s) - s = re.sub("(#define [A-Za-z0-9_]+) +\(\(\(.*?\) .*\)", "\g<1>...", s) - s = re.sub("(#define [A-Za-z0-9_]+) +\(\(.*?\).*?\) ", "\g<1>...", s) + s = re.sub(r"(#define [A-Za-z0-9_]+) +\(\(.*?\) \(.*?\)\)", r"\g<1>...", s) + s = re.sub(r"(#define [A-Za-z0-9_]+) +\(\(\(.*?\) .*\)", r"\g<1>...", s) + s = re.sub(r"(#define [A-Za-z0-9_]+) +\(\(.*?\).*?\) ", r"\g<1>...", s) s = re.sub( - "(#define [A-Za-z0-9_]+) .*\n.*?.*\)\)", "\g<1>...", s, flags=re.MULTILINE + "(#define [A-Za-z0-9_]+) .*\n.*?.*\\)\\)", r"\g<1>...", s, flags=re.MULTILINE ) - s = re.sub("(#define [A-Za-z0-9_]+) .*", "\g<1>...", s) + s = re.sub("(#define [A-Za-z0-9_]+) .*", r"\g<1>...", s) # Restructure structs and untions with ... - s = re.sub("\[.+?\]", "[...]", s) + s = re.sub(r"\[.+?\]", "[...]", s) return s @@ -75,7 +75,7 @@ def prepare_types(dirpath): # Remove false define (workaround) s = re.sub( - "#define TPM2_MAX_TAGGED_POLICIES.*\n.*TPMS_TAGGED_POLICY\)\)", + "#define TPM2_MAX_TAGGED_POLICIES.*\n.*TPMS_TAGGED_POLICY\\)\\)", "", s, flags=re.MULTILINE, @@ -268,19 +268,19 @@ def prepare_mu(dirpath): # At least tpm2-tss 3.0.3 have duplicated BYTE (un)marshal functions which break cffi # So removing them is needed until 3.1.x has reached most distributions n = re.findall( - "TSS2_RC\s+Tss2_MU_BYTE_Marshal\(.+?\);", s, re.DOTALL | re.MULTILINE + r"TSS2_RC\s+Tss2_MU_BYTE_Marshal\(.+?\);", s, re.DOTALL | re.MULTILINE ) if len(n) > 1: s = re.sub( - "TSS2_RC\s+Tss2_MU_BYTE_Marshal\(.+?\);", "", s, 1, re.DOTALL | re.MULTILINE + r"TSS2_RC\s+Tss2_MU_BYTE_Marshal\(.+?\);", "", s, 1, re.DOTALL | re.MULTILINE ) n = re.findall( - "TSS2_RC\s+Tss2_MU_BYTE_Unmarshal\(.+?\);", s, re.DOTALL | re.MULTILINE + r"TSS2_RC\s+Tss2_MU_BYTE_Unmarshal\(.+?\);", s, re.DOTALL | re.MULTILINE ) if len(n) > 1: s = re.sub( - "TSS2_RC\s+Tss2_MU_BYTE_Unmarshal\(.+?\);", + r"TSS2_RC\s+Tss2_MU_BYTE_Unmarshal\(.+?\);", "", s, 1, @@ -442,7 +442,7 @@ def prepare( if __name__ == "__main__": if len(sys.argv) != 3: - print("Usage: {0} ".format(sys.argv[0])) + print(f"Usage: {sys.argv[0]} ") exit(1) build_fapi = pkgconfig.installed("tss2-fapi", ">=3.0.0") diff --git a/setup.py b/setup.py index 1b5f5135..9fa1ece8 100644 --- a/setup.py +++ b/setup.py @@ -21,8 +21,7 @@ class type_generator(build_ext): - cares = set( - ( + cares = { "TPM2_ALG_ID", "TPM2_ST", "TPM2_ECC_CURVE", @@ -40,8 +39,7 @@ class type_generator(build_ext): "TPM2_GENERATED", "ESYS_TR", "TSS2_POLICY_PCR_SELECTOR", - ) - ) + } type_mapping = { "TPM2_ALG_ID": "TPM2_ALG", @@ -268,11 +266,11 @@ def run(self): if not self.dry_run: self.mkpath(os.path.dirname(p)) - with open(p, "wt") as tf: + with open(p, "w") as tf: tf.seek(0) tf.truncate(0) tf.write(mout) - with open(vp, "wt") as vf: + with open(vp, "w") as vf: vf.seek(0) vf.truncate(0) vf.write(vout) diff --git a/src/tpm2_pytss/TCTI.py b/src/tpm2_pytss/TCTI.py index 360e5280..4169fb6e 100644 --- a/src/tpm2_pytss/TCTI.py +++ b/src/tpm2_pytss/TCTI.py @@ -10,7 +10,7 @@ from typing import Optional, Tuple, Union -class PollData(object): +class PollData: """Initialize a PollData object with OS specific details. Initialize a PollData object that holds all OS specific state and metadata information diff --git a/src/tpm2_pytss/TSS2_Exception.py b/src/tpm2_pytss/TSS2_Exception.py index 309e25db..df736c32 100644 --- a/src/tpm2_pytss/TSS2_Exception.py +++ b/src/tpm2_pytss/TSS2_Exception.py @@ -13,7 +13,7 @@ def __init__(self, rc: Union["TSS2_RC", "TPM2_RC", int]): rc = TSS2_RC(rc) errmsg = ffi.string(lib.Tss2_RC_Decode(rc)).decode() - super(TSS2_Exception, self).__init__(f"{errmsg}") + super().__init__(f"{errmsg}") self._rc = rc self._handle = 0 diff --git a/src/tpm2_pytss/encoding.py b/src/tpm2_pytss/encoding.py index 2b755b7a..8b35a9f1 100644 --- a/src/tpm2_pytss/encoding.py +++ b/src/tpm2_pytss/encoding.py @@ -83,7 +83,7 @@ import warnings -class base_encdec(object): +class base_encdec: """Base encoder/decoder for TPM types Args: @@ -1580,7 +1580,7 @@ def decode_tpms_pcr_selection( def decode_tpms_tagged_property( self, dst: TPMS_TAGGED_PROPERTY, src: Dict[str, Any] ) -> TPMS_TAGGED_PROPERTY: - pmap = dict([(v, k) for k, v in self._build_pt_map().items()]) + pmap = {v: k for k, v in self._build_pt_map().items()} ps, v = src.popitem() dst.property = pmap[ps] if dst.property == TPM2_PT.PERMANENT: diff --git a/src/tpm2_pytss/policy.py b/src/tpm2_pytss/policy.py index cc6beadf..a1d4b7f2 100644 --- a/src/tpm2_pytss/policy.py +++ b/src/tpm2_pytss/policy.py @@ -387,7 +387,7 @@ def _policy_cb_exec_polaction(action, userdata): return TPM2_RC.SUCCESS -class policy(object): +class policy: """Initialize policy object. Args: diff --git a/src/tpm2_pytss/tsskey.py b/src/tpm2_pytss/tsskey.py index 5ad216f9..2297c911 100644 --- a/src/tpm2_pytss/tsskey.py +++ b/src/tpm2_pytss/tsskey.py @@ -108,7 +108,7 @@ def set(self, value): self._trailer = b"" -class TSSPrivKey(object): +class TSSPrivKey: """TSSPrivKey is class to create/load keys for/from tpm2-tss-engine / tpm2-openssl. Note: diff --git a/src/tpm2_pytss/types.py b/src/tpm2_pytss/types.py index b155e6de..0425626c 100644 --- a/src/tpm2_pytss/types.py +++ b/src/tpm2_pytss/types.py @@ -68,7 +68,7 @@ class TPM2_HANDLE(int): pass -class TPM_OBJECT(object): +class TPM_OBJECT: """ Abstract Base class for all TPM Objects. Not suitable for direct instantiation.""" def __init__(self, _cdata=None, **kwargs): @@ -336,7 +336,7 @@ def __eq__(self, value): return b == value -class TPML_Iterator(object): +class TPML_Iterator: """ Iterator class for iterating over TPML data types. This class is used in enumerated for loops, such as: diff --git a/test/TSS2_BaseTest.py b/test/TSS2_BaseTest.py index 429e0e85..7f9efc74 100644 --- a/test/TSS2_BaseTest.py +++ b/test/TSS2_BaseTest.py @@ -14,7 +14,7 @@ from tpm2_pytss import * -class BaseTpmSimulator(object): +class BaseTpmSimulator: def __init__(self): self.tpm = None self._port = None @@ -26,7 +26,7 @@ def ready(port): def start(self): logger = logging.getLogger("DEBUG") - logger.debug('Setting up simulator: "{}"'.format(self.exe)) + logger.debug(f'Setting up simulator: "{self.exe}"') tpm = None for _ in range(0, 20): @@ -89,13 +89,13 @@ def _start(self, port): "socket", "--tpm2", "--server", - "port={}".format(port), + f"port={port}", "--ctrl", - "type=tcp,port={}".format(port + 1), + f"type=tcp,port={port + 1}", "--flags", "not-need-init", "--tpmstate", - "dir={}".format(self.working_dir.name), + f"dir={self.working_dir.name}", ] tpm = subprocess.Popen(cmd) @@ -129,7 +129,7 @@ def _start(self, port): cwd = os.getcwd() os.chdir(self.working_dir.name) try: - cmd = ["tpm_server", "-rm", "-port", "{}".format(port)] + cmd = ["tpm_server", "-rm", "-port", f"{port}"] tpm = subprocess.Popen(cmd) return tpm @@ -150,7 +150,7 @@ def get_tcti(self): return TCTILdr("mssim", f"port={self._port}") -class TpmSimulator(object): +class TpmSimulator: SIMULATORS = [ SwtpmSimulator, diff --git a/test/test_crypto.py b/test/test_crypto.py index b8a2a4e9..e4889db0 100644 --- a/test/test_crypto.py +++ b/test/test_crypto.py @@ -873,7 +873,7 @@ def test_verify_signature_bad(self): hsig = TPMT_SIGNATURE(sigAlg=TPM2_ALG.HMAC) with self.assertRaises(ValueError) as e: - crypto._verify_signature(hsig, str("not bytes"), b"1234") + crypto._verify_signature(hsig, "not bytes", b"1234") self.assertEqual( str(e.exception), f"bad key type for {TPM2_ALG.HMAC}, expected bytes, got str", @@ -888,7 +888,7 @@ def test_verify_signature_bad(self): badecc = TPMT_SIGNATURE(sigAlg=TPM2_ALG.ECDSA) with self.assertRaises(ValueError) as e: - crypto._verify_signature(badecc, str("bad"), b"1234") + crypto._verify_signature(badecc, "bad", b"1234") self.assertEqual( str(e.exception), f"bad key type for {TPM2_ALG.ECDSA}, expected ECC public key, got str", @@ -904,7 +904,7 @@ def test_verify_signature_bad(self): badrsa = TPMT_SIGNATURE(sigAlg=TPM2_ALG.RSAPSS) with self.assertRaises(ValueError) as e: - crypto._verify_signature(badrsa, str("bad"), b"1234") + crypto._verify_signature(badrsa, "bad", b"1234") self.assertEqual( str(e.exception), f"bad key type for {TPM2_ALG.RSAPSS}, expected RSA public key, got str", diff --git a/test/test_types.py b/test/test_types.py index 35d7a35e..5d5e5d68 100644 --- a/test/test_types.py +++ b/test/test_types.py @@ -1295,7 +1295,7 @@ def test_TPMS_SENSITIVE_CREATE_with_string(self): x = TPMS_SENSITIVE_CREATE(userAuth="password") p = str(x.userAuth) - self.assertEqual(p, binascii.hexlify("password".encode()).decode()) + self.assertEqual(p, binascii.hexlify(b"password").decode()) def test_TPM2B_SIMPLE_OBJECT(self): bob = b"bunchofbytes"