8000 minor code cleanup · pythonnet/pythonnet@e5bce06 · GitHub
[go: up one dir, main page]

Skip to content

Commit e5bce06

Browse files
committed
minor code cleanup
1 parent ed594c1 commit e5bce06

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

src/runtime/clrobject.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal CLRObject(object ob, IntPtr tp)
3333

3434
// Fix the BaseException args (and __cause__ in case of Python 3)
3535
// slot if wrapping a CLR exception
36-
Exceptions.SetArgsAndCause(py);
36+
Exceptions.SetArgsAndCause(ObjectReference);
3737
}
3838

3939
protected CLRObject()
@@ -78,6 +78,9 @@ internal static IntPtr GetInstHandle(object ob)
7878
return co.pyHandle;
7979
}
8080

81+
internal static NewReference GetReference(object ob)
82+
=> NewReference.DangerousFromPointer(GetInstHandle(ob));
83+
8184
internal static CLRObject Restore(object ob, IntPtr pyHandle, InterDomainContext context)
8285
{
8386
CLRObject co = new CLRObject()

src/runtime/exceptions.cs

Lines changed: 8 additions & 17 deletions
< 3C0F td data-grid-cell-id="diff-d8671ce7630fc9163c6732cfe4309cc3ccb1089b33d7e9809d516f809fc36eb2-190-181-0" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">190
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,18 @@ internal ExceptionClassObject(Type tp) : base(tp)
2424
{
2525
}
2626

27-
internal static Exception ToException(IntPtr ob)
27+
internal static Exception ToException(BorrowedReference ob)
2828
{
2929
var co = GetManagedObject(ob) as CLRObject;
30-
if (co == null)
31-
{
32-
return null;
33-
}
34-
var e = co.inst as Exception;
35-
if (e == null)
36-
{
37-
return null;
38-
}
39-
return e;
30+
return co?.inst as Exception;
4031
}
4132

4233
/// <summary>
4334
/// Exception __repr__ implementation
4435
/// </summary>
4536
public new static IntPtr tp_repr(IntPtr ob)
4637
{
47-
Exception e = ToException(ob);
38+
Exception e = ToException(new BorrowedReference(ob));
4839
if (e == null)
4940
{
5041
return Exceptions.RaiseTypeError("invalid object");
@@ -67,7 +58,7 @@ internal static Exception ToException(IntPtr ob)
6758
/// </summary>
6859
public new static IntPtr tp_str(IntPtr ob)
6960
{
70-
Exception e = ToException(ob);
61+
Exception e = ToException(new BorrowedReference(ob));
7162
if (e == null)
7263
{
7364
return Exceptions.RaiseTypeError("invalid object");
@@ -157,7 +148,7 @@ internal static void Shutdown()
157148
/// pointer.
158149
/// </summary>
159150
/// <param name="ob">The python object wrapping </param>
160-
internal static void SetArgsAndCause(IntPtr ob)
151+
internal static void SetArgsAndCause(BorrowedReference ob)
161152
{
162153
// e: A CLR Exception
163154
Exception e = ExceptionClassObject.ToException(ob);
@@ -178,13 +169,13 @@ internal static void SetArgsAndCause(IntPtr ob)
178169
args = Runtime.PyTuple_New(0);
179170
}
180171

181-
Marshal.WriteIntPtr(ob, ExceptionOffset.args, args);
172+
Marshal.WriteIntPtr(ob.DangerousGetAddress(), ExceptionOffset.args, args);
182173

183174
if (e.InnerException != null)
184175
{
185176
// Note: For an AggregateException, InnerException is only the first of the InnerExceptions.
186-
IntPtr cause = CLRObject.GetInstHandle(e.InnerException);
187-
Marshal.WriteIntPtr(ob, ExceptionOffset.cause, cause);
177+
using var cause = CLRObject.GetReference(e.InnerException);
178+
Runtime.PyException_SetCause(ob, cause.Steal());
188179
}
189180
}
181

0 commit comments

Comments
 (0)
0