Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 2 | |
| 3 | .. _cell-objects: |
| 4 | |
| 5 | Cell Objects |
| 6 | ------------ |
| 7 | |
| 8 | "Cell" objects are used to implement variables referenced by multiple scopes. |
| 9 | For each such variable, a cell object is created to store the value; the local |
| 10 | variables of each stack frame that references the value contains a reference to |
| 11 | the cells from outer scopes which also use that variable. When the value is |
| 12 | accessed, the value contained in the cell is used instead of the cell object |
| 13 | itself. This de-referencing of the cell object requires support from the |
| 14 | generated byte-code; these are not automatically de-referenced when accessed. |
| 15 | Cell objects are not likely to be useful elsewhere. |
| 16 | |
| 17 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 18 | .. c:type:: PyCellObject |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 19 | |
| 20 | The C structure used for cell objects. |
| 21 | |
| 22 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 23 | .. c:var:: PyTypeObject PyCell_Type |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 24 | |
| 25 | The type object corresponding to cell objects. |
| 26 | |
| 27 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 28 | .. c:function:: int PyCell_Check(ob) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 29 | |
Antonio Cuni | 315fc52 | 2021-01-06 12:38:26 +0100 | [diff] [blame] | 30 | Return true if *ob* is a cell object; *ob* must not be ``NULL``. This |
| 31 | function always succeeds. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 32 | |
| 33 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 34 | .. c:function:: PyObject* PyCell_New(PyObject *ob) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 35 | |
| 36 | Create and return a new cell object containing the value *ob*. The parameter may |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 37 | be ``NULL``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 38 | |
| 39 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 40 | .. c:function:: PyObject* PyCell_Get(PyObject *cell) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 41 | |
| 42 | Return the contents of the cell *cell*. |
| 43 | |
| 44 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 45 | .. c:function:: PyObject* PyCell_GET(PyObject *cell) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 46 | |
| 47 | Return the contents of the cell *cell*, but without checking that *cell* is |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 48 | non-``NULL`` and a cell object. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 49 | |
| 50 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 51 | .. c:function:: int PyCell_Set(PyObject *cell, PyObject *value) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 52 | |
| 53 | Set the contents of the cell object *cell* to *value*. This releases the |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 54 | reference to any current content of the cell. *value* may be ``NULL``. *cell* |
| 55 | must be non-``NULL``; if it is not a cell object, ``-1`` will be returned. On |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 56 | success, ``0`` will be returned. |
| 57 | |
| 58 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 59 | .. c:function:: void PyCell_SET(PyObject *cell, PyObject *value) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 60 | |
| 61 | Sets the value of the cell object *cell* to *value*. No reference counts are |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 62 | adjusted, and no checks are made for safety; *cell* must be non-``NULL`` and must |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 63 | be a cell object. |