Skip to content

object.GetRawPythonProxy() extension, that bypasses all conversions #1078

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 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Added function that sets Py_NoSiteFlag to 1.
- Added support for Jetson Nano.
- Added support for __len__ for .NET classes that implement ICollection
- Added `object.GetRawPythonProxy() -> PyObject` extension method, that bypasses any conversions

### Changed

Expand All @@ -21,6 +22,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Removes PyLong_GetMax and PyClass_New when targetting Python3
- Added support for converting python iterators to C# arrays
- Changed usage of obselete function GetDelegateForFunctionPointer(IntPtr, Type) to GetDelegateForFunctionPointer<TDelegate>(IntPtr)
- When calling C# from Python, enable passing argument of any type to a parameter of C# type `object` by wrapping it into `PyObject` instance. ([#881][i881])
- Added support for kwarg parameters when calling .NET methods from Python

### Fixed
Expand Down Expand Up @@ -58,7 +60,6 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Reattach python exception traceback information (#545)
- PythonEngine.Intialize will now call `Py_InitializeEx` with a default value of 0, so signals will not be configured by default on embedding. This is different from the previous behaviour, where `Py_Initialize` was called instead, which sets initSigs to 1. ([#449][i449])
- Refactored MethodBinder.Bind in preparation to make it extensible (#829)
- When calling C# from Python, enable passing argument of any type to a parameter of C# type `object` by wrapping it into `PyObject` instance. ([#881][i881])
- Look for installed Windows 10 sdk's during installation instead of relying on specific versions.

### Fixed
Expand Down
3 changes: 3 additions & 0 deletions pythonnet.15.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Repo", "Repo", "{441A0123-F4C6-4EE4-9AEE-315FD79BE2D5}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitignore = .gitignore
CHANGELOG.md = CHANGELOG.md
README.rst = README.rst
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CI", "CI", "{D301657F-5EAF-4534-B280-B858D651B2E5}"
Expand Down
23 changes: 23 additions & 0 deletions src/embed_tests/TestConverter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Python.Runtime;

Expand Down Expand Up @@ -44,5 +46,26 @@ public void TestConvertDoubleToManaged(
Assert.IsTrue(converted);
Assert.IsTrue(((double) convertedValue).Equals(testValue));
}

[Test]
public void RawListProxy()
{
var list = new List<string> {"hello", "world"};
var listProxy = list.GetRawPythonProxy();
var clrObject = (CLRObject)ManagedType.GetManagedObject(listProxy.Handle);
Assert.AreSame(list, clrObject.inst);
}

[Test]
public void RawPyObjectProxy()
{
var pyObject = "hello world!".ToPython();
var pyObjectProxy = pyObject.GetRawPythonProxy();
var clrObject = (CLRObject)ManagedType.GetManagedObject(pyObjectProxy.Handle);
Assert.AreSame(pyObject, clrObject.inst);

var proxiedHandle = pyObjectProxy.GetAttr("Handle").As<IntPtr>();
Assert.AreEqual(pyObject.Handle, proxiedHandle);
}
}
}
6 changes: 6 additions & 0 deletions src/runtime/NewReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public void Dispose()
this.pointer = IntPtr.Zero;
}

/// <summary>
/// Creates <see cref="NewReference"/> from a raw pointer
/// </summary>
public static NewReference DangerousFromPointer(IntPtr pointer)
=> new NewReference {pointer = pointer};

[Pure]
internal static IntPtr DangerousGetAddress(in NewReference reference)
=> IsNull(reference) ? throw new NullReferenceException() : reference.pointer;
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/clrobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,17 @@ internal static IntPtr GetInstHandle(object ob)
CLRObject co = GetInstance(ob);
return co.pyHandle;
}

/// <summary>
/// Creates <see cref="CLRObject"/> proxy for the given object,
/// and returns a <see cref="NewReference"/> to it.
/// </summary>
internal static NewReference MakeNewReference(object obj)
{
if (obj is null) throw new ArgumentNullException(nameof(obj));

// TODO: CLRObject currently does not have Dispose or finalizer which might change in the future
return NewReference.DangerousFromPointer(GetInstHandle(obj));
}
}
}
11 changes: 11 additions & 0 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -967,5 +967,16 @@ public static PyObject ToPython(this object o)
{
return new PyObject(Converter.ToPython(o, o?.GetType()));
}

/// <summary>
/// Gets raw Python proxy for this object (bypasses all conversions,
/// except <c>null</c> &lt;==&gt; <c>None</c>)
/// </summary>
public static PyObject GetRawPythonProxy(this object o)
{
if (o is null) return new PyObject(new BorrowedReference(Runtime.PyNone));

return CLRObject.MakeNewReference(o).MoveToPyObject();
}
}
}