getting rid of a few minor warnings and compile errors

This commit is contained in:
Victor Nova 2021-10-18 20:10:53 -07:00
Родитель cf606a2f08
Коммит bb84c4852e
10 изменённых файлов: 53 добавлений и 54 удалений

Просмотреть файл

@ -10,7 +10,7 @@ namespace Python.Runtime.Codecs
public bool CanDecode(PyType objectType, Type targetType)
{
return targetType.IsEnum
&& objectType.IsSubclass(new BorrowedReference(Runtime.PyLongType));
&& objectType.IsSubclass(Runtime.PyLongType);
}
public bool CanEncode(Type type)
@ -18,7 +18,7 @@ namespace Python.Runtime.Codecs
return type == typeof(object) || type == typeof(ValueType) || type.IsEnum;
}
public bool TryDecode<T>(PyObject pyObj, out T value)
public bool TryDecode<T>(PyObject pyObj, out T? value)
{
value = default;
if (!typeof(T).IsEnum) return false;
@ -27,7 +27,7 @@ namespace Python.Runtime.Codecs
if (!PyInt.IsIntType(pyObj)) return false;
object result;
object? result;
try
{
result = pyObj.AsManagedObject(etype);
@ -46,7 +46,7 @@ namespace Python.Runtime.Codecs
return false;
}
public PyObject TryEncode(object value)
public PyObject? TryEncode(object value)
{
if (value is null) return null;

Просмотреть файл

@ -21,11 +21,11 @@ namespace Python.Runtime
static BorrowedReference GetBaseType(Type type)
{
if (type == typeof(Exception))
return new BorrowedReference(Exceptions.Exception);
return Exceptions.Exception;
return type.BaseType is not null
? ClassManager.GetClass(type.BaseType).ObjectReference
: new BorrowedReference(Runtime.PyBaseObjectType);
: Runtime.PyBaseObjectType;
}
DefaultBaseTypeProvider(){}

Просмотреть файл

@ -70,7 +70,7 @@ namespace Python.Runtime.Mixins
if (type.IsInterface && type.BaseType is null)
{
newBases.RemoveAll(@base => @base.Handle == Runtime.PyBaseObjectType);
newBases.RemoveAll(@base => PythonReferenceComparer.Instance.Equals(@base, Runtime.PyBaseObjectType));
}
return newBases;

Просмотреть файл

@ -9,12 +9,12 @@ namespace Python.Runtime
/// </summary>
[Pure]
public static bool IsNone(this in NewReference reference)
=> reference.DangerousGetAddress() == Runtime.PyNone;
=> reference.BorrowNullable() == Runtime.PyNone;
/// <summary>
/// Checks if the reference points to Python object <c>None</c>.
/// </summary>
[Pure]
public static bool IsNone(this BorrowedReference reference)
=> reference.DangerousGetAddress() == Runtime.PyNone;
=> reference == Runtime.PyNone;
}
}

Просмотреть файл

@ -349,7 +349,7 @@ namespace Python.Runtime
Type[] parameterTypes = (from param in parameters select param.ParameterType).ToArray();
// If the method isn't abstract create a method for calling the original method
string baseMethodName = null;
string? baseMethodName = null;
if (!method.IsAbstract)
{
baseMethodName = "_" + baseType.Name + "__" + method.Name;
@ -678,7 +678,7 @@ namespace Python.Runtime
PyObject py_result = method.Invoke(pyargs);
disposeList.Add(py_result);
return (T)py_result.AsManagedObject(typeof(T));
return py_result.As<T>();
}
}
}
@ -781,7 +781,7 @@ namespace Python.Runtime
using var pyself = new PyObject(self.ObjectReference);
using (PyObject pyvalue = pyself.GetAttr(propertyName))
{
return (T)pyvalue.AsManagedObject(typeof(T));
return pyvalue.As<T>();
}
}
finally

Просмотреть файл

@ -53,7 +53,8 @@ namespace Python.Runtime
{
if (!_containingType.Valid)
{
return Exceptions.RaiseTypeError(_containingType.DeletedMessage);
Exceptions.RaiseTypeError(_containingType.DeletedMessage);
return null;
}
object result;
Type tp = _containingType.Value;
@ -83,7 +84,7 @@ namespace Python.Runtime
return result;
}
Binding binding = Bind(inst, args, kw, info);
Binding? binding = Bind(inst, args, kw, info);
if (binding == null)
{
@ -94,9 +95,8 @@ namespace Python.Runtime
// if there is a default constructor and, if so, assume that
// any extra args are intended for the subclass' __init__.
IntPtr eargs = Runtime.PyTuple_New(0);
binding = Bind(inst, eargs, IntPtr.Zero);
Runtime.XDecref(eargs);
using var eargs = Runtime.PyTuple_New(0);
binding = Bind(inst, eargs.BorrowOrThrow(), kw: null);
if (binding == null)
{

Просмотреть файл

@ -1,8 +1,5 @@
using System.Diagnostics;
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace Python.Runtime
{
@ -13,7 +10,6 @@ namespace Python.Runtime
{
public unsafe static int Initialize(IntPtr data, int size)
{
IntPtr gs = IntPtr.Zero;
try
{
var dllPath = Encoding.UTF8.GetString((byte*)data.ToPointer(), size);
@ -27,11 +23,18 @@ namespace Python.Runtime
PythonDLL = null;
}
gs = PyGILState_Ensure();
var gs = PyGILState_Ensure();
// Console.WriteLine("Startup thread");
PythonEngine.InitExt();
// Console.WriteLine("Startup finished");
try
{
// Console.WriteLine("Startup thread");
PythonEngine.InitExt();
// Console.WriteLine("Startup finished");
}
finally
{
PyGILState_Release(gs);
}
}
catch (Exception exc)
{
@ -40,27 +43,27 @@ namespace Python.Runtime
);
return 1;
}
finally
{
if (gs != IntPtr.Zero)
{
PyGILState_Release(gs);
}
}
return 0;
}
public unsafe static int Shutdown(IntPtr data, int size)
{
IntPtr gs = IntPtr.Zero;
try
{
var command = Encoding.UTF8.GetString((byte*)data.ToPointer(), size);
if (command == "full_shutdown")
{
gs = PyGILState_Ensure();
PythonEngine.Shutdown();
var gs = PyGILState_Ensure();
try
{
PythonEngine.Shutdown();
}
finally
{
PyGILState_Release(gs);
}
}
}
catch (Exception exc)
@ -70,13 +73,7 @@ namespace Python.Runtime
);
return 1;
}
finally
{
if (gs != IntPtr.Zero)
{
PyGILState_Release(gs);
}
}
return 0;
}
}

