longobject.h 3.7 KB

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