Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

Commit 8a70f09

Browse files
committed
Python CoreCLR debugged under windows.
1 parent 65d1eda commit 8a70f09

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/Python.Runtime/pythonengine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public static void Initialize()
148148
IntPtr builtins = Runtime.PyEval_GetBuiltins();
149149
Runtime.PyDict_SetItemString(module_globals, "__builtins__", builtins);
150150

151-
var assembly = Assembly.GetEntryAssembly();
151+
var assembly = typeof(Runtime).GetTypeInfo().Assembly;
152152
using (Stream stream = assembly.GetManifestResourceStream("Python.Runtime.resources.clr.py"))
153153
using (StreamReader reader = new StreamReader(stream))
154154
{
@@ -257,6 +257,7 @@ public static void Shutdown()
257257
{
258258
if (initialized)
259259
{
260+
Runtime.Py_Main(3, new[] { "some.exe", "-c", "exit" });
260261
Runtime.Shutdown();
261262
initialized = false;
262263
}

src/Python.Runtime/runtime.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Runtime.InteropServices;
33
using System.Security;
44
using ReflectionBridge.Extensions;
5+
using System.Linq;
56

67
#if (UCS4)
78
using System.Text;
@@ -703,10 +704,38 @@ internal unsafe static extern IntPtr
703704
PyGILState_GetThisThreadState();
704705

705706
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35)
706-
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
707-
ExactSpelling=true, CharSet=CharSet.Ansi)]
708-
public unsafe static extern int
709-
Py_Main(int argc, [MarshalAsAttribute(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPWStr)] string[] argv);
707+
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
708+
ExactSpelling = true, CharSet = CharSet.Ansi)]
709+
private unsafe static extern int Py_Main(int argc, [MarshalAsAttribute(UnmanagedType.SysUInt)] IntPtr lplpargv);
710+
711+
public unsafe static int Py_Main(int argc, string[] argv)
712+
{
713+
// Totally ignoring argc.
714+
argc = argv.Length;
715+
716+
int allStringsLength = argv.Sum(x => x.Length + 1);
717+
int requiredSize = IntPtr.Size * argc + allStringsLength * UCS;
718+
IntPtr mem = Marshal.AllocHGlobal(requiredSize);
719+
try
720+
{
721+
// Preparing array of pointers to UTF32 strings.
722+
IntPtr curStrPtr = mem + argc * IntPtr.Size;
723+
for (int i = 0; i < argv.Length; i++)
724+
{
725+
Encoding enc = UCS == 2 ? Encoding.Unicode : Encoding.UTF32;
726+
byte[] zstr = enc.GetBytes(argv[i] + "\0");
727+
Marshal.Copy(zstr, 0, curStrPtr, zstr.Length);
728+
Marshal.WriteIntPtr(mem + IntPtr.Size * i, curStrPtr);
729+
curStrPtr += zstr.Length;
730+
}
731+
return Py_Main(argc, mem);
732+
}
733+
finally
734+
{
735+
Marshal.FreeHGlobal(mem);
736+
}
737+
}
738+
710739
#else
711740
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
712741
ExactSpelling = true, CharSet = CharSet.Ansi)]

0 commit comments

Comments
 (0)