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

Commit93a1313

Browse files
committed
use PyType instances instead of raw pointers in TypeManager type cache and ConstructorBinding instances
1 parentdb746aa commit93a1313

13 files changed

+142
-157
lines changed

‎src/runtime/classbase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public static int tp_clear(IntPtr ob)
373373
protectedoverridevoidOnSave(InterDomainContextcontext)
374374
{
375375
base.OnSave(context);
376-
if(pyHandle!=tpHandle)
376+
if(!this.IsClrMetaTypeInstance())
377377
{
378378
IntPtrdict=GetObjectDict(pyHandle);
379379
Runtime.XIncref(dict);
@@ -384,7 +384,7 @@ protected override void OnSave(InterDomainContext context)
384384
protectedoverridevoidOnLoad(InterDomainContextcontext)
385385
{
386386
base.OnLoad(context);
387-
if(pyHandle!=tpHandle)
387+
if(!this.IsClrMetaTypeInstance())
388388
{
389389
IntPtrdict=context.Storage.GetValue<IntPtr>("dict");
390390
SetObjectDict(pyHandle,dict);

‎src/runtime/classderived.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,13 @@ internal static IntPtr ToPython(IPythonDerivedType obj)
122122
/// </summary>
123123
internalstaticTypeCreateDerivedType(stringname,
124124
TypebaseType,
125-
IntPtrpy_dict,
125+
BorrowedReferencedictRef,
126126
stringnamespaceStr,
127127
stringassemblyName,
128128
stringmoduleName="Python.Runtime.Dynamic.dll")
129129
{
130+
// TODO: clean up
131+
IntPtrpy_dict=dictRef.DangerousGetAddress();
130132
if(null!=namespaceStr)
131133
{
132134
name=namespaceStr+"."+name;
@@ -824,7 +826,7 @@ public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, objec
824826
try
825827
{
826828
// create the python object
827-
IntPtrtype=TypeManager.GetTypeHandle(obj.GetType());
829+
BorrowedReferencetype=TypeManager.GetTypeReference(obj.GetType());
828830
self=newCLRObject(obj,type);
829831

830832
// set __pyobj__ to self and deref the python object which will allow this

‎src/runtime/classmanager.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ internal static void SaveRuntimeData(RuntimeDataStorage storage)
117117
// Python object's dictionary tool; thus raising an AttributeError
118118
// instead of a TypeError.
119119
// Classes are re-initialized on in RestoreRuntimeData.
120-
vardict=newBorrowedReference(Marshal.ReadIntPtr(cls.Value.tpHandle,TypeOffset.tp_dict));
120+
usingvardict=Runtime.PyObject_GenericGetDict(cls.Value.TypeReference);
121121
foreach(varmemberincls.Value.dotNetMembers)
122122
{
123123
// No need to decref the member, the ClassBase instance does
@@ -135,7 +135,7 @@ internal static void SaveRuntimeData(RuntimeDataStorage storage)
135135
}
136136
}
137137
// We modified the Type object, notify it we did.
138-
Runtime.PyType_Modified(cls.Value.tpHandle);
138+
Runtime.PyType_Modified(cls.Value.TypeReference);
139139
}
140140
}
141141

@@ -155,7 +155,7 @@ internal static Dictionary<ManagedType, InterDomainContext> RestoreRuntimeData(R
155155
// re-init the class
156156
InitClassBase(pair.Key.Value,pair.Value);
157157
// We modified the Type object, notify it we did.
158-
Runtime.PyType_Modified(pair.Value.tpHandle);
158+
Runtime.PyType_Modified(pair.Value.TypeReference);
159159
varcontext=contexts[pair.Value.pyHandle];
160160
pair.Value.Load(context);
161161
loadedObjs.Add(pair.Value,context);
@@ -266,10 +266,10 @@ private static void InitClassBase(Type type, ClassBase impl)
266266
// point to the managed methods providing the implementation.
267267

268268

269-
IntPtrtp=TypeManager.GetTypeHandle(impl,type);
269+
varpyType=TypeManager.GetType(impl,type);
270270

271271
// Finally, initialize the class __dict__ and return the object.
272-
vardict=newBorrowedReference(Marshal.ReadIntPtr(tp,TypeOffset.tp_dict));
272+
usingvardict=Runtime.PyObject_GenericGetDict(pyType.Reference);
273273

274274

275275
if(impl.dotNetMembers==null)
@@ -312,7 +312,7 @@ private static void InitClassBase(Type type, ClassBase impl)
312312
// Implement Overloads on the class object
313313
if(!CLRModule._SuppressOverloads)
314314
{
315-
varctors=newConstructorBinding(type,tp,co.binder);
315+
varctors=newConstructorBinding(type,pyType,co.binder);
316316
// ExtensionType types are untracked, so don't Incref() them.
317317
// TODO: deprecate __overloads__ soon...
318318
Runtime.PyDict_SetItem(dict,PyIdentifier.__overloads__,ctors.ObjectReference);
@@ -332,7 +332,7 @@ private static void InitClassBase(Type type, ClassBase impl)
332332

333333
// The type has been modified after PyType_Ready has been called
334334
// Refresh the type
335-
Runtime.PyType_Modified(tp);
335+
Runtime.PyType_Modified(pyType.Reference);
336336
}
337337

338338
internalstaticboolShouldBindMethod(MethodBasemb)

‎src/runtime/clrobject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ internal CLRObject(object ob, IntPtr tp)
2626
if(obisExceptione)Exceptions.SetArgsAndCause(e,py);
2727
}
2828

29+
internalCLRObject(objectob,BorrowedReferencetp):this(ob,tp.DangerousGetAddress()){}
30+
2931
protectedCLRObject()
3032
{
3133
}

‎src/runtime/constructorbinding.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ namespace Python.Runtime
2323
internalclassConstructorBinding:ExtensionType
2424
{
2525
privateMaybeTypetype;// The managed Type being wrapped in a ClassObject
26-
privateIntPtrpyTypeHndl;// The python type tells GetInstHandle which Type to create.
26+
privatePyTypetypeToCreate;// The python type tells GetInstHandle which Type to create.
2727
privateConstructorBinderctorBinder;
2828

2929
[NonSerialized]
3030
privateIntPtrrepr;
3131

32-
publicConstructorBinding(Typetype,IntPtrpyTypeHndl,ConstructorBinderctorBinder)
32+
publicConstructorBinding(Typetype,PyTypetypeToCreate,ConstructorBinderctorBinder)
3333
{
3434
this.type=type;
35-
this.pyTypeHndl=pyTypeHndl;// steal a type reference
35+
this.typeToCreate=typeToCreate;
3636
this.ctorBinder=ctorBinder;
3737
repr=IntPtr.Zero;
3838
}
@@ -110,7 +110,7 @@ public static IntPtr mp_subscript(IntPtr op, IntPtr key)
110110
{
111111
returnExceptions.RaiseTypeError("No match found for constructor signature");
112112
}
113-
varboundCtor=newBoundContructor(tp,self.pyTypeHndl,self.ctorBinder,ci);
113+
varboundCtor=newBoundContructor(tp,self.typeToCreate,self.ctorBinder,ci);
114114

115115
returnboundCtor.pyHandle;
116116
}
@@ -169,7 +169,7 @@ public static int tp_clear(IntPtr ob)
169169
publicstaticinttp_traverse(IntPtrob,IntPtrvisit,IntPtrarg)
170170
{
171171
varself=(ConstructorBinding)GetManagedObject(ob);
172-
intres=PyVisit(self.pyTypeHndl,visit,arg);
172+
intres=PyVisit(self.typeToCreate.Handle,visit,arg);
173173
if(res!=0)returnres;
174174

175175
res=PyVisit(self.repr,visit,arg);
@@ -190,15 +190,15 @@ public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
190190
internalclassBoundContructor:ExtensionType
191191
{
192192
privateTypetype;// The managed Type being wrapped in a ClassObject
193-
privateIntPtrpyTypeHndl;// The python type tells GetInstHandle which Type to create.
193+
privatePyTypetypeToCreate;// The python type tells GetInstHandle which Type to create.
194194
privateConstructorBinderctorBinder;
195195
privateConstructorInfoctorInfo;
196196
privateIntPtrrepr;
197197

198-
publicBoundContructor(Typetype,IntPtrpyTypeHndl,ConstructorBinderctorBinder,ConstructorInfoci)
198+
publicBoundContructor(Typetype,PyTypetypeToCreate,ConstructorBinderctorBinder,ConstructorInfoci)
199199
{
200200
this.type=type;
201-
this.pyTypeHndl=pyTypeHndl;// steal a type reference
201+
this.typeToCreate=typeToCreate;
202202
this.ctorBinder=ctorBinder;
203203
ctorInfo=ci;
204204
repr=IntPtr.Zero;
@@ -229,7 +229,7 @@ public static IntPtr tp_call(IntPtr op, IntPtr args, IntPtr kw)
229229
}
230230
// Instantiate the python object that wraps the result of the method call
231231
// and return the PyObject* to it.
232-
returnCLRObject.GetInstHandle(obj,self.pyTypeHndl);
232+
returnCLRObject.GetInstHandle(obj,self.typeToCreate.Reference).DangerousMoveToPointer();
233233
}
234234

235235
/// <summary>
@@ -272,7 +272,7 @@ public static int tp_clear(IntPtr ob)
272272
publicstaticinttp_traverse(IntPtrob,IntPtrvisit,IntPtrarg)
273273
{
274274
varself=(BoundContructor)GetManagedObject(ob);
275-
intres=PyVisit(self.pyTypeHndl,visit,arg);
275+
intres=PyVisit(self.typeToCreate.Handle,visit,arg);
276276
if(res!=0)returnres;
277277

278278
res=PyVisit(self.repr,visit,arg);

‎src/runtime/exceptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,15 @@ internal static void ErrorOccurredCheck(IntPtr pointer)
206206
}
207207
}
208208

209+
internalstaticIntPtrErrorCheckIfNull(IntPtrpointer)
210+
{
211+
if(pointer==IntPtr.Zero&&ErrorOccurred())
212+
{
213+
thrownewPythonException();
214+
}
215+
returnpointer;
216+
}
217+
209218
/// <summary>
210219
/// ExceptionMatches Method
211220
/// </summary>

‎src/runtime/extensiontype.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public ExtensionType()
1818
// The Python instance object is related to an instance of a
1919
// particular concrete subclass with a hidden CLR gchandle.
2020

21-
IntPtrtp=TypeManager.GetTypeHandle(GetType());
21+
BorrowedReferencetp=TypeManager.GetTypeReference(GetType());
2222

2323
//int rc = (int)Marshal.ReadIntPtr(tp, TypeOffset.ob_refcnt);
2424
//if (rc > 1050)
@@ -27,11 +27,11 @@ public ExtensionType()
2727
// DebugUtil.DumpType(tp);
2828
//}
2929

30-
IntPtrpy=Runtime.PyType_GenericAlloc(tp,0);
30+
NewReferencepy=Runtime.PyType_GenericAlloc(tp,0);
3131

32-
//Steals a ref to tpHandle.
33-
tpHandle=tp;
34-
pyHandle=py;
32+
//Borrowed reference. Valid as long as pyHandle is valid.
33+
tpHandle=tp.DangerousGetAddress();
34+
pyHandle=py.DangerousMoveToPointer();
3535

3636
#ifDEBUG
3737
GetGCHandle(ObjectReference,TypeReference,outvarexisting);

‎src/runtime/managedtype.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
usingSystem;
23
usingSystem.Collections.Generic;
34
usingSystem.Diagnostics;
@@ -27,8 +28,8 @@ internal enum TrackTypes
2728
internalIntPtrpyHandle;// PyObject *
2829
internalIntPtrtpHandle;// PyType *
2930

30-
internalBorrowedReferenceObjectReference=>newBorrowedReference(pyHandle);
31-
internalBorrowedReferenceTypeReference=>newBorrowedReference(tpHandle);
31+
internalBorrowedReferenceObjectReference=>new(pyHandle);
32+
internalBorrowedReferenceTypeReference=>new(tpHandle);
3233

3334
privatestaticreadonlyDictionary<ManagedType,TrackTypes>_managedObjs=newDictionary<ManagedType,TrackTypes>();
3435

@@ -78,12 +79,12 @@ internal void FreeGCHandle()
7879
}
7980
}
8081

81-
internalstaticManagedTypeGetManagedObject(BorrowedReferenceob)
82+
internalstaticManagedType?GetManagedObject(BorrowedReferenceob)
8283
=>GetManagedObject(ob.DangerousGetAddress());
8384
/// <summary>
8485
/// Given a Python object, return the associated managed object or null.
8586
/// </summary>
86-
internalstaticManagedTypeGetManagedObject(IntPtrob)
87+
internalstaticManagedType?GetManagedObject(IntPtrob)
8788
{
8889
if(ob!=IntPtr.Zero)
8990
{
@@ -106,7 +107,7 @@ internal static ManagedType GetManagedObject(IntPtr ob)
106107
/// <summary>
107108
/// Given a Python object, return the associated managed object type or null.
108109
/// </summary>
109-
internalstaticManagedTypeGetManagedObjectType(IntPtrob)
110+
internalstaticManagedType?GetManagedObjectType(IntPtrob)
110111
{
111112
if(ob!=IntPtr.Zero)
112113
{
@@ -121,18 +122,6 @@ internal static ManagedType GetManagedObjectType(IntPtr ob)
121122
returnnull;
122123
}
123124

124-
125-
internalstaticManagedTypeGetManagedObjectErr(IntPtrob)
126-
{
127-
ManagedTyperesult=GetManagedObject(ob);
128-
if(result==null)
129-
{
130-
Exceptions.SetError(Exceptions.TypeError,"invalid argument, expected CLR type");
131-
}
132-
returnresult;
133-
}
134-
135-
136125
internalstaticboolIsInstanceOfManagedType(BorrowedReferenceob)
137126
=>IsInstanceOfManagedType(ob.DangerousGetAddressOrNull());
138127
internalstaticboolIsInstanceOfManagedType(IntPtrob)
@@ -156,6 +145,13 @@ internal static bool IsManagedType(BorrowedReference type)
156145
return(flags&TypeFlags.HasClrInstance)!=0;
157146
}
158147

148+
publicboolIsClrMetaTypeInstance()
149+
{
150+
Debug.Assert(Runtime.PyCLRMetaType!=IntPtr.Zero);
151+
Debug.Assert(pyHandle!=IntPtr.Zero);
152+
returnRuntime.PyObject_TYPE(pyHandle)==Runtime.PyCLRMetaType;
153+
}
154+
159155
internalstaticIDictionary<ManagedType,TrackTypes>GetManagedObjects()
160156
{
161157
return_managedObjs;
@@ -185,7 +181,8 @@ internal void CallTypeClear()
185181
{
186182
return;
187183
}
188-
varclearPtr=Marshal.ReadIntPtr(tpHandle,TypeOffset.tp_clear);
184+
185+
varclearPtr=Runtime.PyType_GetSlot(TypeReference,TypeSlotID.tp_clear);
189186
if(clearPtr==IntPtr.Zero)
190187
{
191188
return;
@@ -203,7 +200,7 @@ internal void CallTypeTraverse(Interop.ObjObjFunc visitproc, IntPtr arg)
203200
{
204201
return;
205202
}
206-
vartraversePtr=Marshal.ReadIntPtr(tpHandle,TypeOffset.tp_traverse);
203+
vartraversePtr=Runtime.PyType_GetSlot(TypeReference,TypeSlotID.tp_traverse);
207204
if(traversePtr==IntPtr.Zero)
208205
{
209206
return;

‎src/runtime/metatype.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
132132
{
133133
if(clsDict.HasKey("__assembly__")||clsDict.HasKey("__namespace__"))
134134
{
135-
returnTypeManager.CreateSubType(name,base_type,dict);
135+
returnTypeManager.CreateSubType(name,base_type,clsDict.Reference);
136136
}
137137
}
138138
}
@@ -266,7 +266,7 @@ public static int tp_setattro(IntPtr tp, IntPtr name, IntPtr value)
266266
}
267267

268268
intres=Runtime.PyObject_GenericSetAttr(tp,name,value);
269-
Runtime.PyType_Modified(tp);
269+
Runtime.PyType_Modified(newBorrowedReference(tp));
270270

271271
returnres;
272272
}

‎src/runtime/pytype.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66

77
namespacePython.Runtime
88
{
9+
[Serializable]
910
publicclassPyType:PyObject
1011
{
1112
/// <summary>Creates heap type object from the <paramref name="spec"/>.</summary>
1213
publicPyType(TypeSpecspec,PyTuple?bases=null):base(FromSpec(spec,bases)){}
1314
/// <summary>Wraps an existing type object.</summary>
1415
publicPyType(PyObjecto):base(FromObject(o)){}
1516

17+
internalPyType(BorrowedReferencereference):base(reference)
18+
{
19+
if(!Runtime.PyType_Check(this.Handle))
20+
thrownewArgumentException("object is not a type");
21+
}
22+
1623
/// <summary>Checks if specified object is a Python type.</summary>
1724
publicstaticboolIsType(PyObjectvalue)
1825
{
@@ -21,6 +28,12 @@ public static bool IsType(PyObject value)
2128
returnRuntime.PyType_Check(value.obj);
2229
}
2330

31+
internalIntPtrGetSlot(TypeSlotIDslot)
32+
{
33+
IntPtrresult=Runtime.PyType_GetSlot(this.Reference,slot);
34+
returnExceptions.ErrorCheckIfNull(result);
35+
}
36+
2437
privatestaticBorrowedReferenceFromObject(PyObjecto)
2538
{
2639
if(oisnull)thrownewArgumentNullException(nameof(o));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp