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

Use PyType instances instead of raw pointers#1431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletionssrc/runtime/classbase.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -373,7 +373,7 @@ public static int tp_clear(IntPtr ob)
protectedoverridevoidOnSave(InterDomainContextcontext)
{
base.OnSave(context);
if(pyHandle!=tpHandle)
if(!this.IsClrMetaTypeInstance())
{
IntPtrdict=GetObjectDict(pyHandle);
Runtime.XIncref(dict);
Expand All@@ -384,7 +384,7 @@ protected override void OnSave(InterDomainContext context)
protectedoverridevoidOnLoad(InterDomainContextcontext)
{
base.OnLoad(context);
if(pyHandle!=tpHandle)
if(!this.IsClrMetaTypeInstance())
{
IntPtrdict=context.Storage.GetValue<IntPtr>("dict");
SetObjectDict(pyHandle,dict);
Expand Down
6 changes: 4 additions & 2 deletionssrc/runtime/classderived.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -122,11 +122,13 @@ internal static IntPtr ToPython(IPythonDerivedType obj)
/// </summary>
internalstaticTypeCreateDerivedType(stringname,
TypebaseType,
IntPtrpy_dict,
BorrowedReferencedictRef,
stringnamespaceStr,
stringassemblyName,
stringmoduleName="Python.Runtime.Dynamic.dll")
{
// TODO: clean up
IntPtrpy_dict=dictRef.DangerousGetAddress();
if(null!=namespaceStr)
{
name=namespaceStr+"."+name;
Expand DownExpand Up@@ -824,7 +826,7 @@ public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, objec
try
{
// create the python object
IntPtrtype=TypeManager.GetTypeHandle(obj.GetType());
BorrowedReferencetype=TypeManager.GetTypeReference(obj.GetType());
self=newCLRObject(obj,type);

// set __pyobj__ to self and deref the python object which will allow this
Expand Down
14 changes: 7 additions & 7 deletionssrc/runtime/classmanager.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -117,7 +117,7 @@ internal static void SaveRuntimeData(RuntimeDataStorage storage)
// Python object's dictionary tool; thus raising an AttributeError
// instead of a TypeError.
// Classes are re-initialized on in RestoreRuntimeData.
vardict=newBorrowedReference(Marshal.ReadIntPtr(cls.Value.tpHandle,TypeOffset.tp_dict));
usingvardict=Runtime.PyObject_GenericGetDict(cls.Value.TypeReference);
foreach(varmemberincls.Value.dotNetMembers)
{
// No need to decref the member, the ClassBase instance does
Expand All@@ -135,7 +135,7 @@ internal static void SaveRuntimeData(RuntimeDataStorage storage)
}
}
// We modified the Type object, notify it we did.
Runtime.PyType_Modified(cls.Value.tpHandle);
Runtime.PyType_Modified(cls.Value.TypeReference);
}
}

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


IntPtrtp=TypeManager.GetTypeHandle(impl,type);
varpyType=TypeManager.GetType(impl,type);

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


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

// The type has been modified after PyType_Ready has been called
// Refresh the type
Runtime.PyType_Modified(tp);
Runtime.PyType_Modified(pyType.Reference);
}

internalstaticboolShouldBindMethod(MethodBasemb)
Expand Down
2 changes: 2 additions & 0 deletionssrc/runtime/clrobject.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,6 +26,8 @@ internal CLRObject(object ob, IntPtr tp)
if(obisExceptione)Exceptions.SetArgsAndCause(e,py);
}

internalCLRObject(objectob,BorrowedReferencetp):this(ob,tp.DangerousGetAddress()){}

protectedCLRObject()
{
}
Expand Down
20 changes: 10 additions & 10 deletionssrc/runtime/constructorbinding.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,16 +23,16 @@ namespace Python.Runtime
internalclassConstructorBinding:ExtensionType
{
privateMaybeTypetype;// The managed Type being wrapped in a ClassObject
privateIntPtrpyTypeHndl;// The python type tells GetInstHandle which Type to create.
privatePyTypetypeToCreate;// The python type tells GetInstHandle which Type to create.
privateConstructorBinderctorBinder;

[NonSerialized]
privateIntPtrrepr;

publicConstructorBinding(Typetype,IntPtrpyTypeHndl,ConstructorBinderctorBinder)
publicConstructorBinding(Typetype,PyTypetypeToCreate,ConstructorBinderctorBinder)
{
this.type=type;
this.pyTypeHndl=pyTypeHndl;// steal a type reference
this.typeToCreate=typeToCreate;
this.ctorBinder=ctorBinder;
repr=IntPtr.Zero;
}
Expand DownExpand Up@@ -110,7 +110,7 @@ public static IntPtr mp_subscript(IntPtr op, IntPtr key)
{
returnExceptions.RaiseTypeError("No match found for constructor signature");
}
varboundCtor=newBoundContructor(tp,self.pyTypeHndl,self.ctorBinder,ci);
varboundCtor=newBoundContructor(tp,self.typeToCreate,self.ctorBinder,ci);

returnboundCtor.pyHandle;
}
Expand DownExpand Up@@ -169,7 +169,7 @@ public static int tp_clear(IntPtr ob)
publicstaticinttp_traverse(IntPtrob,IntPtrvisit,IntPtrarg)
{
varself=(ConstructorBinding)GetManagedObject(ob);
intres=PyVisit(self.pyTypeHndl,visit,arg);
intres=PyVisit(self.typeToCreate.Handle,visit,arg);
if(res!=0)returnres;

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

publicBoundContructor(Typetype,IntPtrpyTypeHndl,ConstructorBinderctorBinder,ConstructorInfoci)
publicBoundContructor(Typetype,PyTypetypeToCreate,ConstructorBinderctorBinder,ConstructorInfoci)
{
this.type=type;
this.pyTypeHndl=pyTypeHndl;// steal a type reference
this.typeToCreate=typeToCreate;
this.ctorBinder=ctorBinder;
ctorInfo=ci;
repr=IntPtr.Zero;
Expand DownExpand Up@@ -229,7 +229,7 @@ public static IntPtr tp_call(IntPtr op, IntPtr args, IntPtr kw)
}
// Instantiate the python object that wraps the result of the method call
// and return the PyObject* to it.
returnCLRObject.GetInstHandle(obj,self.pyTypeHndl);
returnCLRObject.GetInstHandle(obj,self.typeToCreate.Reference).DangerousMoveToPointer();
}

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

res=PyVisit(self.repr,visit,arg);
Expand Down
9 changes: 9 additions & 0 deletionssrc/runtime/exceptions.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -206,6 +206,15 @@ internal static void ErrorOccurredCheck(IntPtr pointer)
}
}

