File tree Expand file tree Collapse file tree 6 files changed +57
-0
lines changed Expand file tree Collapse file tree 6 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
13
13
- Added function that sets Py_NoSiteFlag to 1.
14
14
- Added support for Jetson Nano.
15
15
- Added support for __ len__ for .NET classes that implement ICollection
16
+ - Added ` object.GetRawPythonProxy() -> PyObject ` extension method, that bypasses any conversions
16
17
17
18
### Changed
18
19
Original file line number Diff line number Diff line change @@ -17,6 +17,9 @@ EndProject
17
17
Project ("{2150E333-8FDC-42A3-9474-1A3956D46DE8}" ) = "Repo" , "Repo" , "{441A0123-F4C6-4EE4-9AEE-315FD79BE2D5}"
18
18
Project Section (SolutionItems ) = preProject
19
19
.editorconfig = .editorconfig
20
+ .gitignore = .gitignore
21
+ CHANGELOG .md = CHANGELOG .md
22
+ README .rst = README .rst
20
23
EndProject Section
21
24
EndProject
22
25
Project ("{2150E333-8FDC-42A3-9474-1A3956D46DE8}" ) = "CI" , "CI" , "{D301657F-5EAF-4534-B280-B858D651B2E5}"
Original file line number Diff line number Diff line change
1
+ using System . Collections . Generic ;
1
2
using NUnit . Framework ;
2
3
using Python . Runtime ;
3
4
4
5
namespace Python . EmbeddingTest
5
6
{
7
+ using System ;
8
+
6
9
public class TestConverter
7
10
{
8
11
[ OneTimeSetUp ]
@@ -44,5 +47,26 @@ public void TestConvertDoubleToManaged(
44
47
Assert . IsTrue ( converted ) ;
45
48
Assert . IsTrue ( ( ( double ) convertedValue ) . Equals ( testValue ) ) ;
46
49
}
50
+
51
+ [ Test ]
52
+ public void RawListProxy ( )
53
+ {
54
+ var list = new List < string > { "hello" , "world" } ;
55
+ var listProxy = list . GetRawPythonProxy ( ) ;
56
+ var clrObject = ( CLRObject ) ManagedType . GetManagedObject ( listProxy . Handle ) ;
57
+ Assert . AreSame ( list , clrObject . inst ) ;
58
+ }
59
+
60
+ [ Test ]
61
+ public void RawPyObjectProxy ( )
62
+ {
63
+ var pyObject = "hello world!" . ToPython ( ) ;
64
+ var pyObjectProxy = pyObject . GetRawPythonProxy ( ) ;
65
+ var clrObject = ( CLRObject ) ManagedType . GetManagedObject ( pyObjectProxy . Handle ) ;
66
+ Assert . AreSame ( pyObject , clrObject . inst ) ;
67
+
68
+ var proxiedHandle = pyObjectProxy . GetAttr ( "Handle" ) . As < IntPtr > ( ) ;
69
+ Assert . AreEqual ( pyObject . Handle , proxiedHandle ) ;
70
+ }
47
71
}
48
72
}
Original file line number Diff line number Diff line change @@ -33,6 +33,12 @@ public void Dispose()
33
33
this . pointer = IntPtr . Zero ;
34
34
}
35
35
36
+ /// <summary>
37
+ /// Creates <see cref="NewReference"/> from a raw pointer
38
+ /// </summary>
39
+ public static NewReference DangerousFromPointer ( IntPtr pointer )
40
+ => new NewReference { pointer = pointer } ;
41
+
36
42
[ Pure ]
37
43
internal static IntPtr DangerousGetAddress ( in NewReference reference )
38
44
=> IsNull ( reference ) ? throw new NullReferenceException ( ) : reference . pointer ;
Original file line number Diff line number Diff line change @@ -68,5 +68,17 @@ internal static IntPtr GetInstHandle(object ob)
68
68
CLRObject co = GetInstance ( ob ) ;
69
69
return co . pyHandle ;
70
70
}
71
+
72
+ /// <summary>
73
+ /// Creates <see cref="CLRObject"/> proxy for the given object,
74
+ /// and returns a <see cref="NewReference"/> to it.
75
+ /// </summary>
76
+ internal static NewReference MakeNewReference ( object obj )
77
+ {
78
+ if ( obj is null ) throw new ArgumentNullException ( nameof ( obj ) ) ;
79
+
80
+ // TODO: CLRObject currently does not have Dispose or finalizer which might change in the future
81
+ return NewReference . DangerousFromPointer ( GetInstHandle ( obj ) ) ;
82
+ }
71
83
}
72
84
}
Original file line number Diff line number Diff line change @@ -967,5 +967,16 @@ public static PyObject ToPython(this object o)
967
967
{
968
968
return new PyObject ( Converter . ToPython ( o , o ? . GetType ( ) ) ) ;
969
969
}
970
+
971
+ /// <summary>
972
+ /// Gets raw Python proxy for this object (bypasses all conversions,
973
+ /// except <c>null</c> <==> <c>None</c>)
974
+ /// </summary>
975
+ public static PyObject GetRawPythonProxy ( this object o )
976
+ {
977
+ if ( o is null ) return new PyObject ( new BorrowedReference ( Runtime . PyNone ) ) ;
978
+
979
+ return CLRObject . MakeNewReference ( o ) . MoveToPyObject ( ) ;
980
+ }
970
981
}
971
982
}
You can’t perform that action at this time.
0 commit comments