1
0

pgcompat.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #ifndef PYPY_VERSION
  32. #define Text_FromLocale(s) PyUnicode_DecodeLocale((s), "strict")
  33. #else /* PYPY_VERSION */
  34. /* workaround: missing function for pypy */
  35. #define Text_FromLocale PyUnicode_FromString
  36. #endif /* PYPY_VERSION */
  37. #define Text_FromUTF8 PyUnicode_FromString
  38. #define Text_FromUTF8AndSize PyUnicode_FromStringAndSize
  39. #define Text_FromFormat PyUnicode_FromFormat
  40. #define Text_GetSize PyUnicode_GetSize
  41. #define Text_GET_SIZE PyUnicode_GET_SIZE
  42. /* Binary interface. Use bytes. */
  43. #define Bytes_Type PyBytes_Type
  44. #define Bytes_Check PyBytes_Check
  45. #define Bytes_Size PyBytes_Size
  46. #define Bytes_AsString PyBytes_AsString
  47. #define Bytes_AsStringAndSize PyBytes_AsStringAndSize
  48. #define Bytes_FromStringAndSize PyBytes_FromStringAndSize
  49. #define Bytes_FromFormat PyBytes_FromFormat
  50. #define Bytes_AS_STRING PyBytes_AS_STRING
  51. #define Bytes_GET_SIZE PyBytes_GET_SIZE
  52. #define Bytes_AsDecodeObject PyBytes_AsDecodedObject
  53. #define Object_Unicode PyObject_Str
  54. #define IsTextObj(x) (PyUnicode_Check(x) || PyBytes_Check(x))
  55. /* Renamed builtins */
  56. #define BUILTINS_MODULE "builtins"
  57. #define BUILTINS_UNICODE "str"
  58. #define BUILTINS_UNICHR "chr"
  59. /* Defaults for unicode file path encoding */
  60. #define UNICODE_DEF_FS_CODEC Py_FileSystemDefaultEncoding
  61. #if defined(MS_WIN32)
  62. #define UNICODE_DEF_FS_ERROR "replace"
  63. #else
  64. #define UNICODE_DEF_FS_ERROR "surrogateescape"
  65. #endif
  66. #else /* #if PY_MAJOR_VERSION >= 3 */
  67. #define PY3 0
  68. /* Module init function returns nothing. */
  69. #define MODINIT_RETURN(x) return
  70. #define MODINIT_DEFINE(mod_name) PyMODINIT_FUNC init##mod_name (void)
  71. #define DECREF_MOD(mod)
  72. /* Type header differs. */
  73. #define TYPE_HEAD(x,y) \
  74. PyObject_HEAD_INIT(x) \
  75. 0,
  76. /* Text interface. Use ascii strings. */
  77. #define Text_Type PyString_Type
  78. #define Text_Check PyString_Check
  79. #define Text_FromLocale PyString_FromString
  80. #define Text_FromUTF8 PyString_FromString
  81. #define Text_FromUTF8AndSize PyString_FromStringAndSize
  82. #define Text_FromFormat PyString_FromFormat
  83. #define Text_GetSize PyString_GetSize
  84. #define Text_GET_SIZE PyString_GET_SIZE
  85. /* Binary interface. Use ascii strings. */
  86. #define Bytes_Type PyString_Type
  87. #define Bytes_Check PyString_Check
  88. #define Bytes_Size PyString_Size
  89. #define Bytes_AsString PyString_AsString
  90. #define Bytes_AsStringAndSize PyString_AsStringAndSize
  91. #define Bytes_FromStringAndSize PyString_FromStringAndSize
  92. #define Bytes_FromFormat PyString_FromFormat
  93. #define Bytes_AS_STRING PyString_AS_STRING
  94. #define Bytes_GET_SIZE PyString_GET_SIZE
  95. #define Bytes_AsDecodedObject PyString_AsDecodedObject
  96. #define Object_Unicode PyObject_Unicode
  97. /* Renamed builtins */
  98. #define BUILTINS_MODULE "__builtin__"
  99. #define BUILTINS_UNICODE "unicode"
  100. #define BUILTINS_UNICHR "unichr"
  101. /* Defaults for unicode file path encoding */
  102. #define UNICODE_DEF_FS_CODEC Py_FileSystemDefaultEncoding
  103. #define UNICODE_DEF_FS_ERROR "strict"
  104. #endif /* #if PY_MAJOR_VERSION >= 3 */
  105. #define PY2 (!PY3)
  106. #define MODINIT_ERROR MODINIT_RETURN (NULL)
  107. /* Module state. These macros are used to define per-module macros.
  108. * v - global state variable (Python 2.x)
  109. * s - global state structure (Python 3.x)
  110. */
  111. #define PY2_GETSTATE(v) (&(v))
  112. #define PY3_GETSTATE(s, m) ((struct s *) PyModule_GetState (m))
  113. /* Pep 3123: Making PyObject_HEAD conform to standard C */
  114. #if !defined(Py_TYPE)
  115. #define Py_TYPE(o) (((PyObject *)(o))->ob_type)
  116. #define Py_REFCNT(o) (((PyObject *)(o))->ob_refcnt)
  117. #define Py_SIZE(o) (((PyVarObject *)(o))->ob_size)
  118. #endif
  119. /* Encode a unicode file path */
  120. #define Unicode_AsEncodedPath(u) \
  121. PyUnicode_AsEncodedString ((u), UNICODE_DEF_FS_CODEC, UNICODE_DEF_FS_ERROR)
  122. #define RELATIVE_MODULE(m) ("." m)
  123. #define HAVE_OLD_BUFPROTO PY2
  124. #if !defined(PG_ENABLE_OLDBUF) /* allow for command line override */
  125. #if HAVE_OLD_BUFPROTO
  126. #define PG_ENABLE_OLDBUF 1
  127. #else
  128. #define PG_ENABLE_OLDBUF 0
  129. #endif
  130. #endif
  131. #ifndef Py_TPFLAGS_HAVE_NEWBUFFER
  132. #define Py_TPFLAGS_HAVE_NEWBUFFER 0
  133. #endif
  134. #ifndef Py_TPFLAGS_HAVE_CLASS
  135. #define Py_TPFLAGS_HAVE_CLASS 0
  136. #endif
  137. #ifndef Py_TPFLAGS_CHECKTYPES
  138. #define Py_TPFLAGS_CHECKTYPES 0
  139. #endif
  140. #if PY_VERSION_HEX >= 0x03020000
  141. #define Slice_GET_INDICES_EX(slice, length, start, stop, step, slicelength) \
  142. PySlice_GetIndicesEx(slice, length, start, stop, step, slicelength)
  143. #else
  144. #define Slice_GET_INDICES_EX(slice, length, start, stop, step, slicelength) \
  145. PySlice_GetIndicesEx((PySliceObject *)(slice), length, \
  146. start, stop, step, slicelength)
  147. #endif
  148. /* Support new buffer protocol? */
  149. #if !defined(PG_ENABLE_NEWBUF) /* allow for command line override */
  150. #if !defined(PYPY_VERSION)
  151. #define PG_ENABLE_NEWBUF 1
  152. #else
  153. #define PG_ENABLE_NEWBUF 0
  154. #endif
  155. #endif
  156. #endif /* #if !defined(PGCOMPAT_H) */