8000 apply amos changes from soft shutdown · pythonnet/pythonnet@630da19 · GitHub
[go: up one dir, main page]

Skip to content

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

8000
Appearance settings

Commit 630da19

Browse files
committed
apply amos changes from soft shutdown
1 parent 61d1b7c commit 630da19

File tree

6 files changed

+79
-119
lines changed

6 files changed

+79
-119
lines changed

src/runtime/interop.cs

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Runtime.InteropServices;
66
using System.Reflection;
77
using System.Text;
8-
using System.Collections.Generic;
98

109
namespace Python.Runtime
1110
{
@@ -69,27 +68,66 @@ public ModulePropertyAttribute()
6968
}
7069
}
7170

71+
internal static partial class TypeOffset
72+
{
73+
static TypeOffset()
74+
{
75+
Type type = typeof(TypeOffset);
76+
FieldInfo[] fields = type.GetFields();
77+
int size = IntPtr.Size;
78+
for (int i = 0; i < fields.Length; i++)
79+
{
80+
int offset = i * size;
81+
FieldInfo fi = fields[i];
82+
fi.SetValue(null, offset);
83+
}
84+
}
85+
86+
public static int magic() => ManagedDataOffsets.Magic;
87+
}
88+
7289
internal static class ManagedDataOffsets
7390
{
74-
static ManagedDataOffsets()
91+
public static int Magic { get; private set; }
92+
public static readonly Dictionary<string, int> NameMapping = new Dictionary<string, int>();
93+
94+
static class DataOffsets
7595
{
76-
FieldInfo[] fi = typeof(ManagedDataOffsets).GetFields(BindingFlags.Static | BindingFlags.Public);
77-
for (int i = 0; i < fi.Length; i++)
96+
public static readonly int ob_data;
97+
public static readonly int ob_dict;
98+
99+
static DataOffsets()
78100
{
79-
fi[i].SetValue(null, -(i * IntPtr.Size) - IntPtr.Size);
101+
FieldInfo[] fields = typeof(DataOffsets).GetFields(BindingFlags.Static | BindingFlags.Public);
102+
for (int i = 0; i < fields.Length; i++)
103+
{
104+
fields[i].SetValue(null, -(i * IntPtr.Size) - IntPtr.Size);
105+
}
80106
}
107+
}
81108

82-
size = fi.Length * IntPtr.Size;
109+
static ManagedDataOffsets()
110+
{
111+
Type type = typeof(TypeOffset);
112+
foreach (FieldInfo fi in type.GetFields())
113+
{
114+
NameMapping[fi.Name] = (int)fi.GetValue(null);
115+
}
116+
Magic = TypeOffset.members;
117+
FieldInfo[] fields = typeof(DataOffsets).GetFields(BindingFlags.Static | BindingFlags.Public);
118+
size = fields.Length * IntPtr.Size;
83119
}
84120

85-
public static readonly int ob_data;
86-
public static readonly int ob_dict;
121+
public static int GetSlotOffset(string name)
122+
{
123+
return NameMapping[name];
124+
}
87125

88126
private static int BaseOffset(IntPtr type)
89127
{
90128
Debug.Assert(type != IntPtr.Zero);
91129
int typeSize = Marshal.ReadInt32(type, TypeOffset.tp_basicsize);
92-
Debug.Assert(typeSize > 0 && typeSize <= ExceptionOffset.Size());
130+
Debug.Assert(typeSize > 0);
93131
return typeSize;
94132
}
95133
public static int DataOffset(IntPtr type)
@@ -102,6 +140,8 @@ public static int DictOffset(IntPtr type)
102140
return BaseOffset(type) + ob_dict;
103141
}
104142

143+
public static int ob_data => DataOffsets.ob_data;
144+
public static int ob_dict => DataOffsets.ob_dict;
105145
public static int Size { get { return size; } }
106146

