[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/attackgithub/pythonnet/master/src/runtime/pylong.cs [Back]  [Original]

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)
        {
            if (!IsLongType(o))
            {
                throw new ArgumentException("object is not a long");
            }
            Runtime.XIncref(o.obj);
            obj = o.obj;
        }


        /// 
        /// PyLong Constructor
        /// 
        /// 
        /// Creates a new PyLong from an int32 value.
        /// 
        public PyLong(int value)
        {
            obj = Runtime.PyLong_FromLong(value);
            Runtime.CheckExceptionOccurred();
        }


        /// 
        /// PyLong Constructor
        /// 
        /// 
        /// Creates a new PyLong from a uint32 value.
        /// 
        [CLSCompliant(false)]
        public PyLong(uint value)
        {
            obj = Runtime.PyLong_FromLong(value);
            Runtime.CheckExceptionOccurred();
        }


        /// 
        /// PyLong Constructor
        /// 
        /// 
        /// Creates a new PyLong from an int64 value.
        /// 
        public PyLong(long value)
        {
            obj = Runtime.PyLong_FromLongLong(value);
            Runtime.CheckExceptionOccurred();
        }


        /// 
        /// PyLong Constructor
        /// 
        /// 
        /// Creates a new PyLong from a uint64 value.
        /// 
        [CLSCompliant(false)]
        public PyLong(ulong value)
        {
            obj = Runtime.PyLong_FromUnsignedLongLong(value);
            Runtime.CheckExceptionOccurred();
        }


        /// 
        /// PyLong Constructor
        /// 
        /// 
        /// Creates a new PyLong from an int16 value.
        /// 
        public PyLong(short value)
        {
            obj = Runtime.PyLong_FromLong(value);
            Runtime.CheckExceptionOccurred();
        }


        /// 
        /// PyLong Constructor
        /// 
        /// 
        /// Creates a new PyLong from an uint16 value.
        /// 
        [CLSCompliant(false)]
        public PyLong(ushort value)
        {
            obj = Runtime.PyLong_FromLong(value);
            Runtime.CheckExceptionOccurred();
        }


        /// 
        /// PyLong Constructor
        /// 
        /// 
        /// Creates a new PyLong from a byte value.
        /// 
        public PyLong(byte value)
        {
            obj = Runtime.PyLong_FromLong(value);
            Runtime.CheckExceptionOccurred();
        }


        /// 
        /// PyLong Constructor
        /// 
        /// 
        /// Creates a new PyLong from an sbyte value.
        /// 
        [CLSCompliant(false)]
        public PyLong(sbyte value)
        {
            obj = Runtime.PyLong_FromLong(value);
            Runtime.CheckExceptionOccurred();
        }


        /// 
        /// PyLong Constructor
        /// 
        /// 
        /// Creates a new PyLong from an double value.
        /// 
        public PyLong(double value)
        {
            obj = Runtime.PyLong_FromDouble(value);
            Runtime.CheckExceptionOccurred();
        }


        /// 
        /// PyLong Constructor
        /// 
        /// 
        /// Creates a new PyLong from a string value.
        /// 
        public PyLong(string value)
        {
            obj = Runtime.PyLong_FromString(value, IntPtr.Zero, 0);
            Runtime.CheckExceptionOccurred();
        }


        /// 
        /// 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);
            Runtime.CheckExceptionOccurred();
            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);
        }
    }
}

Web Proxy Viewer  |  New URL  |  Original Page