Просмотреть файл

@ -174,7 +174,7 @@ namespace Python.Runtime
return pyErr;
}
if (PyObjectConversions.TryDecode(valRef, typeRef, typeof(Exception), out object decoded)
if (PyObjectConversions.TryDecode(valRef, typeRef, typeof(Exception), out object? decoded)
&& decoded is Exception decodedException)
{
return decodedException;
@ -199,7 +199,7 @@ namespace Python.Runtime
using var pyErrType = Runtime.InteropModule.GetAttr("PyErr");
using var pyErrInfo = pyErrType.Invoke(new PyTuple(), errorDict);
if (PyObjectConversions.TryDecode(pyErrInfo.Reference, pyErrType.Reference,
typeof(Exception), out object decoded) && decoded is Exception decodedPyErrInfo)
typeof(Exception), out object? decoded) && decoded is Exception decodedPyErrInfo)
{
return decodedPyErrInfo;
}
@ -394,11 +394,11 @@ namespace Python.Runtime
=> new PythonException(type: Type, value: Value, traceback: Traceback,
Message, InnerException);
internal bool Is(IntPtr type)
internal bool Is(BorrowedReference type)
{
return Runtime.PyErr_GivenExceptionMatches(
given: (Value ?? Type).Reference,
typeOrTypes: new BorrowedReference(type)) != 0;
typeOrTypes: type) != 0;
}
private static void CheckRuntimeIsRunning()

Просмотреть файл

@ -176,7 +176,7 @@ namespace Python.Runtime
private static void InitPyMembers()
{
using (var builtinsOwned = PyImport_Import(new BorrowedReference(PyIdentifier.builtins)))
using (var builtinsOwned = PyImport_Import(PyIdentifier.builtins))
{
var builtins = builtinsOwned.Borrow();
SetPyMember(out PyNotImplemented, PyObject_GetAttrString(builtins, "NotImplemented").StealNullable());
@ -403,10 +403,11 @@ namespace Python.Runtime
_pyRefs.Add(obj);
}
private static void SetPyMemberTypeOf(out PyObject obj, PyObject value)
private static void SetPyMemberTypeOf(out PyType obj, PyObject value)
{
var type = PyObject_Type(value);
SetPyMember(out obj, type.StealNullable());
obj = new PyType(type.StealOrThrow(), prevalidated: true);
_pyRefs.Add(obj);
}
private static void SetPyMemberTypeOf(out PyObject obj, StolenReference value)
@ -513,8 +514,8 @@ namespace Python.Runtime
internal static PyObject PyDictType;
internal static PyObject PyLongType;
internal static PyObject PyFloatType;
internal static PyObject PyBoolType;
internal static PyObject PyNoneType;
internal static PyType PyBoolType;
internal static PyType PyNoneType;
internal static PyType PyTypeType;
internal static int* Py_NoSiteFlag;

Просмотреть файл

@ -9,7 +9,7 @@ namespace Python.Runtime.Slots
{
internal static class mp_length_slot
{
private static MethodInfo _lengthMethod;
private static MethodInfo? _lengthMethod;
public static MethodInfo Method
{
get
@ -22,7 +22,7 @@ namespace Python.Runtime.Slots
nameof(mp_length_slot.mp_length),
BindingFlags.Static | BindingFlags.NonPublic);
Debug.Assert(_lengthMethod != null);
return _lengthMethod;
return _lengthMethod!;
}
}
@ -47,12 +47,13 @@ namespace Python.Runtime.Slots
/// Implements __len__ for classes that implement ICollection
/// (this includes any IList implementer or Array subclass)
/// </summary>
private static int mp_length(IntPtr ob)
private static nint mp_length(BorrowedReference ob)
{
var co = ManagedType.GetManagedObject(ob) as CLRObject;
if (co == null)
{
Exceptions.RaiseTypeError("invalid object");
return -1;
}
// first look for ICollection implementation directly