1 /**
2   Mirror _longobject.h
3 
4   Long (arbitrary precision) integer object interface
5   */
6 module deimos.python.longobject;
7 
8 import deimos.python.pyport;
9 import deimos.python.object;
10 import deimos.python.unicodeobject;
11 import deimos.python.longintrepr;
12 
13 extern(C):
14 // Python-header-file: Include/longobject.h:
15 
16 /// _
17 mixin(PyAPI_DATA!"PyTypeObject PyLong_Type");
18 
19 // D translation of C macro:
20 /// _
21 int PyLong_Check()(PyObject* op) {
22     version(Python_2_6_Or_Later){
23         return PyType_FastSubclass((op).ob_type, Py_TPFLAGS_LONG_SUBCLASS);
24     }else{
25         return PyObject_TypeCheck(op, &PyLong_Type);
26     }
27 }
28 // D translation of C macro:
29 /// _
30 int PyLong_CheckExact()(PyObject* op) {
31     return Py_TYPE(op) == &PyLong_Type;
32 }
33 
34 /// _
35 PyObject* PyLong_FromLong(C_long);
36 /// _
37 PyObject* PyLong_FromUnsignedLong(C_ulong);
38 
39 /// _
40 PyObject* PyLong_FromLongLong(C_longlong);
41 /// _
42 PyObject* PyLong_FromUnsignedLongLong(C_ulonglong);
43 
44 /// _
45 PyObject* PyLong_FromDouble(double);
46 version(Python_2_6_Or_Later){
47     /// Availability: >= 2.6
48     PyObject* PyLong_FromSize_t(size_t);
49     /// Availability: >= 2.6
50     PyObject* PyLong_FromSsize_t(Py_ssize_t);
51 }
52 /// _
53 PyObject* PyLong_FromVoidPtr(void*);
54 
55 /// _
56 C_long PyLong_AsLong(PyObject*);
57 /// _
58 C_ulong PyLong_AsUnsignedLong(PyObject*);
59 /// _
60 C_ulong PyLong_AsUnsignedLongMask(PyObject*);
61 version(Python_2_6_Or_Later){
62     /// Availability: >= 2.6
63     Py_ssize_t PyLong_AsSsize_t(PyObject*);
64 }
65 version(Python_2_7_Or_Later) {
66     /// Availability: >= 2.7
67     PyObject* PyLong_GetInfo();
68 
69     /** _PyLong_Frexp returns a double x and an exponent e such that the
70       true value is approximately equal to x * 2**e.  e is >= 0.  x is
71       0.0 if and only if the input is 0 (in which case, e and x are both
72       zeroes); otherwise, 0.5 <= abs(x) < 1.0.  On overflow, which is
73       possible if the number of bits doesn't fit into a Py_ssize_t, sets
74       OverflowError and returns -1.0 for x, 0 for e. */
75     /// Availability: >= 2.7
76     double _PyLong_Frexp(PyLongObject* a, Py_ssize_t* e);
77 }
78 version(Python_3_0_Or_Later) {
79     /// Availability: 3.*
80     size_t PyLong_AsSize_t(PyObject*);
81 }
82 
83 /// _
84 C_longlong PyLong_AsLongLong(PyObject*);
85 /// _
86 C_ulonglong PyLong_AsUnsignedLongLong(PyObject*);
87 /// _
88 C_ulonglong PyLong_AsUnsignedLongLongMask(PyObject*);
89 version(Python_2_7_Or_Later) {
90     /// Availability: >= 2.7
91     C_long PyLong_AsLongAndOverflow(PyObject*, int*);
92     /// Availability: >= 2.7
93     C_longlong PyLong_AsLongLongAndOverflow(PyObject*, int*);
94 }
95 
96 /// _
97 double PyLong_AsDouble(PyObject*);
98 /// _
99 PyObject*  PyLong_FromVoidPtr(void*);
100 /// _
101 void * PyLong_AsVoidPtr(PyObject*);
102 
103 /**
104 Convert string to python long. Roughly, parses format
105 
106 space* sign? space* Integer ('l'|'L')? Null
107 
108 Integer:
109         '0' ('x'|'X') HexDigits
110         '0' OctalDigits
111         DecimalDigits
112 
113 Params:
114 str = null-terminated string to convert.
115 pend = if not null, return pointer to the terminating null character.
116 base = base in which string integer is encoded. possible values are 8,
117         10, 16, or 0 to autodetect base.
118 */
119 PyObject* PyLong_FromString(char* str, char** pend, int base);
120 /// _
121 PyObject* PyLong_FromUnicode(Py_UNICODE*, int, int);
122 /** Return 0 if v is 0, -1 if v < 0, +1 if v > 0.
123    v must not be NULL, and must be a normalized long.
124    There are no error cases.
125 */
126 int _PyLong_Sign(PyObject* v);
127 /** Return the number of bits needed to represent the
128    absolute value of a long.  For example, this returns 1 for 1 and -1, 2
129    for 2 and -2, and 2 for 3 and -3.  It returns 0 for 0.
130    v must not be NULL, and must be a normalized long.
131    (size_t)-1 is returned and OverflowError set if the true result doesn't
132    fit in a size_t.
133 */
134 size_t _PyLong_NumBits(PyObject* v);
135 version(Python_3_0_Or_Later) {
136     /// Availability: 3.*
137     PyObject* _PyLong_DivmodNear(PyObject*, PyObject*);
138 }
139 /** View the n unsigned bytes as a binary integer in
140    base 256, and return a Python long with the same numeric value.
141    If n is 0, the integer is 0.  Else:
142    If little_endian is 1/true, bytes[n-1] is the MSB and bytes[0] the LSB;
143    else (little_endian is 0/false) bytes[0] is the MSB and bytes[n-1] the
144    LSB.
145    If is_signed is 0/false, view the bytes as a non-negative integer.
146    If is_signed is 1/true, view the bytes as a 2's-complement integer,
147    non-negative if bit 0x80 of the MSB is clear, negative if set.
148    Error returns:
149    + Return NULL with the appropriate exception set if there's not
150      enough memory to create the Python long.
151 */
152 PyObject* _PyLong_FromByteArray(
153         const(ubyte)* bytes, size_t n,
154         int little_endian, int is_signed);
155 /** Convert the least-significant 8*n bits of long
156    v to a base-256 integer, stored in array bytes.  Normally return 0,
157    return -1 on error.
158    If little_endian is 1/true, store the MSB at bytes[n-1] and the LSB at
159    bytes[0]; else (little_endian is 0/false) store the MSB at bytes[0] and
160    the LSB at bytes[n-1].
161    If is_signed is 0/false, it's an error if v < 0; else (v >= 0) n bytes
162    are filled and there's nothing special about bit 0x80 of the MSB.
163    If is_signed is 1/true, bytes is filled with the 2's-complement
164    representation of v's value.  Bit 0x80 of the MSB is the sign bit.
165    Error returns (-1):
166    + is_signed is 0 and v < 0.  TypeError is set in this case, and bytes
167      isn't altered.
168    + n isn't big enough to hold the full mathematical value of v.  For
169      example, if is_signed is 0 and there are more digits in the v than
170      fit in n; or if is_signed is 1, v < 0, and n is just 1 bit shy of
171      being large enough to hold a sign bit.  OverflowError is set in this
172      case, but bytes holds the least-signficant n bytes of the true value.
173 */
174 int _PyLong_AsByteArray(PyLongObject* v,
175         ubyte* bytes, size_t n,
176         int little_endian, int is_signed);
177 
178 version(Python_3_0_Or_Later) {
179     /// Availability: 3.*
180     PyObject* _PyLong_Format(PyObject* aa, int base);
181     /// Availability: 3.*
182     PyObject* _PyLong_FormatAdvanced(PyObject* obj,
183             Py_UNICODE* format_spec,
184             Py_ssize_t format_spec_len);
185     /// Availability: 3.*
186     C_ulong PyOS_strtoul(char*, char**, int);
187     /// Availability: 3.*
188     C_long PyOS_strtol(char*, char**, int);
189 }else version(Python_2_6_Or_Later) {
190     /** _PyLong_Format: Convert the long to a string object with given base,
191        appending a base prefix of 0[box] if base is 2, 8 or 16.
192        Add a trailing "L" if addL is non-zero.
193        If newstyle is zero, then use the pre-2.6 behavior of octal having
194        a leading "0", instead of the prefix "0o" */
195     PyObject* _PyLong_Format(PyObject* aa, int base, int addL, int newstyle);
196     /** Format the object based on the format_spec, as defined in PEP 3101
197        (Advanced String Formatting). */
198     PyObject* _PyLong_FormatAdvanced(PyObject* obj,
199             char *format_spec,
200             Py_ssize_t format_spec_len);
201 }
202 
203 
204