From 563aca2a1bd03bfa7191d921a6b4d58200431080 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 20 Sep 2023 09:34:44 +0200 Subject: [PATCH 1/4] gh-109599: Add types.CapsuleType --- Doc/library/types.rst | 6 ++++++ Lib/test/test_sys.py | 3 ++- Lib/test/test_types.py | 4 ++++ Lib/types.py | 4 ++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Doc/library/types.rst b/Doc/library/types.rst index 875916be1049a3..f37e583460f4db 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -472,6 +472,12 @@ Standard names are defined for the following types: .. versionadded:: 3.12 +.. class:: CapsuleType + + The type of :ref:`capsule objects`. + + .. versionadded:: 3.13 + Additional Utility Classes and Functions ---------------------------------------- diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index f4948ceec66226..c616a27364b494 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -1,5 +1,6 @@ import builtins import codecs +import _datetime import gc import locale import operator @@ -1581,7 +1582,7 @@ def delx(self): del self.__x x = property(getx, setx, delx, "") check(x, size('5Pi')) # PyCapsule - # XXX + check(_datetime.datetime_CAPI, size('6P')) # rangeiterator check(iter(range(1)), size('3l')) check(iter(range(2**65)), size('3P')) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index c6bff79f903828..da32c4ea6477ce 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -4,6 +4,7 @@ import collections.abc from collections import namedtuple import copy +import _datetime import gc import inspect import pickle @@ -636,6 +637,9 @@ def test_traceback_and_frame_types(self): self.assertIsInstance(exc.__traceback__, types.TracebackType) self.assertIsInstance(exc.__traceback__.tb_frame, types.FrameType) + def test_capsule_type(self): + self.assertIsInstance(_datetime.datetime_CAPI, types.CapsuleType) + class UnionTests(unittest.TestCase): diff --git a/Lib/types.py b/Lib/types.py index b4aa19cec40c89..0cb626f9574d54 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -1,6 +1,8 @@ """ Define names for built-in types that aren't directly accessible as a builtin. """ + +import _socket import sys # Iterators in Python aren't a matter of type but of protocol. A large @@ -330,4 +332,6 @@ def wrapped(*args, **kwargs): NoneType = type(None) NotImplementedType = type(NotImplemented) +CapsuleType = type(_socket.CAPI) + __all__ = [n for n in globals() if n[:1] != '_'] From e88b0fdeb84e7d57e18277e696fba0a85f6bba9a Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 20 Sep 2023 07:38:18 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-09-20-07-38-14.gh-issue-109599.IaSLJz.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-09-20-07-38-14.gh-issue-109599.IaSLJz.rst diff --git a/Misc/NEWS.d/next/Library/2023-09-20-07-38-14.gh-issue-109599.IaSLJz.rst b/Misc/NEWS.d/next/Library/2023-09-20-07-38-14.gh-issue-109599.IaSLJz.rst new file mode 100644 index 00000000000000..8a15e765545f88 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-09-20-07-38-14.gh-issue-109599.IaSLJz.rst @@ -0,0 +1 @@ +Expose the type of PyCapsule objects as ``types.CapsuleType``. From 0dfea2372d6e8a16bcc08de702500e1b13a603c1 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 20 Sep 2023 09:52:52 +0200 Subject: [PATCH 3/4] Fix bootstrap import issues --- Lib/types.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/types.py b/Lib/types.py index 0cb626f9574d54..1484c22ee9dffa 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -2,7 +2,6 @@ Define names for built-in types that aren't directly accessible as a builtin. """ -import _socket import sys # Iterators in Python aren't a matter of type but of protocol. A large @@ -332,6 +331,11 @@ def wrapped(*args, **kwargs): NoneType = type(None) NotImplementedType = type(NotImplemented) -CapsuleType = type(_socket.CAPI) +def __getattr__(name): + if name == 'CapsuleType': + import _socket + return type(_socket.CAPI) + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") __all__ = [n for n in globals() if n[:1] != '_'] +__all__ += ['CapsuleType'] From 30af924a73760bd9617cffafe58aa76b12f24910 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 20 Sep 2023 10:25:24 +0200 Subject: [PATCH 4/4] Style: add space in reST markup --- Doc/library/types.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/types.rst b/Doc/library/types.rst index f37e583460f4db..54c3907dec98cc 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -474,7 +474,7 @@ Standard names are defined for the following types: .. class:: CapsuleType - The type of :ref:`capsule objects`. + The type of :ref:`capsule objects `. .. versionadded:: 3.13