-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-148829: Implement PEP 661 #148831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
gh-148829: Implement PEP 661 #148831
Changes from 11 commits
559e527
ededfb7
39f364c
0adf314
e4a106a
e5b8789
bdcb400
cc4545c
be58215
91de05b
4a970a6
9cfd101
a851235
2beb1c2
e2d92a1
53a84b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,6 +112,7 @@ Other Objects | |
| picklebuffer.rst | ||
| weakref.rst | ||
| capsule.rst | ||
| sentinel.rst | ||
| frame.rst | ||
| gen.rst | ||
| coro.rst | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,31 @@ | ||||||||||||||
| .. highlight:: c | ||||||||||||||
|
|
||||||||||||||
| .. _sentinelobjects: | ||||||||||||||
|
|
||||||||||||||
| Sentinel Objects | ||||||||||||||
| ---------------- | ||||||||||||||
|
|
||||||||||||||
| .. c:var:: PyTypeObject PySentinel_Type | ||||||||||||||
|
|
||||||||||||||
| This instance of :c:type:`PyTypeObject` represents the Python | ||||||||||||||
| :class:`sentinel` type. This is the same object as :class:`sentinel`. | ||||||||||||||
|
|
||||||||||||||
| .. versionadded:: next | ||||||||||||||
|
|
||||||||||||||
| .. 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. | ||||||||||||||
|
|
||||||||||||||
| .. versionadded:: next | ||||||||||||||
|
|
||||||||||||||
| .. c:function:: PyObject* PySentinel_New(const char *name, const char *module_name) | ||||||||||||||
|
|
||||||||||||||
| Return a new :class:`sentinel` object with :attr:`~sentinel.__name__` set to | ||||||||||||||
| *name* and :attr:`~sentinel.__module__` set to *module_name*. | ||||||||||||||
| Return ``NULL`` with an exception set on failure. | ||||||||||||||
|
|
||||||||||||||
| *module_name* should be the name of an importable module if the sentinel | ||||||||||||||
| should support pickling. | ||||||||||||||
|
Comment on lines
+28
to
+29
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding this would help people use pickle properly. People need to assign this sentinel instance to that module before |
||||||||||||||
|
|
||||||||||||||
| .. versionadded:: next | ||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,13 +19,13 @@ are always available. They are listed here in alphabetical order. | |||||||||||||||||
| | | :func:`ascii` | | :func:`filter` | | :func:`map` | | **S** | | ||||||||||||||||||
| | | | | :func:`float` | | :func:`max` | | |func-set|_ | | ||||||||||||||||||
| | | **B** | | :func:`format` | | |func-memoryview|_ | | :func:`setattr` | | ||||||||||||||||||
| | | :func:`bin` | | |func-frozenset|_ | | :func:`min` | | :func:`slice` | | ||||||||||||||||||
| | | :func:`bool` | | | | | | :func:`sorted` | | ||||||||||||||||||
| | | :func:`breakpoint` | | **G** | | **N** | | :func:`staticmethod` | | ||||||||||||||||||
| | | |func-bytearray|_ | | :func:`getattr` | | :func:`next` | | |func-str|_ | | ||||||||||||||||||
| | | |func-bytes|_ | | :func:`globals` | | | | :func:`sum` | | ||||||||||||||||||
| | | | | | | **O** | | :func:`super` | | ||||||||||||||||||
| | | **C** | | **H** | | :func:`object` | | | | ||||||||||||||||||
| | | :func:`bin` | | |func-frozenset|_ | | :func:`min` | | :func:`sentinel` | | ||||||||||||||||||
| | | :func:`bool` | | | | | | :func:`slice` | | ||||||||||||||||||
| | | :func:`breakpoint` | | **G** | | **N** | | :func:`sorted` | | ||||||||||||||||||
| | | |func-bytearray|_ | | :func:`getattr` | | :func:`next` | | :func:`staticmethod` | | ||||||||||||||||||
| | | |func-bytes|_ | | :func:`globals` | | | | |func-str|_ | | ||||||||||||||||||
| | | | | | | **O** | | :func:`sum` | | ||||||||||||||||||
| | | **C** | | **H** | | :func:`object` | | :func:`super` | | ||||||||||||||||||
| | | :func:`callable` | | :func:`hasattr` | | :func:`oct` | | **T** | | ||||||||||||||||||
| | | :func:`chr` | | :func:`hash` | | :func:`open` | | |func-tuple|_ | | ||||||||||||||||||
| | | :func:`classmethod` | | :func:`help` | | :func:`ord` | | :func:`type` | | ||||||||||||||||||
|
|
@@ -1827,6 +1827,45 @@ are always available. They are listed here in alphabetical order. | |||||||||||||||||
| :func:`setattr`. | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| .. class:: sentinel(name, /) | ||||||||||||||||||
|
|
||||||||||||||||||
| Return a new unique sentinel object. *name* must be a :class:`str`, and is | ||||||||||||||||||
| used as the returned object's representation:: | ||||||||||||||||||
|
|
||||||||||||||||||
| >>> MISSING = sentinel("MISSING") | ||||||||||||||||||
| >>> MISSING | ||||||||||||||||||
| MISSING | ||||||||||||||||||
|
|
||||||||||||||||||
| Sentinel objects are truthy and compare equal only to themselves. They are | ||||||||||||||||||
| intended to be compared with the :keyword:`is` operator. | ||||||||||||||||||
|
|
||||||||||||||||||
| Shallow and deep copies of a sentinel object return the object itself. | ||||||||||||||||||
|
|
||||||||||||||||||
| Sentinels importable from their defining module by name preserve their | ||||||||||||||||||
| identity when pickled and unpickled. Sentinels that are not importable by | ||||||||||||||||||
| module and name are not picklable. | ||||||||||||||||||
|
Comment on lines
+1844
to
+1846
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| Sentinels are conventionally assigned to a variable with a matching name. | ||||||||||||||||||
| Sentinels defined in this way can be used in :term:`type hints<type hint>`:: | ||||||||||||||||||
|
|
||||||||||||||||||
| MISSING = sentinel("MISSING") | ||||||||||||||||||
|
|
||||||||||||||||||
| def next_value(default: int | MISSING = MISSING): | ||||||||||||||||||
| ... | ||||||||||||||||||
|
|
||||||||||||||||||
| Sentinel objects support the :ref:`| <bitwise>` operator for use in type expressions. | ||||||||||||||||||
|
|
||||||||||||||||||
| .. attribute:: __name__ | ||||||||||||||||||
|
|
||||||||||||||||||
| The sentinel's name. | ||||||||||||||||||
|
|
||||||||||||||||||
| .. attribute:: __module__ | ||||||||||||||||||
|
|
||||||||||||||||||
| The name of the module where the sentinel was created. | ||||||||||||||||||
|
|
||||||||||||||||||
| .. versionadded:: next | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| .. class:: slice(stop, /) | ||||||||||||||||||
| slice(start, stop, step=None, /) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* Sentinel object interface */ | ||
|
|
||
| #ifndef Py_SENTINELOBJECT_H | ||
| #define Py_SENTINELOBJECT_H | ||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| #ifndef Py_LIMITED_API | ||
| PyAPI_DATA(PyTypeObject) PySentinel_Type; | ||
|
|
||
| #define PySentinel_Check(op) Py_IS_TYPE((op), &PySentinel_Type) | ||
|
|
||
| PyAPI_FUNC(PyObject *) PySentinel_New( | ||
| const char *name, | ||
| const char *module_name); | ||
| #endif | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
| #endif /* !Py_SENTINELOBJECT_H */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add :class:`sentinel`, implementing :pep:`661`. PEP by Tal Einat; patch by | ||
| Jelle Zijlstra. |
Uh oh!
There was an error while loading. Please reload this page.