| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,15 +10,15 @@ public sealed class EnumPyIntCodec : IPyObjectEncoder, IPyObjectDecoder | |||
| 10 | 10 | public bool CanDecode(PyType objectType, Type targetType) | |
| 11 | 11 | { | |
| 12 | 12 | return targetType.IsEnum | |
| 13 | - && objectType.IsSubclass(new BorrowedReference(Runtime.PyLongType)); | ||
| 13 | + && objectType.IsSubclass(Runtime.PyLongType); | ||
| 14 | 14 | } | |
| 15 | 15 | ||
| 16 | 16 | public bool CanEncode(Type type) | |
| 17 | 17 | { | |
| 18 | 18 | return type == typeof(object) || type == typeof(ValueType) || type.IsEnum; | |
| 19 | 19 | } | |
| 20 | 20 | ||
| 21 | - public bool TryDecode<T>(PyObject pyObj, out T value) | ||
| 21 | + public bool TryDecode<T>(PyObject pyObj, out T? value) | ||
| 22 | 22 | { | |
| 23 | 23 | value = default; | |
| 24 | 24 | if (!typeof(T).IsEnum) return false; | |
@@ -27,7 +27,7 @@ public bool TryDecode<T>(PyObject pyObj, out T value) | |||
| 27 | 27 | ||
| 28 | 28 | if (!PyInt.IsIntType(pyObj)) return false; | |
| 29 | 29 | ||
| 30 | - object result; | ||
| 30 | + object? result; | ||
| 31 | 31 | try | |
| 32 | 32 | { | |
| 33 | 33 | result = pyObj.AsManagedObject(etype); | |
@@ -46,7 +46,7 @@ public bool TryDecode<T>(PyObject pyObj, out T value) | |||
| 46 | 46 | return false; | |
| 47 | 47 | } | |
| 48 | 48 | ||
| 49 | - public PyObject TryEncode(object value) | ||
| 49 | + public PyObject? TryEncode(object value) | ||
| 50 | 50 | { | |
| 51 | 51 | if (value is null) return null; | |
| 52 | 52 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,11 +21,11 @@ public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases) | |||
| 21 | 21 | static BorrowedReference GetBaseType(Type type) | |
| 22 | 22 | { | |
| 23 | 23 | if (type == typeof(Exception)) | |
| 24 | - return new BorrowedReference(Exceptions.Exception); | ||
| 24 | + return Exceptions.Exception; | ||
| 25 | 25 | ||
| 26 | 26 | return type.BaseType is not null | |
| 27 | 27 | ? ClassManager.GetClass(type.BaseType).ObjectReference | |
| 28 | - : new BorrowedReference(Runtime.PyBaseObjectType); | ||
| 28 | + : Runtime.PyBaseObjectType; | ||
| 29 | 29 | } | |
| 30 | 30 | ||
| 31 | 31 | DefaultBaseTypeProvider(){} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -70,7 +70,7 @@ public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases) | |||
| 70 | 70 | ||
| 71 | 71 | if (type.IsInterface && type.BaseType is null) | |
| 72 | 72 | { | |
| 73 | - newBases.RemoveAll(@base => @base.Handle == Runtime.PyBaseObjectType); | ||
| 73 | + newBases.RemoveAll(@base => PythonReferenceComparer.Instance.Equals(@base, Runtime.PyBaseObjectType)); | ||
| 74 | 74 | } | |
| 75 | 75 | ||
| 76 | 76 | return newBases; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,12 +9,12 @@ static class ReferenceExtensions | |||
| 9 | 9 | /// </summary> | |
| 10 | 10 | [Pure] | |
| 11 | 11 | public static bool IsNone(this in NewReference reference) | |
| 12 | - => reference.DangerousGetAddress() == Runtime.PyNone; | ||
| 12 | + => reference.BorrowNullable() == Runtime.PyNone; | ||
| 13 | 13 | /// <summary> | |
| 14 | 14 | /// Checks if the reference points to Python object <c>None</c>. | |
| 15 | 15 | /// </summary> | |
| 16 | 16 | [Pure] | |
| 17 | 17 | public static bool IsNone(this BorrowedReference reference) | |
| 18 | - => reference.DangerousGetAddress() == Runtime.PyNone; | ||
| 18 | + => reference == Runtime.PyNone; | ||
| 19 | 19 | } | |
| 20 | 20 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -349,7 +349,7 @@ private static void AddVirtualMethod(MethodInfo method, Type baseType, TypeBuild | |||
| 349 | 349 | Type[] parameterTypes = (from param in parameters select param.ParameterType).ToArray(); | |
| 350 | 350 | ||
| 351 | 351 | // If the method isn't abstract create a method for calling the original method | |
| 352 | - string baseMethodName = null; | ||
| 352 | + string? baseMethodName = null; | ||
| 353 | 353 | if (!method.IsAbstract) | |
| 354 | 354 | { | |
| 355 | 355 | baseMethodName = "_" + baseType.Name + "__" + method.Name; | |
@@ -678,7 +678,7 @@ public static T InvokeMethod<T>(IPythonDerivedType obj, string methodName, strin | |||
| 678 | 678 | ||
| 679 | 679 | PyObject py_result = method.Invoke(pyargs); | |
| 680 | 680 | disposeList.Add(py_result); | |
| 681 | - return (T)py_result.AsManagedObject(typeof(T)); | ||
| 681 | + return py_result.As<T>(); | ||
| 682 | 682 | } | |
| 683 | 683 | } | |
| 684 | 684 | } | |
@@ -781,7 +781,7 @@ public static T InvokeGetProperty<T>(IPythonDerivedType obj, string propertyName | |||
| 781 | 781 | using var pyself = new PyObject(self.ObjectReference); | |
| 782 | 782 | using (PyObject pyvalue = pyself.GetAttr(propertyName)) | |
| 783 | 783 | { | |
| 784 | - return (T)pyvalue.AsManagedObject(typeof(T)); | ||
| 784 | + return pyvalue.As<T>(); | ||
| 785 | 785 | } | |
| 786 | 786 | } | |
| 787 | 787 | finally | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,7 +53,8 @@ internal ConstructorBinder(Type containingType) | |||
| 53 | 53 | { | |
| 54 | 54 | if (!_containingType.Valid) | |
| 55 | 55 | { | |
| 56 | - return Exceptions.RaiseTypeError(_containingType.DeletedMessage); | ||
| 56 | + Exceptions.RaiseTypeError(_containingType.DeletedMessage); | ||
| 57 | + return null; | ||
| 57 | 58 | } | |
| 58 | 59 | object result; | |
| 59 | 60 | Type tp = _containingType.Value; | |
@@ -83,7 +84,7 @@ internal ConstructorBinder(Type containingType) | |||
| 83 | 84 | return result; | |
| 84 | 85 | } | |
| 85 | 86 | ||
| 86 | - Binding binding = Bind(inst, args, kw, info); | ||
| 87 | + Binding? binding = Bind(inst, args, kw, info); | ||
| 87 | 88 | ||
| 88 | 89 | if (binding == null) | |
| 89 | 90 | { | |
@@ -94,9 +95,8 @@ internal ConstructorBinder(Type containingType) | |||
| 94 | 95 | // if there is a default constructor and, if so, assume that | |
| 95 | 96 | // any extra args are intended for the subclass' __init__. | |
| 96 | 97 | ||
| 97 | - IntPtr eargs = Runtime.PyTuple_New(0); | ||
| 98 | - binding = Bind(inst, eargs, IntPtr.Zero); | ||
| 99 | - Runtime.XDecref(eargs); | ||
| 98 | + using var eargs = Runtime.PyTuple_New(0); | ||
| 99 | + binding = Bind(inst, eargs.BorrowOrThrow(), kw: null); | ||
| 100 | 100 | ||
| 101 | 101 | if (binding == null) | |
| 102 | 102 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,8 +1,5 @@ | |||
| 1 | - using System.Diagnostics; | ||
| 2 | 1 | using System; | |
| 3 | - using System.Runtime.InteropServices; | ||
| 4 | 2 | using System.Text; | |
| 5 | - using System.Threading; | ||
| 6 | 3 | ||
| 7 | 4 | namespace Python.Runtime | |
| 8 | 5 | { | |
@@ -13,7 +10,6 @@ static class Loader | |||
| 13 | 10 | { | |
| 14 | 11 | public unsafe static int Initialize(IntPtr data, int size) | |
| 15 | 12 | { | |
| 16 | - IntPtr gs = IntPtr.Zero; | ||
| 17 | 13 | try | |
| 18 | 14 | { | |
| 19 | 15 | var dllPath = Encoding.UTF8.GetString((byte*)data.ToPointer(), size); | |
@@ -27,11 +23,18 @@ public unsafe static int Initialize(IntPtr data, int size) | |||
| 27 | 23 | PythonDLL = null; | |
| 28 | 24 | } | |
| 29 | 25 | ||
| 30 | - gs = PyGILState_Ensure(); | ||
| 26 | + var gs = PyGILState_Ensure(); | ||
| 31 | 27 | ||
| 32 | - // Console.WriteLine("Startup thread"); | ||
| 33 | - PythonEngine.InitExt(); | ||
| 34 | - // Console.WriteLine("Startup finished"); | ||
| 28 | + try | ||
| 29 | + { | ||
| 30 | + // Console.WriteLine("Startup thread"); | ||
| 31 | + PythonEngine.InitExt(); | ||
| 32 | + // Console.WriteLine("Startup finished"); | ||
| 33 | + } | ||
| 34 | + finally | ||
| 35 | + { | ||
| 36 | + PyGILState_Release(gs); | ||
| 37 | + } | ||
| 35 | 38 | } | |
| 36 | 39 | catch (Exception exc) | |
| 37 | 40 | { | |
@@ -40,27 +43,27 @@ public unsafe static int Initialize(IntPtr data, int size) | |||
| 40 | 43 | ); | |
| 41 | 44 | return 1; | |
| 42 | 45 | } | |
| 43 | - finally | ||
| 44 | - { | ||
| 45 | - if (gs != IntPtr.Zero) | ||
| 46 | - { | ||
| 47 | - PyGILState_Release(gs); | ||
| 48 | - } | ||
| 49 | - } | ||
| 46 | + | ||
| 50 | 47 | return 0; | |
| 51 | 48 | } | |
| 52 | 49 | ||
| 53 | 50 | public unsafe static int Shutdown(IntPtr data, int size) | |
| 54 | 51 | { | |
| 55 | - IntPtr gs = IntPtr.Zero; | ||
| 56 | 52 | try | |
| 57 | 53 | { | |
| 58 | 54 | var command = Encoding.UTF8.GetString((byte*)data.ToPointer(), size); | |
| 59 | 55 | ||
| 60 | 56 | if (command == "full_shutdown") | |
| 61 | 57 | { | |
| 62 | - gs = PyGILState_Ensure(); | ||
| 63 | - PythonEngine.Shutdown(); | ||
| 58 | + var gs = PyGILState_Ensure(); | ||
| 59 | + try | ||
| 60 | + { | ||
| 61 | + PythonEngine.Shutdown(); | ||
| 62 | + } | ||
| 63 | + finally | ||
| 64 | + { | ||
| 65 | + PyGILState_Release(gs); | ||
| 66 | + } | ||
| 64 | 67 | } | |
| 65 | 68 | } | |
| 66 | 69 | catch (Exception exc) | |
@@ -70,13 +73,7 @@ public unsafe static int Shutdown(IntPtr data, int size) | |||
| 70 | 73 | ); | |
| 71 | 74 | return 1; | |
| 72 | 75 | } | |
| 73 | - finally | ||
| 74 | - { | ||
| 75 | - if (gs != IntPtr.Zero) | ||
| 76 | - { | ||
| 77 | - PyGILState_Release(gs); | ||
| 78 | - } | ||
| 79 | - } | ||
| 76 | + | ||
| 80 | 77 | return 0; | |
| 81 | 78 | } | |
| 82 | 79 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -174,7 +174,7 @@ private static Exception FromPyErr(BorrowedReference typeRef, BorrowedReference | |||
| 174 | 174 | return pyErr; | |
| 175 | 175 | } | |
| 176 | 176 | ||
| 177 | - if (PyObjectConversions.TryDecode(valRef, typeRef, typeof(Exception), out object decoded) | ||
| 177 | + if (PyObjectConversions.TryDecode(valRef, typeRef, typeof(Exception), out object? decoded) | ||
| 178 | 178 | && decoded is Exception decodedException) | |
| 179 | 179 | { | |
| 180 | 180 | return decodedException; | |
@@ -199,7 +199,7 @@ private static Exception FromPyErr(BorrowedReference typeRef, BorrowedReference | |||
| 199 | 199 | using var pyErrType = Runtime.InteropModule.GetAttr("PyErr"); | |
| 200 | 200 | using var pyErrInfo = pyErrType.Invoke(new PyTuple(), errorDict); | |
| 201 | 201 | if (PyObjectConversions.TryDecode(pyErrInfo.Reference, pyErrType.Reference, | |
| 202 | - typeof(Exception), out object decoded) && decoded is Exception decodedPyErrInfo) | ||
| 202 | + typeof(Exception), out object? decoded) && decoded is Exception decodedPyErrInfo) | ||
| 203 | 203 | { | |
| 204 | 204 | return decodedPyErrInfo; | |
| 205 | 205 | } | |
@@ -394,11 +394,11 @@ public PythonException Clone() | |||
| 394 | 394 | => new PythonException(type: Type, value: Value, traceback: Traceback, | |
| 395 | 395 | Message, InnerException); | |
| 396 | 396 | ||
| 397 | - internal bool Is(IntPtr type) | ||
| 397 | + internal bool Is(BorrowedReference type) | ||
| 398 | 398 | { | |
| 399 | 399 | return Runtime.PyErr_GivenExceptionMatches( | |
| 400 | 400 | given: (Value ?? Type).Reference, | |
| 401 | - typeOrTypes: new BorrowedReference(type)) != 0; | ||
| 401 | + typeOrTypes: type) != 0; | ||
| 402 | 402 | } | |
| 403 | 403 | ||
| 404 | 404 | private static void CheckRuntimeIsRunning() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -176,7 +176,7 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd | |||
| 176 | 176 | ||
| 177 | 177 | private static void InitPyMembers() | |
| 178 | 178 | { | |
| 179 | - using (var builtinsOwned = PyImport_Import(new BorrowedReference(PyIdentifier.builtins))) | ||
| 179 | + using (var builtinsOwned = PyImport_Import(PyIdentifier.builtins)) | ||
| 180 | 180 | { | |
| 181 | 181 | var builtins = builtinsOwned.Borrow(); | |
| 182 | 182 | SetPyMember(out PyNotImplemented, PyObject_GetAttrString(builtins, "NotImplemented").StealNullable()); | |
@@ -403,10 +403,11 @@ private static void SetPyMember(out PyObject obj, StolenReference value) | |||
| 403 | 403 | _pyRefs.Add(obj); | |
| 404 | 404 | } | |
| 405 | 405 | ||
| 406 | - private static void SetPyMemberTypeOf(out PyObject obj, PyObject value) | ||
| 406 | + private static void SetPyMemberTypeOf(out PyType obj, PyObject value) | ||
| 407 | 407 | { | |
| 408 | 408 | var type = PyObject_Type(value); | |
| 409 | - SetPyMember(out obj, type.StealNullable()); | ||
| 409 | + obj = new PyType(type.StealOrThrow(), prevalidated: true); | ||
| 410 | + _pyRefs.Add(obj); | ||
| 410 | 411 | } | |
| 411 | 412 | ||
| 412 | 413 | private static void SetPyMemberTypeOf(out PyObject obj, StolenReference value) | |
@@ -513,8 +514,8 @@ private static void MoveClrInstancesOnwershipToPython() | |||
| 513 | 514 | internal static PyObject PyDictType; | |
| 514 | 515 | internal static PyObject PyLongType; | |
| 515 | 516 | internal static PyObject PyFloatType; | |
| 516 | - internal static PyObject PyBoolType; | ||
| 517 | - internal static PyObject PyNoneType; | ||
| 517 | + internal static PyType PyBoolType; | ||
| 518 | + internal static PyType PyNoneType; | ||
| 518 | 519 | internal static PyType PyTypeType; | |
| 519 | 520 | ||
| 520 | 521 | internal static int* Py_NoSiteFlag; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ namespace Python.Runtime.Slots | |||
| 9 | 9 | { | |
| 10 | 10 | internal static class mp_length_slot | |
| 11 | 11 | { | |
| 12 | - private static MethodInfo _lengthMethod; | ||
| 12 | + private static MethodInfo? _lengthMethod; | ||
| 13 | 13 | public static MethodInfo Method | |
| 14 | 14 | { | |
| 15 | 15 | get | |
@@ -22,7 +22,7 @@ public static MethodInfo Method | |||
| 22 | 22 | nameof(mp_length_slot.mp_length), | |
| 23 | 23 | BindingFlags.Static | BindingFlags.NonPublic); | |
| 24 | 24 | Debug.Assert(_lengthMethod != null); | |
| 25 | - return _lengthMethod; | ||
| 25 | + return _lengthMethod!; | ||
| 26 | 26 | } | |
| 27 | 27 | } | |
| 28 | 28 | ||
@@ -47,12 +47,13 @@ public static bool CanAssign(Type clrType) | |||
| 47 | 47 | /// Implements __len__ for classes that implement ICollection | |
| 48 | 48 | /// (this includes any IList implementer or Array subclass) | |
| 49 | 49 | /// </summary> | |
| 50 | - private static int mp_length(IntPtr ob) | ||
| 50 | + private static nint mp_length(BorrowedReference ob) | ||
| 51 | 51 | { | |
| 52 | 52 | var co = ManagedType.GetManagedObject(ob) as CLRObject; | |
| 53 | 53 | if (co == null) | |
| 54 | 54 | { | |
| 55 | 55 | Exceptions.RaiseTypeError("invalid object"); | |
| 56 | + return -1; | ||
| 56 | 57 | } | |
| 57 | 58 | ||
| 58 | 59 | // first look for ICollection implementation directly | |
| Back | FazBrowse Home | New Git URL |
0 commit comments