Skip to content

Commit 66c2f40

Browse files
pitroublurb-it[bot]
authored andcommitted
pythongh-109599: Add types.CapsuleType (python#109600)
--------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 51cd05d commit 66c2f40

File tree

5 files changed

+21
-1
lines changed

5 files changed

+21
-1
lines changed

Doc/library/types.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,12 @@ Standard names are defined for the following types:
472472

473473
.. versionadded:: 3.12
474474

475+
.. class:: CapsuleType
476+
477+
The type of :ref:`capsule objects <capsules>`.
478+
479+
.. versionadded:: 3.13
480+
475481

476482
Additional Utility Classes and Functions
477483
----------------------------------------

Lib/test/test_sys.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import builtins
22
import codecs
3+
import _datetime
34
import gc
45
import locale
56
import operator
@@ -1581,7 +1582,7 @@ def delx(self): del self.__x
15811582
x = property(getx, setx, delx, "")
15821583
check(x, size('5Pi'))
15831584
# PyCapsule
1584-
# XXX
1585+
check(_datetime.datetime_CAPI, size('6P'))
15851586
# rangeiterator
15861587
check(iter(range(1)), size('3l'))
15871588
check(iter(range(2**65)), size('3P'))

Lib/test/test_types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import collections.abc
55
from collections import namedtuple
66
import copy
7+
import _datetime
78
import gc
89
import inspect
910
import pickle
@@ -636,6 +637,9 @@ def test_traceback_and_frame_types(self):
636637
self.assertIsInstance(exc.__traceback__, types.TracebackType)
637638
self.assertIsInstance(exc.__traceback__.tb_frame, types.FrameType)
638639

640+
def test_capsule_type(self):
641+
self.assertIsInstance(_datetime.datetime_CAPI, types.CapsuleType)
642+
639643

640644
class UnionTests(unittest.TestCase):
641645

Lib/types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Define names for built-in types that aren't directly accessible as a builtin.
33
"""
4+
45
import sys
56

67
# Iterators in Python aren't a matter of type but of protocol. A large
@@ -330,4 +331,11 @@ def wrapped(*args, **kwargs):
330331
NoneType = type(None)
331332
NotImplementedType = type(NotImplemented)
332333

334+
def __getattr__(name):
335+
if name == 'CapsuleType':
336+
import _socket
337+
return type(_socket.CAPI)
338+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
339+
333340
__all__ = [n for n in globals() if n[:1] != '_']
341+
__all__ += ['CapsuleType']
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Expose the type of PyCapsule objects as ``types.CapsuleType``.

0 commit comments

Comments
 (0)