object.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. #ifndef Py_CPYTHON_OBJECT_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /********************* String Literals ****************************************/
  8. /* This structure helps managing static strings. The basic usage goes like this:
  9. Instead of doing
  10. r = PyObject_CallMethod(o, "foo", "args", ...);
  11. do
  12. _Py_IDENTIFIER(foo);
  13. ...
  14. r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...);
  15. PyId_foo is a static variable, either on block level or file level. On first
  16. usage, the string "foo" is interned, and the structures are linked. On interpreter
  17. shutdown, all strings are released (through _PyUnicode_ClearStaticStrings).
  18. Alternatively, _Py_static_string allows choosing the variable name.
  19. _PyUnicode_FromId returns a borrowed reference to the interned string.
  20. _PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
  21. */
  22. typedef struct _Py_Identifier {
  23. struct _Py_Identifier *next;
  24. const char* string;
  25. PyObject *object;
  26. } _Py_Identifier;
  27. #define _Py_static_string_init(value) { .next = NULL, .string = value, .object = NULL }
  28. #define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
  29. #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
  30. /* buffer interface */
  31. typedef struct bufferinfo {
  32. void *buf;
  33. PyObject *obj; /* owned reference */
  34. Py_ssize_t len;
  35. Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
  36. pointed to by strides in simple case.*/
  37. int readonly;
  38. int ndim;
  39. char *format;
  40. Py_ssize_t *shape;
  41. Py_ssize_t *strides;
  42. Py_ssize_t *suboffsets;
  43. void *internal;
  44. } Py_buffer;
  45. typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
  46. typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
  47. typedef PyObject *(*vectorcallfunc)(PyObject *callable, PyObject *const *args,
  48. size_t nargsf, PyObject *kwnames);
  49. /* Maximum number of dimensions */
  50. #define PyBUF_MAX_NDIM 64
  51. /* Flags for getting buffers */
  52. #define PyBUF_SIMPLE 0
  53. #define PyBUF_WRITABLE 0x0001
  54. /* we used to include an E, backwards compatible alias */
  55. #define PyBUF_WRITEABLE PyBUF_WRITABLE
  56. #define PyBUF_FORMAT 0x0004
  57. #define PyBUF_ND 0x0008
  58. #define PyBUF_STRIDES (0x0010 | PyBUF_ND)
  59. #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
  60. #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
  61. #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
  62. #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
  63. #define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE)
  64. #define PyBUF_CONTIG_RO (PyBUF_ND)
  65. #define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE)
  66. #define PyBUF_STRIDED_RO (PyBUF_STRIDES)
  67. #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT)
  68. #define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT)
  69. #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT)
  70. #define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT)
  71. #define PyBUF_READ 0x100
  72. #define PyBUF_WRITE 0x200
  73. /* End buffer interface */
  74. typedef struct {
  75. /* Number implementations must check *both*
  76. arguments for proper type and implement the necessary conversions
  77. in the slot functions themselves. */
  78. binaryfunc nb_add;
  79. binaryfunc nb_subtract;
  80. binaryfunc nb_multiply;
  81. binaryfunc nb_remainder;
  82. binaryfunc nb_divmod;
  83. ternaryfunc nb_power;
  84. unaryfunc nb_negative;
  85. unaryfunc nb_positive;
  86. unaryfunc nb_absolute;
  87. inquiry nb_bool;
  88. unaryfunc nb_invert;
  89. binaryfunc nb_lshift;
  90. binaryfunc nb_rshift;
  91. binaryfunc nb_and;
  92. binaryfunc nb_xor;
  93. binaryfunc nb_or;
  94. unaryfunc nb_int;
  95. void *nb_reserved; /* the slot formerly known as nb_long */
  96. unaryfunc nb_float;
  97. binaryfunc nb_inplace_add;
  98. binaryfunc nb_inplace_subtract;
  99. binaryfunc nb_inplace_multiply;
  100. binaryfunc nb_inplace_remainder;
  101. ternaryfunc nb_inplace_power;
  102. binaryfunc nb_inplace_lshift;
  103. binaryfunc nb_inplace_rshift;
  104. binaryfunc nb_inplace_and;
  105. binaryfunc nb_inplace_xor;
  106. binaryfunc nb_inplace_or;
  107. binaryfunc nb_floor_divide;
  108. binaryfunc nb_true_divide;
  109. binaryfunc nb_inplace_floor_divide;
  110. binaryfunc nb_inplace_true_divide;
  111. unaryfunc nb_index;
  112. binaryfunc nb_matrix_multiply;
  113. binaryfunc nb_inplace_matrix_multiply;
  114. } PyNumberMethods;
  115. typedef struct {
  116. lenfunc sq_length;
  117. binaryfunc sq_concat;
  118. ssizeargfunc sq_repeat;
  119. ssizeargfunc sq_item;
  120. void *was_sq_slice;
  121. ssizeobjargproc sq_ass_item;
  122. void *was_sq_ass_slice;
  123. objobjproc sq_contains;
  124. binaryfunc sq_inplace_concat;
  125. ssizeargfunc sq_inplace_repeat;
  126. } PySequenceMethods;
  127. typedef struct {
  128. lenfunc mp_length;
  129. binaryfunc mp_subscript;
  130. objobjargproc mp_ass_subscript;
  131. } PyMappingMethods;
  132. typedef struct {
  133. unaryfunc am_await;
  134. unaryfunc am_aiter;
  135. unaryfunc am_anext;
  136. } PyAsyncMethods;
  137. typedef struct {
  138. getbufferproc bf_getbuffer;
  139. releasebufferproc bf_releasebuffer;
  140. } PyBufferProcs;
  141. /* Allow printfunc in the tp_vectorcall_offset slot for
  142. * backwards-compatibility */
  143. typedef Py_ssize_t printfunc;
  144. typedef struct _typeobject {
  145. PyObject_VAR_HEAD
  146. const char *tp_name; /* For printing, in format "<module>.<name>" */
  147. Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
  148. /* Methods to implement standard operations */
  149. destructor tp_dealloc;
  150. Py_ssize_t tp_vectorcall_offset;
  151. getattrfunc tp_getattr;
  152. setattrfunc tp_setattr;
  153. PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
  154. or tp_reserved (Python 3) */
  155. reprfunc tp_repr;
  156. /* Method suites for standard classes */
  157. PyNumberMethods *tp_as_number;
  158. PySequenceMethods *tp_as_sequence;
  159. PyMappingMethods *tp_as_mapping;
  160. /* More standard operations (here for binary compatibility) */
  161. hashfunc tp_hash;
  162. ternaryfunc tp_call;
  163. reprfunc tp_str;
  164. getattrofunc tp_getattro;
  165. setattrofunc tp_setattro;
  166. /* Functions to access object as input/output buffer */
  167. PyBufferProcs *tp_as_buffer;
  168. /* Flags to define presence of optional/expanded features */
  169. unsigned long tp_flags;
  170. const char *tp_doc; /* Documentation string */
  171. /* Assigned meaning in release 2.0 */
  172. /* call function for all accessible objects */
  173. traverseproc tp_traverse;
  174. /* delete references to contained objects */
  175. inquiry tp_clear;
  176. /* Assigned meaning in release 2.1 */
  177. /* rich comparisons */
  178. richcmpfunc tp_richcompare;
  179. /* weak reference enabler */
  180. Py_ssize_t tp_weaklistoffset;
  181. /* Iterators */
  182. getiterfunc tp_iter;
  183. iternextfunc tp_iternext;
  184. /* Attribute descriptor and subclassing stuff */
  185. struct PyMethodDef *tp_methods;
  186. struct PyMemberDef *tp_members;
  187. struct PyGetSetDef *tp_getset;
  188. struct _typeobject *tp_base;
  189. PyObject *tp_dict;
  190. descrgetfunc tp_descr_get;
  191. descrsetfunc tp_descr_set;
  192. Py_ssize_t tp_dictoffset;
  193. initproc tp_init;
  194. allocfunc tp_alloc;
  195. newfunc tp_new;
  196. freefunc tp_free; /* Low-level free-memory routine */
  197. inquiry tp_is_gc; /* For PyObject_IS_GC */
  198. PyObject *tp_bases;
  199. PyObject *tp_mro; /* method resolution order */
  200. PyObject *tp_cache;
  201. PyObject *tp_subclasses;
  202. PyObject *tp_weaklist;
  203. destructor tp_del;
  204. /* Type attribute cache version tag. Added in version 2.6 */
  205. unsigned int tp_version_tag;
  206. destructor tp_finalize;
  207. vectorcallfunc tp_vectorcall;
  208. /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
  209. Py_DEPRECATED(3.8) int (*tp_print)(PyObject *, FILE *, int);
  210. #ifdef COUNT_ALLOCS
  211. /* these must be last and never explicitly initialized */
  212. Py_ssize_t tp_allocs;
  213. Py_ssize_t tp_frees;
  214. Py_ssize_t tp_maxalloc;
  215. struct _typeobject *tp_prev;
  216. struct _typeobject *tp_next;
  217. #endif
  218. } PyTypeObject;
  219. /* The *real* layout of a type object when allocated on the heap */
  220. typedef struct _heaptypeobject {
  221. /* Note: there's a dependency on the order of these members
  222. in slotptr() in typeobject.c . */
  223. PyTypeObject ht_type;
  224. PyAsyncMethods as_async;
  225. PyNumberMethods as_number;
  226. PyMappingMethods as_mapping;
  227. PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
  228. so that the mapping wins when both
  229. the mapping and the sequence define
  230. a given operator (e.g. __getitem__).
  231. see add_operators() in typeobject.c . */
  232. PyBufferProcs as_buffer;
  233. PyObject *ht_name, *ht_slots, *ht_qualname;
  234. struct _dictkeysobject *ht_cached_keys;
  235. /* here are optional user slots, followed by the members. */
  236. } PyHeapTypeObject;
  237. /* access macro to the members which are floating "behind" the object */
  238. #define PyHeapType_GET_MEMBERS(etype) \
  239. ((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize))
  240. PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *);
  241. PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
  242. PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *);
  243. PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, _Py_Identifier *);
  244. PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
  245. PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
  246. PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);
  247. struct _Py_Identifier;
  248. PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
  249. PyAPI_FUNC(void) _Py_BreakPoint(void);
  250. PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
  251. PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
  252. PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
  253. PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *);
  254. PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *);
  255. PyAPI_FUNC(int) _PyObject_HasAttrId(PyObject *, struct _Py_Identifier *);
  256. /* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which
  257. don't raise AttributeError.
  258. Return 1 and set *result != NULL if an attribute is found.
  259. Return 0 and set *result == NULL if an attribute is not found;
  260. an AttributeError is silenced.
  261. Return -1 and set *result == NULL if an error other than AttributeError
  262. is raised.
  263. */
  264. PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **);
  265. PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, struct _Py_Identifier *, PyObject **);
  266. PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
  267. PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *);
  268. PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
  269. PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);
  270. /* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes
  271. dict as the last parameter. */
  272. PyAPI_FUNC(PyObject *)
  273. _PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *, int);
  274. PyAPI_FUNC(int)
  275. _PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
  276. PyObject *, PyObject *);
  277. #define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
  278. static inline void _Py_Dealloc_inline(PyObject *op)
  279. {
  280. destructor dealloc = Py_TYPE(op)->tp_dealloc;
  281. #ifdef Py_TRACE_REFS
  282. _Py_ForgetReference(op);
  283. #else
  284. _Py_INC_TPFREES(op);
  285. #endif
  286. (*dealloc)(op);
  287. }
  288. #define _Py_Dealloc(op) _Py_Dealloc_inline(op)
  289. /* Safely decref `op` and set `op` to `op2`.
  290. *
  291. * As in case of Py_CLEAR "the obvious" code can be deadly:
  292. *
  293. * Py_DECREF(op);
  294. * op = op2;
  295. *
  296. * The safe way is:
  297. *
  298. * Py_SETREF(op, op2);
  299. *
  300. * That arranges to set `op` to `op2` _before_ decref'ing, so that any code
  301. * triggered as a side-effect of `op` getting torn down no longer believes
  302. * `op` points to a valid object.
  303. *
  304. * Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of
  305. * Py_DECREF.
  306. */
  307. #define Py_SETREF(op, op2) \
  308. do { \
  309. PyObject *_py_tmp = _PyObject_CAST(op); \
  310. (op) = (op2); \
  311. Py_DECREF(_py_tmp); \
  312. } while (0)
  313. #define Py_XSETREF(op, op2) \
  314. do { \
  315. PyObject *_py_tmp = _PyObject_CAST(op); \
  316. (op) = (op2); \
  317. Py_XDECREF(_py_tmp); \
  318. } while (0)
  319. PyAPI_DATA(PyTypeObject) _PyNone_Type;
  320. PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type;
  321. /* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE.
  322. * Defined in object.c.
  323. */
  324. PyAPI_DATA(int) _Py_SwappedOp[];
  325. /* This is the old private API, invoked by the macros before 3.2.4.
  326. Kept for binary compatibility of extensions using the stable ABI. */
  327. PyAPI_FUNC(void) _PyTrash_deposit_object(PyObject*);
  328. PyAPI_FUNC(void) _PyTrash_destroy_chain(void);
  329. PyAPI_FUNC(void)
  330. _PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks,
  331. size_t sizeof_block);
  332. PyAPI_FUNC(void)
  333. _PyObject_DebugTypeStats(FILE *out);
  334. /* Define a pair of assertion macros:
  335. _PyObject_ASSERT_FROM(), _PyObject_ASSERT_WITH_MSG() and _PyObject_ASSERT().
  336. These work like the regular C assert(), in that they will abort the
  337. process with a message on stderr if the given condition fails to hold,
  338. but compile away to nothing if NDEBUG is defined.
  339. However, before aborting, Python will also try to call _PyObject_Dump() on
  340. the given object. This may be of use when investigating bugs in which a
  341. particular object is corrupt (e.g. buggy a tp_visit method in an extension
  342. module breaking the garbage collector), to help locate the broken objects.
  343. The WITH_MSG variant allows you to supply an additional message that Python
  344. will attempt to print to stderr, after the object dump. */
  345. #ifdef NDEBUG
  346. /* No debugging: compile away the assertions: */
  347. # define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \
  348. ((void)0)
  349. #else
  350. /* With debugging: generate checks: */
  351. # define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \
  352. ((expr) \
  353. ? (void)(0) \
  354. : _PyObject_AssertFailed((obj), Py_STRINGIFY(expr), \
  355. (msg), (filename), (lineno), (func)))
  356. #endif
  357. #define _PyObject_ASSERT_WITH_MSG(obj, expr, msg) \
  358. _PyObject_ASSERT_FROM(obj, expr, msg, __FILE__, __LINE__, __func__)
  359. #define _PyObject_ASSERT(obj, expr) \
  360. _PyObject_ASSERT_WITH_MSG(obj, expr, NULL)
  361. #define _PyObject_ASSERT_FAILED_MSG(obj, msg) \
  362. _PyObject_AssertFailed((obj), NULL, (msg), __FILE__, __LINE__, __func__)
  363. /* Declare and define _PyObject_AssertFailed() even when NDEBUG is defined,
  364. to avoid causing compiler/linker errors when building extensions without
  365. NDEBUG against a Python built with NDEBUG defined.
  366. msg, expr and function can be NULL. */
  367. PyAPI_FUNC(void) _PyObject_AssertFailed(
  368. PyObject *obj,
  369. const char *expr,
  370. const char *msg,
  371. const char *file,
  372. int line,
  373. const char *function);
  374. /* Check if an object is consistent. For example, ensure that the reference
  375. counter is greater than or equal to 1, and ensure that ob_type is not NULL.
  376. Call _PyObject_AssertFailed() if the object is inconsistent.
  377. If check_content is zero, only check header fields: reduce the overhead.
  378. The function always return 1. The return value is just here to be able to
  379. write:
  380. assert(_PyObject_CheckConsistency(obj, 1)); */
  381. PyAPI_FUNC(int) _PyObject_CheckConsistency(
  382. PyObject *op,
  383. int check_content);
  384. #ifdef __cplusplus
  385. }
  386. #endif