From ed95b92426e6b6f75a5321a76aac5bb6f3c8150e Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Sun, 27 Feb 2022 00:52:29 +0100 Subject: [PATCH 1/3] Replace `toml` with `tomli` --- bandit/core/config.py | 12 ++++++------ setup.cfg | 2 +- test-requirements.txt | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/bandit/core/config.py b/bandit/core/config.py index 7c259bcc0..928c1231e 100644 --- a/bandit/core/config.py +++ b/bandit/core/config.py @@ -7,9 +7,9 @@ import yaml try: - import toml + import tomli except ImportError: - toml = None + tomli = None from bandit.core import constants from bandit.core import extension_loader @@ -34,14 +34,14 @@ def __init__(self, config_file=None): if config_file: try: - f = open(config_file) + f = open(config_file, "rb") except OSError: raise utils.ConfigError( "Could not read config file.", config_file ) if config_file.endswith(".toml"): - if toml is None: + if tomli is None: raise utils.ConfigError( "toml parser not available, reinstall with toml extra", config_file, @@ -49,8 +49,8 @@ def __init__(self, config_file=None): try: with f: - self._config = toml.load(f)["tool"]["bandit"] - except toml.TomlDecodeError as err: + self._config = tomli.load(f)["tool"]["bandit"] + except tomli.TOMLDecodeError as err: LOG.error(err) raise utils.ConfigError("Error parsing file.", config_file) else: diff --git a/setup.cfg b/setup.cfg index f00237e92..f42577e5e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,7 +30,7 @@ project_urls = yaml = PyYAML toml = - toml + tomli>=1.1.0 [entry_points] console_scripts = diff --git a/test-requirements.txt b/test-requirements.txt index b2e3c379f..b6b14a141 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -7,6 +7,6 @@ flake8>=4.0.0 # Apache-2.0 stestr>=2.5.0 # Apache-2.0 testscenarios>=0.5.0 # Apache-2.0/BSD testtools>=2.3.0 # MIT -toml # MIT +tomli>=1.1.0 # MIT beautifulsoup4>=4.8.0 # MIT pylint==1.9.4 # GPLv2 From d40fed85f0c9e33ff3593fdfcf3a3651b545bda7 Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Tue, 8 Mar 2022 21:31:00 +0100 Subject: [PATCH 2/3] Only require `tomli` on Python < 3.11 --- bandit/core/config.py | 18 +++++++++++------- setup.cfg | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/bandit/core/config.py b/bandit/core/config.py index 928c1231e..236f357c5 100644 --- a/bandit/core/config.py +++ b/bandit/core/config.py @@ -3,13 +3,17 @@ # # SPDX-License-Identifier: Apache-2.0 import logging +import sys import yaml -try: - import tomli -except ImportError: - tomli = None +if sys.version_info >= (3, 11): + import tomllib +else: + try: + import tomli as tomllib + except ImportError: + tomllib = None from bandit.core import constants from bandit.core import extension_loader @@ -41,7 +45,7 @@ def __init__(self, config_file=None): ) if config_file.endswith(".toml"): - if tomli is None: + if tomllib is None: raise utils.ConfigError( "toml parser not available, reinstall with toml extra", config_file, @@ -49,8 +53,8 @@ def __init__(self, config_file=None): try: with f: - self._config = tomli.load(f)["tool"]["bandit"] - except tomli.TOMLDecodeError as err: + self._config = tomllib.load(f)["tool"]["bandit"] + except tomllib.TOMLDecodeError as err: LOG.error(err) raise utils.ConfigError("Error parsing file.", config_file) else: diff --git a/setup.cfg b/setup.cfg index f42577e5e..47badb973 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,7 +30,7 @@ project_urls = yaml = PyYAML toml = - tomli>=1.1.0 + tomli>=1.1.0; python_version < "3.11" [entry_points] console_scripts = From 3d30e5d46a6924c7e0b3acbebfe3c60773bdf151 Mon Sep 17 00:00:00 2001 From: Eric Brown Date: Fri, 25 Mar 2022 15:25:03 -0700 Subject: [PATCH 3/3] Update test-requirements.txt --- test-requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-requirements.txt b/test-requirements.txt index b6b14a141..6c8c57e7f 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -7,6 +7,6 @@ flake8>=4.0.0 # Apache-2.0 stestr>=2.5.0 # Apache-2.0 testscenarios>=0.5.0 # Apache-2.0/BSD testtools>=2.3.0 # MIT -tomli>=1.1.0 # MIT +tomli>=1.1.0;python_version<"3.11" # MIT beautifulsoup4>=4.8.0 # MIT pylint==1.9.4 # GPLv2