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

Commitf788b6a

Browse files
committed
WIP replaced magic offsets with per-type calculation
removed some macro-like method copy-pastes from CPython
1 parent23527d1 commitf788b6a

18 files changed

+272
-479
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: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,19 +355,22 @@ public static void tp_dealloc(IntPtr ob)
355355
{
356356
ManagedTypeself=GetManagedObject(ob);
357357
tp_clear(ob);
358-
Runtime.PyObject_GC_UnTrack(self.pyHandle);
359-
Runtime.PyObject_GC_Del(self.pyHandle);
360-
self.FreeGCHandle();
358+
Runtime.PyObject_GC_UnTrack(ob);
359+
Runtime.PyObject_GC_Del(ob);
360+
self?.FreeGCHandle();
361361
}
362362

363363
publicstaticinttp_clear(IntPtrob)
364364
{
365365
ManagedTypeself=GetManagedObject(ob);
366-
if(!self.IsTypeObject())
366+
boolisTypeObject=Runtime.PyObject_TYPE(ob)==Runtime.PyCLRMetaType;
367+
Debug.Assert(selfisnull||isTypeObject==self.IsTypeObject());
368+
369+
if(!isTypeObject)
367370
{
368371
ClearObjectDict(ob);
369372
}
370-
self.tpHandle=IntPtr.Zero;
373+
if(selfis notnull)self.tpHandle=IntPtr.Zero;
371374
return0;
372375
}
373376

@@ -391,7 +394,7 @@ protected override void OnLoad(InterDomainContext context)
391394
SetObjectDict(pyHandle,dict);
392395
}
393396
gcHandle=AllocGCHandle();
394-
Marshal.WriteIntPtr(pyHandle,TypeOffset.magic(),(IntPtr)gcHandle);
397+
ReplaceGCHandle(ObjectReference,gcHandle);
395398
}
396399

397400

‎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+
ReplaceGCHandle(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+
ReplaceGCHandle(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 & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,17 @@ 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-
2817
GCHandlegc=AllocGCHandle(TrackTypes.Wrapper);
29-
Marshal.WriteIntPtr(py,ObjectOffset.magic(tp),(IntPtr)gc);
18+
3019
tpHandle=tp;
3120
pyHandle=py;
3221
inst=ob;
3322

23+
InitGCHandle(ObjectReference,type:TypeReference,gc);
24+
3425
// Fix the BaseException args (and __cause__ in case of Python 3)
3526
// slot if wrapping a CLR exception
36-
Exceptions.SetArgsAndCause(py);
27+
if(obisExceptione)Exceptions.SetArgsAndCause(e,py);
3728
}
3829

3930
protectedCLRObject()
@@ -78,6 +69,9 @@ internal static IntPtr GetInstHandle(object ob)
7869
returnco.pyHandle;
7970
}
8071

72+
internalstaticNewReferenceGetReference(objectob)
73+
=>NewReference.DangerousFromPointer(GetInstHandle(ob));
74+
8175
internalstaticCLRObjectRestore(objectob,IntPtrpyHandle,InterDomainContextcontext)
8276
{
8377
CLRObjectco=newCLRObject()
@@ -101,7 +95,7 @@ protected override void OnLoad(InterDomainContext context)
10195
{
10296
base.OnLoad(context);
10397
GCHandlegc=AllocGCHandle(TrackTypes.Wrapper);
104-
Marshal.WriteIntPtr(pyHandle,ObjectOffset.magic(tpHandle),(IntPtr)gc);
98+
ReplaceGCHandle(ObjectReference,TypeReference,gc);
10599
}
106100
}
107101
}

‎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+
ReplaceGCHandle(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