Skip to content
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

GH-118761: Expose more core interpreter types in _types #132103

Merged
merged 10 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
run_with_locale, cpython_only, no_rerun,
MISSING_C_DOCSTRINGS,
)
from test.support.import_helper import import_fresh_module

import collections.abc
from collections import namedtuple, UserDict
import copy
Expand Down Expand Up @@ -34,6 +36,17 @@ def clear_typing_caches():

class TypesTests(unittest.TestCase):

def test_names(self):
c_only_names = {'CapsuleType'}
ignored = {'new_class', 'resolve_bases', 'prepare_class',
'get_original_bases', 'DynamicClassAttribute', 'coroutine'}

c_types = import_fresh_module('types', fresh=['_types'])
py_types = import_fresh_module('types', blocked=['_types'])
for name in c_types.__all__:
if name not in c_only_names | ignored:
self.assertIs(getattr(c_types, name), getattr(py_types, name))

def test_truth_values(self):
if None: self.fail('None is true instead of false')
if 0: self.fail('0 is true instead of false')
Expand Down
118 changes: 61 additions & 57 deletions Lib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
try:
from _types import *
except ImportError:
import sys

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)

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(sys)

WrapperDescriptorType = type(object.__init__)
MethodWrapperType = type(object().__str__)
MethodDescriptorType = type(str.join)
ClassMethodDescriptorType = type(dict.__dict__['fromkeys'])
try:
raise TypeError
except TypeError as exc:
TracebackType = type(exc.__traceback__)
FrameType = type(exc.__traceback__.tb_frame)

ModuleType = type(_types)
GetSetDescriptorType = type(FunctionType.__code__)
MemberDescriptorType = type(FunctionType.__globals__)

try:
raise TypeError
except TypeError as exc:
TracebackType = type(exc.__traceback__)
FrameType = type(exc.__traceback__.tb_frame)
GenericAlias = type(list[int])
UnionType = type(int | str)

GetSetDescriptorType = type(FunctionType.__code__)
MemberDescriptorType = type(FunctionType.__globals__)
EllipsisType = type(Ellipsis)
NoneType = type(None)
NotImplementedType = type(NotImplemented)

CapsuleType = _types.CapsuleType
# CapsuleType cannot be accessed from pure Python,
# so there is no fallback definition.

del _types, _f, _g, _C, _c, _ag, _cell_factory # Not for export
del sys, _f, _g, _C, _c, _ag, _cell_factory # Not for export


# Provide a PEP 3115 compliant mechanism for class creation
Expand Down Expand Up @@ -326,11 +337,4 @@ def wrapped(*args, **kwargs):

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
46 changes: 40 additions & 6 deletions Modules/_typesmodule.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
/* _types module */

#include "Python.h"
#include "pycore_descrobject.h" // _PyMethodWrapper_Type
#include "pycore_namespace.h" // _PyNamespace_Type
#include "pycore_object.h" // _PyNone_Type, _PyNotImplemented_Type
#include "pycore_unionobject.h" // _PyUnion_Type

static int
_types_exec(PyObject *m)
{
if (PyModule_AddObjectRef(m, "CapsuleType", (PyObject *)&PyCapsule_Type) < 0) {
return -1;
}
if (PyModule_AddObjectRef(m, "SimpleNamespace", (PyObject *)&_PyNamespace_Type) < 0) {
return -1;
}
#define EXPORT_STATIC_TYPE(NAME, TYPE) \
do { \
if (PyModule_AddObjectRef(m, (NAME), (PyObject *)&(TYPE)) < 0) { \
return -1; \
} \
} while (0)

EXPORT_STATIC_TYPE("AsyncGeneratorType", PyAsyncGen_Type);
EXPORT_STATIC_TYPE("BuiltinFunctionType", PyCFunction_Type);
// Same as BuiltinFunctionType
EXPORT_STATIC_TYPE("BuiltinMethodType", PyCFunction_Type);
EXPORT_STATIC_TYPE("CapsuleType", PyCapsule_Type);
EXPORT_STATIC_TYPE("CellType", PyCell_Type);
EXPORT_STATIC_TYPE("ClassMethodDescriptorType", PyClassMethodDescr_Type);
EXPORT_STATIC_TYPE("CodeType", PyCode_Type);
EXPORT_STATIC_TYPE("CoroutineType", PyCoro_Type);
EXPORT_STATIC_TYPE("EllipsisType", PyEllipsis_Type);
EXPORT_STATIC_TYPE("FrameType", PyFrame_Type);
EXPORT_STATIC_TYPE("FunctionType", PyFunction_Type);
EXPORT_STATIC_TYPE("GeneratorType", PyGen_Type);
EXPORT_STATIC_TYPE("GenericAlias", Py_GenericAliasType);
EXPORT_STATIC_TYPE("GetSetDescriptorType", PyGetSetDescr_Type);
// Same as FunctionType
EXPORT_STATIC_TYPE("LambdaType", PyFunction_Type);
EXPORT_STATIC_TYPE("MappingProxyType", PyDictProxy_Type);
EXPORT_STATIC_TYPE("MemberDescriptorType", PyMemberDescr_Type);
EXPORT_STATIC_TYPE("MethodDescriptorType", PyMethodDescr_Type);
EXPORT_STATIC_TYPE("MethodType", PyMethod_Type);
EXPORT_STATIC_TYPE("MethodWrapperType", _PyMethodWrapper_Type);
EXPORT_STATIC_TYPE("ModuleType", PyModule_Type);
EXPORT_STATIC_TYPE("NoneType", _PyNone_Type);
EXPORT_STATIC_TYPE("NotImplementedType", _PyNotImplemented_Type);
EXPORT_STATIC_TYPE("SimpleNamespace", _PyNamespace_Type);
EXPORT_STATIC_TYPE("TracebackType", PyTraceBack_Type);
EXPORT_STATIC_TYPE("UnionType", _PyUnion_Type);
EXPORT_STATIC_TYPE("WrapperDescriptorType", PyWrapperDescr_Type);
#undef EXPORT_STATIC_TYPE
return 0;
}

Expand Down
Loading