Skip to content

Expose PyType.Get #1581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
15 changes: 9 additions & 6 deletions src/runtime/pytype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ public PyType(TypeSpec spec, PyTuple? bases = null) : base(FromSpec(spec, bases)
/// <summary>Wraps an existing type object.</summary>
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))
{
}

Expand Down Expand Up @@ -69,17 +74,15 @@ internal static bool IsType(BorrowedReference value)

/// <summary>
/// Gets <see cref="PyType"/>, which represents the specified CLR type.
/// Must be called after the CLR type was mapped to its Python type.
/// </summary>
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
Expand Down