object.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #ifndef Py_OBJECT_H
  2. #define Py_OBJECT_H
  3. #include <stdio.h>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #include "cpyext_object.h"
  8. #define PY_SSIZE_T_MAX ((Py_ssize_t)(((size_t)-1)>>1))
  9. #define PY_SSIZE_T_MIN (-PY_SSIZE_T_MAX-1)
  10. #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
  11. /*
  12. CPython has this for backwards compatibility with really old extensions, and now
  13. we have it for compatibility with CPython.
  14. */
  15. #define staticforward static
  16. #define PyObject_HEAD_INIT(type) \
  17. 1, 0, type,
  18. #define PyVarObject_HEAD_INIT(type, size) \
  19. PyObject_HEAD_INIT(type) size,
  20. #ifdef PYPY_DEBUG_REFCOUNT
  21. /* Slow version, but useful for debugging */
  22. #define Py_INCREF(ob) (Py_IncRef((PyObject *)(ob)))
  23. #define Py_DECREF(ob) (Py_DecRef((PyObject *)(ob)))
  24. #define Py_XINCREF(ob) (Py_IncRef((PyObject *)(ob)))
  25. #define Py_XDECREF(ob) (Py_DecRef((PyObject *)(ob)))
  26. #else
  27. /* Fast version */
  28. #define Py_INCREF(ob) (((PyObject *)(ob))->ob_refcnt++)
  29. #define Py_DECREF(op) \
  30. do { \
  31. PyObject *_py_decref_tmp = (PyObject *)(op); \
  32. if (--(_py_decref_tmp)->ob_refcnt != 0) \
  33. ; \
  34. else \
  35. _Py_Dealloc(_py_decref_tmp); \
  36. } while (0)
  37. #define Py_XINCREF(op) \
  38. do { \
  39. PyObject *_py_xincref_tmp = (PyObject *)(op); \
  40. if (_py_xincref_tmp != NULL) \
  41. Py_INCREF(_py_xincref_tmp); \
  42. } while (0)
  43. #define Py_XDECREF(op) \
  44. do { \
  45. PyObject *_py_xdecref_tmp = (PyObject *)(op); \
  46. if (_py_xdecref_tmp != NULL) \
  47. Py_DECREF(_py_xdecref_tmp); \
  48. } while (0)
  49. #endif
  50. #define Py_CLEAR(op) \
  51. do { \
  52. PyObject *_py_tmp = (PyObject *)(op); \
  53. if (_py_tmp != NULL) { \
  54. (op) = NULL; \
  55. Py_DECREF(_py_tmp); \
  56. } \
  57. } while (0)
  58. #define Py_SETREF(op, op2) \
  59. do { \
  60. PyObject *_py_tmp = (PyObject *)(op); \
  61. (op) = (op2); \
  62. Py_DECREF(_py_tmp); \
  63. } while (0)
  64. #define Py_XSETREF(op, op2) \
  65. do { \
  66. PyObject *_py_tmp = (PyObject *)(op); \
  67. (op) = (op2); \
  68. Py_XDECREF(_py_tmp); \
  69. } while (0)
  70. #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
  71. #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
  72. #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
  73. #define _Py_ForgetReference(ob) /* nothing */
  74. #define Py_None (&_Py_NoneStruct)
  75. /*
  76. Py_NotImplemented is a singleton used to signal that an operation is
  77. not implemented for a given type combination.
  78. */
  79. #define Py_NotImplemented (&_Py_NotImplementedStruct)
  80. /* Rich comparison opcodes */
  81. /*
  82. XXX: Also defined in slotdefs.py
  83. */
  84. #define Py_LT 0
  85. #define Py_LE 1
  86. #define Py_EQ 2
  87. #define Py_NE 3
  88. #define Py_GT 4
  89. #define Py_GE 5
  90. /* Py3k buffer interface, adapted for PyPy */
  91. /* Flags for getting buffers */
  92. #define PyBUF_SIMPLE 0
  93. #define PyBUF_WRITABLE 0x0001
  94. /* we used to include an E, backwards compatible alias */
  95. #define PyBUF_WRITEABLE PyBUF_WRITABLE
  96. #define PyBUF_FORMAT 0x0004
  97. #define PyBUF_ND 0x0008
  98. #define PyBUF_STRIDES (0x0010 | PyBUF_ND)
  99. #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
  100. #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
  101. #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
  102. #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
  103. #define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE)
  104. #define PyBUF_CONTIG_RO (PyBUF_ND)
  105. #define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE)
  106. #define PyBUF_STRIDED_RO (PyBUF_STRIDES)
  107. #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT)
  108. #define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT)
  109. #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT)
  110. #define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT)
  111. #define PyBUF_READ 0x100
  112. #define PyBUF_WRITE 0x200
  113. #define PyBUF_SHADOW 0x400
  114. /* end Py3k buffer interface */
  115. PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
  116. /* Flag bits for printing: */
  117. #define Py_PRINT_RAW 1 /* No string quotes etc. */
  118. /*
  119. `Type flags (tp_flags)
  120. These flags are used to extend the type structure in a backwards-compatible
  121. fashion. Extensions can use the flags to indicate (and test) when a given
  122. type structure contains a new feature. The Python core will use these when
  123. introducing new functionality between major revisions (to avoid mid-version
  124. changes in the PYTHON_API_VERSION).
  125. Arbitration of the flag bit positions will need to be coordinated among
  126. all extension writers who publically release their extensions (this will
  127. be fewer than you might expect!)..
  128. Most flags were removed as of Python 3.0 to make room for new flags. (Some
  129. flags are not for backwards compatibility but to indicate the presence of an
  130. optional feature; these flags remain of course.)
  131. Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
  132. Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
  133. given type object has a specified feature.
  134. */
  135. /* Set if the type object is dynamically allocated */
  136. #define Py_TPFLAGS_HEAPTYPE (1L<<9)
  137. /* Set if the type allows subclassing */
  138. #define Py_TPFLAGS_BASETYPE (1L<<10)
  139. /* Set if the type is 'ready' -- fully initialized */
  140. #define Py_TPFLAGS_READY (1L<<12)
  141. /* Set while the type is being 'readied', to prevent recursive ready calls */
  142. #define Py_TPFLAGS_READYING (1L<<13)
  143. /* Objects support garbage collection (see objimp.h) */
  144. #define Py_TPFLAGS_HAVE_GC (1L<<14)
  145. /* These two bits are preserved for Stackless Python, next after this is 17 */
  146. #ifdef STACKLESS
  147. #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3L<<15)
  148. #else
  149. #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0
  150. #endif
  151. /* Objects support type attribute cache */
  152. #define Py_TPFLAGS_HAVE_VERSION_TAG (1L<<18)
  153. #define Py_TPFLAGS_VALID_VERSION_TAG (1L<<19)
  154. /* Type is abstract and cannot be instantiated */
  155. #define Py_TPFLAGS_IS_ABSTRACT (1L<<20)
  156. /* These flags are used to determine if a type is a subclass. */
  157. #define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24)
  158. #define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25)
  159. #define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26)
  160. #define Py_TPFLAGS_BYTES_SUBCLASS (1UL << 27)
  161. #define Py_TPFLAGS_UNICODE_SUBCLASS (1UL << 28)
  162. #define Py_TPFLAGS_DICT_SUBCLASS (1UL << 29)
  163. #define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30)
  164. #define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31)
  165. #define Py_TPFLAGS_DEFAULT ( \
  166. Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
  167. Py_TPFLAGS_HAVE_VERSION_TAG | \
  168. 0)
  169. /* NOTE: The following flags reuse lower bits (removed as part of the
  170. * Python 3.0 transition). */
  171. /* Type structure has tp_finalize member (3.4) */
  172. #define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
  173. #ifdef Py_LIMITED_API
  174. #define PyType_HasFeature(t,f) ((PyType_GetFlags(t) & (f)) != 0)
  175. #else
  176. #define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
  177. #endif
  178. #define PyType_FastSubclass(t,f) PyType_HasFeature(t,f)
  179. #define PyType_Check(op) \
  180. PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS)
  181. #define PyType_CheckExact(op) (Py_TYPE(op) == &PyType_Type)
  182. /* objimpl.h ----------------------------------------------*/
  183. #define PyObject_New(type, typeobj) \
  184. ( (type *) _PyObject_New(typeobj) )
  185. #define PyObject_NewVar(type, typeobj, n) \
  186. ( (type *) _PyObject_NewVar((typeobj), (n)) )
  187. #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
  188. #define _PyObject_VAR_SIZE(typeobj, nitems) \
  189. (size_t) \
  190. ( ( (typeobj)->tp_basicsize + \
  191. (nitems)*(typeobj)->tp_itemsize + \
  192. (SIZEOF_VOID_P - 1) \
  193. ) & ~(SIZEOF_VOID_P - 1) \
  194. )
  195. #define PyObject_INIT PyObject_Init
  196. #define PyObject_INIT_VAR PyObject_InitVar
  197. /*
  198. #define PyObject_NEW(type, typeobj) \
  199. ( (type *) PyObject_Init( \
  200. (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
  201. */
  202. #define PyObject_NEW PyObject_New
  203. #define PyObject_NEW_VAR PyObject_NewVar
  204. /*
  205. #define PyObject_NEW_VAR(type, typeobj, n) \
  206. ( (type *) PyObject_InitVar( \
  207. (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
  208. (typeobj), (n)) )
  209. */
  210. #define PyObject_GC_New(type, typeobj) \
  211. ( (type *) _PyObject_GC_New(typeobj) )
  212. #define PyObject_GC_NewVar(type, typeobj, size) \
  213. ( (type *) _PyObject_GC_NewVar(typeobj, size) )
  214. /* A dummy PyGC_Head, just to please some tests. Don't use it! */
  215. typedef union _gc_head {
  216. char dummy;
  217. } PyGC_Head;
  218. /* dummy GC macros */
  219. #define _PyGC_FINALIZED(o) 1
  220. #define PyType_IS_GC(tp) 1
  221. #define PyObject_GC_Track(o) do { } while(0)
  222. #define PyObject_GC_UnTrack(o) do { } while(0)
  223. #define _PyObject_GC_TRACK(o) do { } while(0)
  224. #define _PyObject_GC_UNTRACK(o) do { } while(0)
  225. /* Utility macro to help write tp_traverse functions.
  226. * To use this macro, the tp_traverse function must name its arguments
  227. * "visit" and "arg". This is intended to keep tp_traverse functions
  228. * looking as much alike as possible.
  229. */
  230. #define Py_VISIT(op) \
  231. do { \
  232. if (op) { \
  233. int vret = visit((PyObject *)(op), arg); \
  234. if (vret) \
  235. return vret; \
  236. } \
  237. } while (0)
  238. #define PyObject_TypeCheck(ob, tp) \
  239. ((ob)->ob_type == (tp) || PyType_IsSubtype((ob)->ob_type, (tp)))
  240. #define Py_TRASHCAN_SAFE_BEGIN(pyObj) do {
  241. #define Py_TRASHCAN_SAFE_END(pyObj) ; } while(0);
  242. /* note: the ";" at the start of Py_TRASHCAN_SAFE_END is needed
  243. if the code has a label in front of the macro call */
  244. /* Copied from CPython ----------------------------- */
  245. PyAPI_FUNC(int) PyObject_AsReadBuffer(PyObject *, const void **, Py_ssize_t *);
  246. PyAPI_FUNC(int) PyObject_AsWriteBuffer(PyObject *, void **, Py_ssize_t *);
  247. PyAPI_FUNC(int) PyObject_CheckReadBuffer(PyObject *);
  248. PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
  249. /* Get the memory area pointed to by the indices for the buffer given.
  250. Note that view->ndim is the assumed size of indices
  251. */
  252. PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
  253. Py_ssize_t len, char fort);
  254. PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
  255. Py_ssize_t len, char fort);
  256. /* Copy len bytes of data from the contiguous chunk of memory
  257. pointed to by buf into the buffer exported by obj. Return
  258. 0 on success and return -1 and raise a PyBuffer_Error on
  259. error (i.e. the object does not have a buffer interface or
  260. it is not working).
  261. If fort is 'F' and the object is multi-dimensional,
  262. then the data will be copied into the array in
  263. Fortran-style (first dimension varies the fastest). If
  264. fort is 'C', then the data will be copied into the array
  265. in C-style (last dimension varies the fastest). If fort
  266. is 'A', then it does not matter and the copy will be made
  267. in whatever way is more efficient.
  268. */
  269. #define PyObject_MALLOC PyObject_Malloc
  270. #define PyObject_REALLOC PyObject_Realloc
  271. #define PyObject_FREE PyObject_Free
  272. #define PyObject_Del PyObject_Free
  273. #define PyObject_DEL PyObject_Free
  274. #ifndef Py_LIMITED_API
  275. PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);
  276. #endif
  277. /* PyPy internal ----------------------------------- */
  278. PyAPI_FUNC(int) PyPyType_Register(PyTypeObject *);
  279. #define PyObject_Length PyObject_Size
  280. #define _PyObject_GC_Del PyObject_GC_Del
  281. #ifdef __cplusplus
  282. }
  283. #endif
  284. #endif /* !Py_OBJECT_H */