Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit30ffeab

Browse files
committed
Replaced magic offsets with per-type calculation
removed some macro-like method copy-pastes from CPython and bits of dead codeAll Python types created to represent CLR concepts derive from `CLR MetaType` (as before), which now has two new fields:- `tp_clr_inst_offset`, which is similar to `tp_dictoffset` in that it tells where to find `GCHandle` in instances of this type (e.g. where to find `GCHandle` pointing to `System.Uri` in corresponding Python object)- `tp_clr_inst`, which holds an optional instance of `ManagedType`, that implements the behavior of the type itself (e.g. `GCHandle` pointing to `ClassObject(type = System.Uri)`)So the layout of all Python types created by Python.NET is PyType type; nint tp_clr_inst_offset; GCHandel tp_clr_inst; (points, for example, to an instance of `ClassObject`)When a Python type, that reflects CLR type is created, the layout of instances will be: BaseTypeFields base; optional (if not in base): IntPtr dict; optional (if not in base): IntPtr weaklist; GCHandle gcHandle; (points to `CLRObject` instance, which in turn, for example, points to the actual instance of `System.Uri`)The offset to GC handle is recorded in the Python type's `tp_clr_inst_offset`, and can be found as `PyObject_Type(inst).tp_clr_inst_offset`. Or, preferably, accessor functions in `ManagedType` should be used.
1 parent0775458 commit30ffeab

18 files changed

+266
-507
lines changed

‎src/embed_tests/TestPyType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
usingSystem.Runtime.InteropServices;
12
usingSystem.Text;
23

34
usingNUnit.Framework;
@@ -30,7 +31,7 @@ public void CanCreateHeapType()
3031
usingvardoc=newStrPtr(docStr,Encoding.UTF8);
3132
varspec=newTypeSpec(
3233
name:name,
33-
basicSize:ObjectOffset.Size(Runtime.Runtime.PyTypeType),
34+
basicSize:Marshal.ReadInt32(Runtime.Runtime.PyBaseObjectType,TypeOffset.tp_basicsize),
3435
slots:newTypeSpec.Slot[]{
3536
new(TypeSlotID.tp_doc,doc.RawPointer),
3637
},

‎src/runtime/classbase.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
usingSystem;
22
usingSystem.Collections;
33
usingSystem.Collections.Generic;
4-
usingSystem.Diagnostics;
5-
usingSystem.Runtime.InteropServices;
6-
usingSystem.Runtime.Serialization;
74

85
namespacePython.Runtime
96
{
@@ -355,19 +352,21 @@ public static void tp_dealloc(IntPtr ob)
355352
{
356353
ManagedTypeself=GetManagedObject(ob);
357354
tp_clear(ob);
358-
Runtime.PyObject_GC_UnTrack(self.pyHandle);
359-
Runtime.PyObject_GC_Del(self.pyHandle);
360-
self.FreeGCHandle();
355+
Runtime.PyObject_GC_UnTrack(ob);
356+
Runtime.PyObject_GC_Del(ob);
357+
self?.FreeGCHandle();
361358
}
362359

363360
publicstaticinttp_clear(IntPtrob)
364361
{
365362
ManagedTypeself=GetManagedObject(ob);
366-
if(!self.IsTypeObject())
363+
364+
boolisTypeObject=Runtime.PyObject_TYPE(ob)==Runtime.PyCLRMetaType;
365+
if(!isTypeObject)
367366
{
368367
ClearObjectDict(ob);
369368
}
370-
self.tpHandle=IntPtr.Zero;
369+
if(selfis notnull)self.tpHandle=IntPtr.Zero;
371370
return0;
372371
}
373372

@@ -391,7 +390,7 @@ protected override void OnLoad(InterDomainContext context)
391390
SetObjectDict(pyHandle,dict);
392391
}
393392
gcHandle=AllocGCHandle();
394-
Marshal.WriteIntPtr(pyHandle,TypeOffset.magic(),(IntPtr)gcHandle);
393+
SetGCHandle(ObjectReference,gcHandle);
395394
}
396395

397396

‎src/runtime/classderived.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
usingSystem;
22
usingSystem.Collections.Generic;
3+
usingSystem.Diagnostics;
34
usingSystem.Linq;
45
usingSystem.Reflection;
56
usingSystem.Reflection.Emit;
@@ -75,8 +76,8 @@ internal ClassDerivedObject(Type tp) : base(tp)
7576
// So we don't call PyObject_GC_Del here and instead we set the python
7677
// reference to a weak reference so that the C# object can be collected.
7778
GCHandlegc=GCHandle.Alloc(self,GCHandleType.Weak);
78-
intgcOffset=ObjectOffset.magic(Runtime.PyObject_TYPE(self.pyHandle));
79-
Marshal.WriteIntPtr(self.pyHandle,gcOffset,(IntPtr)gc);
79+
Debug.Assert(self.TypeReference==Runtime.PyObject_TYPE(self.ObjectReference));
80+
SetGCHandle(self.ObjectReference,self.TypeReference,gc);
8081
self.gcHandle.Free();
8182
self.gcHandle=gc;
8283
}
@@ -106,7 +107,7 @@ internal static IntPtr ToPython(IPythonDerivedType obj)
106107
Runtime._Py_NewReference(self.pyHandle);
107108
#endif
108109
GCHandlegc=GCHandle.Alloc(self,GCHandleType.Normal);
109-
Marshal.WriteIntPtr(self.pyHandle,ObjectOffset.magic(self.tpHandle),(IntPtr)gc);
110+
SetGCHandle(self.ObjectReference,self.TypeReference,gc);
110111
self.gcHandle.Free();
111112
self.gcHandle=gc;
112113

@@ -883,11 +884,6 @@ public static void Finalize(IPythonDerivedType obj)
883884
// the C# object is being destroyed which must mean there are no more
884885
// references to the Python object as well so now we can dealloc the
885886
// python object.
886-
IntPtrdict=Marshal.ReadIntPtr(self.pyHandle,ObjectOffset.TypeDictOffset(self.tpHandle));
887-
if(dict!=IntPtr.Zero)
888-
{
889-
Runtime.XDecref(dict);
890-
}
891887
Runtime.PyObject_GC_Del(self.pyHandle);
892888
self.gcHandle.Free();
893889
}

‎src/runtime/clrobject.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,16 @@ internal CLRObject(object ob, IntPtr tp)
1414
System.Diagnostics.Debug.Assert(tp!=IntPtr.Zero);
1515
IntPtrpy=Runtime.PyType_GenericAlloc(tp,0);
1616

17-
varflags=(TypeFlags)Util.ReadCLong(tp,TypeOffset.tp_flags);
18-
if((flags&TypeFlags.Subclass)!=0)
19-
{
20-
IntPtrdict=Marshal.ReadIntPtr(py,ObjectOffset.TypeDictOffset(tp));
21-
if(dict==IntPtr.Zero)
22-
{
23-
dict=Runtime.PyDict_New();
24-
Marshal.WriteIntPtr(py,ObjectOffset.TypeDictOffset(tp),dict);
25-
}
26-
}
27-
28-
GCHandlegc=AllocGCHandle(TrackTypes.Wrapper);
29-
Marshal.WriteIntPtr(py,ObjectOffset.magic(tp),(IntPtr)gc);
3017
tpHandle=tp;
3118
pyHandle=py;
3219
inst=ob;
3320

21+
GCHandlegc=AllocGCHandle(TrackTypes.Wrapper);
22+
InitGCHandle(ObjectReference,type:TypeReference,gc);
23+
3424
// Fix the BaseException args (and __cause__ in case of Python 3)
3525
// slot if wrapping a CLR exception
36-
Exceptions.SetArgsAndCause(py);
26+
if(obisExceptione)Exceptions.SetArgsAndCause(e,py);
3727
}
3828

3929
protectedCLRObject()
@@ -78,6 +68,9 @@ internal static IntPtr GetInstHandle(object ob)
7868
returnco.pyHandle;
7969
}
8070

71+
internalstaticNewReferenceGetReference(objectob)
72+
=>NewReference.DangerousFromPointer(GetInstHandle(ob));
73+
8174
internalstaticCLRObjectRestore(objectob,IntPtrpyHandle,InterDomainContextcontext)
8275
{
8376
CLRObjectco=newCLRObject()
@@ -101,7 +94,7 @@ protected override void OnLoad(InterDomainContext context)
10194
{
10295
base.OnLoad(context);
10396
GCHandlegc=AllocGCHandle(TrackTypes.Wrapper);
104-
Marshal.WriteIntPtr(pyHandle,ObjectOffset.magic(tpHandle),(IntPtr)gc);
97+
SetGCHandle(ObjectReference,TypeReference,gc);
10598
}
10699
}
107100
}

