Skip to content

gh-149044: Implement PEP 820 – PySlot: Unified slot system for the C API#149055

Open
encukou wants to merge 48 commits intopython:mainfrom
encukou:slots-pep820-mrg
Open

gh-149044: Implement PEP 820 – PySlot: Unified slot system for the C API#149055
encukou wants to merge 48 commits intopython:mainfrom
encukou:slots-pep820-mrg

Conversation

@encukou
Copy link
Copy Markdown
Member

@encukou encukou commented Apr 27, 2026

Copy link
Copy Markdown
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation looks pretty good. I haven't looked at all of the C code yet.

Comment thread Doc/c-api/slots.rst Outdated
Comment thread Doc/c-api/slots.rst Outdated
@AlexWaygood AlexWaygood removed their request for review April 28, 2026 14:06
@brettcannon brettcannon removed their request for review April 28, 2026 16:44
Comment thread Doc/c-api/slots.rst Outdated
encukou and others added 2 commits April 30, 2026 12:07
@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented Apr 30, 2026

Documentation build overview

📚 cpython-previews | 🛠️ Build #32521677 | 📁 Comparing bef20e9 against main (a386a52)

  🔍 Preview build  

72 files changed · + 2 added · ± 70 modified

+ Added

± Modified

Copy link
Copy Markdown
Contributor

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote FFI bindings for this PR for PyO3 this afternoon and spotted several minor issues.

Comment thread Include/slots.h
Comment on lines +48 to +52
#define PySlot_PTR(NAME, VALUE) \
{(NAME), PySlot_INTPTR, {0}, {(void*)(VALUE)}}

#define PySlot_PTR_STATIC(NAME, VALUE) \
{(NAME), PySlot_INTPTR | PySlot_STATIC, {0}, {(void*)(VALUE)}}
Copy link
Copy Markdown
Contributor

@ngoldbaum ngoldbaum May 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you spell these last two with the explicit names of the struct fields like the ones above? Also while you're touching this code, IMO these two definitions should come before PySlot_END.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These macros are for code compatible with C++11 and below, which doesn't support designated initializers (that is, the explicit names).

So, both style issues are intentional :) I added a comment.

Comment thread Include/slots.h Outdated

#define PySlot_OPTIONAL 0x01
#define PySlot_STATIC 0x02
#define PySlot_INTPTR 0x04
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: since these are u16 constants, consider typsetting them as e.g. 0x0001 like you did for Py_slot_invalid below

Comment thread Include/slots.h
#define PySlot_STATIC 0x02
#define PySlot_INTPTR 0x04

#define Py_slot_invalid 0xffff
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for consistency should this be called Py_slot_INVALID maybe?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be consistent with the wrong thing. Slot IDs are named Py_tp_repr, Py_mod_exec, etc. Flags are a new thing, so they follow the current style guide: mixed prefix + uppercase.

Comment thread Include/slots_generated.h
#define _Py_SLOT_COMPAT_VALUE(OLD, NEW) OLD
#endif

#define Py_slot_end 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth adding a comment above these that they're all u16 values?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In C, the preprocessor macros are just numbers :)
In PyType_Slot, these values are used as int.

Comment thread Include/object.h Outdated
#define Py_TP_USE_SPEC NULL
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 15)
PyAPI_FUNC(PyObject *) PyType_FromSlots(struct PySlot *);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing the argument name, the docs call it slots.

Comment thread Include/slots_generated.h
#define Py_tp_finalize 80
#define Py_am_send 81
#define Py_tp_vectorcall 82
#define Py_tp_token 83
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything through here is duplicated with typeslots.h. Was that intentional? It probably makes sense for there to be only one source of truth for these constants.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch! I seem to have dropped the commit that removes the unused file.

Comment thread Include/slots.h
#ifndef _Py_HAVE_SLOTS_H
#define _Py_HAVE_SLOTS_H

typedef void (*_Py_funcptr_t)(void);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyO3 needs to wrap this type directly so you might as well give this a public non-underscored name.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can PyO3 use the underlying type, or better yet, some “generic C function pointer” type?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants