File tree Expand file tree Collapse file tree 4 files changed +41
-4
lines changed Expand file tree Collapse file tree 4 files changed +41
-4
lines changed Original file line number Diff line number Diff line change @@ -510,6 +510,9 @@ Callable types
510
510
| :attr: `__closure__ ` | ``None `` or a tuple of cells | Read-only |
511
511
| | that contain bindings for the | |
512
512
| | function's free variables. | |
513
+ | | See below for information on | |
514
+ | | the ``cell_contents `` | |
515
+ | | attribute. | |
513
516
+-------------------------+-------------------------------+-----------+
514
517
| :attr: `__annotations__ ` | A dict containing annotations | Writable |
515
518
| | of parameters. The keys of | |
@@ -530,6 +533,9 @@ Callable types
530
533
implementation only supports function attributes on user-defined functions.
531
534
Function attributes on built-in functions may be supported in the future. *
532
535
536
+ A cell object has the attribute ``cell_contents ``. This can be used to get
537
+ the value of the cell, as well as set the value.
538
+
533
539
Additional information about a function's definition can be retrieved from its
534
540
code object; see the description of internal types below.
535
541
Original file line number Diff line number Diff line change @@ -93,6 +93,26 @@ def f(): print(a)
93
93
self .fail ("shouldn't be able to read an empty cell" )
94
94
a = 12
95
95
96
+ def test_set_cell (self ):
97
+ a = 12
98
+ def f (): return a
99
+ c = f .__closure__
100
+ c [0 ].cell_contents = 9
101
+ self .assertEqual (c [0 ].cell_contents , 9 )
102
+ self .assertEqual (f (), 9 )
103
+ self .assertEqual (a , 9 )
104
+ del c [0 ].cell_contents
105
+ try :
106
+ c [0 ].cell_contents
107
+ except ValueError :
108
+ pass
109
+ else :
110
+ self .fail ("shouldn't be able to read an empty cell" )
111
+ with self .assertRaises (NameError ):
112
+ f ()
113
+ with self .assertRaises (UnboundLocalError ):
114
+ print (a )
115
+
96
116
def test___name__ (self ):
97
117
self .assertEqual (self .b .__name__ , 'b' )
98
118
self .b .__name__ = 'c'
Original file line number Diff line number Diff line change @@ -10,6 +10,11 @@ What's New in Python 3.7.0 alpha 1?
10
10
Core and Builtins
11
11
-----------------
12
12
13
+ - bpo-30486: Allows setting cell values for __closure__. Patch by Lisa Roach.
14
+
15
+ - bpo-30537: itertools.islice now accepts integer-like objects (having
16
+ an __index__ method) as start, stop, and slice arguments
17
+
13
18
- bpo-25324: Tokens needed for parsing in Python moved to C. ``COMMENT``,
14
19
``NL`` and ``ENCODING``. This way the tokens and tok_names in the token
15
20
module don't get changed when you import the tokenize module.
@@ -128,9 +133,6 @@ Core and Builtins
128
133
129
134
- bpo-29546: Improve from-import error message with location
130
135
131
- - bpo-30537: itertools.islice now accepts integer-like objects (having
132
- an __index__ method) as start, stop, and slice arguments
133
-
134
136
- Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0].
135
137
136
138
- Issue #29337: Fixed possible BytesWarning when compare the code objects.
Original file line number Diff line number Diff line change @@ -140,8 +140,17 @@ cell_get_contents(PyCellObject *op, void *closure)
140
140
return op -> ob_ref ;
141
141
}
142
142
143
+ int
144
+ cell_set_contents (PyCellObject * op , PyObject * obj )
145
+ {
146
+ Py_XINCREF (obj );
147
+ Py_XSETREF (op -> ob_ref , obj );
148
+ return 0 ;
149
+ }
150
+
143
151
static PyGetSetDef cell_getsetlist [] = {
144
- {"cell_contents" , (getter )cell_get_contents , NULL },
152
+ {"cell_contents" , (getter )cell_get_contents ,
153
+ (setter )cell_set_contents , NULL },
145
154
{NULL } /* sentinel */
146
155
};
147
156
You can’t perform that action at this time.
0 commit comments