Skip to content

Commit 0185bcb

Browse files
committed
Make the code compile with ILibPython
1 parent e61dd72 commit 0185bcb

File tree

7 files changed

+83
-819
lines changed

7 files changed

+83
-819
lines changed

Python.Runtime.Native/AssemblyInfo.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
4+
[assembly: CLSCompliant(true)]
5+
[assembly: InternalsVisibleTo("Python.Test.Embed")]
6+
[assembly: InternalsVisibleTo("Python.Runtime")]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Reflection;
3+
using System.Runtime.Loader;
4+
5+
namespace Python.Runtime.Platform
6+
{
7+
class InternalLoadContext : AssemblyLoadContext
8+
{
9+
protected override Assembly Load(AssemblyName name) => null;
10+
11+
protected override IntPtr LoadUnmanagedDll(string name)
12+
{
13+
if (name == "__Internal")
14+
{
15+
var loader = LibraryLoader.Get(OperatingSystemType.Linux);
16+
return loader.Load(null);
17+
}
18+
19+
return IntPtr.Zero;
20+
}
21+
22+
public static AssemblyLoadContext Instance { get; } = new InternalLoadContext();
23+
}
24+
}

Python.Runtime/platform/LibraryLoader.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@ class LinuxLoader : ILibraryLoader
4040

4141
public IntPtr Load(string dllToLoad)
4242
{
43-
var filename = $"lib{dllToLoad}.so";
43+
string filename;
44+
if (dllToLoad != null)
45+
{
46+
filename = $"lib{dllToLoad}.so";
47+
}
48+
else
49+
{
50+
filename = null;
51+
}
4452
ClearError();
4553
var res = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
4654
if (res == IntPtr.Zero)
@@ -111,7 +119,15 @@ class DarwinLoader : ILibraryLoader
111119

112120
public IntPtr Load(string dllToLoad)
113121
{
114-
var filename = $"lib{dllToLoad}.dylib";
122+
string filename;
123+
if (dllToLoad != null)
124+
{
125+
filename = $"lib{dllToLoad}.dylib";
126+
}
127+
else
128+
{
129+
filename = null;
130+
}
115131
ClearError();
116132
var res = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
117133
if (res == IntPtr.Zero)

Python.Runtime/pythonengine.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
using System.Runtime.CompilerServices;
2+
using System.Runtime.Loader;
13
using System;
24
using System.Collections.Generic;
35
using System.IO;
46
using System.Linq;
57
using System.Reflection;
68
using System.Runtime.InteropServices;
9+
using Python.Runtime.Native;
710

811
namespace Python.Runtime
912
{
@@ -254,10 +257,17 @@ static void OnDomainUnload(object _, EventArgs __)
254257
/// CPython interpreter process - this bootstraps the managed runtime
255258
/// when it is imported by the CLR extension module.
256259
/// </summary>
257-
public static int InternalInitialize(int size, IntPtr data)
260+
public static int InternalInitialize(IntPtr data, int size)
258261
{
262+
263+
259264
try
260265
{
266+
// Console.WriteLine("Before Initialize");
267+
// Console.Out.Flush();
268+
269+
// Python.Runtime.Platform.InternalLoadContext.Instance.EnterContextualReflection();
270+
261271
Initialize(setSysArgv: false);
262272

263273
// Trickery - when the import hook is installed into an already
@@ -277,6 +287,9 @@ public static int InternalInitialize(int size, IntPtr data)
277287
// still doesn't work if you use the interactive interpreter,
278288
// since there is no line info to get the import line ;(
279289

290+
// Console.WriteLine("Initialized");
291+
// Console.Out.Flush();
292+
280293
string code =
281294
"import traceback\n" +
282295
"for item in traceback.extract_stack():\n" +
@@ -290,13 +303,23 @@ public static int InternalInitialize(int size, IntPtr data)
290303
" break\n";
291304

292305
PythonEngine.Exec(code);
306+
307+
// Console.WriteLine("Exec'd traceback hack");
308+
// Console.Out.Flush();
309+
293310
return 0;
294311
}
295312
catch (PythonException e)
296313
{
297314
e.Restore();
298315
return -1;
299316
}
317+
catch (Exception e)
318+
{
319+
// Console.Error.WriteLine(e.ToString());
320+
// Console.Error.Write(e.StackTrace);
321+
return -2;
322+
}
300323
}
301324

302325
/// <summary>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy