Skip to content

Commit fbc0376

Browse files
committed
Implement backwards compatible loading for Windows
1 parent 21cc289 commit fbc0376

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

clr.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Legacy Python.NET loader for backwards compatibility
3+
"""
4+
5+
def _load():
6+
import os, sys
7+
import importlib.util as util
8+
9+
if sys.maxsize > 2 ** 32:
10+
arch = "amd64"
11+
else:
12+
arch = "x86"
13+
14+
path = os.path.join(os.path.dirname(__file__), "pythonnet", "dlls", arch, "clr.pyd")
15+
del sys.modules["clr"]
16+
17+
spec = util.spec_from_file_location("clr", path)
18+
clr = util.module_from_spec(spec)
19+
spec.loader.exec_module(clr)
20+
21+
sys.modules["clr"] = clr
22+
23+
_load()

setup.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ def run(self):
120120
_write_configure_props()
121121

122122
for lib in dotnet_modules:
123-
output = lib.args.pop("output")
123+
output = os.path.join(self.build_lib, lib.args.pop("output"))
124+
rename = lib.args.pop("rename", {})
125+
124126
opts = sum(
125127
[
126128
["--" + name.replace("_", "-"), value]
@@ -130,15 +132,30 @@ def run(self):
130132
)
131133

132134
opts.extend(["--configuration", self.dotnet_config])
133-
opts.extend(["--output", os.path.join(self.build_lib, output)])
135+
opts.extend(["--output", output])
134136

137+
self.announce("Running dotnet build...", level=distutils.log.INFO)
135138
self.spawn(["dotnet", "build", lib.path] + opts)
136139

137-
self.distribution.ext_modules = other_modules
140+
for k, v in rename.items():
141+
source = os.path.join(output, k)
142+
dest = os.path.join(output, v)
143+
144+
if os.path.isfile(source):
145+
try:
146+
os.remove(dest)
147+
except OSError:
148+
pass
138149

139-
super().run()
150+
self.move_file(src=source, dst=dest, level=distutils.log.INFO)
151+
else:
152+
self.warn("Can't find file to rename: %s" % source)
140153

141-
self.distribution.ext_modules = orig_modules
154+
if other_modules:
155+
self.distribution.ext_modules = other_modules
156+
super().run()
157+
self.distribution.ext_modules = orig_modules
158+
# If no modules need to be compiled, skip
142159

143160

144161
class bdist_wheel_patched(bdist_wheel):
@@ -159,12 +176,14 @@ def finalize_options(self):
159176
"src/clrmodule/",
160177
runtime="win-x64",
161178
output="pythonnet/dlls/amd64",
179+
rename={"clr.dll": "clr.pyd"},
162180
),
163181
DotnetLib(
164182
"clrmodule-x86",
165183
"src/clrmodule/",
166184
runtime="win-x86",
167185
output="pythonnet/dlls/x86",
186+
rename={"clr.dll": "clr.pyd"},
168187
),
169188
]
170189

@@ -186,9 +205,6 @@ def finalize_options(self):
186205
)
187206
ext_modules.append(clr_ext)
188207
except Exception:
189-
import traceback
190-
191-
traceback.print_exc()
192208
print("Failed to find mono libraries via pkg-config, skipping the Mono CLR loader")
193209

194210

@@ -208,6 +224,7 @@ def finalize_options(self):
208224
"bdist_wheel": bdist_wheel_patched,
209225
"configure": Configure,
210226
},
227+
py_modules=["clr"],
211228
ext_modules=ext_modules,
212229
classifiers=[
213230
"Development Status :: 5 - Production/Stable",

src/clrmodule/clrmodule.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<TargetFrameworks>net472</TargetFrameworks>
44
<RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
5+
<AssemblyName>clr</AssemblyName>
56
</PropertyGroup>
67

78
<ItemGroup>

0 commit comments

Comments
 (0)