Skip to content

Commit

Permalink
Merge pull request #3117 from pygame-community/ankith26-fix-editable
Browse files Browse the repository at this point in the history
Fix editable install on windows
  • Loading branch information
oddbookworm authored Sep 27, 2024
2 parents f554e77 + 98170b3 commit ddc900e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dist
*.so
__pycache__
_headers/*
buildconfig/win_dll_dirs.json

# cython generated files
src_c/_sdl2/*.c
Expand Down
14 changes: 14 additions & 0 deletions buildconfig/_meson_win_dll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
A helper file invoked by the meson buildconfig to write DLL paths to a json file
"""

import json
import sys

from pathlib import Path

dll_parents = {str(Path(i).parent) for i in sys.argv[1:]}
win_dll_dirs_file = Path(__file__).parent / "win_dll_dirs.json"

if __name__ == "__main__":
win_dll_dirs_file.write_text(json.dumps(list(dll_parents)), encoding="utf-8")
9 changes: 9 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ if plat == 'win' and host_machine.cpu_family().startswith('x86')
)
endif

run_command(
[
find_program('python3', 'python'),
base_dir / 'buildconfig' / '_meson_win_dll.py',
dlls,
],
check: true,
)

# put dlls in root of install
install_data(dlls, install_dir: pg_dir, install_tag: 'pg-tag')
else
Expand Down
30 changes: 23 additions & 7 deletions src_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,32 @@
# Choose Windows display driver
if os.name == "nt":
pygame_dir = os.path.split(__file__)[0]
dll_parents = {pygame_dir}
try:
# For editable support, add some more folders where DLLs are available.
# This block only executes under an editable install. In a "normal"
# install, the json file will not be installed at the supplied path.
with open(
os.path.join(
os.path.dirname(pygame_dir), "buildconfig", "win_dll_dirs.json"
),
encoding="utf-8",
) as f:
import json

dll_parents.update(json.load(f))
del json
except (FileNotFoundError, ValueError):
pass

# pypy does not find the dlls, so we add package folder to PATH.
os.environ["PATH"] = os.environ["PATH"] + ";" + pygame_dir

# Windows store python does not find the dlls, so we run this
if sys.version_info > (3, 8):
os.add_dll_directory(pygame_dir) # only available in 3.8+
for d in dll_parents:
# adding to PATH is the legacy way, os.add_dll_directory is the new
# and recommended method. For extra safety we do both
os.environ["PATH"] = os.environ["PATH"] + ";" + d
os.add_dll_directory(d)

# cleanup namespace
del pygame_dir
del pygame_dir, dll_parents

# when running under X11, always set the SDL window WM_CLASS to make the
# window managers correctly match the pygame window.
Expand Down

0 comments on commit ddc900e

Please sign in to comment.