107147
private static readonly int size;
@@ -320,34 +360,34 @@ public static void FreeModuleDef(IntPtr ptr)
320360
/// </summary>
321361
internal class TypeFlags
322362
{
323-
public static int HeapType = (1 << 9);
324-
public static int BaseType = (1 << 10);
325-
public static int Ready = (1 << 12);
326-
public static int Readying = (1 << 13);
327-
public static int HaveGC = (1 << 14);
363+
public const int HeapType = (1 << 9);
364+
public const int BaseType = (1 << 10);
365+
public const int Ready = (1 << 12);
366+
public const int Readying = (1 << 13);
367+
public const int HaveGC = (1 << 14);
328368
// 15 and 16 are reserved for stackless
329-
public static int HaveStacklessExtension = 0;
369+
public const int HaveStacklessExtension = 0;
330370
/* XXX Reusing reserved constants */
331-
public static int Managed = (1 << 15); // PythonNet specific
332-
public static int Subclass = (1 << 16); // PythonNet specific
333-
public static int HaveIndex = (1 << 17);
371+
public const int Managed = (1 << 15); // PythonNet specific
372+
public const int Subclass = (1 << 16); // PythonNet specific
373+
public const int HaveIndex = (1 << 17);
334374
/* Objects support nb_index in PyNumberMethods */
335-
public static int HaveVersionTag = (1 << 18);
336-
public static int ValidVersionTag = (1 << 19);
337-
public static int IsAbstract = (1 << 20);
338-
public static int HaveNewBuffer = (1 << 21);
375+
public const int HaveVersionTag = (1 << 18);
376+
public const int ValidVersionTag = (1 << 19);
377+
public const int IsAbstract = (1 << 20);
378+
public const int HaveNewBuffer = (1 << 21);
339379
// TODO: Implement FastSubclass functions
340-
public static int IntSubclass = (1 << 23);
341-
public static int LongSubclass = (1 << 24);
342-
public static int ListSubclass = (1 << 25);
343-
public static int TupleSubclass = (1 << 26);
344-
public static int StringSubclass = (1 << 27);
345-
public static int UnicodeSubclass = (1 << 28);
346-
public static int DictSubclass = (1 << 29);
347-
public static int BaseExceptionSubclass = (1 << 30);
348-
public static int TypeSubclass = (1 << 31);
349-
350-
public static int Default = (
380+
public const int IntSubclass = (1 << 23);
381+
public const int LongSubclass = (1 << 24);
382+
public const int ListSubclass = (1 << 25);
383+
public const int TupleSubclass = (1 << 26);
384+
public const int StringSubclass = (1 << 27);
385+
public const int UnicodeSubclass = (1 << 28);
386+
public const int DictSubclass = (1 << 29);
387+
public const int BaseExceptionSubclass = (1 << 30);
388+
public const int TypeSubclass = (1 << 31);
389+
390+
public const int Default = (
351391
HaveStacklessExtension |
352392
HaveVersionTag);
353393
}

src/runtime/interop34.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,9 @@
1212

1313
namespace Python.Runtime
1414
{
15-
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
15+
[StructLayout(LayoutKind.Sequential)]
1616
internal class TypeOffset
1717
{
18-
static TypeOffset()
19-
{
20-
Type type = typeof(TypeOffset);
21-
FieldInfo[] fi = type.GetFields();
22-
int size = IntPtr.Size;
23-
for (int i = 0; i < fi.Length; i++)
24-
{
25-
fi[i].SetValue(null, i * size);
26-
}
27-
}
28-
29-
public static int magic()
30-
{
31-
return ob_size;
32-
}
33-
3418
// Auto-generated from PyHeapTypeObject in Python.h
3519
public static int ob_refcnt = 0;
3620
public static int ob_type = 0;

src/runtime/interop35.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,9 @@
1212

1313
namespace Python.Runtime
1414
{
15-
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
15+
[StructLayout(LayoutKind.Sequential)]
1616
internal class TypeOffset
1717
{
18-
static TypeOffset()
19-
{
20-
Type type = typeof(TypeOffset);
21-
FieldInfo[] fi = type.GetFields();
22-
int size = IntPtr.Size;
23-
for (int i = 0; i < fi.Length; i++)
24-
{
25-
fi[i].SetValue(null, i * size);
26-
}
27-
}
28-
29-
public static int magic()
30-
{
31-
return ob_size;
32-
}
33-
3418
// Auto-generated from PyHeapTypeObject in Python.h
3519
public static int ob_refcnt = 0;
3620
public static int ob_type = 0;

src/runtime/interop36.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,9 @@
1212

1313
namespace Python.Runtime
1414
{
15-
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
15+
[StructLayout(LayoutKind.Sequential)]
1616
internal class TypeOffset
1717
{
18-
static TypeOffset()
19-
{
20-
Type type = typeof(TypeOffset);
21-
FieldInfo[] fi = type.GetFields();
22-
int size = IntPtr.Size;
23-
for (int i = 0; i < fi.Length; i++)
24-
{
25-
fi[i].SetValue(null, i * size);
26-
}
27-
}
28-
29-
public static int magic()
30-
{
31-
return ob_size;
32-
}
33-
3418
// Auto-generated from PyHeapTypeObject in Python.h
3519
public static int ob_refcnt = 0;
3620
public static int ob_type = 0;

src/runtime/interop37.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,9 @@
1212

1313
namespace Python.Runtime
1414
{
15-
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
15+
[StructLayout(LayoutKind.Sequential)]
1616
internal class TypeOffset
1717
{
18-
static TypeOffset()
19-
{
20-
Type type = typeof(TypeOffset);
21-
FieldInfo[] fi = type.GetFields();
22-
int size = IntPtr.Size;
23-
for (int i = 0; i < fi.Length; i++)
24-
{
25-
fi[i].SetValue(null, i * size);
26-
}
27-
}
28-
29-
public static int magic()
30-
{
31-
return ob_size;
32-
}
33-
3418
// Auto-generated from PyHeapTypeObject in Python.h
3519
public static int ob_refcnt = 0;
3620
public static int ob_type = 0;

src/runtime/interop38.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,9 @@
1313

1414
namespace Python.Runtime
1515
{
16-
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
17-
internal class TypeOffset
16+
[StructLayout(LayoutKind.Sequential)]
17+
internal static partial class TypeOffset
1818
{
19-
static TypeOffset()
20-
{
21-
Type type = typeof(TypeOffset);
22-
FieldInfo[] fi = type.GetFields();
23-
int size = IntPtr.Size;
24-
for (int i = 0; i < fi.Length; i++)
25-
{
26-
fi[i].SetValue(null, i * size);
27-
}
28-
}
29-
30-
public static int magic()
31-
{
32-
return ob_size;
33-
}
34-
3519
// Auto-generated from PyHeapTypeObject in Python.h
3620
public static int ob_refcnt = 0;
3721
public static int ob_type = 0;

0 commit comments

Comments
 (0)
0