PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/pythonnet/src/runtime/pylong.cs

https://github.com/bcline3078/PySharp
C# | 291 lines | 99 code | 54 blank | 138 comment | 23 complexity | c641bdf0bcdee5f72a9518cc3d4f0ba0 MD5 | raw file
  1. // ==========================================================================
  2. // This software is subject to the provisions of the Zope Public License,
  3. // Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
  4. // THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  5. // WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  6. // WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  7. // FOR A PARTICULAR PURPOSE.
  8. // ==========================================================================
  9. using System;
  10. namespace Python.Runtime {
  11. /// <summary>
  12. /// Represents a Python long int object. See the documentation at
  13. /// http://www.python.org/doc/current/api/longObjects.html
  14. /// </summary>
  15. public class PyLong : PyNumber {
  16. /// <summary>
  17. /// PyLong Constructor
  18. /// </summary>
  19. ///
  20. /// <remarks>
  21. /// Creates a new PyLong from an existing object reference. Note
  22. /// that the instance assumes ownership of the object reference.
  23. /// The object reference is not checked for type-correctness.
  24. /// </remarks>
  25. public PyLong(IntPtr ptr) : base(ptr) {}
  26. /// <summary>
  27. /// PyLong Constructor
  28. /// </summary>
  29. ///
  30. /// <remarks>
  31. /// Copy constructor - obtain a PyLong from a generic PyObject. An
  32. /// ArgumentException will be thrown if the given object is not a
  33. /// Python long object.
  34. /// </remarks>
  35. public PyLong(PyObject o) : base() {
  36. if (!IsLongType(o)) {
  37. throw new ArgumentException("object is not a long");
  38. }
  39. Runtime.Incref(o.obj);
  40. obj = o.obj;
  41. }
  42. /// <summary>
  43. /// PyLong Constructor
  44. /// </summary>
  45. ///
  46. /// <remarks>
  47. /// Creates a new PyLong from an int32 value.
  48. /// </remarks>
  49. public PyLong(int value) : base() {
  50. obj = Runtime.PyLong_FromLong((long)value);
  51. if (obj == IntPtr.Zero) {
  52. throw new PythonException();
  53. }
  54. }
  55. /// <summary>
  56. /// PyLong Constructor
  57. /// </summary>
  58. ///
  59. /// <remarks>
  60. /// Creates a new PyLong from a uint32 value.
  61. /// </remarks>
  62. [CLSCompliant(false)]
  63. public PyLong(uint value) : base() {
  64. obj = Runtime.PyLong_FromLong((long)value);
  65. if (obj == IntPtr.Zero) {
  66. throw new PythonException();
  67. }
  68. }
  69. /// <summary>
  70. /// PyLong Constructor
  71. /// </summary>
  72. ///
  73. /// <remarks>
  74. /// Creates a new PyLong from an int64 value.
  75. /// </remarks>
  76. public PyLong(long value) : base() {
  77. obj = Runtime.PyLong_FromLongLong(value);
  78. if (obj == IntPtr.Zero) {
  79. throw new PythonException();
  80. }
  81. }
  82. /// <summary>
  83. /// PyLong Constructor
  84. /// </summary>
  85. ///
  86. /// <remarks>
  87. /// Creates a new PyLong from a uint64 value.
  88. /// </remarks>
  89. [CLSCompliant(false)]
  90. public PyLong(ulong value) : base() {
  91. obj = Runtime.PyLong_FromUnsignedLongLong(value);
  92. if (obj == IntPtr.Zero) {
  93. throw new PythonException();
  94. }
  95. }
  96. /// <summary>
  97. /// PyLong Constructor
  98. /// </summary>
  99. ///
  100. /// <remarks>
  101. /// Creates a new PyLong from an int16 value.
  102. /// </remarks>
  103. public PyLong(short value) : base() {
  104. obj = Runtime.PyLong_FromLong((long)value);
  105. if (obj == IntPtr.Zero) {
  106. throw new PythonException();
  107. }
  108. }
  109. /// <summary>
  110. /// PyLong Constructor
  111. /// </summary>
  112. ///
  113. /// <remarks>
  114. /// Creates a new PyLong from an uint16 value.
  115. /// </remarks>
  116. [CLSCompliant(false)]
  117. public PyLong(ushort value) : base() {
  118. obj = Runtime.PyLong_FromLong((long)value);
  119. if (obj == IntPtr.Zero) {
  120. throw new PythonException();
  121. }
  122. }
  123. /// <summary>
  124. /// PyLong Constructor
  125. /// </summary>
  126. ///
  127. /// <remarks>
  128. /// Creates a new PyLong from a byte value.
  129. /// </remarks>
  130. public PyLong(byte value) : base() {
  131. obj = Runtime.PyLong_FromLong((long)value);
  132. if (obj == IntPtr.Zero) {
  133. throw new PythonException();
  134. }
  135. }
  136. /// <summary>
  137. /// PyLong Constructor
  138. /// </summary>
  139. ///
  140. /// <remarks>
  141. /// Creates a new PyLong from an sbyte value.
  142. /// </remarks>
  143. [CLSCompliant(false)]
  144. public PyLong(sbyte value) : base() {
  145. obj = Runtime.PyLong_FromLong((long)value);
  146. if (obj == IntPtr.Zero) {
  147. throw new PythonException();
  148. }
  149. }
  150. /// <summary>
  151. /// PyLong Constructor
  152. /// </summary>
  153. ///
  154. /// <remarks>
  155. /// Creates a new PyLong from an double value.
  156. /// </remarks>
  157. public PyLong(double value) : base() {
  158. obj = Runtime.PyLong_FromDouble(value);
  159. if (obj == IntPtr.Zero) {
  160. throw new PythonException();
  161. }
  162. }
  163. /// <summary>
  164. /// PyLong Constructor
  165. /// </summary>
  166. ///
  167. /// <remarks>
  168. /// Creates a new PyLong from a string value.
  169. /// </remarks>
  170. public PyLong(string value) : base() {
  171. obj = Runtime.PyLong_FromString(value, IntPtr.Zero, 0);
  172. if (obj == IntPtr.Zero) {
  173. throw new PythonException();
  174. }
  175. }
  176. /// <summary>
  177. /// IsLongType Method
  178. /// </summary>
  179. ///
  180. /// <remarks>
  181. /// Returns true if the given object is a Python long.
  182. /// </remarks>
  183. public static bool IsLongType(PyObject value) {
  184. return Runtime.PyLong_Check(value.obj);
  185. }
  186. /// <summary>
  187. /// AsLong Method
  188. /// </summary>
  189. ///
  190. /// <remarks>
  191. /// <remarks>
  192. /// Convert a Python object to a Python long if possible, raising
  193. /// a PythonException if the conversion is not possible. This is
  194. /// equivalent to the Python expression "long(object)".
  195. /// </remarks>
  196. public static PyLong AsLong(PyObject value) {
  197. IntPtr op = Runtime.PyNumber_Long(value.obj);
  198. if (op == IntPtr.Zero) {
  199. throw new PythonException();
  200. }
  201. return new PyLong(op);
  202. }
  203. /// <summary>
  204. /// ToInt16 Method
  205. /// </summary>
  206. ///
  207. /// <remarks>
  208. /// Return the value of the Python long object as an int16.
  209. /// </remarks>
  210. public short ToInt16()
  211. {
  212. return System.Convert.ToInt16(this.ToInt64());
  213. }
  214. /// <summary>
  215. /// ToInt32 Method
  216. /// </summary>
  217. ///
  218. /// <remarks>
  219. /// Return the value of the Python long object as an int32.
  220. /// </remarks>
  221. public int ToInt32()
  222. {
  223. return System.Convert.ToInt32(this.ToInt64());
  224. }
  225. /// <summary>
  226. /// ToInt64 Method
  227. /// </summary>
  228. ///
  229. /// <remarks>
  230. /// Return the value of the Python long object as an int64.
  231. /// </remarks>
  232. public long ToInt64()
  233. {
  234. return Runtime.PyLong_AsLongLong(obj);
  235. }
  236. }
  237. }