Skip to content
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
Prev Previous commit
Next Next commit
implemented NativeTypeOffset for installation with setup.py
  • Loading branch information
lostmsu committed Nov 29, 2020
commit 6dd92ad9fb3300042ca2551f312463346f1c3a96
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/src/runtime/interopNative.cs

# General binaries and Build results
*.dll
*.exe
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/interop36.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Auto-generated by geninterop.py.
// DO NOT MODIFY BY HAND.

// Python 3.6: ABI flags: ''

// ReSharper disable InconsistentNaming
// ReSharper disable IdentifierTypo

Expand Down
2 changes: 2 additions & 0 deletions src/runtime/interop37.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Auto-generated by geninterop.py.
// DO NOT MODIFY BY HAND.

// Python 3.7: ABI flags: ''

// ReSharper disable InconsistentNaming
// ReSharper disable IdentifierTypo

Expand Down
2 changes: 2 additions & 0 deletions src/runtime/interop38.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Auto-generated by geninterop.py.
// DO NOT MODIFY BY HAND.

// Python 3.8: ABI flags: ''

// ReSharper disable InconsistentNaming
// ReSharper disable IdentifierTypo

Expand Down
2 changes: 2 additions & 0 deletions src/runtime/interop39.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Auto-generated by geninterop.py.
// DO NOT MODIFY BY HAND.

// Python 3.9: ABI flags: ''

// ReSharper disable InconsistentNaming
// ReSharper disable IdentifierTypo

Expand Down
6 changes: 5 additions & 1 deletion src/runtime/native/ABI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ internal static void Initialize(Version version, BorrowedReference pyType)

var thisAssembly = Assembly.GetExecutingAssembly();

const string nativeTypeOffsetClassName = "Python.Runtime.NativeTypeOffset";
Comment thread
lostmsu marked this conversation as resolved.
string className = "Python.Runtime.TypeOffset" + offsetsClassSuffix;
Type typeOffsetsClass = thisAssembly.GetType(className, throwOnError: false);
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)
throw new NotSupportedException($"Python ABI v{version} is not supported");
var typeOffsets = (ITypeOffsets)Activator.CreateInstance(typeOffsetsClass);
Expand Down
29 changes: 12 additions & 17 deletions tools/geninterop/geninterop.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,15 @@ def preprocess_python_headers():


def gen_interop_head(writer):
if hasattr(sys, "abiflags"):
if "d" in sys.abiflags:
defines.append("PYTHON_WITH_PYDEBUG")
raise NotImplementedError
if "m" in sys.abiflags:
defines.append("PYTHON_WITH_PYMALLOC")
raise NotImplementedError
if "u" in sys.abiflags:
defines.append("PYTHON_WITH_WIDE_UNICODE")
raise NotImplementedError

filename = os.path.basename(__file__)
abi_flags = getattr(sys, "abiflags", "")
py_ver = "{0}.{1}".format(PY_MAJOR, PY_MINOR)
class_definition = """
// Auto-generated by %s.
// DO NOT MODIFY BY HAND.

// Python %s: ABI flags: '%s'

// ReSharper disable InconsistentNaming
// ReSharper disable IdentifierTypo

Expand All @@ -270,7 +263,7 @@ def gen_interop_head(writer):
using Python.Runtime.Native;

namespace Python.Runtime
{""" % filename
{""" % (filename, py_ver, abi_flags)
writer.extend(class_definition)


Expand All @@ -281,20 +274,21 @@ def gen_interop_tail(writer):
writer.extend(tail)


def gen_heap_type_members(parser, writer):
def gen_heap_type_members(parser, writer, type_name = None):
"""Generate the TypeOffset C# class"""
members = parser.get_struct_members("PyHeapTypeObject")
type_name = type_name or "TypeOffset{0}{1}".format(PY_MAJOR, PY_MINOR)
class_definition = """
[SuppressMessage("Style", "IDE1006:Naming Styles",
Justification = "Following CPython",
Scope = "type")]

[StructLayout(LayoutKind.Sequential)]
internal class TypeOffset{0}{1} : GeneratedTypeOffsets, ITypeOffsets
internal class {0} : GeneratedTypeOffsets, ITypeOffsets
{{
public TypeOffset{0}{1}() {{ }}
public {0}() {{ }}
// Auto-generated from PyHeapTypeObject in Python.h
""".format(PY_MAJOR, PY_MINOR)
""".format(type_name)

# All the members are sizeof(void*) so we don't need to do any
# extra work to determine the size based on the type.
Expand Down Expand Up @@ -347,9 +341,10 @@ def main():

writer = Writer()
# generate the C# code
offsets_type_name = "NativeTypeOffset" if len(sys.argv) > 1 else None
gen_interop_head(writer)

gen_heap_type_members(ast_parser, writer)
gen_heap_type_members(ast_parser, writer, type_name = offsets_type_name)

ver_define = "PYTHON{0}{1}".format(PY_MAJOR, PY_MINOR)
writer.append(code="#if " + ver_define)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these still behind a compile-time flag? Do you plan to change that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I am approaching this gradually.

Copy link
Copy Markdown
Member Author

@lostmsu lostmsu Nov 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this out, and the rest of the conditional stuff came from 1b466df. Since it was only used to determine if methods in Python.Runtime itself implement slots (which we can do on our own), I took a liberty to mostly revert it in this PR. Reversal is in this commit

Expand Down