Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
/pythonnetPublic archive
forked frompythonnet/pythonnet

Commit9fd877e

Browse files
authored
reimplemented some of the PyList members using BorrowedReference (pythonnet#1068)
1 parent8ad1062 commit9fd877e

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

‎src/embed_tests/pyimport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void SetUp()
3939

4040
IntPtrstr=Runtime.Runtime.PyString_FromString(testPath);
4141
IntPtrpath=Runtime.Runtime.PySys_GetObject("path");
42-
Runtime.Runtime.PyList_Append(path,str);
42+
Runtime.Runtime.PyList_Append(newBorrowedReference(path),str);
4343
}
4444

4545
[TearDown]

‎src/runtime/BorrowedReference.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ readonly ref struct BorrowedReference
1414
publicIntPtrDangerousGetAddress()
1515
=>this.IsNull?thrownewNullReferenceException():this.pointer;
1616

17-
BorrowedReference(IntPtrpointer)
17+
/// <summary>
18+
/// Creates new instance of <see cref="BorrowedReference"/> from raw pointer. Unsafe.
19+
/// </summary>
20+
publicBorrowedReference(IntPtrpointer)
1821
{
1922
this.pointer=pointer;
2023
}

‎src/runtime/pylist.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public static PyList AsList(PyObject value)
120120
/// </remarks>
121121
publicvoidAppend(PyObjectitem)
122122
{
123-
intr=Runtime.PyList_Append(obj,item.obj);
123+
intr=Runtime.PyList_Append(this.Reference,item.obj);
124124
if(r<0)
125125
{
126126
thrownewPythonException();
@@ -135,7 +135,7 @@ public void Append(PyObject item)
135135
/// </remarks>
136136
publicvoidInsert(intindex,PyObjectitem)
137137
{
138-
intr=Runtime.PyList_Insert(obj,index,item.obj);
138+
intr=Runtime.PyList_Insert(this.Reference,index,item.obj);
139139
if(r<0)
140140
{
141141
thrownewPythonException();
@@ -151,7 +151,7 @@ public void Insert(int index, PyObject item)
151151
/// </remarks>
152152
publicvoidReverse()
153153
{
154-
intr=Runtime.PyList_Reverse(obj);
154+
intr=Runtime.PyList_Reverse(this.Reference);
155155
if(r<0)
156156
{
157157
thrownewPythonException();
@@ -167,7 +167,7 @@ public void Reverse()
167167
/// </remarks>
168168
publicvoidSort()
169169
{
170-
intr=Runtime.PyList_Sort(obj);
170+
intr=Runtime.PyList_Sort(this.Reference);
171171
if(r<0)
172172
{
173173
thrownewPythonException();

‎src/runtime/pyobject.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public class PyObject : DynamicObject, IEnumerable, IPyDisposable
3333
privatebooldisposed=false;
3434
privatebool_finalized=false;
3535

36+
internalBorrowedReferenceReference=>newBorrowedReference(obj);
37+
3638
/// <summary>
3739
/// PyObject Constructor
3840
/// </summary>
@@ -52,9 +54,24 @@ public PyObject(IntPtr ptr)
5254
#endif
5355
}
5456

57+
/// <summary>
58+
/// Creates new <see cref="PyObject"/> pointing to the same object as
59+
/// the <paramref name="reference"/>. Increments refcount, allowing <see cref="PyObject"/>
60+
/// to have ownership over its own reference.
61+
/// </summary>
62+
internalPyObject(BorrowedReferencereference)
63+
{
64+
if(reference.IsNull)thrownewArgumentNullException(nameof(reference));
65+
66+
obj=Runtime.SelfIncRef(reference.DangerousGetAddress());
67+
#ifTRACE_ALLOC
68+
Traceback=newStackTrace(1);
69+
#endif
70+
}
71+
5572
// Protected default constructor to allow subclasses to manage
5673
// initialization in different ways as appropriate.
57-
[Obsolete("Please, always use PyObject(IntPtr)")]
74+
[Obsolete("Please, always use PyObject(*Reference)")]
5875
protectedPyObject()
5976
{
6077
#ifTRACE_ALLOC

‎src/runtime/runtime.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ internal static void Initialize(bool initSigs = false)
341341
stringrtdir=RuntimeEnvironment.GetRuntimeDirectory();
342342
IntPtrpath=PySys_GetObject("path");
343343
IntPtritem=PyString_FromString(rtdir);
344-
PyList_Append(path,item);
344+
PyList_Append(newBorrowedReference(path),item);
345345
XDecref(item);
346346
AssemblyManager.UpdatePath();
347347
}
@@ -1658,22 +1658,22 @@ internal static int PyList_SetItem(IntPtr pointer, long index, IntPtr value)
16581658
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl)]
16591659
privatestaticexternint PyList_SetItem(IntPtr pointer, IntPtr index, IntPtr value);
16601660

1661-
internalstaticint PyList_Insert(IntPtr pointer,long index, IntPtr value)
1661+
internalstaticint PyList_Insert(BorrowedReference pointer,long index, IntPtr value)
16621662
{
16631663
return PyList_Insert(pointer,new IntPtr(index), value);
16641664
}
16651665

16661666
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl)]
1667-
privatestaticexternint PyList_Insert(IntPtr pointer, IntPtr index, IntPtr value);
1667+
privatestaticexternint PyList_Insert(BorrowedReference pointer, IntPtr index, IntPtr value);
16681668

16691669
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl)]
1670-
internalstaticexternint PyList_Append(IntPtr pointer, IntPtr value);
1670+
internalstaticexternint PyList_Append(BorrowedReference pointer, IntPtr value);
16711671

16721672
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl)]
1673-
internalstaticexternint PyList_Reverse(IntPtr pointer);
1673+
internalstaticexternint PyList_Reverse(BorrowedReference pointer);
16741674

16751675
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl)]
1676-
internalstaticexternint PyList_Sort(IntPtr pointer);
1676+
internalstaticexternint PyList_Sort(BorrowedReference pointer);
16771677

16781678
internalstatic IntPtr PyList_GetSlice(IntPtr pointer,long start,long end)
16791679
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp