| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,13 +29,6 @@ internal virtual bool CanSubclass() | |||
| 29 | 29 | return !type.IsEnum; | |
| 30 | 30 | } | |
| 31 | 31 | ||
| 32 | - /// <summary> | ||
| 33 | - /// Implements __init__ for reflected classes and value types. | ||
| 34 | - /// </summary> | ||
| 35 | - public static int tp_init(IntPtr ob, IntPtr args, IntPtr kw) | ||
| 36 | - { | ||
| 37 | - return 0; | ||
| 38 | - } | ||
| 39 | 32 | ||
| 40 | 33 | /// <summary> | |
| 41 | 34 | /// Default implementation of [] semantics for reflected types. | |
@@ -254,13 +247,9 @@ public static IntPtr tp_str(IntPtr ob) | |||
| 254 | 247 | public static void tp_dealloc(IntPtr ob) | |
| 255 | 248 | { | |
| 256 | 249 | ManagedType self = GetManagedObject(ob); | |
| 257 | - if (self.pyHandle != self.tpHandle) | ||
| 258 | - { | ||
| 259 | - ClearObjectDict(ob); | ||
| 260 | - } | ||
| 250 | + tp_clear(ob); | ||
| 261 | 251 | Runtime.PyObject_GC_UnTrack(self.pyHandle); | |
| 262 | 252 | Runtime.PyObject_GC_Del(self.pyHandle); | |
| 263 | - Runtime.XDecref(self.tpHandle); | ||
| 264 | 253 | self.FreeGCHandle(); | |
| 265 | 254 | } | |
| 266 | 255 | ||
@@ -271,9 +260,7 @@ public static int tp_clear(IntPtr ob) | |||
| 271 | 260 | { | |
| 272 | 261 | ClearObjectDict(ob); | |
| 273 | 262 | } | |
| 274 | - Runtime.XDecref(self.tpHandle); | ||
| 275 | - self.tpHandle = IntPtr.Zero; | ||
| 276 | - self.FreeGCHandle(); | ||
| 263 | + Runtime.Py_CLEAR(ref self.tpHandle); | ||
| 277 | 264 | return 0; | |
| 278 | 265 | } | |
| 279 | 266 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -54,7 +54,7 @@ internal static void RemoveClasses() | |||
| 54 | 54 | foreach (var cls in cache.Values) | |
| 55 | 55 | { | |
| 56 | 56 | cls.TypeTraverse(OnVisit, visitedPtr); | |
| 57 | - // XXX: Force release some resouces. | ||
| 57 | + // XXX: Force release instance resources but not dealloc itself. | ||
| 58 | 58 | cls.TypeClear(); | |
| 59 | 59 | } | |
| 60 | 60 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -92,21 +92,5 @@ public static void tp_dealloc(IntPtr ob) | |||
| 92 | 92 | ManagedType self = GetManagedObject(ob); | |
| 93 | 93 | FinalizeObject(self); | |
| 94 | 94 | } | |
| 95 | - | ||
| 96 | - public static int tp_clear(IntPtr ob) | ||
| 97 | - { | ||
| 98 | - ManagedType self = GetManagedObject(ob); | ||
| 99 | - Runtime.XDecref(self.tpHandle); | ||
| 100 | - self.FreeGCHandle(); | ||
| 101 | - return 0; | ||
| 102 | - } | ||
| 103 | - | ||
| 104 | - //public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg) | ||
| 105 | - //{ | ||
| 106 | - // ManagedType self = GetManagedObject(ob); | ||
| 107 | - // int res = PyVisit(self.tpHandle, visit, arg); | ||
| 108 | - // if (res != 0) return res; | ||
| 109 | - // return 0; | ||
| 110 | - //} | ||
| 111 | 95 | } | |
| 112 | 96 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,7 +59,7 @@ public IncorrectRefCountException(IntPtr ptr) | |||
| 59 | 59 | IntPtr pyname = Runtime.PyObject_Unicode(PyPtr); | |
| 60 | 60 | string name = Runtime.GetManagedString(pyname); | |
| 61 | 61 | Runtime.XDecref(pyname); | |
| 62 | - _message = $"{name} may has a incorrect ref count"; | ||
| 62 | + _message = $"<{name}> may has a incorrect ref count"; | ||
| 63 | 63 | } | |
| 64 | 64 | } | |
| 65 | 65 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ | |||
| 5 | 5 | using System.Reflection; | |
| 6 | 6 | using System.Text; | |
| 7 | 7 | using System.Collections.Generic; | |
| 8 | + using System.Linq; | ||
| 8 | 9 | ||
| 9 | 10 | namespace Python.Runtime | |
| 10 | 11 | { | |
@@ -262,6 +263,14 @@ public static void FreeModuleDef(IntPtr ptr) | |||
| 262 | 263 | } | |
| 263 | 264 | #endif // PYTHON3 | |
| 264 | 265 | ||
| 266 | + static class TypeOffsetHelper | ||
| 267 | + { | ||
| 268 | + public static string GetSlotNameByOffset(int offset) | ||
| 269 | + { | ||
| 270 | + return typeof(TypeOffset).GetFields().First(fi => (int)fi.GetValue(null) == offset).Name; | ||
| 271 | + } | ||
| 272 | + } | ||
| 273 | + | ||
| 265 | 274 | /// <summary> | |
| 266 | 275 | /// TypeFlags(): The actual bit values for the Type Flags stored | |
| 267 | 276 | /// in a class. | |
@@ -448,11 +457,6 @@ internal static Type GetPrototype(string name) | |||
| 448 | 457 | } | |
| 449 | 458 | ||
| 450 | 459 | internal static ThunkInfo GetThunk(MethodInfo method, string funcType = null) | |
| 451 | - { | ||
| 452 | - return GetThunkImpl(method, funcType); | ||
| 453 | - } | ||
| 454 | - | ||
| 455 | - private static ThunkInfo GetThunkImpl(MethodInfo method, string funcType) | ||
| 456 | 460 | { | |
| 457 | 461 | Type dt; | |
| 458 | 462 | if (funcType != null) | |
@@ -473,6 +477,7 @@ private static ThunkInfo GetThunkImpl(MethodInfo method, string funcType) | |||
| 473 | 477 | return new ThunkInfo(d, fp); | |
| 474 | 478 | } | |
| 475 | 479 | ||
| 480 | + | ||
| 476 | 481 | [UnmanagedFunctionPointer(CallingConvention.Cdecl)] | |
| 477 | 482 | public delegate IntPtr UnaryFunc(IntPtr ob); | |
| 478 | 483 | ||
@@ -552,4 +557,14 @@ struct PyGC_Head | |||
| 552 | 557 | { | |
| 553 | 558 | public PyGC_Node gc; | |
| 554 | 559 | } | |
| 560 | + | ||
| 561 | + | ||
| 562 | + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] | ||
| 563 | + struct PyMethodDef | ||
| 564 | + { | ||
| 565 | + public IntPtr ml_name; | ||
| 566 | + public IntPtr ml_meth; | ||
| 567 | + public int ml_flags; | ||
| 568 | + public IntPtr ml_doc; | ||
| 569 | + } | ||
| 555 | 570 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,15 @@ public static IntPtr Initialize() | |||
| 22 | 22 | return PyCLRMetaType; | |
| 23 | 23 | } | |
| 24 | 24 | ||
| 25 | + public static void Release() | ||
| 26 | + { | ||
| 27 | + if (Runtime.Refcount(PyCLRMetaType) > 1) | ||
| 28 | + { | ||
| 29 | + SlotsHolder.ReleaseTypeSlots(PyCLRMetaType); | ||
| 30 | + } | ||
| 31 | + Runtime.XDecref(PyCLRMetaType); | ||
| 32 | + PyCLRMetaType = IntPtr.Zero; | ||
| 33 | + } | ||
| 25 | 34 | ||
| 26 | 35 | /// <summary> | |
| 27 | 36 | /// Metatype __new__ implementation. This is called to create a new | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -38,10 +38,8 @@ public MethodBinding(MethodObject m, IntPtr target) : this(m, target, IntPtr.Zer | |||
| 38 | 38 | ||
| 39 | 39 | private void ClearMembers() | |
| 40 | 40 | { | |
| 41 | - Runtime.XDecref(target); | ||
| 42 | - target = IntPtr.Zero; | ||
| 43 | - Runtime.XDecref(targetType); | ||
| 44 | - targetType = IntPtr.Zero; | ||
| 41 | + Runtime.Py_CLEAR(ref target); | ||
| 42 | + Runtime.Py_CLEAR(ref targetType); | ||
| 45 | 43 | } | |
| 46 | 44 | ||
| 47 | 45 | /// <summary> | |
@@ -249,11 +247,11 @@ public static IntPtr tp_repr(IntPtr ob) | |||
| 249 | 247 | FinalizeObject(self); | |
| 250 | 248 | } | |
| 251 | 249 | ||
| 252 | - public new static int tp_clear(IntPtr ob) | ||
| 250 | + public static int tp_clear(IntPtr ob) | ||
| 253 | 251 | { | |
| 254 | 252 | var self = (MethodBinding)GetManagedObject(ob); | |
| 255 | 253 | self.ClearMembers(); | |
| 256 | - return ExtensionType.tp_clear(ob); | ||
| 254 | + return 0; | ||
| 257 | 255 | } | |
| 258 | 256 | } | |
| 259 | 257 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -211,11 +211,11 @@ public static IntPtr tp_repr(IntPtr ob) | |||
| 211 | 211 | ExtensionType.FinalizeObject(self); | |
| 212 | 212 | } | |
| 213 | 213 | ||
| 214 | - public new static int tp_clear(IntPtr ob) | ||
| 214 | + public static int tp_clear(IntPtr ob) | ||
| 215 | 215 | { | |
| 216 | 216 | var self = (MethodObject)GetManagedObject(ob); | |
| 217 | 217 | self.ClearMembers(); | |
| 218 | - return ExtensionType.tp_clear(ob); | ||
| 218 | + return 0; | ||
| 219 | 219 | } | |
| 220 | 220 | } | |
| 221 | 221 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -304,13 +304,7 @@ public static IntPtr tp_repr(IntPtr ob) | |||
| 304 | 304 | public new static void tp_dealloc(IntPtr ob) | |
| 305 | 305 | { | |
| 306 | 306 | var self = (ModuleObject)GetManagedObject(ob); | |
| 307 | - Runtime.XDecref(self.dict); | ||
| 308 | - self.dict = IntPtr.Zero; | ||
| 309 | - foreach (var attr in self.cache.Values) | ||
| 310 | - { | ||
| 311 | - Runtime.XDecref(attr.pyHandle); | ||
| 312 | - } | ||
| 313 | - self.cache.Clear(); | ||
| 307 | + tp_clear(ob); | ||
| 314 | 308 | ExtensionType.tp_dealloc(ob); | |
| 315 | 309 | } | |
| 316 | 310 | ||
@@ -327,7 +321,7 @@ public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg) | |||
| 327 | 321 | return 0; | |
| 328 | 322 | } | |
| 329 | 323 | ||
| 330 | - public new static int tp_clear(IntPtr ob) | ||
| 324 | + public static int tp_clear(IntPtr ob) | ||
| 331 | 325 | { | |
| 332 | 326 | var self = (ModuleObject)GetManagedObject(ob); | |
| 333 | 327 | Runtime.XDecref(self.dict); | |
@@ -337,7 +331,7 @@ public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg) | |||
| 337 | 331 | Runtime.XDecref(attr.pyHandle); | |
| 338 | 332 | } | |
| 339 | 333 | self.cache.Clear(); | |
| 340 | - return ExtensionType.tp_clear(ob); | ||
| 334 | + return 0; | ||
| 341 | 335 | } | |
| 342 | 336 | } | |
| 343 | 337 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -194,10 +194,10 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true, | |||
| 194 | 194 | // register the atexit callback (this doesn't use Py_AtExit as the C atexit | |
| 195 | 195 | // callbacks are called after python is fully finalized but the python ones | |
| 196 | 196 | // are called while the python engine is still running). | |
| 197 | - string code = | ||
| 198 | - "import atexit, clr\n" + | ||
| 199 | - "atexit.register(clr._AtExit)\n"; | ||
| 200 | - PythonEngine.Exec(code); | ||
| 197 | + //string code = | ||
| 198 | + // "import atexit, clr\n" + | ||
| 199 | + // "atexit.register(clr._AtExit)\n"; | ||
| 200 | + //PythonEngine.Exec(code); | ||
| 201 | 201 | } | |
| 202 | 202 | ||
| 203 | 203 | // Load the clr.py resource into the clr module | |
@@ -756,6 +756,9 @@ public static void With(PyObject obj, Action<dynamic> Body) | |||
| 756 | 756 | traceBack = ex.PyTB; | |
| 757 | 757 | } | |
| 758 | 758 | ||
| 759 | + Runtime.XIncref(type); | ||
| 760 | + Runtime.XIncref(val); | ||
| 761 | + Runtime.XIncref(traceBack); | ||
| 759 | 762 | var exitResult = obj.InvokeMethod("__exit__", new PyObject(type), new PyObject(val), new PyObject(traceBack)); | |
| 760 | 763 | ||
| 761 | 764 | if (ex != null && !exitResult.IsTrue()) throw ex; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments