From ebf912c8f1af2557778ece973f3e471d287f303c Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 6 Aug 2018 17:05:58 +0200 Subject: [PATCH] src/trezor/crypto: introduce SecureContext usage: from trezor.crypto import SecureContext with SecureContext() as sc: sc.var1 = ... sc.var2 = ... SecureContext will call destructors of all variables assigned to sc in the block. It will also call gc.collect() --- src/trezor/crypto/__init__.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/trezor/crypto/__init__.py b/src/trezor/crypto/__init__.py index 7eb67a3d99..254bc3f3d9 100644 --- a/src/trezor/crypto/__init__.py +++ b/src/trezor/crypto/__init__.py @@ -1,3 +1,5 @@ +from gc import collect + from trezorcrypto import ( # noqa: F401 bip32, bip39, @@ -8,3 +10,18 @@ from trezorcrypto import ( # noqa: F401 random, rfc6979, ) + + +class SecureContext: + def __init__(self): + pass + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + for k in self.__dict__: + o = getattr(self, k) + if hasattr(o, "__del__"): + o.__del__() + collect()