| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - <?xml version="1.0" encoding="utf-8"?> | ||
| 1 | + <?xml version="1.0" encoding="utf-8"?> | ||
| 2 | 2 | <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> | |
| 3 | 3 | <PropertyGroup> | |
| 4 | 4 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | |
@@ -34,7 +34,7 @@ | |||
| 34 | 34 | <DebugType>pdbonly</DebugType> | |
| 35 | 35 | </PropertyGroup> | |
| 36 | 36 | <PropertyGroup Condition=" '$(Configuration)' == 'ReleaseMonoPY3'"> | |
| 37 | - <DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS4</DefineConstants> | ||
| 37 | + <DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS4</DefineConstants> | ||
| 38 | 38 | <Optimize>true</Optimize> | |
| 39 | 39 | <DebugType>pdbonly</DebugType> | |
| 40 | 40 | </PropertyGroup> | |
@@ -46,7 +46,7 @@ | |||
| 46 | 46 | </PropertyGroup> | |
| 47 | 47 | <PropertyGroup Condition=" '$(Configuration)' == 'DebugMonoPY3'"> | |
| 48 | 48 | <DebugSymbols>true</DebugSymbols> | |
| 49 | - <DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS4;TRACE;DEBUG</DefineConstants> | ||
| 49 | + <DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS4;TRACE;DEBUG</DefineConstants> | ||
| 50 | 50 | <Optimize>false</Optimize> | |
| 51 | 51 | <DebugType>full</DebugType> | |
| 52 | 52 | </PropertyGroup> | |
@@ -56,7 +56,7 @@ | |||
| 56 | 56 | <DebugType>pdbonly</DebugType> | |
| 57 | 57 | </PropertyGroup> | |
| 58 | 58 | <PropertyGroup Condition=" '$(Configuration)' == 'ReleaseWinPY3'"> | |
| 59 | - <DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS2</DefineConstants> | ||
| 59 | + <DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS2</DefineConstants> | ||
| 60 | 60 | <Optimize>true</Optimize> | |
| 61 | 61 | <DebugType>pdbonly</DebugType> | |
| 62 | 62 | </PropertyGroup> | |
@@ -68,7 +68,7 @@ | |||
| 68 | 68 | </PropertyGroup> | |
| 69 | 69 | <PropertyGroup Condition=" '$(Configuration)' == 'DebugWinPY3'"> | |
| 70 | 70 | <DebugSymbols>true</DebugSymbols> | |
| 71 | - <DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS2;TRACE;DEBUG</DefineConstants> | ||
| 71 | + <DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS2;TRACE;DEBUG</DefineConstants> | ||
| 72 | 72 | <Optimize>false</Optimize> | |
| 73 | 73 | <DebugType>full</DebugType> | |
| 74 | 74 | </PropertyGroup> | |
@@ -137,11 +137,12 @@ | |||
| 137 | 137 | <Compile Include="pythonexception.cs" /> | |
| 138 | 138 | <Compile Include="pytuple.cs" /> | |
| 139 | 139 | <Compile Include="runtime.cs" /> | |
| 140 | + <Compile Include="runtime_state.cs" /> | ||
| 140 | 141 | <Compile Include="typemanager.cs" /> | |
| 141 | 142 | <Compile Include="typemethod.cs" /> | |
| 142 | 143 | <Compile Include="Util.cs" /> | |
| 143 | - <Compile Include="platform\Types.cs" /> | ||
| 144 | - <Compile Include="platform\LibraryLoader.cs" /> | ||
| 144 | + <Compile Include="platform\Types.cs" /> | ||
| 145 | + <Compile Include="platform\LibraryLoader.cs" /> | ||
| 145 | 146 | </ItemGroup> | |
| 146 | 147 | <ItemGroup Condition=" '$(PythonInteropFile)' != '' "> | |
| 147 | 148 | <Compile Include="$(PythonInteropFile)" /> | |
@@ -151,7 +152,7 @@ | |||
| 151 | 152 | <Compile Include="interop34.cs" /> | |
| 152 | 153 | <Compile Include="interop35.cs" /> | |
| 153 | 154 | <Compile Include="interop36.cs" /> | |
| 154 | - <Compile Include="interop37.cs" /> | ||
| 155 | + <Compile Include="interop37.cs" /> | ||
| 155 | 156 | </ItemGroup> | |
| 156 | 157 | <ItemGroup> | |
| 157 | 158 | <None Include="..\pythonnet.snk" /> | |
@@ -170,4 +171,4 @@ | |||
| 170 | 171 | <Copy SourceFiles="$(TargetAssembly)" DestinationFolder="$(PythonBuildDir)" /> | |
| 171 | 172 | <!--Copy SourceFiles="$(TargetAssemblyPdb)" Condition="Exists('$(TargetAssemblyPdb)')" DestinationFolder="$(PythonBuildDir)" /--> | |
| 172 | 173 | </Target> | |
| 173 | - </Project> | ||
| 174 | + </Project> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,7 +56,6 @@ internal static void RemoveClasses() | |||
| 56 | 56 | cls.TypeTraverse(OnVisit, visitedPtr); | |
| 57 | 57 | // XXX: Force release some resouces. | |
| 58 | 58 | cls.TypeClear(); | |
| 59 | - //Runtime.XDecref(cls.pyHandle); | ||
| 60 | 59 | } | |
| 61 | 60 | } | |
| 62 | 61 | finally | |
@@ -189,8 +188,7 @@ private static void InitClassBase(Type type, ClassBase impl) | |||
| 189 | 188 | var name = (string)iter.Key; | |
| 190 | 189 | Runtime.PyDict_SetItemString(dict, name, item.pyHandle); | |
| 191 | 190 | // info.members are already useless | |
| 192 | - Runtime.XDecref(item.pyHandle); | ||
| 193 | - item.pyHandle = IntPtr.Zero; | ||
| 191 | + item.DecrRefCount(); | ||
| 194 | 192 | } | |
| 195 | 193 | ||
| 196 | 194 | // If class has constructors, generate an __doc__ attribute. | |
@@ -225,6 +223,7 @@ private static void InitClassBase(Type type, ClassBase impl) | |||
| 225 | 223 | // TODO: deprecate __overloads__ soon... | |
| 226 | 224 | Runtime.PyDict_SetItemString(dict, "__overloads__", ctors.pyHandle); | |
| 227 | 225 | Runtime.PyDict_SetItemString(dict, "Overloads", ctors.pyHandle); | |
| 226 | + ctors.DecrRefCount(); | ||
| 228 | 227 | } | |
| 229 | 228 | ||
| 230 | 229 | // don't generate the docstring if one was already set from a DocStringAttribute. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -38,6 +38,7 @@ public ExtensionType() | |||
| 38 | 38 | ||
| 39 | 39 | Runtime.PyObject_GC_UnTrack(py); | |
| 40 | 40 | ||
| 41 | + // Steals a ref to tpHandle. | ||
| 41 | 42 | tpHandle = tp; | |
| 42 | 43 | pyHandle = py; | |
| 43 | 44 | } | |
@@ -49,7 +50,8 @@ public ExtensionType() | |||
| 49 | 50 | public static void FinalizeObject(ManagedType self) | |
| 50 | 51 | { | |
| 51 | 52 | Runtime.PyObject_GC_Del(self.pyHandle); | |
| 52 | - Runtime.XDecref(self.tpHandle); | ||
| 53 | + // Not necessary decref for `tpHandle`. | ||
| 54 | + // Runtime.XDecref(self.tpHandle); | ||
| 53 | 55 | self.FreeGCHandle(); | |
| 54 | 56 | } | |
| 55 | 57 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,7 +43,6 @@ internal static void Initialize() | |||
| 43 | 43 | py_import = Runtime.PyObject_GetAttrString(mod, "__import__"); | |
| 44 | 44 | hook = new MethodWrapper(typeof(ImportHook), "__import__", "TernaryFunc"); | |
| 45 | 45 | Runtime.PyObject_SetAttrString(mod, "__import__", hook.ptr); | |
| 46 | - Runtime.XDecref(hook.ptr); | ||
| 47 | 46 | ||
| 48 | 47 | root = new CLRModule(); | |
| 49 | 48 | ||
@@ -88,7 +87,7 @@ internal static void Shutdown() | |||
| 88 | 87 | Runtime.XDecref(root.pyHandle); | |
| 89 | 88 | root = null; | |
| 90 | 89 | ||
| 91 | - hook.Dispose(); | ||
| 90 | + hook.Release(); | ||
| 92 | 91 | hook = null; | |
| 93 | 92 | ||
| 94 | 93 | Runtime.XDecref(py_import); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -335,7 +335,6 @@ internal class TypeFlags | |||
| 335 | 335 | ||
| 336 | 336 | internal class Interop | |
| 337 | 337 | { | |
| 338 | - private static Dictionary<KeyValuePair<MethodInfo,string>, Tuple<Delegate, IntPtr>> keepAlive; | ||
| 339 | 338 | private static Hashtable pmap; | |
| 340 | 339 | ||
| 341 | 340 | static Interop() | |
@@ -352,8 +351,6 @@ static Interop() | |||
| 352 | 351 | p[item.Name] = item; | |
| 353 | 352 | } | |
| 354 | 353 | ||
| 355 | - keepAlive = new Dictionary<KeyValuePair<MethodInfo, string>, Tuple<Delegate, IntPtr>>(); | ||
| 356 | - //Marshal.AllocHGlobal(IntPtr.Size); | ||
| 357 | 354 | pmap = new Hashtable(); | |
| 358 | 355 | ||
| 359 | 356 | pmap["tp_dealloc"] = p["DestructorFunc"]; | |
@@ -450,24 +447,12 @@ internal static Type GetPrototype(string name) | |||
| 450 | 447 | return pmap[name] as Type; | |
| 451 | 448 | } | |
| 452 | 449 | ||
| 453 | - internal static IntPtr GetThunk(MethodInfo method, string funcType = null) | ||
| 450 | + internal static ThunkInfo GetThunk(MethodInfo method, string funcType = null) | ||
| 454 | 451 | { | |
| 455 | - var key = new KeyValuePair<MethodInfo, string>(method, funcType); | ||
| 456 | - Tuple<Delegate, IntPtr> thunkPair; | ||
| 457 | - if (keepAlive.TryGetValue(key, out thunkPair)) | ||
| 458 | - { | ||
| 459 | - return thunkPair.Item2; | ||
| 460 | - } | ||
| 461 | - thunkPair = GetThunkImpl(method, funcType); | ||
| 462 | - if (thunkPair == null) | ||
| 463 | - { | ||
| 464 | - return IntPtr.Zero; | ||
| 465 | - } | ||
| 466 | - keepAlive[key] = thunkPair; | ||
| 467 | - return thunkPair.Item2; | ||
| 452 | + return GetThunkImpl(method, funcType); | ||
| 468 | 453 | } | |
| 469 | 454 | ||
| 470 | - private static Tuple<Delegate, IntPtr> GetThunkImpl(MethodInfo method, string funcType) | ||
| 455 | + private static ThunkInfo GetThunkImpl(MethodInfo method, string funcType) | ||
| 471 | 456 | { | |
| 472 | 457 | Type dt; | |
| 473 | 458 | if (funcType != null) | |
@@ -477,15 +462,15 @@ private static Tuple<Delegate, IntPtr> GetThunkImpl(MethodInfo method, string fu | |||
| 477 | 462 | ||
| 478 | 463 | if (dt == null) | |
| 479 | 464 | { | |
| 480 | - return null; | ||
| 465 | + return ThunkInfo.Empty; | ||
| 481 | 466 | } | |
| 482 | 467 | IntPtr tmp = Marshal.AllocHGlobal(IntPtr.Size); | |
| 483 | 468 | Delegate d = Delegate.CreateDelegate(dt, method); | |
| 484 | 469 | Thunk cb = new Thunk(d); | |
| 485 | 470 | Marshal.StructureToPtr(cb, tmp, false); | |
| 486 | 471 | IntPtr fp = Marshal.ReadIntPtr(tmp, 0); | |
| 487 | 472 | Marshal.FreeHGlobal(tmp); | |
| 488 | - return new Tuple<Delegate, IntPtr>(d, fp); | ||
| 473 | + return new ThunkInfo(d, fp); | ||
| 489 | 474 | } | |
| 490 | 475 | ||
| 491 | 476 | [UnmanagedFunctionPointer(CallingConvention.Cdecl)] | |
@@ -540,6 +525,19 @@ public Thunk(Delegate d) | |||
| 540 | 525 | } | |
| 541 | 526 | } | |
| 542 | 527 | ||
| 528 | + internal struct ThunkInfo | ||
| 529 | + { | ||
| 530 | + public Delegate Target; | ||
| 531 | + public IntPtr Address; | ||
| 532 | + | ||
| 533 | + public static readonly ThunkInfo Empty = new ThunkInfo(null, IntPtr.Zero); | ||
| 534 | + | ||
| 535 | + public ThunkInfo(Delegate target, IntPtr addr) | ||
| 536 | + { | ||
| 537 | + Target = target; | ||
| 538 | + Address = addr; | ||
| 539 | + } | ||
| 540 | + } | ||
| 543 | 541 | ||
| 544 | 542 | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] | |
| 545 | 543 | struct PyGC_Node | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,51 +8,46 @@ namespace Python.Runtime | |||
| 8 | 8 | /// currently used mainly to implement special cases like the CLR | |
| 9 | 9 | /// import hook. | |
| 10 | 10 | /// </summary> | |
| 11 | - internal class MethodWrapper : IDisposable | ||
| 11 | + internal class MethodWrapper | ||
| 12 | 12 | { | |
| 13 | 13 | public IntPtr mdef; | |
| 14 | 14 | public IntPtr ptr; | |
| 15 | + public ThunkInfo _thunk; | ||
| 15 | 16 | private bool _disposed = false; | |
| 16 | 17 | ||
| 17 | 18 | public MethodWrapper(Type type, string name, string funcType = null) | |
| 18 | 19 | { | |
| 19 | 20 | // Turn the managed method into a function pointer | |
| 20 | 21 | ||
| 21 | - IntPtr fp = Interop.GetThunk(type.GetMethod(name), funcType); | ||
| 22 | + _thunk = Interop.GetThunk(type.GetMethod(name), funcType); | ||
| 22 | 23 | ||
| 23 | 24 | // Allocate and initialize a PyMethodDef structure to represent | |
| 24 | 25 | // the managed method, then create a PyCFunction. | |
| 25 | 26 | ||
| 26 | 27 | mdef = Runtime.PyMem_Malloc(4 * IntPtr.Size); | |
| 27 | - TypeManager.WriteMethodDef(mdef, name, fp, 0x0003); | ||
| 28 | + TypeManager.WriteMethodDef(mdef, name, _thunk.Address, 0x0003); | ||
| 28 | 29 | ptr = Runtime.PyCFunction_NewEx(mdef, IntPtr.Zero, IntPtr.Zero); | |
| 29 | 30 | } | |
| 30 | 31 | ||
| 31 | - ~MethodWrapper() | ||
| 32 | - { | ||
| 33 | - Dispose(); | ||
| 34 | - } | ||
| 35 | 32 | ||
| 36 | 33 | public IntPtr Call(IntPtr args, IntPtr kw) | |
| 37 | 34 | { | |
| 38 | 35 | return Runtime.PyCFunction_Call(ptr, args, kw); | |
| 39 | 36 | } | |
| 40 | 37 | ||
| 41 | - public void Dispose() | ||
| 38 | + public void Release() | ||
| 42 | 39 | { | |
| 43 | 40 | if (_disposed) | |
| 44 | 41 | { | |
| 45 | 42 | return; | |
| 46 | 43 | } | |
| 47 | - using (Py.GIL()) | ||
| 44 | + _disposed = true; | ||
| 45 | + bool freeDef = Runtime.Refcount(ptr) == 1; | ||
| 46 | + Runtime.XDecref(ptr); | ||
| 47 | + if (freeDef && mdef != IntPtr.Zero) | ||
| 48 | 48 | { | |
| 49 | - _disposed = true; | ||
| 50 | - Runtime.XDecref(ptr); | ||
| 51 | - if (mdef != IntPtr.Zero) | ||
| 52 | - { | ||
| 53 | - Runtime.PyMem_Free(mdef); | ||
| 54 | - mdef = IntPtr.Zero; | ||
| 55 | - } | ||
| 49 | + Runtime.PyMem_Free(mdef); | ||
| 50 | + mdef = IntPtr.Zero; | ||
| 56 | 51 | } | |
| 57 | 52 | } | |
| 58 | 53 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -213,12 +213,7 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true, | |||
| 213 | 213 | Runtime.PyDict_SetItemString(module_globals, "__builtins__", builtins); | |
| 214 | 214 | ||
| 215 | 215 | Assembly assembly = Assembly.GetExecutingAssembly(); | |
| 216 | - Stream stream = assembly.GetManifestResourceStream("clr.py"); | ||
| 217 | - if (stream == null) | ||
| 218 | - { | ||
| 219 | - stream = File.OpenRead(@"I:\repos\pythonnet\src\runtime\resources\clr.py"); | ||
| 220 | - } | ||
| 221 | - using (stream) | ||
| 216 | + using (Stream stream = assembly.GetManifestResourceStream("clr.py")) | ||
| 222 | 217 | using (var reader = new StreamReader(stream)) | |
| 223 | 218 | { | |
| 224 | 219 | // add the contents of clr.py to the module | |
| Back | FazBrowse Home | New Git URL |
0 commit comments