- Notifications
You must be signed in to change notification settings - Fork750
First chunk of modernisation changes#1109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
Show all changes
13 commits Select commitHold shift + click to select a range
f04e948
First chunk of modernisation changes
filmor52e4f15
Move new files
filmor832c63f
Drop obsolete tools
filmor063d0f9
Add loader DLL that fixes the __Internal ref
filmorbaf0535
Use deferred reading to side-step dependency loading
filmor054b872
Fix a few bugs
filmord1babcf
Leak libpython handle
filmor7f84915
Remove debug write-line
filmor5c3109d
Add Shutdown call, disable Py_Finalize for now
filmorbe22720
Update interop3.cs to Python 3.7 (temporary)
filmor503b4b8
Add unloading as atexit call
filmor912ef3b
Write qualname
filmor1adc2ff
Last state
filmorFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2,6 +2,7 @@ | ||
*.dll | ||
*.exe | ||
*.pdb | ||
pythonnet/dlls/ | ||
### JetBrains ### | ||
.idea/ | ||
7 changes: 0 additions & 7 deletionsNuGet.config
This file was deleted.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
16 changes: 16 additions & 0 deletionsPython.Loader/InternalDllImportResolver.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
namespace Python.Loader | ||
{ | ||
public static class InternalDllImportResolver | ||
{ | ||
public static IntPtr Resolve(string libraryName, Assembly assembly, int? flags) { | ||
if (libraryName == "__Internal") { | ||
} | ||
} | ||
} | ||
} |
109 changes: 109 additions & 0 deletionsPython.Loader/Loader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using Mono.Cecil; | ||
namespace Python.Loader | ||
{ | ||
public class RuntimeLoader | ||
{ | ||
AssemblyDefinition assembly; | ||
static public RuntimeLoader FromFile(string filename) | ||
{ | ||
return new RuntimeLoader(File.OpenRead(filename)); | ||
} | ||
public RuntimeLoader(Stream stream) | ||
{ | ||
assembly = AssemblyDefinition.ReadAssembly(stream, new ReaderParameters | ||
{ | ||
InMemory = true, | ||
}); | ||
} | ||
public void Remap(string pythonDll) | ||
{ | ||
var moduleRef = new ModuleReference(pythonDll); | ||
var module = assembly.MainModule; | ||
module.ModuleReferences.Add(moduleRef); | ||
foreach (var type in module.Types) | ||
{ | ||
foreach (var func in type.Methods) | ||
{ | ||
if (func.HasPInvokeInfo) | ||
{ | ||
var info = func.PInvokeInfo; | ||
if (info.Module.Name == "__Internal") | ||
{ | ||
info.Module = moduleRef; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
public Assembly LoadAssembly() | ||
{ | ||
using (var stream = new MemoryStream()) | ||
{ | ||
assembly.Write(stream); | ||
return Assembly.Load(stream.ToArray()); | ||
} | ||
} | ||
} | ||
static class Internal | ||
{ | ||
static Type PythonEngine = null; | ||
public static int Initialize(IntPtr data, int size) | ||
{ | ||
try | ||
{ | ||
var buf = new byte[size]; | ||
Marshal.Copy(data, buf, 0, size); | ||
var str = UTF8Encoding.Default.GetString(buf); | ||
var splitted = str.Split(';'); | ||
var dllPath = splitted[0]; | ||
var pythonDll = splitted[1]; | ||
var loader = RuntimeLoader.FromFile(dllPath); | ||
loader.Remap(pythonDll); | ||
var assembly = loader.LoadAssembly(); | ||
PythonEngine = assembly.GetType("Python.Runtime.PythonEngine"); | ||
var method = PythonEngine.GetMethod("InternalInitialize"); | ||
return (int)method.Invoke(null, new object[] { data, size }); | ||
} | ||
catch (Exception exc) | ||
{ | ||
Console.WriteLine($"{exc}\n{exc.StackTrace}"); | ||
return -1; | ||
} | ||
} | ||
public static int Shutdown(IntPtr data, int size) | ||
{ | ||
if (PythonEngine == null) | ||
return -2; | ||
try | ||
{ | ||
var method = PythonEngine.GetMethod("InternalShutdown"); | ||
return (int)method.Invoke(null, new object[] { data, size }); | ||
} | ||
catch (Exception exc) | ||
{ | ||
Console.WriteLine($"{exc}\n{exc.StackTrace}"); | ||
return -1; | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletionsPython.Loader/Python.Loader.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Mono.Cecil" Version="0.11.2" /> | ||
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" /> | ||
</ItemGroup> | ||
</Project> |
5 changes: 5 additions & 0 deletionsPython.Runtime/AssemblyInfo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
[assembly: CLSCompliant(true)] | ||
[assembly: InternalsVisibleTo("Python.Test.Embed")] |
File renamed without changes.
File renamed without changes.
24 changes: 15 additions & 9 deletionssrc/runtime/CustomMarshaler.cs → Python.Runtime/CustomMarshaler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletionsPython.Runtime/Python.Runtime.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<EmbeddedResource Include="resources\clr.py"> | ||
<LogicalName>clr.py</LogicalName> | ||
</EmbeddedResource> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="System.Reflection.Emit" Version="4.6.0" /> | ||
</ItemGroup> | ||
</Project> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
38 changes: 31 additions & 7 deletionssrc/runtime/codegenerator.cs → Python.Runtime/codegenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.