27
|
1 #ifndef Py_LONGOBJECT_H
|
|
2 #define Py_LONGOBJECT_H
|
|
3
|
|
4 /* Long (arbitrary precision) integer object interface */
|
|
5
|
|
6 typedef struct _longobject PyLongObject; /* Revealed in longintrepr.h */
|
|
7
|
|
8 PyAPI_DATA(PyTypeObject) PyLong_Type;
|
|
9
|
|
10 #define PyLong_Check(op) \
|
|
11 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_LONG_SUBCLASS)
|
|
12 #define PyLong_CheckExact(op) (Py_TYPE(op) == &PyLong_Type)
|
|
13
|
|
14 PyAPI_FUNC(PyObject *) PyLong_FromLong(long);
|
|
15 PyAPI_FUNC(PyObject *) PyLong_FromUnsignedLong(unsigned long);
|
|
16 PyAPI_FUNC(PyObject *) PyLong_FromSize_t(size_t);
|
|
17 PyAPI_FUNC(PyObject *) PyLong_FromSsize_t(Py_ssize_t);
|
|
18 PyAPI_FUNC(PyObject *) PyLong_FromDouble(double);
|
|
19 PyAPI_FUNC(long) PyLong_AsLong(PyObject *);
|
|
20 PyAPI_FUNC(long) PyLong_AsLongAndOverflow(PyObject *, int *);
|
|
21 PyAPI_FUNC(Py_ssize_t) PyLong_AsSsize_t(PyObject *);
|
|
22 PyAPI_FUNC(size_t) PyLong_AsSize_t(PyObject *);
|
|
23 PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLong(PyObject *);
|
|
24 PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLongMask(PyObject *);
|
|
25 PyAPI_FUNC(PyObject *) PyLong_GetInfo(void);
|
|
26
|
|
27 /* It may be useful in the future. I've added it in the PyInt -> PyLong
|
|
28 cleanup to keep the extra information. [CH] */
|
|
29 #define PyLong_AS_LONG(op) PyLong_AsLong(op)
|
|
30
|
|
31 /* Issue #1983: pid_t can be longer than a C long on some systems */
|
|
32 #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
|
|
33 #define _Py_PARSE_PID "i"
|
|
34 #define PyLong_FromPid PyLong_FromLong
|
|
35 #define PyLong_AsPid PyLong_AsLong
|
|
36 #elif SIZEOF_PID_T == SIZEOF_LONG
|
|
37 #define _Py_PARSE_PID "l"
|
|
38 #define PyLong_FromPid PyLong_FromLong
|
|
39 #define PyLong_AsPid PyLong_AsLong
|
|
40 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
|
|
41 #define _Py_PARSE_PID "L"
|
|
42 #define PyLong_FromPid PyLong_FromLongLong
|
|
43 #define PyLong_AsPid PyLong_AsLongLong
|
|
44 #else
|
|
45 #error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
|
|
46 #endif /* SIZEOF_PID_T */
|
|
47
|
|
48 /* Used by Python/mystrtoul.c. */
|
|
49 #ifndef Py_LIMITED_API
|
|
50 PyAPI_DATA(unsigned char) _PyLong_DigitValue[256];
|
|
51 #endif
|
|
52
|
|
53 /* _PyLong_Frexp returns a double x and an exponent e such that the
|
|
54 true value is approximately equal to x * 2**e. e is >= 0. x is
|
|
55 0.0 if and only if the input is 0 (in which case, e and x are both
|
|
56 zeroes); otherwise, 0.5 <= abs(x) < 1.0. On overflow, which is
|
|
57 possible if the number of bits doesn't fit into a Py_ssize_t, sets
|
|
58 OverflowError and returns -1.0 for x, 0 for e. */
|
|
59 #ifndef Py_LIMITED_API
|
|
60 PyAPI_FUNC(double) _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e);
|
|
61 #endif
|
|
62
|
|
63 PyAPI_FUNC(double) PyLong_AsDouble(PyObject *);
|
|
64 PyAPI_FUNC(PyObject *) PyLong_FromVoidPtr(void *);
|
|
65 PyAPI_FUNC(void *) PyLong_AsVoidPtr(PyObject *);
|
|
66
|
|
67 #ifdef HAVE_LONG_LONG
|
|
68 PyAPI_FUNC(PyObject *) PyLong_FromLongLong(PY_LONG_LONG);
|
|
69 PyAPI_FUNC(PyObject *) PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG);
|
|
70 PyAPI_FUNC(PY_LONG_LONG) PyLong_AsLongLong(PyObject *);
|
|
71 PyAPI_FUNC(unsigned PY_LONG_LONG) PyLong_AsUnsignedLongLong(PyObject *);
|
|
72 PyAPI_FUNC(unsigned PY_LONG_LONG) PyLong_AsUnsignedLongLongMask(PyObject *);
|
|
73 PyAPI_FUNC(PY_LONG_LONG) PyLong_AsLongLongAndOverflow(PyObject *, int *);
|
|
74 #endif /* HAVE_LONG_LONG */
|
|
75
|
|
76 PyAPI_FUNC(PyObject *) PyLong_FromString(char *, char **, int);
|
|
77 #ifndef Py_LIMITED_API
|
|
78 PyAPI_FUNC(PyObject *) PyLong_FromUnicode(Py_UNICODE*, Py_ssize_t, int);
|
|
79 PyAPI_FUNC(PyObject *) PyLong_FromUnicodeObject(PyObject *u, int base);
|
|
80 #endif
|
|
81
|
|
82 #ifndef Py_LIMITED_API
|
|
83 /* _PyLong_Sign. Return 0 if v is 0, -1 if v < 0, +1 if v > 0.
|
|
84 v must not be NULL, and must be a normalized long.
|
|
85 There are no error cases.
|
|
86 */
|
|
87 PyAPI_FUNC(int) _PyLong_Sign(PyObject *v);
|
|
88
|
|
89
|
|
90 /* _PyLong_NumBits. Return the number of bits needed to represent the
|
|
91 absolute value of a long. For example, this returns 1 for 1 and -1, 2
|
|
92 for 2 and -2, and 2 for 3 and -3. It returns 0 for 0.
|
|
93 v must not be NULL, and must be a normalized long.
|
|
94 (size_t)-1 is returned and OverflowError set if the true result doesn't
|
|
95 fit in a size_t.
|
|
96 */
|
|
97 PyAPI_FUNC(size_t) _PyLong_NumBits(PyObject *v);
|
|
98
|
|
99 /* _PyLong_DivmodNear. Given integers a and b, compute the nearest
|
|
100 integer q to the exact quotient a / b, rounding to the nearest even integer
|
|
101 in the case of a tie. Return (q, r), where r = a - q*b. The remainder r
|
|
102 will satisfy abs(r) <= abs(b)/2, with equality possible only if q is
|
|
103 even.
|
|
104 */
|
|
105 PyAPI_FUNC(PyObject *) _PyLong_DivmodNear(PyObject *, PyObject *);
|
|
106
|
|
107 /* _PyLong_FromByteArray: View the n unsigned bytes as a binary integer in
|
|
108 base 256, and return a Python long with the same numeric value.
|
|
109 If n is 0, the integer is 0. Else:
|
|
110 If little_endian is 1/true, bytes[n-1] is the MSB and bytes[0] the LSB;
|
|
111 else (little_endian is 0/false) bytes[0] is the MSB and bytes[n-1] the
|
|
112 LSB.
|
|
113 If is_signed is 0/false, view the bytes as a non-negative integer.
|
|
114 If is_signed is 1/true, view the bytes as a 2's-complement integer,
|
|
115 non-negative if bit 0x80 of the MSB is clear, negative if set.
|
|
116 Error returns:
|
|
117 + Return NULL with the appropriate exception set if there's not
|
|
118 enough memory to create the Python long.
|
|
119 */
|
|
120 PyAPI_FUNC(PyObject *) _PyLong_FromByteArray(
|
|
121 const unsigned char* bytes, size_t n,
|
|
122 int little_endian, int is_signed);
|
|
123
|
|
124 /* _PyLong_AsByteArray: Convert the least-significant 8*n bits of long
|
|
125 v to a base-256 integer, stored in array bytes. Normally return 0,
|
|
126 return -1 on error.
|
|
127 If little_endian is 1/true, store the MSB at bytes[n-1] and the LSB at
|
|
128 bytes[0]; else (little_endian is 0/false) store the MSB at bytes[0] and
|
|
129 the LSB at bytes[n-1].
|
|
130 If is_signed is 0/false, it's an error if v < 0; else (v >= 0) n bytes
|
|
131 are filled and there's nothing special about bit 0x80 of the MSB.
|
|
132 If is_signed is 1/true, bytes is filled with the 2's-complement
|
|
133 representation of v's value. Bit 0x80 of the MSB is the sign bit.
|
|
134 Error returns (-1):
|
|
135 + is_signed is 0 and v < 0. TypeError is set in this case, and bytes
|
|
136 isn't altered.
|
|
137 + n isn't big enough to hold the full mathematical value of v. For
|
|
138 example, if is_signed is 0 and there are more digits in the v than
|
|
139 fit in n; or if is_signed is 1, v < 0, and n is just 1 bit shy of
|
|
140 being large enough to hold a sign bit. OverflowError is set in this
|
|
141 case, but bytes holds the least-signficant n bytes of the true value.
|
|
142 */
|
|
143 PyAPI_FUNC(int) _PyLong_AsByteArray(PyLongObject* v,
|
|
144 unsigned char* bytes, size_t n,
|
|
145 int little_endian, int is_signed);
|
|
146
|
|
147
|
|
148 /* _PyLong_Format: Convert the long to a string object with given base,
|
|
149 appending a base prefix of 0[box] if base is 2, 8 or 16. */
|
|
150 PyAPI_FUNC(PyObject *) _PyLong_Format(PyObject *aa, int base);
|
|
151
|
|
152 /* Format the object based on the format_spec, as defined in PEP 3101
|
|
153 (Advanced String Formatting). */
|
|
154 PyAPI_FUNC(PyObject *) _PyLong_FormatAdvanced(PyObject *obj,
|
|
155 PyObject *format_spec,
|
|
156 Py_ssize_t start,
|
|
157 Py_ssize_t end);
|
|
158 #endif /* Py_LIMITED_API */
|
|
159
|
|
160 /* These aren't really part of the long object, but they're handy. The
|
|
161 functions are in Python/mystrtoul.c.
|
|
162 */
|
|
163 PyAPI_FUNC(unsigned long) PyOS_strtoul(char *, char **, int);
|
|
164 PyAPI_FUNC(long) PyOS_strtol(char *, char **, int);
|
|
165
|
|
166 #endif /* !Py_LONGOBJECT_H */
|