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

Fixed segfault in ClassDerived.tp_dealloc#1330

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
filmor merged 4 commits intopythonnet:masterfromlosttech:bugs/1327
Dec 18, 2020
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
3 changes: 2 additions & 1 deletionsrc/runtime/classderived.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -75,7 +75,8 @@ internal ClassDerivedObject(Type tp) : base(tp)
// So we don't call PyObject_GC_Del here and instead we set the python
// reference to a weak reference so that the C# object can be collected.
GCHandle gc = GCHandle.Alloc(self, GCHandleType.Weak);
Marshal.WriteIntPtr(self.pyHandle, ObjectOffset.magic(self.tpHandle), (IntPtr)gc);
int gcOffset = ObjectOffset.magic(Runtime.PyObject_TYPE(self.pyHandle));
Marshal.WriteIntPtr(self.pyHandle, gcOffset, (IntPtr)gc);
self.gcHandle.Free();
self.gcHandle = gc;
}
Expand Down
2 changes: 2 additions & 0 deletionssrc/runtime/clrobject.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Python.Runtime
Expand DownExpand Up@@ -85,6 +86,7 @@ internal static CLRObject Restore(object ob, IntPtr pyHandle, InterDomainContext
pyHandle = pyHandle,
tpHandle = Runtime.PyObject_TYPE(pyHandle)
};
Debug.Assert(co.tpHandle != IntPtr.Zero);
co.Load(context);
return co;
}
Expand Down
4 changes: 3 additions & 1 deletionsrc/runtime/converter.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -313,7 +313,9 @@ internal static bool ToManaged(IntPtr value, Type type,
return Converter.ToManagedValue(value, type, out result, setError);
}


internal static bool ToManagedValue(BorrowedReference value, Type obType,
out object result, bool setError)
=> ToManagedValue(value.DangerousGetAddress(), obType, out result, setError);
internal static bool ToManagedValue(IntPtr value, Type obType,
out object result, bool setError)
{
Expand Down
8 changes: 5 additions & 3 deletionssrc/runtime/native/TypeOffset.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -85,9 +85,11 @@ internal static void Use(ITypeOffsets offsets)
internal static Dictionary<string, int> GetOffsets()
{
var properties = typeof(TypeOffset).GetProperties(FieldFlags);
return properties.ToDictionary(
keySelector: p => p.Name,
elementSelector: p => (int)p.GetValue(obj: null, index: null));
var result = properties.ToDictionary(
keySelector: p => p.Name,
elementSelector: p => (int)p.GetValue(obj: null, index: null));
Debug.Assert(result.Values.Any(v => v != 0));
return result;
}

internal static int GetOffsetUncached(string name)
Expand Down
2 changes: 2 additions & 0 deletionssrc/runtime/runtime.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1641,6 +1641,8 @@ internal static bool PyDict_Check(IntPtr ob)
/// </summary>
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyDict_GetItem(IntPtr pointer, IntPtr key);
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern BorrowedReference PyDict_GetItemWithError(BorrowedReference pointer, BorrowedReference key);

/// <summary>
/// Return value: Borrowed reference.
Expand Down
68 changes: 31 additions & 37 deletionssrc/runtime/typemanager.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@
using System.Runtime.InteropServices;
using System.Diagnostics;
using Python.Runtime.Slots;
using static Python.Runtime.PythonException;

namespace Python.Runtime
{
Expand DownExpand Up@@ -142,7 +143,7 @@ internal static IntPtr GetTypeHandle(ManagedType obj, Type type)
/// </summary>
internal static IntPtr CreateType(Type impl)
{
IntPtr type = AllocateTypeObject(impl.Name);
IntPtr type = AllocateTypeObject(impl.Name, metatype: Runtime.PyTypeType);
int ob_size = ObjectOffset.Size(type);

// Set tp_basicsize to the size of our managed instance objects.
Expand DownExpand Up@@ -211,7 +212,7 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)
base_ = bc.pyHandle;
}

IntPtr type = AllocateTypeObject(name);
IntPtr type = AllocateTypeObject(name, Runtime.PyCLRMetaType);

Marshal.WriteIntPtr(type, TypeOffset.ob_type, Runtime.PyCLRMetaType);
Runtime.XIncref(Runtime.PyCLRMetaType);
Expand DownExpand Up@@ -302,6 +303,7 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)

internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr py_dict)
{
var dictRef = new BorrowedReference(py_dict);
// Utility to create a subtype of a managed type with the ability for the
// a python subtype able to override the managed implementation
string name = Runtime.GetManagedString(py_name);
Expand All@@ -311,40 +313,29 @@ internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr
object assembly = null;
object namespaceStr = null;

var disposeList = new List<PyObject>();
try
using (var assemblyKey = new PyString("__assembly__"))
{
var assemblyKey = new PyObject(Converter.ToPython("__assembly__", typeof(string)));
disposeList.Add(assemblyKey);
if (0 != Runtime.PyMapping_HasKey(py_dict, assemblyKey.Handle))
var assemblyPtr = Runtime.PyDict_GetItemWithError(dictRef, assemblyKey.Reference);
if (assemblyPtr.IsNull)
{
var pyAssembly = new PyObject(Runtime.PyDict_GetItem(py_dict, assemblyKey.Handle));
Runtime.XIncref(pyAssembly.Handle);
disposeList.Add(pyAssembly);
if (!Converter.ToManagedValue(pyAssembly.Handle, typeof(string), out assembly, false))
{
throw new InvalidCastException("Couldn't convert __assembly__ value to string");
}
if (Exceptions.ErrorOccurred()) return IntPtr.Zero;
}
else if (!Converter.ToManagedValue(assemblyPtr, typeof(string), out assembly, false))
{
return Exceptions.RaiseTypeError("Couldn't convert __assembly__ value to string");
}

var namespaceKey = new PyObject(Converter.ToPythonImplicit("__namespace__"));
disposeList.Add(namespaceKey);
if (0 != Runtime.PyMapping_HasKey(py_dict, namespaceKey.Handle))
using (var namespaceKey = new PyString("__namespace__"))
{
var pyNamespace = new PyObject(Runtime.PyDict_GetItem(py_dict, namespaceKey.Handle));
Runtime.XIncref(pyNamespace.Handle);
disposeList.Add(pyNamespace);
if (!Converter.ToManagedValue(pyNamespace.Handle, typeof(string), out namespaceStr, false))
var pyNamespace = Runtime.PyDict_GetItemWithError(dictRef, namespaceKey.Reference);
if (pyNamespace.IsNull)
{
throw new InvalidCastException("Couldn't convert __namespace__ value to string");
if (Exceptions.ErrorOccurred()) return IntPtr.Zero;
}
else if (!Converter.ToManagedValue(pyNamespace, typeof(string), out namespaceStr, false))
{
return Exceptions.RaiseTypeError("Couldn't convert __namespace__ value to string");
}
}
}
finally
{
foreach (PyObject o in disposeList)
{
o.Dispose();
}
}

Expand All@@ -370,14 +361,14 @@ internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr
// by default the class dict will have all the C# methods in it, but as this is a
// derived class we want the python overrides in there instead if they exist.
IntPtr cls_dict = Marshal.ReadIntPtr(py_type, TypeOffset.tp_dict);
Runtime.PyDict_Update(cls_dict, py_dict);
ThrowIfIsNotZero(Runtime.PyDict_Update(cls_dict, py_dict));
Runtime.XIncref(py_type);
// Update the __classcell__ if it exists
var cell = new BorrowedReference(Runtime.PyDict_GetItemString(cls_dict, "__classcell__"));
if (!cell.IsNull)
{
Runtime.PyCell_Set(cell, py_type);
Runtime.PyDict_DelItemString(cls_dict, "__classcell__");
ThrowIfIsNotZero(Runtime.PyCell_Set(cell, py_type));
ThrowIfIsNotZero(Runtime.PyDict_DelItemString(cls_dict, "__classcell__"));
}

return py_type;
Expand DownExpand Up@@ -436,12 +427,15 @@ internal static IntPtr CreateMetaType(Type impl, out SlotsHolder slotsHolder)
// the standard type slots, and has to subclass PyType_Type for
// certain functions in the C runtime to work correctly with it.

IntPtr type = AllocateTypeObject("CLR Metatype");
IntPtr py_type = Runtime.PyTypeType;
IntPtr type = AllocateTypeObject("CLR Metatype", metatype: Runtime.PyTypeType);

IntPtr py_type = Runtime.PyTypeType;
Marshal.WriteIntPtr(type, TypeOffset.tp_base, py_type);
Runtime.XIncref(py_type);

int size = TypeOffset.magic() + IntPtr.Size;
Marshal.WriteIntPtr(type, TypeOffset.tp_basicsize, new IntPtr(size));

const int flags = TypeFlags.Default
| TypeFlags.Managed
| TypeFlags.HeapType
Expand DownExpand Up@@ -531,7 +525,7 @@ internal static IntPtr BasicSubType(string name, IntPtr base_, Type impl)
// Utility to create a subtype of a std Python type, but with
// a managed type able to override implementation

IntPtr type = AllocateTypeObject(name);
IntPtr type = AllocateTypeObject(name, metatype: Runtime.PyTypeType);
//Marshal.WriteIntPtr(type, TypeOffset.tp_basicsize, (IntPtr)obSize);
//Marshal.WriteIntPtr(type, TypeOffset.tp_itemsize, IntPtr.Zero);

Expand DownExpand Up@@ -573,9 +567,9 @@ internal static IntPtr BasicSubType(string name, IntPtr base_, Type impl)
/// <summary>
/// Utility method to allocate a type object &amp; do basic initialization.
/// </summary>
internal static IntPtr AllocateTypeObject(string name)
internal static IntPtr AllocateTypeObject(string name, IntPtr metatype)
{
IntPtr type = Runtime.PyType_GenericAlloc(Runtime.PyTypeType, 0);
IntPtr type = Runtime.PyType_GenericAlloc(metatype, 0);
// Clr type would not use __slots__,
// and the PyMemberDef after PyHeapTypeObject will have other uses(e.g. type handle),
// thus set the ob_size to 0 for avoiding slots iterations.
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp