Skip to content

Prevent crash during debugging when attempting to inspect PyObject #1483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions src/runtime/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ internal static class Util
{
internal const string UnstableApiMessage =
"This API is unstable, and might be changed or removed in the next minor release";
internal const string MinimalPythonVersionRequired =
"Only Python 3.5 or newer is supported";

internal static Int64 ReadCLong(IntPtr tp, int offset)
{
Expand Down
1 change: 1 addition & 0 deletions src/runtime/clrobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Python.Runtime
{
[Serializable]
[DebuggerDisplay("clrO: {inst}")]
internal class CLRObject : ManagedType
{
internal object inst;
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/debughelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,13 @@ public static void AssertHasReferences(IntPtr obj)
long refcount = Runtime.Refcount(obj);
Debug.Assert(refcount > 0, "Object refcount is 0 or less");
}

[Conditional("DEBUG")]
public static void EnsureGIL()
{
Debug.Assert(HaveInterpreterLock(), "GIL must be acquired");
}

public static bool HaveInterpreterLock() => Runtime.PyGILState_Check() == 1;
}
}
5 changes: 5 additions & 0 deletions src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Python.Runtime
/// for details.
/// </summary>
[Serializable]
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
public partial class PyObject : DynamicObject, IEnumerable<PyObject>, IDisposable
{
#if TRACE_ALLOC
Expand Down Expand Up @@ -1069,6 +1070,10 @@ public override string ToString()
return result;
}

string DebuggerDisplay => DebugUtil.HaveInterpreterLock()
? this.ToString()
: $"pyobj at 0x{this.obj:X} (get Py.GIL to see more info)";


/// <summary>
/// Equals Method
Expand Down
11 changes: 10 additions & 1 deletion src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ internal static unsafe long Refcount(IntPtr op)

internal static IntPtr PyThreadState_Swap(IntPtr key) => Delegates.PyThreadState_Swap(key);


internal static int PyGILState_Check() => Delegates.PyGILState_Check();
internal static IntPtr PyGILState_Ensure() => Delegates.PyGILState_Ensure();


Expand Down Expand Up @@ -2302,6 +2302,14 @@ static Delegates()
PyThread_get_thread_ident = (delegate* unmanaged[Cdecl]<int>)GetFunctionByName(nameof(PyThread_get_thread_ident), GetUnmanagedDll(_PythonDll));
PyThread_set_key_value = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr, int>)GetFunctionByName(nameof(PyThread_set_key_value), GetUnmanagedDll(_PythonDll));
PyThreadState_Swap = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyThreadState_Swap), GetUnmanagedDll(_PythonDll));
try
{
PyGILState_Check = (delegate* unmanaged[Cdecl]<int>)GetFunctionByName(nameof(PyGILState_Check), GetUnmanagedDll(_PythonDll));
}
catch (MissingMethodException e)
{
throw new NotSupportedException(Util.MinimalPythonVersionRequired, innerException: e);
}
PyGILState_Ensure = (delegate* unmanaged[Cdecl]<IntPtr>)GetFunctionByName(nameof(PyGILState_Ensure), GetUnmanagedDll(_PythonDll));
PyGILState_Release = (delegate* unmanaged[Cdecl]<IntPtr, void>)GetFunctionByName(nameof(PyGILState_Release), GetUnmanagedDll(_PythonDll));
PyGILState_GetThisThreadState = (delegate* unmanaged[Cdecl]<IntPtr>)GetFunctionByName(nameof(PyGILState_GetThisThreadState), GetUnmanagedDll(_PythonDll));
Expand Down Expand Up @@ -2605,6 +2613,7 @@ static Delegates()
internal static delegate* unmanaged[Cdecl]<int> PyThread_get_thread_ident { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr, int> PyThread_set_key_value { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyThreadState_Swap { get; }
internal static delegate* unmanaged[Cdecl]<int> PyGILState_Check { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr> PyGILState_Ensure { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr, void> PyGILState_Release { get; }
internal static delegate* unmanaged[Cdecl]<IntPtr> PyGILState_GetThisThreadState { get; }
Expand Down