Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
filmor wants to merge13 commits intopythonnet:masterfromfilmor:modernisation-light
Closed
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@
*.dll
*.exe
*.pdb
pythonnet/dlls/

### JetBrains ###
.idea/
Expand Down
7 changes: 0 additions & 7 deletionsNuGet.config
View file
Open in desktop

This file was deleted.

16 changes: 16 additions & 0 deletionsPython.Loader/InternalDllImportResolver.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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.
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsPython.Runtime/Python.Runtime.csproj
View file
Open in desktop
Original file line numberDiff line numberDiff 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.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,16 +13,40 @@ namespace Python.Runtime
/// </summary>
internalclassCodeGenerator
{
privateAssemblyBuilderaBuilder;
privateModuleBuildermBuilder;
privateAssemblyBuilder_aBuilder=null;

internalCodeGenerator()
privateAssemblyBuilderaBuilder
{
get
{
if(_aBuilder==null)
{
varaname=newAssemblyName{Name="__CodeGenerator_Assembly"};
varaa=AssemblyBuilderAccess.Run;

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

return_aBuilder;
}
}

privateModuleBuilder_mBuilder=null;
privateModuleBuildermBuilder
{
varaname=newAssemblyName{Name="__CodeGenerator_Assembly"};
varaa=AssemblyBuilderAccess.Run;
get
{
if(_mBuilder==null)
{
_mBuilder=aBuilder.DefineDynamicModule("__CodeGenerator_Module");
}

return_mBuilder;
}
}

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

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

[8]ページ先頭

©2009-2025 Movatter.jp