From 0bf277a985a7eee336fb3971750be1b4f3f4b1ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20La=C3=ADns?= Date: Thu, 17 Jun 2021 15:08:00 +0100 Subject: [PATCH] main: remove import side-effects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filipe LaĆ­ns --- CHANGELOG.rst | 2 ++ src/build/__main__.py | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 75db4707..abfc0177 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,9 +15,11 @@ Breaking Changes - Binary distributions are now built via the sdist by default in the CLI (`PR #304`_, Fixes `#257`_) - ``python -m build`` will now build a sdist, extract it, and build a wheel from the source - As a side-effect of `PR #304`_, ``build.__main__.build_package`` no longer does CLI error handling (print nice message and exit the program) +- Importing ``build.__main__`` no longer has any side-effects, it no longer overrides ``warnings.showwarning`` or runs ``colorama.init`` on import (`PR #312`_) .. _PR #303: https://github.com/pypa/build/pull/303 .. _PR #304: https://github.com/pypa/build/pull/304 +.. _PR #312: https://github.com/pypa/build/pull/312 .. _#257: https://github.com/pypa/build/issues/257 .. _#301: https://github.com/pypa/build/issues/301 diff --git a/src/build/__main__.py b/src/build/__main__.py index efd8745f..179b37ab 100644 --- a/src/build/__main__.py +++ b/src/build/__main__.py @@ -22,14 +22,6 @@ from build.env import IsolatedEnvBuilder -try: - import colorama -except ImportError: - pass -else: - colorama.init() # fix colors on windows - - __all__ = ['build', 'main', 'main_parser'] @@ -51,7 +43,15 @@ def _showwarning(message, category, filename, lineno, file=None, line=None): # print('{} {}'.format(prefix, str(message))) -warnings.showwarning = _showwarning +def _setup_cli(): # type: () -> None + warnings.showwarning = _showwarning + + try: + import colorama + except ImportError: + pass + else: + colorama.init() # fix colors on windows def _error(msg, code=1): # type: (str, int) -> None # pragma: no cover @@ -264,6 +264,7 @@ def main(cli_args, prog=None): # type: (List[str], Optional[str]) -> None # no :param cli_args: CLI arguments :param prog: Program name to show in help text """ + _setup_cli() parser = main_parser() if prog: parser.prog = prog