From 499343c44b3fa35342b1b2021a423e026aa05fc6 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 7 Jul 2025 12:30:56 +0300 Subject: [PATCH] gh-136355: Deprecate `-b` and `-bb` CLI flags in 3.15 --- Doc/using/cmdline.rst | 10 ++++++++ Doc/whatsnew/3.15.rst | 12 +++++++++ Lib/test/test_cmd_line.py | 25 +++++++++++++++++-- ...-07-07-12-24-00.gh-issue-136355.MTcA8j.rst | 2 ++ Python/initconfig.c | 6 +++++ 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-07-07-12-24-00.gh-issue-136355.MTcA8j.rst diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index cad49e2deeb46f..22cad9ea5780dc 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -254,6 +254,16 @@ Miscellaneous options .. versionchanged:: 3.5 Affects also comparisons of :class:`bytes` with :class:`int`. + .. deprecated-removed:: 3.15 3.17 + + Deprecate :option:`-b` and :option:`-bb` + and schedule them for removal in Python 3.17. + They were mainly a transition helpers for Python2 -> Python3 era. + In 3.17 no :exc:`BytesWarning` won't be raised for these cases. + If you want to check for the same things in the future, + use any type-checker of your choice. + + .. option:: -B If given, Python won't try to write ``.pyc`` files on the diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index 706a816f888b30..58b9509bcd55dd 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -206,6 +206,18 @@ module_name Deprecated ========== +CLI +--- + +* Deprecate :option:`-b` and :option:`!-bb` + and schedule them for removal in Python 3.17. + They were mainly a transition helpers for Python2 -> Python3 era. + In 3.17 no :exc:`BytesWarning` won't be raised for these cases. + If you want to check for the same things in the future, + use any type-checker of your choice. + + (Contributed by Nikita Sobolev in :gh:`136355`.) + hashlib ------- diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index c17d749d4a17ed..caec8b7ccf9e65 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -32,6 +32,12 @@ def _kill_python_and_exit_code(p): return data, returncode +b_deprecation_msg = ( + '-b option is deprecated since Python 3.15 ' + 'and will be removed in Python 3.17' +) + + class CmdLineTest(unittest.TestCase): def test_directories(self): assert_python_failure('.') @@ -706,7 +712,7 @@ def run_xdev(self, *args, check_exitcode=True, xdev=True): env=env) if check_exitcode: self.assertEqual(proc.returncode, 0, proc) - return proc.stdout.rstrip() + return self.maybe_remove_b_deprecation_msg(proc.stdout) @support.cpython_only def test_xdev(self): @@ -789,7 +795,22 @@ def check_warnings_filters(self, cmdline_option, envvar, use_pywarning=False): universal_newlines=True, env=env) self.assertEqual(proc.returncode, 0, proc) - return proc.stdout.rstrip() + return self.maybe_remove_b_deprecation_msg(proc.stdout) + + def maybe_remove_b_deprecation_msg(self, output): + return output.replace(b_deprecation_msg + '\n', '').rstrip() + + def test_b_deprecation_msg_stderr(self): + for arg in ['-b', '-bb']: + with self.subTest(arg=arg): + args = (sys.executable, arg, '-c', '') + proc = subprocess.run(args, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True) + self.assertEqual(proc.returncode, 0, proc) + self.assertEqual(proc.stdout, '') + self.assertEqual(proc.stderr.rstrip(), b_deprecation_msg) def test_warnings_filter_precedence(self): expected_filters = ("error::BytesWarning " diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-07-12-24-00.gh-issue-136355.MTcA8j.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-07-12-24-00.gh-issue-136355.MTcA8j.rst new file mode 100644 index 00000000000000..47c34840063abb --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-07-12-24-00.gh-issue-136355.MTcA8j.rst @@ -0,0 +1,2 @@ +Deprecate :option:`-b` and :option:`!-bb` and schedule them +for removal in the future versions of Python. diff --git a/Python/initconfig.c b/Python/initconfig.c index 71d7cfed5c44c1..093444a506681d 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -256,6 +256,7 @@ static const char usage_help[] = "\ Options (and corresponding environment variables):\n\ -b : issue warnings about converting bytes/bytearray to str and comparing\n\ bytes/bytearray with str or bytes with int. (-bb: issue errors)\n\ + deprecated since 3.15 and will be removed in 3.17\n\ -B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\ -c cmd : program passed in as string (terminates option list)\n\ -d : turn on parser debugging output (for experts only, only works on\n\ @@ -2944,6 +2945,11 @@ config_parse_cmdline(PyConfig *config, PyWideStringList *warnoptions, return _PyStatus_EXIT(0); case 'b': + if (!config->bytes_warning) { + fprintf(stderr, + "-b option is deprecated since Python 3.15 " + "and will be removed in Python 3.17\n"); + } config->bytes_warning++; break;