• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef Py_INTERNAL_CELL_H
2 #define Py_INTERNAL_CELL_H
3 
4 #include "pycore_critical_section.h"
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 #ifndef Py_BUILD_CORE
11 #  error "this header requires Py_BUILD_CORE define"
12 #endif
13 
14 // Sets the cell contents to `value` and return previous contents. Steals a
15 // reference to `value`.
16 static inline PyObject *
PyCell_SwapTakeRef(PyCellObject * cell,PyObject * value)17 PyCell_SwapTakeRef(PyCellObject *cell, PyObject *value)
18 {
19     PyObject *old_value;
20     Py_BEGIN_CRITICAL_SECTION(cell);
21     old_value = cell->ob_ref;
22     cell->ob_ref = value;
23     Py_END_CRITICAL_SECTION();
24     return old_value;
25 }
26 
27 static inline void
PyCell_SetTakeRef(PyCellObject * cell,PyObject * value)28 PyCell_SetTakeRef(PyCellObject *cell, PyObject *value)
29 {
30     PyObject *old_value = PyCell_SwapTakeRef(cell, value);
31     Py_XDECREF(old_value);
32 }
33 
34 // Gets the cell contents. Returns a new reference.
35 static inline PyObject *
PyCell_GetRef(PyCellObject * cell)36 PyCell_GetRef(PyCellObject *cell)
37 {
38     PyObject *res;
39     Py_BEGIN_CRITICAL_SECTION(cell);
40     res = Py_XNewRef(cell->ob_ref);
41     Py_END_CRITICAL_SECTION();
42     return res;
43 }
44 
45 #ifdef __cplusplus
46 }
47 #endif
48 #endif   /* !Py_INTERNAL_CELL_H */
49