diff --git a/src/runtime/converter.cs b/src/runtime/converter.cs index 4ef7ca46d..2b79caf39 100644 --- a/src/runtime/converter.cs +++ b/src/runtime/converter.cs @@ -937,7 +937,7 @@ private static bool ToArray(IntPtr value, Type obType, out object? result, bool public static class ConverterExtension { - public static PyObject ToPython(this object o) + public static PyObject ToPython(this object? o) { if (o is null) return Runtime.None; return new PyObject(Converter.ToPython(o, o.GetType())); diff --git a/src/runtime/pytype.cs b/src/runtime/pytype.cs index b144d09c3..9a0b34724 100644 --- a/src/runtime/pytype.cs +++ b/src/runtime/pytype.cs @@ -15,13 +15,18 @@ public PyType(TypeSpec spec, PyTuple? bases = null) : base(FromSpec(spec, bases) /// Wraps an existing type object. public PyType(PyObject o) : base(FromObject(o)) { } + internal PyType(PyType o) + : base(o is not null ? o.Reference : throw new ArgumentNullException(nameof(o))) + { + } + internal PyType(BorrowedReference reference) : base(reference) { if (!Runtime.PyType_Check(this.Handle)) throw new ArgumentException("object is not a type"); } - internal PyType(StolenReference reference) : base(EnsureIsType(in reference)) + internal PyType(in StolenReference reference) : base(EnsureIsType(in reference)) { } @@ -69,17 +74,15 @@ internal static bool IsType(BorrowedReference value) /// /// Gets , which represents the specified CLR type. - /// Must be called after the CLR type was mapped to its Python type. /// - internal static PyType Get(Type clrType) + public static PyType Get(Type clrType) { - if (clrType == null) + if (clrType is null) { throw new ArgumentNullException(nameof(clrType)); } - ClassBase pyClass = ClassManager.GetClass(clrType); - return new PyType(pyClass.ObjectReference); + return new PyType(TypeManager.GetType(clrType)); } internal BorrowedReference BaseReference