Skip to content

Removes new object.GetRawPythonProxy extension method #1132

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
May 1, 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
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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
- Added PythonException.Format method to format exceptions the same as traceback.format_exception

### Changed
Expand Down
2 changes: 1 addition & 1 deletion src/embed_tests/Codecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static void TupleRoundtripGeneric<T, TTuple>() {
class ObjectToEncoderInstanceEncoder<T> : IPyObjectEncoder
{
public bool CanEncode(Type type) => type == typeof(T);
public PyObject TryEncode(object value) => this.GetRawPythonProxy();
public PyObject TryEncode(object value) => PyObject.FromManagedObject(this);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/embed_tests/TestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void TestConvertDoubleToManaged(
public void RawListProxy()
{
var list = new List<string> {"hello", "world"};
var listProxy = list.GetRawPythonProxy();
var listProxy = PyObject.FromManagedObject(list);
var clrObject = (CLRObject)ManagedType.GetManagedObject(listProxy.Handle);
Assert.AreSame(list, clrObject.inst);
}
Expand All @@ -60,7 +60,7 @@ public void RawListProxy()
public void RawPyObjectProxy()
{
var pyObject = "hello world!".ToPython();
var pyObjectProxy = pyObject.GetRawPythonProxy();
var pyObjectProxy = PyObject.FromManagedObject(pyObject);
var clrObject = (CLRObject)ManagedType.GetManagedObject(pyObjectProxy.Handle);
Assert.AreSame(pyObject, clrObject.inst);

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/Codecs/RawProxyEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public PyObject TryEncode(object value)
{
if (value is null) throw new ArgumentNullException(nameof(value));

return value.GetRawPythonProxy();
return PyObject.FromManagedObject(value);
}

public virtual bool CanEncode(Type type) => false;
Expand Down
12 changes: 0 additions & 12 deletions src/runtime/clrobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,5 @@ 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: 0 additions & 11 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -967,16 +967,5 @@ 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();
}
}
}
3 changes: 2 additions & 1 deletion src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public IntPtr Handle


/// <summary>
/// FromManagedObject Method
/// Gets raw Python proxy for this object (bypasses all conversions,
/// except <c>null</c> &lt;==&gt; <c>None</c>)
/// </summary>
/// <remarks>
/// Given an arbitrary managed object, return a Python instance that
Expand Down