internalstaticIntPtrErrorCheckIfNull(IntPtrpointer)
{
if(pointer==IntPtr.Zero&&ErrorOccurred())
{
thrownewPythonException();
}
returnpointer;
}

/// <summary>
/// ExceptionMatches Method
/// </summary>
Expand Down
10 changes: 5 additions & 5 deletionssrc/runtime/extensiontype.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,7 @@ public ExtensionType()
// The Python instance object is related to an instance of a
// particular concrete subclass with a hidden CLR gchandle.

IntPtrtp=TypeManager.GetTypeHandle(GetType());
BorrowedReferencetp=TypeManager.GetTypeReference(GetType());

//int rc = (int)Marshal.ReadIntPtr(tp, TypeOffset.ob_refcnt);
//if (rc > 1050)
Expand All@@ -27,11 +27,11 @@ public ExtensionType()
// DebugUtil.DumpType(tp);
//}

IntPtrpy=Runtime.PyType_GenericAlloc(tp,0);
NewReferencepy=Runtime.PyType_GenericAlloc(tp,0);

//Steals a ref to tpHandle.
tpHandle=tp;
pyHandle=py;
//Borrowed reference. Valid as long as pyHandle is valid.
tpHandle=tp.DangerousGetAddress();
pyHandle=py.DangerousMoveToPointer();

#ifDEBUG
GetGCHandle(ObjectReference,TypeReference,outvarexisting);
Expand Down
35 changes: 16 additions & 19 deletionssrc/runtime/managedtype.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
#nullable enable
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Diagnostics;
Expand DownExpand Up@@ -27,8 +28,8 @@ internal enum TrackTypes
internalIntPtrpyHandle;// PyObject *
internalIntPtrtpHandle;// PyType *

internalBorrowedReferenceObjectReference=>newBorrowedReference(pyHandle);
internalBorrowedReferenceTypeReference=>newBorrowedReference(tpHandle);
internalBorrowedReferenceObjectReference=>new(pyHandle);
internalBorrowedReferenceTypeReference=>new(tpHandle);

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

Expand DownExpand Up@@ -78,12 +79,12 @@ internal void FreeGCHandle()
}
}

internalstaticManagedTypeGetManagedObject(BorrowedReferenceob)
internalstaticManagedType?GetManagedObject(BorrowedReferenceob)
=>GetManagedObject(ob.DangerousGetAddress());
/// <summary>
/// Given a Python object, return the associated managed object or null.
/// </summary>
internalstaticManagedTypeGetManagedObject(IntPtrob)
internalstaticManagedType?GetManagedObject(IntPtrob)
{
if(ob!=IntPtr.Zero)
{
Expand All@@ -106,7 +107,7 @@ internal static ManagedType GetManagedObject(IntPtr ob)
/// <summary>
/// Given a Python object, return the associated managed object type or null.
/// </summary>
internalstaticManagedTypeGetManagedObjectType(IntPtrob)
internalstaticManagedType?GetManagedObjectType(IntPtrob)
{
if(ob!=IntPtr.Zero)
{
Expand All@@ -121,18 +122,6 @@ internal static ManagedType GetManagedObjectType(IntPtr ob)
returnnull;
}


internalstaticManagedTypeGetManagedObjectErr(IntPtrob)
{
ManagedTyperesult=GetManagedObject(ob);
if(result==null)
{
Exceptions.SetError(Exceptions.TypeError,"invalid argument, expected CLR type");
}
returnresult;
}


internalstaticboolIsInstanceOfManagedType(BorrowedReferenceob)
=>IsInstanceOfManagedType(ob.DangerousGetAddressOrNull());
internalstaticboolIsInstanceOfManagedType(IntPtrob)
Expand All@@ -156,6 +145,13 @@ internal static bool IsManagedType(BorrowedReference type)
return(flags&TypeFlags.HasClrInstance)!=0;
}

publicboolIsClrMetaTypeInstance()
{
Debug.Assert(Runtime.PyCLRMetaType!=IntPtr.Zero);
Debug.Assert(pyHandle!=IntPtr.Zero);
returnRuntime.PyObject_TYPE(pyHandle)==Runtime.PyCLRMetaType;
}

internalstaticIDictionary<ManagedType,TrackTypes>GetManagedObjects()
{
return_managedObjs;
Expand DownExpand Up@@ -185,7 +181,8 @@ internal void CallTypeClear()
{
return;
}
varclearPtr=Marshal.ReadIntPtr(tpHandle,TypeOffset.tp_clear);

varclearPtr=Runtime.PyType_GetSlot(TypeReference,TypeSlotID.tp_clear);
if(clearPtr==IntPtr.Zero)
{
return;
Expand All@@ -203,7 +200,7 @@ internal void CallTypeTraverse(Interop.ObjObjFunc visitproc, IntPtr arg)
{
return;
}
vartraversePtr=Marshal.ReadIntPtr(tpHandle,TypeOffset.tp_traverse);
vartraversePtr=Runtime.PyType_GetSlot(TypeReference,TypeSlotID.tp_traverse);
if(traversePtr==IntPtr.Zero)
{
return;
Expand Down
4 changes: 2 additions & 2 deletionssrc/runtime/metatype.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -132,7 +132,7 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
{
if(clsDict.HasKey("__assembly__")||clsDict.HasKey("__namespace__"))
{
returnTypeManager.CreateSubType(name,base_type,dict);
returnTypeManager.CreateSubType(name,base_type,clsDict.Reference);
}
}
}
Expand DownExpand Up@@ -266,7 +266,7 @@ public static int tp_setattro(IntPtr tp, IntPtr name, IntPtr value)
}

intres=Runtime.PyObject_GenericSetAttr(tp,name,value);
Runtime.PyType_Modified(tp);
Runtime.PyType_Modified(newBorrowedReference(tp));

returnres;
}
Expand Down
13 changes: 13 additions & 0 deletionssrc/runtime/pytype.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,13 +6,20 @@

namespacePython.Runtime
{
[Serializable]
publicclassPyType:PyObject
{
/// <summary>Creates heap type object from the <paramref name="spec"/>.</summary>
publicPyType(TypeSpecspec,PyTuple?bases=null):base(FromSpec(spec,bases)){}
/// <summary>Wraps an existing type object.</summary>
publicPyType(PyObjecto):base(FromObject(o)){}

internalPyType(BorrowedReferencereference):base(reference)
{
if(!Runtime.PyType_Check(this.Handle))
thrownewArgumentException("object is not a type");
}

/// <summary>Checks if specified object is a Python type.</summary>
publicstaticboolIsType(PyObjectvalue)
{
Expand All@@ -21,6 +28,12 @@ public static bool IsType(PyObject value)
returnRuntime.PyType_Check(value.obj);
}

internalIntPtrGetSlot(TypeSlotIDslot)
{
IntPtrresult=Runtime.PyType_GetSlot(this.Reference,slot);
returnExceptions.ErrorCheckIfNull(result);
}

privatestaticBorrowedReferenceFromObject(PyObjecto)
{
if(oisnull)thrownewArgumentNullException(nameof(o));
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp