Skip to content

Commit

Permalink
Merge pull request #104 from virtuald/py-311
Browse files Browse the repository at this point in the history
Fixes for Python 3.11, from #104
  • Loading branch information
benfogle authored Dec 11, 2022
2 parents 5c3b53a + 6b3fef9 commit 6778487
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
24 changes: 23 additions & 1 deletion crossenv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ def find_sysconfig_data(self, paths):
raise FileNotFoundError("No _sysconfigdata*.py found in host lib")


def _is_python_source_dir(self, d):
fn = getattr(sysconfig, '_is_python_source_dir', None)
if fn:
return fn(d)

# removed in python 3.11
for fn in ("Setup", "Setup.local"):
if os.path.isfile(os.path.join(d, "Modules", fn)):
return True
return False


def find_host_python(self, host):
"""
Find Python paths and other info based on a path.
Expand All @@ -245,7 +257,7 @@ def find_host_python(self, host):
else:
self.host_project_base = os.path.dirname(host)

if sysconfig._is_python_source_dir(self.host_project_base):
if self._is_python_source_dir(self.host_project_base):
self.host_makefile = os.path.join(self.host_project_base, 'Makefile')
pybuilddir = os.path.join(self.host_project_base, 'pybuilddir.txt')
try:
Expand Down Expand Up @@ -696,6 +708,16 @@ def make_cross_python(self, context):
# directory.
stdlib = os.path.abspath(os.path.dirname(os.__file__))

# In python 3.11, the import machinery imports from math, which breaks
# in our cross environment.. so we inject lib-dynload to the path also
dynload = os.path.join(stdlib, "lib-dynload")

# In python 3.11, several system packages are frozen by default, which
# prevents us from patching it. Disable it.
xopt = subprocess.run([context.build_env_exe, "--help-xoptions"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
disable_frozen_modules = (b'frozen_modules=' in xopt.stdout)

context.sentinel = random.randint(0,0xffffffff)

extra_envs = list(self.extra_env_vars)
Expand Down
5 changes: 4 additions & 1 deletion crossenv/scripts/pywrapper.py.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ os.environ['_PYTHON_HOST_PLATFORM']={{repr(self.host_platform)}}
os.environ['_PYTHON_SYSCONFIGDATA_NAME']={{repr(sysconfig_name)}}
os.environ['PYTHONHOME']={{repr(self.host_home)}}
oldpath = os.environ.get('PYTHONPATH')
newpath = os.pathsep.join([{{repr(context.lib_path)}}, {{repr(stdlib)}}])
newpath = os.pathsep.join([{{repr(context.lib_path)}}, {{repr(stdlib)}}, {{repr(dynload)}}])
if oldpath:
path = os.pathsep.join([newpath, oldpath])
else:
Expand All @@ -39,5 +39,8 @@ for name, assign, value in extra_envs:
else:
os.environ[name] = value

if {{repr(disable_frozen_modules)}}:
sys.argv[1:1] = ["-X", "frozen_modules=off"]

# This will fix up argv0 so that sys.executable will be correct
os.execv({{repr(context.build_env_exe)}}, sys.argv)

0 comments on commit 6778487

Please sign in to comment.