Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a9ed3b

Browse files
committed
minor error fixes
1 parent 7a9e411 commit 9a9ed3b

2 files changed

Lines changed: 20 additions & 29 deletions

File tree

src/runtime/native/ABI.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,15 @@ internal static void Initialize(Version version)
3636

3737
static unsafe int GetRefCountOffset()
3838
{
39-
IntPtr tempObject = Runtime.PyList_New(0);
40-
IntPtr* tempPtr = (IntPtr*)tempObject;
39+
using var tempObject = Runtime.PyList_New(0);
40+
IntPtr* tempPtr = (IntPtr*)tempObject.DangerousGetAddress();
4141
int offset = 0;
4242
while(tempPtr[offset] != (IntPtr)1)
4343
{
4444
offset++;
4545
if (offset > 100)
4646
throw new InvalidProgramException("PyObject_HEAD could not be found withing reasonable distance from the start of PyObject");
4747
}
48-
Runtime.XDecref(tempObject);
4948
return offset * IntPtr.Size;
5049
}
5150
}

src/runtime/pybuffer.cs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ unsafe internal PyBuffer(PyObject exporter, PyBUF flags)
1515
{
1616
_view = new Py_buffer();
1717

18-
if (Runtime.PyObject_GetBuffer(exporter.Handle, ref _view, (int)flags) < 0)
18+
if (Runtime.PyObject_GetBuffer(exporter, out _view, (int)flags) < 0)
1919
{
2020
throw PythonException.ThrowLastAsClrException();
2121
}
@@ -46,25 +46,25 @@ unsafe internal PyBuffer(PyObject exporter, PyBUF flags)
4646
public int Dimensions => _view.ndim;
4747
public bool ReadOnly => _view._readonly;
4848
public IntPtr Buffer => _view.buf;
49-
public string Format => _view.format;
49+
public string? Format => _view.format;
5050

5151
/// <summary>
5252
/// An array of length <see cref="Dimensions"/> indicating the shape of the memory as an n-dimensional array.
5353
/// </summary>
54-
public long[] Shape { get; private set; }
54+
public long[]? Shape { get; private set; }
5555

5656
/// <summary>
5757
/// An array of length <see cref="Dimensions"/> giving the number of bytes to skip to get to a new element in each dimension.
5858
/// Will be null except when PyBUF_STRIDES or PyBUF_INDIRECT flags in GetBuffer/>.
5959
/// </summary>
60-
public long[] Strides { get; private set; }
60+
public long[]? Strides { get; private set; }
6161

6262
/// <summary>
6363
/// An array of Py_ssize_t of length ndim. If suboffsets[n] >= 0,
6464
/// the values stored along the nth dimension are pointers and the suboffset value dictates how many bytes to add to each pointer after de-referencing.
6565
/// A suboffset value that is negative indicates that no de-referencing should occur (striding in a contiguous memory block).
6666
/// </summary>
67-
public long[] SubOffsets { get; private set; }
67+
public long[]? SubOffsets { get; private set; }
6868

6969
private static char OrderStyleToChar(BufferOrderStyle order, bool eitherOneValid)
7070
{
@@ -162,7 +162,7 @@ internal static void FillContiguousStrides(int ndims, IntPtr shape, IntPtr strid
162162
/// If this function is used as part of a getbufferproc, exporter MUST be set to the exporting object and flags must be passed unmodified.Otherwise, exporter MUST be NULL.
163163
/// </remarks>
164164
/// <returns>On success, set view->obj to a new reference to exporter and return 0. Otherwise, raise PyExc_BufferError, set view->obj to NULL and return -1;</returns>
165-
internal void FillInfo(IntPtr exporter, IntPtr buf, long len, bool _readonly, int flags)
165+
internal void FillInfo(BorrowedReference exporter, IntPtr buf, long len, bool _readonly, int flags)
166166
{
167167
if (disposedValue)
168168
throw new ObjectDisposedException(nameof(PyBuffer));
@@ -213,9 +213,19 @@ public int Read(byte[] buffer, int offset, int count) {
213213
return copylen;
214214
}
215215

216+
~PyBuffer()
217+
{
218+
this.Dispose();
219+
Finalizer.Instance.AddFinalizedObject(ref _view.obj);
220+
}
221+
216222
private bool disposedValue = false; // To detect redundant calls
217223

218-
private void Dispose(bool disposing)
224+
/// <summary>
225+
/// Release the buffer view and decrement the reference count for view->obj. This function MUST be called when the buffer is no longer being used, otherwise reference leaks may occur.
226+
/// It is an error to call this function on a buffer that was not obtained via <see cref="PyObject.GetBuffer"/>.
227+
/// </summary>
228+
public void Dispose()
219229
{
220230
if (!disposedValue)
221231
{
@@ -225,31 +235,13 @@ private void Dispose(bool disposing)
225235
// this also decrements ref count for _view->obj
226236
Runtime.PyBuffer_Release(ref _view);
227237

228-
_exporter = null;
238+
_exporter = null!;
229239
Shape = null;
230240
Strides = null;
231241
SubOffsets = null;
232242

233243
disposedValue = true;
234244
}
235-
}
236-
237-
~PyBuffer()
238-
{
239-
if (disposedValue)
240-
{
241-
return;
242-
}
243-
Finalizer.Instance.AddFinalizedObject(ref _view.obj);
244-
}
245-
246-
/// <summary>
247-
/// Release the buffer view and decrement the reference count for view->obj. This function MUST be called when the buffer is no longer being used, otherwise reference leaks may occur.
248-
/// It is an error to call this function on a buffer that was not obtained via <see cref="PyObject.GetBuffer"/>.
249-
/// </summary>
250-
public void Dispose()
251-
{
252-
Dispose(true);
253245
GC.SuppressFinalize(this);
254246
}
255247
}

0 commit comments

Comments
 (0)