From ada8b6d1b1b02ae7c38f161c2a0ad866559fe18b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 6 May 2022 01:34:11 +0200 Subject: gh-57684: Add -P cmdline option and PYTHONSAFEPATH env var (#31542) Add the -P command line option and the PYTHONSAFEPATH environment variable to not prepend a potentially unsafe path to sys.path. * Add sys.flags.safe_path flag. * Add PyConfig.safe_path member. * Programs/_bootstrap_python.c uses config.safe_path=0. * Update subprocess._optim_args_from_interpreter_flags() to handle the -P command line option. * Modules/getpath.py sets safe_path to 1 if a "._pth" file is present. --- Python/sysmodule.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index d5a62fc12b..edd1d1f23f 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -2479,6 +2479,7 @@ static PyStructSequence_Field flags_fields[] = { {"dev_mode", "-X dev"}, {"utf8_mode", "-X utf8"}, {"warn_default_encoding", "-X warn_default_encoding"}, + {"safe_path", "-P"}, {0} }; @@ -2486,7 +2487,7 @@ static PyStructSequence_Desc flags_desc = { "sys.flags", /* name */ flags__doc__, /* doc */ flags_fields, /* fields */ - 16 + 17 }; static int @@ -2526,6 +2527,7 @@ set_flags_from_config(PyInterpreterState *interp, PyObject *flags) SetFlagObj(PyBool_FromLong(config->dev_mode)); SetFlag(preconfig->utf8_mode); SetFlag(config->warn_default_encoding); + SetFlagObj(PyBool_FromLong(config->safe_path)); #undef SetFlagObj #undef SetFlag return 0; -- cgit v1.2.1