‎src/runtime/converter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
580580
{
581581
if(Runtime.PyBytes_Size(value)==1)
582582
{
583-
op=Runtime.PyBytes_AS_STRING(value);
583+
op=Runtime.PyBytes_AsString(value);
584584
result=(byte)Marshal.ReadByte(op);
585585
returntrue;
586586
}
@@ -606,7 +606,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
606606
{
607607
if(Runtime.PyBytes_Size(value)==1)
608608
{
609-
op=Runtime.PyBytes_AS_STRING(value);
609+
op=Runtime.PyBytes_AsString(value);
610610
result=(byte)Marshal.ReadByte(op);
611611
returntrue;
612612
}
@@ -632,7 +632,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
632632
{
633633
if(Runtime.PyBytes_Size(value)==1)
634634
{
635-
op=Runtime.PyBytes_AS_STRING(value);
635+
op=Runtime.PyBytes_AsString(value);
636636
result=(byte)Marshal.ReadByte(op);
637637
returntrue;
638638
}

‎src/runtime/exceptions.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,8 @@ internal static void Shutdown()
156156
/// pointer.
157157
/// </summary>
158158
/// <param name="ob">The python object wrapping </param>
159-
internalstaticvoidSetArgsAndCause(IntPtrob)
159+
internalstaticvoidSetArgsAndCause(Exceptione,IntPtrob)
160160
{
161-
// e: A CLR Exception
162-
Exceptione=ExceptionClassObject.ToException(ob);
163-
if(e==null)
164-
{
165-
return;
166-
}
167-
168161
IntPtrargs;
169162
if(!string.IsNullOrEmpty(e.Message))
170163
{
@@ -177,13 +170,14 @@ internal static void SetArgsAndCause(IntPtr ob)
177170
args=Runtime.PyTuple_New(0);
178171
}
179172

180-
Marshal.WriteIntPtr(ob,ExceptionOffset.args,args);
173+
if(Runtime.PyObject_SetAttrString(ob,"args",args)!=0)
174+
thrownewPythonException();
181175

182176
if(e.InnerException!=null)
183177
{
184178
// Note: For an AggregateException, InnerException is only the first of the InnerExceptions.
185-
IntPtrcause=CLRObject.GetInstHandle(e.InnerException);
186-
Marshal.WriteIntPtr(ob,ExceptionOffset.cause,cause);
179+
usingvarcause=CLRObject.GetReference(e.InnerException);
180+
Runtime.PyException_SetCause(ob,cause.DangerousMoveToPointer());
187181
}
188182
}
189183

‎src/runtime/extensiontype.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ public ExtensionType()
3333
tpHandle=tp;
3434
pyHandle=py;
3535

36-
SetupGc();
36+
SetupGc(force:false);
3737
}
3838

39-
voidSetupGc()
39+
voidSetupGc(boolforce)
4040
{
4141
GCHandlegc=AllocGCHandle(TrackTypes.Extension);
42-
Marshal.WriteIntPtr(pyHandle,ObjectOffset.magic(tpHandle),(IntPtr)gc);
42+
if(force)
43+
SetGCHandle(ObjectReference,TypeReference,gc);
44+
else
45+
InitGCHandle(ObjectReference,TypeReference,gc);
4346

4447
// We have to support gc because the type machinery makes it very
4548
// hard not to - but we really don't have a need for it in most
@@ -106,7 +109,7 @@ public static void tp_dealloc(IntPtr ob)
106109
protectedoverridevoidOnLoad(InterDomainContextcontext)
107110
{
108111
base.OnLoad(context);
109-
SetupGc();
112+
SetupGc(force:true);
110113
}
111114
}
112115
}

‎src/runtime/importhook.cs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,6 @@ internal static class ImportHook
1515
privatestaticIntPtrpy_clr_module;
1616
staticBorrowedReferenceClrModuleReference=>newBorrowedReference(py_clr_module);
1717

18-
privatestaticIntPtrmodule_def=IntPtr.Zero;
19-
20-
internalstaticvoidInitializeModuleDef()
21-
{
22-
if(module_def==IntPtr.Zero)
23-
{
24-
module_def=ModuleDefOffset.AllocModuleDef("clr");
25-
}
26-
}
27-
28-
internalstaticvoidReleaseModuleDef()
29-
{
30-
if(module_def==IntPtr.Zero)
31-
{
32-
return;
33-
}
34-
ModuleDefOffset.FreeModuleDef(module_def);
35-
module_def=IntPtr.Zero;
36-
}
37-
3818
/// <summary>
3919
/// Initialize just the __import__ hook itself.
4020
/// </summary>
@@ -90,8 +70,7 @@ internal static unsafe void Initialize()
9070
root=newCLRModule();
9171

9272
// create a python module with the same methods as the clr module-like object
93-
InitializeModuleDef();
94-
py_clr_module=Runtime.PyModule_Create2(module_def,3);
73+
py_clr_module=Runtime.PyModule_New("clr").DangerousMoveToPointer();
9574

9675
// both dicts are borrowed references
9776
BorrowedReferencemod_dict=Runtime.PyModule_GetDict(ClrModuleReference);
@@ -116,13 +95,8 @@ internal static void Shutdown()
11695

11796
RestoreImport();
11897

119-
boolshouldFreeDef=Runtime.Refcount(py_clr_module)==1;
12098
Runtime.XDecref(py_clr_module);
12199
py_clr_module=IntPtr.Zero;
122-
if(shouldFreeDef)
123-
{
124-
ReleaseModuleDef();
125-
}
126100

127101
Runtime.XDecref(root.pyHandle);
128102
root=null;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp