8000 bpo-41073: PyType_GetSlot() can now accept static types. by shihai1991 · Pull Request #21931 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-41073: PyType_GetSlot() can now accept static types. #21931

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

Merged
merged 27 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
25e6a1b
PyType_GetSlot() could accept static types
shihai1991 Jul 8, 2020
7fcf9eb
update docs' style
shihai1991 Jul 8, 2020
159fc09
use typeslots not in limit apis
shihai1991 Jul 9, 2020
588a847
add test cases to against PyType_GetSlot()
shihai1991 Aug 20, 2020
ec0348d
update typeslots.h
shihai1991 Aug 20, 2020
abda762
Add generate statictypeslots feature
shihai1991 Aug 22, 2020
6d94381
revert typeslots.inc to master base
shihai1991 Aug 23, 2020
eb3907d
update get static type slots
shihai1991 Sep 2, 2020
7537c86
Merge remote-tracking branch 'origin/master' into bpo_41073_add_types…
shihai1991 Sep 5, 2020
fe9f858
Fix compile error
shihai1991 Sep 5, 2020
93d849c
Use petr's idea
shihai1991 Oct 20, 2020
5171eb7
apply petr's comment
shihai1991 Oct 25, 2020
b3f992a
Merge remote-tracking branch 'origin/master' into bpo_41073_add_types…
shihai1991 Oct 29, 2020
2a6e0f4
update release doc
shihai1991 Oct 29, 2020
a9f66f3
remove new adding slots
shihai1991 Oct 30, 2020
c1844e3
apply petr's comment
shihai1991 Nov 1, 2020
ccdd9b6
Call test_get_statictype_slots in the test suite
encukou Nov 3, 2020
9194565
Raise exception on the invalid slot 0
encukou Nov 3, 2020
708cd82
Use if rather than assert in tests
encukou Nov 3, 2020
b351b2c
Add/adjust comments & news entries
encukou Nov 3, 2020
2df9180
Align method definitions in _testcapi
encukou Nov 3, 2020
2b077e3
Rewrite the slot-setting code a bit
encukou Nov 3, 2020
943174b
Remove the loop in PyType_GetSlot; we already know the position
encukou Nov 3, 2020
2d8b57f
typeslots.py: Add back code writing null entries
encukou Nov 3, 2020
2d72953
Merge remote-tracking branch 'origin/master' into bpo_41073_add_types…
shihai1991 Nov 7, 2020
6a688d9
Remove redundant test function
encukou Nov 10, 2020
fc2c06b
Merge master branch; regenerate typeslots.inc
encukou Nov 10, 2020
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
5 changes: 3 additions & 2 deletions Doc/c-api/type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ Type Objects

See :c:member:`PyType_Slot.slot` for possible values of the *slot* argument.

An exception is raised if *type* is not a heap type.

.. versionadded:: 3.4

.. versionchanged:: 3.10
:c:func:`PyType_GetSlot` could accept static types.

.. c:function:: PyObject* PyType_GetModule(PyTypeObject *type)

Return the module object associated with the given type when the type was
Expand Down
40 changes: 40 additions & 0 deletions Include/typeslots.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,43 @@
/* New in 3.5 */
#define Py_tp_finalize 80
#endif
/* New in 3.10 */
#if defined(Py_LIMITED_API)
#undef Py_tp_as_async
#undef Py_tp_as_buffer
#undef Py_tp_as_mapping
#undef Py_tp_as_number
#undef Py_tp_as_sequence
#undef Py_tp_basicsize
#undef Py_tp_cache
#undef Py_tp_dict
#undef Py_tp_dictoffset
#undef Py_tp_flags
#undef Py_tp_itemsize
#undef Py_tp_mro
#undef Py_tp_name
#undef Py_tp_subclasses
#undef Py_tp_vectorcall_offset
#undef Py_tp_version_tag
#undef Py_tp_weaklist
#undef Py_tp_weaklistoffset
#else
#define Py_tp_as_async 81
#define Py_tp_as_buffer 82
#define Py_tp_as_mapping 83
#define Py_tp_as_number 84
#define Py_tp_as_sequence 85
#define Py_tp_basicsize 86
#define Py_tp_cache 87
#define Py_tp_dict 88
#define Py_tp_dictoffset 89
#define Py_tp_flags 90
#define Py_tp_itemsize 91
#define Py_tp_mro 92
#define Py_tp_name 93
#define Py_tp_subclasses 94
#define Py_tp_vectorcall_offset 95
#define Py_tp_version_tag 96
#define Py_tp_weaklist 97
#define Py_tp_weaklistoffset 98
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:c:func:`PyType_GetSlot()` could accept static types.
30 changes: 30 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,35 @@ test_buildvalue_N(PyObject *self, PyObject *Py_UNUSED(ignored))
}


static PyObject *
test_get_statictype_slots(PyObject *self, PyObject *Py_UNUSED(ignored))
{
char *tp_name = PyType_GetSlot(&PyLong_Type, Py_tp_name);
assert(strcmp(tp_name, "int") == 0);

newfunc tp_new = PyType_GetSlot(&PyLong_Type, Py_tp_new);
PyObject *args = PyTuple_New(0);
PyObject *object = tp_new(&PyLong_Type, args, NULL);
assert(object);

reprfunc tp_repr = PyType_GetSlot(&PyLong_Type, Py_tp_repr);
PyObject *decimal_str = tp_repr(object);
assert(decimal_str);

PyNumberMethods *tp_as_number = PyType_GetSlot(&PyLong_Type,
Py_tp_as_number);
PyObject *object2 = tp_new(&PyLong_Type, args, NULL);
PyObject *res = tp_as_number->nb_add(object, object2);
assert(res);

Py_DECREF(res);
Py_DECREF(decimal_str);
Py_DECREF(args);
Py_DECREF(object);
Py_RETURN_NONE;
}


static PyObject *
get_args(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -5606,6 +5635,7 @@ static PyMethodDef TestMethods[] = {
{"test_buildvalue_N", test_buildvalue_N, METH_NOARGS},
{"test_buildvalue_issue38913", test_buildvalue_issue38913, METH_NOARGS},
{"get_args", get_args, METH_VARARGS},
{"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS},
{"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs, METH_VARARGS|METH_KEYWORDS},
{"getargs_tuple", getargs_tuple, METH_VARARGS},
{"getargs_keywords", (PyCFunction)(void(*)(void))getargs_keywords,
Expand Down
2 changes: 1 addition & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3110,7 +3110,7 @@ PyType_FromSpec(PyType_Spec *spec)
void *
PyType_GetSlot(PyTypeObject *type, int slot)
{
if (!_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE) || slot < 0) {
if (slot < 0) {
PyErr_BadInternalCall();
return NULL;
}
Expand Down
18 changes: 18 additions & 0 deletions Objects/typeslots.inc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0