From ca554f62c446c6cfa07b9bb56466992f5d7624be Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone <exarkun@twistedmatrix.com> Date: Thu, 23 Sep 2021 09:41:41 -0400 Subject: [PATCH] Make a safer way to check if an interface has an attribute --- src/_zkapauthorizer/foolscap.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/_zkapauthorizer/foolscap.py b/src/_zkapauthorizer/foolscap.py index 701a5ea..1478d76 100644 --- a/src/_zkapauthorizer/foolscap.py +++ b/src/_zkapauthorizer/foolscap.py @@ -141,6 +141,20 @@ def add_arguments(schema, kwargs): return modified_schema +def remoteinterface_hasattr(ri, name): + """ + :param InterfaceClass ri: A ``RemoteInterface`` to inspect. + :param str name: The name of an attribute. + + :return bool: ``True`` if and only if ``ri`` has the attribute named by + ``name``, ``False`` otherwise. + """ + try: + ri[name] + except KeyError: + return False + return True + class RIPrivacyPassAuthorizedStorageServer(RemoteInterface): """ @@ -162,7 +176,7 @@ class RIPrivacyPassAuthorizedStorageServer(RemoteInterface): add_lease = add_passes(RIStorageServer["add_lease"]) - if "renew_lease" in RIStorageServer: + if remoteinterface_hasattr(RIStorageServer, "renew_lease"): # Tahoe-LAFS 1.16.0 drops renew_lease from the interface. Do likewise # here, if we discover we have a version of Tahoe that has done so. # If Tahoe has dropped this method then nothing in Tahoe is going to -- GitLab