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

Fixes hostpython build with macOS venv #2159

Merged
merged 1 commit into from
Apr 29, 2020

Conversation

AndreMiras
Copy link
Member

Running the freshly compiled host python binary from another Python
process (e.g. sh module or os.system()) may get wrongly detected as
if it was ran directly from the venv.
This happens when pyvenv.cfg is present (e.g. venv/pyvenv.cfg).
It makes sysconfig.is_python_build() returns False instead of True
since it's using the compiled interpreter (e.g. ./build-dir/python).
https://docs.python.org/3/library/sysconfig.html#sysconfig.is_python_build
https://github.com/python/cpython/blob/v3.8.2/Lib/sysconfig.py#L127
This is because the sys._home attribute is used during the detection.
The issue was first seen in macOS venv which generates the pyvenv.cfg.
To compare both behaviours, try the following within and without a venv:

import os
os.system("./build-dir/python -E -c 'import sysconfig; print(sysconfig._sys_home)'")

One would return /usr/local/bin and the other None

Refs:

@AndreMiras AndreMiras requested a review from opacam April 28, 2020 22:06
@@ -764,7 +764,7 @@ def run_pymodules_install(ctx, modules, project_dir=None,
shprint(host_python, '-m', 'venv', 'venv')

# Prepare base environment and upgrade pip:
base_env = copy.copy(os.environ)
base_env = dict(copy.copy(os.environ))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has nothing to do with the macOS venv fix, but it suddenly started failing (develop branch included). So I included the fix in the PR but dedicated commit

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 but you created a separate PR right #2160? nevermind, it's fine to me!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I did separate it in case this PR would take longer to be reviewed/merged.
I've now merged the other one so I'll rebase and address some of the other comments you made, thanks!

@AndreMiras AndreMiras force-pushed the feature/fix_macos_venv_build branch 2 times, most recently from 9438b08 to b8f300f Compare April 29, 2020 06:14
cd testapps/on_device_unit_tests/ && \
python3 setup.py apk --sdk-dir $(ANDROID_SDK_HOME) --ndk-dir $(ANDROID_NDK_HOME) \
--arch=$($@_APP_ARCH)

clean:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Glad that we can remove this!

@@ -35,6 +35,8 @@ class Hostpython3Recipe(Recipe):
'''The default url to download our host python recipe. This url will
change depending on the python version set in attribute :attr:`version`.'''

patches = ('patches/pyconfig_detection.patch',)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the new patch working/needed for Python 3.7?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, I've only verified for 3.8 to be honest. I can apply it to 3.8 only to be safe

Comment on lines 55 to 59
name = 'python3'

patches = [
('patches/pyconfig_detection.patch'),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to patch python3 recipe as well? I understand that the problem is with hostpython and MacOs during build stage...:thinking:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I also thought, but then I saw the build failing with the exact same issue so I decided to add it there as well

opacam
opacam previously approved these changes Apr 29, 2020
Copy link
Member

@opacam opacam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, sure that macOs Users will be delighted with this.

Thanks!!! 😄

@rdbisme
Copy link

rdbisme commented Apr 29, 2020

This has been fixed also upstream, apparently: https://bugs.python.org/issue40261

[I went on trying to understand what was happening]

@AndreMiras
Copy link
Member Author

Very nice to see @rubendibattista !
So I could have saved a day of my life by just waiting 3.8.3 to be released 😂
Then it also means this patch needs to be applied to 3.8.2 only for now. Will update

@opacam
Copy link
Member

opacam commented Apr 29, 2020

Nice, so in Python 3.8.3 this should be solved 😄

¡¡¡thanks for the info!!!

@rdbisme
Copy link

rdbisme commented Apr 29, 2020

@AndreMiras at least for you was just one day! I spent one week after giving up... I reached that situation of crazyness where I was patching CPython directly. :)

@rdbisme
Copy link

rdbisme commented Apr 29, 2020

@opacam and 3.7.8

@rdbisme
Copy link

rdbisme commented Apr 29, 2020

However this is the change they're applying upstream: python/cpython@5765aca

Care to see if it solves the problem also here?

@opacam
Copy link
Member

opacam commented Apr 29, 2020

Maybe it will solve the problem, but I don't have a Mac to test...anyway...if you want to try, the following patch will modify hostpython/python recipes to use the branch you mentioned from CPython 😉

@AndreMiras
Copy link
Member Author

Opacam the problem with this approach is it hardcodes the version and people won't be able to use different versions. Plus I'd rather not rely on something not yet release like so.
@rubendibattista I haven't tested that patch you shared. Good thing it's an upstream one so it should be properly tested. Basically I would apply only Modules/main.c change.
I'll rebase this PR and address some of the comments and then create another PR where I apply Modules/main.c patch only and let's see if that compiles

Running the freshly compiled python binary from another Python
process (e.g. sh module or os.system()) may get wrongly detected as
if it was ran directly from the venv.
This happens when `pyvenv.cfg` is present (e.g. venv/pyvenv.cfg).
It makes `sysconfig.is_python_build()` returns `False` instead of `True`
since it's using the compiled interpreter (e.g. ./build-dir/python).
https://docs.python.org/3/library/sysconfig.html#sysconfig.is_python_build
https://github.com/python/cpython/blob/v3.8.2/Lib/sysconfig.py#L127
This is because the `sys._home` attribute is used during the detection.
The issue was first seen in macOS venv which generates the `pyvenv.cfg`.
To compare both behaviours, try the following within and without a venv:
```python
import os
os.system("./build-dir/python -E -c 'import sysconfig; print(sysconfig._sys_home)'")
```
One would return `/usr/local/bin` and the other `None`

Refs:
- kivy/kivy-ios#401
- kivy#2063
@@ -35,6 +36,10 @@ class Hostpython3Recipe(Recipe):
'''The default url to download our host python recipe. This url will
change depending on the python version set in attribute :attr:`version`.'''

patches = (
('patches/pyconfig_detection.patch', is_version_lt("3.8.3")),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now applying to anything lower than 3.8.3. Since this issue was also impacting some recent version of the 3.7 branch I thought it would be easier to go that way than being ultra ranging inside MINOR and PATCH with a complex expression (given MAJOR.MINOR.PATCH)

@rdbisme
Copy link

rdbisme commented Apr 29, 2020

@AndreMiras the patch on pythonw.c is not needed?

In any case I could test this. I'll do it ASAP

@AndreMiras
Copy link
Member Author

OK the patch got rejected, it doesn't seem to apply properly I haven't investigated much, but i think my solution is good enough until 3.8.3 is released.
Let me know what you guys think

@AndreMiras
Copy link
Member Author

@rubendibattista which patch on pythonw.c you mean the code comment? Because the only upstream fix I see is Modules/main.c the rest is unit testing and code comments, right?
Yes please give it a try and let me know

@rdbisme
Copy link

rdbisme commented Apr 29, 2020

@AndreMiras Ok, let's go with your initial PR. I'll try to see if I can adapt the upstream patch to current versions of Python.

Copy link
Member

@opacam opacam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@AndreMiras AndreMiras merged commit 83dc122 into kivy:develop Apr 29, 2020
@AndreMiras AndreMiras deleted the feature/fix_macos_venv_build branch April 29, 2020 16:03
This was referenced May 1, 2020
@AndreMiras
Copy link
Member Author

AndreMiras commented May 1, 2020

I found out why the upstream patch didn't apply. It was for the 3.7 branch 😅
The 3.8 one is python/cpython#19110
As for now the fix seems to work, but I will give the upstream patch a try on kivy-ios.
Thanks again for the heads up.
Edit: OK that doesn't fix it 😮 I think it's because the system Python is the one leaking the env down to the compiled host Python

@rdbisme
Copy link

rdbisme commented May 1, 2020

🔝

lerela added a commit to chronolife-rd/python-for-android that referenced this pull request Jun 23, 2020
🔖 v2020.06.02

* Adds missing requests sub dependencies (kivy#2221)
* Bumps to Gradle 6.4.1 (kivy#2222)
* Bumps to Cython==0.29.19 (kivy#2220)
* Updates install and troubleshooting docs (kivy#2219)
* Bumps to Ubuntu 20.04 (kivy#2218)
* Attempt to improve the issue template (kivy#2217)
* Add `opencv_extras` recipe (kivy#2209)
* Split logic for build modes & debug symbols (kivy#2213)
* Troubleshoot SSL error (kivy#2205)
* Remove superfluous recipes fixes (kivy#2202)
* Add tests for hostpython3 recipe (kivy#2196)
* Fix for 'cannot find setuptools module' (kivy#2195)
* Rename `Hostpython3Recipe` class to camel case (kivy#2194)
* Fix `test_should_build` (kivy#2193)
* Add initial tests for python3 recipe (kivy#2192)
* PythonActivityUtil helper for unpacking data (kivy#2189)
* Fixes flake8 errors post update (kivy#2191)
* Share PythonUtil.java between bootstraps (kivy#2188)
* Java code linting using PMD 6.23.0 (kivy#2187)
* Deletes deprecated renpy Python{Activity,Service}.java (kivy#2186)
* Removes java concurrency/ folder (kivy#2185)
* Reuse common AssetExtract.java (kivy#2182)
* Use common Hardware.java (kivy#2183)
* Moves kamranzafar/ java directory to common/ (kivy#2184)
* Updates release documentation (kivy#2177)
* Fixes service only unittest loading (kivy#2181)
* Narrows some context manager scopes (kivy#2179)
* Downgrades to SDL2 2.0.9 (kivy#2180)
* Bump to SDL2 2.0.10 & extract .java from SDL2 tarball (kivy#2113)
* Adds pygame recipe (kivy#2164)
* Adds macOS install instructions (2165)
* Removed python2 support mention from README (kivy#2162)
* Adding more assets (kivy#2132)
* Get --add-source working for dirs in Gradle builds (kivy#2156)
* Fixes python build with macOS venv (kivy#2159)
renabriseno68 added a commit to renabriseno68/kivy-ios-android-developer that referenced this pull request Sep 13, 2022
Borrowed from p4a:
kivy/python-for-android#2159
Also refs upstream issue:
https://bugs.python.org/issue40261
Note that the fix used upstream cannot really be used directly here
because the actual system Python is impacted.
Adds venv build to CI to check for regressions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants