Skip to content
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

custom PYTHONPATH may break apps embedding Python #59161

Closed
jankratochvil mannequin opened this issue May 29, 2012 · 16 comments
Closed

custom PYTHONPATH may break apps embedding Python #59161

jankratochvil mannequin opened this issue May 29, 2012 · 16 comments
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@jankratochvil
Copy link
Mannequin

jankratochvil mannequin commented May 29, 2012

BPO 14956
Nosy @brettcannon, @ncoghlan, @vstinner, @merwok, @bitdancer, @davidmalcolm, @meadori, @Romain-Geissler-1A

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2019-05-27.14:51:32.617>
created_at = <Date 2012-05-29.19:56:51.840>
labels = ['type-bug', 'docs']
title = 'custom PYTHONPATH may break apps embedding Python'
updated_at = <Date 2019-05-27.14:51:32.615>
user = 'https://bugs.python.org/jankratochvil'

bugs.python.org fields:

activity = <Date 2019-05-27.14:51:32.615>
actor = 'vstinner'
assignee = 'docs@python'
closed = True
closed_date = <Date 2019-05-27.14:51:32.617>
closer = 'vstinner'
components = ['Documentation']
creation = <Date 2012-05-29.19:56:51.840>
creator = 'jankratochvil'
dependencies = []
files = []
hgrepos = []
issue_num = 14956
keywords = []
message_count = 16.0
messages = ['161906', '161907', '161908', '161909', '161911', '161912', '161913', '161918', '161919', '161955', '161957', '161959', '161970', '194137', '328727', '343633']
nosy_count = 11.0
nosy_names = ['brett.cannon', 'ncoghlan', 'vstinner', 'eric.araujo', 'r.david.murray', 'dmalcolm', 'meador.inge', 'jankratochvil', 'docs@python', 'Kevin.Harms', 'RomainGeissler1A']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue14956'
versions = ['Python 2.7', 'Python 3.2', 'Python 3.3']

@jankratochvil
Copy link
Mannequin Author

jankratochvil mannequin commented May 29, 2012

People use custom Python builds setting PYTHONHOME and PYTHONPATH to these custom builds. This may be expected to break running system Python binary but it also unexpectedly breaks various applications which also embed Python:

$ echo foo >site.py; export PYTHONPATH=.
$ gdb
Traceback (most recent call last):
  File "./site.py", line 1, in <module>
    foo
NameError: name 'foo' is not defined
$ _

It is not obvious to the user who even already forgot about custom PYTHONPATH why GDB breaks.

Standard Fedora applications may link with system libpython incompatible with the PYTHONHOME/PYTHONPATH files for the other Python version. Python does not keep the scripts too compatible across Python versions.

This issues was discussed first for GDB upstream:
[RFA] ignore PYTHONHOME environment variable.
http://sourceware.org/ml/gdb-patches/2010-11/msg00328.html
http://sourceware.org/ml/gdb-patches/2010-12/msg00214.html
http://sourceware.org/ml/gdb-patches/2011-01/msg00307.html
http://sourceware.org/ml/gdb-patches/2012-05/msg00710.html
(first mail of the thread in each month)
Then also in Fedora:
https://fedorahosted.org/fesco/ticket/858
This Python issue would not be filed by the user if it gets resolved:
bpo-12140

Exactly the same problem exists for many other apps, yum for all

$ echo foo >site.py; PYTHONPATH=. yum list bash
Traceback (most recent call last):
  File "./site.py", line 1, in <module>
    foo
NameError: name 'foo' is not defined
$ _

Various proposal have been, applicable possibly in some combination together:

  • Ignore PYTHONHOME/PYTHONPATH for embedded applications. (dot)
  • Ignore PYTHONHOME, use only GDB_PYTHONHOME (if set). This proposal is not applicable for all applications in general.
  • Print warning if PYTHONHOME/PYTHONPATH is set.
  • Recover from Python startup failure and suppress the environment vars.
    or Recover from Python startup failure and disable the Python support, if possible.
  • Should the special care affect PYTHONHOME? Differently for PYTHONPATH? What about PYTHONVERBOSE and other PYTHON* settings?

How should get PYTHONHOME/PYTHONPATH inherited into child processes, if any (debuggees in the GDB case).

Another question is whether the behavior should be the same if upstream application has been linked with non-system libpython location.

@jankratochvil jankratochvil mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels May 29, 2012
@merwok
Copy link
Member

merwok commented May 29, 2012

An alternate idea would be to recommend more strongly in the doc that people should not shadow standard library module names (in your example, site).

@jankratochvil
Copy link
Mannequin Author

jankratochvil mannequin commented May 29, 2012

This site.py is only an example how it can happen. In reality it is due to .py files intended for incompatible version of Python. I am not a Python programmer to have some appropriate incompatible code at hand.

@merwok
Copy link
Member

merwok commented May 29, 2012

This site.py is only an example how it can happen.
site is special, as it’s one of the modules imported during Python startup. Can you reproduce the bug with the same file named os.py? (I expect you can.) Can you reproduce with spam.py? (I think you can’t.)

@merwok merwok changed the title custom PYTHONPATH breaks Python-embedded apps custom PYTHONPATH may break apps embedding Python May 29, 2012
@bitdancer
Copy link
Member

I think it is up to each embedding application to decide if it wants to respect the environment variables or not. I don't think that's a decision that core can make.

Now, if we aren't providing an easy way for an embedding application to make that decision, that might be something we could fix. But I suspect we are (I don't know the C API well enough to say for sure).

@jankratochvil
Copy link
Mannequin Author

jankratochvil mannequin commented May 29, 2012

I fully agree with site.py/os.py/spam.py but I find it offtopic for this Issue.

I do not find too important if some unsetenv/setenv gets called by the app or by libpython. But the rules should apply for every embedded app the same, the Fedora ticket is suggesting new PEP (Informational) for it.

@bitdancer
Copy link
Member

An informational PEP sounds like exactly what I said: each application needs to decide. The PEP would provide the information on which to base that decision, and suggestions for how to do it.

An informational PEP on best practices for embedding sounds like a great idea to me. I hope someone will write one.

@ncoghlan
Copy link
Contributor

It wouldn't be a PEP, it would be an addition to the embedding docs:
http://docs.python.org/extending/embedding.html

@ncoghlan
Copy link
Contributor

For the more general breakage due to PYTHONHOME and PYTHONPATH, yeah, global environment variables are bad, particularly when an OS relies on tools written in (or embedding) Python.

That's the reason virtualenv (and 3.3's forthcoming venv) are a preferred alternative - they give you a space to play in that shouldn't break your system Python or apps that embed it.

@ncoghlan ncoghlan added docs Documentation in the Doc dir and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) labels May 29, 2012
@merwok
Copy link
Member

merwok commented May 30, 2012

I fully agree with site.py/os.py/spam.py but I find it offtopic for this Issue.

I don’t understand this message :) There is nothing to agree with or judge on or off-topic; I was trying to understand the root of the bug and really asking you in good faith to try two things to see if my idea was right.

@jankratochvil
Copy link
Mannequin Author

jankratochvil mannequin commented May 30, 2012

While it should be documented this is not only a docs issue. It should be solved in some way during runtime.

@bitdancer
Copy link
Member

No it shouldn't. As mentioned in the Fedora thread you linked, this is no different than the user setting LD_LIBRARY_PATH to something that screws up a system installed program.

@ncoghlan
Copy link
Contributor

If we don't expose the mechanism behind -E to embedding applications via
the C API, then a non-docs change may be needed. However, writing (or at
least trying to write) the relevant docs is a good way to check whether or
not that is the case.

@KevinHarms
Copy link
Mannequin

KevinHarms mannequin commented Aug 1, 2013

As far as I can tell, this issue is not resolved. It would be very nice for the C API to have the equivalent for -E for security reasons. I would like to make a setgid binary and do not want users to be able to set PYTHON* variables to force loading of custom code.

@Romain-Geissler-1A
Copy link
Mannequin

Romain-Geissler-1A mannequin commented Oct 28, 2018

Hi,

Just updating this 6 year old bug report. The last comment says it's not possible to ignore environment when using the C-API. I don't know back then, but today it is possible. I have just built gdb 8.2 against python 3.7.1, and patched gdb with this simple patch, which apparently works:

--- gdb/python/python.c
+++ gdb/python/python.c
@@ -1726,6 +1726,9 @@
#endif
#endif

+  // Force using the toolchain python without being troubled by $PYTHONHOME or $PYTHONPATH.
+  Py_IgnoreEnvironmentFlag = 1;
+
   Py_Initialize ();
   PyEval_InitThreads ();

Cheers,
Romain

@vstinner
Copy link
Member

This issue has been fixed in 36763 by the PEP-587 which provides a new "Isolated Configuration" and a way to tune the Python configuration without impacting subprocesses. For example, the Isolated Configuration ignores environment variables by default.

In Python 3.7 and older, you can set Py_IgnoreEnvironmentFlag to 1 (the flag exists also in Python 2.7), or even set Py_IsolatedFlag to 1 (new in Python 3.4).

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants