Skip to content

Commit 676fbb4

Browse files
committed
PyObject.Length raises an exception when object does not have a concept of length
1 parent 6f10641 commit 676fbb4

File tree

3 files changed

+8
-17
lines changed

3 files changed

+8
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ when .NET expects an integer [#1342][i1342]
3636
- BREAKING: Methods with `ref` or `out` parameters and void return type return a tuple of only the `ref` and `out` parameters.
3737
- BREAKING: to call Python from .NET `Runtime.PythonDLL` property must be set to Python DLL name
3838
or the DLL must be loaded in advance. This must be done before calling any other Python.NET functions.
39+
- BREAKING: `PyObject.Length()` now raises a `PythonException` when object does not support a concept of length.
3940
- Sign Runtime DLL with a strong name
4041
- Implement loading through `clr_loader` instead of the included `ClrModule`, enables
4142
support for .NET Core

src/runtime/pyobject.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -615,19 +615,15 @@ public virtual void DelItem(int index)
615615

616616

617617
/// <summary>
618-
/// Length Method
619-
/// </summary>
620-
/// <remarks>
621618
/// Returns the length for objects that support the Python sequence
622-
/// protocol, or 0 if the object does not support the protocol.
623-
/// </remarks>
619+
/// protocol.
620+
/// </summary>
624621
public virtual long Length()
625622
{
626-
var s = Runtime.PyObject_Size(obj);
623+
var s = Runtime.PyObject_Size(Reference);
627624
if (s < 0)
628625
{
629-
Runtime.PyErr_Clear();
630-
return 0;
626+
throw new PythonException();
631627
}
632628
return s;
633629
}

src/runtime/runtime.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,13 +1115,7 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2)
11151115

11161116
internal static int PyObject_Not(IntPtr pointer) => Delegates.PyObject_Not(pointer);
11171117

1118-
internal static long PyObject_Size(IntPtr pointer)
1119-
{
1120-
return (long)_PyObject_Size(pointer);
1121-
}
1122-
1123-
1124-
private static IntPtr _PyObject_Size(IntPtr pointer) => Delegates._PyObject_Size(pointer);
1118+
internal static nint PyObject_Size(BorrowedReference pointer) => Delegates.PyObject_Size(pointer);
11251119

11261120

11271121
internal static nint PyObject_Hash(IntPtr op) => Delegates.PyObject_Hash(op);
@@ -2317,7 +2311,7 @@ static Delegates()
23172311
PyCallable_Check = (delegate* unmanaged[Cdecl]<IntPtr, int>)GetFunctionByName(nameof(PyCallable_Check), GetUnmanagedDll(_PythonDll));
23182312
PyObject_IsTrue = (delegate* unmanaged[Cdecl]<BorrowedReference, int>)GetFunctionByName(nameof(PyObject_IsTrue), GetUnmanagedDll(_PythonDll));
23192313
PyObject_Not = (delegate* unmanaged[Cdecl]<IntPtr, int>)GetFunctionByName(nameof(PyObject_Not), GetUnmanagedDll(_PythonDll));
2320-
_PyObject_Size = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName("PyObject_Size", GetUnmanagedDll(_PythonDll));
2314+
PyObject_Size = (delegate* unmanaged[Cdecl]<BorrowedReference, nint>)GetFunctionByName("PyObject_Size", GetUnmanagedDll(_PythonDll));
23212315
PyObject_Hash = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Hash), GetUnmanagedDll(_PythonDll));
23222316
PyObject_Repr = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Repr), GetUnmanagedDll(_PythonDll));
23232317
PyObject_Str = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Str), GetUnmanagedDll(_PythonDll));
@@ -2589,7 +2583,7 @@ static Delegates()
25892583
internal static delegate* unmanaged[Cdecl]<IntPtr, int> PyCallable_Check { get; }
25902584
internal static delegate* unmanaged[Cdecl]<BorrowedReference, int> PyObject_IsTrue { get; }
25912585
internal static delegate* unmanaged[Cdecl]<IntPtr, int> PyObject_Not { get; }
2592-
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> _PyObject_Size { get; }
2586+
internal static delegate* unmanaged[Cdecl]<BorrowedReference, nint> PyObject_Size { get; }
25932587
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Hash { get; }
25942588
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Repr { get; }
25952589
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Str { get; }

0 commit comments

Comments
 (0)