Skip to content

Commit 014dde5

Browse files
committed
Other interop parts of Python.Runtime.dll moved to Python.Runtime.Interop.dll
1 parent e2576cf commit 014dde5

21 files changed

+504
-294
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
3+
namespace Python.Runtime.Interop
4+
{
5+
using System.Reflection;
6+
using System.Runtime.InteropServices;
7+
8+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
9+
internal class ExceptionOffset
10+
{
11+
static ExceptionOffset()
12+
{
13+
Type type = typeof(ExceptionOffset);
14+
FieldInfo[] fi = type.GetFields();
15+
int size = IntPtr.Size;
16+
for (int i = 0; i < fi.Length; i++)
17+
{
18+
fi[i].SetValue(null, (i * size) + ObjectOffset.ob_type + size);
19+
}
20+
}
21+
22+
// PyException_HEAD
23+
// (start after PyObject_HEAD)
24+
public static int dict = 0;
25+
public static int args = 0;
26+
#if (PYTHON25 || PYTHON26 || PYTHON27)
27+
public static int message = 0;
28+
#elif (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35)
29+
public static int traceback = 0;
30+
public static int context = 0;
31+
public static int cause = 0;
32+
#if !PYTHON32
33+
public static int suppress_context = 0;
34+
#endif
35+
#endif
36+
37+
// extra c# data
38+
public static int ob_dict;
39+
public static int ob_data;
40+
}
41+
}

src/Python.Runtime.Interop/PythonInterop.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace Python.Runtime
22
{
3+
using System;
4+
using System.Linq;
5+
using System.Reflection;
36
using System.Runtime.InteropServices;
47

58
using Python.Runtime.InteropContracts;
@@ -35,5 +38,32 @@ public bool IsPyDebug
3538
#endif
3639
}
3740
}
41+
42+
public void InitTypeOffset()
43+
{
44+
CopyStaticFields<Interop.TypeOffset, TypeOffset>();
45+
}
46+
47+
private void CopyStaticFields<TFrom, TTo>()
48+
{
49+
Type type = typeof(TFrom);
50+
var actualValues = type.GetFields().ToDictionary(x => x.Name);
51+
52+
Type targetType = typeof(TTo);
53+
var targetFields = targetType.GetFields();
54+
for (int i = 0; i < targetFields.Length; i++)
55+
{
56+
FieldInfo actualField;
57+
if (actualValues.TryGetValue(targetFields[i].Name, out actualField))
58+
{
59+
targetFields[i].SetValue(null, actualField.GetValue(null));
60+
}
61+
}
62+
}
63+
64+
public void InitExceptionOffset()
65+
{
66+
CopyStaticFields<Interop.ExceptionOffset, ExceptionOffset>();
67+
}
3868
}
3969
}

src/Python.Runtime/interop26.cs renamed to src/Python.Runtime.Interop/interop26.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using System.Reflection;
1212
using System.Text;
1313

14-
namespace Python.Runtime {
14+
namespace Python.Runtime.Interop {
1515

1616
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
1717
internal class TypeOffset {

src/Python.Runtime/interop27.cs renamed to src/Python.Runtime.Interop/interop27.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using System.Reflection;
1010
using System.Text;
1111

12-
namespace Python.Runtime
12+
namespace Python.Runtime.Interop
1313
{
1414
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
1515
internal class TypeOffset

src/Python.Runtime/interop32.cs renamed to src/Python.Runtime.Interop/interop32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using System.Reflection;
1212
using System.Text;
1313

14-
namespace Python.Runtime {
14+
namespace Python.Runtime.Interop {
1515

1616
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
1717
internal class TypeOffset {

src/Python.Runtime/interop33.cs renamed to src/Python.Runtime.Interop/interop33.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11

22
// Auto-generated by geninterop.py.
33
// DO NOT MODIFIY BY HAND.
4-
4+
#if (PYTHON33)
55
using System;
66
using System.Collections;
77
using System.Collections.Specialized;
88
using System.Runtime.InteropServices;
99
using System.Reflection;
1010
using System.Text;
1111

12-
namespace Python.Runtime {
12+
namespace Python.Runtime.Interop {
1313

1414
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
1515
internal class TypeOffset {
@@ -135,3 +135,4 @@ public static int magic() {
135135
public static int members = 0;
136136
}
137137
}
138+
#endif

src/Python.Runtime/interop34.cs renamed to src/Python.Runtime.Interop/interop34.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using System.Reflection;
1212
using System.Text;
1313

14-
namespace Python.Runtime {
14+
namespace Python.Runtime.Interop {
1515

1616
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
1717
internal class TypeOffset {

src/Python.Runtime/interop35.cs renamed to src/Python.Runtime.Interop/interop35.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using System.Reflection;
1212
using System.Text;
1313

14-
namespace Python.Runtime {
14+
namespace Python.Runtime.Interop {
1515

1616
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
1717
internal class TypeOffset {
@@ -25,10 +25,6 @@ static TypeOffset() {
2525
}
2626
}
2727

28-
public static int magic() {
29-
return ob_size;
30-
}
31-
3228
// Auto-generated from PyHeapTypeObject in Python.h
3329
public static int ob_refcnt = 0;
3430
public static int ob_type = 0;

src/Python.Runtime/InteropContracts/IPythonInterop.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,15 @@ public interface IPythonInterop
1414
OSPlatform TargetPlatform { get; }
1515

1616
bool IsPyDebug { get; }
17+
18+
/// <summary>
19+
/// Initializes values in the <see cref="TypeOffset"/>.
20+
/// </summary>
21+
void InitTypeOffset();
22+
23+
/// <summary>
24+
/// Initializes values in the <see cref="ExceptionOffset"/>.
25+
/// </summary>
26+
void InitExceptionOffset();
1727
}
1828
}

src/Python.Runtime/InteropContracts/IPythonRuntimeInterop.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace Python.Runtime.InteropContracts
22
{
33
using System;
4-
using System.Runtime.InteropServices;
54

65
public interface IPythonRuntimeInterop
76
{

0 commit comments

Comments
 (0)