From ccc012674fc5707145aadf035440f6d63c9d5bbc Mon Sep 17 00:00:00 2001 From: SomberNight Date: Thu, 1 Jun 2023 17:34:32 +0000 Subject: [PATCH] unconditionally raise ImportError if asserts are disabled I have reconsidered and now think that we should always hard-fail if asserts asserts are disabled. It is just easier to reason about the code knowing that asserts are evaluated. If an end-user or library user has a concrete use case where this is a problem, please open an issue and let us know. follow-up 0f541be6f11a372d202c99476e6d051184006bba 0e5464ca13ce2f993107b4a293982ea4bfc434b5 --- electrum/__init__.py | 14 ++++++++++++-- run_electrum | 13 ------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/electrum/__init__.py b/electrum/__init__.py index f806c548b..f4c355028 100644 --- a/electrum/__init__.py +++ b/electrum/__init__.py @@ -35,5 +35,15 @@ from .logging import get_logger __version__ = ELECTRUM_VERSION _logger = get_logger(__name__) -if not __debug__: - _logger.warning(f"__debug__ is False. running with asserts disabled!") + + +# Ensure that asserts are enabled. For sanity and paranoia, we require this. +# Code *should not rely* on asserts being enabled. In particular, safety and security checks should +# always explicitly raise exceptions. However, this rule is mistakenly broken occasionally... +try: + assert False +except AssertionError: + pass +else: + raise ImportError("Running with asserts disabled. Refusing to continue. Exiting...") + diff --git a/run_electrum b/run_electrum index 36d9817b9..79eddd15f 100755 --- a/run_electrum +++ b/run_electrum @@ -63,19 +63,6 @@ if is_pyinstaller: # causes ImportErrors and other runtime failures). (see #4072) _file = open(sys.executable, 'rb') -if is_binary_distributable: - # Ensure that asserts are enabled. - # Code *should not rely* on asserts being enabled. In particular, safety and security checks should - # always explicitly raise exceptions. However, this rule is mistakenly broken occasionally... - # In case we are a binary build, we know for a fact that we want the asserts, so enforce them. - # When running from source, defer to the user. (a warning is logged in __init__.py) - try: - assert False - except AssertionError: - pass - else: - sys.exit("Error: Running with asserts disabled, in a binary distributable! Please check build settings.") - def check_imports(): # pure-python dependencies need to be imported here for pyinstaller