1 #ifndef Py_LONGOBJECT_H
2 #define Py_LONGOBJECT_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6
7
8 /* Long (arbitrary precision) integer object interface */
9
10 // PyLong_Type is declared by object.h
11
12 #define PyLong_Check(op) \
13 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_LONG_SUBCLASS)
14 #define PyLong_CheckExact(op) Py_IS_TYPE((op), &PyLong_Type)
15
16 PyAPI_FUNC(PyObject *) PyLong_FromLong(long);
17 PyAPI_FUNC(PyObject *) PyLong_FromUnsignedLong(unsigned long);
18 PyAPI_FUNC(PyObject *) PyLong_FromSize_t(size_t);
19 PyAPI_FUNC(PyObject *) PyLong_FromSsize_t(Py_ssize_t);
20 PyAPI_FUNC(PyObject *) PyLong_FromDouble(double);
21
22 PyAPI_FUNC(long) PyLong_AsLong(PyObject *);
23 PyAPI_FUNC(long) PyLong_AsLongAndOverflow(PyObject *, int *);
24 PyAPI_FUNC(Py_ssize_t) PyLong_AsSsize_t(PyObject *);
25 PyAPI_FUNC(size_t) PyLong_AsSize_t(PyObject *);
26 PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLong(PyObject *);
27 PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLongMask(PyObject *);
28
29 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
30 PyAPI_FUNC(int) PyLong_AsInt(PyObject *);
31 #endif
32
33 PyAPI_FUNC(PyObject *) PyLong_GetInfo(void);
34
35 /* It may be useful in the future. I've added it in the PyInt -> PyLong
36 cleanup to keep the extra information. [CH] */
37 #define PyLong_AS_LONG(op) PyLong_AsLong(op)
38
39 /* Issue #1983: pid_t can be longer than a C long on some systems */
40 #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
41 #define _Py_PARSE_PID "i"
42 #define PyLong_FromPid PyLong_FromLong
43 # if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
44 # define PyLong_AsPid PyLong_AsInt
45 # elif SIZEOF_INT == SIZEOF_LONG
46 # define PyLong_AsPid PyLong_AsLong
47 # else
48 static inline int
PyLong_AsPid(PyObject * obj)49 PyLong_AsPid(PyObject *obj)
50 {
51 int overflow;
52 long result = PyLong_AsLongAndOverflow(obj, &overflow);
53 if (overflow || result > INT_MAX || result < INT_MIN) {
54 PyErr_SetString(PyExc_OverflowError,
55 "Python int too large to convert to C int");
56 return -1;
57 }
58 return (int)result;
59 }
60 # endif
61 #elif SIZEOF_PID_T == SIZEOF_LONG
62 #define _Py_PARSE_PID "l"
63 #define PyLong_FromPid PyLong_FromLong
64 #define PyLong_AsPid PyLong_AsLong
65 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
66 #define _Py_PARSE_PID "L"
67 #define PyLong_FromPid PyLong_FromLongLong
68 #define PyLong_AsPid PyLong_AsLongLong
69 #else
70 #error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
71 #endif /* SIZEOF_PID_T */
72
73 #if SIZEOF_VOID_P == SIZEOF_INT
74 # define _Py_PARSE_INTPTR "i"
75 # define _Py_PARSE_UINTPTR "I"
76 #elif SIZEOF_VOID_P == SIZEOF_LONG
77 # define _Py_PARSE_INTPTR "l"
78 # define _Py_PARSE_UINTPTR "k"
79 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_VOID_P == SIZEOF_LONG_LONG
80 # define _Py_PARSE_INTPTR "L"
81 # define _Py_PARSE_UINTPTR "K"
82 #else
83 # error "void* different in size from int, long and long long"
84 #endif /* SIZEOF_VOID_P */
85
86 PyAPI_FUNC(double) PyLong_AsDouble(PyObject *);
87 PyAPI_FUNC(PyObject *) PyLong_FromVoidPtr(void *);
88 PyAPI_FUNC(void *) PyLong_AsVoidPtr(PyObject *);
89
90 PyAPI_FUNC(PyObject *) PyLong_FromLongLong(long long);
91 PyAPI_FUNC(PyObject *) PyLong_FromUnsignedLongLong(unsigned long long);
92 PyAPI_FUNC(long long) PyLong_AsLongLong(PyObject *);
93 PyAPI_FUNC(unsigned long long) PyLong_AsUnsignedLongLong(PyObject *);
94 PyAPI_FUNC(unsigned long long) PyLong_AsUnsignedLongLongMask(PyObject *);
95 PyAPI_FUNC(long long) PyLong_AsLongLongAndOverflow(PyObject *, int *);
96
97 PyAPI_FUNC(PyObject *) PyLong_FromString(const char *, char **, int);
98
99 /* These aren't really part of the int object, but they're handy. The
100 functions are in Python/mystrtoul.c.
101 */
102 PyAPI_FUNC(unsigned long) PyOS_strtoul(const char *, char **, int);
103 PyAPI_FUNC(long) PyOS_strtol(const char *, char **, int);
104
105 #ifndef Py_LIMITED_API
106 # define Py_CPYTHON_LONGOBJECT_H
107 # include "cpython/longobject.h"
108 # undef Py_CPYTHON_LONGOBJECT_H
109 #endif
110
111 #ifdef __cplusplus
112 }
113 #endif
114 #endif /* !Py_LONGOBJECT_H */
115