From eaa53f76fa458d979967e80e07d49e5dfd0e9db8 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 24 May 2019 18:16:46 +0200 Subject: [PATCH] bpo-32573: sys.argv always exists and is non-empty Ensure that sys.argv always exists and is non-empty. When Python is embedded in an application and ``argv`` is not set, use ['']. --- Lib/test/test_embed.py | 2 +- .../2019-05-24-18-23-15.bpo-32573.LSe-jY.rst | 2 ++ Modules/main.c | 15 +++++++++++---- 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-05-24-18-23-15.bpo-32573.LSe-jY.rst diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py index de3c30340c6dba3..501921d098aeb62 100644 --- a/Lib/test/test_embed.py +++ b/Lib/test/test_embed.py @@ -287,7 +287,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): 'coerce_c_locale_warn': 0, 'program_name': './_testembed', - 'argv': [], + 'argv': [''], 'program': None, 'xoptions': [], diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-05-24-18-23-15.bpo-32573.LSe-jY.rst b/Misc/NEWS.d/next/Core and Builtins/2019-05-24-18-23-15.bpo-32573.LSe-jY.rst new file mode 100644 index 000000000000000..175de8a53e6f373 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-05-24-18-23-15.bpo-32573.LSe-jY.rst @@ -0,0 +1,2 @@ +Ensure that :data:`sys.argv` always exists and is non-empty. When Python is +embedded in an application and ``argv`` is not set, use ``['']``. diff --git a/Modules/main.c b/Modules/main.c index acc59c6c40a9975..0e5f807b979d286 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -2373,6 +2373,17 @@ _PyCoreConfig_Read(_PyCoreConfig *config) } } + /* ensure that sys.argv is always set and non-empty */ + if (config->argc < 0) { + config->argc = 0; + } + if (config->argc == 0) { + err = wstrlist_append(&config->argc, &config->argv, L""); + if (_Py_INIT_FAILED(err)) { + return err; + } + } + /* default values */ if (config->dev_mode) { if (config->faulthandler < 0) { @@ -2401,10 +2412,6 @@ _PyCoreConfig_Read(_PyCoreConfig *config) if (config->utf8_mode < 0) { config->utf8_mode = 0; } - if (config->argc < 0) { - config->argc = 0; - } - return _Py_INIT_OK(); }