pgcompat.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Python 2.x/3.x compitibility tools
  2. */
  3. #if !defined(PGCOMPAT_H)
  4. #define PGCOMPAT_H
  5. #if PY_MAJOR_VERSION >= 3
  6. #define PY3 1
  7. /* Define some aliases for the removed PyInt_* functions */
  8. #define PyInt_Check(op) PyLong_Check(op)
  9. #define PyInt_FromString PyLong_FromString
  10. #define PyInt_FromUnicode PyLong_FromUnicode
  11. #define PyInt_FromLong PyLong_FromLong
  12. #define PyInt_FromSize_t PyLong_FromSize_t
  13. #define PyInt_FromSsize_t PyLong_FromSsize_t
  14. #define PyInt_AsLong PyLong_AsLong
  15. #define PyInt_AsSsize_t PyLong_AsSsize_t
  16. #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask
  17. #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
  18. #define PyInt_AS_LONG PyLong_AS_LONG
  19. #define PyNumber_Int PyNumber_Long
  20. /* Weakrefs flags changed in 3.x */
  21. #define Py_TPFLAGS_HAVE_WEAKREFS 0
  22. /* Module init function returns new module instance. */
  23. #define MODINIT_RETURN(x) return x
  24. #define MODINIT_DEFINE(mod_name) PyMODINIT_FUNC PyInit_##mod_name (void)
  25. #define DECREF_MOD(mod) Py_DECREF (mod)
  26. /* Type header differs. */
  27. #define TYPE_HEAD(x,y) PyVarObject_HEAD_INIT(x,y)
  28. /* Text interface. Use unicode strings. */
  29. #define Text_Type PyUnicode_Type
  30. #define Text_Check PyUnicode_Check
  31. #define Text_FromUTF8 PyUnicode_FromString
  32. #define Text_FromUTF8AndSize PyUnicode_FromStringAndSize
  33. #define Text_FromFormat PyUnicode_FromFormat
  34. #define Text_GetSize PyUnicode_GetSize
  35. #define Text_GET_SIZE PyUnicode_GET_SIZE
  36. /* Binary interface. Use bytes. */
  37. #define Bytes_Type PyBytes_Type
  38. #define Bytes_Check PyBytes_Check
  39. #define Bytes_Size PyBytes_Size
  40. #define Bytes_AsString PyBytes_AsString
  41. #define Bytes_AsStringAndSize PyBytes_AsStringAndSize
  42. #define Bytes_FromStringAndSize PyBytes_FromStringAndSize
  43. #define Bytes_AS_STRING PyBytes_AS_STRING
  44. #define Bytes_GET_SIZE PyBytes_GET_SIZE
  45. #define IsTextObj(x) (PyUnicode_Check(x) || PyBytes_Check(x))
  46. /* Renamed builtins */
  47. #define BUILTINS_MODULE "builtins"
  48. #define BUILTINS_UNICODE "str"
  49. #define BUILTINS_UNICHR "chr"
  50. #else /* PY_VERSION_HEX >= 0x03000000 */
  51. #define PY3 0
  52. /* Module init function returns nothing. */
  53. #define MODINIT_RETURN(x) return
  54. #define MODINIT_DEFINE(mod_name) PyMODINIT_FUNC init##mod_name (void)
  55. #define DECREF_MOD(mod)
  56. /* Type header differs. */
  57. #define TYPE_HEAD(x,y) \
  58. PyObject_HEAD_INIT(x) \
  59. 0,
  60. /* Text interface. Use ascii strings. */
  61. #define Text_Type PyString_Type
  62. #define Text_Check PyString_Check
  63. #define Text_FromUTF8 PyString_FromString
  64. #define Text_FromUTF8AndSize PyString_FromStringAndSize
  65. #define Text_FromFormat PyString_FromFormat
  66. #define Text_GetSize PyString_GetSize
  67. #define Text_GET_SIZE PyString_GET_SIZE
  68. /* Binary interface. Use ascii strings. */
  69. #define Bytes_Type PyString_Type
  70. #define Bytes_Check PyString_Check
  71. #define Bytes_Size PyString_Size
  72. #define Bytes_AsString PyString_AsString
  73. #define Bytes_AsStringAndSize PyString_AsStringAndSize
  74. #define Bytes_FromStringAndSize PyString_FromStringAndSize
  75. #define Bytes_AS_STRING PyString_AS_STRING
  76. #define Bytes_GET_SIZE PyString_GET_SIZE
  77. /* Renamed builtins */
  78. #define BUILTINS_MODULE "__builtin__"
  79. #define BUILTINS_UNICODE "unicode"
  80. #define BUILTINS_UNICHR "unichr"
  81. #endif /* PY_VERSION_HEX >= 0x03000000 */
  82. #define MODINIT_ERROR MODINIT_RETURN (NULL)
  83. /* Module state. These macros are used to define per-module macros.
  84. * v - global state variable (Python 2.x)
  85. * s - global state structure (Python 3.x)
  86. */
  87. #define PY2_GETSTATE(v) (&(v))
  88. #define PY3_GETSTATE(s, m) ((struct s *) PyModule_GetState (m))
  89. /* Pep 3123: Making PyObject_HEAD conform to standard C */
  90. #if !defined(Py_TYPE)
  91. #define Py_TYPE(o) (((PyObject*)(o))->ob_type)
  92. #define Py_REFCNT(o) (((PyObject*)(o))->ob_refcnt)
  93. #define Py_SIZE(o) (((PyVarObject*)(o))->ob_size)
  94. #endif
  95. #endif /* !defined(PGCOMPAT_H) */