using System;
namespace Python.Runtime
{
///
/// Represents a Python long int object. See the documentation at
/// PY2: https://docs.python.org/2/c-api/long.html
/// PY3: https://docs.python.org/3/c-api/long.html
/// for details.
///
public class PyLong : PyNumber
{
///
/// PyLong Constructor
///
///
/// Creates a new PyLong from an existing object reference. Note
/// that the instance assumes ownership of the object reference.
/// The object reference is not checked for type-correctness.
///
public PyLong(IntPtr ptr) : base(ptr)
{
}
///
/// PyLong Constructor
///
///
/// Copy constructor - obtain a PyLong from a generic PyObject. An
/// ArgumentException will be thrown if the given object is not a
/// Python long object.
///
public PyLong(PyObject o) : base(FromObject(o))
{
}
private static IntPtr FromObject(PyObject o)
{
if (o == null || !IsLongType(o))
{
throw new ArgumentException("object is not a long");
}
Runtime.XIncref(o.obj);
return o.obj;
}
private static IntPtr FromInt(int value)
{
IntPtr val = Runtime.PyLong_FromLong(value);
PythonException.ThrowIfIsNull(val);
return val;
}
///
/// PyLong Constructor
///
///
/// Creates a new PyLong from an int32 value.
///
public PyLong(int value) : base(FromInt(value))
{
}
///
/// PyLong Constructor
///
///
/// Creates a new PyLong from a uint32 value.
///
[CLSCompliant(false)]
public PyLong(uint value) : base(FromInt((int)value))
{
}
private static IntPtr FromLong(long value)
{
IntPtr val = Runtime.PyLong_FromLongLong(value);
PythonException.ThrowIfIsNull(val);
return val;
}
///
/// PyLong Constructor
///
///
/// Creates a new PyLong from an int64 value.
///
public PyLong(long value) : base(FromLong(value))
{
}
private static IntPtr FromULong(ulong value)
{
IntPtr val = Runtime.PyLong_FromUnsignedLongLong(value);
PythonException.ThrowIfIsNull(val);
return val;
}
///
/// PyLong Constructor
///
///
/// Creates a new PyLong from a uint64 value.
///
[CLSCompliant(false)]
public PyLong(ulong value) : base(FromULong(value))
{
}
///
/// PyLong Constructor
///
///
/// Creates a new PyLong from an int16 value.
///
public PyLong(short value) : base(FromInt((int)value))
{
}
///
/// PyLong Constructor
///
///
/// Creates a new PyLong from an uint16 value.
///
[CLSCompliant(false)]
public PyLong(ushort value) : base(FromInt((int)value))
{
}
///
/// PyLong Constructor
///
///
/// Creates a new PyLong from a byte value.
///
public PyLong(byte value) : base(FromInt((int)value))
{
}
///
/// PyLong Constructor
///
///
/// Creates a new PyLong from an sbyte value.
///
[CLSCompliant(false)]
public PyLong(sbyte value) : base(FromInt((int)value))
{
}
private static IntPtr FromDouble(double value)
{
IntPtr val = Runtime.PyLong_FromDouble(value);
PythonException.ThrowIfIsNull(val);
return val;
}
///
/// PyLong Constructor
///
///
/// Creates a new PyLong from an double value.
///
public PyLong(double value) : base(FromDouble(value))
{
}
private static IntPtr FromString(string value)
{
IntPtr val = Runtime.PyLong_FromString(value, IntPtr.Zero, 0);
PythonException.ThrowIfIsNull(val);
return val;
}
///
/// PyLong Constructor
///
///
/// Creates a new PyLong from a string value.
///
public PyLong(string value) : base(FromString(value))
{
}
///
/// IsLongType Method
///
///
/// Returns true if the given object is a Python long.
///
public static bool IsLongType(PyObject value)
{
return Runtime.PyLong_Check(value.obj);
}
///
/// AsLong Method
///
///
/// Convert a Python object to a Python long if possible, raising
/// a PythonException if the conversion is not possible. This is
/// equivalent to the Python expression "long(object)".
///
public static PyLong AsLong(PyObject value)
{
IntPtr op = Runtime.PyNumber_Long(value.obj);
PythonException.ThrowIfIsNull(op);
return new PyLong(op);
}
///
/// ToInt16 Method
///
///
/// Return the value of the Python long object as an int16.
///
public short ToInt16()
{
return Convert.ToInt16(ToInt64());
}
///
/// ToInt32 Method
///
///
/// Return the value of the Python long object as an int32.
///
public int ToInt32()
{
return Convert.ToInt32(ToInt64());
}
///
/// ToInt64 Method
///
///
/// Return the value of the Python long object as an int64.
///
public long ToInt64()
{
return Runtime.PyLong_AsLongLong(obj);
}
}
}