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

Commit7ea7773

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

12 files changed

+134
-201
lines changed

‎src/runtime/classbase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public static int tp_clear(IntPtr ob)
374374
protectedoverridevoidOnSave(InterDomainContextcontext)
375375
{
376376
base.OnSave(context);
377-
if(pyHandle!=tpHandle)
377+
if(!this.IsTypeObject())
378378
{
379379
IntPtrdict=GetObjectDict(pyHandle);
380380
Runtime.XIncref(dict);
@@ -385,7 +385,7 @@ protected override void OnSave(InterDomainContext context)
385385
protectedoverridevoidOnLoad(InterDomainContextcontext)
386386
{
387387
base.OnLoad(context);
388-
if(pyHandle!=tpHandle)
388+
if(!this.IsTypeObject())
389389
{
390390
IntPtrdict=context.Storage.GetValue<IntPtr>("dict");
391391
SetObjectDict(pyHandle,dict);

‎src/runtime/classderived.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,13 @@ internal static IntPtr ToPython(IPythonDerivedType obj)
124124
/// </summary>
125125
internalstaticTypeCreateDerivedType(stringname,
126126
TypebaseType,
127-
IntPtrpy_dict,
127+
BorrowedReferencedictRef,
128128
stringnamespaceStr,
129129
stringassemblyName,
130130
stringmoduleName="Python.Runtime.Dynamic.dll")
131131
{
132+
// TODO: clean up
133+
IntPtrpy_dict=dictRef.DangerousGetAddress();
132134
if(null!=namespaceStr)
133135
{
134136
name=namespaceStr+"."+name;
@@ -826,7 +828,7 @@ public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, objec
826828
try
827829
{
828830
// create the python object
829-
IntPtrtype=TypeManager.GetTypeHandle(obj.GetType());
831+
BorrowedReferencetype=TypeManager.GetTypeReference(obj.GetType());
830832
self=newCLRObject(obj,type);
831833

832834
// set __pyobj__ to self and deref the python object which will allow this
@@ -886,6 +888,7 @@ public static void Finalize(IPythonDerivedType obj)
886888
IntPtrdict=Marshal.ReadIntPtr(self.pyHandle,ObjectOffset.TypeDictOffset(self.tpHandle));
887889
if(dict!=IntPtr.Zero)
888890
{
891+
thrownewInvalidProgramException();
889892
Runtime.XDecref(dict);
890893
}
891894
Runtime.PyObject_GC_Del(self.pyHandle);

‎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
@@ -36,6 +36,8 @@ internal CLRObject(object ob, IntPtr tp)
3636
Exceptions.SetArgsAndCause(py);
3737
}
3838

39+
internalCLRObject(objectob,BorrowedReferencetp):this(ob,tp.DangerousGetAddress()){}
40+
3941
protectedCLRObject()
4042
{
4143
}

‎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
@@ -212,6 +212,15 @@ internal static void ErrorOccurredCheck(IntPtr pointer)
212212
}
213213
}
214214

215+
internalstaticIntPtrErrorCheckIfNull(IntPtrpointer)
216+
{
217+
if(pointer==IntPtr.Zero&&ErrorOccurred())
218+
{
219+
thrownewPythonException();
220+
}
221+
returnpointer;
222+
}
223+
215224
/// <summary>
216225
/// ExceptionMatches Method
217226
/// </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
SetupGc();
3737
}

‎src/runtime/managedtype.cs

Lines changed: 9 additions & 18 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,7 +28,8 @@ internal enum TrackTypes
2728
internalIntPtrpyHandle;// PyObject *
2829
internalIntPtrtpHandle;// PyType *
2930

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

3234
privatestaticreadonlyDictionary<ManagedType,TrackTypes>_managedObjs=newDictionary<ManagedType,TrackTypes>();
3335

@@ -77,12 +79,12 @@ internal void FreeGCHandle()
7779
}
7880
}
7981

80-
internalstaticManagedTypeGetManagedObject(BorrowedReferenceob)
82+
internalstaticManagedType?GetManagedObject(BorrowedReferenceob)
8183
=>GetManagedObject(ob.DangerousGetAddress());
8284
/// <summary>
8385
/// Given a Python object, return the associated managed object or null.
8486
/// </summary>
85-
internalstaticManagedTypeGetManagedObject(IntPtrob)
87+
internalstaticManagedType?GetManagedObject(IntPtrob)
8688
{
8789
if(ob!=IntPtr.Zero)
8890
{
@@ -112,7 +114,7 @@ internal static ManagedType GetManagedObject(IntPtr ob)
112114
/// <summary>
113115
/// Given a Python object, return the associated managed object type or null.
114116
/// </summary>
115-
internalstaticManagedTypeGetManagedObjectType(IntPtrob)
117+
internalstaticManagedType?GetManagedObjectType(IntPtrob)
116118
{
117119
if(ob!=IntPtr.Zero)
118120
{
@@ -128,18 +130,6 @@ internal static ManagedType GetManagedObjectType(IntPtr ob)
128130
returnnull;
129131
}
130132

131-
132-
internalstaticManagedTypeGetManagedObjectErr(IntPtrob)
133-
{
134-
ManagedTyperesult=GetManagedObject(ob);
135-
if(result==null)
136-
{
137-
Exceptions.SetError(Exceptions.TypeError,"invalid argument, expected CLR type");
138-
}
139-
returnresult;
140-
}
141-
142-
143133
internalstaticboolIsManagedType(BorrowedReferenceob)
144134
=>IsManagedType(ob.DangerousGetAddressOrNull());
145135
internalstaticboolIsManagedType(IntPtrob)
@@ -195,7 +185,8 @@ internal void CallTypeClear()
195185
{
196186
return;
197187
}
198-
varclearPtr=Marshal.ReadIntPtr(tpHandle,TypeOffset.tp_clear);
188+
189+
varclearPtr=Runtime.PyType_GetSlot(TypeReference,TypeSlotID.tp_clear);
199190
if(clearPtr==IntPtr.Zero)
200191
{
201192
return;
@@ -213,7 +204,7 @@ internal void CallTypeTraverse(Interop.ObjObjFunc visitproc, IntPtr arg)
213204
{
214205
return;
215206
}
216-
vartraversePtr=Marshal.ReadIntPtr(tpHandle,TypeOffset.tp_traverse);
207+
vartraversePtr=Runtime.PyType_GetSlot(TypeReference,TypeSlotID.tp_traverse);
217208
if(traversePtr==IntPtr.Zero)
218209
{
219210
return;

‎src/runtime/metatype.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
134134
{
135135
if(clsDict.HasKey("__assembly__")||clsDict.HasKey("__namespace__"))
136136
{
137-
returnTypeManager.CreateSubType(name,base_type,dict);
137+
returnTypeManager.GetOrCreateSubType(name,base_type,clsDict.Reference);
138138
}
139139
}
140140
}
@@ -257,7 +257,7 @@ public static int tp_setattro(IntPtr tp, IntPtr name, IntPtr value)
257257
}
258258

259259
intres=Runtime.PyObject_GenericSetAttr(tp,name,value);
260-
Runtime.PyType_Modified(tp);
260+
Runtime.PyType_Modified(newBorrowedReference(tp));
261261

262262
returnres;
263263
}

‎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