-
Notifications
You must be signed in to change notification settings - Fork 543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an option to opt-out of PYTHONSAFEPATH #2060
Comments
Yeah, we should respect an outer env setting at least. Using Anyways, at the least, we can update the logic so it doesn't always force the value. |
I would lean towards a dedicated attribute which also clearly documents that this environment variable is set by default, but allowing it to be unset via the
Do you mean |
Any thoughts @aignas? If we can agree on disabling via |
How about using |
That sounds like quite a good solution. I am just wondering how this would interact with things like the multiprocessing library which starts up other interpreters. I guess arguments such as |
Yes. So given
The issue with dedicated attributes for these sort of settings is the API surface is very large because it grows to encompass all the env vars and interpreter flags, of which there are many: https://docs.python.org/3/using/cmdline.html I am +1 on a general
This does sound appealing. Actually, that sounds necessary if one py_binary has another py_binary as a data dependency and they have different settings. It'll be a bit annoying to code since the shell code will need to reconcile the outer environment, the
Multiprocessing will propagate the interpreter args (sys.flags) either directly when spawn is used, or implicitly when fork is used. For environment variables, it'll inherit them. So, insofar as multiprocessing is concerned, these settings will propagate regardless. |
By default, PYTHONSAFEPATH is enabled to help prevent imports being found where they shouldn't be. However, this behavior can't be disabled, which makes it harder to use a py_binary when the non-safe path behavior is explicitly desired. To fix, the bootstrap now respects the caller environment's PYTHONSAFEPATH environment variable, if set. This allows the callers to set `PYTHONSAFEPATH=` (empty string) to override the default behavior that enables it. Fixes #2060
Reopening because: The PR fixes it for bootstrap=script, but that isn't the default yet. While it'll become the default in a couple releases, it won't be used for Windows. Also, I suspect that zip invocations going through @allsey87 if you want to send a PR to update https://github.com/bazelbuild/rules_python/blob/main/python/private/python_bootstrap_template.txt, SGTM. All it probably requires is modifying L500 to conditionally set PYTHONSAFEPATH based on the existing environment. You can probably copy/paste the I probably won't have time to fix this myself before next release, but can probably fit in reviewing a PR. |
I'm travelling at the moment so the earliest I can get to this will be on the 29th |
@rickeylev looking into this now but I have stumbled upon what might be another bug. It seems the environment variables set via the diff --git a/python/private/python_bootstrap_template.txt b/python/private/python_bootstrap_template.txt
index 0f9c90b..083b880 100644
--- a/python/private/python_bootstrap_template.txt
+++ b/python/private/python_bootstrap_template.txt
@@ -497,7 +497,8 @@ def Main():
# Don't prepend a potentially unsafe path to sys.path
# See: https://docs.python.org/3.11/using/cmdline.html#envvar-PYTHONSAFEPATH
- new_env['PYTHONSAFEPATH'] = '1'
+ if os.environ.get('PYTHONSAFEPATH') != '':
+ new_env['PYTHONSAFEPATH'] = '1'
main_filename = os.path.join(module_space, main_rel_path)
main_filename = GetWindowsPathWithUNCPrefix(main_filename)
""" Rule for building meson from source. """
load("@rules_python//python:defs.bzl", "py_binary")
def meson_tool(name, main, data, requirements = [], **kwargs):
py_binary(
name = name,
srcs = [main],
data = data,
deps = requirements,
python_version = "PY3",
main = main,
env = {
"PYTHONSAFEPATH": ""
},
**kwargs
) Is this the expected behaviour? |
This is why I pointed to the |
This all sounds a bit hacky to me. Considering that
I see where you are coming from, but would say that In my opinion, either adding the attribute |
Are they really, though? It's "merely" a convention today, and I couldn't find anything authoritative saying they must be ignored outside of run/test. Over the years, I've seen people (myself included) confused and annoyed that the In any case, we're skirting this design question for now by just making the startup respect the existing environment variable, if present. It will already respect other Python environment variables (e.g. PYTHONHASHSEED, PYTHONIOENCODING, etc), so it makes it a bit more consistent in that regard. (Whether they're set via the I'm pretty strongly -1 on a dedicated attribute. There's simply too many other
I'd be fine with |
Well, I think this is actually an XY problem. What I actually need is to just stop |
🚀 feature request
Relevant Rules
py_binary
Description
py_binary
setsPYTHONSAFEPATH=1
which while being a sensible default does cause problems. Most notably, inrules_foreign_cc
wherepy_binary
is used to wrap up the Meson build system (written in Python). While this is not an issue for the build system itself, it is an issue when Meson runs compilers or build scripts that are written in Python. The issue here is that Meson is starting processes which inherit Meson's environment variables includingPYTHONSAFEPATH
. This often breaks scripts which rely on importing modules from the directory in which they are contained.Describe the solution you'd like
I would like an option on
py_binary
that allows me to opt out ofPYTHONSAFEPATH
.Describe alternatives you've considered
--action_env=PYTHONSAFEPATH=
: no effectenv
argument onpy_binary
as follows: no effectThe text was updated successfully, but these errors were encountered: