Skip to content

TypeOffset class no longer depends on target Python version #1292

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
TypeOffset class no longer depends on target Python version
Instead, for each supported Python version a separate class is generated (e.g. TypeOffset36). Then the Runtime picks the correct class using reflection, and copies only the necessary TypeOffset members over from it.

ManagedDataOffsets.Magic is now also read at runtime from tp_basicsize of PyType
  • Loading branch information
lostmsu committed Nov 29, 2020
commit bbc0dfbb9071dca92c491bfea679a096df269263
5 changes: 5 additions & 0 deletions pythonnet.15.sln
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "conda.recipe", "conda.recip
conda.recipe\README.md = conda.recipe\README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{BC426F42-8494-4AA5-82C9-5109ACD97BD1}"
ProjectSection(SolutionItems) = preProject
tools\geninterop\geninterop.py = tools\geninterop\geninterop.py
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
<Compile Include="Codecs\DecoderGroup.cs" />
<Compile Include="Codecs\RawProxyEncoder.cs" />
<Compile Include="Codecs\TupleCodecs.cs" />
<Compile Include="native\ABI.cs" />
<Compile Include="native\GeneratedTypeOffsets.cs" />
<Compile Include="native\ITypeOffsets.cs" />
<Compile Include="native\TypeOffset.cs" />
<Compile Include="converterextensions.cs" />
<Compile Include="finalizer.cs" />
<Compile Include="intern.cs" />
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/debughelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ internal static void DumpType(IntPtr type)
//DebugUtil.Print(" mro: ", op);


FieldInfo[] slots = typeof(TypeOffset).GetFields();
var slots = TypeOffset.GetOffsets();
int size = IntPtr.Size;

for (var i = 0; i < slots.Length; i++)
foreach (var entry in slots)
{
int offset = i * size;
name = slots[i].Name;
int offset = entry.Value;
name = entry.Key;
op = Marshal.ReadIntPtr(type, offset);
Console.WriteLine(" {0}: {1}", name, op);
}
Expand Down
23 changes: 2 additions & 21 deletions src/runtime/interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,12 @@ public ModulePropertyAttribute()

internal static partial class TypeOffset
{
static TypeOffset()
{
Type type = typeof(TypeOffset);
FieldInfo[] fields = type.GetFields();
int size = IntPtr.Size;
for (int i = 0; i < fields.Length; i++)
{
int offset = i * size;
FieldInfo fi = fields[i];
fi.SetValue(null, offset);
}
}

public static int magic() => ManagedDataOffsets.Magic;
}

internal static class ManagedDataOffsets
{
public static int Magic { get; private set; }
public static int Magic { get; internal set; }
public static readonly Dictionary<string, int> NameMapping = new Dictionary<string, int>();

static class DataOffsets
Expand All @@ -108,13 +95,7 @@ static DataOffsets()

static ManagedDataOffsets()
{
Type type = typeof(TypeOffset);
foreach (FieldInfo fi in type.GetFields())
{
NameMapping[fi.Name] = (int)fi.GetValue(null);
}
// XXX: Use the members after PyHeapTypeObject as magic slot
Magic = TypeOffset.members;
NameMapping = TypeOffset.GetOffsets();

FieldInfo[] fields = typeof(DataOffsets).GetFields(BindingFlags.Static | BindingFlags.Public);
size = fields.Length * IntPtr.Size;
Expand Down
238 changes: 120 additions & 118 deletions src/runtime/interop36.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,135 +2,137 @@
// Auto-generated by geninterop.py.
// DO NOT MODIFY BY HAND.

// ReSharper disable InconsistentNaming
// ReSharper disable IdentifierTypo

#if PYTHON36
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Text;

using Python.Runtime.Native;

namespace Python.Runtime
{
[SuppressMessage("Style", "IDE1006:Naming Styles",
Justification = "Following CPython",
Scope = "type")]

[StructLayout(LayoutKind.Sequential)]
internal static partial class TypeOffset
internal class TypeOffset36 : GeneratedTypeOffsets, ITypeOffsets
{
public TypeOffset36() { }
// Auto-generated from PyHeapTypeObject in Python.h
public static int ob_refcnt = 0;
public static int ob_type = 0;
public static int ob_size = 0;
public static int tp_name = 0;
public static int tp_basicsize = 0;
public static int tp_itemsize = 0;
public static int tp_dealloc = 0;
public static int tp_print = 0;
public static int tp_getattr = 0;
public static int tp_setattr = 0;
public static int tp_as_async = 0;
public static int tp_repr = 0;
public static int tp_as_number = 0;
public static int tp_as_sequence = 0;
public static int tp_as_mapping = 0;
public static int tp_hash = 0;
public static int tp_call = 0;
public static int tp_str = 0;
public static int tp_getattro = 0;
public static int tp_setattro = 0;
public static int tp_as_buffer = 0;
public static int tp_flags = 0;
public static int tp_doc = 0;
public static int tp_traverse = 0;
public static int tp_clear = 0;
public static int tp_richcompare = 0;
public static int tp_weaklistoffset = 0;
public static int tp_iter = 0;
public static int tp_iternext = 0;
public static int tp_methods = 0;
public static int tp_members = 0;
public static int tp_getset = 0;
public static int tp_base = 0;
public static int tp_dict = 0;
public static int tp_descr_get = 0;
public static int tp_descr_set = 0;
public static int tp_dictoffset = 0;
public static int tp_init = 0;
public static int tp_alloc = 0;
public static int tp_new = 0;
public static int tp_free = 0;
public static int tp_is_gc = 0;
public static int tp_bases = 0;
public static int tp_mro = 0;
public static int tp_cache = 0;
public static int tp_subclasses = 0;
public static int tp_weaklist = 0;
public static int tp_del = 0;
public static int tp_version_tag = 0;
public static int tp_finalize = 0;
public static int am_await = 0;
public static int am_aiter = 0;
public static int am_anext = 0;
public static int nb_add = 0;
public static int nb_subtract = 0;
public static int nb_multiply = 0;
public static int nb_remainder = 0;
public static int nb_divmod = 0;
public static int nb_power = 0;
public static int nb_negative = 0;
public static int nb_positive = 0;
public static int nb_absolute = 0;
public static int nb_bool = 0;
public static int nb_invert = 0;
public static int nb_lshift = 0;
public static int nb_rshift = 0;
public static int nb_and = 0;
public static int nb_xor = 0;
public static int nb_or = 0;
public static int nb_int = 0;
public static int nb_reserved = 0;
public static int nb_float = 0;
public static int nb_inplace_add = 0;
public static int nb_inplace_subtract = 0;
public static int nb_inplace_multiply = 0;
public static int nb_inplace_remainder = 0;
public static int nb_inplace_power = 0;
public static int nb_inplace_lshift = 0;
public static int nb_inplace_rshift = 0;
public static int nb_inplace_and = 0;
public static int nb_inplace_xor = 0;
public static int nb_inplace_or = 0;
public static int nb_floor_divide = 0;
public static int nb_true_divide = 0;
public static int nb_inplace_floor_divide = 0;
public static int nb_inplace_true_divide = 0;
public static int nb_index = 0;
public static int nb_matrix_multiply = 0;
public static int nb_inplace_matrix_multiply = 0;
public static int mp_length = 0;
public static int mp_subscript = 0;
public static int mp_ass_subscript = 0;
public static int sq_length = 0;
public static int sq_concat = 0;
public static int sq_repeat = 0;
public static int sq_item = 0;
public static int was_sq_slice = 0;
public static int sq_ass_item = 0;
public static int was_sq_ass_slice = 0;
public static int sq_contains = 0;
public static int sq_inplace_concat = 0;
public static int sq_inplace_repeat = 0;
public static int bf_getbuffer = 0;
public static int bf_releasebuffer = 0;
public static int name = 0;
public static int ht_slots = 0;
public static int qualname = 0;
public static int ht_cached_keys = 0;

/* here are optional user slots, followed by the members. */
public static int members = 0;
public int ob_refcnt { get; private set; }
public int ob_type { get; private set; }
public int ob_size { get; private set; }
public int tp_name { get; private set; }
public int tp_basicsize { get; private set; }
public int tp_itemsize { get; private set; }
public int tp_dealloc { get; private set; }
public int tp_print { get; private set; }
public int tp_getattr { get; private set; }
public int tp_setattr { get; private set; }
public int tp_as_async { get; private set; }
public int tp_repr { get; private set; }
public int tp_as_number { get; private set; }
public int tp_as_sequence { get; private set; }
public int tp_as_mapping { get; private set; }
public int tp_hash { get; private set; }
public int tp_call { get; private set; }
public int tp_str { get; private set; }
public int tp_getattro { get; private set; }
public int tp_setattro { get; private set; }
public int tp_as_buffer { get; private set; }
public int tp_flags { get; private set; }
public int tp_doc { get; private set; }
public int tp_traverse { get; private set; }
public int tp_clear { get; private set; }
public int tp_richcompare { get; private set; }
public int tp_weaklistoffset { get; private set; }
public int tp_iter { get; private set; }
public int tp_iternext { get; private set; }
public int tp_methods { get; private set; }
public int tp_members { get; private set; }
public int tp_getset { get; private set; }
public int tp_base { get; private set; }
public int tp_dict { get; private set; }
public int tp_descr_get { get; private set; }
public int tp_descr_set { get; private set; }
public int tp_dictoffset { get; private set; }
public int tp_init { get; private set; }
public int tp_alloc { get; private set; }
public int tp_new { get; private set; }
public int tp_free { get; private set; }
public int tp_is_gc { get; private set; }
public int tp_bases { get; private set; }
public int tp_mro { get; private set; }
public int tp_cache { get; private set; }
public int tp_subclasses { get; private set; }
public int tp_weaklist { get; private set; }
public int tp_del { get; private set; }
public int tp_version_tag { get; private set; }
public int tp_finalize { get; private set; }
public int am_await { get; private set; }
public int am_aiter { get; private set; }
public int am_anext { get; private set; }
public int nb_add { get; private set; }
public int nb_subtract { get; private set; }
public int nb_multiply { get; private set; }
public int nb_remainder { get; private set; }
public int nb_divmod { get; private set; }
public int nb_power { get; private set; }
public int nb_negative { get; private set; }
public int nb_positive { get; private set; }
public int nb_absolute { get; private set; }
public int nb_bool { get; private set; }
public int nb_invert { get; private set; }
public int nb_lshift { get; private set; }
public int nb_rshift { get; private set; }
public int nb_and { get; private set; }
public int nb_xor { get; private set; }
public int nb_or { get; private set; }
public int nb_int { get; private set; }
public int nb_reserved { get; private set; }
public int nb_float { get; private set; }
public int nb_inplace_add { get; private set; }
public int nb_inplace_subtract { get; private set; }
public int nb_inplace_multiply { get; private set; }
public int nb_inplace_remainder { get; private set; }
public int nb_inplace_power { get; private set; }
public int nb_inplace_lshift { get; private set; }
public int nb_inplace_rshift { get; private set; }
public int nb_inplace_and { get; private set; }
public int nb_inplace_xor { get; private set; }
public int nb_inplace_or { get; private set; }
public int nb_floor_divide { get; private set; }
public int nb_true_divide { get; private set; }
public int nb_inplace_floor_divide { get; private set; }
public int nb_inplace_true_divide { get; private set; }
public int nb_index { get; private set; }
public int nb_matrix_multiply { get; private set; }
public int nb_inplace_matrix_multiply { get; private set; }
public int mp_length { get; private set; }
public int mp_subscript { get; private set; }
public int mp_ass_subscript { get; private set; }
public int sq_length { get; private set; }
public int sq_concat { get; private set; }
public int sq_repeat { get; private set; }
public int sq_item { get; private set; }
public int was_sq_slice { get; private set; }
public int sq_ass_item { get; private set; }
public int was_sq_ass_slice { get; private set; }
public int sq_contains { get; private set; }
public int sq_inplace_concat { get; private set; }
public int sq_inplace_repeat { get; private set; }
public int bf_getbuffer { get; private set; }
public int bf_releasebuffer { get; private set; }
public int name { get; private set; }
public int ht_slots { get; private set; }
public int qualname { get; private set; }
public int ht_cached_keys { get; private set; }
}

#if PYTHON36
[StructLayout(LayoutKind.Sequential)]
internal struct PyNumberMethods
{
Expand Down Expand Up @@ -221,5 +223,5 @@ internal static partial class SlotTypes
};
}

}
#endif
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy