using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
namespace Python.Runtime
{
///
/// Performs data conversions between managed types and Python types.
///
[SuppressUnmanagedCodeSecurity]
internal class Converter
{
private Converter()
{
}
private static readonly Type objectType;
private static readonly Type stringType;
private static readonly Type singleType;
private static readonly Type doubleType;
private static readonly Type int16Type;
private static readonly Type int32Type;
private static readonly Type int64Type;
private static readonly Type boolType;
private static readonly Type typeType;
static Converter()
{
objectType = typeof(Object);
stringType = typeof(String);
int16Type = typeof(Int16);
int32Type = typeof(Int32);
int64Type = typeof(Int64);
singleType = typeof(Single);
doubleType = typeof(Double);
boolType = typeof(Boolean);
typeType = typeof(Type);
}
///
/// Given a builtin Python type, return the corresponding CLR type.
///
internal static Type? GetTypeByAlias(BorrowedReference op)
{
if (op == Runtime.PyStringType)
return stringType;
if (op == Runtime.PyUnicodeType)
return stringType;
if (op == Runtime.PyLongType)
return int32Type;
if (op == Runtime.PyLongType)
return int64Type;
if (op == Runtime.PyFloatType)
return doubleType;
if (op == Runtime.PyBoolType)
return boolType;
return null;
}
internal static BorrowedReference GetPythonTypeByAlias(Type op)
{
if (op == stringType)
return Runtime.PyUnicodeType.Reference;
if (op == int16Type)
return Runtime.PyLongType.Reference;
if (op == int32Type)
return Runtime.PyLongType.Reference;
if (op == int64Type)
return Runtime.PyLongType.Reference;
if (op == doubleType)
return Runtime.PyFloatType.Reference;
if (op == singleType)
return Runtime.PyFloatType.Reference;
if (op == boolType)
return Runtime.PyBoolType.Reference;
return BorrowedReference.Null;
}
internal static NewReference ToPython(T value)
=> ToPython(value, typeof(T));
private static readonly Func