-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
gh-145497: Compute static builtin type count at compile time via X-macro #151004
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
Open
corona10
wants to merge
1
commit into
python:main
Choose a base branch
from
corona10:gh-145497
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,297 @@ | ||
| #ifndef Py_INTERNAL_STATIC_BUILTIN_TYPES_H | ||
| #define Py_INTERNAL_STATIC_BUILTIN_TYPES_H | ||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| #ifndef Py_BUILD_CORE | ||
| # error "this header requires Py_BUILD_CORE define" | ||
| #endif | ||
|
|
||
| /* Single source of truth for every static builtin type registered at | ||
| startup, partitioned by where it is registered. Adding a | ||
| registration anywhere in the tree requires adding an entry to the | ||
| matching list; _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES then adjusts at | ||
| compile time (gh-145497). */ | ||
|
|
||
|
|
||
| #ifdef _Py_TIER2 | ||
| # define _Py_FOREACH_TIER2_PREINIT_TYPE(TYPE) TYPE(_PyUOpExecutor_Type) | ||
| #else | ||
| # define _Py_FOREACH_TIER2_PREINIT_TYPE(TYPE) | ||
| #endif | ||
|
|
||
| /* Registered via static_types[] in Objects/object.c. | ||
| Order matters: base types first; trailing subclasses last so | ||
| _PyTypes_FiniTypes() tears them down before their base. */ | ||
| #define _Py_FOREACH_STATIC_PREINIT_TYPE(TYPE) \ | ||
| TYPE(PyBaseObject_Type) \ | ||
| TYPE(PyType_Type) \ | ||
| TYPE(PyStaticMethod_Type) \ | ||
| TYPE(PyCFunction_Type) \ | ||
| \ | ||
| TYPE(PyAsyncGen_Type) \ | ||
| TYPE(PyByteArrayIter_Type) \ | ||
| TYPE(PyByteArray_Type) \ | ||
| TYPE(PyBytesIter_Type) \ | ||
| TYPE(PyBytes_Type) \ | ||
| TYPE(PyCallIter_Type) \ | ||
| TYPE(PyCapsule_Type) \ | ||
| TYPE(PyCell_Type) \ | ||
| TYPE(PyClassMethodDescr_Type) \ | ||
| TYPE(PyClassMethod_Type) \ | ||
| TYPE(PyCode_Type) \ | ||
| TYPE(PyComplex_Type) \ | ||
| TYPE(PyContextToken_Type) \ | ||
| TYPE(PyContextVar_Type) \ | ||
| TYPE(PyContext_Type) \ | ||
| TYPE(PyCoro_Type) \ | ||
| TYPE(PyDictItems_Type) \ | ||
| TYPE(PyDictIterItem_Type) \ | ||
| TYPE(PyDictIterKey_Type) \ | ||
| TYPE(PyDictIterValue_Type) \ | ||
| TYPE(PyDictKeys_Type) \ | ||
| TYPE(PyDictProxy_Type) \ | ||
| TYPE(PyDictRevIterItem_Type) \ | ||
| TYPE(PyDictRevIterKey_Type) \ | ||
| TYPE(PyDictRevIterValue_Type) \ | ||
| TYPE(PyDictValues_Type) \ | ||
| TYPE(PyDict_Type) \ | ||
| TYPE(PyEllipsis_Type) \ | ||
| TYPE(PyEnum_Type) \ | ||
| TYPE(PyFilter_Type) \ | ||
| TYPE(PyFloat_Type) \ | ||
| TYPE(PyFrameLocalsProxy_Type) \ | ||
| TYPE(PyFrame_Type) \ | ||
| TYPE(PyFrozenDict_Type) \ | ||
| TYPE(PyFrozenSet_Type) \ | ||
| TYPE(PyFunction_Type) \ | ||
| TYPE(PyGen_Type) \ | ||
| TYPE(PyGetSetDescr_Type) \ | ||
| TYPE(PyInstanceMethod_Type) \ | ||
| TYPE(PyLazyImport_Type) \ | ||
| TYPE(PyListIter_Type) \ | ||
| TYPE(PyListRevIter_Type) \ | ||
| TYPE(PyList_Type) \ | ||
| TYPE(PyLongRangeIter_Type) \ | ||
| TYPE(PyLong_Type) \ | ||
| TYPE(PyMap_Type) \ | ||
| TYPE(PyMemberDescr_Type) \ | ||
| TYPE(PyMemoryView_Type) \ | ||
| TYPE(PyMethodDescr_Type) \ | ||
| TYPE(PyMethod_Type) \ | ||
| TYPE(PyModuleDef_Type) \ | ||
| TYPE(PyModule_Type) \ | ||
| TYPE(PyODictIter_Type) \ | ||
| TYPE(PyPickleBuffer_Type) \ | ||
| TYPE(PyProperty_Type) \ | ||
| TYPE(PyRangeIter_Type) \ | ||
| TYPE(PyRange_Type) \ | ||
| TYPE(PyReversed_Type) \ | ||
| TYPE(PySTEntry_Type) \ | ||
| TYPE(PySentinel_Type) \ | ||
| TYPE(PySeqIter_Type) \ | ||
| TYPE(PySetIter_Type) \ | ||
| TYPE(PySet_Type) \ | ||
| TYPE(PySlice_Type) \ | ||
| TYPE(PyStdPrinter_Type) \ | ||
| TYPE(PySuper_Type) \ | ||
| TYPE(PyTraceBack_Type) \ | ||
| TYPE(PyTupleIter_Type) \ | ||
| TYPE(PyTuple_Type) \ | ||
| TYPE(PyUnicodeIter_Type) \ | ||
| TYPE(PyUnicode_Type) \ | ||
| TYPE(PyWrapperDescr_Type) \ | ||
| TYPE(PyZip_Type) \ | ||
| TYPE(Py_GenericAliasType) \ | ||
| TYPE(_PyAnextAwaitable_Type) \ | ||
| TYPE(_PyAsyncGenASend_Type) \ | ||
| TYPE(_PyAsyncGenAThrow_Type) \ | ||
| TYPE(_PyAsyncGenWrappedValue_Type) \ | ||
| TYPE(_PyBufferWrapper_Type) \ | ||
| TYPE(_PyContextTokenMissing_Type) \ | ||
| TYPE(_PyCoroWrapper_Type) \ | ||
| TYPE(_Py_GenericAliasIterType) \ | ||
| TYPE(_PyHamtItems_Type) \ | ||
| TYPE(_PyHamtKeys_Type) \ | ||
| TYPE(_PyHamtValues_Type) \ | ||
| TYPE(_PyHamt_ArrayNode_Type) \ | ||
| TYPE(_PyHamt_BitmapNode_Type) \ | ||
| TYPE(_PyHamt_CollisionNode_Type) \ | ||
| TYPE(_PyHamt_Type) \ | ||
| TYPE(_PyInstructionSequence_Type) \ | ||
| TYPE(_PyInterpolation_Type) \ | ||
| TYPE(_PyLegacyEventHandler_Type) \ | ||
| TYPE(_PyLineIterator) \ | ||
| TYPE(_PyManagedBuffer_Type) \ | ||
| TYPE(_PyMemoryIter_Type) \ | ||
| TYPE(_PyMethodWrapper_Type) \ | ||
| TYPE(_PyNamespace_Type) \ | ||
| TYPE(_PyNone_Type) \ | ||
| TYPE(_PyNotImplemented_Type) \ | ||
| TYPE(_PyPositionsIterator) \ | ||
| TYPE(_PyTemplate_Type) \ | ||
| TYPE(_PyTemplateIter_Type) \ | ||
| TYPE(_PyUnicodeASCIIIter_Type) \ | ||
| TYPE(_PyUnion_Type) \ | ||
| _Py_FOREACH_TIER2_PREINIT_TYPE(TYPE) \ | ||
| TYPE(_PyWeakref_CallableProxyType) \ | ||
| TYPE(_PyWeakref_ProxyType) \ | ||
| TYPE(_PyWeakref_RefType) \ | ||
| TYPE(_PyTypeAlias_Type) \ | ||
| TYPE(_PyNoDefault_Type) \ | ||
| \ | ||
| TYPE(PyBool_Type) \ | ||
| TYPE(PyCMethod_Type) \ | ||
| TYPE(PyODictItems_Type) \ | ||
| TYPE(PyODictKeys_Type) \ | ||
| TYPE(PyODictValues_Type) \ | ||
| TYPE(PyODict_Type) | ||
|
|
||
|
|
||
| /* Registered via static_exceptions[] in Objects/exceptions.c. | ||
| NAME is the suffix; the real type is &_PyExc_<NAME>. */ | ||
| #define _Py_FOREACH_STATIC_EXCEPTION_TYPE(EXC) \ | ||
| EXC(BaseException) \ | ||
| \ | ||
| EXC(BaseExceptionGroup) \ | ||
| EXC(Exception) \ | ||
| EXC(GeneratorExit) \ | ||
| EXC(KeyboardInterrupt) \ | ||
| EXC(SystemExit) \ | ||
| \ | ||
| EXC(ArithmeticError) \ | ||
| EXC(AssertionError) \ | ||
| EXC(AttributeError) \ | ||
| EXC(BufferError) \ | ||
| EXC(EOFError) \ | ||
| EXC(ImportError) \ | ||
| EXC(LookupError) \ | ||
| EXC(MemoryError) \ | ||
| EXC(NameError) \ | ||
| EXC(OSError) \ | ||
| EXC(ReferenceError) \ | ||
| EXC(RuntimeError) \ | ||
| EXC(StopAsyncIteration) \ | ||
| EXC(StopIteration) \ | ||
| EXC(SyntaxError) \ | ||
| EXC(SystemError) \ | ||
| EXC(TypeError) \ | ||
| EXC(ValueError) \ | ||
| EXC(Warning) \ | ||
| \ | ||
| EXC(FloatingPointError) \ | ||
| EXC(OverflowError) \ | ||
| EXC(ZeroDivisionError) \ | ||
| \ | ||
| EXC(BytesWarning) \ | ||
| EXC(DeprecationWarning) \ | ||
| EXC(EncodingWarning) \ | ||
| EXC(FutureWarning) \ | ||
| EXC(ImportWarning) \ | ||
| EXC(PendingDeprecationWarning) \ | ||
| EXC(ResourceWarning) \ | ||
| EXC(RuntimeWarning) \ | ||
| EXC(SyntaxWarning) \ | ||
| EXC(UnicodeWarning) \ | ||
| EXC(UserWarning) \ | ||
| \ | ||
| EXC(BlockingIOError) \ | ||
| EXC(ChildProcessError) \ | ||
| EXC(ConnectionError) \ | ||
| EXC(FileExistsError) \ | ||
| EXC(FileNotFoundError) \ | ||
| EXC(InterruptedError) \ | ||
| EXC(IsADirectoryError) \ | ||
| EXC(NotADirectoryError) \ | ||
| EXC(PermissionError) \ | ||
| EXC(ProcessLookupError) \ | ||
| EXC(TimeoutError) \ | ||
| \ | ||
| EXC(IndentationError) \ | ||
| EXC(IndexError) \ | ||
| EXC(KeyError) \ | ||
| EXC(ImportCycleError) \ | ||
| EXC(ModuleNotFoundError) \ | ||
| EXC(NotImplementedError) \ | ||
| EXC(PythonFinalizationError) \ | ||
| EXC(RecursionError) \ | ||
| EXC(UnboundLocalError) \ | ||
| EXC(UnicodeError) \ | ||
| \ | ||
| EXC(BrokenPipeError) \ | ||
| EXC(ConnectionAbortedError) \ | ||
| EXC(ConnectionRefusedError) \ | ||
| EXC(ConnectionResetError) \ | ||
| \ | ||
| EXC(TabError) \ | ||
| \ | ||
| EXC(UnicodeDecodeError) \ | ||
| EXC(UnicodeEncodeError) \ | ||
| EXC(UnicodeTranslateError) | ||
|
|
||
|
|
||
| /* Static exceptions whose exposed name differs from the symbol's | ||
| suffix after `_PyExc_`. Each entry is (NAME, "exposed name"). */ | ||
| #define _Py_FOREACH_RENAMED_EXCEPTION_TYPE(EXC) \ | ||
| EXC(IncompleteInputError, "_IncompleteInputError") | ||
|
|
||
|
|
||
| /* One TYPE(name) per textual _PyStaticType_InitBuiltin / | ||
| _PyStructSequence_InitBuiltin{,WithFlags} call site outside the two | ||
| arrays above. Count-only -- the calls themselves stay at their | ||
| existing locations. Forgetting an entry trips the | ||
| `index < _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES` assertion in | ||
| Objects/typeobject.c on interpreter start. */ | ||
| #define _Py_FOREACH_STATIC_EXTRA_TYPE(TYPE) \ | ||
| /* Python/crossinterp_exceptions.h */ \ | ||
| TYPE(_PyExc_InterpreterError) \ | ||
| TYPE(_PyExc_InterpreterNotFoundError) \ | ||
| /* Objects/unicodeobject.c */ \ | ||
| TYPE(EncodingMapType) \ | ||
| TYPE(PyFieldNameIter_Type) \ | ||
| TYPE(PyFormatterIter_Type) \ | ||
| /* Python/thread.c */ \ | ||
| TYPE(ThreadInfoType) \ | ||
| /* Python/sysmodule.c */ \ | ||
| TYPE(Hash_InfoType) \ | ||
| TYPE(AsyncGenHooksType) \ | ||
| TYPE(VersionInfoType) \ | ||
| TYPE(FlagsType) \ | ||
| TYPE(WindowsVersionType) \ | ||
| /* Python/errors.c */ \ | ||
| TYPE(UnraisableHookArgsType) \ | ||
| /* Objects/longobject.c */ \ | ||
| TYPE(Int_InfoType) \ | ||
| /* Objects/floatobject.c */ \ | ||
| TYPE(FloatInfoType) | ||
|
|
||
|
|
||
| /* Variadic so it accepts both single-arg X(name) and two-arg X(name, str). */ | ||
| #define _PY_COUNT_STATIC_TYPE_(...) + 1 | ||
|
|
||
| #define _Py_NUM_MANAGED_PREINITIALIZED_TYPES \ | ||
| (0 _Py_FOREACH_STATIC_PREINIT_TYPE(_PY_COUNT_STATIC_TYPE_)) | ||
|
|
||
| #define _Py_NUM_MANAGED_STATIC_EXCEPTION_TYPES \ | ||
| (0 _Py_FOREACH_STATIC_EXCEPTION_TYPE(_PY_COUNT_STATIC_TYPE_) \ | ||
| _Py_FOREACH_RENAMED_EXCEPTION_TYPE(_PY_COUNT_STATIC_TYPE_)) | ||
|
|
||
| #define _Py_NUM_MANAGED_STATIC_EXTRA_TYPES \ | ||
| (0 _Py_FOREACH_STATIC_EXTRA_TYPE(_PY_COUNT_STATIC_TYPE_)) | ||
|
|
||
| #define _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES \ | ||
| (_Py_NUM_MANAGED_PREINITIALIZED_TYPES + \ | ||
| _Py_NUM_MANAGED_STATIC_EXCEPTION_TYPES + \ | ||
| _Py_NUM_MANAGED_STATIC_EXTRA_TYPES) | ||
|
|
||
| #define _Py_MAX_MANAGED_STATIC_EXT_TYPES 10 | ||
|
|
||
| #define _Py_MAX_MANAGED_STATIC_TYPES \ | ||
| (_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES + _Py_MAX_MANAGED_STATIC_EXT_TYPES) | ||
|
|
||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
| #endif /* !Py_INTERNAL_STATIC_BUILTIN_TYPES_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core_and_Builtins/2026-06-06-17-02-39.gh-issue-145497.FPE2LI.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Compute static builtin type count at compile time. Patch by Donghee Na. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a huge fan of moving all static types to their own header file. I think it makes it harder to track down where static types are being defined, because it separates the code from the definition.