FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-148829: Add PySentinel_CheckExact() by scoder · Pull Request #149725 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .py  (1) .rst  (2) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 14 additions & 2 deletions Doc/c-api/sentinel.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@ 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 does not currently allow subclasses,
so this check is exact.
Future Python versions may choose to allow subtyping.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Can you add "This function always succeeds." to both functions?

This function always succeeds.

.. 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 Python versions may choose to allow subtyping.
This function always succeeds.

.. versionadded:: 3.15

Expand Down
5 changes: 4 additions & 1 deletion Include/cpython/sentinelobject.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :c:func:`PySentinel_CheckExact` for exact :class:`sentinel` type tests
to accompany the existing :c:func:`PySentinel_Check`.
7 changes: 7 additions & 0 deletions Modules/_testcapi/object.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL