Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.dll
*.exe
*.pdb
pythonnet/dlls/

### JetBrains ###
.idea/
Expand Down
7 changes: 0 additions & 7 deletions NuGet.config

This file was deleted.

16 changes: 16 additions & 0 deletions Python.Loader/InternalDllImportResolver.cs
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 deletions Python.Loader/Loader.cs
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 deletions Python.Loader/Python.Loader.csproj
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 deletions Python.Runtime/AssemblyInfo.cs
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.
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ public static int GetUnicodeByteLength(IntPtr p)
var len = 0;
while (true)
{
int c = Runtime._UCS == 2
? Marshal.ReadInt16(p, len * 2)
: Marshal.ReadInt32(p, len * 4);
#if UCS2
int c = Marshal.ReadInt16(p, len * 2);
#else
int c = Marshal.ReadInt32(p, len * 4);
#endif

if (c == 0)
{
Expand All @@ -120,9 +122,11 @@ public static int GetUnicodeByteLength(IntPtr p)
/// </remarks>
public static IntPtr Py3UnicodePy2StringtoPtr(string s)
{
return Runtime.IsPython3
? Instance.MarshalManagedToNative(s)
: Marshal.StringToHGlobalAnsi(s);
#if PYTHON2
return Marshal.StringToHGlobalAnsi(s);
#else
return Instance.MarshalManagedToNative(s);
#endif
}

/// <summary>
Expand All @@ -137,9 +141,11 @@ public static IntPtr Py3UnicodePy2StringtoPtr(string s)
/// </returns>
public static string PtrToPy3UnicodePy2String(IntPtr p)
{
return Runtime.IsPython3
? PtrToStringUni(p)
: Marshal.PtrToStringAnsi(p);
#if PYTHON2
return Marshal.PtrToStringAnsi(p);
#else
return PtrToStringUni(p);
#endif
}
}

Expand Down
File renamed without changes.
18 changes: 18 additions & 0 deletions Python.Runtime/Python.Runtime.csproj
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.
38 changes: 31 additions & 7 deletions src/runtime/codegenerator.cs → Python.Runtime/codegenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,40 @@ namespace Python.Runtime
/// </summary>
internal class CodeGenerator
{
private AssemblyBuilder aBuilder;
private ModuleBuilder mBuilder;
private AssemblyBuilder _aBuilder = null;

internal CodeGenerator()
private AssemblyBuilder aBuilder
{
get
{
if (_aBuilder == null)
{
var aname = new AssemblyName { Name = "__CodeGenerator_Assembly" };
var aa = AssemblyBuilderAccess.Run;

_aBuilder = Thread.GetDomain().DefineDynamicAssembly(aname, aa);
}

return _aBuilder;
}
}

private ModuleBuilder _mBuilder = null;
private ModuleBuilder mBuilder
{
var aname = new AssemblyName { Name = "__CodeGenerator_Assembly" };
var aa = AssemblyBuilderAccess.Run;
get
{
if (_mBuilder == null)
{
_mBuilder = aBuilder.DefineDynamicModule("__CodeGenerator_Module");
}

return _mBuilder;
}
}

aBuilder = Thread.GetDomain().DefineDynamicAssembly(aname, aa);
mBuilder = aBuilder.DefineDynamicModule("__CodeGenerator_Module");
internal CodeGenerator()
{
}

/// <summary>
Expand Down
File renamed without changes.
File renamed without changes.
Loading