Skip to content

Commit 3a5e526

Browse files
committed
Demonstrate where a potential fix could be and why it doesn't seem to work
1 parent 3d2bc7d commit 3a5e526

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/scikit_build_core/resources/_editable_redirect.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import importlib.abc
44
import importlib.util
5+
import importlib.machinery
6+
import importlib.readers # Might be Python version specific?
57
import os
68
import subprocess
79
import sys
@@ -22,6 +24,27 @@ def __dir__() -> list[str]:
2224
return __all__
2325

2426

27+
# Note: This solution relies on importlib's call stack in Python 3.11. Python 3.9 looks
28+
# different, so might require a different solution, but I haven't gone deeper into that
29+
# yet since I don't have a solution for the 3.11 case yet anyway.
30+
class ScikitBuildRedirectingReader(importlib.readers.FileReader):
31+
def files(self):
32+
# ATTENTION: This is where the problem is. The expectation is that this returns
33+
# a Traversable object. We could hack together an object that satisfies that
34+
# API, but methods like `joinpath` don't have sensible implementations if
35+
# `files` could return multiple paths instead of a single one. We could do some
36+
# hackery to figure out which paths exist on the backend by hiding some internal
37+
# representation that knows both possible roots and checks for existence when
38+
# necessary, but that seriously violates the principle of least surprise for the
39+
# user so I'd be quite skeptical.
40+
return self.path
41+
42+
43+
class ScikitBuildRedirectingLoader(importlib.machinery.SourceFileLoader):
44+
def get_resource_reader(self, module):
45+
return ScikitBuildRedirectingReader(self)
46+
47+
2548
class ScikitBuildRedirectingFinder(importlib.abc.MetaPathFinder):
2649
def __init__(
2750
self,
@@ -92,13 +115,15 @@ def find_spec(
92115
fullname,
93116
os.path.join(self.dir, redir),
94117
submodule_search_locations=submodule_search_locations,
118+
loader=ScikitBuildRedirectingLoader(fullname, os.path.join(self.dir, redir)),
95119
)
96120
if fullname in self.known_source_files:
97121
redir = self.known_source_files[fullname]
98122
return importlib.util.spec_from_file_location(
99123
fullname,
100124
redir,
101125
submodule_search_locations=submodule_search_locations,
126+
loader=ScikitBuildRedirectingLoader(fullname, redir),
102127
)
103128
return None
104129

0 commit comments

Comments
 (0)