Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Include/internal/pycore_uop_ids.h generated
Include/internal/pycore_uop_metadata.h generated
Include/opcode.h generated
Include/opcode_ids.h generated
Include/slots_generated.h generated
Include/token.h generated
Lib/_opcode_metadata.py generated
Lib/idlelib/help.html generated
Expand All @@ -94,7 +95,6 @@ Lib/test/test_stable_abi_ctypes.py generated
Lib/test/test_zoneinfo/data/*.json generated
Lib/token.py generated
Misc/sbom.spdx.json generated
Objects/typeslots.inc generated
PC/python3dll.c generated
Parser/parser.c generated
Parser/token.c generated
Expand All @@ -104,6 +104,7 @@ Python/executor_cases.c.h generated
Python/generated_cases.c.h generated
Python/optimizer_cases.c.h generated
Python/opcode_targets.h generated
Python/slots_generated.c generated
Python/stdlib_module_names.h generated
Tools/peg_generator/pegen/grammar_parser.py generated
aclocal.m4 generated
Expand Down
3 changes: 2 additions & 1 deletion Include/Python.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ __pragma(warning(disable: 4201))
#include "object.h"
#include "refcount.h"
#include "objimpl.h"
#include "typeslots.h"
#include "slots.h"
#include "slots_generated.h"
#include "pyhash.h"
#include "cpython/pydebug.h"
#include "bytearrayobject.h"
Expand Down
2 changes: 1 addition & 1 deletion Include/exports.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
#define PyMODINIT_FUNC _PyINIT_FUNC_DECLSPEC PyObject*
#endif
#ifndef PyMODEXPORT_FUNC
#define PyMODEXPORT_FUNC _PyINIT_FUNC_DECLSPEC PyModuleDef_Slot*
#define PyMODEXPORT_FUNC _PyINIT_FUNC_DECLSPEC PySlot*
#endif

#endif /* Py_EXPORTS_H */
2 changes: 1 addition & 1 deletion Include/internal/pycore_importdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ extern void _Py_ext_module_loader_result_apply_error(

/* The module init function. */
typedef PyObject *(*PyModInitFunction)(void);
typedef PyModuleDef_Slot *(*PyModExportFunction)(void);
typedef PySlot *(*PyModExportFunction)(void);
#ifdef HAVE_DYNAMIC_LOADING
extern int _PyImport_GetModuleExportHooks(
struct _Py_ext_module_loader_info *info,
Expand Down
132 changes: 132 additions & 0 deletions Include/internal/pycore_slots.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#ifndef _Py_PYCORE_SLOTS_H
#define _Py_PYCORE_SLOTS_H

#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif

#include <stdbool.h>

typedef enum _PySlot_DTYPE {
_PySlot_DTYPE_VOID,
_PySlot_DTYPE_FUNC,
_PySlot_DTYPE_PTR,
_PySlot_DTYPE_SIZE,
_PySlot_DTYPE_INT64,
_PySlot_DTYPE_UINT64,
}_PySlot_DTYPE;

typedef enum _PySlot_KIND {
_PySlot_KIND_TYPE,
_PySlot_KIND_MOD,
_PySlot_KIND_COMPAT,
_PySlot_KIND_SLOT,
} _PySlot_KIND;

typedef enum _PySlot_PROBLEM_HANDLING {
_PySlot_PROBLEM_ALLOW = 0,
_PySlot_PROBLEM_DEPRECATED,
_PySlot_PROBLEM_REJECT,
} _PySlot_PROBLEM_HANDLING;

PyAPI_DATA(char *) _PySlot_names[];

#define _PySlot_MAX_NESTING 5

/* State for one nesting level of a slots iterator */
typedef struct _PySlotIterator_state {
union {
// tagged by slot_struct_kind:
const PySlot *slot; // with _PySlot_KIND_SLOT
const PyType_Slot *tp_slot; // with _PySlot_KIND_TYPE
const PyModuleDef_Slot *mod_slot; // with _PySlot_KIND_MOD
const void *any_slot;
};
_PySlot_KIND slot_struct_kind;
bool ignoring_fallbacks :1;
} _PySlotIterator_state;

/* State for a slots iterator */
typedef struct {
_PySlotIterator_state *state;
_PySlotIterator_state states[_PySlot_MAX_NESTING];
_PySlot_KIND kind;
uint8_t recursion_level;
unsigned int seen[_Py_slot_COUNT / sizeof(unsigned int) + 1];
bool is_at_end :1;
bool is_first_run :1;

// Name of the object (type/module) being defined, NULL if unknown.
// Must be set by the callers as soon as it's known.
const char *name;

/* Output information: */

// The slot. Always a copy; may be modified by caller of the iterator.
PySlot current;

} _PySlotIterator;

/* Initialize an iterator using a Py_Slot array */
PyAPI_FUNC(void)
_PySlotIterator_Init(_PySlotIterator *it, const PySlot *slots,
_PySlot_KIND result_kind);

/* Initialize an iterator using a legacy slot array */
PyAPI_FUNC(void)
_PySlotIterator_InitLegacy(_PySlotIterator *it, const void *slots,
_PySlot_KIND kind);

/* Reset a *successfully exhausted* iterator to the beginning.
* The *slots* must be the same as for the previous
* `_PySlotIterator_InitWithKind` call.
* (Subsequent iterations skip some validation.)
*/
PyAPI_FUNC(void) _PySlotIterator_Rewind(_PySlotIterator *it, const void *slots);

/* Iteration function.
*
* Return false at the end (when successfully exhausted).
* Otherwise (even on error), fill output information in `it` and return true.
*
* On error, set an exception and set `it->current.sl_id` to `Py_slot_invalid`.
*/
PyAPI_FUNC(bool) _PySlotIterator_Next(_PySlotIterator *it);

/* Return 1 if given slot was "seen" by an earlier _PySlotIterator_Next call.
* (This state is not reset by rewinding.)
*/
PyAPI_FUNC(bool) _PySlotIterator_SawSlot(_PySlotIterator *, int);

static inline const char *
_PySlot_GetName(uint16_t id)
{
if (id >= _Py_slot_COUNT) {
return "<unknown_slot>";
}
if (id == Py_slot_invalid) {
return "Py_slot_invalid";
}
return _PySlot_names[id];
}

static inline void
_PySlot_err_bad_slot(char *kind, uint16_t id)
{
if (id < _Py_slot_COUNT) {
PyErr_Format(PyExc_SystemError, "invalid %s slot %d (%s)",
kind, (unsigned int)id, _PySlot_names[id]);
}
else if (id == Py_slot_invalid) {
PyErr_Format(PyExc_SystemError, "invalid slot (Py_slot_invalid, %u)",
(unsigned int)id);
}
else {
PyErr_Format(PyExc_SystemError, "unknown %s slot ID %u",
kind, (unsigned int)id);
}
}

#include "internal/pycore_slots_generated.h"

#endif // _Py_PYCORE_SLOTS_H
Loading