Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make the code compile with ILibPython
  • Loading branch information
filmor committed Dec 16, 2019
commit 0185bcb48d05a8407a41fc5eedae4c4afb27b214
6 changes: 6 additions & 0 deletions Python.Runtime.Native/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System;
using System.Runtime.CompilerServices;

[assembly: CLSCompliant(true)]
[assembly: InternalsVisibleTo("Python.Test.Embed")]
[assembly: InternalsVisibleTo("Python.Runtime")]
24 changes: 24 additions & 0 deletions Python.Runtime/platform/InternalLoadContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Reflection;
using System.Runtime.Loader;

namespace Python.Runtime.Platform
{
class InternalLoadContext : AssemblyLoadContext
{
protected override Assembly Load(AssemblyName name) => null;

protected override IntPtr LoadUnmanagedDll(string name)
{
if (name == "__Internal")
{
var loader = LibraryLoader.Get(OperatingSystemType.Linux);
return loader.Load(null);
}

return IntPtr.Zero;
}

public static AssemblyLoadContext Instance { get; } = new InternalLoadContext();
}
}
20 changes: 18 additions & 2 deletions Python.Runtime/platform/LibraryLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ class LinuxLoader : ILibraryLoader

public IntPtr Load(string dllToLoad)
{
var filename = $"lib{dllToLoad}.so";
string filename;
if (dllToLoad != null)
{
filename = $"lib{dllToLoad}.so";
}
else
{
filename = null;
}
ClearError();
var res = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
if (res == IntPtr.Zero)
Expand Down Expand Up @@ -111,7 +119,15 @@ class DarwinLoader : ILibraryLoader

public IntPtr Load(string dllToLoad)
{
var filename = $"lib{dllToLoad}.dylib";
string filename;
if (dllToLoad != null)
{
filename = $"lib{dllToLoad}.dylib";
}
else
{
filename = null;
}
ClearError();
var res = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
if (res == IntPtr.Zero)
Expand Down
25 changes: 24 additions & 1 deletion Python.Runtime/pythonengine.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Runtime.CompilerServices;
using System.Runtime.Loader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Python.Runtime.Native;

namespace Python.Runtime
{
Expand Down Expand Up @@ -254,10 +257,17 @@ static void OnDomainUnload(object _, EventArgs __)
/// CPython interpreter process - this bootstraps the managed runtime
/// when it is imported by the CLR extension module.
/// </summary>
public static int InternalInitialize(int size, IntPtr data)
public static int InternalInitialize(IntPtr data, int size)
{


try
{
// Console.WriteLine("Before Initialize");
// Console.Out.Flush();

// Python.Runtime.Platform.InternalLoadContext.Instance.EnterContextualReflection();

Initialize(setSysArgv: false);

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

// Console.WriteLine("Initialized");
// Console.Out.Flush();

string code =
"import traceback\n" +
"for item in traceback.extract_stack():\n" +
Expand All @@ -290,13 +303,23 @@ public static int InternalInitialize(int size, IntPtr data)
" break\n";

PythonEngine.Exec(code);

// Console.WriteLine("Exec'd traceback hack");
// Console.Out.Flush();

return 0;
}
catch (PythonException e)
{
e.Restore();
return -1;
}
catch (Exception e)
{
// Console.Error.WriteLine(e.ToString());
// Console.Error.Write(e.StackTrace);
return -2;
}
}

/// <summary>
Expand Down
Loading