| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -421,13 +421,18 @@ public PyObject TryEncode(object value) | |||
| 421 | 421 | } | |
| 422 | 422 | } | |
| 423 | 423 | ||
| 424 | - class InstancelessExceptionDecoder : IPyObjectDecoder | ||
| 424 | + class InstancelessExceptionDecoder : IPyObjectDecoder, IDisposable | ||
| 425 | 425 | { | |
| 426 | 426 | readonly PyObject PyErr = Py.Import("clr.interop").GetAttr("PyErr"); | |
| 427 | 427 | ||
| 428 | 428 | public bool CanDecode(PyType objectType, Type targetType) | |
| 429 | 429 | => PythonReferenceComparer.Instance.Equals(PyErr, objectType); | |
| 430 | 430 | ||
| 431 | + public void Dispose() | ||
| 432 | + { | ||
| 433 | + PyErr.Dispose(); | ||
| 434 | + } | ||
| 435 | + | ||
| 431 | 436 | public bool TryDecode<T>(PyObject pyObj, out T value) | |
| 432 | 437 | { | |
| 433 | 438 | if (pyObj.HasAttr("value")) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,7 @@ public void SetUp() | |||
| 24 | 24 | [OneTimeTearDown] | |
| 25 | 25 | public void Dispose() | |
| 26 | 26 | { | |
| 27 | + ExtraBaseTypeProvider.ExtraBase.Dispose(); | ||
| 27 | 28 | PythonEngine.Shutdown(); | |
| 28 | 29 | } | |
| 29 | 30 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -176,6 +176,7 @@ public static void TestRunExitFuncs() | |||
| 176 | 176 | { | |
| 177 | 177 | Assert.Fail(msg); | |
| 178 | 178 | } | |
| 179 | + PythonEngine.InteropConfiguration = InteropConfiguration.MakeDefault(); | ||
| 179 | 180 | return; | |
| 180 | 181 | } | |
| 181 | 182 | bool called = false; | |
@@ -187,6 +188,7 @@ public static void TestRunExitFuncs() | |||
| 187 | 188 | atexit.Dispose(); | |
| 188 | 189 | Runtime.Runtime.Shutdown(); | |
| 189 | 190 | Assert.True(called); | |
| 191 | + PythonEngine.InteropConfiguration = InteropConfiguration.MakeDefault(); | ||
| 190 | 192 | } | |
| 191 | 193 | } | |
| 192 | 194 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,7 +8,7 @@ namespace Python.Runtime.Codecs | |||
| 8 | 8 | /// <summary> | |
| 9 | 9 | /// Represents a group of <see cref="IPyObjectDecoder"/>s. Useful to group them by priority. | |
| 10 | 10 | /// </summary> | |
| 11 | - public sealed class DecoderGroup: IPyObjectDecoder, IEnumerable<IPyObjectDecoder> | ||
| 11 | + public sealed class DecoderGroup: IPyObjectDecoder, IEnumerable<IPyObjectDecoder>, IDisposable | ||
| 12 | 12 | { | |
| 13 | 13 | readonly List<IPyObjectDecoder> decoders = new List<IPyObjectDecoder>(); | |
| 14 | 14 | ||
@@ -46,6 +46,15 @@ public bool TryDecode<T>(PyObject pyObj, out T value) | |||
| 46 | 46 | /// <inheritdoc /> | |
| 47 | 47 | public IEnumerator<IPyObjectDecoder> GetEnumerator() => this.decoders.GetEnumerator(); | |
| 48 | 48 | IEnumerator IEnumerable.GetEnumerator() => this.decoders.GetEnumerator(); | |
| 49 | + | ||
| 50 | + public void Dispose() | ||
| 51 | + { | ||
| 52 | + foreach (var decoder in this.decoders.OfType<IDisposable>()) | ||
| 53 | + { | ||
| 54 | + decoder.Dispose(); | ||
| 55 | + } | ||
| 56 | + this.decoders.Clear(); | ||
| 57 | + } | ||
| 49 | 58 | } | |
| 50 | 59 | ||
| 51 | 60 | public static class DecoderGroupExtensions | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,7 +8,7 @@ namespace Python.Runtime.Codecs | |||
| 8 | 8 | /// <summary> | |
| 9 | 9 | /// Represents a group of <see cref="IPyObjectDecoder"/>s. Useful to group them by priority. | |
| 10 | 10 | /// </summary> | |
| 11 | - public sealed class EncoderGroup: IPyObjectEncoder, IEnumerable<IPyObjectEncoder> | ||
| 11 | + public sealed class EncoderGroup: IPyObjectEncoder, IEnumerable<IPyObjectEncoder>, IDisposable | ||
| 12 | 12 | { | |
| 13 | 13 | readonly List<IPyObjectEncoder> encoders = new List<IPyObjectEncoder>(); | |
| 14 | 14 | ||
@@ -47,6 +47,15 @@ public PyObject TryEncode(object value) | |||
| 47 | 47 | /// <inheritdoc /> | |
| 48 | 48 | public IEnumerator<IPyObjectEncoder> GetEnumerator() => this.encoders.GetEnumerator(); | |
| 49 | 49 | IEnumerator IEnumerable.GetEnumerator() => this.encoders.GetEnumerator(); | |
| 50 | + | ||
| 51 | + public void Dispose() | ||
| 52 | + { | ||
| 53 | + foreach (var encoder in this.encoders.OfType<IDisposable>()) | ||
| 54 | + { | ||
| 55 | + encoder.Dispose(); | ||
| 56 | + } | ||
| 57 | + this.encoders.Clear(); | ||
| 58 | + } | ||
| 50 | 59 | } | |
| 51 | 60 | ||
| 52 | 61 | public static class EncoderGroupExtensions | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,10 +2,11 @@ namespace Python.Runtime | |||
| 2 | 2 | { | |
| 3 | 3 | using System; | |
| 4 | 4 | using System.Collections.Generic; | |
| 5 | + using System.Linq; | ||
| 5 | 6 | ||
| 6 | 7 | using Python.Runtime.Mixins; | |
| 7 | 8 | ||
| 8 | - public sealed class InteropConfiguration | ||
| 9 | + public sealed class InteropConfiguration: IDisposable | ||
| 9 | 10 | { | |
| 10 | 11 | internal readonly PythonBaseTypeProviderGroup pythonBaseTypeProviders | |
| 11 | 12 | = new PythonBaseTypeProviderGroup(); | |
@@ -24,5 +25,14 @@ public static InteropConfiguration MakeDefault() | |||
| 24 | 25 | }, | |
| 25 | 26 | }; | |
| 26 | 27 | } | |
| 28 | + | ||
| 29 | + public void Dispose() | ||
| 30 | + { | ||
| 31 | + foreach (var provider in PythonBaseTypeProviders.OfType<IDisposable>()) | ||
| 32 | + { | ||
| 33 | + provider.Dispose(); | ||
| 34 | + } | ||
| 35 | + PythonBaseTypeProviders.Clear(); | ||
| 36 | + } | ||
| 27 | 37 | } | |
| 28 | 38 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | 5 | namespace Python.Runtime.Mixins | |
| 6 | 6 | { | |
| 7 | - class CollectionMixinsProvider : IPythonBaseTypeProvider | ||
| 7 | + class CollectionMixinsProvider : IPythonBaseTypeProvider, IDisposable | ||
| 8 | 8 | { | |
| 9 | 9 | readonly Lazy<PyObject> mixinsModule; | |
| 10 | 10 | public CollectionMixinsProvider(Lazy<PyObject> mixinsModule) | |
@@ -86,5 +86,13 @@ static Type[] NewInterfaces(Type type) | |||
| 86 | 86 | ||
| 87 | 87 | static Type GetDefinition(Type type) | |
| 88 | 88 | => type.IsGenericType ? type.GetGenericTypeDefinition() : type; | |
| 89 | + | ||
| 90 | + public void Dispose() | ||
| 91 | + { | ||
| 92 | + if (this.mixinsModule.IsValueCreated) | ||
| 93 | + { | ||
| 94 | + this.mixinsModule.Value.Dispose(); | ||
| 95 | + } | ||
| 96 | + } | ||
| 89 | 97 | } | |
| 90 | 98 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -166,8 +166,8 @@ internal static void Reset() | |||
| 166 | 166 | { | |
| 167 | 167 | clrToPython.Clear(); | |
| 168 | 168 | pythonToClr.Clear(); | |
| 169 | - encoders.Clear(); | ||
| 170 | - decoders.Clear(); | ||
| 169 | + encoders.Dispose(); | ||
| 170 | + decoders.Dispose(); | ||
| 171 | 171 | } | |
| 172 | 172 | } | |
| 173 | 173 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -381,7 +381,6 @@ public static void Shutdown(ShutdownMode mode) | |||
| 381 | 381 | ExecuteShutdownHandlers(); | |
| 382 | 382 | // Remember to shut down the runtime. | |
| 383 | 383 | Runtime.Shutdown(mode); | |
| 384 | - PyObjectConversions.Reset(); | ||
| 385 | 384 | ||
| 386 | 385 | initialized = false; | |
| 387 | 386 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -373,6 +373,11 @@ internal static void Shutdown(ShutdownMode mode) | |||
| 373 | 373 | PyCLRMetaType = IntPtr.Zero; | |
| 374 | 374 | ||
| 375 | 375 | Exceptions.Shutdown(); | |
| 376 | + PythonEngine.InteropConfiguration.Dispose(); | ||
| 377 | + DisposeLazyModule(clrInterop); | ||
| 378 | + DisposeLazyModule(inspect); | ||
| 379 | + PyObjectConversions.Reset(); | ||
| 380 | + | ||
| 376 | 381 | Finalizer.Shutdown(); | |
| 377 | 382 | InternString.Shutdown(); | |
| 378 | 383 | ||
@@ -424,6 +429,14 @@ internal static void Shutdown() | |||
| 424 | 429 | Shutdown(mode); | |
| 425 | 430 | } | |
| 426 | 431 | ||
| 432 | + static void DisposeLazyModule(Lazy<PyObject> module) | ||
| 433 | + { | ||
| 434 | + if (module.IsValueCreated) | ||
| 435 | + { | ||
| 436 | + module.Value.Dispose(); | ||
| 437 | + } | ||
| 438 | + } | ||
| 439 | + | ||
| 427 | 440 | private static Lazy<PyObject> GetModuleLazy(string moduleName) | |
| 428 | 441 | => moduleName is null | |
| 429 | 442 | ? throw new ArgumentNullException(nameof(moduleName)) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments