2
2
3
3
import importlib .abc
4
4
import importlib .util
5
+ import importlib .machinery
6
+ import importlib .readers # Might be Python version specific?
5
7
import os
6
8
import subprocess
7
9
import sys
@@ -22,6 +24,27 @@ def __dir__() -> list[str]:
22
24
return __all__
23
25
24
26
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
+
25
48
class ScikitBuildRedirectingFinder (importlib .abc .MetaPathFinder ):
26
49
def __init__ (
27
50
self ,
@@ -92,13 +115,15 @@ def find_spec(
92
115
fullname ,
93
116
os .path .join (self .dir , redir ),
94
117
submodule_search_locations = submodule_search_locations ,
118
+ loader = ScikitBuildRedirectingLoader (fullname , os .path .join (self .dir , redir )),
95
119
)
96
120
if fullname in self .known_source_files :
97
121
redir = self .known_source_files [fullname ]
98
122
return importlib .util .spec_from_file_location (
99
123
fullname ,
100
124
redir ,
101
125
submodule_search_locations = submodule_search_locations ,
126
+ loader = ScikitBuildRedirectingLoader (fullname , redir ),
102
127
)
103
128
return None
104
129
0 commit comments