// ==========================================================================
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
// ==========================================================================
using System;
namespace Python.Runtime {
///
/// Represents a Python long int object. See the documentation at
/// http://www.python.org/doc/current/api/longObjects.html
///
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() {
if (!IsLongType(o)) {
throw new ArgumentException("object is not a long");
}
Runtime.Incref(o.obj);
obj = o.obj;
}
///
/// PyLong Constructor
///
///
///
/// Creates a new PyLong from an int32 value.
///
public PyLong(int value) : base() {
obj = Runtime.PyLong_FromLong((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
///
/// PyLong Constructor
///
///
///
/// Creates a new PyLong from a uint32 value.
///
[CLSCompliant(false)]
public PyLong(uint value) : base() {
obj = Runtime.PyLong_FromLong((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
///
/// PyLong Constructor
///
///
///
/// Creates a new PyLong from an int64 value.
///
public PyLong(long value) : base() {
obj = Runtime.PyLong_FromLongLong(value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
///
/// PyLong Constructor
///
///
///
/// Creates a new PyLong from a uint64 value.
///
[CLSCompliant(false)]
public PyLong(ulong value) : base() {
obj = Runtime.PyLong_FromUnsignedLongLong(value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
///
/// PyLong Constructor
///
///
///
/// Creates a new PyLong from an int16 value.
///
public PyLong(short value) : base() {
obj = Runtime.PyLong_FromLong((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
///
/// PyLong Constructor
///
///
///
/// Creates a new PyLong from an uint16 value.
///
[CLSCompliant(false)]
public PyLong(ushort value) : base() {
obj = Runtime.PyLong_FromLong((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
///
/// PyLong Constructor
///
///
///
/// Creates a new PyLong from a byte value.
///
public PyLong(byte value) : base() {
obj = Runtime.PyLong_FromLong((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
///
/// PyLong Constructor
///
///
///
/// Creates a new PyLong from an sbyte value.
///
[CLSCompliant(false)]
public PyLong(sbyte value) : base() {
obj = Runtime.PyLong_FromLong((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
///
/// PyLong Constructor
///
///
///
/// Creates a new PyLong from an double value.
///
public PyLong(double value) : base() {
obj = Runtime.PyLong_FromDouble(value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
///
/// PyLong Constructor
///
///
///
/// Creates a new PyLong from a string value.
///
public PyLong(string value) : base() {
obj = Runtime.PyLong_FromString(value, IntPtr.Zero, 0);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
///
/// 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);
if (op == IntPtr.Zero) {
throw new PythonException();
}
return new PyLong(op);
}
///
/// ToInt16 Method
///
///
///
/// Return the value of the Python long object as an int16.
///
public short ToInt16()
{
return System.Convert.ToInt16(this.ToInt64());
}
///
/// ToInt32 Method
///
///
///
/// Return the value of the Python long object as an int32.
///
public int ToInt32()
{
return System.Convert.ToInt32(this.ToInt64());
}
///
/// ToInt64 Method
///
///
///
/// Return the value of the Python long object as an int64.
///
public long ToInt64()
{
return Runtime.PyLong_AsLongLong(obj);
}
}
}