forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathABI.cs
More file actions
37 lines (33 loc) · 1.77 KB
/
ABI.cs
File metadata and controls
37 lines (33 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
namespace Python.Runtime.Native
{
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
static class ABI
{
internal static void Initialize(Version version, BorrowedReference pyType)
{
string offsetsClassSuffix = string.Format(CultureInfo.InvariantCulture,
"{0}{1}", version.Major, version.Minor);
var thisAssembly = Assembly.GetExecutingAssembly();
const string nativeTypeOffsetClassName = "Python.Runtime.NativeTypeOffset";
string className = "Python.Runtime.TypeOffset" + offsetsClassSuffix;
Type typeOffsetsClass =
// Try platform native offsets first. It is only present when generated by setup.py
thisAssembly.GetType(nativeTypeOffsetClassName, throwOnError: false)
?? thisAssembly.GetType(className, throwOnError: false);
if (typeOffsetsClass is null)
{
var types = thisAssembly.GetTypes().Select(type => type.Name).Where(name => name.StartsWith("TypeOffset"));
string message = $"Searching for {className}, found {string.Join(",", types)}. " +
"If you are building Python.NET from source, make sure you have run 'python setup.py configure' to fill in configured.props";
throw new NotSupportedException($"Python ABI v{version} is not supported: {message}");
}
var typeOffsets = (ITypeOffsets)Activator.CreateInstance(typeOffsetsClass);
TypeOffset.Use(typeOffsets);
ManagedDataOffsets.Magic = Marshal.ReadInt32(pyType.DangerousGetAddress(), TypeOffset.tp_basicsize);
}
}
}