Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
PyObject.Length raises an exception when object does not have a conce…
…pt of length
  • Loading branch information
lostmsu committed Feb 26, 2021
commit 676fbb42cd904f042ed1387c95c54baf090fbc98
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ when .NET expects an integer [#1342][i1342]
- BREAKING: Methods with `ref` or `out` parameters and void return type return a tuple of only the `ref` and `out` parameters.
- BREAKING: to call Python from .NET `Runtime.PythonDLL` property must be set to Python DLL name
or the DLL must be loaded in advance. This must be done before calling any other Python.NET functions.
- BREAKING: `PyObject.Length()` now raises a `PythonException` when object does not support a concept of length.
- Sign Runtime DLL with a strong name
- Implement loading through `clr_loader` instead of the included `ClrModule`, enables
support for .NET Core
Expand Down
12 changes: 4 additions & 8 deletions src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -615,19 +615,15 @@ public virtual void DelItem(int index)


/// <summary>
/// Length Method
/// </summary>
/// <remarks>
/// Returns the length for objects that support the Python sequence
/// protocol, or 0 if the object does not support the protocol.
/// </remarks>
/// protocol.
/// </summary>
public virtual long Length()
{
var s = Runtime.PyObject_Size(obj);
var s = Runtime.PyObject_Size(Reference);
if (s < 0)
{
Runtime.PyErr_Clear();
return 0;
throw new PythonException();
}
return s;
}
Expand Down
12 changes: 3 additions & 9 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,13 +1115,7 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2)

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

internal static long PyObject_Size(IntPtr pointer)
{
return (long)_PyObject_Size(pointer);
}


private static IntPtr _PyObject_Size(IntPtr pointer) => Delegates._PyObject_Size(pointer);
internal static nint PyObject_Size(BorrowedReference pointer) => Delegates.PyObject_Size(pointer);


internal static nint PyObject_Hash(IntPtr op) => Delegates.PyObject_Hash(op);
Expand Down Expand Up @@ -2317,7 +2311,7 @@ static Delegates()
PyCallable_Check = (delegate* unmanaged[Cdecl]<IntPtr, int>)GetFunctionByName(nameof(PyCallable_Check), GetUnmanagedDll(_PythonDll));
PyObject_IsTrue = (delegate* unmanaged[Cdecl]<BorrowedReference, int>)GetFunctionByName(nameof(PyObject_IsTrue), GetUnmanagedDll(_PythonDll));
PyObject_Not = (delegate* unmanaged[Cdecl]<IntPtr, int>)GetFunctionByName(nameof(PyObject_Not), GetUnmanagedDll(_PythonDll));
_PyObject_Size = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName("PyObject_Size", GetUnmanagedDll(_PythonDll));
PyObject_Size = (delegate* unmanaged[Cdecl]<BorrowedReference, nint>)GetFunctionByName("PyObject_Size", GetUnmanagedDll(_PythonDll));
PyObject_Hash = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Hash), GetUnmanagedDll(_PythonDll));
PyObject_Repr = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Repr), GetUnmanagedDll(_PythonDll));
PyObject_Str = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Str), GetUnmanagedDll(_PythonDll));
Expand Down Expand Up @@ -2589,7 +2583,7 @@ static Delegates()
internal static delegate* unmanaged[Cdecl]<IntPtr, int> PyCallable_Check { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, int> PyObject_IsTrue { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr, int> PyObject_Not { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> _PyObject_Size { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, nint> PyObject_Size { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Hash { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Repr { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Str { get; }
Expand Down