mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
gh-109599: Expose CapsuleType
via the _types
module (#131969)
This commit is contained in:
parent
7473c600a5
commit
231a50fa9a
10 changed files with 82 additions and 12 deletions
19
Lib/types.py
19
Lib/types.py
|
@ -2,7 +2,7 @@
|
||||||
Define names for built-in types that aren't directly accessible as a builtin.
|
Define names for built-in types that aren't directly accessible as a builtin.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
import _types
|
||||||
|
|
||||||
# Iterators in Python aren't a matter of type but of protocol. A large
|
# Iterators in Python aren't a matter of type but of protocol. A large
|
||||||
# and changing number of builtin types implement *some* flavor of
|
# and changing number of builtin types implement *some* flavor of
|
||||||
|
@ -14,7 +14,7 @@ FunctionType = type(_f)
|
||||||
LambdaType = type(lambda: None) # Same as FunctionType
|
LambdaType = type(lambda: None) # Same as FunctionType
|
||||||
CodeType = type(_f.__code__)
|
CodeType = type(_f.__code__)
|
||||||
MappingProxyType = type(type.__dict__)
|
MappingProxyType = type(type.__dict__)
|
||||||
SimpleNamespace = type(sys.implementation)
|
SimpleNamespace = _types.SimpleNamespace
|
||||||
|
|
||||||
def _cell_factory():
|
def _cell_factory():
|
||||||
a = 1
|
a = 1
|
||||||
|
@ -49,7 +49,7 @@ MethodWrapperType = type(object().__str__)
|
||||||
MethodDescriptorType = type(str.join)
|
MethodDescriptorType = type(str.join)
|
||||||
ClassMethodDescriptorType = type(dict.__dict__['fromkeys'])
|
ClassMethodDescriptorType = type(dict.__dict__['fromkeys'])
|
||||||
|
|
||||||
ModuleType = type(sys)
|
ModuleType = type(_types)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
raise TypeError
|
raise TypeError
|
||||||
|
@ -60,7 +60,9 @@ except TypeError as exc:
|
||||||
GetSetDescriptorType = type(FunctionType.__code__)
|
GetSetDescriptorType = type(FunctionType.__code__)
|
||||||
MemberDescriptorType = type(FunctionType.__globals__)
|
MemberDescriptorType = type(FunctionType.__globals__)
|
||||||
|
|
||||||
del sys, _f, _g, _C, _c, _ag, _cell_factory # Not for export
|
CapsuleType = _types.CapsuleType
|
||||||
|
|
||||||
|
del _types, _f, _g, _C, _c, _ag, _cell_factory # Not for export
|
||||||
|
|
||||||
|
|
||||||
# Provide a PEP 3115 compliant mechanism for class creation
|
# Provide a PEP 3115 compliant mechanism for class creation
|
||||||
|
@ -331,11 +333,4 @@ EllipsisType = type(Ellipsis)
|
||||||
NoneType = type(None)
|
NoneType = type(None)
|
||||||
NotImplementedType = type(NotImplemented)
|
NotImplementedType = type(NotImplemented)
|
||||||
|
|
||||||
def __getattr__(name):
|
__all__ = [n for n in globals() if not n.startswith('_')] # for pydoc
|
||||||
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']
|
|
||||||
|
|
|
@ -150,6 +150,7 @@ PYTHONPATH=$(COREPYTHONPATH)
|
||||||
#_socket socketmodule.c
|
#_socket socketmodule.c
|
||||||
#_statistics _statisticsmodule.c
|
#_statistics _statisticsmodule.c
|
||||||
#_struct _struct.c
|
#_struct _struct.c
|
||||||
|
#_types _typesmodule.c
|
||||||
#_typing _typingmodule.c
|
#_typing _typingmodule.c
|
||||||
#_zoneinfo _zoneinfo.c
|
#_zoneinfo _zoneinfo.c
|
||||||
#array arraymodule.c
|
#array arraymodule.c
|
||||||
|
|
|
@ -23,6 +23,7 @@ _sre _sre/sre.c
|
||||||
_sysconfig _sysconfig.c
|
_sysconfig _sysconfig.c
|
||||||
_thread _threadmodule.c
|
_thread _threadmodule.c
|
||||||
time timemodule.c
|
time timemodule.c
|
||||||
|
_types _typesmodule.c
|
||||||
_typing _typingmodule.c
|
_typing _typingmodule.c
|
||||||
_weakref _weakref.c
|
_weakref _weakref.c
|
||||||
|
|
||||||
|
|
37
Modules/_typesmodule.c
Normal file
37
Modules/_typesmodule.c
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
/* _types module */
|
||||||
|
|
||||||
|
#include "Python.h"
|
||||||
|
#include "pycore_namespace.h" // _PyNamespace_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;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct PyModuleDef_Slot _typesmodule_slots[] = {
|
||||||
|
{Py_mod_exec, _types_exec},
|
||||||
|
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
|
||||||
|
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
|
||||||
|
{0, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct PyModuleDef typesmodule = {
|
||||||
|
.m_base = PyModuleDef_HEAD_INIT,
|
||||||
|
.m_name = "_types",
|
||||||
|
.m_doc = "Define names for built-in types.",
|
||||||
|
.m_size = 0,
|
||||||
|
.m_slots = _typesmodule_slots,
|
||||||
|
};
|
||||||
|
|
||||||
|
PyMODINIT_FUNC
|
||||||
|
PyInit__types(void)
|
||||||
|
{
|
||||||
|
return PyModuleDef_Init(&typesmodule);
|
||||||
|
}
|
|
@ -19,6 +19,7 @@ extern PyObject* PyInit__operator(void);
|
||||||
extern PyObject* PyInit__signal(void);
|
extern PyObject* PyInit__signal(void);
|
||||||
extern PyObject* PyInit__statistics(void);
|
extern PyObject* PyInit__statistics(void);
|
||||||
extern PyObject* PyInit__sysconfig(void);
|
extern PyObject* PyInit__sysconfig(void);
|
||||||
|
extern PyObject* PyInit__types(void);
|
||||||
extern PyObject* PyInit__typing(void);
|
extern PyObject* PyInit__typing(void);
|
||||||
extern PyObject* PyInit_time(void);
|
extern PyObject* PyInit_time(void);
|
||||||
extern PyObject* PyInit__thread(void);
|
extern PyObject* PyInit__thread(void);
|
||||||
|
@ -107,6 +108,7 @@ struct _inittab _PyImport_Inittab[] = {
|
||||||
{"time", PyInit_time},
|
{"time", PyInit_time},
|
||||||
{"_thread", PyInit__thread},
|
{"_thread", PyInit__thread},
|
||||||
{"_tokenize", PyInit__tokenize},
|
{"_tokenize", PyInit__tokenize},
|
||||||
|
{"_types", PyInit__types},
|
||||||
{"_typing", PyInit__typing},
|
{"_typing", PyInit__typing},
|
||||||
{"_statistics", PyInit__statistics},
|
{"_statistics", PyInit__statistics},
|
||||||
|
|
||||||
|
|
|
@ -485,6 +485,7 @@
|
||||||
<ClCompile Include="..\Modules\_sysconfig.c" />
|
<ClCompile Include="..\Modules\_sysconfig.c" />
|
||||||
<ClCompile Include="..\Modules\_threadmodule.c" />
|
<ClCompile Include="..\Modules\_threadmodule.c" />
|
||||||
<ClCompile Include="..\Modules\_tracemalloc.c" />
|
<ClCompile Include="..\Modules\_tracemalloc.c" />
|
||||||
|
<ClCompile Include="..\Modules\_typesmodule.c" />
|
||||||
<ClCompile Include="..\Modules\_typingmodule.c" />
|
<ClCompile Include="..\Modules\_typingmodule.c" />
|
||||||
<ClCompile Include="..\Modules\timemodule.c" />
|
<ClCompile Include="..\Modules\timemodule.c" />
|
||||||
<ClCompile Include="..\Modules\xxsubtype.c" />
|
<ClCompile Include="..\Modules\xxsubtype.c" />
|
||||||
|
|
|
@ -998,6 +998,9 @@
|
||||||
<ClCompile Include="..\Modules\_statisticsmodule.c">
|
<ClCompile Include="..\Modules\_statisticsmodule.c">
|
||||||
<Filter>Modules</Filter>
|
<Filter>Modules</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\Modules\_typesmodule.c">
|
||||||
|
<Filter>Modules</Filter>
|
||||||
|
</ClCompile>
|
||||||
<ClCompile Include="..\Modules\_typingmodule.c">
|
<ClCompile Include="..\Modules\_typingmodule.c">
|
||||||
<Filter>Modules</Filter>
|
<Filter>Modules</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
|
1
Python/stdlib_module_names.h
generated
1
Python/stdlib_module_names.h
generated
|
@ -94,6 +94,7 @@ static const char* _Py_stdlib_module_names[] = {
|
||||||
"_tkinter",
|
"_tkinter",
|
||||||
"_tokenize",
|
"_tokenize",
|
||||||
"_tracemalloc",
|
"_tracemalloc",
|
||||||
|
"_types",
|
||||||
"_typing",
|
"_typing",
|
||||||
"_uuid",
|
"_uuid",
|
||||||
"_warnings",
|
"_warnings",
|
||||||
|
|
28
configure
generated
vendored
28
configure
generated
vendored
|
@ -783,6 +783,8 @@ MODULE__INTERPRETERS_FALSE
|
||||||
MODULE__INTERPRETERS_TRUE
|
MODULE__INTERPRETERS_TRUE
|
||||||
MODULE__TYPING_FALSE
|
MODULE__TYPING_FALSE
|
||||||
MODULE__TYPING_TRUE
|
MODULE__TYPING_TRUE
|
||||||
|
MODULE__TYPES_FALSE
|
||||||
|
MODULE__TYPES_TRUE
|
||||||
MODULE__STRUCT_FALSE
|
MODULE__STRUCT_FALSE
|
||||||
MODULE__STRUCT_TRUE
|
MODULE__STRUCT_TRUE
|
||||||
MODULE_SELECT_FALSE
|
MODULE_SELECT_FALSE
|
||||||
|
@ -31008,6 +31010,28 @@ then :
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
if test "$py_cv_module__types" != "n/a"
|
||||||
|
then :
|
||||||
|
py_cv_module__types=yes
|
||||||
|
fi
|
||||||
|
if test "$py_cv_module__types" = yes; then
|
||||||
|
MODULE__TYPES_TRUE=
|
||||||
|
MODULE__TYPES_FALSE='#'
|
||||||
|
else
|
||||||
|
MODULE__TYPES_TRUE='#'
|
||||||
|
MODULE__TYPES_FALSE=
|
||||||
|
fi
|
||||||
|
|
||||||
|
as_fn_append MODULE_BLOCK "MODULE__TYPES_STATE=$py_cv_module__types$as_nl"
|
||||||
|
if test "x$py_cv_module__types" = xyes
|
||||||
|
then :
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
@ -33723,6 +33747,10 @@ if test -z "${MODULE__STRUCT_TRUE}" && test -z "${MODULE__STRUCT_FALSE}"; then
|
||||||
as_fn_error $? "conditional \"MODULE__STRUCT\" was never defined.
|
as_fn_error $? "conditional \"MODULE__STRUCT\" was never defined.
|
||||||
Usually this means the macro was only invoked conditionally." "$LINENO" 5
|
Usually this means the macro was only invoked conditionally." "$LINENO" 5
|
||||||
fi
|
fi
|
||||||
|
if test -z "${MODULE__TYPES_TRUE}" && test -z "${MODULE__TYPES_FALSE}"; then
|
||||||
|
as_fn_error $? "conditional \"MODULE__TYPES\" was never defined.
|
||||||
|
Usually this means the macro was only invoked conditionally." "$LINENO" 5
|
||||||
|
fi
|
||||||
if test -z "${MODULE__TYPING_TRUE}" && test -z "${MODULE__TYPING_FALSE}"; then
|
if test -z "${MODULE__TYPING_TRUE}" && test -z "${MODULE__TYPING_FALSE}"; then
|
||||||
as_fn_error $? "conditional \"MODULE__TYPING\" was never defined.
|
as_fn_error $? "conditional \"MODULE__TYPING\" was never defined.
|
||||||
Usually this means the macro was only invoked conditionally." "$LINENO" 5
|
Usually this means the macro was only invoked conditionally." "$LINENO" 5
|
||||||
|
|
|
@ -7787,6 +7787,7 @@ PY_STDLIB_MOD_SIMPLE([_queue])
|
||||||
PY_STDLIB_MOD_SIMPLE([_random])
|
PY_STDLIB_MOD_SIMPLE([_random])
|
||||||
PY_STDLIB_MOD_SIMPLE([select])
|
PY_STDLIB_MOD_SIMPLE([select])
|
||||||
PY_STDLIB_MOD_SIMPLE([_struct])
|
PY_STDLIB_MOD_SIMPLE([_struct])
|
||||||
|
PY_STDLIB_MOD_SIMPLE([_types])
|
||||||
PY_STDLIB_MOD_SIMPLE([_typing])
|
PY_STDLIB_MOD_SIMPLE([_typing])
|
||||||
PY_STDLIB_MOD_SIMPLE([_interpreters])
|
PY_STDLIB_MOD_SIMPLE([_interpreters])
|
||||||
PY_STDLIB_MOD_SIMPLE([_interpchannels])
|
PY_STDLIB_MOD_SIMPLE([_interpchannels])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue