mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
GH-118761: Expose more core interpreter types in `_types
` (#132103)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
This commit is contained in:
parent
92fb949eac
commit
1755157207
3 changed files with 130 additions and 65 deletions
122
Lib/types.py
122
Lib/types.py
|
@ -2,67 +2,78 @@
|
|||
Define names for built-in types that aren't directly accessible as a builtin.
|
||||
"""
|
||||
|
||||
import _types
|
||||
|
||||
# Iterators in Python aren't a matter of type but of protocol. A large
|
||||
# and changing number of builtin types implement *some* flavor of
|
||||
# iterator. Don't check the type! Use hasattr to check for both
|
||||
# "__iter__" and "__next__" attributes instead.
|
||||
|
||||
def _f(): pass
|
||||
FunctionType = type(_f)
|
||||
LambdaType = type(lambda: None) # Same as FunctionType
|
||||
CodeType = type(_f.__code__)
|
||||
MappingProxyType = type(type.__dict__)
|
||||
SimpleNamespace = _types.SimpleNamespace
|
||||
|
||||
def _cell_factory():
|
||||
a = 1
|
||||
def f():
|
||||
nonlocal a
|
||||
return f.__closure__[0]
|
||||
CellType = type(_cell_factory())
|
||||
|
||||
def _g():
|
||||
yield 1
|
||||
GeneratorType = type(_g())
|
||||
|
||||
async def _c(): pass
|
||||
_c = _c()
|
||||
CoroutineType = type(_c)
|
||||
_c.close() # Prevent ResourceWarning
|
||||
|
||||
async def _ag():
|
||||
yield
|
||||
_ag = _ag()
|
||||
AsyncGeneratorType = type(_ag)
|
||||
|
||||
class _C:
|
||||
def _m(self): pass
|
||||
MethodType = type(_C()._m)
|
||||
|
||||
BuiltinFunctionType = type(len)
|
||||
BuiltinMethodType = type([].append) # Same as BuiltinFunctionType
|
||||
|
||||
WrapperDescriptorType = type(object.__init__)
|
||||
MethodWrapperType = type(object().__str__)
|
||||
MethodDescriptorType = type(str.join)
|
||||
ClassMethodDescriptorType = type(dict.__dict__['fromkeys'])
|
||||
|
||||
ModuleType = type(_types)
|
||||
|
||||
try:
|
||||
raise TypeError
|
||||
except TypeError as exc:
|
||||
TracebackType = type(exc.__traceback__)
|
||||
FrameType = type(exc.__traceback__.tb_frame)
|
||||
from _types import *
|
||||
except ImportError:
|
||||
import sys
|
||||
|
||||
GetSetDescriptorType = type(FunctionType.__code__)
|
||||
MemberDescriptorType = type(FunctionType.__globals__)
|
||||
def _f(): pass
|
||||
FunctionType = type(_f)
|
||||
LambdaType = type(lambda: None) # Same as FunctionType
|
||||
CodeType = type(_f.__code__)
|
||||
MappingProxyType = type(type.__dict__)
|
||||
SimpleNamespace = type(sys.implementation)
|
||||
|
||||
CapsuleType = _types.CapsuleType
|
||||
def _cell_factory():
|
||||
a = 1
|
||||
def f():
|
||||
nonlocal a
|
||||
return f.__closure__[0]
|
||||
CellType = type(_cell_factory())
|
||||
|
||||
del _types, _f, _g, _C, _c, _ag, _cell_factory # Not for export
|
||||
def _g():
|
||||
yield 1
|
||||
GeneratorType = type(_g())
|
||||
|
||||
async def _c(): pass
|
||||
_c = _c()
|
||||
CoroutineType = type(_c)
|
||||
_c.close() # Prevent ResourceWarning
|
||||
|
||||
async def _ag():
|
||||
yield
|
||||
_ag = _ag()
|
||||
AsyncGeneratorType = type(_ag)
|
||||
|
||||
class _C:
|
||||
def _m(self): pass
|
||||
MethodType = type(_C()._m)
|
||||
|
||||
BuiltinFunctionType = type(len)
|
||||
BuiltinMethodType = type([].append) # Same as BuiltinFunctionType
|
||||
|
||||
WrapperDescriptorType = type(object.__init__)
|
||||
MethodWrapperType = type(object().__str__)
|
||||
MethodDescriptorType = type(str.join)
|
||||
ClassMethodDescriptorType = type(dict.__dict__['fromkeys'])
|
||||
|
||||
ModuleType = type(sys)
|
||||
|
||||
try:
|
||||
raise TypeError
|
||||
except TypeError as exc:
|
||||
TracebackType = type(exc.__traceback__)
|
||||
FrameType = type(exc.__traceback__.tb_frame)
|
||||
|
||||
GetSetDescriptorType = type(FunctionType.__code__)
|
||||
MemberDescriptorType = type(FunctionType.__globals__)
|
||||
|
||||
GenericAlias = type(list[int])
|
||||
UnionType = type(int | str)
|
||||
|
||||
EllipsisType = type(Ellipsis)
|
||||
NoneType = type(None)
|
||||
NotImplementedType = type(NotImplemented)
|
||||
|
||||
# CapsuleType cannot be accessed from pure Python,
|
||||
# so there is no fallback definition.
|
||||
|
||||
del sys, _f, _g, _C, _c, _ag, _cell_factory # Not for export
|
||||
|
||||
|
||||
# Provide a PEP 3115 compliant mechanism for class creation
|
||||
|
@ -326,11 +337,4 @@ def coroutine(func):
|
|||
|
||||
return wrapped
|
||||
|
||||
GenericAlias = type(list[int])
|
||||
UnionType = type(int | str)
|
||||
|
||||
EllipsisType = type(Ellipsis)
|
||||
NoneType = type(None)
|
||||
NotImplementedType = type(NotImplemented)
|
||||
|
||||
__all__ = [n for n in globals() if not n.startswith('_')] # for pydoc
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue