Compatibility fixes for mjpython.

- Find Python functions in the interpreter binary itself rather than trying to find libpython.dylib, because this doesn't exist when using Conda. (https://github.com/conda-forge/python-feedstock/issues/595#issuecomment-1311275470)

- Increase the stack size of both the Python main thread and the OS main thread to 16MiB, to match what Python normally uses on macOS. (https://github.com/python/cpython/blob/3.11/configure#L11038)

- Check whether dlfcn and pthread function calls actually succeeds. Emit error messages and exit on failure rather than continuing the program (which generally leads to segfaults).

- Fix pixelated shadow in the icon.

PiperOrigin-RevId: 517390522
Change-Id: Ib91679b4baa63bac5f6b5893e634712d72161ea5
This commit is contained in:
Saran Tunyasuvunakool
2023-03-17 05:36:58 -07:00
committed by Copybara-Service
parent 08027b4e43
commit 0c4e191dd6
3 changed files with 97 additions and 28 deletions
+27 -10
View File
@@ -21,23 +21,40 @@ GUI calls without blocking the user's Python script. In other words, Python's
idea of the "main thread" is different from the thread that holds the
com.apple.main-thread DispatchQueue.
"""
import ctypes
import importlib.util
import os
import platform
import sys
import sysconfig
if platform.system() != 'Darwin':
raise RuntimeError('This script only works on macOS')
_NSGetExecutablePath = getattr(ctypes.CDLL(None), '_NSGetExecutablePath')
def get_executable_path():
c_path_size = ctypes.c_int32(0)
_NSGetExecutablePath(None, ctypes.byref(c_path_size))
c_path = (ctypes.c_char * c_path_size.value)()
_NSGetExecutablePath(ctypes.byref(c_path), ctypes.byref(c_path_size))
return c_path.value.decode()
def main(argv):
os.environ['MJPYTHON_LIBPYTHON'] = os.path.join(
sysconfig.get_config_var('PYTHONFRAMEWORKPREFIX'),
sysconfig.get_config_var('INSTSONAME'),
)
module_dir = os.path.dirname(importlib.util.find_spec('mujoco').origin)
os.environ['MJPYTHON_BIN'] = os.path.join(
module_dir, 'MuJoCo (mjpython).app/Contents/MacOS/mjpython')
# Conda doesn't create a separate shared library for Python.
# We instead use the Python binary itself, which can be dlopened just as well.
os.environ['MJPYTHON_LIBPYTHON'] = get_executable_path()
# argv[0] is currently the path to this script.
# Replace it with sys.executable to preserve e.g. virtualenv path.
argv[0] = sys.executable
mujoco_dir = os.path.dirname(importlib.util.find_spec('mujoco').origin)
os.execve(
os.path.join(mujoco_dir, 'MuJoCo (mjpython).app/Contents/MacOS/mjpython'),
argv, os.environ)
os.execve(os.environ['MJPYTHON_BIN'], argv, os.environ)
if __name__ == '__main__':