8000 fixed __pyobj__ access · pythonnet/pythonnet@e422367 · GitHub
[go: up one dir, main page]

Skip to content

Commit e422367

Browse files
committed
fixed __pyobj__ access
1 parent 7a4daeb commit e422367

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/runtime/UnsafeReferenceWithRun.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
using System;
2+
using System.ComponentModel;
23

34
namespace Python.Runtime;
45

5-
struct UnsafeReferenceWithRun
6+
[EditorBrowsable(EditorBrowsableState.Never)]
7+
[Obsolete(Util.InternalUseOnly)]
8+
public struct UnsafeReferenceWithRun
69
{
7-
public UnsafeReferenceWithRun(BorrowedReference pyObj)
10+
internal UnsafeReferenceWithRun(BorrowedReference pyObj)
811
{
912
RawObj = pyObj.DangerousGetAddressOrNull();
1013
Run = Runtime.GetRun();
1114
}
1215

13-
public IntPtr RawObj;
14-
public BorrowedReference Ref => new(RawObj);
15-
public int Run;
16+
internal IntPtr RawObj;
17+
internal BorrowedReference Ref => new(RawObj);
18+
internal int Run;
1619

17-
public BorrowedReference CheckRun()
20+
internal BorrowedReference CheckRun()
1821
{
1922
if (Run != Runtime.GetRun())
2023
throw new RuntimeShutdownException(RawObj);

src/runtime/classderived.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@ internal static Type CreateDerivedType(string name,
168168

169169
// add a field for storing the python object pointer
170170
// FIXME: fb not used
171-
FieldBuilder fb = typeBuilder.DefineField("__pyobj__",
171+
FieldBuilder fb = typeBuilder.DefineField(PyObjName,
172+
#pragma warning disable CS0618 // Type or member is obsolete. OK for internal use.
172173
typeof(UnsafeReferenceWithRun),
173-
FieldAttributes.Public);
174+
#pragma warning restore CS0618 // Type or member is obsolete
175+
FieldAttributes.Private);
174176

175177
// override any constructors
176178
ConstructorInfo[] constructors = baseClass.GetConstructors();
@@ -646,6 +648,9 @@ private static ModuleBuilder GetModuleBuilder(string assemblyName, string module
646648
[Obsolete(Util.InternalUseOnly)]
647649
public class PythonDerivedType
648650
{
651+
internal const string PyObjName = "__pyobj__";
652+
internal const BindingFlags PyObjFlags = BindingFlags.Instance | BindingFlags.NonPublic;
653+
649654
/// <summary>
650655
/// This is the implementation of the overridden methods in the derived
651656
/// type. It looks for a python method with the same name as the method
@@ -849,15 +854,17 @@ public static void PyFinalize(IPythonDerivedType obj)
849854
Finalizer.Instance.AddDerivedFinalizedObject(ref self.RawObj, self.Run);
850855
}
851856

857+
internal static FieldInfo? GetPyObjField(Type type) => type.GetField(PyObjName, PyObjFlags);
858+
852859
internal static UnsafeReferenceWithRun GetPyObj(IPythonDerivedType obj)
853860
{
854-
FieldInfo fi = obj.GetType().GetField("__pyobj__");
861+
FieldInfo fi = GetPyObjField(obj.GetType())!;
855862
return (UnsafeReferenceWithRun)fi.GetValue(obj);
856863
}
857864

858865
static void SetPyObj(IPythonDerivedType obj, BorrowedReference pyObj)
859866
{
860-
FieldInfo fi = obj.GetType().GetField("__pyobj__");
867+
FieldInfo fi = GetPyObjField(obj.GetType())!;
861868
fi.SetValue(obj, new UnsafeReferenceWithRun(pyObj));
862869
}
863870
}

src/runtime/classmanager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ internal static ClassBase CreateClass(Type type)
184184
impl = new ExceptionClassObject(type);
185185
}
186186

187-
else if (null != type.GetField("__pyobj__"))
187+
#pragma warning disable CS0618 // Type or member is obsolete. OK for internal use.
188+
else if (null != PythonDerivedType.GetPyObjField(type))
189+
#pragma warning restore CS0618 // Type or member is obsolete
188190
{
189191
impl = new ClassDerivedObject(type);
190192
}

0 commit comments

Comments
 (0)
0