Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
df8d2cde
Commit
df8d2cde
authored
Feb 07, 2019
by
Pierre Glaser
Committed by
Antoine Pitrou
Feb 07, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-35911: add cell constructor (GH-11771)
Add a cell constructor, expose the cell type in the types module.
parent
f289084c
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
71 additions
and
2 deletions
+71
-2
Doc/library/types.rst
Doc/library/types.rst
+8
-0
Doc/reference/datamodel.rst
Doc/reference/datamodel.rst
+3
-1
Lib/test/test_funcattrs.py
Lib/test/test_funcattrs.py
+9
-0
Lib/types.py
Lib/types.py
+7
-0
Misc/NEWS.d/next/Core and Builtins/2019-02-06-17-50-59.bpo-35911.oiWE8.rst
...Core and Builtins/2019-02-06-17-50-59.bpo-35911.oiWE8.rst
+3
-0
Objects/cellobject.c
Objects/cellobject.c
+41
-1
No files found.
Doc/library/types.rst
View file @
df8d2cde
...
@@ -136,6 +136,14 @@ Standard names are defined for the following types:
...
@@ -136,6 +136,14 @@ Standard names are defined for the following types:
The type for code objects such as returned by :func:`compile`.
The type for code objects such as returned by :func:`compile`.
.. data:: CellType
The type for cell objects: such objects are used as containers for
a function's free variables.
.. versionadded:: 3.8
.. data:: MethodType
.. data:: MethodType
The type of methods of user-defined class instances.
The type of methods of user-defined class instances.
...
...
Doc/reference/datamodel.rst
View file @
df8d2cde
...
@@ -539,7 +539,9 @@ Callable types
...
@@ -539,7 +539,9 @@ Callable types
the value of the cell, as well as set the value.
the value of the cell, as well as set the value.
Additional information about a function's definition can be retrieved from its
Additional information about a function's definition can be retrieved from its
code object; see the description of internal types below.
code object; see the description of internal types below. The
:data:`cell <types.CellType>` type can be accessed in the :mod:`types`
module.
Instance methods
Instance methods
.. index::
.. index::
...
...
Lib/test/test_funcattrs.py
View file @
df8d2cde
...
@@ -83,6 +83,15 @@ class FunctionPropertiesTest(FuncAttrsTest):
...
@@ -83,6 +83,15 @@ class FunctionPropertiesTest(FuncAttrsTest):
self
.
assertEqual
(
c
[
0
].
__class__
.
__name__
,
"cell"
)
self
.
assertEqual
(
c
[
0
].
__class__
.
__name__
,
"cell"
)
self
.
cannot_set_attr
(
f
,
"__closure__"
,
c
,
AttributeError
)
self
.
cannot_set_attr
(
f
,
"__closure__"
,
c
,
AttributeError
)
def
test_cell_new
(
self
):
cell_obj
=
types
.
CellType
(
1
)
self
.
assertEqual
(
cell_obj
.
cell_contents
,
1
)
cell_obj
=
types
.
CellType
()
msg
=
"shouldn't be able to read an empty cell"
with
self
.
assertRaises
(
ValueError
,
msg
=
msg
):
cell_obj
.
cell_contents
def
test_empty_cell
(
self
):
def
test_empty_cell
(
self
):
def
f
():
print
(
a
)
def
f
():
print
(
a
)
try
:
try
:
...
...
Lib/types.py
View file @
df8d2cde
...
@@ -15,6 +15,13 @@ CodeType = type(_f.__code__)
...
@@ -15,6 +15,13 @@ CodeType = type(_f.__code__)
MappingProxyType
=
type
(
type
.
__dict__
)
MappingProxyType
=
type
(
type
.
__dict__
)
SimpleNamespace
=
type
(
sys
.
implementation
)
SimpleNamespace
=
type
(
sys
.
implementation
)
def
_cell_factory
():
a
=
1
def
f
():
nonlocal
a
return
f
.
__closure__
[
0
]
CellType
=
type
(
_cell_factory
())
def
_g
():
def
_g
():
yield
1
yield
1
GeneratorType
=
type
(
_g
())
GeneratorType
=
type
(
_g
())
...
...
Misc/NEWS.d/next/Core and Builtins/2019-02-06-17-50-59.bpo-35911.oiWE8.rst
0 → 100644
View file @
df8d2cde
Enable the creation of cell objects by adding a ``cell.__new__`` method, and
expose the type ``cell`` in ``Lib/types.py`` under the name CellType. Patch by
Pierre Glaser.
Objects/cellobject.c
View file @
df8d2cde
...
@@ -20,6 +20,37 @@ PyCell_New(PyObject *obj)
...
@@ -20,6 +20,37 @@ PyCell_New(PyObject *obj)
return
(
PyObject
*
)
op
;
return
(
PyObject
*
)
op
;
}
}
PyDoc_STRVAR
(
cell_new_doc
,
"cell([contents])
\n
"
"--
\n
"
"
\n
"
"Create a new cell object.
\n
"
"
\n
"
" contents
\n
"
" the contents of the cell. If not specified, the cell will be empty,
\n
"
" and
\n
further attempts to access its cell_contents attribute will
\n
"
" raise a ValueError."
);
static
PyObject
*
cell_new
(
PyTypeObject
*
type
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
PyObject
*
return_value
=
NULL
;
PyObject
*
obj
=
NULL
;
if
(
!
_PyArg_NoKeywords
(
"cell"
,
kwargs
))
{
goto
exit
;
}
/* min = 0: we allow the cell to be empty */
if
(
!
PyArg_UnpackTuple
(
args
,
"cell"
,
0
,
1
,
&
obj
))
{
goto
exit
;
}
return_value
=
PyCell_New
(
obj
);
exit:
return
return_value
;
}
PyObject
*
PyObject
*
PyCell_Get
(
PyObject
*
op
)
PyCell_Get
(
PyObject
*
op
)
{
{
...
@@ -146,7 +177,7 @@ PyTypeObject PyCell_Type = {
...
@@ -146,7 +177,7 @@ PyTypeObject PyCell_Type = {
0
,
/* tp_setattro */
0
,
/* tp_setattro */
0
,
/* tp_as_buffer */
0
,
/* tp_as_buffer */
Py_TPFLAGS_DEFAULT
|
Py_TPFLAGS_HAVE_GC
,
/* tp_flags */
Py_TPFLAGS_DEFAULT
|
Py_TPFLAGS_HAVE_GC
,
/* tp_flags */
0
,
/* tp_doc */
cell_new_doc
,
/* tp_doc */
(
traverseproc
)
cell_traverse
,
/* tp_traverse */
(
traverseproc
)
cell_traverse
,
/* tp_traverse */
(
inquiry
)
cell_clear
,
/* tp_clear */
(
inquiry
)
cell_clear
,
/* tp_clear */
cell_richcompare
,
/* tp_richcompare */
cell_richcompare
,
/* tp_richcompare */
...
@@ -156,4 +187,13 @@ PyTypeObject PyCell_Type = {
...
@@ -156,4 +187,13 @@ PyTypeObject PyCell_Type = {
0
,
/* tp_methods */
0
,
/* tp_methods */
0
,
/* tp_members */
0
,
/* tp_members */
cell_getsetlist
,
/* tp_getset */
cell_getsetlist
,
/* tp_getset */
0
,
/* tp_base */
0
,
/* tp_dict */
0
,
/* tp_descr_get */
0
,
/* tp_descr_set */
0
,
/* tp_dictoffset */
0
,
/* tp_init */
0
,
/* tp_alloc */
(
newfunc
)
cell_new
,
/* tp_new */
0
,
/* tp_free */
};
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment