Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Doc/c-api/sentinel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ Sentinel objects

.. c:function:: int PySentinel_Check(PyObject *o)

Return true if *o* is a :class:`sentinel` object. The :class:`sentinel` type
does not allow subclasses, so this check is exact.
Return true if *o* is a :class:`sentinel` object or a subtype. The :class:`sentinel` type
Comment thread
scoder marked this conversation as resolved.
Outdated
does not currently allow subclasses, so this check is exact.
Future CPython versions may choose to allow subtyping.
Comment thread
scoder marked this conversation as resolved.
Outdated

.. versionadded:: 3.15

.. c:function:: int PySentinel_CheckExact(PyObject *o)

Return true if *o* is a :class:`sentinel` object, but not a subtype.
The :class:`sentinel` type does not currently allow subclasses.
Future CPython versions may choose to allow subtyping.
Comment thread
scoder marked this conversation as resolved.
Outdated

.. versionadded:: 3.15

Expand Down
5 changes: 4 additions & 1 deletion Include/cpython/sentinelobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ extern "C" {

PyAPI_DATA(PyTypeObject) PySentinel_Type;

#define PySentinel_Check(op) Py_IS_TYPE((op), &PySentinel_Type)
#define PySentinel_CheckExact(op) Py_IS_TYPE((op), &PySentinel_Type)

/* Alias as long as subclasses are not allowed. */
#define PySentinel_Check(op) PySentinel_CheckExact(op)

PyAPI_FUNC(PyObject *) PySentinel_New(
const char *name,
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_capi/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def test_pysentinel_new(self):
self.assertIs(type(marker), sentinel)
self.assertTrue(_testcapi.pysentinel_check(marker))
self.assertFalse(_testcapi.pysentinel_check(object()))
self.assertTrue(_testcapi.pysentinel_checkexact(marker))
self.assertFalse(_testcapi.pysentinel_checkexact(object()))
self.assertEqual(marker.__name__, "CAPI_SENTINEL")
self.assertEqual(marker.__module__, __name__)
self.assertEqual(repr(marker), "CAPI_SENTINEL")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add PySentinel_CheckExact() for exact type tests
to accompany the existing PySentinel_Check().
Comment thread
scoder marked this conversation as resolved.
Outdated
7 changes: 7 additions & 0 deletions Modules/_testcapi/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ pysentinel_check(PyObject *self, PyObject *obj)
return PyBool_FromLong(PySentinel_Check(obj));
}

static PyObject *
pysentinel_checkexact(PyObject *self, PyObject *obj)
{
return PyBool_FromLong(PySentinel_CheckExact(obj));
}


static PyMethodDef test_methods[] = {
{"call_pyobject_print", call_pyobject_print, METH_VARARGS},
Expand Down Expand Up @@ -604,6 +610,7 @@ static PyMethodDef test_methods[] = {
{"pyobject_dump", pyobject_dump, METH_VARARGS},
{"pysentinel_new", pysentinel_new, METH_VARARGS},
{"pysentinel_check", pysentinel_check, METH_O},
{"pysentinel_checkexact", pysentinel_checkexact, METH_O},
{NULL},
};

Expand Down
Loading