Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Commit 6a3cfc8

Browse files
committed
Adds an unchecked version to get a BorrowedReference pointer
And other code review changes
1 parent 12c0206 commit 6a3cfc8

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

src/runtime/BorrowedReference.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ readonly ref struct BorrowedReference
1111
public bool IsNull => this.pointer == IntPtr.Zero;
1212

1313
/// <summary>Gets a raw pointer to the Python object</summary>
14-
public IntPtr DangerousGetAddress() => this.pointer;
14+
public IntPtr DangerousGetAddress()
15+
=> this.IsNull ? throw new NullReferenceException() : this.pointer;
16+
17+
/// <summary>
18+
/// Gets a raw pointer to the Python object. Does not throw an exception
19+
/// if the pointer is null
20+
/// </summary>
21+
public IntPtr DangerousGetAddressUnchecked() => this.pointer;
1522

1623
/// <summary>
1724
/// Creates new instance of <see cref="BorrowedReference"/> from raw pointer. Unsafe.

src/runtime/runtime_data.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ internal static void Stash()
7070
Marshal.WriteIntPtr(mem, (IntPtr)ms.Length);
7171
Marshal.Copy(data, 0, mem + IntPtr.Size, (int)ms.Length);
7272

73-
IntPtr capsule = PySys_GetObject("clr_data").DangerousGetAddress();
73+
IntPtr capsule = PySys_GetObject("clr_data").DangerousGetAddressUnchecked();
7474
if (capsule != IntPtr.Zero)
7575
{
7676
IntPtr oldData = PyCapsule_GetPointer(capsule, null);
@@ -96,7 +96,7 @@ internal static void RestoreRuntimeData()
9696

9797
private static void RestoreRuntimeDataImpl()
9898
{
99-
IntPtr capsule = PySys_GetObject("clr_data").DangerousGetAddress();
99+
IntPtr capsule = PySys_GetObject("clr_data").DangerousGetAddressUnchecked();
100100
if (capsule == IntPtr.Zero)
101101
{
102102
return;

tools/geninterop/geninterop.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
import sysconfig
2222
import subprocess
2323

24-
if sys.version_info.major > 2:
25-
from io import StringIO
26-
else:
27-
from StringIO import StringIO
24+
from StringIO import StringIO
2825

2926
from pycparser import c_ast, c_parser
3027

0 commit comments

Comments
 (0)