Skip to content

Commit f34d89e

Browse files
committed
Fixes hostpython build with macOS venv
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 see in macOS which generates the `pyvenv.cfg` upon `venv` module use. 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
1 parent e2b9cfe commit f34d89e

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
# installs java 1.8, android's SDK/NDK and p4a
5353
- make -f ci/makefiles/osx.mk
5454
- export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
55-
script: make testapps-no-venv/armeabi-v7a
55+
script: make testapps/armeabi-v7a
5656
- <<: *testapps
5757
name: Rebuild updated recipes
5858
script: travis_wait 30 make docker/run/make/rebuild_updated_recipes

Makefile

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ testapps/%: virtualenv
4747
python setup.py apk --sdk-dir $(ANDROID_SDK_HOME) --ndk-dir $(ANDROID_NDK_HOME) \
4848
--arch=$($@_APP_ARCH)
4949

50-
testapps-no-venv/%:
51-
pip3 install Cython==0.28.6
52-
pip3 install -e .
53-
$(eval $@_APP_ARCH := $(shell basename $*))
54-
cd testapps/on_device_unit_tests/ && \
55-
python3 setup.py apk --sdk-dir $(ANDROID_SDK_HOME) --ndk-dir $(ANDROID_NDK_HOME) \
56-
--arch=$($@_APP_ARCH)
57-
5850
clean:
5951
find . -type d -name "__pycache__" -exec rm -r {} +
6052
find . -type d -name "*.egg-info" -exec rm -r {} +

pythonforandroid/recipes/hostpython3/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class Hostpython3Recipe(Recipe):
3535
'''The default url to download our host python recipe. This url will
3636
change depending on the python version set in attribute :attr:`version`.'''
3737

38+
patches = ('patches/pyconfig_detection.patch',)
39+
3840
@property
3941
def _exe_name(self):
4042
'''
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff -Nru Python-3.8.2/Lib/site.py Python-3.8.2-new/Lib/site.py
2+
--- Python-3.8.2/Lib/site.py 2020-04-28 12:48:38.000000000 -0700
3+
+++ Python-3.8.2-new/Lib/site.py 2020-04-28 12:52:46.000000000 -0700
4+
@@ -487,7 +487,8 @@
5+
if key == 'include-system-site-packages':
6+
system_site = value.lower()
7+
elif key == 'home':
8+
- sys._home = value
9+
+ # this is breaking pyconfig.h path detection with venv
10+
+ print('Ignoring "sys._home = value" override')
11+
12+
sys.prefix = sys.exec_prefix = site_prefix
13+

0 commit comments

Comments
 (0)